EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
Classes | Functions
EBGeometry::TriangleDistance Namespace Reference

The triangle signed-distance kernel. More...

Classes

struct  Vec3V
 A 3D vector of V, where V is a scalar or a SIMD::Pack: x, y and z each hold one lane per triangle. More...
 

Functions

template<class V , class Source >
EBGEOMETRY_HOST_ALWAYS_INLINE V signedDistance (const Source &a_triangle, const Vec3V< V > &a_point) noexcept
 Signed distance from a_point to a triangle, or to one triangle per lane.
 
template<class V , class Source >
EBGEOMETRY_HOST_ALWAYS_INLINE V unsignedDistance2 (const Source &a_triangle, const Vec3V< V > &a_point) noexcept
 The squared distance from a point to a triangle, without its sign: the candidates of signedDistance() (the face where the point projects inside it, else the nearest vertex or edge), none signed.
 
template<class T , class Source >
T closestPoint (const Source &a_triangle, const Vec3V< T > &a_point, Vec3V< T > &a_closestPoint, Vec3V< T > &a_normal) noexcept
 The closest point on a triangle to a_point, the pseudonormal there, and the signed distance.
 

Detailed Description

The triangle signed-distance kernel.

Function Documentation

◆ closestPoint()

template<class T , class Source >
T EBGeometry::TriangleDistance::closestPoint ( const Source &  a_triangle,
const Vec3V< T > &  a_point,
Vec3V< T > &  a_closestPoint,
Vec3V< T > &  a_normal 
)
inlinenoexcept

The closest point on a triangle to a_point, the pseudonormal there, and the signed distance.

One triangle, scalar only: the candidates of signedDistance() (the face where the point projects inside it, else vertex 0, 1, 2 and edge 0, 1, 2, a later one replacing the best only when strictly closer), in the same order and with the same arithmetic as its scalar branch, so the signed distance is the one signedDistance() computes. A point projecting onto the face's boundary is reported on the edge or vertex there, with that feature's pseudonormal: on the surface, the normal of the feature the point is on. It is not on any distance path: a closest-point query runs it once, on the triangle a search found nearest.

Template Parameters
TScalar type (float or double).
SourceTriangle accessor (see signedDistance()), read with V = T.
Parameters
[in]a_triangleThe triangle.
[in]a_pointThe query point.
[out]a_closestPointThe point of the triangle closest to a_point.
[out]a_normalThe pseudonormal of the feature a_closestPoint lies on: the face normal, an edge's or a vertex's.
Returns
The signed distance, negative behind the face normal.

◆ signedDistance()

template<class V , class Source >
EBGEOMETRY_HOST_ALWAYS_INLINE V EBGeometry::TriangleDistance::signedDistance ( const Source &  a_triangle,
const Vec3V< V > &  a_point 
)
inlinenoexcept

Signed distance from a_point to a triangle, or to one triangle per lane.

Where the point projects inside the triangle, the distance is the signed distance to its plane. Elsewhere it is the distance to the closest vertex or edge, signed by that feature's pseudonormal. The candidates are compared by squared distance in the order vertex 0, 1, 2 and edge 0, 1, 2, a later one replacing the best only when strictly closer, and there is one square root, at the end. A sign test (s > 0) gives -1 at zero and at NaN. For a SIMD::Pack every lane computes every candidate, so the kernel has no branches. For a scalar (a device thread, Triangle, a build without SIMD) it branches instead: it returns at once for a point projecting inside the face, and signs only the closest candidate, with the same candidates, order and arithmetic, so the result is the same.

The triangle is read through a_triangle, at the point each value is needed, so that a SIMD caller loads straight from its arrays into registers rather than staging the whole triangle in memory first. It provides, as Vec3V<V> by value:

  • vertex(k): vertex position k, for k = 0, 1, 2;
  • normal(): the face normal;
  • vertexNormal(k): the pseudonormal of vertex k;
  • edgeNormal(k): the pseudonormal of edge k, from vertex k to vertex k + 1 (mod 3).
    Template Parameters
    VScalar (float or double) or SIMD::Pack.
    SourceTriangle accessor, as above.
    Parameters
    [in]a_triangleTriangle accessor.
    [in]a_pointQuery point, the same in every lane.
    Returns
    The signed distance, negative behind the face normal.

◆ unsignedDistance2()

template<class V , class Source >
EBGEOMETRY_HOST_ALWAYS_INLINE V EBGeometry::TriangleDistance::unsignedDistance2 ( const Source &  a_triangle,
const Vec3V< V > &  a_point 
)
inlinenoexcept

The squared distance from a point to a triangle, without its sign: the candidates of signedDistance() (the face where the point projects inside it, else the nearest vertex or edge), none signed.

A nearest-triangle search compares these and signs only the winner, with signedDistance() on that triangle: the six pseudonormal dot products and the square root are then paid once per query, not once per triangle. The square root of this value equals |signedDistance()| to rounding, so the winner is the triangle a search on signedDistance() picks, except where two triangles are equally near to rounding. With V a scalar, the inside case returns early, as signedDistance()'s does.

Template Parameters
VScalar or SIMD::Pack.
SourceA triangle accessor (see signedDistance()).
Parameters
[in]a_triangleThe triangle(s).
[in]a_pointThe point, broadcast to every lane.
Returns
The squared distance, one per lane.