Commit 6309810e authored by Mark Olesen's avatar Mark Olesen
Browse files

surfMesh reworked

- treat 'ofs' IO just like any other format
- dropped BasicMeshedSurface, since MeshedSurface can now also have zero or
  more zones
- UnsortedMeshedSurface is a special type of MeshedSurface with zero zones,
  but with additional zoneId labels
- use MeshedSurfaceProxy for writing surfaces with points/faces/zones and
  optional faceMap - provides output interface for MeshedSurface,
  UnsortedMeshedSurface and surfMesh.
- simplify output to filenames only, I can't see that the Ostream
  is needed anywhere
- surfMesh renaming now works, after the objectRegistry fix
parent b968e62e
......@@ -271,13 +271,40 @@ int main(int argc, char *argv[])
args.caseName()
);
// start with "constant"
runTime.setTime(instant(0, runTime.constant()), 0);
Info<< "runTime.instance() = " << runTime.instance() << endl;
Info<< "runTime.timeName() = " << runTime.timeName() << endl;
surfMesh surfIn
(
IOobject
(
"default",
runTime.timeName(),
runTime,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);
Info<< "surfIn = " << surfIn.nFaces() << endl;
Info<< "runTime.instance() = " << runTime.instance() << endl;
surfMesh surfOut
(
IOobject
(
"mySurf",
runTime.instance(),
runTime
runTime,
IOobject::NO_READ,
IOobject::NO_WRITE,
false
),
surf.xfer()
);
......@@ -299,6 +326,25 @@ int main(int argc, char *argv[])
dimless
);
Info<<" surf name= " << surfOut.name() <<nl;
Info<< "rename to anotherSurf" << endl;
surfOut.rename("anotherSurf");
Info<<" surf name= " << surfOut.name() <<nl;
// advance time to 1
runTime.setTime(instant(1), 1);
surfOut.setInstance(runTime.timeName());
Info<< "writing surfMesh again well: " << surfOut.objectPath() << endl;
surfOut.write();
// write directly
surfOut.write("someName.ofs");
#if 1
const surfZoneList& zones = surfOut.surfZones();
forAll(zones, zoneI)
{
......@@ -318,9 +364,10 @@ int main(int argc, char *argv[])
(
IOobject
(
"pointIds",
"zoneIds.",
// "pointIds",
surfOut.instance(),
"pointFields",
// "pointFields",
surfOut,
IOobject::NO_READ,
IOobject::NO_WRITE
......@@ -337,6 +384,10 @@ int main(int argc, char *argv[])
Info<< "write pointIds (for testing only): "
<< pointIds.objectPath() << endl;
pointIds.write();
Info<<"surfMesh with these names: " << surfOut.names() << endl;
#endif
}
}
......
......@@ -62,10 +62,10 @@ class primitiveMesh;
class cuttingPlane
:
public plane,
public BasicMeshedSurface<face>
public MeshedSurface<face>
{
//- Private typedefs for convenience
typedef BasicMeshedSurface<face> MeshStorage;
typedef MeshedSurface<face> MeshStorage;
// Private data
......
......@@ -50,11 +50,11 @@ namespace Foam
class sampledPatch
:
public BasicMeshedSurface<face>,
public MeshedSurface<face>,
public sampledSurface
{
//- Private typedefs for convenience
typedef BasicMeshedSurface<face> MeshStorage;
typedef MeshedSurface<face> MeshStorage;
// Private data
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "BasicMeshedSurface.H"
#include "boundBox.H"
#include "mergePoints.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Face>
inline bool Foam::BasicMeshedSurface<Face>::isTri()
{
return false;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Face>
Foam::BasicMeshedSurface<Face>::BasicMeshedSurface()
:
ParentType(List<Face>(), pointField())
{}
template<class Face>
Foam::BasicMeshedSurface<Face>::BasicMeshedSurface
(
const Xfer< pointField >& pointLst,
const Xfer< List<Face> >& faceLst
)
:
ParentType(List<Face>(), pointField())
{
reset(pointLst, faceLst);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Face>
Foam::BasicMeshedSurface<Face>::~BasicMeshedSurface()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Face>
void Foam::BasicMeshedSurface<Face>::clear()
{
ParentType::clearOut();
storedPoints().clear();
storedFaces().clear();
}
template<class Face>
void Foam::BasicMeshedSurface<Face>::movePoints(const pointField& newPoints)
{
// Remove all geometry dependent data
ParentType::clearTopology();
// Adapt for new point position
ParentType::movePoints(newPoints);
// Copy new points
storedPoints() = newPoints;
}
template<class Face>
void Foam::BasicMeshedSurface<Face>::scalePoints(const scalar& scaleFactor)
{
// avoid bad scaling
if (scaleFactor > 0 && scaleFactor != 1.0)
{
// Remove all geometry dependent data
ParentType::clearTopology();
// Adapt for new point position
ParentType::movePoints(pointField());
storedPoints() *= scaleFactor;
}
}
template<class Face>
void Foam::BasicMeshedSurface<Face>::reset
(
const Xfer< pointField >& pointLst,
const Xfer< List<Face> >& faceLst
)
{
ParentType::clearOut();
// Take over new primitive data.
// Optimized to avoid overwriting data at all
if (&pointLst)
{
storedPoints().transfer(pointLst());
}
if (&faceLst)
{
storedFaces().transfer(faceLst());
}
}
template<class Face>
void Foam::BasicMeshedSurface<Face>::reset
(
const Xfer< List<point> >& pointLst,
const Xfer< List<Face> >& faceLst
)
{
ParentType::clearOut();
// Take over new primitive data.
// Optimized to avoid overwriting data at all
if (&pointLst)
{
storedPoints().transfer(pointLst());
}
if (&faceLst)
{
storedFaces().transfer(faceLst());
}
}
// Remove badly degenerate faces, double faces.
template<class Face>
void Foam::BasicMeshedSurface<Face>::cleanup(const bool verbose)
{
// merge points (already done for STL, TRI)
stitchFaces(SMALL, verbose);
checkFaces(verbose);
this->checkTopology(verbose);
}
template<class Face>
bool Foam::BasicMeshedSurface<Face>::stitchFaces
(
const scalar tol,
const bool verbose
)
{
pointField& pointLst = this->storedPoints();
// Merge points
labelList pointMap(pointLst.size());
pointField newPoints(pointLst.size());
bool hasMerged = mergePoints(pointLst, tol, verbose, pointMap, newPoints);
if (!hasMerged)
{
return false;
}
if (verbose)
{
Info<< "BasicMeshedSurface::stitchFaces : Renumbering all faces"
<< endl;
}
// Set the coordinates to the merged ones
pointLst.transfer(newPoints);
List<Face>& faceLst = this->storedFaces();
List<label> faceMap(faceLst.size());
// Reset the point labels to the unique points array
label newFaceI = 0;
forAll(faceLst, faceI)
{
Face& f = faceLst[faceI];
forAll(f, fp)
{
f[fp] = pointMap[f[fp]];
}
// for extra safety: collapse face as well
if (f.collapse() >= 3)
{
if (newFaceI != faceI)
{
faceLst[newFaceI] = f;
}
faceMap[newFaceI] = faceI;
newFaceI++;
}
else if (verbose)
{
Pout<< "BasicMeshedSurface::stitchFaces : "
<< "Removing collapsed face " << faceI << endl
<< " vertices :" << f << endl;
}
}
pointMap.clear();
if (newFaceI != faceLst.size())
{
if (verbose)
{
Pout<< "BasicMeshedSurface::stitchFaces : "
<< "Removed " << faceLst.size() - newFaceI
<< " faces" << endl;
}
faceLst.setSize(newFaceI);
remapFaces(faceMap);
}
faceMap.clear();
// Merging points might have changed geometric factors
ParentType::clearOut();
return true;
}
// Remove badly degenerate faces and double faces.
template<class Face>
bool Foam::BasicMeshedSurface<Face>::checkFaces
(
const bool verbose
)
{
bool changed = false;
List<Face>& faceLst = this->storedFaces();
List<label> faceMap(faceLst.size());
label newFaceI = 0;
// Detect badly labelled faces and mark degenerate faces
const label maxPointI = this->points().size() - 1;
forAll(faceLst, faceI)
{
Face& f = faceLst[faceI];
// avoid degenerate faces
if (f.collapse() >= 3)
{
forAll(f, fp)
{
if (f[fp] < 0 || f[fp] > maxPointI)
{
FatalErrorIn("BasicMeshedSurface::checkFaces(bool)")
<< "face " << f
<< " uses point indices outside point range 0.."
<< maxPointI
<< exit(FatalError);
}
}
faceMap[faceI] = faceI;
newFaceI++;
}
else
{
// mark as bad face
faceMap[faceI] = -1;
changed = true;
if (verbose)
{
WarningIn
(
"BasicMeshedSurface::checkFaces(bool verbose)"
) << "face[" << faceI << "] = " << f
<< " does not have three unique vertices" << endl;
}
}
}
// Detect doubled faces
// do not touch the faces
const labelListList& fFaces = this->faceFaces();
newFaceI = 0;
forAll(faceLst, faceI)
{
// skip already collapsed faces:
if (faceMap[faceI] < 0)
{
continue;
}
const Face& f = faceLst[faceI];
// duplicate face check
bool okay = true;
const labelList& neighbours = fFaces[faceI];
// Check if faceNeighbours use same points as this face.
// Note: discards normal information - sides of baffle are merged.
forAll(neighbours, neighI)
{
const label neiFaceI = neighbours[neighI];
if (neiFaceI <= faceI || faceMap[neiFaceI] < 0)
{
// lower numbered faces already checked
// skip neighbours that are themselves collapsed
continue;
}
const Face& nei = faceLst[neiFaceI];
if (f == nei)
{
okay = false;
if (verbose)
{
WarningIn
(
"BasicMeshedSurface::checkFaces(bool verbose)"
) << "faces share the same vertices:" << nl
<< " face[" << faceI << "] : " << f << nl
<< " face[" << neiFaceI << "] : " << nei << endl;
// printFace(Warning, " ", f, points());
// printFace(Warning, " ", nei, points());
}
break;
}
}
if (okay)
{
faceMap[faceI] = faceI;
newFaceI++;
}
else
{
faceMap[faceI] = -1;
}
}
// Phase 1: pack
// Done to keep numbering constant in phase 1
if (changed || newFaceI < faceLst.size())
{
changed = true;
if (verbose)
{
WarningIn
(
"BasicMeshedSurface::checkFaces(bool verbose)"
) << "Removed " << faceLst.size() - newFaceI
<< " illegal faces." << endl;
}
// compress the face list
newFaceI = 0;
forAll(faceLst, faceI)
{
if (faceMap[faceI] >= 0)
{
if (newFaceI != faceI)
{
faceLst[newFaceI] = faceLst[faceI];
}
faceMap[newFaceI] = faceI;
newFaceI++;
}
}
faceLst.setSize(newFaceI);
remapFaces(faceMap);
}
faceMap.clear();
// Topology can change because of renumbering
ParentType::clearOut();
return changed;
}
template<class Face>
Foam::label Foam::BasicMeshedSurface<Face>::triangulate()
{
return triangulate
(
const_cast<List<label>&>(List<label>::null())
);
}
template<class Face>
Foam::label Foam::BasicMeshedSurface<Face>::triangulate
(
List<label>& faceMapOut
)
{
label nTri = 0;
label maxTri = 0; // the maximum number of triangles for any single face
List<Face>& faceLst = this->storedFaces();
// determine how many triangles will be needed
forAll(faceLst, faceI)
{
const label n = faceLst[faceI].nTriangles();
if (maxTri < n)
{
maxTri = n;
}
nTri += n;
}
// nothing to do
if (nTri <= faceLst.size())
{
if (&faceMapOut)
{
faceMapOut.clear();
}
return 0;
}
List<Face> newFaces(nTri);
List<label> faceMap;
// reuse storage from optional faceMap
if (&faceMapOut)
{
faceMap.transfer(faceMapOut);