triangle.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::triangle
26 
27 Description
28  A triangle primitive used to calculate face areas and swept volumes.
29 
30 SourceFiles
31  triangleI.H
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef triangle_H
36 #define triangle_H
37 
38 #include "intersection.H"
39 #include "vector.H"
40 #include "tensor.H"
41 #include "pointHit.H"
42 #include "Random.H"
43 #include "FixedList.H"
44 #include "UList.H"
45 #include "linePointRef.H"
46 #include "barycentric2D.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 class Istream;
54 class Ostream;
55 
56 // Forward declaration of friend functions and operators
57 
58 template<class Point, class PointRef> class triangle;
59 
60 template<class Point, class PointRef>
61 inline Istream& operator>>
62 (
63  Istream&,
65 );
66 
67 template<class Point, class PointRef>
68 inline Ostream& operator<<
69 (
70  Ostream&,
72 );
73 
74 
75 /*---------------------------------------------------------------------------*\
76  Class triangle Declaration
77 \*---------------------------------------------------------------------------*/
78 
79 template<class Point, class PointRef>
80 class triangle
81 {
82  // Private Data
83 
84  PointRef a_, b_, c_;
85 
86 
87 public:
88 
89  //- Return types for classify
90  enum proxType
91  {
93  POINT, // Close to point
94  EDGE // Close to edge
95  };
96 
97 
98  // Constructors
99 
100  //- Construct from three points
101  inline triangle(const Point& a, const Point& b, const Point& c);
102 
103  //- Construct from three points in the list of points
104  // The indices could be from triFace etc.
105  inline triangle
106  (
107  const UList<Point>&,
108  const FixedList<label, 3>& indices
109  );
110 
111  //- Construct from Istream
112  inline triangle(Istream&);
113 
114 
115  // Member Functions
116 
117  // Access
118 
119  //- Return first vertex
120  inline const Point& a() const;
121 
122  //- Return second vertex
123  inline const Point& b() const;
124 
125  //- Return third vertex
126  inline const Point& c() const;
127 
128 
129  // Properties
130 
131  //- Return centre (centroid)
132  inline Point centre() const;
133 
134  //- Return vector area
135  inline vector area() const;
136 
137  //- Return scalar magnitude
138  inline scalar mag() const;
139 
140  //- Return unit normal
141  inline vector normal() const;
142 
143  //- Return circum-centre
144  inline Point circumCentre() const;
145 
146  //- Return circum-radius
147  inline scalar circumRadius() const;
148 
149  //- Return quality: Ratio of triangle and circum-circle
150  // area, scaled so that an equilateral triangle has a
151  // quality of 1
152  inline scalar quality() const;
153 
154  //- Return swept-volume
155  inline scalar sweptVol(const triangle& t) const;
156 
157  //- Return the inertia tensor, with optional reference
158  // point and density specification
159  inline tensor inertia
160  (
161  PointRef refPt = vector::zero,
162  scalar density = 1.0
163  ) const;
164 
165  //- Return a random point on the triangle from a uniform
166  // distribution
167  inline Point randomPoint(Random& rndGen) const;
168 
169  //- Calculate the point from the given barycentric coordinates.
170  inline Point barycentricToPoint(const barycentric2D& bary) const;
171 
172  //- Calculate the barycentric coordinates from the given point
173  inline barycentric2D pointToBarycentric(const point& pt) const;
174 
175  //- Calculate the barycentric coordinates from the given point.
176  // Returns the determinant.
177  inline scalar pointToBarycentric
178  (
179  const point& pt,
180  barycentric2D& bary
181  ) const;
182 
183  //- Return point intersection with a ray.
184  // For a hit, the distance is signed. Positive number
185  // represents the point in front of triangle.
186  // In case of miss pointHit is set to nearest point
187  // on triangle and its distance to the distance between
188  // the original point and the plane intersection point
189  inline pointHit ray
190  (
191  const point& p,
192  const vector& q,
195  const intersection::direction dir =
197  ) const;
198 
199  //- Fast intersection with a ray.
200  // For a hit, the pointHit.distance() is the line parameter t :
201  // intersection=p+t*q. Only defined for algorithm::visible,
202  // algorithm::fullRay or algorithm::halfRay. tol increases the
203  // virtual size of the triangle by a relative factor.
204  inline pointHit intersection
205  (
206  const point& p,
207  const vector& q,
208  const intersection::algorithm alg,
209  const scalar tol = 0.0
210  ) const;
211 
212  //- Find the nearest point to p on the triangle and classify it:
213  // + near point (nearType=POINT, nearLabel=0, 1, 2)
214  // + near edge (nearType=EDGE, nearLabel=0, 1, 2)
215  // Note: edges are counted from starting
216  // vertex so e.g. edge 2 is from f[2] to f[0]
218  (
219  const point& p,
220  label& nearType,
221  label& nearLabel
222  ) const;
223 
224  //- Return nearest point to p on triangle
225  inline pointHit nearestPoint(const point& p) const;
226 
227  //- Classify nearest point to p in triangle plane
228  // w.r.t. triangle edges and points. Returns inside
229  // (true)/outside (false).
230  bool classify
231  (
232  const point& p,
233  label& nearType,
234  label& nearLabel
235  ) const;
236 
237  //- Return nearest point to line on triangle. Returns hit if
238  // point is inside triangle. Sets edgePoint to point on edge
239  // (hit if nearest is inside line)
240  inline pointHit nearestPoint
241  (
242  const linePointRef& edge,
243  pointHit& edgePoint
244  ) const;
245 
246 
247  // IOstream Operators
248 
249  friend Istream& operator>> <Point, PointRef>
250  (
251  Istream&,
252  triangle&
253  );
254 
255  friend Ostream& operator<< <Point, PointRef>
256  (
257  Ostream&,
258  const triangle&
259  );
260 };
261 
262 
263 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264 
265 } // End namespace Foam
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 #include "triangleI.H"
270 
271 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
272 
273 #endif
274 
275 // ************************************************************************* //
scalar sweptVol(const triangle &t) const
Return swept-volume.
Definition: triangleI.H:186
A triangle primitive used to calculate face areas and swept volumes.
Definition: triangle.H:57
A line primitive.
Definition: line.H:56
const Point & c() const
Return third vertex.
Definition: triangleI.H:82
barycentric2D pointToBarycentric(const point &pt) const
Calculate the barycentric coordinates from the given point.
Definition: triangleI.H:259
scalar mag() const
Return scalar magnitude.
Definition: triangleI.H:103
scalar quality() const
Return quality: Ratio of triangle and circum-circle.
Definition: triangleI.H:170
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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:518
Templated 2D Barycentric derived from VectorSpace. Has 3 components, one of which is redundant...
Definition: Barycentric2D.H:50
vector area() const
Return vector area.
Definition: triangleI.H:96
Random rndGen(label(0))
bool classify(const point &p, label &nearType, label &nearLabel) const
Classify nearest point to p in triangle plane.
Definition: triangleI.H:681
pointHit nearestPoint(const point &p) const
Return nearest point to p on triangle.
Definition: triangleI.H:667
proxType
Return types for classify.
Definition: triangle.H:89
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Point centre() const
Return centre (centroid)
Definition: triangleI.H:89
scalar circumRadius() const
Return circum-radius.
Definition: triangleI.H:146
tensor inertia(PointRef refPt=vector::zero, scalar density=1.0) const
Return the inertia tensor, with optional reference.
Definition: triangleI.H:205
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:60
Random number generator.
Definition: Random.H:57
vector normal() const
Return unit normal.
Definition: triangleI.H:110
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
triangle(const Point &a, const Point &b, const Point &c)
Construct from three points.
Definition: triangleI.H:34
Point barycentricToPoint(const barycentric2D &bary) const
Calculate the point from the given barycentric coordinates.
Definition: triangleI.H:249
const Point & a() const
Return first vertex.
Definition: triangleI.H:70
pointHit ray(const point &p, const vector &q, const intersection::algorithm=intersection::algorithm::fullRay, const intersection::direction dir=intersection::direction::vector) const
Return point intersection with a ray.
Definition: triangleI.H:310
Point circumCentre() const
Return circum-centre.
Definition: triangleI.H:119
const Point & b() const
Return second vertex.
Definition: triangleI.H:76
Point randomPoint(Random &rndGen) const
Return a random point on the triangle from a uniform.
Definition: triangleI.H:241
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:431
Namespace for OpenFOAM.