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 "geomCellLooper.H"
00028 #include "polyMesh.H"
00029 #include "DynamicList.H"
00030 #include "plane.H"
00031 #include "meshTools.H"
00032 #include "SortableList.H"
00033 #include "triSurfaceTools.H"
00034 #include "HashSet.H"
00035 #include "ListOps.H"
00036 #include "transform.H"
00037
00038 #include "addToRunTimeSelectionTable.H"
00039
00040
00041
00042
00043 const Foam::scalar Foam::geomCellLooper::pointEqualTol_ = 1E-3;
00044
00045
00046
00047 Foam::scalar Foam::geomCellLooper::snapTol_ = 0.1;
00048
00049
00050
00051
00052 namespace Foam
00053 {
00054
00055 defineTypeNameAndDebug(geomCellLooper, 0);
00056 addToRunTimeSelectionTable(cellLooper, geomCellLooper, word);
00057
00058 }
00059
00060
00061
00062
00063 Foam::scalar Foam::geomCellLooper::minEdgeLen(const label vertI) const
00064 {
00065 scalar minLen = GREAT;
00066
00067 const labelList& pEdges = mesh().pointEdges()[vertI];
00068
00069 forAll(pEdges, pEdgeI)
00070 {
00071 const edge& e = mesh().edges()[pEdges[pEdgeI]];
00072
00073 minLen = min(minLen, e.mag(mesh().points()));
00074 }
00075 return minLen;
00076 }
00077
00078
00079
00080
00081 bool Foam::geomCellLooper::cutEdge
00082 (
00083 const plane& cutPlane,
00084 const label edgeI,
00085 scalar& weight
00086 ) const
00087 {
00088 const pointField& pts = mesh().points();
00089
00090 const edge& e = mesh().edges()[edgeI];
00091
00092 scalar s = cutPlane.normalIntersect(pts[e.start()], e.vec(pts));
00093
00094 if ((s > -pointEqualTol_) && (s < 1 + pointEqualTol_))
00095 {
00096 weight = s;
00097
00098 return true;
00099 }
00100 else
00101 {
00102
00103 weight = -GREAT;
00104
00105 return false;
00106 }
00107 }
00108
00109
00110
00111 Foam::label Foam::geomCellLooper::snapToVert
00112 (
00113 const scalar tol,
00114 const label edgeI,
00115 const scalar weight
00116 ) const
00117 {
00118 const edge& e = mesh().edges()[edgeI];
00119
00120 if (weight < tol)
00121 {
00122 return e.start();
00123 }
00124 else if (weight > (1-tol))
00125 {
00126 return e.end();
00127 }
00128 else
00129 {
00130 return -1;
00131 }
00132 }
00133
00134
00135 void Foam::geomCellLooper::getBase(const vector& n, vector& e0, vector& e1)
00136 const
00137 {
00138
00139 vector base(1,0,0);
00140
00141 scalar nComp = n & base;
00142
00143 if (mag(nComp) > 0.8)
00144 {
00145
00146
00147 base.x() = 0;
00148 base.y() = 1;
00149
00150 nComp = n & base;
00151
00152 if (mag(nComp) > 0.8)
00153 {
00154 base.y() = 0;
00155 base.z() = 1;
00156
00157 nComp = n & base;
00158 }
00159 }
00160
00161
00162
00163 e0 = base - nComp*n;
00164
00165 e0 /= mag(e0) + VSMALL;
00166
00167 e1 = n ^ e0;
00168
00169
00170
00171
00172
00173
00174 }
00175
00176
00177
00178
00179 bool Foam::geomCellLooper::edgeEndsCut
00180 (
00181 const labelList& loop,
00182 const label index
00183 ) const
00184 {
00185 label edgeI = getEdge(loop[index]);
00186
00187 const edge& e = mesh().edges()[edgeI];
00188
00189 const label prevCut = loop[loop.rcIndex(index)];
00190 const label nextCut = loop[loop.fcIndex(index)];
00191
00192 if (!isEdge(prevCut) && !isEdge(nextCut))
00193 {
00194
00195
00196 label v0 = getVertex(prevCut);
00197 label v1 = getVertex(nextCut);
00198
00199 if
00200 (
00201 (v0 == e[0] && v1 == e[1])
00202 || (v0 == e[1] && v1 == e[0])
00203 )
00204 {
00205 return true;
00206 }
00207 }
00208 return false;
00209 }
00210
00211
00212
00213
00214
00215 Foam::geomCellLooper::geomCellLooper(const polyMesh& mesh)
00216 :
00217 cellLooper(mesh)
00218 {}
00219
00220
00221
00222
00223 Foam::geomCellLooper::~geomCellLooper()
00224 {}
00225
00226
00227
00228
00229 bool Foam::geomCellLooper::cut
00230 (
00231 const vector& refDir,
00232 const label cellI,
00233 const boolList& vertIsCut,
00234 const boolList& edgeIsCut,
00235 const scalarField& edgeWeight,
00236
00237 labelList& loop,
00238 scalarField& loopWeights
00239 ) const
00240 {
00241
00242 return cut
00243 (
00244 plane(mesh().cellCentres()[cellI], refDir),
00245 cellI,
00246 vertIsCut,
00247 edgeIsCut,
00248 edgeWeight,
00249 loop,
00250 loopWeights
00251 );
00252 }
00253
00254
00255 bool Foam::geomCellLooper::cut
00256 (
00257 const plane& cutPlane,
00258 const label cellI,
00259 const boolList&,
00260 const boolList&,
00261 const scalarField&,
00262
00263 labelList& loop,
00264 scalarField& loopWeights
00265 ) const
00266 {
00267 const pointField& points = mesh().points();
00268 const edgeList& edges = mesh().edges();
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 label nEstCuts = 2*mesh().cells()[cellI].size();
00282
00283 DynamicList<label> localLoop(nEstCuts);
00284 DynamicList<scalar> localLoopWeights(nEstCuts);
00285
00286
00287
00288 labelHashSet checkedPoints(nEstCuts);
00289
00290 const labelList& cellEdges = mesh().cellEdges()[cellI];
00291
00292 forAll(cellEdges, i)
00293 {
00294 label edgeI = cellEdges[i];
00295
00296 const edge& e = edges[edgeI];
00297
00298 bool useStart = false;
00299
00300 bool useEnd = false;
00301
00302
00303
00304
00305
00306 if (!checkedPoints.found(e.start()))
00307 {
00308 checkedPoints.insert(e.start());
00309
00310 scalar typStartLen = pointEqualTol_ * minEdgeLen(e.start());
00311
00312
00313 if (cutPlane.distance(points[e.start()]) < typStartLen)
00314 {
00315
00316 localLoop.append(vertToEVert(e.start()));
00317 localLoopWeights.append(-GREAT);
00318
00319 useStart = true;
00320 }
00321 }
00322 if (!checkedPoints.found(e.end()))
00323 {
00324 checkedPoints.insert(e.end());
00325
00326 scalar typEndLen = pointEqualTol_ * minEdgeLen(e.end());
00327
00328
00329 if (cutPlane.distance(points[e.end()]) < typEndLen)
00330 {
00331
00332 localLoop.append(vertToEVert(e.end()));
00333 localLoopWeights.append(-GREAT);
00334
00335 useEnd = true;
00336 }
00337 }
00338
00339
00340
00341
00342
00343 if (!useEnd && !useStart)
00344 {
00345
00346
00347 scalar cutWeight;
00348
00349 if (cutEdge(cutPlane, edgeI, cutWeight))
00350 {
00351
00352 label cutVertI = snapToVert(snapTol_, edgeI, cutWeight);
00353
00354 if (cutVertI == -1)
00355 {
00356
00357 localLoop.append(edgeToEVert(edgeI));
00358 localLoopWeights.append(cutWeight);
00359 }
00360 else
00361 {
00362
00363
00364
00365 label cut = vertToEVert(cutVertI);
00366
00367 if (findIndex(localLoop, cut) == -1)
00368 {
00369 localLoop.append(vertToEVert(cutVertI));
00370 localLoopWeights.append(-GREAT);
00371 }
00372 }
00373 }
00374 }
00375 }
00376
00377 if (localLoop.size() <= 2)
00378 {
00379 return false;
00380 }
00381
00382 localLoop.shrink();
00383 localLoopWeights.shrink();
00384
00385
00386
00387 pointField loopPoints(localLoop.size());
00388 point ctr(vector::zero);
00389
00390 forAll(localLoop, i)
00391 {
00392 loopPoints[i] = coord(localLoop[i], localLoopWeights[i]);
00393 ctr += loopPoints[i];
00394 }
00395 ctr /= localLoop.size();
00396
00397
00398
00399 vector e0, e1;
00400 getBase(cutPlane.normal(), e0, e1);
00401
00402
00403
00404 SortableList<scalar> sortedAngles(localLoop.size());
00405
00406 forAll(sortedAngles, i)
00407 {
00408 vector toCtr(loopPoints[i] - ctr);
00409 toCtr /= mag(toCtr);
00410
00411 sortedAngles[i] = pseudoAngle(e0, e1, toCtr);
00412 }
00413 sortedAngles.sort();
00414
00415 loop.setSize(localLoop.size());
00416 loopWeights.setSize(loop.size());
00417
00418
00419
00420
00421 const labelList& indices = sortedAngles.indices();
00422
00423 forAll(indices, i)
00424 {
00425 loop[i] = localLoop[indices[i]];
00426 loopWeights[i] = localLoopWeights[indices[i]];
00427 }
00428
00429
00430
00431 bool filterLoop = false;
00432
00433 forAll(loop, i)
00434 {
00435 label cut = loop[i];
00436
00437 if (isEdge(cut) && edgeEndsCut(loop, i))
00438 {
00439 filterLoop = true;
00440 }
00441 }
00442
00443 if (filterLoop)
00444 {
00445
00446
00447
00448 labelList filteredLoop(loop.size());
00449 scalarField filteredLoopWeights(loopWeights.size());
00450 label filterI = 0;
00451
00452 forAll(loop, i)
00453 {
00454 label cut = loop[i];
00455
00456 if (isEdge(cut) && edgeEndsCut(loop, i))
00457 {
00458
00459 }
00460 else
00461 {
00462 filteredLoop[filterI] = loop[i];
00463 filteredLoopWeights[filterI] = loopWeights[i];
00464 filterI++;
00465 }
00466 }
00467 filteredLoop.setSize(filterI);
00468 filteredLoopWeights.setSize(filterI);
00469
00470 loop.transfer(filteredLoop);
00471 loopWeights.transfer(filteredLoopWeights);
00472 }
00473
00474 if (debug&2)
00475 {
00476 Pout<< "cell:" << cellI << endl;
00477 forAll(loop, i)
00478 {
00479 Pout<< "At angle:" << sortedAngles[i] << endl
00480 << " cut:";
00481
00482 writeCut(Pout, loop[i], loopWeights[i]);
00483
00484 Pout<< " coord:" << coord(loop[i], loopWeights[i]);
00485
00486 Pout<< endl;
00487 }
00488 }
00489
00490 return true;
00491 }
00492
00493
00494