triangle.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::triangle
29 
30 Description
31  A triangle primitive used to calculate face normals and swept volumes.
32  Uses referred points.
33 
34 SourceFiles
35  triangleI.H
36  triangle.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Foam_triangle_H
41 #define Foam_triangle_H
42 
43 #include "triangleFwd.H"
44 #include "intersection.H"
45 #include "vector.H"
46 #include "tensor.H"
47 #include "pointHit.H"
48 #include "Random.H"
49 #include "FixedList.H"
50 #include "UList.H"
51 #include "line.H"
52 #include "Pair.H"
53 #include "Tuple2.H"
54 #include "barycentric2D.H"
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 // Forward Declarations
62 class plane;
63 
64 template<class Point, class PointRef>
65 inline Istream& operator>>(Istream&, triangle<Point, PointRef>&);
66 
67 template<class Point, class PointRef>
68 inline Ostream& operator<<(Ostream&, const triangle<Point, PointRef>&);
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class triPoints Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 //- Triangle point storage. Default constructable (triangle is not)
76 class triPoints
77 :
78  public FixedList<point, 3>
79 {
81 
82 public:
83 
84  // Generated Methods
85 
86  //- Default construct
87  triPoints() = default;
88 
89  //- The front() accessor (from FixedList) has no purpose
90  void front() = delete;
91 
92  //- The back() accessor (from FixedList) has no purpose
93  void back() = delete;
94 
95 
96  // Constructors
97 
98  //- Construct from three points
99  inline triPoints(const point& p0, const point& p1, const point& p2);
100 
101  //- Construct from point references
102  inline explicit triPoints(const triPointRef& pts);
103 
104  //- Construct from three points
105  inline triPoints(const FixedList<point, 3>& pts);
106 
107  //- Copy construct from subset of points
108  inline triPoints
109  (
110  const UList<point>& points,
111  const FixedList<label, 3>& indices
112  );
113 
114  //- Copy construct from subset of points
115  inline triPoints
116  (
117  const UList<point>& points,
118  const label p0,
119  const label p1,
120  const label p2
121  );
122 
123 
124  // Member Functions
125 
126  //- The first vertex
127  const point& a() const noexcept { return get<0>(); }
128 
129  //- The second vertex
130  const point& b() const noexcept { return get<1>(); }
131 
132  //- The third vertex
133  const point& c() const noexcept { return get<2>(); }
134 
135  //- The first vertex
136  point& a() noexcept { return get<0>(); }
137 
138  //- The second vertex
139  point& b() noexcept { return get<1>(); }
140 
141  //- The third vertex
142  point& c() noexcept { return get<2>(); }
143 
144  //- Flip triangle orientation by swapping second and third vertices
145  inline void flip();
146 
147  //- Return as triangle reference
148  inline triPointRef tri() const;
149 
150 
151  // Properties
153  //- Return centre (centroid)
154  inline point centre() const;
155 
156  //- The area normal - with magnitude equal to area of triangle
157  inline vector areaNormal() const;
158 
159  //- Return unit normal
160  inline vector unitNormal() const;
161 
162  //- The magnitude of the triangle area
163  inline scalar mag() const;
164 
165  //- The magnitude squared of the triangle area
166  inline scalar magSqr() const;
168  //- The enclosing (bounding) box for the triangle
169  inline Pair<point> box() const;
170 
171  //- Edge vector opposite point a(): from b() to c()
172  inline vector vecA() const;
173 
174  //- Edge vector opposite point b(): from c() to a()
175  inline vector vecB() const;
176 
177  //- Edge vector opposite point c(): from a() to b()
178  inline vector vecC() const;
179 };
180 
181 
182 /*---------------------------------------------------------------------------*\
183  Class triangle Declaration
184 \*---------------------------------------------------------------------------*/
185 
186 template<class Point, class PointRef>
187 class triangle
188 {
189 public:
190 
191  // Public Typedefs
192 
193  //- The point type
194  typedef Point point_type;
195 
196  //- Storage type for triangles originating from intersecting triangle
197  //- with another triangle
199 
200  //- Proximity classifications
201  enum proxType
202  {
203  NONE = 0,
204  POINT,
205  EDGE
206  };
207 
208 
209  // Public Classes
210 
211  // Classes for use in sliceWithPlane.
212  // What to do with decomposition of triangle.
213 
214  //- Dummy
215  struct dummyOp
216  {
217  void operator()(const triPoints&) const noexcept
218  {}
219  };
220 
221  //- Sum resulting areas
222  struct sumAreaOp
223  {
224  scalar area_;
225 
226  constexpr sumAreaOp() noexcept : area_(0) {}
227 
228  scalar total() const noexcept { return area_; }
229 
230  inline void operator()(const triPoints&);
231  };
232 
233  //- Store resulting tris
234  struct storeOp
235  {
237  label& nTris_;
238 
239  inline storeOp(triIntersectionList&, label&);
240 
241  inline void operator()(const triPoints&);
242  };
244 
245 private:
246 
247  // Private Data
248 
249  //- Reference to the first triangle point
250  PointRef a_;
251 
252  //- Reference to the second triangle point
253  PointRef b_;
255  //- Reference to the third triangle point
256  PointRef c_;
259  // Private Member Functions
260 
261  //- Helper: calculate intersection point
262  inline static point planeIntersection
263  (
264  const FixedList<scalar, 3>& d,
265  const triPoints& t,
266  const label negI,
267  const label posI
268  );
269 
270  //- Helper: slice triangle with plane
271  template<class AboveOp, class BelowOp>
272  inline static void triSliceWithPlane
273  (
274  const plane& pln,
275  const triPoints& tri,
276  AboveOp& aboveOp,
277  BelowOp& belowOp
278  );
280 
281 public:
282 
283  // Constructors
284 
285  //- Construct from three points
286  inline triangle(const Point& p0, const Point& p1, const Point& p2);
287 
288  //- Construct from three points
289  inline triangle(const FixedList<Point, 3>& pts);
290 
291  //- Construct from three points out of the list of points
292  // The indices could be from triFace etc.
293  inline triangle
294  (
296  const FixedList<label, 3>& indices
297  );
298 
299  //- Construct from three points out of the list of points
300  inline triangle
301  (
302  const UList<Point>& points,
303  const label p0,
304  const label p1,
305  const label p2
306  );
307 
308  //- Construct from Istream
309  inline explicit triangle(Istream& is);
310 
311 
312  // Member Functions
313 
314  // Access
315 
316  //- The first vertex
317  const Point& a() const noexcept { return a_; }
318 
319  //- The second vertex
320  const Point& b() const noexcept { return b_; }
321 
322  //- The third vertex
323  const Point& c() const noexcept { return c_; }
324 
325 
326  // Triangle properties (static calculations)
327 
328  //- The centre (centroid) of three points
329  inline static Point centre
330  (
331  const Point& p0,
332  const Point& p1,
333  const Point& p2
334  );
335 
336  //- The area normal for a triangle defined by three points
337  //- (right-hand rule). Magnitude equal to area of the triangle
338  inline static vector areaNormal
339  (
340  const Point& p0,
341  const Point& p1,
342  const Point& p2
343  );
344 
345  //- The unit normal for a triangle defined by three points
346  //- (right-hand rule).
347  inline static vector unitNormal
348  (
349  const Point& p0,
350  const Point& p1,
351  const Point& p2
352  );
353 
354  //- The enclosing (bounding) box for three points
355  inline static Pair<Point> box
356  (
357  const Point& p0,
358  const Point& p1,
359  const Point& p2
360  );
361 
362 
363  // Properties
364 
365  //- Return centre (centroid)
366  inline Point centre() const;
367 
368  //- The area normal - with magnitude equal to area of triangle
369  inline vector areaNormal() const;
370 
371  //- Return unit normal
372  inline vector unitNormal() const;
373 
374  //- Legacy name for areaNormal().
375  // \deprecated(2018-06) Deprecated for new use
376  FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()")
377  vector normal() const
378  {
379  return areaNormal();
380  }
381 
382  //- The magnitude of the triangle area
383  inline scalar mag() const;
384 
385  //- The magnitude squared of the triangle area
386  inline scalar magSqr() const;
387 
388  //- The enclosing (bounding) box for the triangle
389  inline Pair<Point> box() const;
390 
391  //- Edge vector opposite point a(): from b() to c()
392  inline Point vecA() const;
393 
394  //- Edge vector opposite point b(): from c() to a()
395  inline Point vecB() const;
396 
397  //- Edge vector opposite point c(): from a() to b()
398  inline Point vecC() const;
400 
401  // Properties
402 
403  //- Return circum-centre
404  inline Point circumCentre() const;
405 
406  //- Return circum-radius
407  inline scalar circumRadius() const;
408 
409  //- Return quality: Ratio of triangle and circum-circle
410  // area, scaled so that an equilateral triangle has a
411  // quality of 1
412  inline scalar quality() const;
413 
414  //- Return swept-volume
415  inline scalar sweptVol(const triangle& t) const;
416 
417  //- Return the inertia tensor, with optional reference
418  // point and density specification
419  inline tensor inertia
420  (
421  PointRef refPt = Zero,
422  scalar density = 1.0
423  ) const;
424 
425  //- Return a random point on the triangle from a uniform
426  //- distribution
427  inline Point randomPoint(Random& rndGen) const;
428 
429  //- Calculate the point from the given barycentric coordinates.
430  inline Point barycentricToPoint(const barycentric2D& bary) const;
431 
432  //- Calculate the barycentric coordinates from the given point
433  inline barycentric2D pointToBarycentric(const point& pt) const;
434 
435  //- Calculate the barycentric coordinates from the given point.
436  // Returns the determinant.
437  inline scalar pointToBarycentric
438  (
439  const point& pt,
440  barycentric2D& bary
441  ) const;
442 
443  //- Fast intersection detection with a plane.
444  inline bool intersects
445  (
446  const point& origin,
447  const vector& normal
448  ) const;
449 
450  //- Fast intersection detection with an \b axis plane.
451  inline bool intersects
452  (
454  const point& origin,
456  const vector::components axis
457  ) const;
458 
459  //- Return point intersection with a ray.
460  // For a hit, the distance is signed. Positive number
461  // represents the point in front of triangle.
462  // In case of miss pointHit is set to nearest point
463  // on triangle and its distance to the distance between
464  // the original point and the plane intersection point
465  inline pointHit ray
466  (
467  const point& p,
468  const vector& q,
471  ) const;
472 
473  //- Fast intersection with a ray.
474  // For a hit, the pointHit.distance() is the line parameter t :
475  // intersection=p+t*q. Only defined for VISIBLE, FULL_RAY or
476  // HALF_RAY. tol increases the virtual size of the triangle
477  // by a relative factor.
478  inline pointHit intersection
479  (
480  const point& p,
481  const vector& q,
482  const intersection::algorithm alg,
483  const scalar tol = 0.0
484  ) const;
485 
486  //- Find the nearest point to p on the triangle and classify it:
487  // + near point (nearType=POINT, nearLabel=0, 1, 2)
488  // + near edge (nearType=EDGE, nearLabel=0, 1, 2)
489  // Note: edges are counted from starting
490  // vertex so e.g. edge 2 is from f[2] to f[0]
492  (
493  const point& p,
494  label& nearType,
495  label& nearLabel
496  ) const;
497 
498  //- Return nearest point to p on triangle
499  inline pointHit nearestPoint(const point& p) const;
500 
501  //- Classify nearest point to p in triangle plane
502  // w.r.t. triangle edges and points. Returns inside
503  // (true)/outside (false).
504  bool classify
505  (
506  const point& p,
507  label& nearType,
508  label& nearLabel
509  ) const;
510 
511  //- Return nearest point to line on triangle. Returns hit if
512  // point is inside triangle. Sets edgePoint to point on edge
513  // (hit if nearest is inside line)
514  inline pointHit nearestPoint
515  (
516  const linePointRef& edge,
517  pointHit& edgePoint
518  ) const;
519 
520  //- The sign for which side of the face plane the point is on.
521  // Uses the supplied tolerance for rounding around zero.
522  // \return
523  // - 0: on plane
524  // - +1: above plane
525  // - -1: below plane
526  inline int sign(const point& p, const scalar tol = SMALL) const;
527 
528  //- Decompose triangle into triangles above and below plane
529  template<class AboveOp, class BelowOp>
530  inline void sliceWithPlane
531  (
532  const plane& pln,
533  AboveOp& aboveOp,
534  BelowOp& belowOp
535  ) const;
536 
537  //- Decompose triangle into triangles inside and outside
538  // (with respect to user provided normal) other
539  // triangle.
540  template<class InsideOp, class OutsideOp>
541  inline void triangleOverlap
542  (
543  const vector& n,
544  const triangle<Point, PointRef>& tri,
545  InsideOp& insideOp,
546  OutsideOp& outsideOp
547  ) const;
548 
549 
550  // IOstream Operators
551 
552  friend Istream& operator>> <Point, PointRef>
553  (
554  Istream&,
555  triangle&
556  );
557 
558  friend Ostream& operator<< <Point, PointRef>
559  (
560  Ostream&,
561  const triangle&
562  );
563 };
564 
565 
566 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
567 
568 } // End namespace Foam
569 
570 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571 
572 #include "triangleI.H"
573 
574 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
575 
576 #ifdef NoRepository
577 # include "triangle.C"
578 #endif
579 
580 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
581 
582 #endif
583 
584 // ************************************************************************* //
scalar sweptVol(const triangle &t) const
Return swept-volume.
Definition: triangleI.H:393
const point & c() const noexcept
The third vertex.
Definition: triangle.H:152
A triangle primitive used to calculate face normals and swept volumes. Uses referred points...
Definition: triangle.H:234
A line primitive.
Definition: line.H:52
barycentric2D pointToBarycentric(const point &pt) const
Calculate the barycentric coordinates from the given point.
Definition: triangleI.H:466
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
scalar mag() const
The magnitude of the triangle area.
Definition: triangleI.H:309
void operator()(const triPoints &)
Definition: triangleI.H:1088
Unknown proximity.
Definition: triangle.H:256
vector vecC() const
Edge vector opposite point c(): from a() to b()
Definition: triangleI.H:279
scalar quality() const
Return quality: Ratio of triangle and circum-circle.
Definition: triangleI.H:377
void sliceWithPlane(const plane &pln, AboveOp &aboveOp, BelowOp &belowOp) const
Decompose triangle into triangles above and below plane.
Definition: triangle.C:107
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Pair< Point > box() const
The enclosing (bounding) box for the triangle.
Definition: triangleI.H:236
vector vecB() const
Edge vector opposite point b(): from c() to a()
Definition: triangleI.H:273
tensor inertia(PointRef refPt=Zero, scalar density=1.0) const
Return the inertia tensor, with optional reference.
Definition: triangleI.H:412
Random rndGen
Definition: createFields.H:23
const Point & a() const noexcept
The first vertex.
Definition: triangle.H:399
Point point_type
The point type.
Definition: triangle.H:243
pointHit nearestPointClassify(const point &p, label &nearType, label &nearLabel) const
Find the nearest point to p on the triangle and classify it:
Definition: triangleI.H:754
triPointRef tri() const
Return as triangle reference.
Definition: triangleI.H:287
void flip()
Flip triangle orientation by swapping second and third vertices.
Definition: triangleI.H:293
triPoints()=default
Default construct.
Geometric class that creates a 3D plane and can return the intersection point between a line and the ...
Definition: plane.H:90
const Point & c() const noexcept
The third vertex.
Definition: triangle.H:409
int sign(const point &p, const scalar tol=SMALL) const
The sign for which side of the face plane the point is on.
Definition: triangleI.H:1053
Point vecB() const
Edge vector opposite point b(): from c() to a()
Definition: triangleI.H:255
Close to point.
Definition: triangle.H:257
Templated 2D Barycentric derived from VectorSpace. Has 3 components, one of which is redundant...
Definition: Barycentric2D.H:50
FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") vector normal() const
Legacy name for areaNormal().
Definition: triangle.H:479
void back()=delete
The back() accessor (from FixedList) has no purpose.
triangle(const Point &p0, const Point &p1, const Point &p2)
Construct from three points.
Definition: triangleI.H:81
bool classify(const point &p, label &nearType, label &nearLabel) const
Classify nearest point to p in triangle plane.
Definition: triangleI.H:917
pointHit nearestPoint(const point &p) const
Return nearest point to p on triangle.
Definition: triangleI.H:903
proxType
Proximity classifications.
Definition: triangle.H:254
vector unitNormal() const
Return unit normal.
Definition: triangleI.H:207
Close to edge.
Definition: triangle.H:258
const pointField & points
pointHit ray(const point &p, const vector &q, const intersection::algorithm=intersection::FULL_RAY, const intersection::direction dir=intersection::VECTOR) const
Return point intersection with a ray.
Definition: triangleI.H:557
An edge is a list of two vertex labels. This can correspond to a directed graph edge or an edge on a ...
Definition: edge.H:59
const Point & b() const noexcept
The second vertex.
Definition: triangle.H:404
Point vecA() const
Edge vector opposite point a(): from b() to c()
Definition: triangleI.H:249
Istream & operator>>(Istream &, directionInfo &)
Triangle point storage. Default constructable (triangle is not)
Definition: triangle.H:73
FixedList< triPoints, 27 > triIntersectionList
Storage type for triangles originating from intersecting triangle with another triangle.
Definition: triangle.H:249
scalar magSqr() const
The magnitude squared of the triangle area.
Definition: triangleI.H:315
Point centre() const
Return centre (centroid)
Definition: triangleI.H:155
scalar mag() const
The magnitude of the triangle area.
Definition: triangleI.H:302
Vector< scalar > vector
Definition: vector.H:57
Describes the interaction of a object and a (templated) point. It carries the info of a successful hi...
Definition: pointHit.H:43
scalar circumRadius() const
Return circum-radius.
Definition: triangleI.H:356
void operator()(const triPoints &) const noexcept
Definition: triangle.H:272
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
Random number generator.
Definition: Random.H:55
scalar magSqr() const
The magnitude squared of the triangle area.
Definition: triangleI.H:322
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:265
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
void front()=delete
The front() accessor (from FixedList) has no purpose.
vector areaNormal() const
The area normal - with magnitude equal to area of triangle.
Definition: triangleI.H:186
const point & a() const noexcept
The first vertex.
Definition: triangle.H:142
Point barycentricToPoint(const barycentric2D &bary) const
Calculate the point from the given barycentric coordinates.
Definition: triangleI.H:456
bool intersects(const point &origin, const vector &normal) const
Fast intersection detection with a plane.
Definition: triangleI.H:517
void triangleOverlap(const vector &n, const triangle< Point, PointRef > &tri, InsideOp &insideOp, OutsideOp &outsideOp) const
Decompose triangle into triangles inside and outside.
Definition: triangle.C:120
CGAL::Point_3< K > Point
Pair< point > box() const
The enclosing (bounding) box for the triangle.
Definition: triangleI.H:242
constexpr sumAreaOp() noexcept
Definition: triangle.H:283
storeOp(triIntersectionList &, label &)
Definition: triangleI.H:1076
Store resulting tris.
Definition: triangle.H:293
Point vecC() const
Edge vector opposite point c(): from a() to b()
Definition: triangleI.H:261
Point circumCentre() const
Return circum-centre.
Definition: triangleI.H:329
void operator()(const triPoints &)
Definition: triangleI.H:1066
vector vecA() const
Edge vector opposite point a(): from b() to c()
Definition: triangleI.H:267
triIntersectionList & tris_
Definition: triangle.H:295
Point randomPoint(Random &rndGen) const
Return a random point on the triangle from a uniform distribution.
Definition: triangleI.H:448
label n
components
Component labeling enumeration.
Definition: Vector.H:83
volScalarField & p
pointHit intersection(const point &p, const vector &q, const intersection::algorithm alg, const scalar tol=0.0) const
Fast intersection with a ray.
Definition: triangleI.H:672
scalar total() const noexcept
Definition: triangle.H:285
Tensor of scalars, i.e. Tensor<scalar>.
const point & b() const noexcept
The second vertex.
Definition: triangle.H:147
vector unitNormal() const
Return unit normal.
Definition: triangleI.H:213
const volScalarField & p0
Definition: EEqn.H:36
point centre() const
Return centre (centroid)
Definition: triangleI.H:161
Namespace for OpenFOAM.
vector areaNormal() const
The area normal - with magnitude equal to area of triangle.
Definition: triangleI.H:180
const pointField & pts
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127