/
faceIntersection.C
313 lines (255 loc) · 8.15 KB
/
faceIntersection.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "face.H"
#include "pointHit.H"
#include "triPointRef.H"
#include "line.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::pointHit Foam::face::ray
(
const point& p,
const vector& n,
const pointField& meshPoints,
const intersection::algorithm alg,
const intersection::direction dir
) const
{
// Return potential intersection with face with a ray starting
// at p, direction n (does not need to be normalized)
// Does face-center decomposition and returns triangle intersection
// point closest to p.
// In case of miss the point is the nearest point intersection of the
// face plane and the ray and the distance is the distance between the
// intersection point and the nearest point on the face
// If the face is a triangle, do a direct calculation
if (size() == 3)
{
return triPointRef
(
meshPoints[operator[](0)],
meshPoints[operator[](1)],
meshPoints[operator[](2)]
).ray(p, n, alg, dir);
}
point ctr = Foam::average(points(meshPoints));
scalar nearestHitDist = GREAT;
scalar nearestMissDist = GREAT;
bool eligible = false;
// Initialize to miss, distance = GREAT
pointHit nearest(p);
const labelList& f = *this;
label nPoints = size();
point nextPoint = ctr;
for (label pI = 0; pI < nPoints; pI++)
{
nextPoint = meshPoints[f[fcIndex(pI)]];
// Note: for best accuracy, centre point always comes last
//
pointHit curHit = triPointRef
(
meshPoints[f[pI]],
nextPoint,
ctr
).ray(p, n, alg, dir);
if (curHit.hit())
{
if (Foam::mag(curHit.distance()) < Foam::mag(nearestHitDist))
{
nearestHitDist = curHit.distance();
nearest.setHit();
nearest.setPoint(curHit.hitPoint());
}
}
else if (!nearest.hit())
{
// Miss and no hit yet. Update miss statistics.
if (curHit.eligibleMiss())
{
eligible = true;
// Miss distance is the distance between the plane intersection
// point and the nearest point of the triangle
scalar missDist =
Foam::mag
(
p + curHit.distance()*n
- curHit.missPoint()
);
if (missDist < nearestMissDist)
{
nearestMissDist = missDist;
nearest.setDistance(curHit.distance());
nearest.setPoint(curHit.missPoint());
}
}
}
}
if (nearest.hit())
{
nearest.setDistance(nearestHitDist);
}
else
{
// Haven't hit a single face triangle
nearest.setMiss(eligible);
}
return nearest;
}
Foam::pointHit Foam::face::intersection
(
const point& p,
const vector& q,
const point& ctr,
const pointField& meshPoints,
const intersection::algorithm alg,
const scalar tol
) const
{
// If the face is a triangle, do a direct calculation
if (size() == 3)
{
return triPointRef
(
meshPoints[operator[](0)],
meshPoints[operator[](1)],
meshPoints[operator[](2)]
).intersection(p, q, alg, tol);
}
scalar nearestHitDist = VGREAT;
// Initialize to miss, distance = GREAT
pointHit nearest(p);
const labelList& f = *this;
forAll(f, pI)
{
// Note: for best accuracy, centre point always comes last
pointHit curHit = triPointRef
(
meshPoints[f[pI]],
meshPoints[f[fcIndex(pI)]],
ctr
).intersection(p, q, alg, tol);
if (curHit.hit())
{
if (Foam::mag(curHit.distance()) < Foam::mag(nearestHitDist))
{
nearestHitDist = curHit.distance();
nearest.setHit();
nearest.setPoint(curHit.hitPoint());
}
}
}
if (nearest.hit())
{
nearest.setDistance(nearestHitDist);
}
return nearest;
}
Foam::pointHit Foam::face::nearestPoint
(
const point& p,
const pointField& meshPoints
) const
{
// Dummy labels
label nearType = -1;
label nearLabel = -1;
return nearestPointClassify(p, meshPoints, nearType, nearLabel);
}
Foam::pointHit Foam::face::nearestPointClassify
(
const point& p,
const pointField& meshPoints,
label& nearType,
label& nearLabel
) const
{
// If the face is a triangle, do a direct calculation
if (size() == 3)
{
return triPointRef
(
meshPoints[operator[](0)],
meshPoints[operator[](1)],
meshPoints[operator[](2)]
).nearestPointClassify(p, nearType, nearLabel);
}
const face& f = *this;
point ctr = centre(meshPoints);
// Initialize to miss, distance=GREAT
pointHit nearest(p);
nearType = -1;
nearLabel = -1;
label nPoints = f.size();
point nextPoint = ctr;
for (label pI = 0; pI < nPoints; pI++)
{
nextPoint = meshPoints[f[fcIndex(pI)]];
label tmpNearType = -1;
label tmpNearLabel = -1;
// Note: for best accuracy, centre point always comes last
triPointRef tri
(
meshPoints[f[pI]],
nextPoint,
ctr
);
pointHit curHit = tri.nearestPointClassify
(
p,
tmpNearType,
tmpNearLabel
);
if (Foam::mag(curHit.distance()) < Foam::mag(nearest.distance()))
{
nearest.setDistance(curHit.distance());
// Assume at first that the near type is NONE on the
// triangle (i.e. on the face of the triangle) then it is
// therefore also for the face.
nearType = NONE;
if (tmpNearType == triPointRef::EDGE && tmpNearLabel == 0)
{
// If the triangle edge label is 0, then this is also
// an edge of the face, if not, it is on the face
nearType = EDGE;
nearLabel = pI;
}
else if (tmpNearType == triPointRef::POINT && tmpNearLabel < 2)
{
// If the triangle point label is 0 or 1, then this is
// also a point of the face, if not, it is on the face
nearType = POINT;
nearLabel = pI + tmpNearLabel;
}
if (curHit.hit())
{
nearest.setHit();
nearest.setPoint(curHit.hitPoint());
}
else
{
// In nearest point, miss is always eligible
nearest.setMiss(true);
nearest.setPoint(curHit.missPoint());
}
}
}
return nearest;
}
// ************************************************************************* //