Half-edge meshes

The half-edge mesh (see Half-edge meshes for the concepts) is in the namespace EBGeometry::DCEL. Most programs never handle it directly: a file reader builds it and a mesh distance function wraps it (see Mesh distance functions). Use it directly to walk a mesh’s topology, to move its vertices, or to read its faces’ data:

Pool pool(hostMemoryResource());

const DCEL::MeshT<T> mesh = Parser::readIntoDCEL<T>("bunny.ply", pool);

for (uint32_t i = 0; i < mesh.numFaces(); i++) {
  const DCEL::FaceT<T>& face = mesh.getFace(i);           // face i: the file's face i
  face.getNormal();
}

mesh.signedDistance(p);                                    // every face tested: for small meshes only

Important

The DCEL functionality is not restricted to triangles, but supports N-sided polygons. Every face has an id, its index in the mesh, which the mesh distance functions report for the face closest to a point. Data attached to specific faces, such as boundary conditions, lives in the caller’s own array indexed by that id; see Face ids.

Main types

The main DCEL functionality (vertices, edges, faces) is provided by classes templated on a floating-point precision T:

  • VertexT<T> stores the vertex position, its (outward) normal vector, and the index of an outgoing half-edge from the vertex (into the owning MeshT’s edge array). It also has member functions for computing the vertex pseudonormal, see Pseudonormals. For the full API, see the Doxygen reference for VertexT.

  • EdgeT<T> represents a half-edge: it stores the indices of its owning face, the next edge, its pair edge, and its starting vertex – each an index into the owning MeshT’s corresponding array, not a pointer. For the full API, see the Doxygen reference for EdgeT.

  • FaceT<T> represents a polygon face. Besides the index of its half-edge, it also stores the face normal vector, a 2D embedding of the polygon (which the signed distance computation needs), its centroid and its area. For the full API, see the Doxygen reference for FaceT.

  • EdgeIteratorT<T> walks the half-edge loop around a face by following each edge’s next-edge index until it is back at the face’s first edge. It trusts the loop to close: MeshT::reconcile() checks every face’s loop once and aborts, in every build, on one that does not, and MeshT::sanityCheck() reports it. A mesh read from a file never has a broken loop; one assembled by hand can, and must be reconciled before it is queried. For the full API, see the Doxygen reference for EdgeIteratorT.

Note

VertexT, EdgeT, and FaceT cross-reference each other exclusively by index into the owning MeshT’s own vertex/edge/face arrays – never by pointer. Every member function that needs to resolve one of these indices back into an actual vertex/edge/face (signedDistance(), reconcile(), the point-in-face test, …) therefore takes that owning MeshT as an explicit argument. Since every member is consequently a plain value (Vec3T<T>, T, uint32_t, or an enum), all three classes are trivially copyable – they can be memcpy’d or mirrored to a different address space with no pointer patching, as long as they are interpreted against the same mesh’s arrays on the other side.

  • MeshT<T> stores an entire DCEL mesh – all of its vertices, half-edges, and faces – and provides brute-force (\(\mathcal{O}(N)\)) distance queries, signedDistance() and unsignedDistance2(), that scan every face directly. For anything beyond small meshes, one instead wraps a MeshT<T> in one of the BVH-accelerated classes described in Mesh distance functions. MeshSDF keeps the mesh (its arrays, in the same pool) and reads every face it tests from it, since a face’s half-edge index is only meaningful together with the mesh it was built from. A mesh is typically never constructed by hand – it is built by a file parser reading vertices and faces from disk, see Reading data. Its vertex/edge/face arrays are reserved from a caller-supplied, non-owning Pool – see Memory model below for how this works. For the full API, see the Doxygen reference for MeshT.

Face ids

A face’s id is its index in the mesh’s face array, mesh.getFace(id). The file readers number faces in file order, leaving out degenerate faces, which they remove (see Reading raw file data). The mesh distance functions report the id of the face closest to a point from getClosestFace(), on the host and on a device alike, and TriMeshSDF reports the id of the polygon face a triangle was cut from. The DCEL classes carry no user data of their own: anything attached to a face, such as a boundary condition or a material, is kept in an array of the caller’s, indexed by face id. The data a PLY or VTK file attaches to its faces and vertices is read that way: the PolygonSoup a reader returns holds it in arrays indexed by the same face and vertex ids (see Reading raw file data). Vertices and half-edges are likewise identified by their indices.

Memory model

See Memory model first for the generic Pool/PODVector/MemoryResource foundation (the control block, mirroring, the at()-vs-bind() access-style choice) – this section covers only how MeshT specifically is built on top of it.

MeshT<T>’s vertex/edge/face arrays are three PODVectors. The mesh attaches itself to a Pool on its first reserveVertices()/reserveEdges()/reserveFaces() call and resolves every access through that pool’s control block from then on, so there is exactly one accessor of each kind (getVertex(i), signedDistance(p), …) and no binding, freezing, or base-passing step of any sort. A mesh is queryable as soon as it has data, including while the same pool is still being built into, and several meshes can share one pool without coordinating.

The mutators (reserveVertices()/reserveEdges()/reserveFaces(), addVertex()/addEdge()/addFace()) still take the Pool& explicitly, since they reserve from it. isAttachedTo(pool) reports whether a mesh’s storage came from a given pool, which is how a caller holding both can confirm they belong together.

Because MeshT stores nothing but PODVectors and plain values, it – like VertexT/EdgeT/FaceT themselves – is trivially copyable, so a mesh built and reconciled entirely on the host can cross into another address space by value.

Warning

A reference handed back by getVertex(i)/getEdge(i)/getFace(i) is an address resolved at the moment of the call, and any Pool::reserve() may free the block it points into. This is the general rule of Resolved addresses do not survive a reserve applied to MeshT: resolve, use, discard, and carry elements across a reserve by value rather than by reference. Everything returning by value – signedDistance(), getAllVertexCoordinates(), and so on – is unaffected.

Note

One ``Pool`` backing many meshes is the normal case. Building several meshes one after another into the same pool is safe, and cheaper than giving each its own (see Reading data): when the pool moves its block to grow, every mesh in it keeps resolving correctly without being told. The pool must outlive every mesh in it.

Crossing address spaces

rebasedView(const Pool&) is the one sanctioned way to point a mesh at a mirror of its own pool. It copies only the descriptor – the vertex/edge/face data is whatever the mirror already contains – and since every cross-reference is a byte offset, nothing needs patching on either side:

hostPool.freeze();

EBGeometry::Pool devicePool = EBGeometry::Pool::mirror(hostPool, EBGeometry::GPU::memoryResource());
const auto       deviceMesh = mesh.rebasedView(devicePool);    // rebase on the host ...

myKernel<<<blocks, threads>>>(deviceMesh, ...);                // ... then copy by value

The view resolves as every pool handle’s does (see Memory model), and rebasedView() checks, in every build, that the pool it is handed is a mirror of the mesh’s own pool and holds the mesh’s arrays: the wrong pool, or one mirrored before the mesh’s last reserve, aborts.

Reconciling a moved mesh where its pool is

After a mesh’s vertices move, its faces’ normals, centroids and areas, and the edge and vertex pseudonormals, must be reconciled again before the mesh is queried. MeshT::reconcile() does that on the host, checking the mesh’s topology and finding the faces around each vertex on every call. To reconcile a mesh whose vertices move in device memory, or to reconcile one often on the host, use DCEL::reconcile(pool, mesh, schedule) (EBGeometry_DCEL_Reconcile.hpp). It reconciles the copy of the mesh in pool, the mesh’s own pool or any mirror of it, and runs where that pool’s memory is: in parallel on a device-accessible pool; on a host pool, in host loops, faster than MeshT::reconcile() and with the same values.

What moving vertices never changes, the topology and which faces touch each vertex, is checked and stored once by a DCEL::ReconcileSchedule. It is opt-in, for the meshes you reconcile this way. Build it after the mesh and before freezing the pool: it is stored in the mesh’s pool, so mirroring the pool carries it to the device. Building it on a frozen pool aborts, and so does building it for a mesh with a broken topology, with MeshT::reconcile()’s messages.

auto sdf = Parser::readIntoMeshSDF<T>(file, pool);
const auto mesh = sdf.getMesh();

DCEL::ReconcileSchedule reconcileSchedule(pool, mesh);   // after the build, before the freeze
BVH::RefitSchedule      refitSchedule(pool, sdf.getRoot());
pool.freeze();

Pool devicePool = Pool::mirror(pool, GPU::memoryResource());

for (int step = 0; step < numSteps; step++) {
  moveMyVertices(devicePool);                              // your own kernels
  DCEL::reconcile(devicePool, mesh, reconcileSchedule);
  BVH::refit(devicePool, sdf.getRoot(), refitSchedule, FaceBox{devicePool});   // your functor: a face's box
}

How the vertices move is up to you: your own kernels, another library, GPU::parallelFor(), or a copy from the host. Nothing but a status word comes back to the host in the loop: the mesh, and the mesh SDF over it, are queried in device memory. The rules:

  • The moves must be finished. DCEL::reconcile() launches its passes on the default stream and returns once they are done; synchronize work on a stream that does not synchronize with the default one before calling it.

  • The topology stays. Move vertices, but do not add, remove or relink vertices, edges or faces; that calls for a new mesh (and a new schedule).

  • A mesh SDF needs its BVH refitted too, with BVH::refit() (BVH), before it is queried again.

On a device the values agree with the host’s to rounding, not bit for bit: device compilers fuse multiplies and adds, and the device’s math functions round differently in the last place. Moved vertices that leave a vertex and its two neighbors on a face at the same position abort, with MeshT::reconcile()’s message, as on the host. A schedule built for another mesh, or in a pool of another mirror chain, is refused. For the exact signatures, see the Doxygen reference for the DCEL namespace.

BVH integration

A MeshT<T> is never queried directly for anything beyond tiny meshes – see Bounding volume hierarchies for why an \(\mathcal{O}(N)\) scan over faces doesn’t scale, and BVH for how TreeBVH/PackedBVH are actually built and traversed. This section covers only the DCEL-specific half of that integration: how a mesh’s faces become BVH primitives in the first place.

MeshSDF puts the mesh’s faces in a BVH as they are: each primitive is a face id, bounded by the box around the face’s vertices, and the tree is built with the requested BVH::Construction method (see Preset construction methods). TriMeshSDF instead cuts each face into Triangle values and builds its BVH over SIMD-width groups of triangles, without keeping the mesh. See Mesh distance functions for how the two differ, and Reading data for the file-reading entry points that build a MeshSDF/TriMeshSDF from a mesh file directly.