https://mooseframework.inl.gov
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. More...
 
bool intersect (const LineSegment &l, libMesh::Point &intersect_p) const
 Check if a line segment intersects this triangle. More...
 
bool intersect (const LineSegment &line_segment) const override
 Check if a line segment intersects this triangle, without returning the intersection point. More...
 
Ball computeBoundingBall () const override
 Compute a bounding ball for this triangle. More...
 

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 18 of file Triangle.C.

18  : _p0(p0), _p1(p1), _p2(p2)
19 {
20 }
libMesh::Point _p2
Definition: Triangle.h:50
libMesh::Point _p1
Definition: Triangle.h:50
libMesh::Point _p0
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 88 of file Triangle.C.

89 {
90  const Point centroid = (_p0 + _p1 + _p2) / 3.0;
91 
92  Real max_sq_dist = 0.0;
93  for (const auto * p : {&_p0, &_p1, &_p2})
94  {
95  const Real dist_sq = (*p - centroid).norm_sq();
96  if (dist_sq > max_sq_dist)
97  max_sq_dist = dist_sq;
98  }
99 
100  const Real radius = std::sqrt(max_sq_dist);
101  return Ball(centroid, radius);
102 }
Ball primitive: a circle in 2D or a sphere in 3D.
Definition: Ball.h:34
const Real radius
auto norm_sq(const T &a)
libMesh::Point _p2
Definition: Triangle.h:50
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
libMesh::Point _p1
Definition: Triangle.h:50
libMesh::Point _p0
Definition: Triangle.h:50

◆ 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 35 of file Triangle.C.

Referenced by intersect().

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

◆ 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 81 of file Triangle.C.

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

◆ normal()

Point Triangle::normal ( ) const

Normal vector of the triangle.

Definition at line 23 of file Triangle.C.

24 {
25  const auto v1 = _p1 - _p0;
26  const auto v2 = _p2 - _p0;
27 
28  auto n = v1.cross(v2);
29  n /= n.norm();
30 
31  return n;
32 }
libMesh::Point _p2
Definition: Triangle.h:50
TypeVector< typename CompareTypes< Real, T2 >::supertype > cross(const TypeVector< T2 > &v) const
libMesh::Point _p1
Definition: Triangle.h:50
libMesh::Point _p0
Definition: Triangle.h:50

Member Data Documentation

◆ _p0

libMesh::Point Triangle::_p0
private

Definition at line 50 of file Triangle.h.

Referenced by computeBoundingBall(), intersect(), and normal().

◆ _p1

libMesh::Point Triangle::_p1
private

Definition at line 50 of file Triangle.h.

Referenced by computeBoundingBall(), intersect(), and normal().

◆ _p2

libMesh::Point Triangle::_p2
private

Definition at line 50 of file Triangle.h.

Referenced by computeBoundingBall(), intersect(), and normal().


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