Surface meshes
A surface mesh, read from an STL, PLY, OBJ or VTK file, describes an object by its boundary: planar faces (triangles, or polygons) joined at their edges. EBGeometry turns such a mesh into a signed distance function, which then composes, renders and runs on a GPU like any other shape.
Important
A signed distance needs an inside, so the mesh must be watertight (no holes) and orientable (every face wound the same way). On a mesh with holes, flipped faces or self-intersections only the unsigned distance is well defined: the sign is unreliable near the defect.
The readers accept holes. A mesh that cannot form a surface (an edge shared by more than two faces, faces folding back onto each other) is an error, or, when asked, read with a warning (see Reading data).
Half-edge meshes
EBGeometry stores a mesh as a doubly-connected edge list (DCEL), also known as a half-edge mesh, of three kinds of element:
faces, planar polygons;
half-edges, each running along one side of one face;
vertices.
As Fig. 2 shows, the half-edges circulate the inside of their face, each knowing the next one. A half-edge also knows its starting vertex and its pair, the half-edge along the same edge in the neighboring face. Walking the half-edges of a face visits its edges and vertices, and stepping to a pair moves to the neighboring face.
Fig. 2 The half-edge structure. Each half-edge knows the next half-edge, its pair and its starting vertex; each vertex its position and one of its outgoing half-edges.
Every element is stored in an array of the mesh and refers to the others by their index in those arrays, never by a pointer, so that the mesh can be copied to a GPU as it is.
Signed distance to a mesh
The signed distance to a mesh is the signed distance to its closest face. The point closest to \(\mathbf{x}\) on a face lies in one of three places, see Fig. 3.
Fig. 3 Where the closest point on a face can lie, after projecting \(\mathbf{x}\) onto the face’s plane.
Inside the face. When \(\mathbf{x}\) projects onto the face’s plane inside the face, the signed distance is \(\mathbf{n}_f\cdot\left(\mathbf{x} - \mathbf{x}_f\right)\), with \(\mathbf{n}_f\) the face’s normal and \(\mathbf{x}_f\) a point on the face. Whether the projection is inside a polygon is decided in the polygon’s plane, by the crossing number by default; the winding number and the subtended angle can be chosen instead (
FaceT::setInsideOutsideAlgorithm()).On an edge. Otherwise, the point may project onto an edge \(\mathbf{e}(t) = \mathbf{x}_0 + \left(\mathbf{x}_1 - \mathbf{x}_0\right)t\) at some \(t \in [0, 1]\). The distance is the distance to that point, signed by \(\mathbf{n}_e\cdot\left(\mathbf{x} - \mathbf{x}_0\right)\), with \(\mathbf{n}_e\) the edge’s pseudonormal.
At a vertex. Otherwise the closest point is a vertex \(\mathbf{x}_v\), and the sign is that of \(\mathbf{n}_v\cdot\left(\mathbf{x}-\mathbf{x}_v\right)\), with \(\mathbf{n}_v\) the vertex’s pseudonormal.
Testing every face costs as much as the mesh is large; a bounding volume hierarchy over the faces keeps the test to the faces near the point (see Bounding volume hierarchies).
Pseudonormals
A face has one normal; an edge and a vertex, where faces meet at an angle, do not. The sign near them comes from pseudonormals [1], which give the right sign at every point. An edge’s is the average of its two faces’ normals,
and a vertex’s the average of the normals of the faces around it, weighted by the angle \(\alpha_i\) each face makes at the vertex (see Fig. 4):
Fig. 4 Edge and vertex pseudonormals.
Both are computed when the mesh is built.
The closest point and the gradient
The search for the distance also finds where it is measured to: the closest point \(\mathbf{c}\) on the mesh. From it, the gradient of the signed distance \(S\) follows in closed form,
a unit vector: away from the closest point outside the mesh, toward it inside. Where \(\mathbf{c}\) lies inside a face it is the face’s normal; on an edge or a vertex, the direction from that feature to the point. It costs one distance query, where a finite-difference gradient costs six and needs a step smaller than the mesh’s features. It also moves a point onto the surface in one step, \(\mathbf{c} = \mathbf{x} - S(\mathbf{x})\,\nabla S(\mathbf{x})\).
The formula has no direction on the surface itself, where \(\mathbf{x} = \mathbf{c}\). There the gradient is the pseudonormal of the face, edge or vertex the point is on, the normal a mesh has at its edges and corners. At points with more than one closest point (the medial surface, inside a box along its diagonals) the distance has no gradient; the one reported is seen from the first closest face found, one of the one-sided gradients.
Triangles in SIMD blocks
A mesh of triangles can also be stored as self-contained triangles, without the half-edge structure: each triangle holds its three vertices, its face normal and the pseudonormals of its edges and vertices, so that nothing else is read to find its distance. Triangles stored this way are packed in blocks of \(W\), coordinate by coordinate, so that one SIMD instruction computes the distance to all \(W\) at once (see SIMD acceleration). A polygon face is cut into a fan of triangles from its first vertex, which gives the same distance since the face is planar and convex. Each triangle remembers the face it was cut from.
Choosing a mesh distance function
EBGeometry has three mesh distance functions, all shapes that compose, ray trace exactly and run on a GPU:
Class |
Holds |
Use it for |
|---|---|---|
|
triangles in SIMD blocks, under a BVH |
a triangle mesh (or any mesh, cut into triangles): the fastest, and the default choice |
|
the half-edge mesh, under a BVH |
polygon faces kept as they are, or a mesh that changes after it is built (flipped, its vertices moved), since every query reads the mesh itself |
|
the half-edge mesh, no BVH |
checking the others, and meshes of a few faces: every query tests every face |
All three name the face closest to a point by its face id, the face’s position in the file (after the reader removes faces of zero area), so that per-face data of your own (a material, a boundary condition) is an array indexed by it. Mesh distance functions describes the classes, and MeshSDF reads a mesh and queries it.