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 | Copyright (C) 2011-2013 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 normals 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 "cachedRandom.H"
44 #include "FixedList.H"
45 #include "UList.H"
46 #include "linePointRef.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 scalar magnitude
135  inline scalar mag() const;
136 
137  //- Return vector normal
138  inline vector normal() const;
139 
140  //- Return circum-centre
141  inline Point circumCentre() const;
142 
143  //- Return circum-radius
144  inline scalar circumRadius() const;
145 
146  //- Return quality: Ratio of triangle and circum-circle
147  // area, scaled so that an equilateral triangle has a
148  // quality of 1
149  inline scalar quality() const;
150 
151  //- Return swept-volume
152  inline scalar sweptVol(const triangle& t) const;
153 
154  //- Return the inertia tensor, with optional reference
155  // point and density specification
156  inline tensor inertia
157  (
158  PointRef refPt = vector::zero,
159  scalar density = 1.0
160  ) const;
161 
162  //- Return a random point on the triangle from a uniform
163  // distribution
164  inline Point randomPoint(Random& rndGen) const;
165 
166  //- Return a random point on the triangle from a uniform
167  // distribution
168  inline Point randomPoint(cachedRandom& rndGen) const;
169 
170  //- Calculate the barycentric coordinates of the given
171  // point, in the same order as a, b, c. Returns the
172  // determinant of the solution.
173  inline scalar barycentric
174  (
175  const point& pt,
176  List<scalar>& bary
177  ) const;
178 
179  //- Return point intersection with a ray.
180  // For a hit, the distance is signed. Positive number
181  // represents the point in front of triangle.
182  // In case of miss pointHit is set to nearest point
183  // on triangle and its distance to the distance between
184  // the original point and the plane intersection point
185  inline pointHit ray
186  (
187  const point& p,
188  const vector& q,
191  ) const;
192 
193  //- Fast intersection with a ray.
194  // For a hit, the pointHit.distance() is the line parameter t :
195  // intersection=p+t*q. Only defined for VISIBLE, FULL_RAY or
196  // HALF_RAY. tol increases the virtual size of the triangle
197  // by a relative factor.
198  inline pointHit intersection
199  (
200  const point& p,
201  const vector& q,
202  const intersection::algorithm alg,
203  const scalar tol = 0.0
204  ) const;
205 
206  //- Find the nearest point to p on the triangle and classify it:
207  // + near point (nearType=POINT, nearLabel=0, 1, 2)
208  // + near edge (nearType=EDGE, nearLabel=0, 1, 2)
209  // Note: edges are counted from starting
210  // vertex so e.g. edge 2 is from f[2] to f[0]
212  (
213  const point& p,
214  label& nearType,
215  label& nearLabel
216  ) const;
217 
218  //- Return nearest point to p on triangle
219  inline pointHit nearestPoint(const point& p) const;
220 
221  //- Classify nearest point to p in triangle plane
222  // w.r.t. triangle edges and points. Returns inside
223  // (true)/outside (false).
224  bool classify
225  (
226  const point& p,
227  label& nearType,
228  label& nearLabel
229  ) const;
230 
231  //- Return nearest point to line on triangle. Returns hit if
232  // point is inside triangle. Sets edgePoint to point on edge
233  // (hit if nearest is inside line)
234  inline pointHit nearestPoint
235  (
236  const linePointRef& edge,
237  pointHit& edgePoint
238  ) const;
239 
240 
241  // IOstream operators
242 
243  friend Istream& operator>> <Point, PointRef>
244  (
245  Istream&,
246  triangle&
247  );
248 
249  friend Ostream& operator<< <Point, PointRef>
250  (
251  Ostream&,
252  const triangle&
253  );
254 };
255 
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 
259 } // End namespace Foam
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 #include "triangleI.H"
264 
265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266 
267 #endif
268 
269 // ************************************************************************* //
Point randomPoint(Random &rndGen) const
Return a random point on the triangle from a uniform.
Definition: triangleI.H:232
Simple random number generator.
Definition: Random.H:49
cachedRandom rndGen(label(0),-1)
triangle(const Point &a, const Point &b, const Point &c)
Construct from three points.
Definition: triangleI.H:34
scalar quality() const
Return quality: Ratio of triangle and circum-circle.
Definition: triangleI.H:161
scalar mag() const
Return scalar magnitude.
Definition: triangleI.H:96
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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:425
const Point & c() const
Return third vertex.
Definition: triangleI.H:82
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:59
scalar circumRadius() const
Return circum-radius.
Definition: triangleI.H:137
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:309
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
A triangle primitive used to calculate face normals and swept volumes.
Definition: triangle.H:57
Namespace for OpenFOAM.
A line primitive.
Definition: line.H:56
pointHit nearestPoint(const point &p) const
Return nearest point to p on triangle.
Definition: triangleI.H:657
scalar sweptVol(const triangle &t) const
Return swept-volume.
Definition: triangleI.H:177
vector normal() const
Return vector normal.
Definition: triangleI.H:103
volScalarField & p
Definition: createFields.H:51
Point centre() const
Return centre (centroid)
Definition: triangleI.H:89
Random number generator.
Definition: cachedRandom.H:63
bool classify(const point &p, label &nearType, label &nearLabel) const
Classify nearest point to p in triangle plane.
Definition: triangleI.H:671
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
static const Vector zero
Definition: Vector.H:80
proxType
Return types for classify.
Definition: triangle.H:89
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const Point & a() const
Return first vertex.
Definition: triangleI.H:70
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:508
const Point & b() const
Return second vertex.
Definition: triangleI.H:76
tensor inertia(PointRef refPt=vector::zero, scalar density=1.0) const
Return the inertia tensor, with optional reference.
Definition: triangleI.H:196
scalar barycentric(const point &pt, List< scalar > &bary) const
Calculate the barycentric coordinates of the given.
Definition: triangleI.H:268
Point circumCentre() const
Return circum-centre.
Definition: triangleI.H:110