23#ifndef VCL_SPACE_CORE_TRIANGLE_H
24#define VCL_SPACE_CORE_TRIANGLE_H
30template<Po
intConcept Po
intT>
33 std::array<PointT, 3> mPoints;
36 using ScalarType = PointT::ScalarType;
38 using PointType = PointT;
40 static const uint DIM = PointT::DIM;
44 Triangle(
const PointT& p0,
const PointT& p1,
const PointT&
p2) :
53 constexpr uint
size()
const {
return 3; }
60 const PointT&
point(uint
i)
const {
return mPoints.at(
i); }
62 PointT&
point(uint
i) {
return mPoints.at(
i); }
64 PointT& point0() {
return mPoints[0]; }
66 const PointT& point0()
const {
return mPoints[0]; }
68 PointT& point1() {
return mPoints[1]; }
70 const PointT& point1()
const {
return mPoints[1]; }
72 PointT& point2() {
return mPoints[2]; }
74 const PointT& point2()
const {
return mPoints[2]; }
83 return sideLength(mPoints[0], mPoints[1], mPoints[2],
i);
112 return normal(mPoints[0], mPoints[1], mPoints[2]);
122 return barycenter(mPoints[0], mPoints[1], mPoints[2]);
139 mPoints[0], mPoints[1], mPoints[2],
w0, w1, w2);
156 PointT barycentricCoordinatePoint(
161 return barycentricCoordinatePoint(
162 mPoints[0], mPoints[1], mPoints[2],
b0,
b1,
b2);
180 return barycentricCoordinatePoint(
181 mPoints[0], mPoints[1], mPoints[2],
b(0),
b(1),
b(2));
199 return circumcenter(mPoints[0], mPoints[1], mPoints[2]);
209 return perimeter(mPoints[0], mPoints[1], mPoints[2]);
217 ScalarType
area()
const {
return area(mPoints[0], mPoints[1], mPoints[2]); }
234 return quality(mPoints[0], mPoints[1], mPoints[2]);
251 return qualityRadii(mPoints[0], mPoints[1], mPoints[2]);
293 case 0:
return p0.dist(p1);
294 case 1:
return p1.dist(
p2);
295 case 2:
return p2.dist(p0);
296 default:
throw std::out_of_range(
"Invalid side index");
312 static PointT
normal(
const PointT& p0,
const PointT& p1,
const PointT&
p2)
313 requires (PointT::DIM == 3)
315 return (p1 - p0).cross(
p2 - p0);
333 return (p0 + p1 +
p2) / 3;
359 return (p0 *
w0 + p1 * w1 +
p2 * w2) / (
w0 + w1 + w2);
418 ScalarType
a2 = p1.squaredDist(
p2);
419 ScalarType
b2 =
p2.squaredDist(p0);
420 ScalarType
c2 = p0.squaredDist(p1);
445 return p0.dist(p1) + p1.dist(
p2) +
p2.dist(p0);
459 static ScalarType
area(
const PointT& p0,
const PointT& p1,
const PointT&
p2)
461 if constexpr (DIM == 2) {
462 return ((p1[0] - p0[0]) * (
p2[1] - p0[1]) -
463 (
p2[0] - p0[0]) * (p1[1] - p0[1])) /
466 if constexpr (DIM == 3) {
467 return normal(p0, p1,
p2).norm() / 2;
473 s * (s - p0.dist(p1)) * (s - p1.dist(
p2)) * (s -
p2.dist(p0)));
499 ScalarType
a =
area();
503 PointType
d10 = p1 - p0;
504 PointType
d20 =
p2 - p0;
505 PointType
d12 = p1 -
p2;
507 std::min({
d10.squaredNorm(),
d20.squaredNorm(),
d12.squaredNorm()});
535 double a = p0.dist(p1);
536 double b =
p2.dist(p0);
537 double c = p1.dist(
p2);
539 double sum = (
a +
b + c) * 0.5;
540 double area2 = sum * (
a +
b - sum) * (
a + c - sum) * (
b + c - sum);
545 return (8 *
area2) / (
a *
b * c * sum);
573 double a = p0.dist(p1);
574 double b =
p2.dist(p0);
575 double c = p1.dist(
p2);
577 double sum = (
a +
b + c) * 0.5;
578 double area2 = sum * (
a +
b - sum) * (
a + c - sum) * (
b + c - sum);
581 return (4.0 * std::sqrt(3.0) * std::sqrt(
area2)) /
582 (
a *
a +
b *
b + c * c);
588template<
typename Scalar>
589using Triangle2 = Triangle<Point2<Scalar>>;
591using Triangle2f = Triangle<Point2f>;
592using Triangle2d = Triangle<Point2d>;
594template<
typename Scalar>
595using Triangle3 = Triangle<Point3<Scalar>>;
597using Triangle3f = Triangle<Point3f>;
598using Triangle3d = Triangle<Point3d>;
A class representing a box in N-dimensional space.
Definition box.h:46
ScalarType area() const
Computes the area of the triangle.
Definition triangle.h:217
ScalarType sideLength2() const
Returns the length of the third side of the triangle.
Definition triangle.h:102
static PointT barycentricCoordinatePoint(const PointT &p0, const PointT &p1, const PointT &p2, ScalarType b0, ScalarType b1, ScalarType b2)
Computes the point in a triangle with the given barycentric coordinates.
Definition triangle.h:383
ScalarType quality() const
Calculates the quality measure of the triangle.
Definition triangle.h:232
ScalarType qualityMeanRatio() const
Compute the mean ratio of the triangle shape quality measure.
Definition triangle.h:269
ScalarType perimeter() const
Computes the perimeter of the triangle.
Definition triangle.h:207
PointT weightedBarycenter(ScalarType w0, ScalarType w1, ScalarType w2) const
Computes the weighted barycenter of the triangle.
Definition triangle.h:136
static ScalarType quality(const PointT &p0, const PointT &p1, const PointT &p2)
Calculates the quality measure of a triangle, given its three vertices.
Definition triangle.h:494
ScalarType sideLength0() const
Returns the length of the first side of the triangle.
Definition triangle.h:90
PointT barycenter() const
Computes the barycenter of the triangle.
Definition triangle.h:120
static ScalarType qualityMeanRatio(const PointT &p0, const PointT &p1, const PointT &p2)
Compute the mean ratio of a triangle shape quality measure.
Definition triangle.h:568
static PointT barycenter(const PointT &p0, const PointT &p1, const PointT &p2)
Computes the barycenter of the triangle composed by the points p0, p1, and p2.
Definition triangle.h:328
PointT normal() const
Returns the normal of the triangle.
Definition triangle.h:110
ScalarType sideLength(uint i) const
Returns the length of the i-th side of the triangle.
Definition triangle.h:81
static ScalarType area(const PointT &p0, const PointT &p1, const PointT &p2)
Computes the area of the triangle composed by the points p0, p1, and p2, considering that these three...
Definition triangle.h:459
PointT barycentricCoordinatePoint(const Point3< ScalarType > &b) const
Computes the point in the triangle with the given barycentric coordinates.
Definition triangle.h:178
PointT circumcenter() const
Compute the circumcenter of the triangle.
Definition triangle.h:197
static PointT weightedBarycenter(const PointT &p0, const PointT &p1, const PointT &p2, ScalarType w0, ScalarType w1, ScalarType w2)
Computes the weighted barycenter of a triangle composed of three points.
Definition triangle.h:351
PointT weightedBarycenter(const Point3< ScalarType > &w) const
Computes the weighted barycenter of the triangle.
Definition triangle.h:151
static PointT normal(const PointT &p0, const PointT &p1, const PointT &p2)
Computes the normal of the triangle composed by the 3D points p0, p1, and p2, considering that these ...
Definition triangle.h:312
ScalarType sideLength1() const
Returns the length of the second side of the triangle.
Definition triangle.h:96
const PointT & point(uint i) const
Returns the i-th point of the triangle.
Definition triangle.h:60
ScalarType qualityRadii() const
Compute a shape quality measure of the triangle.
Definition triangle.h:249
static ScalarType perimeter(const PointT &p0, const PointT &p1, const PointT &p2)
Computes the perimeter of the triangle composed by the points p0, p1, and p2.
Definition triangle.h:440
static PointT circumcenter(const PointT &p0, const PointT &p1, const PointT &p2)
Compute the circumcenter of a triangle.
Definition triangle.h:413
constexpr uint size() const
Returns the number of points of the triangle.
Definition triangle.h:53
ScalarType sideLength(const PointT &p0, const PointT &p1, const PointT &p2, uint i) const
Returns the length of the i-th side of the triangle composed by the points p0, p1,...
Definition triangle.h:286
static ScalarType qualityRadii(const PointT &p0, const PointT &p1, const PointT &p2)
Compute a shape quality measure of the triangle composed by points p0, p1, p2.
Definition triangle.h:530