SOFA API  ca70b0f0
Open source framework for multi-physics simuation
sofa::geometry::Triangle Struct Reference

#include <Triangle.h>

Static Public Attributes

static constexpr sofa::Size NumberOfNodes = 3
 
static constexpr ElementType Element_type = ElementType::TRIANGLE
 

Public Member Functions

 Triangle ()=delete
 

Static Public Member Functions

template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr auto area (const Node &n0, const Node &n1, const Node &n2)
 Compute the area of a triangle. More...
 
template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr auto getBarycentricCoordinates (const Node &p0, const Node &n0, const Node &n1, const Node &n2)
 Compute the barycentric coordinates of the input point in the Triangle. It can be interpreted as masses placed at the vertices of Triangle (n0, n1, n2), such that the point is the center of mass of these masses. More...
 
template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr auto normal (const Node &n0, const Node &n1, const Node &n2)
 Compute the normal of a triangle. More...
 
template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr bool isPointInTriangle (const Node &p0, const Node &n0, const Node &n1, const Node &n2, sofa::type::Vec< 3, T > &baryCoefs)
 Test if input point is inside Triangle (n0, n1, n2) using Triangle. More...
 
template<typename TReal >
static constexpr bool rayIntersection (const sofa::type::Vec< 3, TReal > &n0, const sofa::type::Vec< 3, TReal > &n1, const sofa::type::Vec< 3, TReal > &n2, const sofa::type::Vec< 3, TReal > &origin, const sofa::type::Vec< 3, TReal > &direction, TReal &t, TReal &u, TReal &v)
 Test if a ray intersects a triangle, and gives barycentric coordinates of the intersection if applicable. More...
 
template<typename TReal >
static constexpr bool rayIntersection (const sofa::type::Vec< 3, TReal > &n0, const sofa::type::Vec< 3, TReal > &n1, const sofa::type::Vec< 3, TReal > &n2, const sofa::type::Vec< 3, TReal > &origin, const sofa::type::Vec< 3, TReal > &direction)
 Test if a ray intersects a triangle. More...
 

Attribute details

◆ Element_type

constexpr ElementType sofa::geometry::Triangle::Element_type = ElementType::TRIANGLE
staticconstexpr

◆ NumberOfNodes

constexpr sofa::Size sofa::geometry::Triangle::NumberOfNodes = 3
staticconstexpr

Constructor details

◆ Triangle()

sofa::geometry::Triangle::Triangle ( )
delete

Function details

◆ area()

template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr auto sofa::geometry::Triangle::area ( const Node &  n0,
const Node &  n1,
const Node &  n2 
)
inlinestaticconstexpr

Compute the area of a triangle.

Remarks
Depending of the type of Node, it will either use a optimized version or a generic one
Optimizations are enabled for sofa::type::Vec and the dimension (2D or 3D)
Template Parameters
Nodeiterable container (or sofa::type::Vec with cross() and norm())
Tscalar
Parameters
n0,n1,n2nodes of the triangle
Returns
Area of the triangle (a T scalar)

◆ getBarycentricCoordinates()

template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr auto sofa::geometry::Triangle::getBarycentricCoordinates ( const Node &  p0,
const Node &  n0,
const Node &  n1,
const Node &  n2 
)
inlinestaticconstexpr

Compute the barycentric coordinates of the input point in the Triangle. It can be interpreted as masses placed at the vertices of Triangle (n0, n1, n2), such that the point is the center of mass of these masses.

Template Parameters
Nodeiterable container
Tscalar
Parameters
p0position of the input point to compute the coefficients
n0,n1,n2nodes of the triangle
Returns
sofa::type::Vec<3, T> barycentric coefficients of each vertex of the Triangle. These masses can be zero or negative; they are all positive if and only if the point is inside the Triangle.

◆ isPointInTriangle()

template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr bool sofa::geometry::Triangle::isPointInTriangle ( const Node &  p0,
const Node &  n0,
const Node &  n1,
const Node &  n2,
sofa::type::Vec< 3, T > &  baryCoefs 
)
inlinestaticconstexpr

Test if input point is inside Triangle (n0, n1, n2) using Triangle.

See also
getBarycentricCoordinates . The point is inside the Triangle if and only if Those coordinates are all positive.
Template Parameters
Nodeiterable container
Tscalar
Parameters
p0position of the point to test
n0,n1,n2nodes of the triangle
outputparameter: sofa::type::Vec<3, T> barycentric coordinates of the input point in Triangle
Returns
bool result if point is inside Triangle.

◆ normal()

template<typename Node , typename T = std::decay_t<decltype(*std::begin(std::declval<Node>()))>, typename = std::enable_if_t<std::is_scalar_v<T>>>
static constexpr auto sofa::geometry::Triangle::normal ( const Node &  n0,
const Node &  n1,
const Node &  n2 
)
inlinestaticconstexpr

Compute the normal of a triangle.

Remarks
triangle normal computation is only possible in 3D
normal returned is not normalized
Template Parameters
Nodeiterable container (or sofa::type::Vec with cross() and norm())
Tscalar
Parameters
n0,n1,n2nodes of the triangle
Returns
Vec3 normal of this triangle

◆ rayIntersection() [1/2]

template<typename TReal >
static constexpr bool sofa::geometry::Triangle::rayIntersection ( const sofa::type::Vec< 3, TReal > &  n0,
const sofa::type::Vec< 3, TReal > &  n1,
const sofa::type::Vec< 3, TReal > &  n2,
const sofa::type::Vec< 3, TReal > &  origin,
const sofa::type::Vec< 3, TReal > &  direction 
)
inlinestaticconstexpr

Test if a ray intersects a triangle.

Remarks
Implementation for 3D only
Template Parameters
Nodeiterable container (or sofa::type::Vec with cross() and norm())
Tscalar
Parameters
n0,n1,n2nodes of the triangle
Returns
either if the given ray intersects the given triangle or not

◆ rayIntersection() [2/2]

template<typename TReal >
static constexpr bool sofa::geometry::Triangle::rayIntersection ( const sofa::type::Vec< 3, TReal > &  n0,
const sofa::type::Vec< 3, TReal > &  n1,
const sofa::type::Vec< 3, TReal > &  n2,
const sofa::type::Vec< 3, TReal > &  origin,
const sofa::type::Vec< 3, TReal > &  direction,
TReal &  t,
TReal &  u,
TReal &  v 
)
inlinestaticconstexpr

Test if a ray intersects a triangle, and gives barycentric coordinates of the intersection if applicable.

Remarks
Implementation for 3D only
Template Parameters
Nodeiterable container (or sofa::type::Vec with cross() and norm())
Tscalar
Parameters
n0,n1,n2nodes of the triangle
t,u,vbarycentric coefficients of the potential intersection in the triangle
Returns
either if the given ray intersects the given triangle or not