Skip to content

Signed distance to a mesh, and isolines for many isovalues - #82

Open
jf--- wants to merge 3 commits into
compas-dev:mainfrom
jf---:jf/signed-distance
Open

jf--- wants to merge 3 commits into
compas-dev:mainfrom
jf---:jf/signed-distance

Conversation

@jf---

@jf--- jf--- commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Signed distance to a mesh, and isolines for many isovalues

Two additions for querying one mesh many times. Existing functions are unchanged.

compas_cgal.distance.MeshDistance (new module)

  • closest(points) returns the closest points and the face each lies on; signed_distance(points) returns the distance (negative inside), the closest point, the face and the gradient. Numpy arrays in and out.
  • The AABB_tree is built once in the constructor and shared by every query.
  • The side comes from Side_of_triangle_mesh, with exact predicates and independently of the closest point. A query on the mesh is at distance exactly zero, and its gradient is the angle-weighted pseudonormal of the face, edge or vertex it lies on (the faces containing it are found with all_intersected_primitives).
  • Queries run on all hardware threads by default (num_threads) and release the GIL. The result is the same, bit for bit, whatever the number of threads.

compas_cgal.isolines.isolines_with_indices

  • Takes (vertices, faces), a scalar per vertex and a list of isovalues; returns the polylines and, for each, the index of its isovalue.
  • isolines rebuilds the whole mesh for every isovalue: an isovalue that crosses no face still costs 27 ms on the mesh below. Here the faces are sorted to the isovalues they cross in one pass, and refine_mesh_at_isolevel runs per isovalue on those faces only.
  • The points are those isolines returns, bit for bit. Where a closed polyline starts, its direction, and the order of the polylines of one isovalue may differ.

Validation

  • A box that is neither centred nor dyadic, against its closed form: distance within 4.4e-16, closest point within 5.6e-16, gradient within 9.0e-16.
  • Queries exactly on faces, edges and corners: distance 0, and the gradient is the face normal, the mean of the two faces, the mean of the three. One rounding step off the surface, on either side, the sign is right.
  • Two builds answer bit for bit the same, and so do 1, 2, 7 and all threads. A test checks that another Python thread runs while a query is answered.
  • isolines_with_indices against isolines, isovalue by isovalue: on a sphere, and on an open grid whose isovalues run through whole columns of vertices.
  • Closest points agree with libigl to 3.3e-16 on 734,943 queries, and the signs agree on all of them (log below).

Performance

Apple M1 Max, 10 hardware threads; CGAL 6.1.1, Apple clang 17, the repository's build flags. Mesh: closed, genus 4, 58,060 vertices, 116,132 faces.

MeshDistance
  build (AABB tree and its hint tree): 0.167 s
  234,943 queries near the surface (within 1e-4 of the extent)
    signed_distance, threads   1:   1.441 s    6.13 us/query  x 1.0  identical to 1 thread: True
    signed_distance, threads   2:   0.655 s    2.79 us/query  x 2.2  identical to 1 thread: True
    signed_distance, threads   4:   0.306 s    1.30 us/query  x 4.7  identical to 1 thread: True
    signed_distance, threads   8:   0.153 s    0.65 us/query  x 9.4  identical to 1 thread: True
    signed_distance, threads all:   0.137 s    0.58 us/query  x10.5  identical to 1 thread: True
    closest, threads all:           0.080 s    0.34 us/query
    libigl signed_distance (pseudonormal, its own threads):   0.236 s    1.00 us/query; closest points differ by at most 3.3e-16, signs differ on 0 queries
  500,000 queries uniform in the bounding box
    signed_distance, threads   1:  11.852 s   23.70 us/query  x 1.0  identical to 1 thread: True
    signed_distance, threads   2:   5.579 s   11.16 us/query  x 2.1  identical to 1 thread: True
    signed_distance, threads   4:   2.635 s    5.27 us/query  x 4.5  identical to 1 thread: True
    signed_distance, threads   8:   1.455 s    2.91 us/query  x 8.1  identical to 1 thread: True
    signed_distance, threads all:   1.339 s    2.68 us/query  x 8.9  identical to 1 thread: True
    closest, threads all:           1.276 s    2.55 us/query
    libigl signed_distance (pseudonormal, its own threads):   0.988 s    1.98 us/query; closest points differ by at most 3.3e-16, signs differ on 0 queries

isolines of the height, isovalues evenly spaced
  isolines_with_indices,   100 isovalues:   0.082 s, 196 polylines, 56,539 points
  isolines (the existing binding), 100 isovalues:   3.171 s, 196 polylines
  isolines, one isovalue that crosses no face: 27.0 ms
  isolines_with_indices,  1000 isovalues:   0.743 s, 1979 polylines, 561,867 points

Near the surface MeshDistance is 1.7x faster than libigl; far from it libigl is 1.35x faster, and there the closest-point search is nearly all of the time (2.55 of 2.68 us).

Notes

  • _distance is compiled with CGAL_HAS_THREADS and linked against the platform's thread library, so that CGAL guards what it builds lazily. The other modules' flags are unchanged.
  • compas_cgal.isolines raised TypeError: incompatible function arguments for a list of isovalues unless a module that loads the bound vector types (compas_cgal.geodesics, for one) had been imported first. It now loads them itself; a test imports the module alone in a fresh interpreter.
  • The mesh for signed_distance has to be closed, manifold, free of self-intersections and without degenerate faces. Closedness and manifoldness are checked; the other two are not.
  • Compiling at -O3 in place of the -Os that nanobind applies changed the query times by about 5%.
  • Two commits, one per feature.

…e to a mesh

AABB tree built once, queried from all threads without the GIL. Side from
Side_of_triangle_mesh (exact predicates): a query on the mesh is at distance
exactly zero and takes the angle-weighted pseudonormal of its feature.
Faces are sorted to the isovalues they cross in one pass; refine_mesh_at_isolevel
runs per isovalue on those faces only. Each polyline comes with the index of its
isovalue. Also loads the bound vector types in the module itself.
A face with its three vertices on one line has no normal: the gradient of a
query on it came back NaN, and the mesh was accepted. The constructor now
refuses it, decided with CGAL's exact predicate
(PMP::is_degenerate_triangle_face), so a face whose cross product rounds off
zero in floats is found too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant