1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
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);
190 const pointField& surfPoints = surf.points();
193 Pout<< "Statistics:" << endl;
194 surf.writeStats(Pout);
202 labelList regionSize(surf.patches().size(), 0);
203 scalarField regionSumArea(surf.patches().size(), 0);
207 label region = surf[faceI].region();
209 if (region < 0 || region >= regionSize.size())
211 WarningIn(args.executable())
212 << "Triangle " << faceI << " vertices " << surf[faceI]
213 << " has region " << region
214 << " which is outside the range"
215 << " of regions 0.." << surf.patches().size() - 1
220 regionSize[region]++;
221 regionSumArea[region] += surf[faceI].mag(surfPoints);
225 Pout<< "Region\tSize\tArea" << nl
226 << "------\t----\t----" << nl;
227 forAll(surf.patches(), patchI)
229 Pout<< surf.patches()[patchI].name() << tab
230 << regionSize[patchI] << tab << regionSumArea[patchI] << nl;
240 DynamicList<label> illegalFaces(surf.size()/100 + 1);
244 if (!validTri(verbose, surf, faceI))
246 illegalFaces.append(faceI);
250 if (illegalFaces.size())
252 Pout<< "Surface has " << illegalFaces.size()
253 << " illegal triangles." << endl;
255 OFstream str("illegalFaces");
256 Pout<< "Dumping conflicting face labels to " << str.name() << endl
257 << "Paste this into the input for surfaceSubset" << endl;
262 Pout<< "Surface has no illegal triangles." << endl;
273 scalarField triQ(surf.size(), 0);
276 const labelledTri& f = surf[faceI];
278 if (f[0] == f[1] || f[0] == f[2] || f[1] == f[2])
280 //WarningIn(args.executable())
281 // << "Illegal triangle " << faceI << " vertices " << f
282 // << " coords " << f.points(surf.points()) << endl;
293 vector ba(tri.b() - tri.a());
294 ba /= mag(ba) + VSMALL;
296 vector ca(tri.c() - tri.a());
297 ca /= mag(ca) + VSMALL;
299 if (mag(ba&ca) > 1-1E-3)
305 triQ[faceI] = triPointRef
315 labelList binCount = countBins(0, 1, 20, triQ);
317 Pout<< "Triangle quality (equilateral=1, collapsed=0):"
324 scalar dist = (1.0 - 0.0)/20.0;
326 forAll(binCount, binI)
328 Pout<< " " << min << " .. " << min+dist << " : "
329 << 1.0/surf.size() * binCount[binI]
335 label minIndex = findMin(triQ);
336 label maxIndex = findMax(triQ);
338 Pout<< " min " << triQ[minIndex] << " for triangle " << minIndex
340 << " max " << triQ[maxIndex] << " for triangle " << maxIndex
345 if (triQ[minIndex] < SMALL)
347 WarningIn(args.executable()) << "Minimum triangle quality is "
348 << triQ[minIndex] << ". This might give problems in"
349 << " self-intersection testing later on." << endl;
352 // Dump for subsetting
354 DynamicList<label> problemFaces(surf.size()/100+1);
358 if (triQ[faceI] < 1E-11)
360 problemFaces.append(faceI);
363 OFstream str("badFaces");
365 Pout<< "Dumping bad quality faces to " << str.name() << endl
366 << "Paste this into the input for surfaceSubset" << nl
378 const edgeList& edges = surf.edges();
379 const pointField& localPoints = surf.localPoints();
381 scalarField edgeMag(edges.size());
385 edgeMag[edgeI] = edges[edgeI].mag(localPoints);
388 label minEdgeI = findMin(edgeMag);
389 label maxEdgeI = findMax(edgeMag);
391 const edge& minE = edges[minEdgeI];
392 const edge& maxE = edges[maxEdgeI];
395 Pout<< "Edges:" << nl
396 << " min " << edgeMag[minEdgeI] << " for edge " << minEdgeI
397 << " points " << localPoints[minE[0]] << localPoints[minE[1]]
399 << " max " << edgeMag[maxEdgeI] << " for edge " << maxEdgeI
400 << " points " << localPoints[maxE[0]] << localPoints[maxE[1]]
410 const edgeList& edges = surf.edges();
411 const pointField& localPoints = surf.localPoints();
413 const boundBox bb(localPoints);
414 scalar smallDim = 1E-6 * bb.mag();
416 Pout<< "Checking for points less than 1E-6 of bounding box ("
417 << bb.span() << " meter) apart."
421 SortableList<scalar> sortedMag(mag(localPoints));
425 for (label i = 1; i < sortedMag.size(); i++)
427 label ptI = sortedMag.indices()[i];
429 label prevPtI = sortedMag.indices()[i-1];
431 if (mag(localPoints[ptI] - localPoints[prevPtI]) < smallDim)
433 // Check if neighbours.
434 const labelList& pEdges = surf.pointEdges()[ptI];
440 const edge& e = edges[pEdges[i]];
442 if (e[0] == prevPtI || e[1] == prevPtI)
444 // point1 and point0 are connected through edge.
455 Pout<< " close unconnected points "
456 << ptI << ' ' << localPoints[ptI]
457 << " and " << prevPtI << ' '
458 << localPoints[prevPtI]
460 << mag(localPoints[ptI] - localPoints[prevPtI])
465 Pout<< " small edge between points "
466 << ptI << ' ' << localPoints[ptI]
467 << " and " << prevPtI << ' '
468 << localPoints[prevPtI]
470 << mag(localPoints[ptI] - localPoints[prevPtI])
476 Pout<< "Found " << nClose << " nearby points." << nl
485 DynamicList<label> problemFaces(surf.size()/100 + 1);
487 const labelListList& eFaces = surf.edgeFaces();
489 label nSingleEdges = 0;
490 forAll(eFaces, edgeI)
492 const labelList& myFaces = eFaces[edgeI];
494 if (myFaces.size() == 1)
496 problemFaces.append(myFaces[0]);
502 label nMultEdges = 0;
503 forAll(eFaces, edgeI)
505 const labelList& myFaces = eFaces[edgeI];
507 if (myFaces.size() > 2)
509 forAll(myFaces, myFaceI)
511 problemFaces.append(myFaces[myFaceI]);
517 problemFaces.shrink();
519 if ((nSingleEdges != 0) || (nMultEdges != 0))
521 Pout<< "Surface is not closed since not all edges connected to "
522 << "two faces:" << endl
523 << " connected to one face : " << nSingleEdges << endl
524 << " connected to >2 faces : " << nMultEdges << endl;
526 Pout<< "Conflicting face labels:" << problemFaces.size() << endl;
528 OFstream str("problemFaces");
530 Pout<< "Dumping conflicting face labels to " << str.name() << endl
531 << "Paste this into the input for surfaceSubset" << endl;
537 Pout<< "Surface is closed. All edges connected to two faces." << endl;
543 // Check singly connected domain
544 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
547 label numZones = surf.markZones(boolList(surf.nEdges(), false), faceZone);
549 Pout<< "Number of unconnected parts : " << numZones << endl;
553 Pout<< "Splitting surface into parts ..." << endl << endl;
555 fileName surfFileNameBase(surfFileName.name());
557 for(label zone = 0; zone < numZones; zone++)
559 boolList includeMap(surf.size(), false);
561 forAll(faceZone, faceI)
563 if (faceZone[faceI] == zone)
565 includeMap[faceI] = true;
584 surfFileNameBase.lessExt()
590 Pout<< "writing part " << zone << " size " << subSurf.size()
591 << " to " << subFileName << endl;
593 subSurf.write(subFileName);
604 labelHashSet borderEdge(surf.size()/1000);
605 PatchTools::checkOrientation(surf, false, &borderEdge);
608 // Colour all faces into zones using borderEdge
610 labelList normalZone;
611 label numNormalZones = PatchTools::markZones(surf, borderEdge, normalZone);
614 << "Number of zones (connected area with consistent normal) : "
615 << numNormalZones << endl;
617 if (numNormalZones > 1)
619 Pout<< "More than one normal orientation." << endl;
625 // Check self-intersection
626 // ~~~~~~~~~~~~~~~~~~~~~~~
628 if (checkSelfIntersection)
630 Pout<< "Checking self-intersection." << endl;
632 triSurfaceSearch querySurf(surf);
633 surfaceIntersection inter(querySurf);
635 if (inter.cutEdges().empty() && inter.cutPoints().empty())
637 Pout<< "Surface is not self-intersecting" << endl;
641 Pout<< "Surface is self-intersecting" << endl;
642 Pout<< "Writing edges of intersection to selfInter.obj" << endl;
644 OFstream intStream("selfInter.obj");
645 forAll(inter.cutPoints(), cutPointI)
647 const point& pt = inter.cutPoints()[cutPointI];
649 intStream << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
652 forAll(inter.cutEdges(), cutEdgeI)
654 const edge& e = inter.cutEdges()[cutEdgeI];
656 intStream << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
663 Pout<< "End\n" << endl;
669 // ************************************************************************* //