/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2021-2025 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . \*---------------------------------------------------------------------------*/ #include "polygonTriangulate.H" #include "tensor2D.H" // * * * * * * * * * * * Private Static Member Functions * * * * * * * * * * // template Foam::scalar Foam::polygonTriangulate::area ( const triFace& triPoints, const PointField& points, const vector& normal ) { const point& o = points[triPoints[0]]; const vector ao = points[triPoints[1]] - o; const vector ab = points[triPoints[2]] - o; return (ao ^ ab) & normal; } template Foam::scalar Foam::polygonTriangulate::quality ( const triFace& triPoints, const PointField& points, const vector& normal ) { const scalar An = area(triPoints, points, normal); scalar PSqr = 0; forAll(triPoints, triEdgei) { const point& p0 = points[triPoints[triEdgei]]; const point& p1 = points[triPoints[(triEdgei + 1) % 3]]; PSqr += magSqr(p0 - p1); } static const scalar equilateralAnByPSqr = sqrt(scalar(3))/12; return PSqr != 0 ? An/PSqr/equilateralAnByPSqr : 0; } template bool Foam::polygonTriangulate::intersection ( const edge& edgePointsA, const edge& edgePointsB, const PointField& points, const vector& normal ) { const point& pointA0 = points[edgePointsA[0]]; const point& pointA1 = points[edgePointsA[1]]; const point& pointB0 = points[edgePointsB[0]]; const point& pointB1 = points[edgePointsB[1]]; // Bounding sphere-based rejection for problematic co-linear cases const point centreA = (pointA0 + pointA1)/2; const point centreB = (pointB0 + pointB1)/2; const scalar radiusSqrA = magSqr((pointA0 - pointA1)/2); const scalar radiusSqrB = magSqr((pointB0 - pointB1)/2); if (magSqr(centreA - centreB) > radiusSqrA + radiusSqrB) return false; // Solve for the edge-local coordinates of the intersection. If these are // both in the range 0 -> 1 then the edges intersect. const vector tau0 = perpendicular(normal); const vector tau1 = normal ^ tau0; const point2D point2DA0(tau0 & pointA0, tau1 & pointA0); const point2D point2DA1(tau0 & pointA1, tau1 & pointA1); const point2D point2DB0(tau0 & pointB0, tau1 & pointB0); const point2D point2DB1(tau0 & pointB1, tau1 & pointB1); const tensor2D M = tensor2D(point2DA0 - point2DA1, point2DB1 - point2DB0).T(); const scalar detM = det(M); const tensor2D detMInvM = cof(M); const vector2D magDetMTu = sign(detM)*detMInvM & (point2DA0 - point2DB0); return 0 <= cmptMin(magDetMTu) && cmptMax(magDetMTu) <= mag(detM); } template Foam::label Foam::polygonTriangulate::nIntersections ( const edge& edgePoints, const PointField& points, const vector& normal ) { label n = 0; forAll(points, pi) { const edge otherEdgePoints(pi, points.fcIndex(pi)); if (edgePoints.commonVertex(otherEdgePoints) == -1) { n += intersection(edgePoints, otherEdgePoints, points, normal); } } return n; } template Foam::scalar Foam::polygonTriangulate::angle ( const label pointi, const PointField& points, const vector& normal ) { const point& o = points[pointi]; const vector oa = points[points.rcIndex(pointi)] - o; const vector ob = points[points.fcIndex(pointi)] - o; const vector oaNegNNOa = oa - normal*(normal & oa); const vector obNegNNOb = ob - normal*(normal & ob); return atan2(normal & (oaNegNNOa ^ obNegNNOb), - oaNegNNOa & obNegNNOb) + constant::mathematical::pi; } template bool Foam::polygonTriangulate::ear ( const label pointi, const PointField& points, const vector& normal ) { const point& o = points[pointi]; const vector oa = points[points.rcIndex(pointi)] - o; const vector ob = points[points.fcIndex(pointi)] - o; const tensor A = tensor(ob, oa, normal).T(); const tensor T(A.y() ^ A.z(), A.z() ^ A.x(), A.x() ^ A.y()); const scalar detA = det(A); for ( label pointj = points.fcIndex(points.fcIndex(pointi)); pointj != points.rcIndex(pointi); pointj = points.fcIndex(pointj) ) { const vector detAY = (points[pointj] - o) & T; if (detAY.x() > 0 && detAY.y() > 0 && detAY.x() + detAY.y() < detA) { return false; } } return true; } // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // Foam::List Foam::polygonTriangulate::randomPolygon ( randomGenerator& rndGen, const label n, const scalar error ) { // Get random points on a unit disk List points(n); forAll(points, pointi) { const scalar theta = 2*constant::mathematical::pi*rndGen.sample01(); const scalar r = sqrt(rndGen.sample01()); points[pointi] = point(r*cos(theta), r*sin(theta), 0); } // Initialise intersected polygon labelList pointis(identityMap(n)); // Reorder until non-intersected bool incomplete = true; while (incomplete) { incomplete = false; for (label piA0 = 0; piA0 < n; ++ piA0) { const label piA1 = points.fcIndex(piA0); for (label piB0 = piA0 + 2; piB0 < (piA0 == 0 ? n - 1 : n); ++ piB0) { const label piB1 = points.fcIndex(piB0); if ( intersection ( edge(pointis[piA0], pointis[piA1]), edge(pointis[piB0], pointis[piB1]), points, vector(0, 0, 1) ) ) { SubList