1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
28 #include "triPointRef.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 // Return potential intersection with face with a ray starting
34 // at p, direction n (does not need to be normalized)
35 // Does face-center decomposition and returns triangle intersection
36 // point closest to p.
38 // In case of miss the point is the nearest point intersection of the
39 // face plane and the ray and the distance is the distance between the
40 // intersection point and the nearest point on the face
42 Foam::pointHit Foam::face::ray
46 const pointField& meshPoints,
47 const intersection::algorithm alg,
48 const intersection::direction dir
51 // If the face is a triangle, do a direct calculation
56 meshPoints[operator[](0)],
57 meshPoints[operator[](1)],
58 meshPoints[operator[](2)]
59 ).ray(p, n, alg, dir);
62 point ctr = Foam::average(points(meshPoints));
64 scalar nearestHitDist = GREAT;
66 scalar nearestMissDist = GREAT;
67 bool eligible = false;
69 // Initialize to miss, distance = GREAT
72 const labelList& f = *this;
74 label nPoints = size();
76 point nextPoint = ctr;
78 for (label pI = 0; pI < nPoints; pI++)
80 nextPoint = meshPoints[f[fcIndex(pI)]];
82 // Note: for best accuracy, centre point always comes last
84 pointHit curHit = triPointRef
89 ).ray(p, n, alg, dir);
93 if (Foam::mag(curHit.distance()) < Foam::mag(nearestHitDist))
95 nearestHitDist = curHit.distance();
97 nearest.setPoint(curHit.hitPoint());
100 else if (!nearest.hit())
102 // Miss and no hit yet. Update miss statistics.
103 if (curHit.eligibleMiss())
107 // Miss distance is the distance between the plane intersection
108 // point and the nearest point of the triangle
112 p + curHit.distance()*n
116 if (missDist < nearestMissDist)
118 nearestMissDist = missDist;
119 nearest.setDistance(curHit.distance());
120 nearest.setPoint(curHit.missPoint());
128 nearest.setDistance(nearestHitDist);
132 // Haven't hit a single face triangle
133 nearest.setMiss(eligible);
140 Foam::pointHit Foam::face::intersection
145 const pointField& meshPoints,
146 const intersection::algorithm alg,
150 // If the face is a triangle, do a direct calculation
155 meshPoints[operator[](0)],
156 meshPoints[operator[](1)],
157 meshPoints[operator[](2)]
158 ).intersection(p, q, alg, tol);
161 scalar nearestHitDist = VGREAT;
163 // Initialize to miss, distance = GREAT
166 const labelList& f = *this;
170 // Note: for best accuracy, centre point always comes last
171 pointHit curHit = triPointRef
174 meshPoints[f[fcIndex(pI)]],
176 ).intersection(p, q, alg, tol);
180 if (Foam::mag(curHit.distance()) < Foam::mag(nearestHitDist))
182 nearestHitDist = curHit.distance();
184 nearest.setPoint(curHit.hitPoint());
191 nearest.setDistance(nearestHitDist);
198 Foam::pointHit Foam::face::nearestPoint
201 const pointField& meshPoints
206 label nearLabel = -1;
208 return nearestPointClassify(p, meshPoints, nearType, nearLabel);
212 Foam::pointHit Foam::face::nearestPointClassify
215 const pointField& meshPoints,
220 // If the face is a triangle, do a direct calculation
225 meshPoints[operator[](0)],
226 meshPoints[operator[](1)],
227 meshPoints[operator[](2)]
228 ).nearestPointClassify(p, nearType, nearLabel);
231 const face& f = *this;
232 point ctr = centre(meshPoints);
234 // Initialize to miss, distance=GREAT
240 label nPoints = f.size();
242 point nextPoint = ctr;
244 for (label pI = 0; pI < nPoints; pI++)
246 nextPoint = meshPoints[f[fcIndex(pI)]];
248 label tmpNearType = -1;
249 label tmpNearLabel = -1;
251 // Note: for best accuracy, centre point always comes last
259 pointHit curHit = tri.nearestPointClassify
266 if (Foam::mag(curHit.distance()) < Foam::mag(nearest.distance()))
268 nearest.setDistance(curHit.distance());
270 // Assume at first that the near type is NONE on the
271 // triangle (i.e. on the face of the triangle) then it is
272 // therefore also for the face.
276 if (tmpNearType == triPointRef::EDGE && tmpNearLabel == 0)
278 // If the triangle edge label is 0, then this is also
279 // an edge of the face, if not, it is on the face
285 else if (tmpNearType == triPointRef::POINT && tmpNearLabel < 2)
287 // If the triangle point label is 0 or 1, then this is
288 // also a point of the face, if not, it is on the face
292 nearLabel = pI + tmpNearLabel;
298 nearest.setPoint(curHit.hitPoint());
302 // In nearest point, miss is always eligible
303 nearest.setMiss(true);
304 nearest.setPoint(curHit.missPoint());
313 // ************************************************************************* //