|
| 1 | +#include "default_types.h" |
| 2 | +#include <igl/lipschitz_octree.h> |
| 3 | +#include <nanobind/nanobind.h> |
| 4 | +#include <nanobind/ndarray.h> |
| 5 | +#include <nanobind/eigen/dense.h> |
| 6 | +#include <nanobind/stl/tuple.h> |
| 7 | +#include <nanobind/stl/function.h> |
| 8 | + |
| 9 | +namespace nb = nanobind; |
| 10 | +using namespace nb::literals; |
| 11 | + |
| 12 | +namespace pyigl |
| 13 | +{ |
| 14 | + // Hopefully this will avoid a copy by matching the type in lipschitz_octree_prune |
| 15 | + using MatrixNX3R = Eigen::Matrix<Numeric, Eigen::Dynamic, 3, Eigen::RowMajor>; |
| 16 | + auto lipschitz_octree( |
| 17 | + const nb::DRef<const Eigen::VectorXN> &origin, |
| 18 | + const Numeric h0, |
| 19 | + const Integer max_depth, |
| 20 | + const nb::typed< |
| 21 | + nb::callable, |
| 22 | + Eigen::VectorXN(const nb::DRef<const MatrixNX3R> &)> & udf) |
| 23 | + { |
| 24 | + Eigen::MatrixXI ijk; |
| 25 | + // Instead of MatrixXN if use use MatrixNX3R we might avoid a copy |
| 26 | + const std::function<Eigen::VectorXN(const MatrixNX3R &)> udf_wrapper = |
| 27 | + [&](const MatrixNX3R &Q) -> Eigen::VectorXN |
| 28 | + { |
| 29 | + return nb::cast<Eigen::VectorXN>(udf(Q)); |
| 30 | + }; |
| 31 | + igl::lipschitz_octree<true>(origin, h0, max_depth, udf_wrapper, ijk); |
| 32 | + return ijk; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Bind the wrapper to the Python module |
| 37 | +void bind_lipschitz_octree(nb::module_ &m) |
| 38 | +{ |
| 39 | + m.def( |
| 40 | + "lipschitz_octree", |
| 41 | + &pyigl::lipschitz_octree, |
| 42 | + "origin"_a, |
| 43 | + "h0"_a, |
| 44 | + "max_depth"_a, |
| 45 | + "udf"_a, |
| 46 | +R"(Given a minimum corner position (origin) and a side length (h0) and a |
| 47 | +maximum depth (max_depth), determine the possible active leaf octree cells |
| 48 | +based on an one-Lipschitz non-negative function to a level set (e.g., |
| 49 | +"unsigned distance function"). |
| 50 | +
|
| 51 | + @param[in] origin 3-vector of root cell origin (minimum corner) |
| 52 | + @param[in] h0 side length of root cell |
| 53 | + @param[in] max_depth maximum depth of octree (root is depth=0) |
| 54 | + @param[in] udf 1-Lipschitz function of (unsigned) distance to level set to a |
| 55 | + list of batched query points |
| 56 | + @param[out] ijk #ijk by 3 list of octree leaf cell minimum corner |
| 57 | + subscripts |
| 58 | +)"); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
0 commit comments