/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2026 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 "edgeIntersections.H"
#include "triSurfaceSearch.H"
#include "labelPairLookup.H"
#include "OFstream.H"
#include "HashSet.H"
#include "triSurface.H"
#include "pointIndexHit.H"
#include "treeDataTriSurface.H"
#include "indexedOctree.H"
#include "meshTools.H"
#include "plane.H"
#include "randomGenerator.H"
#include "units.H"
#include "treeBoundBox.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(edgeIntersections, 0);
scalar edgeIntersections::alignedCos_ = cos(degToRad(89.0));
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::edgeIntersections::checkEdges(const triSurface& surf)
{
const pointField& localPoints = surf.localPoints();
const edgeList& edges = surf.edges();
const labelListList& edgeFaces = surf.edgeFaces();
treeBoundBox bb(localPoints);
scalar minSize = small * bb.minDim();
forAll(edges, edgeI)
{
const edge& e = edges[edgeI];
scalar eMag = e.mag(localPoints);
if (eMag < minSize)
{
WarningInFunction
<< "Edge " << edgeI << " vertices " << e
<< " coords:" << localPoints[e[0]] << ' '
<< localPoints[e[1]] << " is very small compared to bounding"
<< " box dimensions " << bb << endl
<< "This might lead to problems in intersection"
<< endl;
}
if (edgeFaces[edgeI].size() == 1)
{
WarningInFunction
<< "Edge " << edgeI << " vertices " << e
<< " coords:" << localPoints[e[0]] << ' '
<< localPoints[e[1]] << " has only one face connected to it:"
<< edgeFaces[edgeI] << endl
<< "This might lead to problems in intersection"
<< endl;
}
}
}
// Update intersections for selected edges.
void Foam::edgeIntersections::intersectEdges
(
const triSurface& surf1,
const pointField& points1, // surf1 meshPoints (not localPoints!)
const triSurfaceSearch& querySurf2,
const scalarField& surf1PointTol, // surf1 tolerance per point
const labelList& edgeLabels
)
{
const triSurface& surf2 = querySurf2.surface();
const vectorField& normals2 = surf2.faceNormals();
const labelList& meshPoints = surf1.meshPoints();
if (debug)
{
Pout<< "Calculating intersection of " << edgeLabels.size() << " edges"
<< " out of " << surf1.nEdges() << " with " << surf2.size()
<< " triangles ..." << endl;
}
pointField start(edgeLabels.size());
pointField end(edgeLabels.size());
vectorField edgeDirs(edgeLabels.size());
// Go through all edges, calculate intersections
forAll(edgeLabels, i)
{
label edgeI = edgeLabels[i];
if (debug)// && (i % 1000 == 0))
{
Pout<< "Intersecting edge " << edgeI << " with surface" << endl;
}
const edge& e = surf1.edges()[edgeI];
const point& pStart = points1[meshPoints[e.start()]];
const point& pEnd = points1[meshPoints[e.end()]];
const vector eVec(pEnd - pStart);
const vector n(eVec/(mag(eVec) + vSmall));
// Start tracking somewhat before pStart and up to somewhat after p1.
// Note that tolerances here are smaller than those used to classify
// hit below.
// This will cause this hit to be marked as degenerate and resolved
// later on.
start[i] = pStart - 0.5*surf1PointTol[e[0]]*n;
end[i] = pEnd + 0.5*surf1PointTol[e[1]]*n;
edgeDirs[i] = n;
}
List> edgeIntersections;
querySurf2.findLineAll
(
start,
end,
edgeIntersections
);
label nHits = 0;
// Classify the hits
forAll(edgeLabels, i)
{
const label edgeI = edgeLabels[i];
labelList& intersectionTypes = classification_[edgeI];
intersectionTypes.setSize(edgeIntersections[i].size(), -1);
this->operator[](edgeI).transfer(edgeIntersections[i]);
forAll(intersectionTypes, hitI)
{
const pointIndexHit& pHit = this->operator[](edgeI)[hitI];
label& hitType = intersectionTypes[hitI];
if (!pHit.hit())
{
continue;
}
const edge& e = surf1.edges()[edgeI];
// Classify point on surface1 edge.
if (mag(pHit.hitPoint() - start[i]) < surf1PointTol[e[0]])
{
// Intersection is close to edge start
hitType = 0;
}
else if (mag(pHit.hitPoint() - end[i]) < surf1PointTol[e[1]])
{
// Intersection is close to edge end
hitType = 1;
}
else if (mag(edgeDirs[i] & normals2[pHit.index()]) < alignedCos_)
{
// Edge is almost coplanar with the face
Pout<< "Flat angle edge:" << edgeI
<< " face:" << pHit.index()
<< " cos:" << mag(edgeDirs[i] & normals2[pHit.index()])
<< endl;
hitType = 2;
}
if (debug)
{
Info<< " hit " << pHit << " classify = " << hitType << endl;
}
nHits++;
}
}
if (debug)
{
Pout<< "Found " << nHits << " intersections of edges with surface ..."
<< endl;
}
}
// If edgeI intersections are close to endpoint of edge shift endpoints
// slightly along edge
// Updates
// - points1 with new endpoint position
// - affectedEdges with all edges affected by moving the point
// Returns true if changed anything.
bool Foam::edgeIntersections::inlinePerturb
(
const triSurface& surf1,
const scalarField& surf1PointTol, // surf1 tolerance per point
const label edgeI,
randomGenerator& rndGen,
pointField& points1,
boolList& affectedEdges
) const
{
bool hasPerturbed = false;
// Check if edge close to endpoint. Note that we only have to check
// the intersection closest to the edge endpoints (i.e. first and last in
// edgeEdgs)
const labelList& edgeEnds = classification_[edgeI];
if (edgeEnds.size())
{
bool perturbStart = false;
bool perturbEnd = false;
// Check first intersection.
if (edgeEnds.first() == 0)
{
perturbStart = true;
}
if (edgeEnds.last() == 1)
{
perturbEnd = true;
}
if (perturbStart || perturbEnd)
{
const edge& e = surf1.edges()[edgeI];
label v0 = surf1.meshPoints()[e[0]];
label v1 = surf1.meshPoints()[e[1]];
vector eVec(points1[v1] - points1[v0]);
vector n = eVec/mag(eVec);
if (perturbStart)
{
// Perturb with something (hopefully) larger than tolerance.
scalar t = 4.0*(rndGen.scalar01() - 0.5);
points1[v0] += t*surf1PointTol[e[0]]*n;
const labelList& pEdges = surf1.pointEdges()[e[0]];
forAll(pEdges, i)
{
affectedEdges[pEdges[i]] = true;
}
}
if (perturbEnd)
{
// Perturb with something larger than tolerance.
scalar t = 4.0*(rndGen.scalar01() - 0.5);
points1[v1] += t*surf1PointTol[e[1]]*n;
const labelList& pEdges = surf1.pointEdges()[e[1]];
forAll(pEdges, i)
{
affectedEdges[pEdges[i]] = true;
}
}
hasPerturbed = true;
}
}
return hasPerturbed;
}
// Perturb single edge endpoint when perpendicular to face
bool Foam::edgeIntersections::rotatePerturb
(
const triSurface& surf1,
const scalarField& surf1PointTol, // surf1 tolerance per point
const label edgeI,
randomGenerator& rndGen,
pointField& points1,
boolList& affectedEdges
) const
{
const labelList& meshPoints = surf1.meshPoints();
const labelList& edgeEnds = classification_[edgeI];
bool hasPerturbed = false;
forAll(edgeEnds, i)
{
if (edgeEnds[i] == 2)
{
const edge& e = surf1.edges()[edgeI];
// Endpoint to modify. Choose either start or end.
label pointi = e[rndGen.sampleAB