Computational geometry: triangulations, hulls, spatial search and meshes
Computational geometry turns questions about shapes into algorithms: which points are nearest, which triangles to draw, where a ray lands, what a part looks like in cross-section. It underlies computer graphics, CAD and 3D printing, robotics, geographic information systems, and scientific computing. This chapter covers lib_jsl_geometry:
- the primitive types: points, vectors, shapes and the tables that let them share vertices;
- rotations, Euler angles and quaternions;
- Delaunay triangulations and Voronoi diagrams;
- convex hulls in two and three dimensions, and boolean operations on polygons;
- k-d trees, quadtrees and octrees for nearest-neighbour and range search;
- intersection tests and closest points;
- smoothing, decimating and slicing triangle meshes;
- four-vectors, Lorentz boosts and the Christoffel symbols of curved space-time.
The central questions
- Exact or approximate predicates? Geometric algorithms branch on signs: is this point left of that line, inside that circle? Rounding can make the signs inconsistent, and the algorithm then loops, crashes or returns overlapping triangles. The library compares each predicate with a bound on its rounding error and treats values below it as zero (collinear or cocircular). Most of the defects fixed in this chapter were of this kind or close to it.
- Plain arrays or connected shapes? The algorithms take plain
[f64; 2]and[f64; 3]arrays and index triples. The primitive types keep shared vertices in tables, which suits editing connected models. - Static or dynamic? A k-d tree or a triangulation is built once; a quadtree accepts insertions and removals.
Testing against SciPy and Shapely
The references come from SciPy 1.17.1’s spatial module (Qhull for triangulations, hulls and Voronoi diagrams; cKDTree; Rotation and Slerp) and from Shapely 2.2.0, on random inputs written out by book/generator/data/make_geometry_refs.py and read back by the generator:
Table 24.1. Geometry routines against reference implementations.
| computation (reference) | result |
|---|---|
Delaunay triangulation, 500 uniform points (Delaunay) | 982 triangles, identical to Qhull’s |
2D convex hull, same points (ConvexHull) | 16 vertices, same set: true; area difference 0.0e0 |
3D convex hull, 1000 points in a ball (ConvexHull) | 140 vertices, same set: true; volume difference 2.7e-15, area difference 2.3e-14 |
3D convex hull, 400 points on a sphere (ConvexHull) | 400 vertices, same set: true; volume difference 3.6e-15, area difference 4.8e-14 |
Voronoi cells of 300 sites clipped to the unit square (Voronoi of the mirrored sites) | largest cell-area difference 1.6e-16 |
8 nearest neighbours of 200 queries among 4000 points in 3D (cKDTree.query) | identical for 200 of 200; largest distance difference 5.0e-16 |
points within 0.08 of 100 centres, quadtree (cKDTree.query_ball_point) | same count for 100 of 100 |
| intersection, union and difference of 200 pairs of star polygons (Shapely) | largest area difference 5.3e-15 |
intrinsic XYZ Euler angles, 100 rotations (Rotation.from_euler, as_euler) | matrix difference 8.9e-16, angle difference 3.6e-15 |
intrinsic ZYX Euler angles, 100 rotations (Rotation.from_euler, as_euler) | matrix difference 1.0e-15, angle difference 1.0e-14 |
intrinsic ZXZ Euler angles, 100 rotations (Rotation.from_euler, as_euler) | matrix difference 8.9e-16, angle difference 3.6e-15 |
intrinsic YZY Euler angles, 100 rotations (Rotation.from_euler, as_euler) | matrix difference 8.9e-16, angle difference 3.6e-15 |
spherical linear interpolation at 11 times (Slerp) | largest quaternion component difference 5.6e-16 |
In the library
All of these are in lib_jsl_geometry (API reference).
| You need | Use | Entry |
|---|---|---|
| Points, vectors and shapes that share vertices; coordinate systems | GeometryTableRegistry, CoordinateVector3D, Triangle3D, SurfaceMesh3D, … | Geometric types |
| To represent, convert or interpolate orientations | Rotation3, EulerSequence, Quaternion | Rotations |
| A triangle mesh of scattered points; nearest-site regions | delaunay_triangulation, voronoi_diagram | Delaunay and Voronoi |
| The convex outline of points; unions and intersections of polygons | convex_hull_2d, convex_hull_3d, polygon_boolean | Hulls and booleans |
| Nearest neighbours, points in a radius or a box | KdTree, QuadTree, OctTree | Spatial trees |
| Ray casting, collision tests, nearest points of shapes | ray_triangle, segment_segment_2d, closest_point_on_triangle, … | Intersections |
| Cleaning, simplifying or slicing a mesh | taubin_smoothing, decimate, slice_mesh | Meshes |
| Relativistic kinematics; free fall in curved space-time | SpaceTimeVector4D, ChristoffelSymbols4D | Space-time |