EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
EBGeometry::ConvexPolyhedronSDF< T > Class Template Reference

Signed distance field for a convex polyhedron: the intersection of the half-spaces behind a set of planes, such as a regular (Platonic) solid or a pyramid. More...

#include <EBGeometry_AnalyticDistanceFunctions.hpp>

Collaboration diagram for EBGeometry::ConvexPolyhedronSDF< T >:
Collaboration graph
[legend]

Public Member Functions

 ConvexPolyhedronSDF ()
 Default constructor. Constructs the cube [-0.5, 0.5]^3.
 
 ConvexPolyhedronSDF (const std::vector< PlaneSDF< T > > &a_planes)
 Constructs the convex polyhedron bounded by a set of planes: the points behind every plane.
 
 ConvexPolyhedronSDF (const ConvexPolyhedronSDF &)=default
 Copy constructor.
 
 ConvexPolyhedronSDF (ConvexPolyhedronSDF &&)=default
 Move constructor.
 
ConvexPolyhedronSDF & operator= (const ConvexPolyhedronSDF &)=default
 Copy assignment.
 
ConvexPolyhedronSDF & operator= (ConvexPolyhedronSDF &&)=default
 Move assignment.
 
uint32_t getNumFaces () const noexcept
 Get the number of faces.
 
const Vec3T< T > & getNormal (const uint32_t a_face) const noexcept
 Get the unit outward normal of a face.
 
const T & getOffset (const uint32_t a_face) const noexcept
 Get the offset of a face's plane: the plane is the points x with dot(getNormal(a_face), x) equal to it.
 
uint32_t getNumVertices () const noexcept
 Get the number of vertices.
 
const Vec3T< T > & getVertex (const uint32_t a_vertex) const noexcept
 Get a vertex.
 
uint32_t getNumEdges () const noexcept
 Get the number of edges.
 
uint32_t getEdgeVertex (const uint32_t a_edge, const uint32_t a_end) const noexcept
 Get one end of an edge.
 
T signedDistance (const Vec3T< T > &a_point) const noexcept
 Signed distance function for the polyhedron.
 
uint32_t getNumParts () const noexcept
 How many parts the shape has (see hasParts): its faces, getNumFaces().
 
uint32_t closestPart (const Vec3T< T > &a_point) const noexcept
 The part whose term decides the value at a point (see hasParts): the face whose plane is farthest above the point (the lowest such face on a tie). Inside, that is the nearest face; outside, the face the nearest point is on whenever that point is inside a face.
 
BoundingVolumes::AABBT< T > computeBoundingVolume () const noexcept
 The shape's axis-aligned bounding box: that of its vertices.
 

Static Public Member Functions

static ConvexPolyhedronSDF tetrahedron (const Vec3T< T > &a_center, T a_radius)
 A regular tetrahedron.
 
static ConvexPolyhedronSDF octahedron (const Vec3T< T > &a_center, T a_radius)
 A regular octahedron.
 
static ConvexPolyhedronSDF dodecahedron (const Vec3T< T > &a_center, T a_radius)
 A regular dodecahedron.
 
static ConvexPolyhedronSDF icosahedron (const Vec3T< T > &a_center, T a_radius)
 A regular icosahedron.
 
static ConvexPolyhedronSDF pyramid (const Vec3T< T > &a_baseCenter, const Vec3T< T > &a_apex, T a_radius, unsigned a_sides, const Vec3T< T > &a_vertexDirection)
 A right pyramid on a regular polygon: a square pyramid with four sides, a tetrahedron-like one with three.
 

Static Public Attributes

static constexpr DistanceQuality distanceQuality = DistanceQuality::Exact
 How far signedDistance() can be trusted as a distance: the Euclidean distance to the surface.
 
static constexpr uint32_t MaxFaces = 20U
 The most faces a polyhedron can have: those of the icosahedron.
 
static constexpr uint32_t MaxVertices = 20U
 The most vertices a polyhedron can have: those of the dodecahedron.
 
static constexpr uint32_t MaxEdges = MaxFaces + MaxVertices - 2U
 The most edges a polyhedron can have, from Euler's formula: MaxFaces + MaxVertices - 2.
 

Protected Member Functions

void build (const std::vector< Vec3T< double > > &a_points, const std::vector< Vec3T< double > > &a_normals, double a_epsilon)
 Sets the polyhedron to the one bounded by planes given in double precision: finds its vertices and edges, and checks it (see the constructor from planes).
 

Static Protected Member Functions

static std::vector< PlaneSDF< T > > cubePlanes ()
 The six planes of the cube [-0.5, 0.5]^3, the default polyhedron.
 
static std::vector< Vec3T< T > > goldenDirections (T a_small, T a_large)
 The twelve directions (0, +-a, +-b), (+-a, +-b, 0) and (+-b, 0, +-a), in that order, the signs within each group in the order (+, +), (+, -), (-, +), (-, -).
 
static ConvexPolyhedronSDF fromNormals (const Vec3T< T > &a_center, const std::vector< Vec3T< T > > &a_vertexDirections, const std::vector< Vec3T< T > > &a_normals, T a_radius, const char *a_who)
 A regular solid from the directions of its vertices and of its faces' normals: each face's plane is as far out along its normal as the farthest vertex, the vertices being at the circumradius from the center.
 

Protected Attributes

uint32_t m_numFaces = 0U
 Number of faces.
 
uint32_t m_numVertices = 0U
 Number of vertices.
 
uint32_t m_numEdges = 0U
 Number of edges.
 
Array< Vec3T< T >, MaxFaces > m_normals
 Unit outward normal of each face.
 
Array< T, MaxFaces > m_offsets
 Offset of each face's plane along its normal.
 
Array< Vec3T< T >, MaxVertices > m_vertices
 The vertices.
 
Array< uint8_t, 2U *MaxEdges > m_edges
 The edges, as the indices of their two vertices: edge e from m_edges[2 e] to m_edges[2 e + 1].
 

Detailed Description

template<class T>
class EBGeometry::ConvexPolyhedronSDF< T >

Signed distance field for a convex polyhedron: the intersection of the half-spaces behind a set of planes, such as a regular (Platonic) solid or a pyramid.

The distance is exact. Inside, it is that to the nearest face plane. Outside, it is that to the face whose plane is farthest from the point when the point's projection onto that plane lies on the face, and otherwise that to the nearest edge or vertex: the largest plane distance alone, which is all most formulas take, is a bound at the edges and corners. The faces, edges and vertices are held in the shape itself, in arrays of fixed size, so the polyhedron is trivially copyable and needs no pool; it has at most MaxFaces faces and MaxVertices vertices. A polyhedron larger than that is a mesh (MeshSDF, TriMeshSDF), which has no limit.

The regular solids and the pyramid are made by the static functions tetrahedron(), octahedron(), dodecahedron(), icosahedron() and pyramid(), which return a ConvexPolyhedronSDF; any other convex polyhedron is made from its planes, with the constructor. A box is BoxSDF and a regular prism PolygonPrismSDF, both cheaper to evaluate than the same solid as a polyhedron. Round its edges with Offset of a slightly smaller polyhedron. By default the polyhedron is the cube [-0.5, 0.5]^3.

Template Parameters
TFloating-point precision.

Constructor & Destructor Documentation

◆ ConvexPolyhedronSDF()

template<class T >
EBGeometry::ConvexPolyhedronSDF< T >::ConvexPolyhedronSDF ( const std::vector< PlaneSDF< T > > &  a_planes)
inlineexplicit

Constructs the convex polyhedron bounded by a set of planes: the points behind every plane.

Runs on the host. The vertices and edges are found from the planes, in double precision, to within a few units in the last place of T times the size of the coordinates. In float, a solid much smaller than its distance from the origin can therefore not be told apart from rounding: build it near the origin and move it with Translate. Face k is the face on plane k, which is also the shape's part k.

Parameters
[in]a_planesThe planes, each with its normal pointing out of the solid (PlaneSDF's sign: the solid is where every plane's value is negative). There must be from 4 to MaxFaces of them, each must bound a face of the solid (no plane may miss it or only touch it at an edge or a vertex), the solid they bound must be finite and have a volume, and it must have at most MaxVertices vertices.

Member Function Documentation

◆ build()

template<class T >
void EBGeometry::ConvexPolyhedronSDF< T >::build ( const std::vector< Vec3T< double > > &  a_points,
const std::vector< Vec3T< double > > &  a_normals,
double  a_epsilon 
)
inlineprotected

Sets the polyhedron to the one bounded by planes given in double precision: finds its vertices and edges, and checks it (see the constructor from planes).

Parameters
[in]a_pointsA point on each plane.
[in]a_normalsEach plane's outward normal, not necessarily of unit length.
[in]a_epsilonThe machine epsilon of the precision the planes are exact in, which sets the tolerance for where they meet.

◆ closestPart()

template<class T >
uint32_t EBGeometry::ConvexPolyhedronSDF< T >::closestPart ( const Vec3T< T > &  a_point) const
inlinenoexcept

The part whose term decides the value at a point (see hasParts): the face whose plane is farthest above the point (the lowest such face on a tie). Inside, that is the nearest face; outside, the face the nearest point is on whenever that point is inside a face.

Parameters
[in]a_pointPosition.
Returns
The part.

◆ computeBoundingVolume()

template<class T >
BoundingVolumes::AABBT< T > EBGeometry::ConvexPolyhedronSDF< T >::computeBoundingVolume ( ) const
inlinenoexcept

The shape's axis-aligned bounding box: that of its vertices.

Returns
The bounding box.

◆ cubePlanes()

template<class T >
std::vector< PlaneSDF< T > > EBGeometry::ConvexPolyhedronSDF< T >::cubePlanes ( )
inlinestaticprotected

The six planes of the cube [-0.5, 0.5]^3, the default polyhedron.

Returns
The planes.

◆ dodecahedron()

template<class T >
ConvexPolyhedronSDF< T > EBGeometry::ConvexPolyhedronSDF< T >::dodecahedron ( const Vec3T< T > &  a_center,
T  a_radius 
)
inlinestatic

A regular dodecahedron.

In its standard orientation, in which the three coordinate planes through the center are mirror planes: its vertices are in the directions (+-1, +-1, +-1), (0, +-1/phi, +-phi), (+-1/phi, +-phi, 0) and (+-phi, 0, +-1/phi), with phi the golden ratio; turn it with Rotate. Its twelve faces have their outward normals along (0, +-phi, +-1), (+-phi, +-1, 0) and (+-1, 0, +-phi), numbered in that order, the signs within each group in the order (+, +), (+, -), (-, +), (-, -).

Parameters
[in]a_centerCenter.
[in]a_radiusCircumradius: the distance from the center to a vertex, finite and positive.
Returns
The dodecahedron.

◆ fromNormals()

template<class T >
ConvexPolyhedronSDF< T > EBGeometry::ConvexPolyhedronSDF< T >::fromNormals ( const Vec3T< T > &  a_center,
const std::vector< Vec3T< T > > &  a_vertexDirections,
const std::vector< Vec3T< T > > &  a_normals,
T  a_radius,
const char *  a_who 
)
inlinestaticprotected

A regular solid from the directions of its vertices and of its faces' normals: each face's plane is as far out along its normal as the farthest vertex, the vertices being at the circumradius from the center.

Parameters
[in]a_centerCenter.
[in]a_vertexDirectionsDirections from the center to the vertices.
[in]a_normalsThe faces' outward normals, in the faces' order.
[in]a_radiusCircumradius, finite and positive.
[in]a_whoThe factory's name, for the messages.
Returns
The solid.

◆ getEdgeVertex()

template<class T >
uint32_t EBGeometry::ConvexPolyhedronSDF< T >::getEdgeVertex ( const uint32_t  a_edge,
const uint32_t  a_end 
) const
inlinenoexcept

Get one end of an edge.

Parameters
[in]a_edgeThe edge, below getNumEdges().
[in]a_end0 or 1.
Returns
The index of the vertex at that end.

◆ getNormal()

template<class T >
const Vec3T< T > & EBGeometry::ConvexPolyhedronSDF< T >::getNormal ( const uint32_t  a_face) const
inlinenoexcept

Get the unit outward normal of a face.

Parameters
[in]a_faceThe face, below getNumFaces().
Returns
The normal.

◆ getNumEdges()

template<class T >
uint32_t EBGeometry::ConvexPolyhedronSDF< T >::getNumEdges ( ) const
inlinenoexcept

Get the number of edges.

Returns
m_numEdges.

◆ getNumFaces()

template<class T >
uint32_t EBGeometry::ConvexPolyhedronSDF< T >::getNumFaces ( ) const
inlinenoexcept

Get the number of faces.

Returns
m_numFaces.

◆ getNumParts()

template<class T >
uint32_t EBGeometry::ConvexPolyhedronSDF< T >::getNumParts ( ) const
inlinenoexcept

How many parts the shape has (see hasParts): its faces, getNumFaces().

Returns
The count.

◆ getNumVertices()

template<class T >
uint32_t EBGeometry::ConvexPolyhedronSDF< T >::getNumVertices ( ) const
inlinenoexcept

Get the number of vertices.

Returns
m_numVertices.

◆ getOffset()

template<class T >
const T & EBGeometry::ConvexPolyhedronSDF< T >::getOffset ( const uint32_t  a_face) const
inlinenoexcept

Get the offset of a face's plane: the plane is the points x with dot(getNormal(a_face), x) equal to it.

Parameters
[in]a_faceThe face, below getNumFaces().
Returns
The offset.

◆ getVertex()

template<class T >
const Vec3T< T > & EBGeometry::ConvexPolyhedronSDF< T >::getVertex ( const uint32_t  a_vertex) const
inlinenoexcept

Get a vertex.

Parameters
[in]a_vertexThe vertex, below getNumVertices().
Returns
Its position.

◆ goldenDirections()

template<class T >
std::vector< Vec3T< T > > EBGeometry::ConvexPolyhedronSDF< T >::goldenDirections ( T  a_small,
T  a_large 
)
inlinestaticprotected

The twelve directions (0, +-a, +-b), (+-a, +-b, 0) and (+-b, 0, +-a), in that order, the signs within each group in the order (+, +), (+, -), (-, +), (-, -).

Parameters
[in]a_smallThe first magnitude, a.
[in]a_largeThe second magnitude, b.
Returns
The directions.

◆ icosahedron()

template<class T >
ConvexPolyhedronSDF< T > EBGeometry::ConvexPolyhedronSDF< T >::icosahedron ( const Vec3T< T > &  a_center,
T  a_radius 
)
inlinestatic

A regular icosahedron.

In its standard orientation, in which the three coordinate planes through the center are mirror planes: its vertices are in the directions (0, +-1, +-phi), (+-1, +-phi, 0) and (+-phi, 0, +-1), with phi the golden ratio; turn it with Rotate. Its twenty faces have their outward normals along (+-1, +-1, +-1), (0, +-phi, +-1/phi), (+-phi, +-1/phi, 0) and (+-1/phi, 0, +-phi), numbered in that order: the first eight with the signs of x, y and z counted as the bits of 4 i + 2 j + k (a minus sign for a set bit), the others with their two signs in the order (+, +), (+, -), (-, +), (-, -).

Parameters
[in]a_centerCenter.
[in]a_radiusCircumradius: the distance from the center to a vertex, finite and positive.
Returns
The icosahedron.

◆ octahedron()

template<class T >
ConvexPolyhedronSDF< T > EBGeometry::ConvexPolyhedronSDF< T >::octahedron ( const Vec3T< T > &  a_center,
T  a_radius 
)
inlinestatic

A regular octahedron.

Its six vertices are on the coordinate axes through the center, so that the solid is |x| + |y| + |z| <= radius around it; turn it with Rotate. Face 4 i + 2 j + k is the one whose outward normal is ((-1)^i, (-1)^j, (-1)^k) / sqrt(3). The inradius is the circumradius over sqrt(3).

Parameters
[in]a_centerCenter.
[in]a_radiusCircumradius: the distance from the center to a vertex, finite and positive.
Returns
The octahedron.

◆ operator=() [1/2]

template<class T >
ConvexPolyhedronSDF & EBGeometry::ConvexPolyhedronSDF< T >::operator= ( const ConvexPolyhedronSDF< T > &  )
default

Copy assignment.

Returns
Reference to (*this).

◆ operator=() [2/2]

template<class T >
ConvexPolyhedronSDF & EBGeometry::ConvexPolyhedronSDF< T >::operator= ( ConvexPolyhedronSDF< T > &&  )
default

Move assignment.

Returns
Reference to (*this).

◆ pyramid()

template<class T >
ConvexPolyhedronSDF< T > EBGeometry::ConvexPolyhedronSDF< T >::pyramid ( const Vec3T< T > &  a_baseCenter,
const Vec3T< T > &  a_apex,
T  a_radius,
unsigned  a_sides,
const Vec3T< T > &  a_vertexDirection 
)
inlinestatic

A right pyramid on a regular polygon: a square pyramid with four sides, a tetrahedron-like one with three.

As for PolygonPrismSDF, the base is a regular polygon of n sides around the axis from the base center to the apex, with one vertex along the given direction. Faces 0 to n - 1 are the sides, counted around the axis from the one that starts at that vertex, as PolygonPrismSDF counts its sides; face n is the base.

Parameters
[in]a_baseCenterCenter of the base.
[in]a_apexThe apex; it must differ from a_baseCenter.
[in]a_radiusCircumradius of the base: the distance from the axis to a base vertex, finite and positive.
[in]a_sidesNumber of sides, from 3 to MaxVertices - 1.
[in]a_vertexDirectionDirection from the axis toward one base vertex. Only its part across the axis is used, and that must not be zero.
Returns
The pyramid.

◆ signedDistance()

template<class T >
T EBGeometry::ConvexPolyhedronSDF< T >::signedDistance ( const Vec3T< T > &  a_point) const
inlinenoexcept

Signed distance function for the polyhedron.

Out of line on a device (its loops over the faces and edges are the whole distance): inlined, it would be copied into every level of every tape kernel, whatever shapes the tape holds, which makes the device compile of every tape kernel much slower. A call costs little beside those loops.

Parameters
[in]a_pointPosition.
Returns
Signed distance from a_point to the surface; negative inside.

◆ tetrahedron()

template<class T >
ConvexPolyhedronSDF< T > EBGeometry::ConvexPolyhedronSDF< T >::tetrahedron ( const Vec3T< T > &  a_center,
T  a_radius 
)
inlinestatic

A regular tetrahedron.

Its vertices are at four alternate corners of a cube around the center, in the directions (1, 1, 1), (1, -1, -1), (-1, 1, -1) and (-1, -1, 1), in that order; turn it with Rotate. Face k is the one opposite vertex k. The inradius is a third of the circumradius.

Parameters
[in]a_centerCenter.
[in]a_radiusCircumradius: the distance from the center to a vertex, finite and positive.
Returns
The tetrahedron.

The documentation for this class was generated from the following file: