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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "argList.H"
00054 #include "triSurface.H"
00055 #include "OFstream.H"
00056 #include "ListOps.H"
00057 #include "triSurfaceTools.H"
00058
00059 using namespace Foam;
00060
00061
00062
00063 void writeOBJ(Ostream& os, const pointField& pts)
00064 {
00065 forAll(pts, i)
00066 {
00067 const point& pt = pts[i];
00068
00069 os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00070 }
00071 }
00072
00073
00074 void dumpPoints(const triSurface& surf, const labelList& borderPoint)
00075 {
00076 fileName fName("borderPoints.obj");
00077
00078 Info<< "Dumping borderPoints as Lightwave .obj file to " << fName
00079 << "\nThis can be visualized with e.g. javaview (www.javaview.de)\n\n";
00080
00081 OFstream os(fName);
00082
00083 forAll(borderPoint, pointI)
00084 {
00085 if (borderPoint[pointI] != -1)
00086 {
00087 const point& pt = surf.localPoints()[pointI];
00088
00089 os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00090 }
00091 }
00092 }
00093
00094
00095 void dumpEdges(const triSurface& surf, const boolList& borderEdge)
00096 {
00097 fileName fName("borderEdges.obj");
00098
00099 Info<< "Dumping borderEdges as Lightwave .obj file to " << fName
00100 << "\nThis can be visualized with e.g. javaview (www.javaview.de)\n\n";
00101
00102 OFstream os(fName);
00103
00104 writeOBJ(os, surf.localPoints());
00105
00106 forAll(borderEdge, edgeI)
00107 {
00108 if (borderEdge[edgeI])
00109 {
00110 const edge& e = surf.edges()[edgeI];
00111
00112 os << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
00113 }
00114 }
00115 }
00116
00117
00118 void dumpFaces
00119 (
00120 const fileName& fName,
00121 const triSurface& surf,
00122 const Map<label>& connectedFaces
00123 )
00124 {
00125 Info<< "Dumping connectedFaces as Lightwave .obj file to " << fName
00126 << "\nThis can be visualized with e.g. javaview (www.javaview.de)\n\n";
00127
00128 OFstream os(fName);
00129
00130 for
00131 (
00132 Map<label>::const_iterator iter = connectedFaces.begin();
00133 iter != connectedFaces.end();
00134 ++iter
00135 )
00136 {
00137 const labelledTri& f = surf.localFaces()[iter.key()];
00138
00139 point ctr(f.centre(surf.localPoints()));
00140
00141 os << "v " << ctr.x() << ' ' << ctr.y() << ' ' << ctr.z() << endl;
00142 }
00143 }
00144
00145
00146 void testSortedEdgeFaces(const triSurface& surf)
00147 {
00148 const labelListList& edgeFaces = surf.edgeFaces();
00149 const labelListList& sortedEdgeFaces = surf.sortedEdgeFaces();
00150
00151 forAll(edgeFaces, edgeI)
00152 {
00153 const labelList& myFaces = edgeFaces[edgeI];
00154 const labelList& sortMyFaces = sortedEdgeFaces[edgeI];
00155
00156 forAll(myFaces, i)
00157 {
00158 if (findIndex(sortMyFaces, myFaces[i]) == -1)
00159 {
00160 FatalErrorIn("testSortedEdgeFaces") << abort(FatalError);
00161 }
00162 }
00163 forAll(sortMyFaces, i)
00164 {
00165 if (findIndex(myFaces, sortMyFaces[i]) == -1)
00166 {
00167 FatalErrorIn("testSortedEdgeFaces") << abort(FatalError);
00168 }
00169 }
00170 }
00171 }
00172
00173
00174
00175 label markBorderEdges
00176 (
00177 const bool debug,
00178 const triSurface& surf,
00179 boolList& borderEdge
00180 )
00181 {
00182 label nBorderEdges = 0;
00183
00184 const labelListList& edgeFaces = surf.edgeFaces();
00185
00186 forAll(edgeFaces, edgeI)
00187 {
00188 if (edgeFaces[edgeI].size() == 4)
00189 {
00190 borderEdge[edgeI] = true;
00191
00192 nBorderEdges++;
00193 }
00194 }
00195
00196 if (debug)
00197 {
00198 dumpEdges(surf, borderEdge);
00199 }
00200
00201 return nBorderEdges;
00202 }
00203
00204
00205
00206
00207 label markBorderPoints
00208 (
00209 const bool debug,
00210 const triSurface& surf,
00211 const boolList& borderEdge,
00212 labelList& borderPoint
00213 )
00214 {
00215 label nPoints = surf.nPoints();
00216
00217 const labelListList& pointEdges = surf.pointEdges();
00218
00219 forAll(pointEdges, pointI)
00220 {
00221 const labelList& pEdges = pointEdges[pointI];
00222
00223 label nBorderEdges = 0;
00224
00225 forAll(pEdges, i)
00226 {
00227 if (borderEdge[pEdges[i]])
00228 {
00229 nBorderEdges++;
00230 }
00231 }
00232
00233 if (nBorderEdges == 2 && borderPoint[pointI] == -1)
00234 {
00235 borderPoint[pointI] = nPoints++;
00236 }
00237 }
00238
00239 label nBorderPoints = nPoints - surf.nPoints();
00240
00241 if (debug)
00242 {
00243 dumpPoints(surf, borderPoint);
00244 }
00245
00246 return nBorderPoints;
00247 }
00248
00249
00250
00251
00252 scalar minEdgeLen(const triSurface& surf, const label pointI)
00253 {
00254 const pointField& points = surf.localPoints();
00255
00256 const labelList& pEdges = surf.pointEdges()[pointI];
00257
00258 scalar minLen = GREAT;
00259
00260 forAll(pEdges, i)
00261 {
00262 label edgeI = pEdges[i];
00263
00264 scalar len = surf.edges()[edgeI].mag(points);
00265
00266 if (len < minLen)
00267 {
00268 minLen = len;
00269 }
00270 }
00271 return minLen;
00272 }
00273
00274
00275
00276 label findEdge
00277 (
00278 const triSurface& surf,
00279 const labelList& edgeLabels,
00280 const label v0,
00281 const label v1
00282 )
00283 {
00284 forAll(edgeLabels, i)
00285 {
00286 label edgeI = edgeLabels[i];
00287
00288 const edge& e = surf.edges()[edgeI];
00289
00290 if
00291 (
00292 (
00293 e.start() == v0
00294 && e.end() == v1
00295 )
00296 || (
00297 e.start() == v1
00298 && e.end() == v0
00299 )
00300 )
00301 {
00302 return edgeI;
00303 }
00304 }
00305
00306
00307 FatalErrorIn("findEdge") << "Cannot find edge with labels " << v0
00308 << ' ' << v1 << " in candidates " << edgeLabels
00309 << " with vertices:" << UIndirectList<edge>(surf.edges(), edgeLabels)()
00310 << abort(FatalError);
00311
00312 return -1;
00313 }
00314
00315
00316
00317 label otherEdge
00318 (
00319 const triSurface& surf,
00320 const label faceI,
00321 const label otherEdgeI,
00322 const label pointI
00323 )
00324 {
00325 const labelList& fEdges = surf.faceEdges()[faceI];
00326
00327 forAll(fEdges, i)
00328 {
00329 label edgeI = fEdges[i];
00330
00331 const edge& e = surf.edges()[edgeI];
00332
00333 if
00334 (
00335 edgeI != otherEdgeI
00336 && (
00337 e.start() == pointI
00338 || e.end() == pointI
00339 )
00340 )
00341 {
00342 return edgeI;
00343 }
00344 }
00345
00346 FatalErrorIn("otherEdge") << "Cannot find other edge on face " << faceI
00347 << " verts:" << surf.localPoints()[faceI]
00348 << " connected to point " << pointI
00349 << " faceEdges:" << UIndirectList<edge>(surf.edges(), fEdges)()
00350 << abort(FatalError);
00351
00352 return -1;
00353 }
00354
00355
00356
00357
00358
00359 void walkSplitLine
00360 (
00361 const triSurface& surf,
00362 const boolList& borderEdge,
00363 const labelList& borderPoint,
00364
00365 const label startFaceI,
00366 const label startEdgeI,
00367 const label startPointI,
00368
00369 Map<label>& faceToEdge,
00370 Map<label>& faceToPoint
00371 )
00372 {
00373 label faceI = startFaceI;
00374 label edgeI = startEdgeI;
00375 label pointI = startPointI;
00376
00377 do
00378 {
00379
00380
00381
00382
00383 do
00384 {
00385
00386 edgeI = otherEdge(surf, faceI, edgeI, pointI);
00387
00388 if (borderEdge[edgeI])
00389 {
00390 if (!faceToEdge.insert(faceI, edgeI))
00391 {
00392
00393 return;
00394 }
00395 else
00396 {
00397
00398 break;
00399 }
00400 }
00401 else if (!faceToPoint.insert(faceI, pointI))
00402 {
00403
00404 return;
00405 }
00406
00407
00408 const labelList& eFaces = surf.edgeFaces()[edgeI];
00409
00410 if (eFaces.size() != 2)
00411 {
00412 FatalErrorIn("walkSplitPoint") << abort(FatalError);
00413 }
00414
00415 if (eFaces[0] == faceI)
00416 {
00417 faceI = eFaces[1];
00418 }
00419 else if (eFaces[1] == faceI)
00420 {
00421 faceI = eFaces[0];
00422 }
00423 else
00424 {
00425 FatalErrorIn("walkSplitPoint") << abort(FatalError);
00426 }
00427 }
00428 while (true);
00429
00430
00431
00432
00433
00434
00435 pointI = surf.edges()[edgeI].otherVertex(pointI);
00436
00437 if (borderPoint[pointI] == -1)
00438 {
00439 return;
00440 }
00441
00442 }
00443 while (true);
00444 }
00445
00446
00447
00448
00449 label sharedFace
00450 (
00451 const triSurface& surf,
00452 const label firstFaceI,
00453 const label sharedEdgeI
00454 )
00455 {
00456
00457
00458 const edge& e = surf.edges()[sharedEdgeI];
00459
00460 const labelledTri& f = surf.localFaces()[firstFaceI];
00461
00462 label startIndex = findIndex(f, e.start());
00463
00464
00465 bool edgeOrder = (f[f.fcIndex(startIndex)] == e.end());
00466
00467
00468
00469
00470 const labelList& eFaces = surf.sortedEdgeFaces()[sharedEdgeI];
00471
00472
00473 label faceIndex = findIndex(eFaces, firstFaceI);
00474
00475 if (edgeOrder)
00476 {
00477
00478 return eFaces[eFaces.rcIndex(faceIndex)];
00479 }
00480 else
00481 {
00482
00483 return eFaces[eFaces.fcIndex(faceIndex)];
00484 }
00485 }
00486
00487
00488
00489
00490 void calcPointVecs
00491 (
00492 const triSurface& surf,
00493 const Map<label>& faceToEdge,
00494 const Map<label>& faceToPoint,
00495 vectorField& borderPointVec
00496 )
00497 {
00498 const labelListList& sortedEdgeFaces = surf.sortedEdgeFaces();
00499 const edgeList& edges = surf.edges();
00500 const pointField& points = surf.localPoints();
00501
00502 boolList edgeDone(surf.nEdges(), false);
00503
00504 for
00505 (
00506 Map<label>::const_iterator iter = faceToEdge.begin();
00507 iter != faceToEdge.end();
00508 ++iter
00509 )
00510 {
00511 label edgeI = iter();
00512
00513 if (!edgeDone[edgeI])
00514 {
00515 edgeDone[edgeI] = true;
00516
00517
00518
00519
00520 label face0I = -1;
00521 label face1I = -1;
00522
00523 const labelList& eFaces = sortedEdgeFaces[edgeI];
00524
00525 forAll(eFaces, i)
00526 {
00527 label faceI = eFaces[i];
00528
00529 if (faceToEdge.found(faceI))
00530 {
00531 if (face0I == -1)
00532 {
00533 face0I = faceI;
00534 }
00535 else if (face1I == -1)
00536 {
00537 face1I = faceI;
00538
00539 break;
00540 }
00541 }
00542 }
00543
00544 if (face0I == -1 && face1I == -1)
00545 {
00546 Info<< "Writing surface to errorSurf.ftr" << endl;
00547
00548 surf.write("errorSurf.ftr");
00549
00550 FatalErrorIn("calcPointVecs")
00551 << "Cannot find two faces using border edge " << edgeI
00552 << " verts:" << edges[edgeI]
00553 << " eFaces:" << eFaces << endl
00554 << "face0I:" << face0I
00555 << " face1I:" << face1I << nl
00556 << "faceToEdge:" << faceToEdge << nl
00557 << "faceToPoint:" << faceToPoint
00558 << "Written surface to errorSurf.ftr"
00559 << abort(FatalError);
00560 }
00561
00562
00563
00564 const edge& e = edges[edgeI];
00565 vector eVec = e.vec(points);
00566
00567
00568
00569 vector midVec(vector::zero);
00570
00571 if (face0I != -1)
00572 {
00573 label v0 = triSurfaceTools::oppositeVertex(surf, face0I, edgeI);
00574 vector e0 = (points[v0] - points[e.start()]) ^ eVec;
00575 e0 /= mag(e0);
00576 midVec = e0;
00577 }
00578
00579 if (face1I != -1)
00580 {
00581 label v1 = triSurfaceTools::oppositeVertex(surf, face1I, edgeI);
00582 vector e1 = (points[e.start()] - points[v1]) ^ eVec;
00583 e1 /= mag(e1);
00584 midVec += e1;
00585 }
00586
00587 scalar magMidVec = mag(midVec);
00588
00589 if (magMidVec > SMALL)
00590 {
00591 midVec /= magMidVec;
00592
00593
00594 borderPointVec[e.start()] += midVec;
00595 borderPointVec[e.end()] += midVec;
00596 }
00597 }
00598 }
00599 }
00600
00601
00602
00603
00604 void renumberFaces
00605 (
00606 const triSurface& surf,
00607 const labelList& pointMap,
00608 const Map<label>& faceToEdge,
00609 List<labelledTri>& newTris
00610 )
00611 {
00612 for
00613 (
00614 Map<label>::const_iterator iter = faceToEdge.begin();
00615 iter != faceToEdge.end();
00616 ++iter
00617 )
00618 {
00619 label faceI = iter.key();
00620
00621 const labelledTri& f = surf.localFaces()[faceI];
00622
00623 forAll(f, fp)
00624 {
00625 if (pointMap[f[fp]] != -1)
00626 {
00627 newTris[faceI][fp] = pointMap[f[fp]];
00628 }
00629 }
00630 }
00631 }
00632
00633
00634
00635
00636 bool splitBorderEdges
00637 (
00638 triSurface& surf,
00639 const boolList& borderEdge,
00640 const labelList& borderPoint
00641 )
00642 {
00643 labelList edgesToBeSplit(surf.nEdges());
00644 label nSplit = 0;
00645
00646 forAll(borderEdge, edgeI)
00647 {
00648 if (borderEdge[edgeI])
00649 {
00650 const edge& e = surf.edges()[edgeI];
00651
00652 if (borderPoint[e.start()] == -1 && borderPoint[e.end()] == -1)
00653 {
00654
00655
00656 edgesToBeSplit[nSplit++] = edgeI;
00657 }
00658 }
00659 }
00660 edgesToBeSplit.setSize(nSplit);
00661
00662 if (nSplit > 0)
00663 {
00664 Info<< "Splitting surface along " << nSplit << " borderEdges that don't"
00665 << " neighbour other borderEdges" << nl << endl;
00666
00667 surf = triSurfaceTools::greenRefine(surf, edgesToBeSplit);
00668
00669 return true;
00670 }
00671 else
00672 {
00673 Info<< "No edges to be split" <<nl << endl;
00674
00675 return false;
00676 }
00677 }
00678
00679
00680
00681
00682 int main(int argc, char *argv[])
00683 {
00684 argList::noParallel();
00685 argList::validArgs.clear();
00686
00687 argList::validArgs.append("surface file");
00688 argList::validArgs.append("output surface file");
00689 argList::validOptions.insert("debug", "");
00690
00691 argList args(argc, argv);
00692
00693 fileName inSurfName(args.additionalArgs()[0]);
00694 fileName outSurfName(args.additionalArgs()[1]);
00695 bool debug = args.optionFound("debug");
00696
00697
00698 Info<< "Reading surface from " << inSurfName << endl;
00699
00700 triSurface surf(inSurfName);
00701
00702
00703 testSortedEdgeFaces(surf);
00704
00705
00706 boolList borderEdge(surf.nEdges(), false);
00707 markBorderEdges(debug, surf, borderEdge);
00708
00709
00710
00711 labelList borderPoint(surf.nPoints(), -1);
00712 markBorderPoints(debug, surf, borderEdge, borderPoint);
00713
00714
00715 splitBorderEdges(surf, borderEdge, borderPoint);
00716
00717
00718 Info<< "Writing split surface to " << outSurfName << nl << endl;
00719 surf.write(outSurfName);
00720 Info<< "Finished writing surface to " << outSurfName << nl << endl;
00721
00722
00723
00724 label nOldBorderEdges = -1;
00725 label nOldBorderPoints = -1;
00726
00727 label iteration = 0;
00728
00729 do
00730 {
00731
00732 boolList borderEdge(surf.nEdges(), false);
00733
00734 label nBorderEdges = markBorderEdges(debug, surf, borderEdge);
00735
00736 if (nBorderEdges == 0)
00737 {
00738 Info<< "Found no border edges. Exiting." << nl << nl;
00739
00740 break;
00741 }
00742
00743
00744 labelList borderPoint(surf.nPoints(), -1);
00745
00746 label nBorderPoints =
00747 markBorderPoints
00748 (
00749 debug,
00750 surf,
00751 borderEdge,
00752 borderPoint
00753 );
00754
00755 if (nBorderPoints == 0)
00756 {
00757 Info<< "Found no border points. Exiting." << nl << nl;
00758
00759 break;
00760 }
00761
00762 Info<< "Found:\n"
00763 << " border edges :" << nBorderEdges << nl
00764 << " border points:" << nBorderPoints << nl
00765 << endl;
00766
00767 if
00768 (
00769 nBorderPoints == nOldBorderPoints
00770 && nBorderEdges == nOldBorderEdges
00771 )
00772 {
00773 Info<< "Stopping since number of border edges and point is same"
00774 << " as in previous iteration" << nl << endl;
00775
00776 break;
00777 }
00778
00779
00780
00781
00782
00783
00784 label startEdgeI = -1;
00785 label startPointI = -1;
00786
00787 forAll(borderEdge, edgeI)
00788 {
00789 if (borderEdge[edgeI])
00790 {
00791 const edge& e = surf.edges()[edgeI];
00792
00793 if ((borderPoint[e[0]] != -1) && (borderPoint[e[1]] == -1))
00794 {
00795 startEdgeI = edgeI;
00796 startPointI = e[0];
00797
00798 break;
00799 }
00800 else if ((borderPoint[e[0]] == -1) && (borderPoint[e[1]] != -1))
00801 {
00802 startEdgeI = edgeI;
00803 startPointI = e[1];
00804
00805 break;
00806 }
00807 }
00808 }
00809
00810 if (startEdgeI == -1)
00811 {
00812 Info<< "Cannot find starting point of splitLine\n" << endl;
00813
00814 break;
00815 }
00816
00817
00818 const labelList& eFaces = surf.edgeFaces()[startEdgeI];
00819
00820 label firstFaceI = eFaces[0];
00821
00822
00823
00824 label secondFaceI = sharedFace(surf, firstFaceI, startEdgeI);
00825
00826 Info<< "Starting local walk from:" << endl
00827 << " edge :" << startEdgeI << endl
00828 << " point:" << startPointI << endl
00829 << " face0:" << firstFaceI << endl
00830 << " face1:" << secondFaceI << endl
00831 << endl;
00832
00833
00834 Map<label> faceToEdge(2*nBorderEdges);
00835
00836 Map<label> faceToPoint(2*nBorderPoints);
00837
00838 faceToEdge.insert(firstFaceI, startEdgeI);
00839
00840 walkSplitLine
00841 (
00842 surf,
00843 borderEdge,
00844 borderPoint,
00845
00846 firstFaceI,
00847 startEdgeI,
00848 startPointI,
00849
00850 faceToEdge,
00851 faceToPoint
00852 );
00853
00854 faceToEdge.insert(secondFaceI, startEdgeI);
00855
00856 walkSplitLine
00857 (
00858 surf,
00859 borderEdge,
00860 borderPoint,
00861
00862 secondFaceI,
00863 startEdgeI,
00864 startPointI,
00865
00866 faceToEdge,
00867 faceToPoint
00868 );
00869
00870 Info<< "Finished local walk and visited" << nl
00871 << " border edges :" << faceToEdge.size() << nl
00872 << " border points(but not edges):" << faceToPoint.size() << nl
00873 << endl;
00874
00875 if (debug)
00876 {
00877 dumpFaces("faceToEdge.obj", surf, faceToEdge);
00878 dumpFaces("faceToPoint.obj", surf, faceToPoint);
00879 }
00880
00881
00882
00883
00884
00885
00886
00887
00888 vectorField borderPointVec(surf.nPoints(), vector(GREAT, GREAT, GREAT));
00889
00890 calcPointVecs(surf, faceToEdge, faceToPoint, borderPointVec);
00891
00892
00893
00894 pointField newPoints(surf.localPoints());
00895 newPoints.setSize(newPoints.size() + nBorderPoints);
00896
00897 forAll(borderPoint, pointI)
00898 {
00899 label newPointI = borderPoint[pointI];
00900
00901 if (newPointI != -1)
00902 {
00903 scalar minLen = minEdgeLen(surf, pointI);
00904
00905 vector n = borderPointVec[pointI];
00906 n /= mag(n);
00907
00908 newPoints[newPointI] = newPoints[pointI] + 0.1 * minLen * n;
00909 }
00910 }
00911
00912
00913
00914
00915
00916
00917
00918 List<labelledTri> newTris(surf.size());
00919
00920 forAll(surf, faceI)
00921 {
00922 newTris[faceI] = surf.localFaces()[faceI];
00923
00924 newTris[faceI].region() = surf[faceI].region();
00925 }
00926
00927
00928 renumberFaces(surf, borderPoint, faceToEdge, newTris);
00929
00930 renumberFaces(surf, borderPoint, faceToPoint, newTris);
00931
00932
00933
00934 forAll(newTris, faceI)
00935 {
00936 const labelledTri& f = newTris[faceI];
00937
00938 forAll(f, fp)
00939 {
00940 const point& pt = newPoints[f[fp]];
00941
00942 if (mag(pt) >= GREAT/2)
00943 {
00944 Info<< "newTri:" << faceI << " verts:" << f
00945 << " vert:" << f[fp] << " point:" << pt << endl;
00946 }
00947 }
00948 }
00949
00950
00951 surf = triSurface(newTris, surf.patches(), newPoints);
00952
00953 if (debug || (iteration != 0 && (iteration % 20) == 0))
00954 {
00955 Info<< "Writing surface to " << outSurfName << nl << endl;
00956
00957 surf.write(outSurfName);
00958
00959 Info<< "Finished writing surface to " << outSurfName << nl << endl;
00960 }
00961
00962
00963 nOldBorderEdges = nBorderEdges;
00964 nOldBorderPoints = nBorderPoints;
00965
00966 iteration++;
00967 }
00968 while (true);
00969
00970 Info<< "Writing final surface to " << outSurfName << nl << endl;
00971
00972 surf.write(outSurfName);
00973
00974 Info << "End\n" << endl;
00975
00976 return 0;
00977 }
00978
00979
00980