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 "surfaceToCell.H"
28 #include "meshSearch.H"
29 #include "triSurface.H"
30 #include "triSurfaceSearch.H"
31 #include "cellClassification.H"
34 #include "addToRunTimeSelectionTable.H"
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
41 defineTypeNameAndDebug(surfaceToCell, 0);
43 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, word);
45 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, istream);
50 Foam::topoSetSource::addToUsageTable Foam::surfaceToCell::usage_
52 surfaceToCell::typeName,
53 "\n Usage: surfaceToCell"
54 "<surface> <outsidePoints> <cut> <inside> <outside> <near> <curvature>\n\n"
55 " <surface> name of triSurface\n"
56 " <outsidePoints> list of points that define outside\n"
57 " <cut> boolean whether to include cells cut by surface\n"
58 " <inside> ,, ,, inside surface\n"
59 " <outside> ,, ,, outside surface\n"
60 " <near> scalar; include cells with centre <= near to surface\n"
61 " <curvature> scalar; include cells close to strong curvature"
63 " (curvature defined as difference in surface normal at nearest"
64 " point on surface for each vertex of cell)\n\n"
68 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
70 Foam::label Foam::surfaceToCell::getNearest
72 const triSurfaceSearch& querySurf,
79 Map<label>::const_iterator iter = cache.find(pointI);
81 if (iter != cache.end())
83 // Found cached answer
88 pointIndexHit inter = querySurf.nearest(pt, span);
90 // Triangle label (can be -1)
91 label triI = inter.index();
93 // Store triangle on point
94 cache.insert(pointI, triI);
101 // Return true if nearest surface to points on cell makes largish angle
102 // with nearest surface to cell centre. Returns false otherwise. Points visited
103 // are cached in pointToNearest
104 bool Foam::surfaceToCell::differingPointNormals
106 const triSurfaceSearch& querySurf,
108 const vector& span, // current search span
110 const label cellTriI, // nearest (to cell centre) surface triangle
112 Map<label>& pointToNearest // cache for nearest triangle to point
115 const triSurface& surf = querySurf.surface();
116 const vectorField& normals = surf.faceNormals();
118 const faceList& faces = mesh().faces();
119 const pointField& points = mesh().points();
121 const labelList& cFaces = mesh().cells()[cellI];
123 forAll(cFaces, cFaceI)
125 const face& f = faces[cFaces[cFaceI]];
129 label pointI = f[fp];
141 if (pointTriI != -1 && pointTriI != cellTriI)
143 scalar cosAngle = normals[pointTriI] & normals[cellTriI];
156 void Foam::surfaceToCell::combine(topoSet& set, const bool add) const
160 if (includeCut_ || includeInside_ || includeOutside_)
163 // Cut cells with surface and classify cells
167 // Construct search engine on mesh
169 meshSearch queryMesh(mesh_, true);
172 // Check all 'outside' points
173 forAll(outsidePoints_, outsideI)
175 const point& outsidePoint = outsidePoints_[outsideI];
177 // Find cell point is in. Linear search.
178 label cellI = queryMesh.findCell(outsidePoint, -1, false);
179 if (returnReduce(cellI, maxOp<label>()) == -1)
181 FatalErrorIn("surfaceToCell::combine(topoSet&, const bool)")
182 << "outsidePoint " << outsidePoint
183 << " is not inside any cell"
188 // Cut faces with surface and classify cells
190 cellClassification cellType
199 Info<< " Marked inside/outside in = "
200 << timer.cpuTimeIncrement() << " s" << endl << endl;
203 forAll(cellType, cellI)
205 label cType = cellType[cellI];
211 && (cType == cellClassification::CUT)
215 && (cType == cellClassification::INSIDE)
219 && (cType == cellClassification::OUTSIDE)
223 addOrDelete(set, cellI, add);
232 // Determine distance to surface
235 const pointField& ctrs = mesh_.cellCentres();
237 // Box dimensions to search in octree.
238 const vector span(nearDist_, nearDist_, nearDist_);
243 Info<< " Selecting cells with cellCentre closer than "
244 << nearDist_ << " to surface" << endl;
246 // No need to test curvature. Insert near cells into set.
250 const point& c = ctrs[cellI];
252 pointIndexHit inter = querySurf().nearest(c, span);
254 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
256 addOrDelete(set, cellI, add);
260 Info<< " Determined nearest surface point in = "
261 << timer.cpuTimeIncrement() << " s" << endl << endl;
266 // Test near cells for curvature
268 Info<< " Selecting cells with cellCentre closer than "
269 << nearDist_ << " to surface and curvature factor"
270 << " less than " << curvature_ << endl;
272 // Cache for nearest surface triangle for a point
273 Map<label> pointToNearest(mesh_.nCells()/10);
277 const point& c = ctrs[cellI];
279 pointIndexHit inter = querySurf().nearest(c, span);
281 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
285 differingPointNormals
290 inter.index(), // nearest surface triangle
295 addOrDelete(set, cellI, add);
300 Info<< " Determined nearest surface point in = "
301 << timer.cpuTimeIncrement() << " s" << endl << endl;
307 void Foam::surfaceToCell::checkSettings() const
314 (includeCut_ && includeInside_ && includeOutside_)
315 || (!includeCut_ && !includeInside_ && !includeOutside_)
321 "surfaceToCell:checkSettings()"
322 ) << "Illegal include cell specification."
323 << " Result would be either all or no cells." << endl
324 << "Please set one of includeCut, includeInside, includeOutside"
325 << " to true, set nearDistance to a value > 0"
326 << " or set curvature to a value -1 .. 1."
332 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
334 // Construct from components
335 Foam::surfaceToCell::surfaceToCell
337 const polyMesh& mesh,
338 const fileName& surfName,
339 const pointField& outsidePoints,
340 const bool includeCut,
341 const bool includeInside,
342 const bool includeOutside,
343 const scalar nearDist,
344 const scalar curvature
349 outsidePoints_(outsidePoints),
350 includeCut_(includeCut),
351 includeInside_(includeInside),
352 includeOutside_(includeOutside),
354 curvature_(curvature),
355 surfPtr_(new triSurface(surfName_)),
356 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
363 // Construct from components. Externally supplied surface.
364 Foam::surfaceToCell::surfaceToCell
366 const polyMesh& mesh,
367 const fileName& surfName,
368 const triSurface& surf,
369 const triSurfaceSearch& querySurf,
370 const pointField& outsidePoints,
371 const bool includeCut,
372 const bool includeInside,
373 const bool includeOutside,
374 const scalar nearDist,
375 const scalar curvature
380 outsidePoints_(outsidePoints),
381 includeCut_(includeCut),
382 includeInside_(includeInside),
383 includeOutside_(includeOutside),
385 curvature_(curvature),
387 querySurfPtr_(&querySurf),
394 // Construct from dictionary
395 Foam::surfaceToCell::surfaceToCell
397 const polyMesh& mesh,
398 const dictionary& dict
402 surfName_(dict.lookup("file")),
403 outsidePoints_(dict.lookup("outsidePoints")),
404 includeCut_(readBool(dict.lookup("includeCut"))),
405 includeInside_(readBool(dict.lookup("includeInside"))),
406 includeOutside_(readBool(dict.lookup("includeOutside"))),
407 nearDist_(readScalar(dict.lookup("nearDistance"))),
408 curvature_(readScalar(dict.lookup("curvature"))),
409 surfPtr_(new triSurface(surfName_)),
410 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
417 // Construct from Istream
418 Foam::surfaceToCell::surfaceToCell
420 const polyMesh& mesh,
425 surfName_(checkIs(is)),
426 outsidePoints_(checkIs(is)),
427 includeCut_(readBool(checkIs(is))),
428 includeInside_(readBool(checkIs(is))),
429 includeOutside_(readBool(checkIs(is))),
430 nearDist_(readScalar(checkIs(is))),
431 curvature_(readScalar(checkIs(is))),
432 surfPtr_(new triSurface(surfName_)),
433 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
440 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
442 Foam::surfaceToCell::~surfaceToCell()
452 delete querySurfPtr_;
458 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
460 void Foam::surfaceToCell::applyToSet
462 const topoSetSource::setAction action,
466 if ( (action == topoSetSource::NEW) || (action == topoSetSource::ADD))
468 Info<< " Adding cells in relation to surface " << surfName_
473 else if (action == topoSetSource::DELETE)
475 Info<< " Removing cells in relation to surface " << surfName_
483 // ************************************************************************* //