Ocean
Triangle.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MATH_TRIANGLE_H
9 #define META_OCEAN_MATH_TRIANGLE_H
10 
11 #include "ocean/math/Math.h"
12 #include "ocean/math/Vector3.h"
13 
14 namespace Ocean
15 {
16 
17 // Forward declaration.
18 template <typename T> class TriangleT;
19 
20 /**
21  * Definition of the Triangle object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single or double precision float data type.
22  * @see TriangleT
23  * @ingroup math
24  */
26 
27 /**
28  * Instantiation of the TriangleT template class using a double precision float data type.
29  * @see TriangleT
30  * @ingroup math
31  */
33 
34 /**
35  * Instantiation of the TriangleT template class using a single precision float data type.
36  * @see TriangleT
37  * @ingroup math
38  */
40 
41 /**
42  * This class implements a base class for all triangle classes.
43  * @tparam T Data type used to represent coordinates
44  * @see TriangleF, TriangleD, TriangleT2, TriangleT3.
45  * @ingroup math
46  */
47 template <typename T>
48 class TriangleT
49 {
50  public:
51 
52  /**
53  * Returns whether a given point, specified as barycentric coordinate, lies inside a triangle.
54  * @param barycentricPoint Point to be checked
55  * @return True, if so
56  */
57  static bool isBarycentricInside(const VectorT3<T>& barycentricPoint);
58 
59  /**
60  * Returns whether the a barycentric coordinate is valid.
61  * @param barycentric Barycentric coordinate to test
62  * @param epsilon Accuracy epsilon, with range [0, infinity)
63  * @return True, if so
64  */
65  static bool isValidBarycentric(const VectorT3<T>& barycentric, const T& epsilon = NumericT<T>::eps());
66 };
67 
68 template <typename T>
69 bool TriangleT<T>::isBarycentricInside(const VectorT3<T>& barycentricPoint)
70 {
71  ocean_assert_accuracy((std::is_same<Scalar, float>::value) || isValidBarycentric(barycentricPoint));
72 
73  return barycentricPoint[0] >= -NumericT<T>::eps() && barycentricPoint[1] >= -NumericT<T>::eps() && barycentricPoint[2] >= -NumericT<T>::eps();
74 }
75 
76 template <typename T>
77 bool TriangleT<T>::isValidBarycentric(const VectorT3<T>& barycentric, const T& epsilon)
78 {
79  return NumericT<T>::isEqual(barycentric[0] + barycentric[1] + barycentric[2], T(1), epsilon);
80 }
81 
82 }
83 
84 #endif // META_OCEAN_MATH_TRIANGLE_H
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static constexpr T eps()
Returns a small epsilon.
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition: Numeric.h:2386
This class implements a base class for all triangle classes.
Definition: Triangle.h:49
static bool isBarycentricInside(const VectorT3< T > &barycentricPoint)
Returns whether a given point, specified as barycentric coordinate, lies inside a triangle.
Definition: Triangle.h:69
static bool isValidBarycentric(const VectorT3< T > &barycentric, const T &epsilon=NumericT< T >::eps())
Returns whether the a barycentric coordinate is valid.
Definition: Triangle.h:77
This class implements a vector with three elements.
Definition: Vector3.h:97
TriangleT< Scalar > Triangle
Definition of the Triangle object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with singl...
Definition: Triangle.h:18
TriangleT< double > TriangleD
Instantiation of the TriangleT template class using a double precision float data type.
Definition: Triangle.h:32
TriangleT< float > TriangleF
Instantiation of the TriangleT template class using a single precision float data type.
Definition: Triangle.h:39
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15