Leaves, surfaces and labels
A composed object answers one question at a point, its signed distance, but an application often needs
a second: which part of the object is that? A boundary condition depends on whether a wall is the inlet,
the outlet or the casing; a material on whether a surface is the machined bore or the cast body; a
picture or a label on which object a pixel shows. A tape answers this with closestLeaf(p). It
names the leaf of the composition whose surface the value at p comes from: the deciding leaf.
When that leaf holds many things itself (a cloud of spheres, a BVH union of objects, another tape),
closestSurface(p) goes on into it and names the one that decides there (see Surfaces: inside a cloud, a union or a nested tape).
The deciding leaf, not the nearest one
A composition takes its value from its operands’ values: a union takes the smallest, an intersection the
largest, a difference the largest of the first operand’s value and the negated values of the others.
Whichever operand the value is taken from is the one that decides it, and following that choice down
the composition ends at a leaf. That leaf is the one closestLeaf(p) names.
This is not the same as the primitive whose surface is geometrically nearest to p. A composition
removes parts of its primitives’ surfaces, and the nearest primitive surface may be one of those removed
parts, which is no longer part of the object. Fig. 8 shows a cross-section of
Difference(box, sphere), a box with a ball carved out of one side. The plane is colored by the leaf
that decides the value: the sphere near the cavity, on both sides of its wall, and the box everywhere
else.
Fig. 8 Difference(box, sphere) in a plane through the box’s and the ball’s centers, colored by the
deciding leaf (the box is leaf 0, the sphere leaf 1). The dashed lines are the parts of the two
primitives’ surfaces that the difference removed. At \(p_1\), outside the object beside the
ball’s removed cap, the nearest primitive surface is the sphere’s, but the value comes from the box,
and the box decides. At \(p_2\), just outside the box’s removed face, it is the other way around.
The point \(p_1\) is the typical case: the cap of the ball that sticks out of the box is not part of the object, so the object’s surface near \(p_1\) is the box’s, and an application tagging that surface wants the box. The deciding leaf gives it; the nearest primitive would not.
Each operation passes on a leaf as follows:
A union takes the leaf of the operand it takes the value from (the smallest), an intersection the leaf of the largest. A difference is an intersection with the complements of the subtracted objects.
A smooth union, intersection or difference blends the two most extreme operands’ values and takes the leaf of the more extreme one.
A blur takes the leaf of its center sample, a mollification that of its sample with the largest weight.
A complement, offset, shell, translation, rotation, scaling, reflection, elongation or repetition passes on its operand’s leaf. Every copy of a repeated object is the same leaf; the copy whose value is taken decides which local point is reported (see below).
A nested composite is one leaf: a fused composition (see Fused composition), which is one shape with one surface however it is built, a tape listed as a primitive of another tape, a BVH union of many objects and a sphere cloud. A scene is therefore a tape whose leaves are its parts: it names the part that decides the value, and each part can be as large a composition as it likes. The collections among them (all but the fused composition) are drilled into by
closestSurface().
Asking for it
closestLeaf(p) is a member of the
Tape and returns a
LeafRef:
m_leaf: the leaf’s number. The leaves are numbered in the order they appear in the expression, depth first, counting each occurrence, so a shape used twice is two leaves. InDifference(Intersection(cube, ball), Union(drills))the cube is leaf 0, the ball leaf 1 and the three drills leaves 2 to 4. The number is the one to index a table of your own with: materials, boundary conditions, colors. It isLeafRef::Nonewhen the query names no leaf, so check it before indexing.m_typeandm_index: which of the tape’s primitive types the leaf is, and which primitive of that type. They are what the accessors below use; most programs need not read them directly.m_localPoint: the point in the primitive’s own frame, after the translations, rotations, scalings, reflections, elongations and repetitions above the leaf. This is where the primitive itself can be queried.
The tape also returns the primitive the reference names:
isPrimitive<P>(ref)says whether the leaf is aP.Pmust be one of the tape’s primitive types; any other type is a compile error.primitive<P>(ref)returns it by value: the shape itself, or for a primitive that lives in a pool (a mesh distance field, a BVH union, a nested tape) a handle that resolves against the same pool as the tape, or the same mirror of it on a device.visitPrimitive(ref, f)callsfwith the primitive whatever its type, asprimitive<P>()returns it. In host code a generic lambda will do; in a CUDA or HIP translation unit, pass a functor whose call operator isEBGEOMETRY_HOST_DEVICE, since the device compiler treats a lambda’s call operator as host code there.
A mesh is one leaf. To tell its faces apart, ask the mesh itself for its closest face at the local point:
using Mesh = TriMeshSDF<T, BVH::DefaultBranchingRatio<T>(), TriangleSoA::DefaultWidth<T>()>;
const LeafRef<T> ref = tape.closestLeaf(p);
if (ref.m_leaf != LeafRef<T>::None && tape.isPrimitive<Mesh>(ref)) {
const Mesh mesh = tape.primitive<Mesh>(ref);
const uint32_t face = mesh.getClosestFace(ref.m_localPoint).faceId; // the face's position in the file
}
The local point matters here: the mesh was translated, rotated or scaled into place in the expression, and its own BVH knows only the mesh’s own coordinates. A nested tape is asked the same way, for its own deciding leaf at the local point.
Surfaces: inside a cloud, a union or a nested tape
A leaf can hold many things: a SphereCloudBVH thousands of spheres, a BVHUnion many objects, a tape
listed as a primitive its own leaves. closestLeaf(p) names the leaf; closestSurface(p) goes on into
it and names the member whose surface decides the value there, at any depth (a sphere of a cloud inside a
union inside a nested tape). It returns a
SurfaceRef:
m_surface: the surface, numbered across the whole scene. Each leaf covers a contiguous range of surface numbers, in leaf order: one surface for a leaf that is one shape, and for a collection one per member, in the collection’s own order (a cloud’s cloud indices, a union’s members in the order its constructor was given them).getFirstSurface(leaf)is where a leaf’s range starts, so data of your own per member (per sphere, say) is an array indexed bym_surface - getFirstSurface(m_leaf): no sphere needs a label.m_leafandm_surfaceLabel: the leaf, asclosestLeaf()numbers it, and the surface’s label (see below).m_part: the part of the deepest shape reached, a box’s face or a mesh’s face (see Parts: a face of a shape).m_localPoint: the point in the frame of the deepest shape reached.
const Tape<T, SphereCloudBVH<T>> tape(pool, Difference(SurfaceLabel(box, "wall"), SurfaceLabel(cloud, "particles")));
const SurfaceRef<T> s = tape.closestSurface(p); // on a device too
restitution[s.m_surfaceLabel]; // per-label data
sphereData[s.m_surface - tape.getFirstSurface(s.m_leaf)]; // per-sphere data, in cloud order
A collection costs one more query of the deciding leaf for each level drilled (a second search of the cloud,
say), paid only by closestSurface(); for a tape whose leaves are all single shapes, the surface is the
leaf, and the tape stores nothing more. A cloud numbers as many surfaces as it has room for
(maxSpheres()), so a rebuild with another number of spheres keeps the scene’s numbering. A shape of your
own becomes a collection by defining getNumSurfaces() and closestSurface() (see
hasSurfaces). A nested tape’s labels are its own, and the outer
tape does not report them: label within one expression. A mesh is one surface; its faces are its parts.
closestSurface(shape, p) is also a free function, for code templated on a shape: a collection answers as
above, and any other shape with surface 0 and its part.
Parts: a face of a shape
A shape can have parts: the pieces of its own surface its formula tells apart, a box’s six faces, a cylinder’s
side and caps, a mesh’s faces. SurfaceRef::m_part is the part of the deepest shape reached, read from the
shape’s own formula at the local point: the term that decides the value there. A part belongs to the shape,
never to the composition: there is no boundary representation, so a box face that a difference cuts in two is
one part, and two boxes whose faces line up into one flat face of the result are two surfaces.
Shape |
Parts |
|---|---|
|
6: -x, +x, -y, +y, -z, +z |
|
3: the side, the end at the first center, the end at the |
|
second (-y and +y for the rounded cylinder) |
|
2: the side, the base |
|
4: the outer wall, the inner wall, the two ends |
|
the sides around the axis from the first vertex, then the two caps |
|
3: the tube, the two ends |
|
2: the sphere and the cut face; the shell and its rim |
|
2: the face on either side of the edge |
the mesh distance fields |
one per face: the face’s id, its position in the file |
any other shape (a sphere, a torus, a fused part) |
1: part 0 |
A shape of your own gets parts by defining getNumParts() and closestPart(localPoint) (see
hasParts). The part is computed only by closestSurface() and
firstSurface(), once, for the deciding leaf; a mesh’s costs one more closest-face search of that mesh.
Surface labels
A leaf number is fine for a program that builds its expression itself, but it depends on the expression’s
order: an edit earlier in the expression renumbers everything after it. A name does not.
SurfaceLabel(object, "name") names the surfaces of an object, and every leaf below it gets the label, unless
a part of it has a label of its own: on a composition (a union, say) the label is a default for its operands.
Labeling an object again, directly or through moves (a move does not make another object), replaces its label. Labels change nothing in how the object is evaluated, and nothing has to be
labeled: an unlabeled leaf has no label (SurfaceLabelId::none()). Several objects may share a name, and so
a label.
A label is a
SurfaceLabelId, a number typed by what it labels, so it
cannot be mixed up with another kind of number; value() is the number, for indexing an array of your own.
const Expr<T> part = Difference(Intersection(SurfaceLabel(box, "wall"), ball), SurfaceLabel(Union(drills), "bores"));
const Tape<T> tape(pool, part);
const ExprIndex<T> index(part);
std::vector<Wall> walls(tape.getNumSurfaceLabels()); // per-label data of your own
walls[index.findSurfaceLabel("wall").value()] = ...; // findSurfaceLabel() is none for an unknown name
walls[index.findSurfaceLabel("bores").value()] = ...;
const SurfaceLabelId label = tape.getSurfaceLabel(tape.closestLeaf(p)); // or tape.getSurfaceLabel(hit.m_primitive)
A mesh’s named patches. A mesh file can name groups of its faces: an OBJ file’s g and o groups,
an ASCII STL file’s named solids. The readers keep them as
FaceGroups (PolygonSoup::getFaceGroups()), and
SurfaceLabel(mesh, groups) labels each face by its group’s name. A face’s group label wins over every label
above the mesh; a face in no group keeps those. The group names are numbered with the other labels, so
findSurfaceLabel("inlet") works as for any name, and SurfaceRef::m_surfaceLabel is the face’s label:
const auto soup = Parser::readIntoPolygonSoup<T>("pump.obj"); // groups inlet, outlet, casing
const auto pump = Parser::readIntoTriMeshSDF<T>("pump.obj", pool);
const Expr<T> part = Difference(SurfaceLabel(pump, soup.getFaceGroups()), SurfaceLabel(bore, "bore"));
const Tape<T> tape(pool, part);
const SurfaceRef<T> s = tape.closestSurface(p); // s.m_surfaceLabel: inlet, outlet, casing or bore
Any shape with parts can be labeled this way, with one group per part. SurfaceLabel(object, groups) takes one
shape, not a composition: label it before translating or combining it.
The tape reports a leaf’s label with getSurfaceLabel(), on the host and inside a GPU kernel, so a kernel
(a particle hitting a wall, say) looks up per-label data in an array of its own. The label is a number;
the names live on the host, in an
ExprIndex built from the same expression. Both
number the labels in the order their names first appear in the expression, depth first (a mesh’s group names
after its own label), so the same expression gives the same numbers to both. getPartSurfaceLabel(leaf, part) is
a part’s label. A tape whose expression has no label stores none, and one whose shapes’ parts are not
labeled stores no table of part labels.
An ExprIndex also numbers the expression’s nodes, depth first like the leaves, and gives each node its
parent, its operation and the leaves below it. The leaves below a node are always a contiguous range of
leaf numbers, and so are the surfaces below it (m_firstSurface and m_numSurfaces), so a group of
surfaces (a whole subexpression) can be handed to a kernel as two numbers. getLeafOfSurface(surface)
finds a surface’s leaf, and pathOf(leaf) lists the nodes from the root down to a leaf: the levels a
surface belongs to. The nodes are
those the expression is built of: Difference(a, b) is an intersection of a with the complement of
b, and translations, rotations and scalings applied one after another are one transform, unless one of
them is labeled.
A FiniteRepetition is one surface: all its copies are the leaf they copy, with its label and
parts. Where each copy needs its own identity, place the copies as objects of their own, in a union
(Union(copies, Accelerate::BVH) for many): each is then a leaf, or a member of a BVH union, with a number
and a label of its own.
Checking the names. A label can name something that has no surface on the object: a part a union covers
entirely, or a hole that misses the block it was meant to cut; a region can have no volume, buried under later
regions (see Regions, materials and domains). Nothing fails then: a boundary condition attached to that label is
simply never applied. ExprIndex::checkLabels(expr, tape) looks for such names (surface labels, regions and
their materials) and returns a warning for each one it finds, without printing anything or changing the object;
making the geometry right is up to you. It evaluates the shapes through the tape compiled from the expression:
const Expr<T> object = Union(SurfaceLabel(SphereSDF<T>(center, T(1)), "shell"),
SurfaceLabel(SphereSDF<T>(center, T(0.5)), "core"));
const Tape<T> tape(pool, object);
for (const std::string& warning : ExprIndex<T>(object).checkLabels(object, tape)) {
std::cerr << warning << '\n';
}
// surface label "core" cannot reach the boundary: leaf 1: it lies inside another operand of the union at node 0
The check is conservative: a name it reports is lost, but it reports only what it can prove from boxes and shape values, on the host. It says nothing about a name under an operation that changes values rather than shapes (an offset, a blur, a smooth union), and nothing about surfaces that coincide exactly. It also warns about a label that every leaf below it overrides with a label of its own. A core inside a shell has no surface of its own on a union, whatever its label; when the core is to keep its surface, as a second material, the two are regions (see Regions, materials and domains), and the core’s surface is the interface between them.
Along a ray
The point queries have ray counterparts (see Rays and segments), named after what they find: closest...
asks about a point, first... about a ray.
Finds |
At a point |
Along a ray |
|---|---|---|
the value, or the crossing |
|
|
the tape’s leaf |
|
|
the surface, drilled, its part and label |
|
|
the region |
|
|
firstSurface(ray) (a member of Tape, and a free function for any shape, with a segment form like
firstHit()’s) returns a
SurfaceHitT: the crossing as firstHit() reports it
(m_hit), and the surface crossed (m_surface), drilled into collections down to the member and its part,
with its label. A miss costs what firstHit() costs; a hit, one more evaluation at the crossing. A kernel
that bounces particles off a geometry reads the surface’s label, the mesh face crossed and the sphere of a
cloud from one call.
HitT::m_primitive is the finest thing a shape can name without drilling: for a tape, the leaf crossed,
numbered as closestLeaf() numbers the leaves; for a mesh distance field, the face (its part); for a BVH
union (not a smooth one), the member crossed (its surface), in the order its constructor was given them; for a
sphere cloud, the sphere; and for any other shape (an analytic shape, a fused composition), 0. The renderer
colors by this number with
Render::ColorBy::Primitive, from a table of colors of your own or a built-in palette, and a click in
the viewer picks the part under the cursor by it (see Rendering).
On the host and on a device
closestLeaf(), closestSurface(), firstSurface(), getFirstSurface(), isPrimitive(),
primitive() and visitPrimitive() can be called on the host and inside a GPU kernel, like
signedDistance(). closestLeaf() evaluates the composition
the way signedDistance() does, carrying the deciding leaf and its local point along with every
intermediate value, so it costs somewhat more than signedDistance(). The two are compiled
separately: signedDistance() carries nothing extra, and a program that never asks for the deciding
leaf pays nothing for it.
Only a tape has closestLeaf(). A fused composition is one shape with one surface and names no
parts; to tell parts apart, make them leaves of a tape (each part can be a fused composition itself).
Examples
Surface labels bores a cylinder through a mesh, labels the two parts, names each point’s part with
closestLeaf()and each ray’s withHitT::m_primitive, looks its label up in a table of its own, and renders the object colored by part.Basic CSG prints the deciding leaf at a few points of the classic CSG solid, a cube intersected with a ball and drilled along three axes.
The AMReX
SurfaceLabelsintegration (see Integrations) labels every cut cell with its deciding leaf and, on the mesh, with the mesh’s closest face at the local point, on a GPU when AMReX is built for one.