cuttingPlane.C
Go to the documentation of this file.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 "cuttingPlane.H"
00028 #include "primitiveMesh.H"
00029 #include "linePointRef.H"
00030 #include "meshTools.H"
00031
00032
00033
00034
00035
00037 const Foam::scalar zeroish = Foam::SMALL;
00038 const Foam::scalar positive = Foam::SMALL * 1E3;
00040
00041
00042
00043
00044 void Foam::cuttingPlane::calcCutCells
00045 (
00046 const primitiveMesh& mesh,
00047 const scalarField& dotProducts,
00048 const UList<label>& cellIdLabels
00049 )
00050 {
00051 const labelListList& cellEdges = mesh.cellEdges();
00052 const edgeList& edges = mesh.edges();
00053
00054 label listSize = cellEdges.size();
00055 if (&cellIdLabels)
00056 {
00057 listSize = cellIdLabels.size();
00058 }
00059
00060 cutCells_.setSize(listSize);
00061 label cutcellI(0);
00062
00063
00064
00065 for (label listI = 0; listI < listSize; ++listI)
00066 {
00067 label cellI = listI;
00068 if (&cellIdLabels)
00069 {
00070 cellI = cellIdLabels[listI];
00071 }
00072
00073 const labelList& cEdges = cellEdges[cellI];
00074
00075 label nCutEdges = 0;
00076
00077 forAll(cEdges, i)
00078 {
00079 const edge& e = edges[cEdges[i]];
00080
00081 if
00082 (
00083 (dotProducts[e[0]] < zeroish && dotProducts[e[1]] > positive)
00084 || (dotProducts[e[1]] < zeroish && dotProducts[e[0]] > positive)
00085 )
00086 {
00087 nCutEdges++;
00088
00089 if (nCutEdges > 2)
00090 {
00091 cutCells_[cutcellI++] = cellI;
00092
00093 break;
00094 }
00095 }
00096 }
00097 }
00098
00099
00100 cutCells_.setSize(cutcellI);
00101 }
00102
00103
00104
00105
00106
00107 void Foam::cuttingPlane::intersectEdges
00108 (
00109 const primitiveMesh& mesh,
00110 const scalarField& dotProducts,
00111 List<label>& edgePoint
00112 )
00113 {
00114
00115 const edgeList& edges = mesh.edges();
00116 const pointField& points = mesh.points();
00117
00118
00119 edgePoint.setSize(edges.size());
00120
00121 DynamicList<point> dynCuttingPoints(4*cutCells_.size());
00122
00123 forAll(edges, edgeI)
00124 {
00125 const edge& e = edges[edgeI];
00126
00127 if
00128 (
00129 (dotProducts[e[0]] < zeroish && dotProducts[e[1]] > positive)
00130 || (dotProducts[e[1]] < zeroish && dotProducts[e[0]] > positive)
00131 )
00132 {
00133
00134 edgePoint[edgeI] = dynCuttingPoints.size();
00135
00136 const point& p0 = points[e[0]];
00137 const point& p1 = points[e[1]];
00138
00139 scalar alpha = lineIntersect(linePointRef(p0, p1));
00140
00141 if (alpha < zeroish)
00142 {
00143 dynCuttingPoints.append(p0);
00144 }
00145 else if (alpha >= 1.0)
00146 {
00147 dynCuttingPoints.append(p1);
00148 }
00149 else
00150 {
00151 dynCuttingPoints.append((1-alpha)*p0 + alpha*p1);
00152 }
00153 }
00154 else
00155 {
00156 edgePoint[edgeI] = -1;
00157 }
00158 }
00159
00160 this->storedPoints().transfer(dynCuttingPoints);
00161 }
00162
00163
00164
00165
00166 bool Foam::cuttingPlane::walkCell
00167 (
00168 const primitiveMesh& mesh,
00169 const UList<label>& edgePoint,
00170 const label cellI,
00171 const label startEdgeI,
00172 DynamicList<label>& faceVerts
00173 )
00174 {
00175 label faceI = -1;
00176 label edgeI = startEdgeI;
00177
00178 label nIter = 0;
00179
00180 faceVerts.clear();
00181 do
00182 {
00183 faceVerts.append(edgePoint[edgeI]);
00184
00185
00186 faceI = meshTools::otherFace(mesh, cellI, faceI, edgeI);
00187
00188
00189 const labelList& fEdges = mesh.faceEdges()[faceI];
00190
00191 label nextEdgeI = -1;
00192
00193
00194
00195
00196
00197
00198 forAll(fEdges, i)
00199 {
00200 label edge2I = fEdges[i];
00201
00202 if (edge2I != edgeI && edgePoint[edge2I] != -1)
00203 {
00204 nextEdgeI = edge2I;
00205 break;
00206 }
00207 }
00208
00209 if (nextEdgeI == -1)
00210 {
00211
00212 WarningIn("Foam::cuttingPlane::walkCell")
00213 << "Did not find closed walk along surface of cell " << cellI
00214 << " starting from edge " << startEdgeI
00215 << " in " << nIter << " iterations." << nl
00216 << "Collected cutPoints so far:" << faceVerts
00217 << endl;
00218
00219 return false;
00220 }
00221
00222 edgeI = nextEdgeI;
00223
00224 nIter++;
00225
00226 if (nIter > 1000)
00227 {
00228 WarningIn("Foam::cuttingPlane::walkCell")
00229 << "Did not find closed walk along surface of cell " << cellI
00230 << " starting from edge " << startEdgeI
00231 << " in " << nIter << " iterations." << nl
00232 << "Collected cutPoints so far:" << faceVerts
00233 << endl;
00234 return false;
00235 }
00236
00237 } while (edgeI != startEdgeI);
00238
00239
00240 if (faceVerts.size() >= 3)
00241 {
00242 return true;
00243 }
00244 else
00245 {
00246 WarningIn("Foam::cuttingPlane::walkCell")
00247 << "Did not find closed walk along surface of cell " << cellI
00248 << " starting from edge " << startEdgeI << nl
00249 << "Collected cutPoints so far:" << faceVerts
00250 << endl;
00251
00252 return false;
00253 }
00254 }
00255
00256
00257
00258 void Foam::cuttingPlane::walkCellCuts
00259 (
00260 const primitiveMesh& mesh,
00261 const UList<label>& edgePoint
00262 )
00263 {
00264 const pointField& cutPoints = this->points();
00265
00266
00267 DynamicList<face> dynCutFaces(cutCells_.size());
00268 DynamicList<label> dynCutCells(cutCells_.size());
00269
00270
00271 DynamicList<label> faceVerts(10);
00272
00273 forAll(cutCells_, i)
00274 {
00275 label cellI = cutCells_[i];
00276
00277
00278 const labelList& cEdges = mesh.cellEdges()[cellI];
00279
00280 label startEdgeI = -1;
00281
00282 forAll(cEdges, cEdgeI)
00283 {
00284 label edgeI = cEdges[cEdgeI];
00285
00286 if (edgePoint[edgeI] != -1)
00287 {
00288 startEdgeI = edgeI;
00289 break;
00290 }
00291 }
00292
00293
00294 if (startEdgeI == -1)
00295 {
00296 FatalErrorIn("Foam::cuttingPlane::walkCellCuts")
00297 << "Cannot find cut edge for cut cell " << cellI
00298 << abort(FatalError);
00299 }
00300
00301
00302 bool okCut = walkCell
00303 (
00304 mesh,
00305 edgePoint,
00306 cellI,
00307 startEdgeI,
00308 faceVerts
00309 );
00310
00311 if (okCut)
00312 {
00313 face f(faceVerts);
00314
00315
00316 if ((f.normal(cutPoints) && normal()) < 0)
00317 {
00318 f = f.reverseFace();
00319 }
00320
00321
00322 label nTri = f.triangles(cutPoints, dynCutFaces);
00323 while (nTri--)
00324 {
00325 dynCutCells.append(cellI);
00326 }
00327 }
00328 }
00329
00330 this->storedFaces().transfer(dynCutFaces);
00331 cutCells_.transfer(dynCutCells);
00332 }
00333
00334
00335
00336
00337
00338 Foam::cuttingPlane::cuttingPlane(const plane& pln)
00339 :
00340 plane(pln)
00341 {}
00342
00343
00344
00345 Foam::cuttingPlane::cuttingPlane
00346 (
00347 const plane& pln,
00348 const primitiveMesh& mesh,
00349 const UList<label>& cellIdLabels
00350 )
00351 :
00352 plane(pln)
00353 {
00354 reCut(mesh, cellIdLabels);
00355 }
00356
00357
00358
00359
00360
00361
00362 void Foam::cuttingPlane::reCut
00363 (
00364 const primitiveMesh& mesh,
00365 const UList<label>& cellIdLabels
00366 )
00367 {
00368 MeshStorage::clear();
00369 cutCells_.clear();
00370
00371 scalarField dotProducts = (mesh.points() - refPoint()) & normal();
00372
00373
00374 calcCutCells(mesh, dotProducts, cellIdLabels);
00375
00376
00377
00378 labelList edgePoint;
00379 intersectEdges(mesh, dotProducts, edgePoint);
00380
00381
00382 walkCellCuts(mesh, edgePoint);
00383 }
00384
00385
00386
00387 void Foam::cuttingPlane::remapFaces
00388 (
00389 const UList<label>& faceMap
00390 )
00391 {
00392
00393 if (&faceMap && faceMap.size())
00394 {
00395 MeshStorage::remapFaces(faceMap);
00396
00397 List<label> newCutCells(faceMap.size());
00398 forAll(faceMap, faceI)
00399 {
00400 newCutCells[faceI] = cutCells_[faceMap[faceI]];
00401 }
00402 cutCells_.transfer(newCutCells);
00403 }
00404 }
00405
00406
00407
00408 void Foam::cuttingPlane::operator=(const cuttingPlane& rhs)
00409 {
00410
00411 if (this == &rhs)
00412 {
00413 FatalErrorIn ("Foam::cuttingPlane::operator=(const cuttingPlane&)")
00414 << "Attempted assignment to self"
00415 << abort(FatalError);
00416 }
00417
00418 static_cast<MeshStorage&>(*this) = rhs;
00419 static_cast<plane&>(*this) = rhs;
00420 cutCells_ = rhs.cutCells();
00421 }
00422
00423
00424
Copyright © 2000-2009 OpenCFD Ltd