1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "treeDataFace.H"
28 #include "triangleFuncs.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::treeDataFace, 0);
34 Foam::scalar Foam::treeDataFace::tolSqr = sqr(1E-6);
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 Foam::treeBoundBox Foam::treeDataFace::calcBb(const label faceI) const
41 const pointField& points = mesh_.points();
43 const face& f = mesh_.faces()[faceI];
45 treeBoundBox bb(points[f[0]], points[f[0]]);
47 for (label fp = 1; fp < f.size(); fp++)
49 const point& p = points[f[fp]];
51 bb.min() = min(bb.min(), p);
52 bb.max() = max(bb.max(), p);
58 void Foam::treeDataFace::update()
60 forAll(faceLabels_, i)
62 isTreeFace_.set(faceLabels_[i], 1);
67 bbs_.setSize(faceLabels_.size());
69 forAll(faceLabels_, i)
71 bbs_[i] = calcBb(faceLabels_[i]);
77 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
79 // Construct from components
80 Foam::treeDataFace::treeDataFace
83 const primitiveMesh& mesh,
84 const labelList& faceLabels
88 faceLabels_(faceLabels),
89 isTreeFace_(mesh.nFaces(), 0),
96 Foam::treeDataFace::treeDataFace
99 const primitiveMesh& mesh
103 faceLabels_(identity(mesh_.nFaces())),
104 isTreeFace_(mesh.nFaces(), 0),
111 Foam::treeDataFace::treeDataFace
114 const polyPatch& patch
117 mesh_(patch.boundaryMesh().mesh()),
120 identity(patch.size())
123 isTreeFace_(mesh_.nFaces(), 0),
130 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
132 Foam::pointField Foam::treeDataFace::points() const
134 pointField cc(faceLabels_.size());
136 forAll(faceLabels_, i)
138 cc[i] = mesh_.faceCentres()[faceLabels_[i]];
145 //- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
146 // Only makes sense for closed surfaces.
147 Foam::label Foam::treeDataFace::getVolumeType
149 const indexedOctree<treeDataFace>& oc,
153 // Need to determine whether sample is 'inside' or 'outside'
154 // Done by finding nearest face. This gives back a face which is
155 // guaranteed to contain nearest point. This point can be
156 // - in interior of face: compare to face normal
157 // - on edge of face: compare to edge normal
158 // - on point of face: compare to point normal
159 // Unfortunately the octree does not give us back the intersection point
160 // or where on the face it has hit so we have to recreate all that
164 // Find nearest face to sample
165 pointIndexHit info = oc.findNearest(sample, sqr(GREAT));
167 if (info.index() == -1)
171 "treeDataFace::getSampleType"
172 "(indexedOctree<treeDataFace>&, const point&)"
173 ) << "Could not find " << sample << " in octree."
174 << abort(FatalError);
178 // Get actual intersection point on face
179 label faceI = faceLabels_[info.index()];
183 Pout<< "getSampleType : sample:" << sample
184 << " nearest face:" << faceI;
187 const pointField& points = mesh_.points();
189 // Retest to classify where on face info is. Note: could be improved. We
190 // already have point.
192 const face& f = mesh_.faces()[faceI];
193 const vector& area = mesh_.faceAreas()[faceI];
194 const point& fc = mesh_.faceCentres()[faceI];
196 pointHit curHit = f.nearestPoint(sample, points);
197 const point& curPt = curHit.rawPoint();
200 // 1] Check whether sample is above face
205 // Nearest point inside face. Compare to face normal.
209 Pout<< " -> face hit:" << curPt
210 << " comparing to face normal " << area << endl;
212 return indexedOctree<treeDataFace>::getSide(area, sample - curPt);
217 Pout<< " -> face miss:" << curPt;
221 // 2] Check whether intersection is on one of the face vertices or
225 const scalar typDimSqr = mag(area) + VSMALL;
229 if ((magSqr(points[f[fp]] - curPt)/typDimSqr) < tolSqr)
231 // Face intersection point equals face vertex fp
233 // Calculate point normal (wrong: uses face normals instead of
235 const labelList& pFaces = mesh_.pointFaces()[f[fp]];
237 vector pointNormal(vector::zero);
241 if (isTreeFace_.get(pFaces[i]) == 1)
243 vector n = mesh_.faceAreas()[pFaces[i]];
244 n /= mag(n) + VSMALL;
252 Pout<< " -> face point hit :" << points[f[fp]]
253 << " point normal:" << pointNormal
255 << magSqr(points[f[fp]] - curPt)/typDimSqr << endl;
257 return indexedOctree<treeDataFace>::getSide
264 if ((magSqr(fc - curPt)/typDimSqr) < tolSqr)
266 // Face intersection point equals face centre. Normal at face centre
267 // is already average of face normals
271 Pout<< " -> centre hit:" << fc
272 << " distance:" << magSqr(fc - curPt)/typDimSqr << endl;
275 return indexedOctree<treeDataFace>::getSide(area, sample - curPt);
281 // 3] Get the 'real' edge the face intersection is on
284 const labelList& myEdges = mesh_.faceEdges()[faceI];
286 forAll(myEdges, myEdgeI)
288 const edge& e = mesh_.edges()[myEdges[myEdgeI]];
291 line<point, const point&>
295 ).nearestDist(sample);
298 if ((magSqr(edgeHit.rawPoint() - curPt)/typDimSqr) < tolSqr)
300 // Face intersection point lies on edge e
302 // Calculate edge normal (wrong: uses face normals instead of
304 const labelList& eFaces = mesh_.edgeFaces()[myEdges[myEdgeI]];
306 vector edgeNormal(vector::zero);
310 if (isTreeFace_.get(eFaces[i]) == 1)
312 vector n = mesh_.faceAreas()[eFaces[i]];
313 n /= mag(n) + VSMALL;
321 Pout<< " -> real edge hit point:" << edgeHit.rawPoint()
322 << " comparing to edge normal:" << edgeNormal
326 // Found face intersection point on this edge. Compare to edge
328 return indexedOctree<treeDataFace>::getSide
338 // 4] Get the internal edge the face intersection is on
343 pointHit edgeHit = line<point, const point&>
347 ).nearestDist(sample);
349 if ((magSqr(edgeHit.rawPoint() - curPt)/typDimSqr) < tolSqr)
351 // Face intersection point lies on edge between two face triangles
353 // Calculate edge normal as average of the two triangle normals
354 vector e = points[f[fp]] - fc;
355 vector ePrev = points[f[f.rcIndex(fp)]] - fc;
356 vector eNext = points[f[f.fcIndex(fp)]] - fc;
358 vector nLeft = ePrev ^ e;
359 nLeft /= mag(nLeft) + VSMALL;
361 vector nRight = e ^ eNext;
362 nRight /= mag(nRight) + VSMALL;
366 Pout<< " -> internal edge hit point:" << edgeHit.rawPoint()
367 << " comparing to edge normal "
368 << 0.5*(nLeft + nRight)
372 // Found face intersection point on this edge. Compare to edge
374 return indexedOctree<treeDataFace>::getSide
376 0.5*(nLeft + nRight),
384 Pout<< "Did not find sample " << sample
385 << " anywhere related to nearest face " << faceI << endl
390 Pout<< " vertex:" << f[fp] << " coord:" << points[f[fp]]
395 // Can't determine status of sample with respect to nearest face.
397 // - tolerances are wrong. (if e.g. face has zero area)
398 // - or (more likely) surface is not closed.
400 return indexedOctree<treeDataFace>::UNKNOWN;
404 // Check if any point on shape is inside cubeBb.
405 bool Foam::treeDataFace::overlaps
408 const treeBoundBox& cubeBb
411 // 1. Quick rejection: bb does not intersect face bb at all
414 if (!cubeBb.overlaps(bbs_[index]))
421 if (!cubeBb.overlaps(calcBb(faceLabels_[index])))
427 const pointField& points = mesh_.points();
430 // 2. Check if one or more face points inside
431 label faceI = faceLabels_[index];
433 const face& f = mesh_.faces()[faceI];
437 if (cubeBb.contains(points[f[fp]]))
443 // 3. Difficult case: all points are outside but connecting edges might
444 // go through cube. Use triangle-bounding box intersection.
445 const point& fc = mesh_.faceCentres()[faceI];
449 bool triIntersects = triangleFuncs::intersectBb
452 points[f[f.fcIndex(fp)]],
466 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
468 void Foam::treeDataFace::findNearest
470 const labelList& indices,
473 scalar& nearestDistSqr,
480 label index = indices[i];
482 const face& f = mesh_.faces()[faceLabels_[index]];
484 pointHit nearHit = f.nearestPoint(sample, mesh_.points());
485 scalar distSqr = sqr(nearHit.distance());
487 if (distSqr < nearestDistSqr)
489 nearestDistSqr = distSqr;
491 nearestPoint = nearHit.rawPoint();
497 bool Foam::treeDataFace::intersects
502 point& intersectionPoint
505 // Do quick rejection test
508 const treeBoundBox& faceBb = bbs_[index];
510 if ((faceBb.posBits(start) & faceBb.posBits(end)) != 0)
512 // start and end in same block outside of faceBb.
517 label faceI = faceLabels_[index];
519 const vector dir(end - start);
521 pointHit inter = mesh_.faces()[faceI].fastIntersection
525 mesh_.faceCentres()[faceI],
527 intersection::HALF_RAY
530 if (inter.hit() && inter.distance() <= 1)
532 // Note: no extra test on whether intersection is in front of us
533 // since using half_ray
534 intersectionPoint = inter.hitPoint();
544 // ************************************************************************* //