00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "searchableSurface.H"
00028 #include "shellSurfaces.H"
00029 #include "boundBox.H"
00030 #include "triSurfaceMesh.H"
00031 #include "refinementSurfaces.H"
00032 #include "searchableSurfaces.H"
00033 #include "orientedSurface.H"
00034 #include "pointIndexHit.H"
00035
00036
00037
00038
00039 namespace Foam
00040 {
00041
00042 template<>
00043 const char*
00044 NamedEnum<shellSurfaces::refineMode, 3>::
00045 names[] =
00046 {
00047 "inside",
00048 "outside",
00049 "distance"
00050 };
00051
00052 const NamedEnum<shellSurfaces::refineMode, 3> shellSurfaces::refineModeNames_;
00053
00054 }
00055
00056
00057
00058
00059
00060 void Foam::shellSurfaces::setAndCheckLevels
00061 (
00062 const label shellI,
00063 const List<Tuple2<scalar, label> >& distLevels
00064 )
00065 {
00066 if (modes_[shellI] != DISTANCE && distLevels.size() != 1)
00067 {
00068 FatalErrorIn
00069 (
00070 "shellSurfaces::shellSurfaces"
00071 "(const searchableSurfaces&, const dictionary&)"
00072 ) << "For refinement mode "
00073 << refineModeNames_[modes_[shellI]]
00074 << " specify only one distance+level."
00075 << " (its distance gets discarded)"
00076 << exit(FatalError);
00077 }
00078
00079 distances_[shellI].setSize(distLevels.size());
00080 levels_[shellI].setSize(distLevels.size());
00081
00082 forAll(distLevels, j)
00083 {
00084 distances_[shellI][j] = distLevels[j].first();
00085 levels_[shellI][j] = distLevels[j].second();
00086
00087
00088 if (j > 0)
00089 {
00090 if
00091 (
00092 (distances_[shellI][j] <= distances_[shellI][j-1])
00093 || (levels_[shellI][j] > levels_[shellI][j-1])
00094 )
00095 {
00096 FatalErrorIn
00097 (
00098 "shellSurfaces::shellSurfaces"
00099 "(const searchableSurfaces&, const dictionary&)"
00100 ) << "For refinement mode "
00101 << refineModeNames_[modes_[shellI]]
00102 << " : Refinement should be specified in order"
00103 << " of increasing distance"
00104 << " (and decreasing refinement level)." << endl
00105 << "Distance:" << distances_[shellI][j]
00106 << " refinementLevel:" << levels_[shellI][j]
00107 << exit(FatalError);
00108 }
00109 }
00110 }
00111
00112 const searchableSurface& shell = allGeometry_[shells_[shellI]];
00113
00114 if (modes_[shellI] == DISTANCE)
00115 {
00116 Info<< "Refinement level according to distance to "
00117 << shell.name() << endl;
00118 forAll(levels_[shellI], j)
00119 {
00120 Info<< " level " << levels_[shellI][j]
00121 << " for all cells within " << distances_[shellI][j]
00122 << " meter." << endl;
00123 }
00124 }
00125 else
00126 {
00127 if (!allGeometry_[shells_[shellI]].hasVolumeType())
00128 {
00129 FatalErrorIn
00130 (
00131 "shellSurfaces::shellSurfaces"
00132 "(const searchableSurfaces&"
00133 ", const PtrList<dictionary>&)"
00134 ) << "Shell " << shell.name()
00135 << " does not support testing for "
00136 << refineModeNames_[modes_[shellI]] << endl
00137 << "Probably it is not closed."
00138 << exit(FatalError);
00139 }
00140
00141 if (modes_[shellI] == INSIDE)
00142 {
00143 Info<< "Refinement level " << levels_[shellI][0]
00144 << " for all cells inside " << shell.name() << endl;
00145 }
00146 else
00147 {
00148 Info<< "Refinement level " << levels_[shellI][0]
00149 << " for all cells outside " << shell.name() << endl;
00150 }
00151 }
00152 }
00153
00154
00155
00156
00157
00158 void Foam::shellSurfaces::orient()
00159 {
00160
00161 boundBox overallBb = boundBox::invertedBox;
00162
00163 bool hasSurface = false;
00164
00165 forAll(shells_, shellI)
00166 {
00167 const searchableSurface& s = allGeometry_[shells_[shellI]];
00168
00169 if (modes_[shellI] != DISTANCE && isA<triSurfaceMesh>(s))
00170 {
00171 const triSurfaceMesh& shell = refCast<const triSurfaceMesh>(s);
00172
00173 if (shell.triSurface::size())
00174 {
00175 const pointField& points = shell.points();
00176
00177 hasSurface = true;
00178
00179 boundBox shellBb(points[0], points[0]);
00180
00181 for (label i = 0; i < points.size(); i++)
00182 {
00183 const point& pt = points[i];
00184 shellBb.min() = min(shellBb.min(), pt);
00185 shellBb.max() = max(shellBb.max(), pt);
00186 }
00187
00188 overallBb.min() = min(overallBb.min(), shellBb.min());
00189 overallBb.max() = max(overallBb.max(), shellBb.max());
00190 }
00191 }
00192 }
00193
00194 if (hasSurface)
00195 {
00196 const point outsidePt = overallBb.max() + overallBb.span();
00197
00198
00199
00200 forAll(shells_, shellI)
00201 {
00202 const searchableSurface& s = allGeometry_[shells_[shellI]];
00203
00204 if (modes_[shellI] != DISTANCE && isA<triSurfaceMesh>(s))
00205 {
00206 triSurfaceMesh& shell = const_cast<triSurfaceMesh&>
00207 (
00208 refCast<const triSurfaceMesh>(s)
00209 );
00210
00211
00212 bool anyFlipped = orientedSurface::orient
00213 (
00214 shell,
00215 outsidePt,
00216 true
00217 );
00218
00219 if (anyFlipped)
00220 {
00221
00222
00223
00224
00225
00226
00227
00228 Info<< "shellSurfaces : Flipped orientation of surface "
00229 << s.name()
00230 << " so point " << outsidePt << " is outside." << endl;
00231 }
00232 }
00233 }
00234 }
00235 }
00236
00237
00238
00239 void Foam::shellSurfaces::findHigherLevel
00240 (
00241 const pointField& pt,
00242 const label shellI,
00243 labelList& maxLevel
00244 ) const
00245 {
00246 const labelList& levels = levels_[shellI];
00247
00248 if (modes_[shellI] == DISTANCE)
00249 {
00250
00251
00252 const scalarField& distances = distances_[shellI];
00253
00254
00255
00256
00257
00258 pointField candidates(pt.size());
00259 labelList candidateMap(pt.size());
00260 scalarField candidateDistSqr(pt.size());
00261 label candidateI = 0;
00262
00263 forAll(maxLevel, pointI)
00264 {
00265 forAllReverse(levels, levelI)
00266 {
00267 if (levels[levelI] > maxLevel[pointI])
00268 {
00269 candidates[candidateI] = pt[pointI];
00270 candidateMap[candidateI] = pointI;
00271 candidateDistSqr[candidateI] = sqr(distances[levelI]);
00272 candidateI++;
00273 break;
00274 }
00275 }
00276 }
00277 candidates.setSize(candidateI);
00278 candidateMap.setSize(candidateI);
00279 candidateDistSqr.setSize(candidateI);
00280
00281
00282 List<pointIndexHit> nearInfo;
00283 allGeometry_[shells_[shellI]].findNearest
00284 (
00285 candidates,
00286 candidateDistSqr,
00287 nearInfo
00288 );
00289
00290
00291 forAll(nearInfo, candidateI)
00292 {
00293 if (nearInfo[candidateI].hit())
00294 {
00295
00296 label minDistI = findLower
00297 (
00298 distances,
00299 mag(nearInfo[candidateI].hitPoint()-candidates[candidateI])
00300 );
00301
00302 label pointI = candidateMap[candidateI];
00303
00304
00305 maxLevel[pointI] = levels[minDistI+1];
00306 }
00307 }
00308 }
00309 else
00310 {
00311
00312
00313
00314
00315
00316 pointField candidates(pt.size());
00317 labelList candidateMap(pt.size());
00318 label candidateI = 0;
00319
00320 forAll(maxLevel, pointI)
00321 {
00322 if (levels[0] > maxLevel[pointI])
00323 {
00324 candidates[candidateI] = pt[pointI];
00325 candidateMap[candidateI] = pointI;
00326 candidateI++;
00327 }
00328 }
00329 candidates.setSize(candidateI);
00330 candidateMap.setSize(candidateI);
00331
00332
00333 List<searchableSurface::volumeType> volType;
00334 allGeometry_[shells_[shellI]].getVolumeType(candidates, volType);
00335
00336 forAll(volType, i)
00337 {
00338 label pointI = candidateMap[i];
00339
00340 if
00341 (
00342 (
00343 modes_[shellI] == INSIDE
00344 && volType[i] == searchableSurface::INSIDE
00345 )
00346 || (
00347 modes_[shellI] == OUTSIDE
00348 && volType[i] == searchableSurface::OUTSIDE
00349 )
00350 )
00351 {
00352 maxLevel[pointI] = levels[0];
00353 }
00354 }
00355 }
00356 }
00357
00358
00359
00360
00361 Foam::shellSurfaces::shellSurfaces
00362 (
00363 const searchableSurfaces& allGeometry,
00364 const PtrList<dictionary>& shellDicts
00365 )
00366 :
00367 allGeometry_(allGeometry)
00368 {
00369 shells_.setSize(shellDicts.size());
00370 modes_.setSize(shellDicts.size());
00371 distances_.setSize(shellDicts.size());
00372 levels_.setSize(shellDicts.size());
00373
00374 forAll(shellDicts, shellI)
00375 {
00376 const dictionary& dict = shellDicts[shellI];
00377 const word name = dict.lookup("name");
00378 const word type = dict.lookup("type");
00379
00380 shells_[shellI] = allGeometry_.findSurfaceID(name);
00381
00382 if (shells_[shellI] == -1)
00383 {
00384 FatalErrorIn
00385 (
00386 "shellSurfaces::shellSurfaces"
00387 "(const searchableSurfaces&, const PtrList<dictionary>&)"
00388 ) << "No surface called " << name << endl
00389 << "Valid surfaces are " << allGeometry_.names()
00390 << exit(FatalError);
00391 }
00392
00393 modes_[shellI] = refineModeNames_.read(dict.lookup("refineMode"));
00394
00395
00396 setAndCheckLevels(shellI, dict.lookup("levels"));
00397 }
00398
00399
00400
00401
00402 orient();
00403 }
00404
00405
00406 Foam::shellSurfaces::shellSurfaces
00407 (
00408 const searchableSurfaces& allGeometry,
00409 const dictionary& shellsDict
00410 )
00411 :
00412 allGeometry_(allGeometry)
00413 {
00414 shells_.setSize(shellsDict.size());
00415 modes_.setSize(shellsDict.size());
00416 distances_.setSize(shellsDict.size());
00417 levels_.setSize(shellsDict.size());
00418
00419 label shellI = 0;
00420 forAllConstIter(dictionary, shellsDict, iter)
00421 {
00422 shells_[shellI] = allGeometry_.findSurfaceID(iter().keyword());
00423
00424 if (shells_[shellI] == -1)
00425 {
00426 FatalErrorIn
00427 (
00428 "shellSurfaces::shellSurfaces"
00429 "(const searchableSurfaces&, const dictionary>&"
00430 ) << "No surface called " << iter().keyword() << endl
00431 << "Valid surfaces are " << allGeometry_.names()
00432 << exit(FatalError);
00433 }
00434 const dictionary& dict = shellsDict.subDict(iter().keyword());
00435
00436 modes_[shellI] = refineModeNames_.read(dict.lookup("mode"));
00437
00438
00439 setAndCheckLevels(shellI, dict.lookup("levels"));
00440
00441 shellI++;
00442 }
00443
00444
00445
00446
00447 orient();
00448 }
00449
00450
00451
00452
00453
00454 Foam::label Foam::shellSurfaces::maxLevel() const
00455 {
00456 label overallMax = 0;
00457 forAll(levels_, shellI)
00458 {
00459 overallMax = max(overallMax, max(levels_[shellI]));
00460 }
00461 return overallMax;
00462 }
00463
00464
00465 void Foam::shellSurfaces::findHigherLevel
00466 (
00467 const pointField& pt,
00468 const labelList& ptLevel,
00469 labelList& maxLevel
00470 ) const
00471 {
00472
00473 maxLevel = ptLevel;
00474
00475 forAll(shells_, shellI)
00476 {
00477 findHigherLevel(pt, shellI, maxLevel);
00478 }
00479 }
00480
00481
00482