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-2019 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 
33 SourceFiles
34  triangleI.H
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef triangle_H
39 #define triangle_H
40 
41 #include "intersection.H"
42 #include "vector.H"
43 #include "tensor.H"
44 #include "pointHit.H"
45 #include "Random.H"
46 #include "FixedList.H"
47 #include "UList.H"
48 #include "linePointRef.H"
49 #include "barycentric2D.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Forward declarations
57 
58 class Istream;
59 class Ostream;
60 class triPoints;
61 class plane;
62 
63 template<class Point, class PointRef> class triangle;
64 
65 template<class Point, class PointRef>
66 inline Istream& operator>>
67 (
68  Istream&,
70 );
71 
72 template<class Point, class PointRef>
73 inline Ostream& operator<<
74 (
75  Ostream&,
77 );
78 
80 
81 
82 /*---------------------------------------------------------------------------*\
83  Class triangle Declaration
84 \*---------------------------------------------------------------------------*/
85 
86 template<class Point, class PointRef>
87 class triangle
88 {
89 public:
90 
91  // Public typedefs
92 
93  //- Storage type for triangles originating from intersecting triangle
94  // with another triangle
96 
97  //- Return types for classify
98  enum proxType
99  {
101  POINT, // Close to point
102  EDGE // Close to edge
103  };
104 
105 
106  // Public classes
107 
108  // Classes for use in sliceWithPlane. What to do with decomposition
109  // of triangle.
110 
111  //- Dummy
112  class dummyOp
113  {
114  public:
115  inline void operator()(const triPoints&);
116  };
117 
118  //- Sum resulting areas
119  class sumAreaOp
120  {
121  public:
122  scalar area_;
123 
124  inline sumAreaOp();
125 
126  inline void operator()(const triPoints&);
127  };
128 
129  //- Store resulting tris
130  class storeOp
131  {
132  public:
134  label& nTris_;
135 
136  inline storeOp(triIntersectionList&, label&);
137 
138  inline void operator()(const triPoints&);
139  };
140 
141 
142 private:
143 
144  // Private data
145 
146  PointRef a_, b_, c_;
147 
148 
149  // Private Member Functions
150 
151  //- Helper: calculate intersection point
152  inline static point planeIntersection
153  (
154  const FixedList<scalar, 3>& d,
155  const triPoints& t,
156  const label negI,
157  const label posI
158  );
159 
160  //- Helper: slice triangle with plane
161  template<class AboveOp, class BelowOp>
162  inline static void triSliceWithPlane
163  (
164  const plane& pln,
165  const triPoints& tri,
166  AboveOp& aboveOp,
167  BelowOp& belowOp
168  );
169 
170 
171 public:
172 
173  // Constructors
174 
175  //- Construct from three points
176  inline triangle(const Point& a, const Point& b, const Point& c);
177 
178  //- Construct from three points
179  inline triangle(const FixedList<Point, 3>& tri);
180 
181  //- Construct from three points in the list of points
182  // The indices could be from triFace etc.
183  inline triangle
184  (
185  const UList<Point>& points,
186  const FixedList<label, 3>& indices
187  );
188 
189  //- Construct from Istream
190  inline triangle(Istream& is);
191 
192 
193  // Member Functions
194 
195  // Access
196 
197  //- Return first vertex
198  inline const Point& a() const;
199 
200  //- Return second vertex
201  inline const Point& b() const;
202 
203  //- Return third vertex
204  inline const Point& c() const;
205 
206 
207  // Properties
208 
209  //- Return centre (centroid)
210  inline Point centre() const;
211 
212  //- The area normal - with magnitude equal to area of triangle
213  inline vector areaNormal() const;
214 
215  //- Return unit normal
216  inline vector unitNormal() const;
217 
218  //- Legacy name for areaNormal().
219  // \deprecated(2018-06) Deprecated for new use
220  vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()")
221  normal() const
222  {
223  return areaNormal();
224  }
225 
226  //- Return scalar magnitude
227  inline scalar mag() const;
228 
229  //- Return circum-centre
230  inline Point circumCentre() const;
231 
232  //- Return circum-radius
233  inline scalar circumRadius() const;
234 
235  //- Return quality: Ratio of triangle and circum-circle
236  // area, scaled so that an equilateral triangle has a
237  // quality of 1
238  inline scalar quality() const;
239 
240  //- Return swept-volume
241  inline scalar sweptVol(const triangle& t) const;
242 
243  //- Return the inertia tensor, with optional reference
244  // point and density specification
245  inline tensor inertia
246  (
247  PointRef refPt = Zero,
248  scalar density = 1.0
249  ) const;
250 
251  //- Return a random point on the triangle from a uniform
252  // distribution
253  inline Point randomPoint(Random& rndGen) const;
254 
255  //- Calculate the point from the given barycentric coordinates.
256  inline Point barycentricToPoint(const barycentric2D& bary) const;
257 
258  //- Calculate the barycentric coordinates from the given point
259  inline barycentric2D pointToBarycentric(const point& pt) const;
260 
261  //- Calculate the barycentric coordinates from the given point.
262  // Returns the determinant.
263  inline scalar pointToBarycentric
264  (
265  const point& pt,
266  barycentric2D& bary
267  ) const;
268 
269  //- Return point intersection with a ray.
270  // For a hit, the distance is signed. Positive number
271  // represents the point in front of triangle.
272  // In case of miss pointHit is set to nearest point
273  // on triangle and its distance to the distance between
274  // the original point and the plane intersection point
275  inline pointHit ray
276  (
277  const point& p,
278  const vector& q,
281  ) const;
282 
283  //- Fast intersection with a ray.
284  // For a hit, the pointHit.distance() is the line parameter t :
285  // intersection=p+t*q. Only defined for VISIBLE, FULL_RAY or
286  // HALF_RAY. tol increases the virtual size of the triangle
287  // by a relative factor.
288  inline pointHit intersection
289  (
290  const point& p,
291  const vector& q,
292  const intersection::algorithm alg,
293  const scalar tol = 0.0
294  ) const;
295 
296  //- Find the nearest point to p on the triangle and classify it:
297  // + near point (nearType=POINT, nearLabel=0, 1, 2)
298  // + near edge (nearType=EDGE, nearLabel=0, 1, 2)
299  // Note: edges are counted from starting
300  // vertex so e.g. edge 2 is from f[2] to f[0]
302  (
303  const point& p,
304  label& nearType,
305  label& nearLabel
306  ) const;
307 
308  //- Return nearest point to p on triangle
309  inline pointHit nearestPoint(const point& p) const;
310 
311  //- Classify nearest point to p in triangle plane
312  // w.r.t. triangle edges and points. Returns inside
313  // (true)/outside (false).
314  bool classify
315  (
316  const point& p,
317  label& nearType,
318  label& nearLabel
319  ) const;
320 
321  //- Return nearest point to line on triangle. Returns hit if
322  // point is inside triangle. Sets edgePoint to point on edge
323  // (hit if nearest is inside line)
324  inline pointHit nearestPoint
325  (
326  const linePointRef& edge,
327  pointHit& edgePoint
328  ) const;
329 
330  //- The sign for which side of the face plane the point is on.
331  // Uses the supplied tolerance for rounding around zero.
332  // \return
333  // - 0: on plane
334  // - +1: front-side
335  // - -1: back-side
336  inline int sign(const point& p, const scalar tol = SMALL) const;
337 
338  //- Decompose triangle into triangles above and below plane
339  template<class AboveOp, class BelowOp>
340  inline void sliceWithPlane
341  (
342  const plane& pln,
343  AboveOp& aboveOp,
344  BelowOp& belowOp
345  ) const;
346 
347  //- Decompose triangle into triangles inside and outside
348  // (with respect to user provided normal) other
349  // triangle.
350  template<class InsideOp, class OutsideOp>
351  inline void triangleOverlap
352  (
353  const vector& n,
354  const triangle<Point, PointRef>& tri,
355  InsideOp& insideOp,
356  OutsideOp& outsideOp
357  ) const;
358 
359 
360  // IOstream operators
361 
362  friend Istream& operator>> <Point, PointRef>
363  (
364  Istream&,
365  triangle&
366  );
367 
368  friend Ostream& operator<< <Point, PointRef>
369  (
370  Ostream&,
371  const triangle&
372  );
373 };
374 
375 
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
377 
378 } // End namespace Foam
379 
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
381 
382 #include "triangleI.H"
383 
384 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
385 
386 #ifdef NoRepository
387 # include "triangle.C"
388 #endif
389 
390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
391 
392 #endif
393 
394 // ************************************************************************* //
Foam::intersection::direction
direction
Definition: intersection.H:66
Foam::Random
Random number generator.
Definition: Random.H:59
Foam::Tensor< scalar >
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::triangle::triIntersectionList
FixedList< triPoints, 27 > triIntersectionList
Storage type for triangles originating from intersecting triangle.
Definition: triangle.H:94
Foam::triangle::storeOp::operator()
void operator()(const triPoints &)
Definition: triangleI.H:873
intersection.H
Foam::triangle::storeOp
Store resulting tris.
Definition: triangle.H:129
Foam::triangle::randomPoint
Point randomPoint(Random &rndGen) const
Return a random point on the triangle from a uniform.
Definition: triangleI.H:254
Foam::PointHit< point >
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::triangle::sumAreaOp::sumAreaOp
sumAreaOp()
Definition: triangleI.H:843
Foam::triangle::sign
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:823
Foam::triangle::classify
bool classify(const point &p, label &nearType, label &nearLabel) const
Classify nearest point to p in triangle plane.
Definition: triangleI.H:685
Foam::triangle::inertia
tensor inertia(PointRef refPt=Zero, scalar density=1.0) const
Return the inertia tensor, with optional reference.
Definition: triangleI.H:218
triangle.C
Foam::triangle::dummyOp
Dummy.
Definition: triangle.H:111
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:63
Foam::intersection::FULL_RAY
Definition: intersection.H:74
Foam::Barycentric2D
Templated 2D Barycentric derived from VectorSpace. Has 3 components, one of which is redundant.
Definition: Barycentric2D.H:54
Foam::triangle::sumAreaOp::operator()
void operator()(const triPoints &)
Definition: triangleI.H:851
pointHit.H
tensor.H
Foam::triangle::centre
Point centre() const
Return centre (centroid)
Definition: triangleI.H:105
Foam::plane
Geometric class that creates a 3D plane and can return the intersection point between a line and the ...
Definition: plane.H:89
Foam::triangle::unitNormal
vector unitNormal() const
Return unit normal.
Definition: triangleI.H:119
Foam::triangle
A triangle primitive used to calculate face normals and swept volumes.
Definition: triangle.H:62
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::triPoints
Triangle storage. Null constructable (unfortunately triangle<point, point> is not)
Definition: triPoints.H:52
Foam::triangle::c
const Point & c() const
Return third vertex.
Definition: triangleI.H:98
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::triangle::a
const Point & a() const
Return first vertex.
Definition: triangleI.H:86
Foam::triangle::NONE
Definition: triangle.H:99
Foam::intersection::algorithm
algorithm
Definition: intersection.H:72
Foam::triangle::ray
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:323
Foam::triangle::sumAreaOp::area_
scalar area_
Definition: triangle.H:121
Foam::triangle::b
const Point & b() const
Return second vertex.
Definition: triangleI.H:92
Foam::triangle::circumRadius
scalar circumRadius() const
Return circum-radius.
Definition: triangleI.H:162
Foam::triangle::POINT
Definition: triangle.H:100
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::intersection::VECTOR
Definition: intersection.H:68
triangleI.H
Random.H
Foam::triangle::sumAreaOp
Sum resulting areas.
Definition: triangle.H:118
Foam::triangle::mag
scalar mag() const
Return scalar magnitude.
Definition: triangleI.H:128
Foam::triangle::nearestPointClassify
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:522
Foam::triangle::FOAM_DEPRECATED_FOR
vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") normal() const
Legacy name for areaNormal().
Definition: triangle.H:219
Foam::triangle::EDGE
Definition: triangle.H:101
UList.H
barycentric2D.H
Foam::triangle::proxType
proxType
Return types for classify.
Definition: triangle.H:97
Foam::Vector< scalar >
Foam::triangle::storeOp::nTris_
label & nTris_
Definition: triangle.H:133
Foam::triangle::quality
scalar quality() const
Return quality: Ratio of triangle and circum-circle.
Definition: triangleI.H:183
Foam::triangle::circumCentre
Point circumCentre() const
Return circum-centre.
Definition: triangleI.H:135
Foam::triangle::dummyOp::operator()
void operator()(const triPoints &)
Definition: triangleI.H:836
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList
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:103
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::triangle::intersection
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:439
Foam::line
A line primitive.
Definition: line.H:59
vector.H
Foam::triangle::triangle
triangle(const Point &a, const Point &b, const Point &c)
Construct from three points.
Definition: triangleI.H:38
Foam::triangle::storeOp::tris_
triIntersectionList & tris_
Definition: triangle.H:132
Point
CGAL::Point_3< K > Point
Definition: CGALIndexedPolyhedron.H:53
rndGen
Random rndGen
Definition: createFields.H:23
Foam::triangle::nearestPoint
pointHit nearestPoint(const point &p) const
Return nearest point to p on triangle.
Definition: triangleI.H:671
Foam::triangle::pointToBarycentric
barycentric2D pointToBarycentric(const point &pt) const
Calculate the barycentric coordinates from the given point.
Definition: triangleI.H:272
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::triangle::barycentricToPoint
Point barycentricToPoint(const barycentric2D &bary) const
Calculate the point from the given barycentric coordinates.
Definition: triangleI.H:262
FixedList.H
Foam::triangle::areaNormal
vector areaNormal() const
The area normal - with magnitude equal to area of triangle.
Definition: triangleI.H:112
Foam::triangle::sweptVol
scalar sweptVol(const triangle &t) const
Return swept-volume.
Definition: triangleI.H:199
Foam::triangle::sliceWithPlane
void sliceWithPlane(const plane &pln, AboveOp &aboveOp, BelowOp &belowOp) const
Decompose triangle into triangles above and below plane.
Definition: triangle.C:115
Foam::triPointRef
triangle< point, const point & > triPointRef
Definition: triangle.H:78
linePointRef.H
Foam::triangle::triangleOverlap
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:128
Foam::triangle::storeOp::storeOp
storeOp(triIntersectionList &, label &)
Definition: triangleI.H:861