Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ endif()
FetchContent_Declare(
libigl
GIT_REPOSITORY https://github.com/libigl/libigl.git
# libigl main, including igl::lazy_cage and core igl::progressive_hulls (moved
# to core in libigl PR #2558).
GIT_TAG c07f8586733378c163aae57f08c9da2ce5352fb9
# libigl main with exact ray, closest-feature, and signed-distance queries.
GIT_TAG 4f0f2dc489c9842d2f7940022d73e24b66baaf25
${libigl_patch}
)
FetchContent_MakeAvailable(libigl)
Expand Down
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,54 @@ The original python bindings were generated and maintained by

The modern python bindings (since 2.5.4.dev0) can largely be blamed on
[@alecjacobson](https://github.com/alecjacobson).

## Exact ray and closest-point queries

The `igl.predicates` module exposes exact ray hit classification/order and
closest-feature selection. Signed distances retain an exact inside/on/outside
sign; their magnitudes and positive barycentric values are approximate.
For a closed, embedded, outward-oriented, vertex-manifold triangle mesh with
nondegenerate faces:

```python
import igl
from igl import predicates

# V: (#V, 3) vertices; F: (#F, 3) triangle indices; P: (#P, 3) query points.
B1, B2, leaf = igl.eytzinger_aabb(*igl.box_simplices(V, F))
TT, TTi = igl.triangle_triangle_adjacency(F)
exposure = predicates.exact_mesh_feature_exposure(V, F, TT, TTi)
S, I, bary = predicates.approximate_signed_distance_exact_sign(
P, V, F, B1, B2, leaf, exposure)
# The uncached overload takes TT, TTi in place of exposure.
```

`S < 0` means inside, `S > 0` outside, and `S == 0` exactly on the surface.
`I` selects a triangle containing an exact globally closest point, with the
lowest triangle index winning an exact tie. One, two, or three positive entries
in a row of `bary` identify its exact vertex, open edge, or open face. This
support and the sign of `S` survive underflow. Reconstruct approximate closest
coordinates with `np.einsum('ij,ijk->ik', bary, V[F[I]])`.

Reuse the tree and exposure for later batches; rebuild both after changing the
mesh. Coordinates use float64 (float32 inputs promote exactly); indices use
int64. Global mesh validity and cache freshness are caller preconditions.
Exact arithmetic follows the C++ predicates' finite-input, exponent-range,
round-to-nearest, and gradual-underflow contract. Extreme exponent spans can
raise an exception; distance magnitudes may overflow. The batch wrapper releases
the GIL and retains C++ parallelism; inputs must stay unchanged during a call.

For unsigned queries, `predicates.point_mesh_closest_point(q, V, F, B1, B2, leaf)`
returns `(distance, I, bary)` and supports degenerate triangles. The lower-level
`exact_point_triangle_closest_feature`,
`exact_point_triangle_squared_distance_compare`,
`exact_point_mesh_closest_triangle`, and `exact_point_mesh_classify` functions
are also available.

`predicates.exact_ray_mesh_intersect(source, dir, V, F, B1, B2, leaf)` returns the
first transverse hit's triangle index, or `-1`. Use `exact_ray_mesh_intersect_all`
for an int64 array of all hit indices in exact near-to-far order. Omitting the
tree builds one per call. Coplanar triangles are ignored and equal-distance
hit order is unspecified. `exact_ray_triangle_intersect` and
`exact_ray_triangle_hit_compare` expose the underlying predicates; the core
`igl.eytzinger_aabb_ray_intersection` accepts Python predicate callbacks.
32 changes: 32 additions & 0 deletions src/box_simplices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/box_simplices.h>
#include <stdexcept>

void bind_box_simplices(nb::module_ &m)
{
m.def("box_simplices", [](
const nb::DRef<const Eigen::MatrixXN> &V,
const nb::DRef<const Eigen::MatrixXI> &F)
{
if(V.cols()==0 || F.cols()==0 || !V.allFinite())
throw std::invalid_argument("Expected finite vertex coordinates and nonempty simplex columns");
if(F.size() && (F.minCoeff()<0 || F.maxCoeff()>=V.rows()))
throw std::out_of_range("Invalid simplex vertex index");
Eigen::MatrixXN B1,B2;
igl::box_simplices(V,F,B1,B2);
return std::make_tuple(std::move(B1),std::move(B2));
}, "V"_a, "F"_a,
R"(Compute one bounding box per simplex.

@param[in] V #V by dim finite vertex coordinates
@param[in] F #F by simplex-size vertex indices
@return (B1, B2): #F by dim minimum and maximum corners. Pass these to
eytzinger_aabb to build a reusable tree.)");
}
36 changes: 36 additions & 0 deletions src/eytzinger_aabb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/eytzinger_aabb.h>
#include <stdexcept>

void bind_eytzinger_aabb(nb::module_ &m)
{
m.def("eytzinger_aabb", [](
const nb::DRef<const Eigen::MatrixXN> &PB1,
const nb::DRef<const Eigen::MatrixXN> &PB2)
{
if(PB1.rows()!=PB2.rows() || PB1.cols()!=PB2.cols() || PB1.cols()==0 ||
!PB1.allFinite() || !PB2.allFinite() || (PB1.array()>PB2.array()).any())
throw std::invalid_argument("Expected matching finite, ordered box corners");
// The C++ builder indexes a complete tree using signed 32-bit integers.
if(PB1.rows()>(Integer(1)<<29))
throw std::invalid_argument("Too many boxes for an Eytzinger tree");
Eigen::MatrixXN B1,B2;
Eigen::VectorXI leaf;
igl::eytzinger_aabb(PB1,PB2,B1,B2,leaf);
for(Eigen::Index i=0;i<leaf.size();++i)
if(leaf(i)==-2) { B1.row(i).setZero(); B2.row(i).setZero(); }
return std::make_tuple(std::move(B1),std::move(B2),std::move(leaf));
}, "PB1"_a, "PB2"_a,
R"(Build an array-based Eytzinger AABB tree.

@param[in] PB1,PB2 #P by dim minimum and maximum primitive box corners
@return (B1, B2, leaf): tree box arrays and int64 primitive indices; -1 denotes
an internal node and -2 empty padding. Rebuild after geometry/topology changes.)");
}
39 changes: 39 additions & 0 deletions src/eytzinger_aabb_ray_intersection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/eytzinger_aabb_ray_intersection.h>
#include <nanobind/stl/function.h>

void bind_eytzinger_aabb_ray_intersection(nb::module_ &m)
{
m.def("eytzinger_aabb_ray_intersection", [](
const Eigen::Vector3d &source,const Eigen::Vector3d &dir,
const std::function<bool(int)> &hit,const std::function<bool(int,int)> &before,
const nb::DRef<const Eigen::MatrixXN> &B1,
const nb::DRef<const Eigen::MatrixXN> &B2,
const nb::DRef<const Eigen::VectorXI> &leaf,bool first)
{
std::vector<int> hits;
if(first) igl::eytzinger_aabb_ray_intersection<true>(source,dir,hit,before,B1,B2,leaf,hits);
else igl::eytzinger_aabb_ray_intersection<false>(source,dir,hit,before,B1,B2,leaf,hits);
Eigen::VectorXI ids(hits.size());
for(std::size_t i=0;i<hits.size();++i) ids(i)=hits[i];
return ids;
}, "source"_a,"dir"_a,"hit"_a,"before"_a,"B1"_a,"B2"_a,"leaf"_a,"first"_a=false,
R"(Traverse a 3D Eytzinger tree with caller-provided intersection predicates.

hit(i) reports a primitive intersection; before(i,j) orders two intersected
primitives strictly near-to-far. Conservative floating-point box culling does
not determine hit order. Python callbacks run with the GIL held.

@param[in] source,dir Ray origin and direction 3-vectors
@param[in] hit,before Intersection and strict ordering callables
@param[in] B1,B2,leaf Outputs of eytzinger_aabb
@param[in] first Return at most the closest primitive when true
@return int64 array of hit primitive indices, sorted using before.)");
}
64 changes: 64 additions & 0 deletions src/predicates/approximate_signed_distance_exact_sign.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/predicates/approximate_signed_distance_exact_sign.h>

void bind_approximate_signed_distance_exact_sign(nb::module_ &m)
{
const char *doc=R"(Compute approximate signed distances with exact closest-feature selection and sign.

Return (S, I, bary): float64 #P distances, int64 #P triangle indices, and float64
#P by 3 barycentrics. I identifies a triangle containing an exact globally
closest point; exact ties choose the lowest triangle id. The zero pattern of
bary identifies the exact closest vertex/open edge/open face (1/2/3 positive
weights). Its positive values and distance magnitudes are approximate.
Reconstruct approximate closest coordinates with
np.einsum('ij,ijk->ik', bary, V[F[I]]).

S is negative inside, positive outside, and +0.0 exactly on the surface.
Nonzero distances and active barycentric weights that underflow are replaced by
the smallest positive float64 value, retaining the exact sign/zero pattern.
Barycentrics need not sum to exactly one; distances can overflow.

P is #P by 3. V,F must form a nonempty, embedded, closed, outward-oriented,
vertex-manifold triangle mesh with nondegenerate faces. Build B1,B2,leaf with
box_simplices and eytzinger_aabb. Supply either array adjacency TT,TTi from
triangle_triangle_adjacency or a MeshFeatureExposure cache. Rebuild tree and
cache after mesh edits; global mesh validity and cache freshness are caller
preconditions. Empty P returns empty arrays. Finite inputs convert to float64
coordinates and int64 indices; extreme exponent spans may raise an exception.
The GIL is released; batches of at least 10,000 queries use libigl parallel_for.
Do not mutate inputs/cache during evaluation.)";
m.def("approximate_signed_distance_exact_sign", [](
const nb::DRef<const Eigen::MatrixXN> &P,
const nb::DRef<const Eigen::MatrixXN> &V,const nb::DRef<const Eigen::MatrixXI> &F,
const nb::DRef<const Eigen::MatrixXN> &B1,const nb::DRef<const Eigen::MatrixXN> &B2,
const nb::DRef<const Eigen::VectorXI> &leaf,const igl::predicates::MeshFeatureExposure &exposure)
{
Eigen::VectorXN S;
Eigen::VectorXI I;
Eigen::MatrixXN bary;
igl::predicates::approximate_signed_distance_exact_sign(P,V,F,B1,B2,leaf,exposure,S,I,bary);
return std::make_tuple(std::move(S),std::move(I),std::move(bary));
}, "P"_a,"V"_a,"F"_a,"B1"_a,"B2"_a,"leaf"_a,"exposure"_a,
nb::call_guard<nb::gil_scoped_release>(),doc);
m.def("approximate_signed_distance_exact_sign", [](
const nb::DRef<const Eigen::MatrixXN> &P,
const nb::DRef<const Eigen::MatrixXN> &V,const nb::DRef<const Eigen::MatrixXI> &F,
const nb::DRef<const Eigen::MatrixXN> &B1,const nb::DRef<const Eigen::MatrixXN> &B2,
const nb::DRef<const Eigen::VectorXI> &leaf,
const nb::DRef<const Eigen::MatrixXI> &TT,const nb::DRef<const Eigen::MatrixXI> &TTi)
{
Eigen::VectorXN S;
Eigen::VectorXI I;
Eigen::MatrixXN bary;
igl::predicates::approximate_signed_distance_exact_sign(P,V,F,B1,B2,leaf,TT,TTi,S,I,bary);
return std::make_tuple(std::move(S),std::move(I),std::move(bary));
}, "P"_a,"V"_a,"F"_a,"B1"_a,"B2"_a,"leaf"_a,"TT"_a,"TTi"_a,
nb::call_guard<nb::gil_scoped_release>(),doc);
}
44 changes: 44 additions & 0 deletions src/predicates/exact_mesh_feature_exposure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/predicates/exact_mesh_feature_exposure.h>

void bind_exact_mesh_feature_exposure(nb::module_ &m)
{
using Exposure=igl::predicates::MeshFeatureExposure;
using Label=igl::predicates::FeatureExposure;
nb::enum_<Label>(m,"FeatureExposure")
.value("EXPOSED_TO_INSIDE",Label::EXPOSED_TO_INSIDE)
.value("FLAT",Label::FLAT)
.value("EXPOSED_TO_OUTSIDE",Label::EXPOSED_TO_OUTSIDE)
.value("UNREACHABLE",Label::UNREACHABLE);
nb::class_<Exposure>(m,"MeshFeatureExposure",
"Reusable exact feature labels; rebuild after changing mesh coordinates, topology, or orientation.")
.def_prop_ro("edges",[](const Exposure &e) { return e.edges; },
"Copy of the #F by 3 edge labels (-1 inside, 0 flat, 1 outside, 2 unreachable).")
.def_prop_ro("vertices",[](const Exposure &e) { return e.vertices; },
"Copy of the #V vertex labels; unreferenced vertices are unreachable.");
m.def("exact_mesh_feature_exposure", [](
const nb::DRef<const Eigen::MatrixXN> &V,
const nb::DRef<const Eigen::MatrixXI> &F,
const nb::DRef<const Eigen::MatrixXI> &TT,
const nb::DRef<const Eigen::MatrixXI> &TTi)
{
Exposure exposure;
igl::predicates::exact_mesh_feature_exposure(V,F,TT,TTi,exposure);
return exposure;
}, "V"_a,"F"_a,"TT"_a,"TTi"_a,nb::call_guard<nb::gil_scoped_release>(),
R"(Cache exact inside/outside exposure of open edges and vertices.

V,F must describe an embedded, closed, vertex-manifold triangle mesh with
nondegenerate faces oriented out of the solid. Use triangle_triangle_adjacency
for TT,TTi; edge i joins corners i and (i+1)%3. Global validity is a precondition.
Rebuild the returned MeshFeatureExposure after changing geometry or topology.
Coordinates convert to float64; indices convert to int64. Arithmetic has
Shewchuk orient3d's range and rounding assumptions.)");
}
39 changes: 39 additions & 0 deletions src/predicates/exact_point_mesh_classify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/predicates/exact_point_mesh_classify.h>

void bind_exact_point_mesh_classify(nb::module_ &m)
{
using Location=igl::predicates::PointMeshLocation;
nb::enum_<Location>(m,"PointMeshLocation")
.value("INSIDE",Location::INSIDE).value("ON_SURFACE",Location::ON_SURFACE)
.value("OUTSIDE",Location::OUTSIDE);
const char *doc=R"(Classify a point using a triangle containing an exact globally closest point.

Return PointMeshLocation.INSIDE, ON_SURFACE, or OUTSIDE. closest_triangle must
come from an exact closest-point search on this mesh. V,F must form an embedded,
closed, outward-oriented, vertex-manifold triangle mesh with nondegenerate
faces. Global validity is a precondition. Supply either array adjacency TT,TTi
or a current MeshFeatureExposure cache. Finite coordinates convert to float64;
indices convert to int64. Extreme exponent spans may raise an exception.)";
m.def("exact_point_mesh_classify", [](
const Eigen::Vector3d &query,const nb::DRef<const Eigen::MatrixXN> &V,
const nb::DRef<const Eigen::MatrixXI> &F,Integer closest_triangle,
const nb::DRef<const Eigen::MatrixXI> &TT,const nb::DRef<const Eigen::MatrixXI> &TTi)
{
return igl::predicates::exact_point_mesh_classify(query,V,F,closest_triangle,TT,TTi);
}, "query"_a,"V"_a,"F"_a,"closest_triangle"_a,"TT"_a,"TTi"_a,doc);
m.def("exact_point_mesh_classify", [](
const Eigen::Vector3d &query,const nb::DRef<const Eigen::MatrixXN> &V,
const nb::DRef<const Eigen::MatrixXI> &F,Integer closest_triangle,
const igl::predicates::MeshFeatureExposure &exposure)
{
return igl::predicates::exact_point_mesh_classify(query,V,F,closest_triangle,exposure);
}, "query"_a,"V"_a,"F"_a,"closest_triangle"_a,"exposure"_a,doc);
}
28 changes: 28 additions & 0 deletions src/predicates/exact_point_mesh_closest_triangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/predicates/exact_point_mesh_closest_triangle.h>

void bind_exact_point_mesh_closest_triangle(nb::module_ &m)
{
m.def("exact_point_mesh_closest_triangle", [](
const Eigen::Vector3d &query,const nb::DRef<const Eigen::MatrixXN> &V,
const nb::DRef<const Eigen::MatrixXI> &F,
const nb::DRef<const Eigen::MatrixXN> &B1,const nb::DRef<const Eigen::MatrixXN> &B2,
const nb::DRef<const Eigen::VectorXI> &leaf)
{
return igl::predicates::exact_point_mesh_closest_triangle(query,V,F,B1,B2,leaf);
}, "query"_a,"V"_a,"F"_a,"B1"_a,"B2"_a,"leaf"_a,
R"(Return the index of a triangle containing an exact globally closest point.

Build B1,B2,leaf with box_simplices and eytzinger_aabb. The tree must contain
every triangle exactly once with conservative bounds. Rebuild after mesh edits.
Degenerate triangles are supported. Exact ties choose the lowest triangle id;
an empty tree returns -1. Finite coordinates convert to float64; indices to
int64. Extreme exponent spans may raise an exception.)");
}
31 changes: 31 additions & 0 deletions src/predicates/exact_point_triangle_closest_feature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "default_types.h"
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <nanobind/stl/tuple.h>

namespace nb = nanobind;
using namespace nb::literals;

#include <igl/predicates/exact_point_triangle_closest_feature.h>

void bind_exact_point_triangle_closest_feature(nb::module_ &m)
{
using Feature=igl::predicates::ClosestPointFeature;
nb::enum_<Feature>(m,"ClosestPointFeature")
.value("VERTEX",Feature::VERTEX).value("EDGE",Feature::EDGE).value("FACE",Feature::FACE);
m.def("exact_point_triangle_closest_feature", [](
const Eigen::Vector3d &query,const Eigen::Vector3d &a,
const Eigen::Vector3d &b,const Eigen::Vector3d &c)
{
int id;
const auto feature=igl::predicates::exact_point_triangle_closest_feature(query,a,b,c,id);
return std::make_tuple(feature,id);
}, "query"_a,"a"_a,"b"_a,"c"_a,
R"(Classify the exact closest point on a closed triangle.

Return (ClosestPointFeature, feature_id): corner i, open edge [i,(i+1)%3],
or face 0. Degenerate triangles prefer the first coincident vertex, then the
first open edge containing the closest point. Finite input 3-vectors convert to
float64; float32 values promote exactly. Extreme exponent spans can raise
an exception.)");
}
Loading
Loading