1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2011 OpenCFD Ltd.
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 "surfaceToCell.H"
28 #include "meshSearch.H"
29 #include "triSurface.H"
30 #include "triSurfaceSearch.H"
31 #include "cellClassification.H"
33 #include "addToRunTimeSelectionTable.H"
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 defineTypeNameAndDebug(surfaceToCell, 0);
42 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, word);
44 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, istream);
49 Foam::topoSetSource::addToUsageTable Foam::surfaceToCell::usage_
51 surfaceToCell::typeName,
52 "\n Usage: surfaceToCell"
53 "<surface> <outsidePoints> <cut> <inside> <outside> <near> <curvature>\n\n"
54 " <surface> name of triSurface\n"
55 " <outsidePoints> list of points that define outside\n"
56 " <cut> boolean whether to include cells cut by surface\n"
57 " <inside> ,, ,, inside surface\n"
58 " <outside> ,, ,, outside surface\n"
59 " <near> scalar; include cells with centre <= near to surface\n"
60 " <curvature> scalar; include cells close to strong curvature"
62 " (curvature defined as difference in surface normal at nearest"
63 " point on surface for each vertex of cell)\n\n"
67 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
69 Foam::label Foam::surfaceToCell::getNearest
71 const triSurfaceSearch& querySurf,
78 Map<label>::const_iterator iter = cache.find(pointI);
80 if (iter != cache.end())
82 // Found cached answer
87 pointIndexHit inter = querySurf.nearest(pt, span);
89 // Triangle label (can be -1)
90 label triI = inter.index();
92 // Store triangle on point
93 cache.insert(pointI, triI);
100 // Return true if nearest surface to points on cell makes largish angle
101 // with nearest surface to cell centre. Returns false otherwise. Points visited
102 // are cached in pointToNearest
103 bool Foam::surfaceToCell::differingPointNormals
105 const triSurfaceSearch& querySurf,
107 const vector& span, // current search span
109 const label cellTriI, // nearest (to cell centre) surface triangle
111 Map<label>& pointToNearest // cache for nearest triangle to point
114 const triSurface& surf = querySurf.surface();
115 const vectorField& normals = surf.faceNormals();
117 const faceList& faces = mesh().faces();
118 const pointField& points = mesh().points();
120 const labelList& cFaces = mesh().cells()[cellI];
122 forAll(cFaces, cFaceI)
124 const face& f = faces[cFaces[cFaceI]];
128 label pointI = f[fp];
140 if (pointTriI != -1 && pointTriI != cellTriI)
142 scalar cosAngle = normals[pointTriI] & normals[cellTriI];
155 void Foam::surfaceToCell::combine(topoSet& set, const bool add) const
159 if (includeCut_ || includeInside_ || includeOutside_)
162 // Cut cells with surface and classify cells
166 // Construct search engine on mesh
168 meshSearch queryMesh(mesh_, true);
171 // Check all 'outside' points
172 forAll(outsidePoints_, outsideI)
174 const point& outsidePoint = outsidePoints_[outsideI];
176 // Find cell point is in. Linear search.
177 label cellI = queryMesh.findCell(outsidePoint, -1, false);
178 if (returnReduce(cellI, maxOp<label>()) == -1)
180 FatalErrorIn("surfaceToCell::combine(topoSet&, const bool)")
181 << "outsidePoint " << outsidePoint
182 << " is not inside any cell"
187 // Cut faces with surface and classify cells
189 cellClassification cellType
198 Info<< " Marked inside/outside in = "
199 << timer.cpuTimeIncrement() << " s" << endl << endl;
202 forAll(cellType, cellI)
204 label cType = cellType[cellI];
210 && (cType == cellClassification::CUT)
214 && (cType == cellClassification::INSIDE)
218 && (cType == cellClassification::OUTSIDE)
222 addOrDelete(set, cellI, add);
231 // Determine distance to surface
234 const pointField& ctrs = mesh_.cellCentres();
236 // Box dimensions to search in octree.
237 const vector span(nearDist_, nearDist_, nearDist_);
242 Info<< " Selecting cells with cellCentre closer than "
243 << nearDist_ << " to surface" << endl;
245 // No need to test curvature. Insert near cells into set.
249 const point& c = ctrs[cellI];
251 pointIndexHit inter = querySurf().nearest(c, span);
253 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
255 addOrDelete(set, cellI, add);
259 Info<< " Determined nearest surface point in = "
260 << timer.cpuTimeIncrement() << " s" << endl << endl;
265 // Test near cells for curvature
267 Info<< " Selecting cells with cellCentre closer than "
268 << nearDist_ << " to surface and curvature factor"
269 << " less than " << curvature_ << endl;
271 // Cache for nearest surface triangle for a point
272 Map<label> pointToNearest(mesh_.nCells()/10);
276 const point& c = ctrs[cellI];
278 pointIndexHit inter = querySurf().nearest(c, span);
280 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
284 differingPointNormals
289 inter.index(), // nearest surface triangle
294 addOrDelete(set, cellI, add);
299 Info<< " Determined nearest surface point in = "
300 << timer.cpuTimeIncrement() << " s" << endl << endl;
306 void Foam::surfaceToCell::checkSettings() const
313 (includeCut_ && includeInside_ && includeOutside_)
314 || (!includeCut_ && !includeInside_ && !includeOutside_)
320 "surfaceToCell:checkSettings()"
321 ) << "Illegal include cell specification."
322 << " Result would be either all or no cells." << endl
323 << "Please set one of includeCut, includeInside, includeOutside"
324 << " to true, set nearDistance to a value > 0"
325 << " or set curvature to a value -1 .. 1."
331 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
333 // Construct from components
334 Foam::surfaceToCell::surfaceToCell
336 const polyMesh& mesh,
337 const fileName& surfName,
338 const pointField& outsidePoints,
339 const bool includeCut,
340 const bool includeInside,
341 const bool includeOutside,
342 const scalar nearDist,
343 const scalar curvature
348 outsidePoints_(outsidePoints),
349 includeCut_(includeCut),
350 includeInside_(includeInside),
351 includeOutside_(includeOutside),
353 curvature_(curvature),
354 surfPtr_(new triSurface(surfName_)),
355 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
362 // Construct from components. Externally supplied surface.
363 Foam::surfaceToCell::surfaceToCell
365 const polyMesh& mesh,
366 const fileName& surfName,
367 const triSurface& surf,
368 const triSurfaceSearch& querySurf,
369 const pointField& outsidePoints,
370 const bool includeCut,
371 const bool includeInside,
372 const bool includeOutside,
373 const scalar nearDist,
374 const scalar curvature
379 outsidePoints_(outsidePoints),
380 includeCut_(includeCut),
381 includeInside_(includeInside),
382 includeOutside_(includeOutside),
384 curvature_(curvature),
386 querySurfPtr_(&querySurf),
393 // Construct from dictionary
394 Foam::surfaceToCell::surfaceToCell
396 const polyMesh& mesh,
397 const dictionary& dict
401 surfName_(dict.lookup("file")),
402 outsidePoints_(dict.lookup("outsidePoints")),
403 includeCut_(readBool(dict.lookup("includeCut"))),
404 includeInside_(readBool(dict.lookup("includeInside"))),
405 includeOutside_(readBool(dict.lookup("includeOutside"))),
406 nearDist_(readScalar(dict.lookup("nearDistance"))),
407 curvature_(readScalar(dict.lookup("curvature"))),
408 surfPtr_(new triSurface(surfName_)),
409 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
416 // Construct from Istream
417 Foam::surfaceToCell::surfaceToCell
419 const polyMesh& mesh,
424 surfName_(checkIs(is)),
425 outsidePoints_(checkIs(is)),
426 includeCut_(readBool(checkIs(is))),
427 includeInside_(readBool(checkIs(is))),
428 includeOutside_(readBool(checkIs(is))),
429 nearDist_(readScalar(checkIs(is))),
430 curvature_(readScalar(checkIs(is))),
431 surfPtr_(new triSurface(surfName_)),
432 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
439 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
441 Foam::surfaceToCell::~surfaceToCell()
451 delete querySurfPtr_;
457 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
459 void Foam::surfaceToCell::applyToSet
461 const topoSetSource::setAction action,
465 if ( (action == topoSetSource::NEW) || (action == topoSetSource::ADD))
467 Info<< " Adding cells in relation to surface " << surfName_
472 else if (action == topoSetSource::DELETE)
474 Info<< " Removing cells in relation to surface " << surfName_
482 // ************************************************************************* //