8 #ifndef META_OCEAN_MATH_TRIANGLE_2_H
9 #define META_OCEAN_MATH_TRIANGLE_2_H
21 template <
typename T>
class TriangleT2;
137 inline T
left()
const;
143 inline T
top()
const;
149 inline T
right()
const;
162 inline T
area()
const;
169 inline T
area2()
const;
178 void cosines(T& cosine0, T& cosine1, T& cosine2)
const;
187 inline void angles(T& angle0, T& angle1, T& angle2)
const;
385 template <
typename T>
387 barycentricFactor_(T(0))
402 template <
typename T>
408 template <
typename T>
414 template <
typename T>
420 template <
typename T>
423 return points_[0].sqrDistance(points_[1]);
426 template <
typename T>
429 return points_[0].sqrDistance(points_[2]);
432 template <
typename T>
435 return points_[1].sqrDistance(points_[2]);
438 template <
typename T>
441 ocean_assert(isValid());
442 return min(points_[0].x(), min(points_[1].x(), points_[2].x()));
445 template <
typename T>
448 ocean_assert(isValid());
449 return min(points_[0].y(), min(points_[1].y(), points_[2].y()));
452 template <
typename T>
455 ocean_assert(isValid());
456 return max(points_[0].x(), max(points_[1].x(), points_[2].x()));
459 template <
typename T>
462 ocean_assert(isValid());
463 return max(points_[0].y(), max(points_[1].y(), points_[2].y()));
466 template <
typename T>
469 const T squaredArea = area2();
471 if (std::is_same<T, float>::value)
473 if (squaredArea <= T(0))
482 template <
typename T>
485 const T a2 = points_[0].sqrDistance(points_[1]);
486 const T b2 = points_[0].sqrDistance(points_[2]);
487 const T c2 = points_[1].sqrDistance(points_[2]);
492 template <
typename T>
495 ocean_assert(isValid());
497 const T sqrDistance01(points_[1].
sqrDistance(points_[0]));
498 const T sqrDistance02(points_[2].
sqrDistance(points_[0]));
499 const T sqrDistance12(points_[2].
sqrDistance(points_[1]));
508 cosine0 = (sqrDistance01 + sqrDistance02 - sqrDistance12) * T(0.5) * factorDistance01 * factorDistance02;
509 cosine1 = (sqrDistance01 + sqrDistance12 - sqrDistance02) * T(0.5) * factorDistance01 * factorDistance12;
510 cosine2 = (sqrDistance02 + sqrDistance12 - sqrDistance01) * T(0.5) * factorDistance02 * factorDistance12;
515 template <
typename T>
518 ocean_assert(isValid());
520 const T sqrDistance01(points_[1].
sqrDistance(points_[0]));
521 const T sqrDistance02(points_[2].
sqrDistance(points_[0]));
522 const T sqrDistance12(points_[2].
sqrDistance(points_[1]));
531 return (sqrDistance01 + sqrDistance02 - sqrDistance12) * T(0.5) * factorDistance01 * factorDistance02 <= cosValue
532 && (sqrDistance01 + sqrDistance12 - sqrDistance02) * T(0.5) * factorDistance01 * factorDistance12 <= cosValue
533 && (sqrDistance02 + sqrDistance12 - sqrDistance01) * T(0.5) * factorDistance02 * factorDistance12 <= cosValue;
536 template <
typename T>
539 ocean_assert(isValid());
541 const T a2 = points_[1].sqrDistance(points_[2]);
542 const T b2 = points_[0].sqrDistance(points_[2]);
543 const T c2 = points_[0].sqrDistance(points_[1]);
545 const T coord0 = a2 * (-a2 + b2 + c2);
546 const T coord1 = b2 * (a2 - b2 + c2);
547 const T coord2 = c2 * (a2 + b2 - c2);
549 const T total = coord0 + coord1 + coord2;
552 const T factor = T(1) / total;
554 return VectorT3<T>(coord0 * factor, coord1 * factor, coord2 * factor);
557 template <
typename T>
560 ocean_assert(isValid());
562 const T a = points_[1].distance(points_[2]);
563 const T b = points_[0].distance(points_[2]);
564 const T c = points_[0].distance(points_[1]);
566 const T total = a + b + c;
569 const T factor = T(1) / total;
571 return VectorT3<T>(a * factor, b * factor, c * factor);
574 template <
typename T>
577 ocean_assert(isValid() && triangle.
isValid());
580 if (left() > triangle.
right() || triangle.
left() > right() || top() > triangle.
bottom() || triangle.
top() > bottom())
609 template <
typename T>
612 ocean_assert(isValid());
640 line01 /= line01.
xy().length();
641 line12 /= line12.
xy().length();
642 line20 /= line20.
xy().length();
644 if (line01 * hPoint2 > T(0.0))
652 line01.
z() -= padWidth;
653 line12.
z() -= padWidth;
654 line20.
z() -= padWidth;
673 template <
typename T>
676 T cosine0, cosine1, cosine2;
677 cosines(cosine0, cosine1, cosine2);
684 template <
typename T>
687 T cosine0, cosine1, cosine2;
688 cosines(cosine0, cosine1, cosine2);
697 template <
typename T>
700 const T sqrLength01(points_[0].
sqrDistance(points_[1]));
701 const T sqrLength02(points_[0].
sqrDistance(points_[2]));
702 const T sqrLength12(points_[1].
sqrDistance(points_[2]));
704 return max(sqrLength01, max(sqrLength02, sqrLength12));
707 template <
typename T>
713 template <
typename T>
716 const T sqrLength01(points_[0].
sqrDistance(points_[1]));
717 const T sqrLength02(points_[0].
sqrDistance(points_[2]));
718 const T sqrLength12(points_[1].
sqrDistance(points_[2]));
720 return min(sqrLength01, min(sqrLength02, sqrLength12));
723 template <
typename T>
729 template <
typename T>
735 template <
typename T>
740 if (triangle.isInside(point))
749 template <
typename T>
752 ocean_assert(isValid());
754 return analyzePoints(points_[0], points_[1], points_[2], yAxisDownwards) < T(0);
757 template <
typename T>
762 return VectorT2<T>((points_[0].x() * barycentric[0] + points_[1].x() * barycentric[1] + points_[2].x() * barycentric[2]),
763 (points_[0].y() * barycentric[0] + points_[1].y() * barycentric[1] + points_[2].y() * barycentric[2]));
766 template <
typename T>
769 ocean_assert(isValid());
771 const T barycentric0 = ((points_[1].y() - points_[2].y()) * (cartesian.
x() - points_[2].x())
772 + (points_[2].x() - points_[1].x()) * (cartesian.
y() -points_[2].y())) * barycentricFactor_;
774 const T barycentric1 = ((points_[2].y() - points_[0].y()) * (cartesian.
x() - points_[2].x())
775 + (points_[0].x() - points_[2].x()) * (cartesian.
y() - points_[2].y())) * barycentricFactor_;
778 if (std::is_same<float, Scalar>::value)
788 return VectorT3<T>(barycentric0, barycentric1, T(1) - barycentric0 - barycentric1);
791 template <
typename T>
794 ocean_assert(isValid());
795 return barycentric2cartesian(barycentricCircumcenter());
798 template <
typename T>
801 ocean_assert(isValid());
802 return barycentric2cartesian(barycentricIncenter());
805 template <
typename T>
808 if (std::is_same<T, double>::value)
814 return points_[0] != points_[1] && points_[0] != points_[2] && points_[1] != points_[2];
818 template <
typename T>
821 ocean_assert(index <= 2u);
822 return points_[index];
825 template <
typename T>
828 return TriangleT2<T>(points_[0] + offset, points_[1] + offset, points_[2] + offset);
831 template <
typename T>
834 points_[0] += offset;
835 points_[1] += offset;
836 points_[2] += offset;
841 template <
typename T>
844 return TriangleT2<T>(points_[0] - offset, points_[1] - offset, points_[2] - offset);
847 template <
typename T>
850 points_[0] -= offset;
851 points_[1] -= offset;
852 points_[2] -= offset;
857 template <
typename T>
865 return vector01.
cross(vector02);
869 return -vector01.
cross(vector02);
This class implements an finite line in 2D space.
Definition: FiniteLine2.h:82
bool intersects(const FiniteLineT2< T > &second) const
Returns whether two finite lines have a unique intersection point.
Definition: FiniteLine2.h:578
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
static T acos(const T value)
Returns the arccosine of a given value.
Definition: Numeric.h:2907
static constexpr bool isNotEqualEps(const T value)
Returns whether a value is not smaller than or equal to a small epsilon.
Definition: Numeric.h:2237
This class implements a 2D triangle with Cartesian coordinates.
Definition: Triangle2.h:81
VectorT3< T > cartesian2barycentric(const VectorT2< T > &cartesian) const
Returns the barycentric coordinate of a given 2D Cartesian coordinate defined in relation to this tri...
Definition: Triangle2.h:767
const VectorT2< T > & operator[](const unsigned int index) const
Returns individual triangle corners.
Definition: Triangle2.h:819
T maxLength() const
Returns the maximal side length of this triangle.
Definition: Triangle2.h:708
TriangleT2< T > & operator+=(const VectorT2< T > &offset)
Shifts the triangle by a given 2D vector (by adding the vector to all three corners of the triangle).
Definition: Triangle2.h:832
bool isInside(const VectorT2< T > &point) const
Returns whether a given point lies inside this triangle.
Definition: Triangle2.h:730
T sqrDistance01() const
Returns the square distance between point0 and point1.
Definition: Triangle2.h:421
T area() const
Returns the area of this triangle.
Definition: Triangle2.h:467
TriangleT2< T > operator+(const VectorT2< T > &offset) const
Shifts the triangle by a given 2D vector (by adding the vector to all three corners of the triangle).
Definition: Triangle2.h:826
T left() const
Returns the most left position of this triangle.
Definition: Triangle2.h:439
TriangleT2< T > & operator-=(const VectorT2< T > &offset)
Shifts the triangle by a given 2D vector (by subtracting the vector from all three corners of the tri...
Definition: Triangle2.h:848
T area2() const
Returns the square area of this triangle.
Definition: Triangle2.h:483
T minLength() const
Returns the minimal side length of this triangle.
Definition: Triangle2.h:724
const VectorT2< T > & point2() const
Returns the third triangle corner.
Definition: Triangle2.h:415
bool isCounterClockwise(const bool yAxisDownwards=true) const
Returns whether this triangles is defined in a counter clockwise manner.
Definition: Triangle2.h:750
T barycentricFactor_
Convert factor for barycentric coordinates.
Definition: Triangle2.h:382
T sqrDistance02() const
Returns the square distance between point0 and point2.
Definition: Triangle2.h:427
void angles(T &angle0, T &angle1, T &angle2) const
Calculates the three angles of the three triangle corners.
Definition: Triangle2.h:674
VectorT2< T > barycentric2cartesian(const VectorT3< T > &barycentric) const
Returns the 2D Cartesian coordinate of a given barycentric coordinate defined in relation to this tri...
Definition: Triangle2.h:758
T top() const
Returns the most top position of this triangle.
Definition: Triangle2.h:446
VectorT2< T > cartesianIncenter() const
Returns the incenter for this triangle in Cartesian coordinates.
Definition: Triangle2.h:799
bool allCosineBelow(const T cosValue) const
Returns whether all cosine values of the three triangle corners are below or equal to a given thresho...
Definition: Triangle2.h:516
T sqrDistance12() const
Returns the square distance between point1 and point2.
Definition: Triangle2.h:433
const VectorT2< T > & point0() const
Returns the first triangle corner.
Definition: Triangle2.h:403
bool intersects(const TriangleT2< T > &triangle) const
Returns whether this triangle has an intersection with a second triangle.
Definition: Triangle2.h:575
static T analyzePoints(const VectorT2< T > &point0, const VectorT2< T > &point1, const VectorT2< T > &point2, const bool yAxisDownwards)
Analyses the layout of three 2D points forming either a triangle or a line.
Definition: Triangle2.h:858
void cosines(T &cosine0, T &cosine1, T &cosine2) const
Calculates the three angle cosine values of the three triangle corners.
Definition: Triangle2.h:493
T minAngle() const
Returns the minimal angle of this triangle.
Definition: Triangle2.h:685
T maxSqrLength() const
Returns the maximal square side length of this triangle.
Definition: Triangle2.h:698
VectorT3< T > barycentricIncenter() const
Returns the incenter for this triangle in barycentric coordinates.
Definition: Triangle2.h:558
TriangleT2< T > operator-(const VectorT2< T > &offset) const
Shifts the triangle by a given 2D vector (by subtracting the vector from all three corners of the tri...
Definition: Triangle2.h:842
T right() const
Returns the most right position of this triangle.
Definition: Triangle2.h:453
VectorT3< T > barycentricCircumcenter() const
Returns the circumcenter for this triangle in barycentric coordinates.
Definition: Triangle2.h:537
VectorT2< T > cartesianCircumcenter() const
Returns the circumcenter for this triangle in Cartesian coordinates.
Definition: Triangle2.h:792
const VectorT2< T > & point1() const
Returns the second triangle corner.
Definition: Triangle2.h:409
TriangleT2()=default
Creates a new 2D triangle object with default parameters.
TriangleT2< T > padded(const T padWidth) const
Pad a given 2D triangle along each edge by a fixed value.
Definition: Triangle2.h:610
bool isValid() const
Returns whether this triangle can provide valid barycentric coordinates (for 64 bit floating point va...
Definition: Triangle2.h:806
T bottom() const
Returns the most bottom position of this triangle.
Definition: Triangle2.h:460
VectorT2< T > points_[3]
The corner positions.
Definition: Triangle2.h:379
T minSqrLength() const
Returns the minimal square side length of this triangle.
Definition: Triangle2.h:714
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
This class implements a vector with two elements.
Definition: Vector2.h:96
const T & x() const noexcept
Returns the x value.
Definition: Vector2.h:698
const T & y() const noexcept
Returns the y value.
Definition: Vector2.h:710
T cross(const VectorT2< T > &vector) const
Returns the cross product of two 2D vectors.
Definition: Vector2.h:544
This class implements a vector with three elements.
Definition: Vector3.h:97
VectorT3< T > cross(const VectorT3< T > &vector) const
Returns the cross product of two vectors.
Definition: Vector3.h:597
VectorT2< T > xy() const noexcept
Returns the x and y component of the vector as new 2D vector.
Definition: Vector3.h:836
const T & z() const noexcept
Returns the z value.
Definition: Vector3.h:824
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
std::vector< Triangle2 > Triangles2
Definition of a vector holding 2D triangles.
Definition: Triangle2.h:57
TriangleT2< Scalar > Triangle2
Definition of the Triangle2 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with sing...
Definition: Triangle2.h:21
std::vector< TriangleF2 > TrianglesF2
Definition of a vector holding 2D triangles with single precision float data type.
Definition: Triangle2.h:64
std::vector< TriangleD2 > TrianglesD2
Definition of a vector holding 2D triangles with double precision float data type.
Definition: Triangle2.h:71
std::vector< TriangleT2< T > > TrianglesT2
Definition of a typename alias for vectors with TriangleT2 objects.
Definition: Triangle2.h:50
TriangleT2< float > TriangleF2
Instantiation of the TriangleT2 template class using a single precision float data type.
Definition: Triangle2.h:42
TriangleT2< double > TriangleD2
Instantiation of the TriangleT2 template class using a double precision float data type.
Definition: Triangle2.h:35
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15