-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #607 from HEXRD/xtensor-support
Add xtensor support, remove Eigen
- Loading branch information
Showing
5 changed files
with
99 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,8 @@ requirements: | |
- numba | ||
- pybind11 | ||
- xsimd | ||
- eigen | ||
- xtensor | ||
- xtensor-python | ||
|
||
run: | ||
- appdirs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 26 additions & 18 deletions
44
hexrd/transforms/cpp_sublibrary/src/inverse_distortion.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
#define FORCE_IMPORT_ARRAY | ||
#define XTENSOR_USE_XSIMD | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/eigen.h> | ||
#include <Eigen/Core> | ||
#include <xtensor/xmath.hpp> | ||
#include <xtensor/xarray.hpp> | ||
#include <xtensor-python/pyarray.hpp> | ||
#include <xtensor/xview.hpp> | ||
|
||
namespace py = pybind11; | ||
constexpr double FOUR_THIRDS_PI = 4.1887902; | ||
constexpr double N_THREE_HALVES_SQRT_3 = -2.59807621; | ||
constexpr double TWO_OVER_SQRT_THREE = 1.154700538; | ||
|
||
Eigen::ArrayXXd ge_41rt_inverse_distortion(const Eigen::ArrayXXd& inputs, const double rhoMax, const Eigen::ArrayXd& params) { | ||
Eigen::ArrayXd radii = inputs.matrix().rowwise().norm(); | ||
if (radii.maxCoeff() < 1e-10) { | ||
return Eigen::ArrayXXd::Zero(inputs.rows(), inputs.cols()); | ||
} | ||
Eigen::ArrayXd inverted_radii = radii.cwiseInverse(); | ||
Eigen::ArrayXd cosines = inputs.col(0) * inverted_radii; | ||
Eigen::ArrayXd cosine_double_angles = 2*cosines.square() - 1; | ||
Eigen::ArrayXd cosine_quadruple_angles = 2*cosine_double_angles.square() - 1; | ||
Eigen::ArrayXd sqrt_p_is = rhoMax / (-params[0]*cosine_double_angles - params[1]*cosine_quadruple_angles - params[2]).sqrt(); | ||
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(); | ||
Eigen::ArrayXXd results = solutions.rowwise().replicate(inputs.cols()).array() * inputs * inverted_radii.rowwise().replicate(inputs.cols()).array(); | ||
|
||
return results; | ||
xt::pyarray<double> ge_41rt_inverse_distortion(const xt::pyarray<double>& inputs, const double rhoMax, const xt::pyarray<double>& params) { | ||
auto radii = xt::sqrt(xt::sum(xt::square(inputs), {1})); | ||
|
||
if (xt::amax(radii)() < 1e-10) { | ||
return xt::zeros<double>({inputs.shape()[0], inputs.shape()[1]}); | ||
} | ||
|
||
auto inverted_radii = xt::pow(radii, -1); | ||
xt::pyarray<double> cosines = xt::view(inputs, xt::all(), 0) * inverted_radii; | ||
auto cosine_double_angles = 2 * xt::square(cosines) - 1; | ||
auto cosine_quadruple_angles = 2 * xt::square(cosine_double_angles) - 1; | ||
auto sqrt_p_is = rhoMax / xt::sqrt(-params[0] * cosine_double_angles - params[1] * cosine_quadruple_angles - params[2]); | ||
auto solutions = TWO_OVER_SQRT_THREE * sqrt_p_is * xt::cos(xt::acos(N_THREE_HALVES_SQRT_3 * radii / sqrt_p_is) / 3 + FOUR_THIRDS_PI); | ||
xt::pyarray<double> results = xt::view(solutions, xt::all(), xt::newaxis()) * inputs * xt::view(inverted_radii, xt::all(), xt::newaxis()); | ||
|
||
return results; | ||
} | ||
|
||
PYBIND11_MODULE(inverse_distortion, m) | ||
{ | ||
m.doc() = "HEXRD inverse distribution plugin"; | ||
xt::import_numpy(); | ||
m.doc() = "HEXRD inverse distribution plugin"; | ||
|
||
m.def("ge_41rt_inverse_distortion", &ge_41rt_inverse_distortion, "Inverse distortion for ge_41rt"); | ||
m.def("ge_41rt_inverse_distortion", &ge_41rt_inverse_distortion, "Inverse distortion for ge_41rt"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters