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 "faceCollapser.H"
00030 #include "polyMesh.H"
00031 #include "polyTopoChange.H"
00032 #include "polyModifyPoint.H"
00033 #include "polyModifyFace.H"
00034 #include "polyRemoveFace.H"
00035 #include "SortableList.H"
00036 #include "meshTools.H"
00037 #include "OFstream.H"
00038
00039
00040
00041
00042
00043 void Foam::faceCollapser::insert
00044 (
00045 const labelList& elems,
00046 const label excludeElem,
00047 labelHashSet& set
00048 )
00049 {
00050 forAll(elems, i)
00051 {
00052 if (elems[i] != excludeElem)
00053 {
00054 set.insert(elems[i]);
00055 }
00056 }
00057 }
00058
00059
00060
00061 Foam::label Foam::faceCollapser::findEdge
00062 (
00063 const edgeList& edges,
00064 const labelList& edgeLabels,
00065 const label v0,
00066 const label v1
00067 )
00068 {
00069 forAll(edgeLabels, i)
00070 {
00071 label edgeI = edgeLabels[i];
00072
00073 const edge& e = edges[edgeI];
00074
00075 if
00076 (
00077 (e[0] == v0 && e[1] == v1)
00078 || (e[0] == v1 && e[1] == v0)
00079 )
00080 {
00081 return edgeI;
00082 }
00083 }
00084
00085 FatalErrorIn("findEdge") << "Cannot find edge between vertices " << v0
00086 << " and " << v1 << " in edge labels " << edgeLabels
00087 << abort(FatalError);
00088
00089 return -1;
00090 }
00091
00092
00093
00094
00095
00096 void Foam::faceCollapser::filterFace
00097 (
00098 const Map<labelList>& splitEdges,
00099 const label faceI,
00100 polyTopoChange& meshMod
00101 ) const
00102 {
00103 const face& f = mesh_.faces()[faceI];
00104 const labelList& fEdges = mesh_.faceEdges()[faceI];
00105
00106
00107 DynamicList<label> newFace(10 * f.size());
00108
00109 forAll(f, fp)
00110 {
00111 label v0 = f[fp];
00112
00113 newFace.append(v0);
00114
00115
00116 label fp1 = f.fcIndex(fp);
00117
00118 label v1 = f[fp1];
00119
00120
00121 label edgeI = findEdge(mesh_.edges(), fEdges, v0, v1);
00122
00123 Map<labelList>::const_iterator edgeFnd =
00124 splitEdges.find(edgeI);
00125
00126 if (edgeFnd != splitEdges.end())
00127 {
00128
00129
00130
00131
00132 const labelList& extraVerts = edgeFnd();
00133
00134 if (v0 == mesh_.edges()[edgeI].start())
00135 {
00136 forAll(extraVerts, i)
00137 {
00138 newFace.append(extraVerts[i]);
00139 }
00140 }
00141 else
00142 {
00143 forAllReverse(extraVerts, i)
00144 {
00145 newFace.append(extraVerts[i]);
00146 }
00147 }
00148 }
00149 }
00150 face newF(newFace.shrink());
00151
00152
00153
00154
00155 if (newF != f)
00156 {
00157 label nei = -1;
00158
00159 label patchI = -1;
00160
00161 if (mesh_.isInternalFace(faceI))
00162 {
00163 nei = mesh_.faceNeighbour()[faceI];
00164 }
00165 else
00166 {
00167 patchI = mesh_.boundaryMesh().whichPatch(faceI);
00168 }
00169
00170
00171 label zoneID = mesh_.faceZones().whichZone(faceI);
00172
00173 bool zoneFlip = false;
00174
00175 if (zoneID >= 0)
00176 {
00177 const faceZone& fZone = mesh_.faceZones()[zoneID];
00178
00179 zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00180 }
00181
00182 meshMod.setAction
00183 (
00184 polyModifyFace
00185 (
00186 newF,
00187 faceI,
00188 mesh_.faceOwner()[faceI],
00189 nei,
00190 false,
00191 patchI,
00192 false,
00193 zoneID,
00194 zoneFlip
00195 )
00196 );
00197 }
00198 }
00199
00200
00201
00202
00203
00204 Foam::faceCollapser::faceCollapser(const polyMesh& mesh)
00205 :
00206 mesh_(mesh)
00207 {}
00208
00209
00210
00211
00212 void Foam::faceCollapser::setRefinement
00213 (
00214 const labelList& faceLabels,
00215 const labelList& fpStart,
00216 const labelList& fpEnd,
00217 polyTopoChange& meshMod
00218 ) const
00219 {
00220 const pointField& points = mesh_.points();
00221 const edgeList& edges = mesh_.edges();
00222 const faceList& faces = mesh_.faces();
00223 const labelListList& edgeFaces = mesh_.edgeFaces();
00224
00225
00226
00227
00228 Map<labelList> splitEdges(faceLabels.size());
00229
00230
00231
00232 labelHashSet affectedFaces(4*faceLabels.size());
00233
00234
00235
00236
00237
00238
00239 forAll(faceLabels, i)
00240 {
00241 const label faceI = faceLabels[i];
00242
00243 const face& f = faces[faceI];
00244
00245 const label fpA = fpStart[i];
00246 const label fpB = fpEnd[i];
00247
00248 const point& pA = points[f[fpA]];
00249 const point& pB = points[f[fpB]];
00250
00251 Pout<< "Face:" << f << " collapsed to fp:" << fpA << ' ' << fpB
00252 << " with points:" << pA << ' ' << pB
00253 << endl;
00254
00255
00256 linePointRef lineAB(pA, pB);
00257
00258
00259
00260
00261 SortableList<scalar> dist(f.size());
00262
00263 dist[fpA] = 0;
00264 dist[fpB] = magSqr(pB - pA);
00265
00266
00267
00268
00269
00270 label fpMin1 = fpA;
00271 label fp = f.fcIndex(fpMin1);
00272
00273 while (fp != fpB)
00274 {
00275
00276 pointHit near = lineAB.nearestDist(points[f[fp]]);
00277
00278 scalar w = magSqr(near.rawPoint() - pA);
00279
00280 if (w <= dist[fpMin1])
00281 {
00282
00283 w = dist[fpMin1] + 1E-6*(dist[fpB] - dist[fpA]);
00284
00285 point newPoint
00286 (
00287 pA + Foam::sqrt(w / (dist[fpB] - dist[fpA]))*(pB - pA)
00288 );
00289
00290 Pout<< "Adapting position of vertex " << f[fp] << " on face "
00291 << f << " from " << near.rawPoint() << " to " << newPoint
00292 << endl;
00293
00294 near.setPoint(newPoint);
00295 }
00296
00297
00298
00299
00300 meshMod.setAction
00301 (
00302 polyModifyPoint
00303 (
00304 f[fp],
00305 near.rawPoint(),
00306 false,
00307 -1,
00308 true
00309 )
00310 );
00311
00312 dist[fp] = w;
00313
00314
00315 fpMin1 = fp;
00316 fp = f.fcIndex(fpMin1);
00317 }
00318
00319
00320
00321
00322
00323 fpMin1 = fpA;
00324 fp = f.rcIndex(fpMin1);
00325
00326 while (fp != fpB)
00327 {
00328
00329 pointHit near = lineAB.nearestDist(points[f[fp]]);
00330
00331 scalar w = magSqr(near.rawPoint() - pA);
00332
00333 if (w <= dist[fpMin1])
00334 {
00335
00336 w = dist[fpMin1] + 1E-6*(dist[fpB] - dist[fpA]);
00337
00338 point newPoint
00339 (
00340 pA + Foam::sqrt(w / (dist[fpB] - dist[fpA]))*(pB - pA)
00341 );
00342
00343 Pout<< "Adapting position of vertex " << f[fp] << " on face "
00344 << f << " from " << near.rawPoint() << " to " << newPoint
00345 << endl;
00346
00347 near.setPoint(newPoint);
00348 }
00349
00350
00351
00352
00353 meshMod.setAction
00354 (
00355 polyModifyPoint
00356 (
00357 f[fp],
00358 near.rawPoint(),
00359 false,
00360 -1,
00361 true
00362 )
00363 );
00364
00365 dist[fp] = w;
00366
00367
00368 fpMin1 = fp;
00369 fp = f.rcIndex(fpMin1);
00370 }
00371
00372 dist.sort();
00373
00374
00375 if (dist.indices()[dist.size()-1] != fpB)
00376 {
00377 OFstream str("conflictingFace.obj");
00378 meshTools::writeOBJ(str, faceList(1, f), points);
00379
00380 FatalErrorIn("faceCollapser::setRefinement")
00381 << "Trying to collapse face:" << faceI << " vertices:" << f
00382 << " to edges between vertices " << f[fpA] << " and "
00383 << f[fpB] << " but " << f[fpB] << " does not seem to be the"
00384 << " vertex furthest away from " << f[fpA] << endl
00385 << "Dumped conflicting face to obj file conflictingFace.obj"
00386 << abort(FatalError);
00387 }
00388
00389
00390
00391 Pout<< "Face:" << f << " fpA:" << fpA << " fpB:" << fpB << nl;
00392
00393 labelList sortedFp(f.size());
00394 forAll(dist.indices(), i)
00395 {
00396 label fp = dist.indices()[i];
00397
00398 Pout<< " fp:" << fp << " distance:" << dist[i] << nl;
00399
00400 sortedFp[fp] = i;
00401 }
00402
00403 const labelList& fEdges = mesh_.faceEdges()[faceI];
00404
00405
00406
00407
00408
00409
00410
00411 forAll(f, fp)
00412 {
00413 label fp1 = f.fcIndex(fp);
00414
00415
00416 label sorted0 = sortedFp[fp];
00417 label sorted1 = sortedFp[fp1];
00418
00419
00420 DynamicList<label> edgePoints(f.size());
00421
00422
00423 bool fpToFp1;
00424
00425 if (sorted0 < sorted1)
00426 {
00427 fpToFp1 = true;
00428
00429 for (label j = sorted0+1; j < sorted1; j++)
00430 {
00431 edgePoints.append(f[dist.indices()[j]]);
00432 }
00433 }
00434 else
00435 {
00436 fpToFp1 = false;
00437
00438 for (label j = sorted1+1; j < sorted0; j++)
00439 {
00440 edgePoints.append(f[dist.indices()[j]]);
00441 }
00442 }
00443
00444 if (edgePoints.size())
00445 {
00446 edgePoints.shrink();
00447
00448 label edgeI = findEdge(edges, fEdges, f[fp], f[fp1]);
00449
00450 const edge& e = edges[edgeI];
00451
00452 if (fpToFp1 == (f[fp] == e.start()))
00453 {
00454 splitEdges.insert(edgeI, edgePoints);
00455 }
00456 else
00457 {
00458 reverse(edgePoints);
00459 splitEdges.insert(edgeI, edgePoints);
00460 }
00461
00462
00463 insert(edgeFaces[edgeI], faceI, affectedFaces);
00464 }
00465 }
00466 }
00467
00468 for
00469 (
00470 Map<labelList>::const_iterator iter = splitEdges.begin();
00471 iter != splitEdges.end();
00472 ++iter
00473 )
00474 {
00475 Pout<< "Split edge:" << iter.key()
00476 << " verts:" << mesh_.edges()[iter.key()]
00477 << " in:" << nl;
00478 const labelList& edgePoints = iter();
00479
00480 forAll(edgePoints, i)
00481 {
00482 Pout<< " " << edgePoints[i] << nl;
00483 }
00484 }
00485
00486
00487
00488
00489
00490
00491 forAll(faceLabels, i)
00492 {
00493 const label faceI = faceLabels[i];
00494
00495 meshMod.setAction(polyRemoveFace(faceI));
00496
00497
00498 affectedFaces.erase(faceI);
00499 }
00500
00501
00502
00503
00504
00505
00506 for
00507 (
00508 labelHashSet::const_iterator iter = affectedFaces.begin();
00509 iter != affectedFaces.end();
00510 ++iter
00511 )
00512 {
00513 filterFace(splitEdges, iter.key(), meshMod);
00514 }
00515 }
00516
00517
00518