/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "repatchMesh.H"
#include "Time.H"
#include "polyMesh.H"
#include "repatcher.H"
#include "faceList.H"
#include "indexedOctree.H"
#include "treeDataPrimitivePatch.H"
#include "triSurface.H"
#include "SortableList.H"
#include "OFstream.H"
#include "uindirectPrimitivePatch.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(repatchMesh, 0);
// Normal along which to divide faces into categories (used in getNearest)
const vector repatchMesh::splitNormal_(3, 2, 1);
// Distance to face tolerance for getNearest
const scalar repatchMesh::distanceTol_ = 1e-2;
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// Returns number of feature edges connected to pointi
Foam::label Foam::repatchMesh::nFeatureEdges(label pointi) const
{
label nFeats = 0;
const labelList& pEdges = mesh().pointEdges()[pointi];
forAll(pEdges, pEdgeI)
{
label edgeI = pEdges[pEdgeI];
if (edgeToFeature_[edgeI] != -1)
{
nFeats++;
}
}
return nFeats;
}
// Returns next feature edge connected to pointi
Foam::label Foam::repatchMesh::nextFeatureEdge
(
const label edgeI,
const label vertI
) const
{
const labelList& pEdges = mesh().pointEdges()[vertI];
forAll(pEdges, pEdgeI)
{
label nbrEdgeI = pEdges[pEdgeI];
if (nbrEdgeI != edgeI)
{
label featI = edgeToFeature_[nbrEdgeI];
if (featI != -1)
{
return nbrEdgeI;
}
}
}
return -1;
}
// Finds connected feature edges, starting from startPointi and returns
// feature labels (not edge labels). Marks feature edges handled in
// featVisited.
Foam::labelList Foam::repatchMesh::collectSegment
(
const boolList& isFeaturePoint,
const label startEdgeI,
boolList& featVisited
) const
{
// Find starting feature point on edge.
label edgeI = startEdgeI;
const edge& e = mesh().edges()[edgeI];
label vertI = e.start();
while (!isFeaturePoint[vertI])
{
// Step to next feature edge
edgeI = nextFeatureEdge(edgeI, vertI);
if ((edgeI == -1) || (edgeI == startEdgeI))
{
break;
}
// Step to next vertex on edge
const edge& e = mesh().edges()[edgeI];
vertI = e.otherVertex(vertI);
}
//
// Now we have:
// edgeI : first edge on this segment
// vertI : one of the endpoints of this segment
//
// Start walking other way and storing edges as we go along.
//
// Untrimmed storage for current segment
labelList featLabels(featureEdges_.size());
label featLabelI = 0;
label initEdgeI = edgeI;
do
{
// Mark edge as visited
label featI = edgeToFeature_[edgeI];
if (featI == -1)
{
FatalErrorInFunction
<< "Problem" << abort(FatalError);
}
featLabels[featLabelI++] = featI;
featVisited[featI] = true;
// Step to next vertex on edge
const edge& e = mesh().edges()[edgeI];
vertI = e.otherVertex(vertI);
// Step to next feature edge
edgeI = nextFeatureEdge(edgeI, vertI);
if ((edgeI == -1) || (edgeI == initEdgeI))
{
break;
}
}
while (!isFeaturePoint[vertI]);
// Trim to size
featLabels.setSize(featLabelI);
return featLabels;
}
// Gets labels of changed faces and propagates them to the edges. Returns
// labels of edges changed.
Foam::labelList Foam::repatchMesh::faceToEdge
(
const boolList& regionEdge,
const label region,
const labelList& changedFaces,
labelList& edgeRegion
) const
{
labelList changedEdges(mesh().nEdges(), -1);
label changedI = 0;
forAll(changedFaces, i)
{
label facei = changedFaces[i];
const labelList& fEdges = mesh().faceEdges()[facei];
forAll(fEdges, fEdgeI)
{
label edgeI = fEdges[fEdgeI];
if (!regionEdge[edgeI] && (edgeRegion[edgeI] == -1))
{
edgeRegion[edgeI] = region;
changedEdges[changedI++] = edgeI;
}
}
}
changedEdges.setSize(changedI);
return changedEdges;
}
// Reverse of faceToEdge: gets edges and returns faces
Foam::labelList Foam::repatchMesh::edgeToFace
(
const label region,
const labelList& changedEdges,
labelList& faceRegion
) const
{
labelList changedFaces(mesh().size(), -1);
label changedI = 0;
forAll(changedEdges, i)
{
label edgeI = changedEdges[i];
const labelList& eFaces = mesh().edgeFaces()[edgeI];
forAll(eFaces, eFacei)
{
label facei = eFaces[eFacei];
if (faceRegion[facei] == -1)
{
faceRegion[facei] = region;
changedFaces[changedI++] = facei;
}
}
}
changedFaces.setSize(changedI);
return changedFaces;
}
// Finds area, starting at facei, delimited by borderEdge
void Foam::repatchMesh::markZone
(
const boolList& borderEdge,
label facei,
label currentZone,
labelList& faceZone
) const
{
faceZone[facei] = currentZone;
// List of faces whose faceZone has been set.
labelList changedFaces(1, facei);
// List of edges whose faceZone has been set.
labelList changedEdges;
// Zones on all edges.
labelList edgeZone(mesh().nEdges(), -1);
while (true)
{
changedEdges = faceToEdge
(
borderEdge,
currentZone,
changedFaces,
edgeZone
);
if (debug)
{
Pout<< "From changedFaces:" << changedFaces.size()
<< " to changedEdges:" << changedEdges.size()
<< endl;
}
if (changedEdges.empty())
{
break;
}
changedFaces = edgeToFace(currentZone, changedEdges, faceZone);
if (debug)
{
Pout<< "From changedEdges:" << changedEdges.size()
<< " to changedFaces:" << changedFaces.size()
<< endl;
}
if (changedFaces.empty())
{
break;
}
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Null constructor
Foam::repatchMesh::repatchMesh()
:
meshPtr_(nullptr),
patches_(),
meshFace_(),
featurePoints_(),
featureEdges_(),
featureToEdge_(),
edgeToFeature_(),
featureSegments_()
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::repatchMesh::~repatchMesh()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::repatchMesh::read(const polyMesh& mesh)
{
patches_.clear();
patches_.setSize(mesh.boundary().size());
// Number of boundary faces
label nBFaces = mesh.nFaces() - mesh.nInternalFaces();
faceList bFaces(nBFaces);
meshFace_.setSize(nBFaces);
label bFacei = 0;
// Collect all boundary faces.
forAll(mesh.boundary(), patchi)
{
const polyPatch& pp = mesh.boundary()[patchi];
patches_.set
(
patchi,
new repatchPatch
(
pp.name(),
patchi,
pp.size(),
bFacei,
pp.type()
)
);
// Collect all faces in global numbering.
forAll(pp, patchFacei)
{
meshFace_[bFacei] = pp.start() + patchFacei;
bFaces[bFacei] = pp[patchFacei];
bFacei++;
}
}
if (debug)
{
Pout<< "read : patches now:" << endl;
forAll(patches_, patchi)
{
const repatchPatch& bp = patches_[patchi];
Pout<< " name : " << bp.name() << endl
<< " size : " << bp.size() << endl
<< " start : " << bp.start() << endl
<< " type : " << bp.physicalType() << endl
<< endl;
}
}
//
// Construct single patch for all of boundary
//
// Temporary primitivePatch to calculate compact points & faces.
PrimitivePatch globalPatch
(
bFaces,
mesh.points()
);
// Store in local(compact) addressing
meshPtr_ = new rMesh(globalPatch.localFaces(), globalPatch.localPoints());
if (debug & 2)
{
const rMesh& msh = *meshPtr_;
Pout<< "** Start of Faces **" << endl;
forAll(msh, facei)
{
const face& f = msh[facei];
point ctr(Zero);
forAll(f, fp)
{
ctr += msh.points()[f[fp]];
}
ctr /= f.size();
Pout<< " " << facei
<< " ctr:" << ctr
<< " verts:" << f
<< endl;
}
Pout<< "** End of Faces **" << endl;
Pout<< "** Start of Points **" << endl;
forAll(msh.points(), pointi)
{
Pout<< " " << pointi
<< " coord:" << msh.points()[pointi]
<< endl;
}
Pout<< "** End of Points **" << endl;
}
// Clear edge storage
featurePoints_.setSize(0);
featureEdges_.setSize(0);
featureToEdge_.setSize(0);
edgeToFeature_.setSize(meshPtr_->nEdges());
edgeToFeature_ = -1;
featureSegments_.setSize(0);
}
void Foam::repatchMesh::readTriSurface(const fileName& fName)
{
triSurface surf(fName);
if (surf.empty())
{
return;
}
// Sort according to region
SortableList