From c3005794ab0b69c9022063fb19f9ce6f16f9486d Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 24 Nov 2016 15:31:32 +0100 Subject: [PATCH] ENH: provide triSurfaceTools::validTri() method to reduce code duplication - identical code was present in surfaceCheck (original source), and isoSurface, isoSurfaceCell (copies). - add in a MeshedSurface variant as well, since this will likely be needed in the near future --- .../surface/surfaceCheck/surfaceCheck.C | 78 +-------- .../triSurfaceTools/triSurfaceTools.C | 165 +++++++++++++++++- .../triSurfaceTools/triSurfaceTools.H | 16 +- .../sampledSurface/isoSurface/isoSurface.C | 74 +------- .../sampledSurface/isoSurface/isoSurface.H | 3 - .../isoSurface/isoSurfaceCell.C | 71 +------- .../isoSurface/isoSurfaceCell.H | 3 - 7 files changed, 183 insertions(+), 227 deletions(-) diff --git a/applications/utilities/surface/surfaceCheck/surfaceCheck.C b/applications/utilities/surface/surfaceCheck/surfaceCheck.C index dcbf38b870..0af29e1962 100644 --- a/applications/utilities/surface/surfaceCheck/surfaceCheck.C +++ b/applications/utilities/surface/surfaceCheck/surfaceCheck.C @@ -55,6 +55,7 @@ Usage #include "triangle.H" #include "triSurface.H" #include "triSurfaceSearch.H" +#include "triSurfaceTools.H" #include "argList.H" #include "OFstream.H" #include "OBJstream.H" @@ -64,79 +65,6 @@ Usage using namespace Foam; -// Does face use valid vertices? -bool validTri -( - const bool verbose, - const triSurface& surf, - const label facei -) -{ - // Simple check on indices ok. - - const labelledTri& f = surf[facei]; - - forAll(f, fp) - { - if (f[fp] < 0 || f[fp] >= surf.points().size()) - { - WarningInFunction - << "triangle " << facei << " vertices " << f - << " uses point indices outside point range 0.." - << surf.points().size()-1 << endl; - return false; - } - } - - if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2])) - { - WarningInFunction - << "triangle " << facei - << " uses non-unique vertices " << f - << " coords:" << f.points(surf.points()) - << endl; - return false; - } - - // duplicate triangle check - - const labelList& fFaces = surf.faceFaces()[facei]; - - // Check if faceNeighbours use same points as this face. - // Note: discards normal information - sides of baffle are merged. - forAll(fFaces, i) - { - label nbrFacei = fFaces[i]; - - if (nbrFacei <= facei) - { - // lower numbered faces already checked - continue; - } - - const labelledTri& nbrF = surf[nbrFacei]; - - if - ( - ((f[0] == nbrF[0]) || (f[0] == nbrF[1]) || (f[0] == nbrF[2])) - && ((f[1] == nbrF[0]) || (f[1] == nbrF[1]) || (f[1] == nbrF[2])) - && ((f[2] == nbrF[0]) || (f[2] == nbrF[1]) || (f[2] == nbrF[2])) - ) - { - WarningInFunction - << "triangle " << facei << " vertices " << f - << " has the same vertices as triangle " << nbrFacei - << " vertices " << nbrF - << " coords:" << f.points(surf.points()) - << endl; - - return false; - } - } - return true; -} - - labelList countBins ( const scalar min, @@ -377,14 +305,12 @@ int main(int argc, char *argv[]) const fileName surfFileName = args[1]; const bool checkSelfIntersect = args.optionFound("checkSelfIntersection"); - const bool verbose = args.optionFound("verbose"); const bool splitNonManifold = args.optionFound("splitNonManifold"); label outputThreshold = 10; args.optionReadIfPresent("outputThreshold", outputThreshold); Info<< "Reading surface from " << surfFileName << " ..." << nl << endl; - // Read // ~~~~ @@ -479,7 +405,7 @@ int main(int argc, char *argv[]) forAll(surf, facei) { - if (!validTri(verbose, surf, facei)) + if (!triSurfaceTools::validTri(surf, facei)) { illegalFaces.append(facei); } diff --git a/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.C b/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.C index 1915de7ecc..aec02f8e06 100644 --- a/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.C +++ b/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,6 +26,7 @@ License #include "triSurfaceTools.H" #include "triSurface.H" +#include "MeshedSurface.H" #include "OFstream.H" #include "mergePoints.H" #include "polyMesh.H" @@ -2743,8 +2744,168 @@ void Foam::triSurfaceTools::calcInterpolationWeights // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -// Tracking: +// Checking: +bool Foam::triSurfaceTools::validTri +( + const triSurface& surf, + const label facei +) +{ + typedef labelledTri FaceType; + const FaceType& f = surf[facei]; + + // Simple check on indices ok. + forAll(f, fp) + { + if (f[fp] < 0 || f[fp] >= surf.points().size()) + { + WarningInFunction + << "triangle " << facei << " vertices " << f + << " uses point indices outside point range 0.." + << surf.points().size()-1 + << endl; + return false; + } + } + + if (f[0] == f[1] || f[0] == f[2] || f[1] == f[2]) + { + WarningInFunction + << "triangle " << facei + << " uses non-unique vertices " << f + << " coords:" << f.points(surf.points()) + << endl; + return false; + } + + // duplicate triangle check + + const labelList& fFaces = surf.faceFaces()[facei]; + + // Check if faceNeighbours use same points as this face. + // Note: discards normal information - sides of baffle are merged. + forAll(fFaces, i) + { + label nbrFacei = fFaces[i]; + + if (nbrFacei <= facei) + { + // lower numbered faces already checked + continue; + } + + const FaceType& nbrF = surf[nbrFacei]; + + // Same as calling triFace::compare(f, nbrF) == 1 only + if + ( + (f[0] == nbrF[0] || f[0] == nbrF[1] || f[0] == nbrF[2]) + && (f[1] == nbrF[0] || f[1] == nbrF[1] || f[1] == nbrF[2]) + && (f[2] == nbrF[0] || f[2] == nbrF[1] || f[2] == nbrF[2]) + ) + { + WarningInFunction + << "triangle " << facei << " vertices " << f + << " has the same vertices as triangle " << nbrFacei + << " vertices " << nbrF + << " coords:" << f.points(surf.points()) + << endl; + + return false; + } + } + + return true; +} + + +bool Foam::triSurfaceTools::validTri +( + const MeshedSurface& surf, + const label facei +) +{ + typedef face FaceType; + const FaceType& f = surf[facei]; + + if (f.size() != 3) + { + WarningInFunction + << "face " << facei + << " is not a triangle, it has " << f.size() + << " indices" + << endl; + return false; + } + + // Simple check on indices ok. + forAll(f, fp) + { + if (f[fp] < 0 || f[fp] >= surf.points().size()) + { + WarningInFunction + << "triangle " << facei << " vertices " << f + << " uses point indices outside point range 0.." + << surf.points().size()-1 + << endl; + return false; + } + } + + if (f[0] == f[1] || f[0] == f[2] || f[1] == f[2]) + { + WarningInFunction + << "triangle " << facei + << " uses non-unique vertices " << f + << " coords:" << f.points(surf.points()) + << endl; + return false; + } + + // duplicate triangle check + + const labelList& fFaces = surf.faceFaces()[facei]; + + // Check if faceNeighbours use same points as this face. + // Note: discards normal information - sides of baffle are merged. + forAll(fFaces, i) + { + label nbrFacei = fFaces[i]; + + if (nbrFacei <= facei) + { + // lower numbered faces already checked + continue; + } + + const FaceType& nbrF = surf[nbrFacei]; + + // Same as calling triFace::compare(f, nbrF) == 1 only + if + ( + (f[0] == nbrF[0] || f[0] == nbrF[1] || f[0] == nbrF[2]) + && (f[1] == nbrF[0] || f[1] == nbrF[1] || f[1] == nbrF[2]) + && (f[2] == nbrF[0] || f[2] == nbrF[1] || f[2] == nbrF[2]) + ) + { + WarningInFunction + << "triangle " << facei << " vertices " << f + << " has the same vertices as triangle " << nbrFacei + << " vertices " << nbrF + << " coords:" << f.points(surf.points()) + << endl; + + return false; + } + } + + return true; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Tracking: // Test point on surface to see if is on face,edge or point. Foam::surfaceLocation Foam::triSurfaceTools::classify diff --git a/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.H b/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.H index 24fd96322e..46db7124cb 100644 --- a/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.H +++ b/src/meshTools/triSurface/triSurfaceTools/triSurfaceTools.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -50,11 +50,14 @@ namespace Foam { // Forward declaration of classes -class triSurface; class edge; class labelledTri; class polyBoundaryMesh; class plane; +class triSurface; +class face; +template class MeshedSurface; + /*---------------------------------------------------------------------------*\ Class triSurfaceTools Declaration @@ -516,6 +519,15 @@ public: static triSurface delaunay2D(const List&); + // Surface checking functionality + + //- Check single triangle for (topological) validity + static bool validTri(const triSurface&, const label facei); + + //- Check single triangle for (topological) validity + static bool validTri(const MeshedSurface&, const label facei); + + // Tracking //- Test point on plane of triangle to see if on edge or point or inside diff --git a/src/sampling/sampledSurface/isoSurface/isoSurface.C b/src/sampling/sampledSurface/isoSurface/isoSurface.C index dc93fd1684..d5d8b7fbde 100644 --- a/src/sampling/sampledSurface/isoSurface/isoSurface.C +++ b/src/sampling/sampledSurface/isoSurface/isoSurface.C @@ -33,6 +33,7 @@ License #include "OFstream.H" #include "meshTools.H" #include "triSurfaceSearch.H" +#include "triSurfaceTools.H" #include "surfaceIntersection.H" #include "intersectedSurface.H" #include "searchableBox.H" @@ -1257,76 +1258,6 @@ void Foam::isoSurface::trimToBox } -bool Foam::isoSurface::validTri(const triSurface& surf, const label facei) -{ - // Simple check on indices ok. - - const labelledTri& f = surf[facei]; - - if - ( - (f[0] < 0) || (f[0] >= surf.points().size()) - || (f[1] < 0) || (f[1] >= surf.points().size()) - || (f[2] < 0) || (f[2] >= surf.points().size()) - ) - { - WarningInFunction - << "triangle " << facei << " vertices " << f - << " uses point indices outside point range 0.." - << surf.points().size()-1 << endl; - - return false; - } - - if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2])) - { - WarningInFunction - << "triangle " << facei - << " uses non-unique vertices " << f - << endl; - return false; - } - - // duplicate triangle check - - const labelList& fFaces = surf.faceFaces()[facei]; - - // Check if faceNeighbours use same points as this face. - // Note: discards normal information - sides of baffle are merged. - forAll(fFaces, i) - { - label nbrFacei = fFaces[i]; - - if (nbrFacei <= facei) - { - // lower numbered faces already checked - continue; - } - - const labelledTri& nbrF = surf[nbrFacei]; - - if - ( - ((f[0] == nbrF[0]) || (f[0] == nbrF[1]) || (f[0] == nbrF[2])) - && ((f[1] == nbrF[0]) || (f[1] == nbrF[1]) || (f[1] == nbrF[2])) - && ((f[2] == nbrF[0]) || (f[2] == nbrF[1]) || (f[2] == nbrF[2])) - ) - { - WarningInFunction - << "triangle " << facei << " vertices " << f - << " fc:" << f.centre(surf.points()) - << " has the same vertices as triangle " << nbrFacei - << " vertices " << nbrF - << " fc:" << nbrF.centre(surf.points()) - << endl; - - return false; - } - } - return true; -} - - Foam::triSurface Foam::isoSurface::subsetMesh ( const triSurface& s, @@ -1756,8 +1687,7 @@ Foam::isoSurface::isoSurface forAll(*this, triI) { - // Copied from surfaceCheck - validTri(*this, triI); + triSurfaceTools::validTri(*this, triI); } fileName stlFile = mesh_.time().path() + ".stl"; diff --git a/src/sampling/sampledSurface/isoSurface/isoSurface.H b/src/sampling/sampledSurface/isoSurface/isoSurface.H index 267cc04c90..e947405566 100644 --- a/src/sampling/sampledSurface/isoSurface/isoSurface.H +++ b/src/sampling/sampledSurface/isoSurface/isoSurface.H @@ -377,9 +377,6 @@ class isoSurface List>& interpolationWeights ); - //- Check single triangle for (topological) validity - static bool validTri(const triSurface&, const label facei); - static triSurface subsetMesh ( const triSurface& s, diff --git a/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.C b/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.C index 9771659586..47ec36069f 100644 --- a/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.C +++ b/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.C @@ -29,6 +29,7 @@ License #include "mergePoints.H" #include "tetMatcher.H" #include "syncTools.H" +#include "triSurfaceTools.H" #include "addToRunTimeSelectionTable.H" #include "Time.H" #include "triPoints.H" @@ -1049,73 +1050,6 @@ Foam::triSurface Foam::isoSurfaceCell::stitchTriPoints } -bool Foam::isoSurfaceCell::validTri(const triSurface& surf, const label facei) -{ - // Simple check on indices ok. - - const labelledTri& f = surf[facei]; - - forAll(f, fp) - { - if (f[fp] < 0 || f[fp] >= surf.points().size()) - { - WarningInFunction - << "triangle " << facei << " vertices " << f - << " uses point indices outside point range 0.." - << surf.points().size()-1 << endl; - - return false; - } - } - - if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2])) - { - WarningInFunction - << "triangle " << facei - << " uses non-unique vertices " << f - << endl; - return false; - } - - // duplicate triangle check - - const labelList& fFaces = surf.faceFaces()[facei]; - - // Check if faceNeighbours use same points as this face. - // Note: discards normal information - sides of baffle are merged. - forAll(fFaces, i) - { - label nbrFacei = fFaces[i]; - - if (nbrFacei <= facei) - { - // lower numbered faces already checked - continue; - } - - const labelledTri& nbrF = surf[nbrFacei]; - - if - ( - ((f[0] == nbrF[0]) || (f[0] == nbrF[1]) || (f[0] == nbrF[2])) - && ((f[1] == nbrF[0]) || (f[1] == nbrF[1]) || (f[1] == nbrF[2])) - && ((f[2] == nbrF[0]) || (f[2] == nbrF[1]) || (f[2] == nbrF[2])) - ) - { - WarningInFunction - << "triangle " << facei << " vertices " << f - << " coords:" << f.points(surf.points()) - << " has the same vertices as triangle " << nbrFacei - << " vertices " << nbrF - << endl; - - return false; - } - } - return true; -} - - void Foam::isoSurfaceCell::calcAddressing ( const triSurface& surf, @@ -1569,8 +1503,7 @@ Foam::isoSurfaceCell::isoSurfaceCell forAll(*this, triI) { - // Copied from surfaceCheck - validTri(*this, triI); + triSurfaceTools::validTri(*this, triI); } } diff --git a/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.H b/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.H index ec496319ec..f848cc4736 100644 --- a/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.H +++ b/src/sampling/sampledSurface/isoSurface/isoSurfaceCell.H @@ -272,9 +272,6 @@ class isoSurfaceCell labelList& triMap // merged to unmerged triangle ) const; - //- Check single triangle for (topological) validity - static bool validTri(const triSurface&, const label); - //- Determine edge-face addressing void calcAddressing (