Skip to content

Commit c768f7f

Browse files
ZackAttack614kevindlewis23kpwelsh
authored
Eigen transforms (#744)
* Pull in Zack's implementation of inverse distortion * sqrt fix for mac install * Use math defines * replace constexpr with const to fix stuff * Including cmath so use_math_defines does something * Move up the math defines because eigen probably include cmath already. * Starting to port over eigen transforms * setup.py * Fixed the function I broke * Update gvec_to_xy * Update import * Update tests/transforms/test_gvec_to_xy_from_file.py Co-authored-by: Kevin Welsh <kevinwelsh132@gmail.com> * Update hexrd/transforms/cpp_sublibrary/src/transforms.cpp Co-authored-by: Kevin Welsh <kevinwelsh132@gmail.com> * Apply suggestions from code review Co-authored-by: Kevin Welsh <kevinwelsh132@gmail.com> * Apply clang-format * Change the name of the function at definition * put str here instead of pathlib Path. I think there are some versions of setuptools or perhaps windows that doesn't accept Paths --------- Co-authored-by: Kevin Lewis <kevindlewis23@gmail.com> Co-authored-by: Kevin Welsh <kevinwelsh132@gmail.com> Co-authored-by: Kevin Welsh <kevin.welsh@verdantevolution.com>
1 parent a1767dc commit c768f7f

File tree

7 files changed

+511
-40
lines changed

7 files changed

+511
-40
lines changed
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
1-
#define _USE_MATH_DEFINES
2-
#include <pybind11/pybind11.h>
3-
#include <pybind11/eigen.h>
1+
#define _USE_MATH_DEFINES
42
#include <Eigen/Core>
5-
#include <xsimd/xsimd.hpp>
63
#include <cmath>
4+
#include <pybind11/eigen.h>
5+
#include <pybind11/pybind11.h>
6+
#include <xsimd/xsimd.hpp>
77

88
namespace py = pybind11;
99
const double FOUR_THIRDS_PI = M_PI * 4.0 / 3.0;
1010
const double N_THREE_HALVES_SQRT_3 = -3.0 / 2.0 * std::sqrt(3.0);
1111
const double TWO_OVER_SQRT_THREE = 2.0 / std::sqrt(3.0);
1212

13-
Eigen::ArrayXXd ge_41rt_inverse_distortion(const Eigen::ArrayXXd& inputs, const double rhoMax, const Eigen::ArrayXd& params) {
13+
Eigen::ArrayXXd ge_41rt_inverse_distortion(const Eigen::ArrayXXd &inputs,
14+
const double rhoMax,
15+
const Eigen::ArrayXd &params) {
1416
Eigen::ArrayXd radii = inputs.matrix().rowwise().norm();
1517
Eigen::ArrayXd inverted_radii = radii.cwiseInverse();
1618
Eigen::ArrayXd cosines = inputs.col(0) * inverted_radii;
17-
Eigen::ArrayXd cosine_double_angles = 2*cosines.square() - 1;
18-
Eigen::ArrayXd cosine_quadruple_angles = 2*cosine_double_angles.square() - 1;
19-
Eigen::ArrayXd sqrt_p_is = rhoMax / (-params[0]*cosine_double_angles - params[1]*cosine_quadruple_angles - params[2]).sqrt();
20-
Eigen::ArrayXd solutions = TWO_OVER_SQRT_THREE*sqrt_p_is*(acos(N_THREE_HALVES_SQRT_3*radii/sqrt_p_is)/3 + FOUR_THIRDS_PI).cos();
21-
Eigen::ArrayXXd results = solutions.rowwise().replicate(inputs.cols()).array() * inputs * inverted_radii.rowwise().replicate(inputs.cols()).array();
19+
Eigen::ArrayXd cosine_double_angles = 2 * cosines.square() - 1;
20+
Eigen::ArrayXd cosine_quadruple_angles =
21+
2 * cosine_double_angles.square() - 1;
22+
Eigen::ArrayXd sqrt_p_is =
23+
rhoMax / (-params[0] * cosine_double_angles -
24+
params[1] * cosine_quadruple_angles - params[2])
25+
.sqrt();
26+
Eigen::ArrayXd solutions =
27+
TWO_OVER_SQRT_THREE * sqrt_p_is *
28+
(acos(N_THREE_HALVES_SQRT_3 * radii / sqrt_p_is) / 3 + FOUR_THIRDS_PI)
29+
.cos();
30+
Eigen::ArrayXXd results =
31+
solutions.rowwise().replicate(inputs.cols()).array() * inputs *
32+
inverted_radii.rowwise().replicate(inputs.cols()).array();
2233

2334
return results;
2435
}
2536

26-
PYBIND11_MODULE(inverse_distortion, m)
27-
{
28-
m.doc() = "HEXRD inverse distribution plugin";
37+
PYBIND11_MODULE(inverse_distortion, m) {
38+
m.doc() = "HEXRD inverse distribution plugin";
2939

30-
m.def("ge_41rt_inverse_distortion", &ge_41rt_inverse_distortion, "Inverse distortion for ge_41rt");
40+
m.def("ge_41rt_inverse_distortion", &ge_41rt_inverse_distortion,
41+
"Inverse distortion for ge_41rt");
3142
}

0 commit comments

Comments
 (0)