23#ifndef VCL_SPACE_CORE_TRIANGLE_H
24#define VCL_SPACE_CORE_TRIANGLE_H
26#include <vclib/concepts/range.h>
27#include <vclib/concepts/space/triangle.h>
28#include <vclib/space/core/point.h>
32template<Po
intConcept Po
intT>
35 std::array<PointT, 3> mPoints;
38 using ScalarType = PointT::ScalarType;
40 using PointType = PointT;
42 static const uint DIM = PointT::DIM;
46 Triangle(
const PointT& p0,
const PointT& p1,
const PointT&
p2) :
55 constexpr uint
size()
const {
return 3; }
62 const PointT&
point(uint
i)
const {
return mPoints.at(
i); }
64 PointT&
point(uint
i) {
return mPoints.at(
i); }
66 PointT& point0() {
return mPoints[0]; }
68 const PointT& point0()
const {
return mPoints[0]; }
70 PointT& point1() {
return mPoints[1]; }
72 const PointT& point1()
const {
return mPoints[1]; }
74 PointT& point2() {
return mPoints[2]; }
76 const PointT& point2()
const {
return mPoints[2]; }
85 return sideLength(mPoints[0], mPoints[1], mPoints[2],
i);
114 return normal(mPoints[0], mPoints[1], mPoints[2]);
124 return barycenter(mPoints[0], mPoints[1], mPoints[2]);
141 mPoints[0], mPoints[1], mPoints[2],
w0, w1, w2);
158 PointT barycentricCoordinatePoint(
163 return barycentricCoordinatePoint(
164 mPoints[0], mPoints[1], mPoints[2],
b0,
b1,
b2);
182 return barycentricCoordinatePoint(
183 mPoints[0], mPoints[1], mPoints[2],
b(0),
b(1),
b(2));
201 return circumcenter(mPoints[0], mPoints[1], mPoints[2]);
211 return perimeter(mPoints[0], mPoints[1], mPoints[2]);
219 ScalarType
area()
const {
return area(mPoints[0], mPoints[1], mPoints[2]); }
236 return quality(mPoints[0], mPoints[1], mPoints[2]);
253 return qualityRadii(mPoints[0], mPoints[1], mPoints[2]);
295 case 0:
return p0.dist(p1);
296 case 1:
return p1.dist(
p2);
297 case 2:
return p2.dist(p0);
298 default:
throw std::out_of_range(
"Invalid side index");
314 static PointT
normal(
const PointT& p0,
const PointT& p1,
const PointT&
p2)
315 requires (PointT::DIM == 3)
317 return (p1 - p0).cross(
p2 - p0);
335 return (p0 + p1 +
p2) / 3;
361 return (p0 *
w0 + p1 * w1 +
p2 * w2) / (
w0 + w1 + w2);
420 ScalarType
a2 = p1.squaredDist(
p2);
421 ScalarType
b2 =
p2.squaredDist(p0);
422 ScalarType
c2 = p0.squaredDist(p1);
447 return p0.dist(p1) + p1.dist(
p2) +
p2.dist(p0);
461 static ScalarType
area(
const PointT& p0,
const PointT& p1,
const PointT&
p2)
463 if constexpr (DIM == 2) {
464 return ((p1[0] - p0[0]) * (
p2[1] - p0[1]) -
465 (
p2[0] - p0[0]) * (p1[1] - p0[1])) /
468 if constexpr (DIM == 3) {
469 return normal(p0, p1,
p2).norm() / 2;
475 s * (s - p0.dist(p1)) * (s - p1.dist(
p2)) * (s -
p2.dist(p0)));
501 ScalarType
a =
area();
505 PointType
d10 = p1 - p0;
506 PointType
d20 =
p2 - p0;
507 PointType
d12 = p1 -
p2;
509 std::min({
d10.squaredNorm(),
d20.squaredNorm(),
d12.squaredNorm()});
537 double a = p0.dist(p1);
538 double b =
p2.dist(p0);
539 double c = p1.dist(
p2);
541 double sum = (
a +
b + c) * 0.5;
542 double area2 = sum * (
a +
b - sum) * (
a + c - sum) * (
b + c - sum);
547 return (8 *
area2) / (
a *
b * c * sum);
575 double a = p0.dist(p1);
576 double b =
p2.dist(p0);
577 double c = p1.dist(
p2);
579 double sum = (
a +
b + c) * 0.5;
580 double area2 = sum * (
a +
b - sum) * (
a + c - sum) * (
b + c - sum);
583 return (4.0 * std::sqrt(3.0) * std::sqrt(
area2)) /
584 (
a *
a +
b *
b + c * c);
590template<
typename Scalar>
591using Triangle2 = Triangle<Point2<Scalar>>;
593using Triangle2f = Triangle<Point2f>;
594using Triangle2d = Triangle<Point2d>;
596template<
typename Scalar>
597using Triangle3 = Triangle<Point3<Scalar>>;
599using Triangle3f = Triangle<Point3f>;
600using Triangle3d = Triangle<Point3d>;
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
ScalarType area() const
Computes the area of the triangle.
Definition triangle.h:219
ScalarType sideLength2() const
Returns the length of the third side of the triangle.
Definition triangle.h:104
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:385
ScalarType quality() const
Calculates the quality measure of the triangle.
Definition triangle.h:234
ScalarType qualityMeanRatio() const
Compute the mean ratio of the triangle shape quality measure.
Definition triangle.h:271
ScalarType perimeter() const
Computes the perimeter of the triangle.
Definition triangle.h:209
PointT weightedBarycenter(ScalarType w0, ScalarType w1, ScalarType w2) const
Computes the weighted barycenter of the triangle.
Definition triangle.h:138
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:496
ScalarType sideLength0() const
Returns the length of the first side of the triangle.
Definition triangle.h:92
PointT barycenter() const
Computes the barycenter of the triangle.
Definition triangle.h:122
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:570
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:330
PointT normal() const
Returns the normal of the triangle.
Definition triangle.h:112
ScalarType sideLength(uint i) const
Returns the length of the i-th side of the triangle.
Definition triangle.h:83
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:461
PointT barycentricCoordinatePoint(const Point3< ScalarType > &b) const
Computes the point in the triangle with the given barycentric coordinates.
Definition triangle.h:180
PointT circumcenter() const
Compute the circumcenter of the triangle.
Definition triangle.h:199
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:353
PointT weightedBarycenter(const Point3< ScalarType > &w) const
Computes the weighted barycenter of the triangle.
Definition triangle.h:153
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:314
ScalarType sideLength1() const
Returns the length of the second side of the triangle.
Definition triangle.h:98
const PointT & point(uint i) const
Returns the i-th point of the triangle.
Definition triangle.h:62
ScalarType qualityRadii() const
Compute a shape quality measure of the triangle.
Definition triangle.h:251
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:442
static PointT circumcenter(const PointT &p0, const PointT &p1, const PointT &p2)
Compute the circumcenter of a triangle.
Definition triangle.h:415
constexpr uint size() const
Returns the number of points of the triangle.
Definition triangle.h:55
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:288
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:532