1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
28 #include "triSurface.H"
29 #include "triSurfaceTools.H"
30 #include "triSurfaceSearch.H"
33 #include "surfaceIntersection.H"
34 #include "SortableList.H"
35 #include "PatchTools.H"
39 // Does face use valid vertices?
40 bool validTri(const bool verbose, const triSurface& surf, const label faceI)
42 // Simple check on indices ok.
44 const labelledTri& f = surf[faceI];
48 (f[0] < 0) || (f[0] >= surf.points().size())
49 || (f[1] < 0) || (f[1] >= surf.points().size())
50 || (f[2] < 0) || (f[2] >= surf.points().size())
53 WarningIn("validTri(const triSurface&, const label)")
54 << "triangle " << faceI << " vertices " << f
55 << " uses point indices outside point range 0.."
56 << surf.points().size()-1 << endl;
61 if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2]))
63 WarningIn("validTri(const triSurface&, const label)")
64 << "triangle " << faceI
65 << " uses non-unique vertices " << f
66 << " coords:" << f.points(surf.points())
71 // duplicate triangle check
73 const labelList& fFaces = surf.faceFaces()[faceI];
75 // Check if faceNeighbours use same points as this face.
76 // Note: discards normal information - sides of baffle are merged.
79 label nbrFaceI = fFaces[i];
81 if (nbrFaceI <= faceI)
83 // lower numbered faces already checked
87 const labelledTri& nbrF = surf[nbrFaceI];
91 ((f[0] == nbrF[0]) || (f[0] == nbrF[1]) || (f[0] == nbrF[2]))
92 && ((f[1] == nbrF[0]) || (f[1] == nbrF[1]) || (f[1] == nbrF[2]))
93 && ((f[2] == nbrF[0]) || (f[2] == nbrF[1]) || (f[2] == nbrF[2]))
96 WarningIn("validTri(const triSurface&, const label)")
97 << "triangle " << faceI << " vertices " << f
98 << " has the same vertices as triangle " << nbrFaceI
99 << " vertices " << nbrF
100 << " coords:" << f.points(surf.points())
115 const scalarField& vals
118 scalar dist = nBins/(max - min);
120 labelList binCount(nBins, 0);
124 scalar val = vals[i];
128 if (Foam::mag(val - min) < SMALL)
132 else if (val >= max - SMALL)
138 index = label((val - min)*dist);
140 if ((index < 0) || (index >= nBins))
144 "countBins(const scalar, const scalar, const label"
145 ", const scalarField&)"
146 ) << "value " << val << " at index " << i
147 << " outside range " << min << " .. " << max << endl;
166 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
169 int main(int argc, char *argv[])
171 argList::noParallel();
173 argList::validArgs.clear();
174 argList::validArgs.append("surface file");
175 argList::validOptions.insert("checkSelfIntersection", "");
176 argList::validOptions.insert("verbose", "");
177 argList args(argc, argv);
179 bool checkSelfIntersection = args.optionFound("checkSelfIntersection");
180 bool verbose = args.optionFound("verbose");
182 fileName surfFileName(args.additionalArgs()[0]);
183 Pout<< "Reading surface from " << surfFileName << " ..." << nl << endl;
189 triSurface surf(surfFileName);
192 Pout<< "Statistics:" << endl;
193 surf.writeStats(Pout);
201 labelList regionSize(surf.patches().size(), 0);
205 label region = surf[faceI].region();
207 if (region < 0 || region >= regionSize.size())
209 WarningIn(args.executable())
210 << "Triangle " << faceI << " vertices " << surf[faceI]
211 << " has region " << region << " which is outside the range"
212 << " of regions 0.." << surf.patches().size()-1
217 regionSize[region]++;
221 Pout<< "Region\tSize" << nl
222 << "------\t----" << nl;
223 forAll(surf.patches(), patchI)
225 Pout<< surf.patches()[patchI].name() << '\t'
226 << regionSize[patchI] << nl;
236 DynamicList<label> illegalFaces(surf.size()/100 + 1);
240 if (!validTri(verbose, surf, faceI))
242 illegalFaces.append(faceI);
246 if (illegalFaces.size())
248 Pout<< "Surface has " << illegalFaces.size()
249 << " illegal triangles." << endl;
251 OFstream str("illegalFaces");
252 Pout<< "Dumping conflicting face labels to " << str.name() << endl
253 << "Paste this into the input for surfaceSubset" << endl;
258 Pout<< "Surface has no illegal triangles." << endl;
269 scalarField triQ(surf.size(), 0);
272 const labelledTri& f = surf[faceI];
274 if (f[0] == f[1] || f[0] == f[2] || f[1] == f[2])
276 //WarningIn(args.executable())
277 // << "Illegal triangle " << faceI << " vertices " << f
278 // << " coords " << f.points(surf.points()) << endl;
289 vector ba(tri.b() - tri.a());
290 ba /= mag(ba) + VSMALL;
292 vector ca(tri.c() - tri.a());
293 ca /= mag(ca) + VSMALL;
295 if (mag(ba&ca) > 1-1E-3)
301 triQ[faceI] = triPointRef
311 labelList binCount = countBins(0, 1, 20, triQ);
313 Pout<< "Triangle quality (equilateral=1, collapsed=0):"
320 scalar dist = (1.0 - 0.0)/20.0;
322 forAll(binCount, binI)
324 Pout<< " " << min << " .. " << min+dist << " : "
325 << 1.0/surf.size() * binCount[binI]
331 label minIndex = findMin(triQ);
332 label maxIndex = findMax(triQ);
334 Pout<< " min " << triQ[minIndex] << " for triangle " << minIndex
336 << " max " << triQ[maxIndex] << " for triangle " << maxIndex
341 if (triQ[minIndex] < SMALL)
343 WarningIn(args.executable()) << "Minimum triangle quality is "
344 << triQ[minIndex] << ". This might give problems in"
345 << " self-intersection testing later on." << endl;
348 // Dump for subsetting
350 DynamicList<label> problemFaces(surf.size()/100+1);
354 if (triQ[faceI] < 1E-11)
356 problemFaces.append(faceI);
359 OFstream str("badFaces");
361 Pout<< "Dumping bad quality faces to " << str.name() << endl
362 << "Paste this into the input for surfaceSubset" << nl
374 const edgeList& edges = surf.edges();
375 const pointField& localPoints = surf.localPoints();
377 scalarField edgeMag(edges.size());
381 edgeMag[edgeI] = edges[edgeI].mag(localPoints);
384 label minEdgeI = findMin(edgeMag);
385 label maxEdgeI = findMax(edgeMag);
387 const edge& minE = edges[minEdgeI];
388 const edge& maxE = edges[maxEdgeI];
391 Pout<< "Edges:" << nl
392 << " min " << edgeMag[minEdgeI] << " for edge " << minEdgeI
393 << " points " << localPoints[minE[0]] << localPoints[minE[1]]
395 << " max " << edgeMag[maxEdgeI] << " for edge " << maxEdgeI
396 << " points " << localPoints[maxE[0]] << localPoints[maxE[1]]
406 const edgeList& edges = surf.edges();
407 const pointField& localPoints = surf.localPoints();
409 const boundBox bb(localPoints);
410 scalar smallDim = 1E-6 * bb.mag();
412 Pout<< "Checking for points less than 1E-6 of bounding box ("
413 << bb.span() << " meter) apart."
417 SortableList<scalar> sortedMag(mag(localPoints));
421 for (label i = 1; i < sortedMag.size(); i++)
423 label ptI = sortedMag.indices()[i];
425 label prevPtI = sortedMag.indices()[i-1];
427 if (mag(localPoints[ptI] - localPoints[prevPtI]) < smallDim)
429 // Check if neighbours.
430 const labelList& pEdges = surf.pointEdges()[ptI];
436 const edge& e = edges[pEdges[i]];
438 if (e[0] == prevPtI || e[1] == prevPtI)
440 // point1 and point0 are connected through edge.
451 Pout<< " close unconnected points "
452 << ptI << ' ' << localPoints[ptI]
453 << " and " << prevPtI << ' '
454 << localPoints[prevPtI]
456 << mag(localPoints[ptI] - localPoints[prevPtI])
461 Pout<< " small edge between points "
462 << ptI << ' ' << localPoints[ptI]
463 << " and " << prevPtI << ' '
464 << localPoints[prevPtI]
466 << mag(localPoints[ptI] - localPoints[prevPtI])
472 Pout<< "Found " << nClose << " nearby points." << nl
481 DynamicList<label> problemFaces(surf.size()/100 + 1);
483 const labelListList& eFaces = surf.edgeFaces();
485 label nSingleEdges = 0;
486 forAll(eFaces, edgeI)
488 const labelList& myFaces = eFaces[edgeI];
490 if (myFaces.size() == 1)
492 problemFaces.append(myFaces[0]);
498 label nMultEdges = 0;
499 forAll(eFaces, edgeI)
501 const labelList& myFaces = eFaces[edgeI];
503 if (myFaces.size() > 2)
505 forAll(myFaces, myFaceI)
507 problemFaces.append(myFaces[myFaceI]);
513 problemFaces.shrink();
515 if ((nSingleEdges != 0) || (nMultEdges != 0))
517 Pout<< "Surface is not closed since not all edges connected to "
518 << "two faces:" << endl
519 << " connected to one face : " << nSingleEdges << endl
520 << " connected to >2 faces : " << nMultEdges << endl;
522 Pout<< "Conflicting face labels:" << problemFaces.size() << endl;
524 OFstream str("problemFaces");
526 Pout<< "Dumping conflicting face labels to " << str.name() << endl
527 << "Paste this into the input for surfaceSubset" << endl;
533 Pout<< "Surface is closed. All edges connected to two faces." << endl;
539 // Check singly connected domain
540 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
543 label numZones = surf.markZones(boolList(surf.nEdges(), false), faceZone);
545 Pout<< "Number of unconnected parts : " << numZones << endl;
549 Pout<< "Splitting surface into parts ..." << endl << endl;
551 fileName surfFileNameBase(surfFileName.name());
553 for(label zone = 0; zone < numZones; zone++)
555 boolList includeMap(surf.size(), false);
557 forAll(faceZone, faceI)
559 if (faceZone[faceI] == zone)
561 includeMap[faceI] = true;
580 surfFileNameBase.lessExt()
586 Pout<< "writing part " << zone << " size " << subSurf.size()
587 << " to " << subFileName << endl;
589 subSurf.write(subFileName);
600 labelHashSet borderEdge(surf.size()/1000);
601 PatchTools::checkOrientation(surf, false, &borderEdge);
604 // Colour all faces into zones using borderEdge
606 labelList normalZone;
607 label numNormalZones = PatchTools::markZones(surf, borderEdge, normalZone);
610 << "Number of zones (connected area with consistent normal) : "
611 << numNormalZones << endl;
613 if (numNormalZones > 1)
615 Pout<< "More than one normal orientation." << endl;
621 // Check self-intersection
622 // ~~~~~~~~~~~~~~~~~~~~~~~
624 if (checkSelfIntersection)
626 Pout<< "Checking self-intersection." << endl;
628 triSurfaceSearch querySurf(surf);
629 surfaceIntersection inter(querySurf);
631 if (inter.cutEdges().empty() && inter.cutPoints().empty())
633 Pout<< "Surface is not self-intersecting" << endl;
637 Pout<< "Surface is self-intersecting" << endl;
638 Pout<< "Writing edges of intersection to selfInter.obj" << endl;
640 OFstream intStream("selfInter.obj");
641 forAll(inter.cutPoints(), cutPointI)
643 const point& pt = inter.cutPoints()[cutPointI];
645 intStream << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
648 forAll(inter.cutEdges(), cutEdgeI)
650 const edge& e = inter.cutEdges()[cutEdgeI];
652 intStream << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
659 Pout<< "End\n" << endl;
665 // ************************************************************************* //