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
00028
00029 #include "orientedSurface.H"
00030 #include "triSurfaceTools.H"
00031 #include "treeBoundBox.H"
00032
00033
00034
00035 defineTypeNameAndDebug(Foam::orientedSurface, 0);
00036
00037
00038
00039
00040
00041 bool Foam::orientedSurface::edgeOrder
00042 (
00043 const labelledTri& f,
00044 const edge& e
00045 )
00046 {
00047 if
00048 (
00049 (f[0] == e[0] && f[1] == e[1])
00050 || (f[1] == e[0] && f[2] == e[1])
00051 || (f[2] == e[0] && f[0] == e[1])
00052 )
00053 {
00054 return true;
00055 }
00056 else
00057 {
00058 return false;
00059 }
00060 }
00061
00062
00063
00064 bool Foam::orientedSurface::consistentEdge
00065 (
00066 const edge& e,
00067 const labelledTri& f0,
00068 const labelledTri& f1
00069 )
00070 {
00071 return edgeOrder(f0, e) ^ edgeOrder(f1, e);
00072 }
00073
00074
00075 Foam::labelList Foam::orientedSurface::faceToEdge
00076 (
00077 const triSurface& s,
00078 const labelList& changedFaces
00079 )
00080 {
00081 labelList changedEdges(3*changedFaces.size());
00082 label changedI = 0;
00083
00084 forAll(changedFaces, i)
00085 {
00086 const labelList& fEdges = s.faceEdges()[changedFaces[i]];
00087
00088 forAll(fEdges, j)
00089 {
00090 changedEdges[changedI++] = fEdges[j];
00091 }
00092 }
00093 changedEdges.setSize(changedI);
00094
00095 return changedEdges;
00096 }
00097
00098
00099 Foam::labelList Foam::orientedSurface::edgeToFace
00100 (
00101 const triSurface& s,
00102 const labelList& changedEdges,
00103 labelList& flip
00104 )
00105 {
00106 labelList changedFaces(2*changedEdges.size());
00107 label changedI = 0;
00108
00109 forAll(changedEdges, i)
00110 {
00111 label edgeI = changedEdges[i];
00112
00113 const labelList& eFaces = s.edgeFaces()[edgeI];
00114
00115 if (eFaces.size() < 2)
00116 {
00117
00118 }
00119 else if (eFaces.size() == 2)
00120 {
00121 label face0 = eFaces[0];
00122 label face1 = eFaces[1];
00123
00124 const labelledTri& f0 = s.localFaces()[face0];
00125 const labelledTri& f1 = s.localFaces()[face1];
00126
00127 if (flip[face0] == UNVISITED)
00128 {
00129 if (flip[face1] == UNVISITED)
00130 {
00131 FatalErrorIn("orientedSurface::edgeToFace") << "Problem"
00132 << abort(FatalError);
00133 }
00134 else
00135 {
00136
00137 if (consistentEdge(s.edges()[edgeI], f0, f1))
00138 {
00139
00140 flip[face0] = (flip[face1] == FLIP ? FLIP : NOFLIP);
00141 }
00142 else
00143 {
00144
00145 flip[face0] = (flip[face1] == FLIP ? NOFLIP : FLIP);
00146 }
00147 changedFaces[changedI++] = face0;
00148 }
00149 }
00150 else
00151 {
00152 if (flip[face1] == UNVISITED)
00153 {
00154
00155 if (consistentEdge(s.edges()[edgeI], f0, f1))
00156 {
00157 flip[face1] = (flip[face0] == FLIP ? FLIP : NOFLIP);
00158 }
00159 else
00160 {
00161 flip[face1] = (flip[face0] == FLIP ? NOFLIP : FLIP);
00162 }
00163 changedFaces[changedI++] = face1;
00164 }
00165 }
00166 }
00167 else
00168 {
00169
00170 }
00171 }
00172 changedFaces.setSize(changedI);
00173
00174 return changedFaces;
00175 }
00176
00177
00178 void Foam::orientedSurface::walkSurface
00179 (
00180 const triSurface& s,
00181 const label startFaceI,
00182 labelList& flipState
00183 )
00184 {
00185
00186 labelList changedFaces(1, startFaceI);
00187
00188 labelList changedEdges;
00189
00190 while(true)
00191 {
00192 changedEdges = faceToEdge(s, changedFaces);
00193
00194 if (debug)
00195 {
00196 Pout<< "From changedFaces:" << changedFaces.size()
00197 << " to changedEdges:" << changedEdges.size()
00198 << endl;
00199 }
00200
00201 if (changedEdges.empty())
00202 {
00203 break;
00204 }
00205
00206 changedFaces = edgeToFace(s, changedEdges, flipState);
00207
00208 if (debug)
00209 {
00210 Pout<< "From changedEdges:" << changedEdges.size()
00211 << " to changedFaces:" << changedFaces.size()
00212 << endl;
00213 }
00214
00215 if (changedFaces.empty())
00216 {
00217 break;
00218 }
00219 }
00220 }
00221
00222
00223 void Foam::orientedSurface::propagateOrientation
00224 (
00225 const triSurface& s,
00226 const point& samplePoint,
00227 const bool orientOutside,
00228 const label nearestFaceI,
00229 const point& nearestPt,
00230 labelList& flipState
00231 )
00232 {
00233
00234
00235
00236 triSurfaceTools::sideType side = triSurfaceTools::surfaceSide
00237 (
00238 s,
00239 samplePoint,
00240 nearestFaceI,
00241 nearestPt,
00242 10*SMALL
00243 );
00244
00245 if (side == triSurfaceTools::UNKNOWN)
00246 {
00247
00248
00249 flipState[nearestFaceI] = NOFLIP;
00250 }
00251 else if ((side == triSurfaceTools::OUTSIDE) == orientOutside)
00252 {
00253
00254
00255 flipState[nearestFaceI] = NOFLIP;
00256 }
00257 else
00258 {
00259
00260 flipState[nearestFaceI] = FLIP;
00261 }
00262
00263 if (debug)
00264 {
00265 vector n = triSurfaceTools::surfaceNormal(s, nearestFaceI, nearestPt);
00266
00267 Pout<< "orientedSurface::propagateOrientation : starting face"
00268 << " orientation:" << nl
00269 << " for samplePoint:" << samplePoint << nl
00270 << " starting from point:" << nearestPt << nl
00271 << " on face:" << nearestFaceI << nl
00272 << " with normal:" << n << nl
00273 << " decided side:" << label(side)
00274 << endl;
00275 }
00276
00277
00278 walkSurface(s, nearestFaceI, flipState);
00279 }
00280
00281
00282 bool Foam::orientedSurface::flipSurface
00283 (
00284 triSurface& s,
00285 const labelList& flipState
00286 )
00287 {
00288 bool hasFlipped = false;
00289
00290
00291 forAll(flipState, faceI)
00292 {
00293 if (flipState[faceI] == UNVISITED)
00294 {
00295 FatalErrorIn
00296 (
00297 "orientSurface(const point&, const label, const point&)"
00298 ) << "unvisited face " << faceI
00299 << abort(FatalError);
00300 }
00301 else if (flipState[faceI] == FLIP)
00302 {
00303 labelledTri& tri = s[faceI];
00304
00305 label tmp = tri[0];
00306
00307 tri[0] = tri[1];
00308 tri[1] = tmp;
00309
00310 hasFlipped = true;
00311 }
00312 }
00313
00314 s.clearOut();
00315
00316 return hasFlipped;
00317 }
00318
00319
00320
00321
00322
00323 Foam::orientedSurface::orientedSurface()
00324 :
00325 triSurface()
00326 {}
00327
00328
00329
00330 Foam::orientedSurface::orientedSurface
00331 (
00332 const triSurface& surf,
00333 const point& samplePoint,
00334 const bool orientOutside
00335 )
00336 :
00337 triSurface(surf)
00338 {
00339 orient(*this, samplePoint, orientOutside);
00340 }
00341
00342
00343
00344 Foam::orientedSurface::orientedSurface
00345 (
00346 const triSurface& surf,
00347 const bool orientOutside
00348 )
00349 :
00350 triSurface(surf)
00351 {
00352
00353 treeBoundBox bb(surf.points(), surf.meshPoints());
00354
00355 point outsidePoint = bb.max() + bb.span();
00356
00357 orient(*this, outsidePoint, orientOutside);
00358 }
00359
00360
00361
00362
00363 bool Foam::orientedSurface::orient
00364 (
00365 triSurface& s,
00366 const point& samplePoint,
00367 const bool orientOutside
00368 )
00369 {
00370 bool anyFlipped = false;
00371
00372
00373
00374
00375 if (s.size() > 0)
00376 {
00377
00378
00379
00380
00381 labelList flipState(s.size(), UNVISITED);
00382
00383 flipState[0] = NOFLIP;
00384 walkSurface(s, 0, flipState);
00385
00386 anyFlipped = flipSurface(s, flipState);
00387 }
00388
00389
00390
00391
00392
00393
00394 labelList flipState(s.size(), UNVISITED);
00395
00396
00397 while (true)
00398 {
00399
00400
00401 scalar minDist = GREAT;
00402 point minPoint;
00403 label minFaceI = -1;
00404
00405 forAll(s, faceI)
00406 {
00407 if (flipState[faceI] == UNVISITED)
00408 {
00409 const labelledTri& f = s[faceI];
00410
00411 pointHit curHit =
00412 triPointRef
00413 (
00414 s.points()[f[0]],
00415 s.points()[f[1]],
00416 s.points()[f[2]]
00417 ).nearestPoint(samplePoint);
00418
00419 if (curHit.distance() < minDist)
00420 {
00421 minDist = curHit.distance();
00422 minPoint = curHit.rawPoint();
00423 minFaceI = faceI;
00424 }
00425 }
00426 }
00427
00428
00429 if (minFaceI == -1)
00430 {
00431 break;
00432 }
00433
00434
00435
00436 propagateOrientation
00437 (
00438 s,
00439 samplePoint,
00440 orientOutside,
00441 minFaceI,
00442 minPoint,
00443 flipState
00444 );
00445 }
00446
00447
00448 bool geomFlipped = flipSurface(s, flipState);
00449
00450 return anyFlipped || geomFlipped;
00451 }
00452
00453
00454