1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 \*---------------------------------------------------------------------------*/
29 #include "surfaceToCell.H"
31 #include "meshSearch.H"
32 #include "triSurface.H"
33 #include "triSurfaceSearch.H"
34 #include "cellClassification.H"
36 #include "addToRunTimeSelectionTable.H"
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
43 defineTypeNameAndDebug(surfaceToCell, 0);
45 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, word);
47 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, istream);
52 Foam::topoSetSource::addToUsageTable Foam::surfaceToCell::usage_
54 surfaceToCell::typeName,
55 "\n Usage: surfaceToCell"
56 "<surface> <outsidePoints> <cut> <inside> <outside> <near> <curvature>\n\n"
57 " <surface> name of triSurface\n"
58 " <outsidePoints> list of points that define outside\n"
59 " <cut> boolean whether to include cells cut by surface\n"
60 " <inside> ,, ,, inside surface\n"
61 " <outside> ,, ,, outside surface\n"
62 " <near> scalar; include cells with centre <= near to surface\n"
63 " <curvature> scalar; include cells close to strong curvature"
65 " (curvature defined as difference in surface normal at nearest"
66 " point on surface for each vertex of cell)\n\n"
70 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
72 Foam::label Foam::surfaceToCell::getNearest
74 const triSurfaceSearch& querySurf,
81 Map<label>::const_iterator iter = cache.find(pointI);
83 if (iter != cache.end())
85 // Found cached answer
90 pointIndexHit inter = querySurf.nearest(pt, span);
92 // Triangle label (can be -1)
93 label triI = inter.index();
95 // Store triangle on point
96 cache.insert(pointI, triI);
103 // Return true if nearest surface to points on cell makes largish angle
104 // with nearest surface to cell centre. Returns false otherwise. Points visited
105 // are cached in pointToNearest
106 bool Foam::surfaceToCell::differingPointNormals
108 const triSurfaceSearch& querySurf,
110 const vector& span, // current search span
112 const label cellTriI, // nearest (to cell centre) surface triangle
114 Map<label>& pointToNearest // cache for nearest triangle to point
117 const triSurface& surf = querySurf.surface();
118 const vectorField& normals = surf.faceNormals();
120 const faceList& faces = mesh().faces();
121 const pointField& points = mesh().points();
123 const labelList& cFaces = mesh().cells()[cellI];
125 forAll(cFaces, cFaceI)
127 const face& f = faces[cFaces[cFaceI]];
131 label pointI = f[fp];
143 if (pointTriI != -1 && pointTriI != cellTriI)
145 scalar cosAngle = normals[pointTriI] & normals[cellTriI];
158 void Foam::surfaceToCell::combine(topoSet& set, const bool add) const
162 if (includeCut_ || includeInside_ || includeOutside_)
165 // Cut cells with surface and classify cells
169 // Construct search engine on mesh
171 meshSearch queryMesh(mesh_, true);
174 // Check all 'outside' points
175 forAll(outsidePoints_, outsideI)
177 const point& outsidePoint = outsidePoints_[outsideI];
179 // Find cell point is in. Linear search.
180 if (queryMesh.findCell(outsidePoint, -1, false) == -1)
182 FatalErrorIn("surfaceToCell::combine(topoSet&, const bool)")
183 << "outsidePoint " << outsidePoint
184 << " is not inside any cell"
189 // Cut faces with surface and classify cells
191 cellClassification cellType
200 Info<< " Marked inside/outside in = "
201 << timer.cpuTimeIncrement() << " s" << endl << endl;
204 forAll(cellType, cellI)
206 label cType = cellType[cellI];
212 && (cType == cellClassification::CUT)
216 && (cType == cellClassification::INSIDE)
220 && (cType == cellClassification::OUTSIDE)
224 addOrDelete(set, cellI, add);
233 // Determine distance to surface
236 const pointField& ctrs = mesh_.cellCentres();
238 // Box dimensions to search in octree.
239 const vector span(nearDist_, nearDist_, nearDist_);
244 Info<< " Selecting cells with cellCentre closer than "
245 << nearDist_ << " to surface" << endl;
247 // No need to test curvature. Insert near cells into set.
251 const point& c = ctrs[cellI];
253 pointIndexHit inter = querySurf().nearest(c, span);
255 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
257 addOrDelete(set, cellI, add);
261 Info<< " Determined nearest surface point in = "
262 << timer.cpuTimeIncrement() << " s" << endl << endl;
267 // Test near cells for curvature
269 Info<< " Selecting cells with cellCentre closer than "
270 << nearDist_ << " to surface and curvature factor"
271 << " less than " << curvature_ << endl;
273 // Cache for nearest surface triangle for a point
274 Map<label> pointToNearest(mesh_.nCells()/10);
278 const point& c = ctrs[cellI];
280 pointIndexHit inter = querySurf().nearest(c, span);
282 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
286 differingPointNormals
291 inter.index(), // nearest surface triangle
296 addOrDelete(set, cellI, add);
301 Info<< " Determined nearest surface point in = "
302 << timer.cpuTimeIncrement() << " s" << endl << endl;
308 void Foam::surfaceToCell::checkSettings() const
315 (includeCut_ && includeInside_ && includeOutside_)
316 || (!includeCut_ && !includeInside_ && !includeOutside_)
322 "surfaceToCell:checkSettings()"
323 ) << "Illegal include cell specification."
324 << " Result would be either all or no cells." << endl
325 << "Please set one of includeCut, includeInside, includeOutside"
326 << " to true, set nearDistance to a value > 0"
327 << " or set curvature to a value -1 .. 1."
333 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
335 // Construct from components
336 Foam::surfaceToCell::surfaceToCell
338 const polyMesh& mesh,
339 const fileName& surfName,
340 const pointField& outsidePoints,
341 const bool includeCut,
342 const bool includeInside,
343 const bool includeOutside,
344 const scalar nearDist,
345 const scalar curvature
350 outsidePoints_(outsidePoints),
351 includeCut_(includeCut),
352 includeInside_(includeInside),
353 includeOutside_(includeOutside),
355 curvature_(curvature),
356 surfPtr_(new triSurface(surfName_)),
357 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
364 // Construct from components. Externally supplied surface.
365 Foam::surfaceToCell::surfaceToCell
367 const polyMesh& mesh,
368 const fileName& surfName,
369 const triSurface& surf,
370 const triSurfaceSearch& querySurf,
371 const pointField& outsidePoints,
372 const bool includeCut,
373 const bool includeInside,
374 const bool includeOutside,
375 const scalar nearDist,
376 const scalar curvature
381 outsidePoints_(outsidePoints),
382 includeCut_(includeCut),
383 includeInside_(includeInside),
384 includeOutside_(includeOutside),
386 curvature_(curvature),
388 querySurfPtr_(&querySurf),
395 // Construct from dictionary
396 Foam::surfaceToCell::surfaceToCell
398 const polyMesh& mesh,
399 const dictionary& dict
403 surfName_(dict.lookup("file")),
404 outsidePoints_(dict.lookup("outsidePoints")),
405 includeCut_(readBool(dict.lookup("includeCut"))),
406 includeInside_(readBool(dict.lookup("includeInside"))),
407 includeOutside_(readBool(dict.lookup("includeOutside"))),
408 nearDist_(readScalar(dict.lookup("nearDistance"))),
409 curvature_(readScalar(dict.lookup("curvature"))),
410 surfPtr_(new triSurface(surfName_)),
411 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
418 // Construct from Istream
419 Foam::surfaceToCell::surfaceToCell
421 const polyMesh& mesh,
426 surfName_(checkIs(is)),
427 outsidePoints_(checkIs(is)),
428 includeCut_(readBool(checkIs(is))),
429 includeInside_(readBool(checkIs(is))),
430 includeOutside_(readBool(checkIs(is))),
431 nearDist_(readScalar(checkIs(is))),
432 curvature_(readScalar(checkIs(is))),
433 surfPtr_(new triSurface(surfName_)),
434 querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
441 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
443 Foam::surfaceToCell::~surfaceToCell()
453 delete querySurfPtr_;
459 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
461 void Foam::surfaceToCell::applyToSet
463 const topoSetSource::setAction action,
467 if ( (action == topoSetSource::NEW) || (action == topoSetSource::ADD))
469 Info<< " Adding cells in relation to surface " << surfName_
474 else if (action == topoSetSource::DELETE)
476 Info<< " Removing cells in relation to surface " << surfName_
484 // ************************************************************************* //