Mesh distance functions

A surface mesh becomes a signed distance function through one of three classes (see Surface meshes for the concepts and for choosing between them). They share the sign convention (negative inside, positive outside), are shapes that compose, ray trace exactly and run on a device, and differ in what they store:

Class

Built from

Stores

TriMeshSDF<T, K, W>

a half-edge mesh or a triangle soup

a PackedBVH whose leaves are groups of W triangles (TriangleAoSoA<T, W>), copied from the mesh: the fastest, and the recommended default

MeshSDF<T, K>

a half-edge mesh

the mesh and a PackedBVH of its face ids: any planar convex polygons, and every query reads the mesh itself

FlatMeshSDF<T>

a half-edge mesh

the mesh alone: every query tests every face; for checks and tiny meshes

See the Doxygen pages of TriMeshSDF, MeshSDF and FlatMeshSDF. The file readers build any of them in one call (see Reading data):

Pool pool(hostMemoryResource());

const auto sdf = Parser::readIntoTriMeshSDF<T>("armadillo.obj", pool);   // or readIntoMeshSDF, readIntoFlatMeshSDF

const T              d    = sdf.signedDistance(p);
const ClosestFace<T> face = sdf.getClosestFace(p);                       // face.faceId: its position in the file

MeshSDF reads a mesh and queries it on the host and on a device.

Values and handles

All three are plain values with no virtual functions, trivially copyable, holding descriptors into the one Pool passed to their constructor: MeshSDF the mesh and its BVH, TriMeshSDF its BVH, FlatMeshSDF the mesh. The pool must outlive them and every copy. As for every pool handle (see Memory model), a copy shares the data, deepCopy(pool) gives storage of its own, and rebasedView(devicePool) is what a kernel takes after the pool is frozen and mirrored. signedDistance(), getClosestFace(), getRoot() and computeBoundingVolume() are callable on the host and on a device.

A copy stored in a BVH union, or placed several times with Translate() in a tape, shares the same data: the mesh exists once however many copies refer to it (see NestedBVH).

The closest face

getClosestFace(point) returns a ClosestFace: the signed distance, as signedDistance() returns it, and the face id, the face’s index in the mesh, which is its position in the file (see Face ids). Where several faces are equally close (at a point nearest an edge or a vertex they share), the first one found is reported. The id indexes whatever per-face data the caller keeps, such as a material or a boundary condition. A TriMeshSDF built from a triangle soup reports the id each Triangle carries (Triangle::setFaceId()).

TriMeshSDF evaluates each leaf’s triangles together, with SIMD instructions on the host; its values can differ from a device’s in the last bit at points nearly equally close to two triangles. What is vectorized is described in SIMD-accelerated classes.

The mesh distance functions are also ray traced exactly: firstHit() and intersects() walk the BVH along the ray and test the faces near it, reporting the face id and its normal (see Ray queries). A ray through an edge or a vertex that several faces share reports the lowest-numbered of them, so the host and a device name the same face and shade it alike; its distance along the ray is then that face’s crossing, which can lie a rounding error past the nearest one.

The closest point and the gradient

closestPoint(point) returns a ClosestPoint: the signed distance and the face id, as getClosestFace() reports them, the closest point on the mesh, and the unit pseudonormal of the face, edge or vertex that point lies on. gradient(point) returns the unit gradient of the signed distance in closed form from the closest point, and the pseudonormal on the surface (see The closest point and the gradient). Both are on all three mesh distance functions and callable on a device:

const ClosestPoint<T> c = sdf.closestPoint(p);  // c.point is on the mesh, at distance |c.signedDistance|
const Vec3T<T>        n = sdf.gradient(p);      // unit length, never NaN
const Vec3T<T>        q = p - c.signedDistance * n; // p moved onto the surface: q equals c.point

Neither is on the path of signedDistance(), so adding them costs a distance query nothing. A query that only needs the distance should keep calling signedDistance(): the closest point costs a little more. For a TriMeshSDF the reported feature is one of its triangles’: on the diagonal of a fan-triangulated polygon, the normal is the polygon’s. MeshClosestPoint shows both.

When the mesh changes

  • MeshSDF reads every face from its mesh, so it sees the mesh as it is: flipping the mesh (MeshT::flip()) flips every distance, and after moving vertices, mesh.reconcile() followed by getRoot().refit() with each face’s new bounding box brings it up to date (see Refitting for moving geometries; on a device, DCEL::reconcile(), Reconciling a moved mesh where its pool is). The price of reading the faces from the mesh is that its queries are somewhat slower than over copies of them.

  • TriMeshSDF copies the triangles when it is built and keeps no mesh, so a change to the mesh is not seen: rebuild it.

  • FlatMeshSDF sees the mesh as it was when it was constructed.

The branching factor and the triangle width

TriMeshSDF takes the BVH’s branching factor K and the triangle group width W as template parameters, and MeshSDF takes K. Parser::readIntoTriMeshSDF defaults them to BVH::DefaultBranchingRatio<T>() and TriangleSoA::DefaultWidth<T>(), both 4, the same in every translation unit and on a device; keep those for any type device code also uses. The host-tuned values, the build switch that widens the default triangle group, and which of the two pays off are in Branching factor and SIMD width; HostTuning measures them on your machine. Any K and W compute the same values, with narrower registers or a scalar loop where no SIMD path matches.

A TriMeshSDF leaf holds up to maxLeafGroups groups of W triangles (4 in Parser::readIntoTriMeshSDF, which builds with TriMeshSDF::defaultConstructionOptions(4)); a leaf of fewer than W triangles pads its group. To set each construction method’s leaf size, see Leaf sizes.