8#ifndef META_OCEAN_MATH_TRIANGLE_2_H
9#define META_OCEAN_MATH_TRIANGLE_2_H
21template <
typename T>
class TriangleT2;
137 inline T
left()
const;
143 inline T
top()
const;
149 inline T
right()
const;
162 inline T
area()
const;
170 inline T
area2()
const;
179 void cosines(T& cosine0, T& cosine1, T& cosine2)
const;
188 inline void angles(T& angle0, T& angle1, T& angle2)
const;
388 barycentricFactor_(T(0))
430 return points_[0].sqrDistance(points_[2]);
436 return points_[1].sqrDistance(points_[2]);
442 ocean_assert(isValid());
443 return min(points_[0].x(), min(points_[1].x(), points_[2].x()));
449 ocean_assert(isValid());
450 return min(points_[0].y(), min(points_[1].y(), points_[2].y()));
456 ocean_assert(isValid());
457 return max(points_[0].x(), max(points_[1].x(), points_[2].x()));
463 ocean_assert(isValid());
464 return max(points_[0].y(), max(points_[1].y(), points_[2].y()));
476 const T a2 = points_[0].sqrDistance(points_[1]);
477 const T b2 = points_[0].sqrDistance(points_[2]);
478 const T c2 = points_[1].sqrDistance(points_[2]);
481 return std::max((T(4) * a2 * c2 -
NumericT<T>::sqr(a2 + c2 - b2)) * T(0.0625), T(0));
487 ocean_assert(isValid());
489 const T sqrDistance01(points_[1].
sqrDistance(points_[0]));
490 const T sqrDistance02(points_[2].
sqrDistance(points_[0]));
491 const T sqrDistance12(points_[2].
sqrDistance(points_[1]));
500 cosine0 = (sqrDistance01 + sqrDistance02 - sqrDistance12) * T(0.5) * factorDistance01 * factorDistance02;
501 cosine1 = (sqrDistance01 + sqrDistance12 - sqrDistance02) * T(0.5) * factorDistance01 * factorDistance12;
502 cosine2 = (sqrDistance02 + sqrDistance12 - sqrDistance01) * T(0.5) * factorDistance02 * factorDistance12;
510 ocean_assert(isValid());
512 const T sqrDistance01(points_[1].
sqrDistance(points_[0]));
513 const T sqrDistance02(points_[2].
sqrDistance(points_[0]));
514 const T sqrDistance12(points_[2].
sqrDistance(points_[1]));
523 return (sqrDistance01 + sqrDistance02 - sqrDistance12) * T(0.5) * factorDistance01 * factorDistance02 <= cosValue
524 && (sqrDistance01 + sqrDistance12 - sqrDistance02) * T(0.5) * factorDistance01 * factorDistance12 <= cosValue
525 && (sqrDistance02 + sqrDistance12 - sqrDistance01) * T(0.5) * factorDistance02 * factorDistance12 <= cosValue;
531 ocean_assert(isValid());
533 const T a2 = points_[1].sqrDistance(points_[2]);
534 const T b2 = points_[0].sqrDistance(points_[2]);
535 const T c2 = points_[0].sqrDistance(points_[1]);
537 const T coord0 = a2 * (-a2 + b2 + c2);
538 const T coord1 = b2 * (a2 - b2 + c2);
539 const T coord2 = c2 * (a2 + b2 - c2);
541 const T total = coord0 + coord1 + coord2;
544 const T factor = T(1) / total;
546 return VectorT3<T>(coord0 * factor, coord1 * factor, coord2 * factor);
552 ocean_assert(isValid());
554 const T a = points_[1].distance(points_[2]);
555 const T b = points_[0].distance(points_[2]);
556 const T c = points_[0].distance(points_[1]);
558 const T total = a + b + c;
561 const T factor = T(1) / total;
563 return VectorT3<T>(a * factor, b * factor, c * factor);
569 ocean_assert(isValid() && triangle.
isValid());
572 if (left() > triangle.
right() || triangle.
left() > right() || top() > triangle.
bottom() || triangle.
top() > bottom())
604 ocean_assert(isValid());
632 line01 /= line01.
xy().length();
633 line12 /= line12.
xy().length();
634 line20 /= line20.
xy().length();
636 if (line01 * hPoint2 > T(0.0))
644 line01.
z() -= padWidth;
645 line12.
z() -= padWidth;
646 line20.
z() -= padWidth;
668 T cosine0, cosine1, cosine2;
669 cosines(cosine0, cosine1, cosine2);
679 T cosine0, cosine1, cosine2;
680 cosines(cosine0, cosine1, cosine2);
692 const T sqrLength01(points_[0].
sqrDistance(points_[1]));
693 const T sqrLength02(points_[0].
sqrDistance(points_[2]));
694 const T sqrLength12(points_[1].
sqrDistance(points_[2]));
696 return max(sqrLength01, max(sqrLength02, sqrLength12));
708 const T sqrLength01(points_[0].
sqrDistance(points_[1]));
709 const T sqrLength02(points_[0].
sqrDistance(points_[2]));
710 const T sqrLength12(points_[1].
sqrDistance(points_[2]));
712 return min(sqrLength01, min(sqrLength02, sqrLength12));
732 if (triangle.isInside(point))
744 ocean_assert(isValid());
746 return analyzePoints(points_[0], points_[1], points_[2], yAxisDownwards) < T(0);
754 return VectorT2<T>((points_[0].x() * barycentric[0] + points_[1].x() * barycentric[1] + points_[2].x() * barycentric[2]),
755 (points_[0].y() * barycentric[0] + points_[1].y() * barycentric[1] + points_[2].y() * barycentric[2]));
761 ocean_assert(isValid());
763 const T barycentric0 = ((points_[1].y() - points_[2].y()) * (cartesian.
x() - points_[2].x())
764 + (points_[2].x() - points_[1].x()) * (cartesian.
y() -points_[2].y())) * barycentricFactor_;
766 const T barycentric1 = ((points_[2].y() - points_[0].y()) * (cartesian.
x() - points_[2].x())
767 + (points_[0].x() - points_[2].x()) * (cartesian.
y() - points_[2].y())) * barycentricFactor_;
770 if (std::is_same<float, Scalar>::value)
780 return VectorT3<T>(barycentric0, barycentric1, T(1) - barycentric0 - barycentric1);
786 ocean_assert(isValid());
787 return barycentric2cartesian(barycentricCircumcenter());
793 ocean_assert(isValid());
794 return barycentric2cartesian(barycentricIncenter());
800 if (std::is_same<T, double>::value)
806 return points_[0] != points_[1] && points_[0] != points_[2] && points_[1] != points_[2];
813 ocean_assert(index <= 2u);
814 return points_[index];
820 return TriangleT2<T>(points_[0] + offset, points_[1] + offset, points_[2] + offset);
826 points_[0] += offset;
827 points_[1] += offset;
828 points_[2] += offset;
836 return TriangleT2<T>(points_[0] - offset, points_[1] - offset, points_[2] - offset);
842 points_[0] -= offset;
843 points_[1] -= offset;
844 points_[2] -= offset;
857 return vector01.
cross(vector02);
861 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:1537
static T acos(const T value)
Returns the arccosine of a given value.
Definition Numeric.h:2916
static constexpr bool isNotEqualEps(const T value)
Returns whether a value is not smaller than or equal to a small epsilon.
Definition Numeric.h:2246
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:759
const VectorT2< T > & operator[](const unsigned int index) const
Returns individual triangle corners.
Definition Triangle2.h:811
T maxLength() const
Returns the maximal side length of this triangle.
Definition Triangle2.h:700
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:824
bool isInside(const VectorT2< T > &point) const
Returns whether a given point lies inside this triangle.
Definition Triangle2.h:722
T sqrDistance01() const
Returns the square distance between point0 and point1.
Definition Triangle2.h:422
T area() const
Returns the area of this triangle.
Definition Triangle2.h:468
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:818
T left() const
Returns the most left position of this triangle.
Definition Triangle2.h:440
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:840
T area2() const
Returns the square area of this triangle.
Definition Triangle2.h:474
T minLength() const
Returns the minimal side length of this triangle.
Definition Triangle2.h:716
const VectorT2< T > & point2() const
Returns the third triangle corner.
Definition Triangle2.h:416
bool isCounterClockwise(const bool yAxisDownwards=true) const
Returns whether this triangles is defined in a counter clockwise manner.
Definition Triangle2.h:742
T barycentricFactor_
Convert factor for barycentric coordinates.
Definition Triangle2.h:383
T sqrDistance02() const
Returns the square distance between point0 and point2.
Definition Triangle2.h:428
void angles(T &angle0, T &angle1, T &angle2) const
Calculates the three angles of the three triangle corners.
Definition Triangle2.h:666
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:750
T top() const
Returns the most top position of this triangle.
Definition Triangle2.h:447
VectorT2< T > cartesianIncenter() const
Returns the incenter for this triangle in Cartesian coordinates.
Definition Triangle2.h:791
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:508
T sqrDistance12() const
Returns the square distance between point1 and point2.
Definition Triangle2.h:434
const VectorT2< T > & point0() const
Returns the first triangle corner.
Definition Triangle2.h:404
bool intersects(const TriangleT2< T > &triangle) const
Returns whether this triangle has an intersection with a second triangle.
Definition Triangle2.h:567
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:850
void cosines(T &cosine0, T &cosine1, T &cosine2) const
Calculates the three angle cosine values of the three triangle corners.
Definition Triangle2.h:485
T minAngle() const
Returns the minimal angle of this triangle.
Definition Triangle2.h:677
T maxSqrLength() const
Returns the maximal square side length of this triangle.
Definition Triangle2.h:690
VectorT3< T > barycentricIncenter() const
Returns the incenter for this triangle in barycentric coordinates.
Definition Triangle2.h:550
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:834
T right() const
Returns the most right position of this triangle.
Definition Triangle2.h:454
VectorT3< T > barycentricCircumcenter() const
Returns the circumcenter for this triangle in barycentric coordinates.
Definition Triangle2.h:529
VectorT2< T > cartesianCircumcenter() const
Returns the circumcenter for this triangle in Cartesian coordinates.
Definition Triangle2.h:784
const VectorT2< T > & point1() const
Returns the second triangle corner.
Definition Triangle2.h:410
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:602
bool isValid() const
Returns whether this triangle can provide valid barycentric coordinates (for 64 bit floating point va...
Definition Triangle2.h:798
T bottom() const
Returns the most bottom position of this triangle.
Definition Triangle2.h:461
VectorT2< T > points_[3]
The corner positions.
Definition Triangle2.h:380
T minSqrLength() const
Returns the minimal square side length of this triangle.
Definition Triangle2.h:706
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:703
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:715
T sqrDistance(const VectorT2< T > &right) const
Returns the square distance between this 2D position and a second 2D position.
Definition Vector2.h:638
T cross(const VectorT2< T > &vector) const
Returns the cross product of two 2D vectors.
Definition Vector2.h:549
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:601
VectorT2< T > xy() const noexcept
Returns the x and y component of the vector as new 2D vector.
Definition Vector3.h:840
const T & z() const noexcept
Returns the z value.
Definition Vector3.h:828
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition base/Utilities.h:1159
std::vector< TriangleD2 > TrianglesD2
Definition of a vector holding 2D triangles with double precision float data type.
Definition Triangle2.h:71
std::vector< Triangle2 > Triangles2
Definition of a vector holding 2D triangles.
Definition Triangle2.h:57
std::vector< TriangleT2< T > > TrianglesT2
Definition of a typename alias for vectors with TriangleT2 objects.
Definition Triangle2.h:50
std::vector< TriangleF2 > TrianglesF2
Definition of a vector holding 2D triangles with single precision float data type.
Definition Triangle2.h:64
The namespace covering the entire Ocean framework.
Definition Accessor.h:15