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 \*---------------------------------------------------------------------------*/
26 #include "treeDataPolyMeshCell.H"
27 #include "indexedOctree.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::treeDataPolyMeshCell, 0);
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37 Foam::treeBoundBox Foam::treeDataPolyMeshCell::calcCellBb(const label cellI) const
39 const cellList& cells = mesh_.cells();
40 const faceList& faces = mesh_.faces();
41 const pointField& points = mesh_.points();
45 vector(GREAT, GREAT, GREAT),
46 vector(-GREAT, -GREAT, -GREAT)
49 const cell& cFaces = cells[cellI];
51 forAll(cFaces, cFaceI)
53 const face& f = faces[cFaces[cFaceI]];
57 const point& p = points[f[fp]];
59 cellBb.min() = min(cellBb.min(), p);
60 cellBb.max() = max(cellBb.max(), p);
67 void Foam::treeDataPolyMeshCell::update()
71 bbs_.setSize(cellLabels_.size());
73 forAll(cellLabels_, i)
75 bbs_[i] = calcCellBb(cellLabels_[i]);
81 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
83 Foam::treeDataPolyMeshCell::treeDataPolyMeshCell
87 const labelUList& cellLabels
91 cellLabels_(cellLabels),
98 Foam::treeDataPolyMeshCell::treeDataPolyMeshCell
101 const polyMesh& mesh,
102 const Xfer<labelList>& cellLabels
106 cellLabels_(cellLabels),
113 Foam::treeDataPolyMeshCell::treeDataPolyMeshCell
120 cellLabels_(identity(mesh_.nCells())),
127 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 Foam::pointField Foam::treeDataPolyMeshCell::shapePoints() const
131 pointField cc(cellLabels_.size());
133 forAll(cellLabels_, i)
135 cc[i] = mesh_.cellCentres()[cellLabels_[i]];
142 bool Foam::treeDataPolyMeshCell::overlaps
145 const treeBoundBox& cubeBb
150 return cubeBb.overlaps(bbs_[index]);
154 return cubeBb.overlaps(calcCellBb(cellLabels_[index]));
159 bool Foam::treeDataPolyMeshCell::contains
165 //return mesh_.pointInCell(sample, cellLabels_[index]);
166 label tetFaceI, tetPtI;
167 mesh_.findTetFacePt(cellLabels_[index], sample, tetFaceI, tetPtI);
168 return tetFaceI != -1;
172 void Foam::treeDataPolyMeshCell::findNearest
174 const labelUList& indices,
177 scalar& nearestDistSqr,
184 label index = indices[i];
185 label cellI = cellLabels_[index];
186 scalar distSqr = magSqr(sample - mesh_.cellCentres()[cellI]);
188 if (distSqr < nearestDistSqr)
190 nearestDistSqr = distSqr;
192 nearestPoint = mesh_.cellCentres()[cellI];
198 bool Foam::treeDataPolyMeshCell::intersects
203 point& intersectionPoint
206 // Do quick rejection test
209 const treeBoundBox& cellBb = bbs_[index];
211 if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0)
213 // start and end in same block outside of cellBb.
219 const treeBoundBox cellBb = calcCellBb(cellLabels_[index]);
221 if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0)
223 // start and end in same block outside of cellBb.
229 // Do intersection with all faces of cell
230 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232 // Disable picking up intersections behind us.
233 scalar oldTol = intersection::setPlanarTol(0.0);
235 const cell& cFaces = mesh_.cells()[cellLabels_[index]];
237 const vector dir(end - start);
238 scalar minDistSqr = magSqr(dir);
243 const face& f = mesh_.faces()[cFaces[i]];
245 pointHit inter = f.ray
250 intersection::HALF_RAY
253 if (inter.hit() && sqr(inter.distance()) <= minDistSqr)
255 // Note: no extra test on whether intersection is in front of us
256 // since using half_ray AND zero tolerance. (note that tolerance
257 // is used to look behind us)
258 minDistSqr = sqr(inter.distance());
259 intersectionPoint = inter.hitPoint();
264 // Restore picking tolerance
265 intersection::setPlanarTol(oldTol);
271 // ************************************************************************* //