https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
Triangle Class Reference

Triangle geometry helper. More...

#include <Triangle.h>

Inheritance diagram for Triangle:
[legend]

Public Member Functions

 Triangle ()=default
 
 Triangle (const libMesh::Point &p0, const libMesh::Point &p1, const libMesh::Point &p2)
 
 ~Triangle () override=default
 
libMesh::Point normal () const
 Normal vector of the triangle.
 
bool intersect (const LineSegment &l, libMesh::Point &intersect_p) const
 Check if a line segment intersects this triangle.
 
bool intersect (const LineSegment &line_segment) const override
 Check if a line segment intersects this triangle, without returning the intersection point.
 
Ball computeBoundingBall () const override
 Compute a bounding ball for this triangle.
 

Private Attributes

libMesh::Point _p0
 
libMesh::Point _p1
 
libMesh::Point _p2
 

Detailed Description

Triangle geometry helper.

Definition at line 21 of file Triangle.h.

Constructor & Destructor Documentation

◆ Triangle() [1/2]

Triangle::Triangle ( )
default

◆ Triangle() [2/2]

Triangle::Triangle ( const libMesh::Point p0,
const libMesh::Point p1,
const libMesh::Point p2 
)

Definition at line 19 of file Triangle.C.

19 : _p0(p0), _p1(p1), _p2(p2)
20{
21}
libMesh::Point _p2
Definition Triangle.h:50
libMesh::Point _p0
Definition Triangle.h:50
libMesh::Point _p1
Definition Triangle.h:50

◆ ~Triangle()

Triangle::~Triangle ( )
overridedefault

Member Function Documentation

◆ computeBoundingBall()

Ball Triangle::computeBoundingBall ( ) const
overridevirtual

Compute a bounding ball for this triangle.

Implements GeometryBase.

Definition at line 91 of file Triangle.C.

92{
93 const Point centroid = (_p0 + _p1 + _p2) / 3.0;
94
95 Real max_sq_dist = 0.0;
96 for (const auto * p : {&_p0, &_p1, &_p2})
97 {
98 const Real dist_sq = (*p - centroid).norm_sq();
99 if (dist_sq > max_sq_dist)
100 max_sq_dist = dist_sq;
101 }
102
103 const Real radius = std::sqrt(max_sq_dist);
104 return Ball(centroid, radius);
105}
Ball primitive: a circle in 2D or a sphere in 3D.
Definition Ball.h:35
auto norm_sq(const T &a)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Real radius

Referenced by SurfaceTri3::computeBoundingBall().

◆ intersect() [1/2]

bool Triangle::intersect ( const LineSegment l,
libMesh::Point intersect_p 
) const

Check if a line segment intersects this triangle.

Definition at line 38 of file Triangle.C.

39{
40 const auto & a = line_segment.start();
41 const auto & b = line_segment.end();
42
43 // (a) Build basic vectors and choose a robust tolerance
44 const Point dir = b - a;
45
46 const Point edge1 = _p1 - _p0;
47 const Point edge2 = _p2 - _p0;
48 constexpr Real eps = libMesh::TOLERANCE;
49
50 // (b) Test for parallel or degenerate configuration
51 const Point pvec = dir.cross(edge2);
52 const Real det = edge1 * pvec;
53 if (std::abs(det) < eps)
54 return false; // ray nearly parallel to triangle
55
56 const Real inv_det = 1.0 / det;
57
58 // (c) Compute barycentric coordinate u and check 0 <= u <= 1
59 const Point tvec = a - _p0;
60 const Real u = (tvec * pvec) * inv_det;
61 if (!MooseUtils::absoluteFuzzyGreaterEqual(u, 0.0, eps) ||
62 !MooseUtils::absoluteFuzzyLessEqual(u, 1.0, eps))
63 return false;
64
65 // (d) Compute barycentric coordinate v and check 0 <= v and u + v <= 1
66 const Point qvec = tvec.cross(edge1);
67 const Real v = (dir * qvec) * inv_det;
68 if (!MooseUtils::absoluteFuzzyGreaterEqual(v, 0.0, eps) ||
69 !MooseUtils::absoluteFuzzyLessEqual(u + v, 1.0, eps))
70 return false;
71
72 // (e) Locate intersection along line segment (0 <= t <= 1 constrains to a--b)
73 const Real t = (edge2 * qvec) * inv_det;
74 if (!MooseUtils::absoluteFuzzyGreaterEqual(t, 0.0, eps) ||
75 !MooseUtils::absoluteFuzzyLessEqual(t, 1.0, eps))
76 return false;
77
78 // (f) Intersection lies inside both the triangle and the segment
79 intersect_p = a + dir * t;
80 return true;
81}
TypeVector< typename CompareTypes< Real, T2 >::supertype > cross(const TypeVector< T2 > &v) const
int eps(unsigned int i, unsigned int j)
2D version
static constexpr Real TOLERANCE

Referenced by SurfaceTri3::intersect(), and intersect().

◆ intersect() [2/2]

bool Triangle::intersect ( const LineSegment line_segment) const
overridevirtual

Check if a line segment intersects this triangle, without returning the intersection point.

Implements GeometryBase.

Definition at line 84 of file Triangle.C.

85{
87 return intersect(line_segment, p);
88}
bool intersect(const LineSegment &l, libMesh::Point &intersect_p) const
Check if a line segment intersects this triangle.
Definition Triangle.C:38

◆ normal()

Point Triangle::normal ( ) const

Normal vector of the triangle.

Definition at line 24 of file Triangle.C.

25{
26 const auto v1 = _p1 - _p0;
27 const auto v2 = _p2 - _p0;
28
29 const auto n = v1.cross(v2);
30 const Real norm = n.norm();
31 if (norm == 0.0)
32 mooseError("Triangle: cannot compute the normal of a degenerate triangle.");
33
34 return n / norm;
35}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
auto norm(const T &a)

Member Data Documentation

◆ _p0

libMesh::Point Triangle::_p0
private

Definition at line 50 of file Triangle.h.

Referenced by computeBoundingBall(), and normal().

◆ _p1

libMesh::Point Triangle::_p1
private

Definition at line 50 of file Triangle.h.

Referenced by computeBoundingBall(), and normal().

◆ _p2

libMesh::Point Triangle::_p2
private

Definition at line 50 of file Triangle.h.

Referenced by computeBoundingBall(), and normal().


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