Commit ce3d79de authored by Mark Olesen's avatar Mark Olesen
Browse files

surfMesh changes

- can read MeshedSurface, UnsortedMeshedSurface from surfMesh/
- can write surfMesh in any third-party format
parent 8e57ed53
......@@ -278,6 +278,14 @@ int main(int argc, char *argv[])
Info<< "runTime.timeName() = " << runTime.timeName() << endl;
Info<< "write MeshedSurface 'yetAnother' via proxy as surfMesh"
<< endl;
surf.write
(
runTime,
"yetAnother"
);
surfMesh surfIn
(
IOobject
......@@ -291,7 +299,16 @@ int main(int argc, char *argv[])
);
Info<< "surfIn = " << surfIn.nFaces() << endl;
MeshedSurface<face> surfIn2(runTime, "foobar");
Info<<"surfIn2 = " << surfIn2.size() << endl;
Info<< "surfIn = " << surfIn.size() << endl;
Info<< "writing surfMesh as obj = oldSurfIn.obj" << endl;
surfIn.write("oldSurfIn.obj");
Info<< "runTime.instance() = " << runTime.instance() << endl;
......
......@@ -28,8 +28,6 @@ License
#include "UnsortedMeshedSurface.H"
#include "MeshedSurfaceProxy.H"
#include "mergePoints.H"
#include "IFstream.H"
#include "OFstream.H"
#include "Time.H"
#include "ListOps.H"
#include "polyBoundaryMesh.H"
......@@ -140,12 +138,7 @@ void Foam::MeshedSurface<Face>::write
if (supported.found(ext))
{
MeshedSurfaceProxy<Face>
(
surf.points(),
surf.faces(),
surf.surfZones()
).write(name);
MeshedSurfaceProxy<Face>(surf).write(name);
}
else
{
......@@ -367,11 +360,37 @@ Foam::MeshedSurface<Face>::MeshedSurface(const fileName& name)
template<class Face>
Foam::MeshedSurface<Face>::MeshedSurface(const Time& d, const word& surfName)
Foam::MeshedSurface<Face>::MeshedSurface
(
const Time& t,
const word& surfName
)
:
ParentType(List<Face>(), pointField())
{
read(this->findMeshFile(d, surfName));
surfMesh mesh
(
IOobject
(
"dummyName",
t.timeName(),
t,
IOobject::MUST_READ,
IOobject::NO_WRITE,
false
),
surfName
);
// same face type as surfMesh
MeshedSurface<face> surf
(
xferMove(mesh.storedPoints()),
xferMove(mesh.storedFaces()),
xferMove(mesh.storedZones())
);
this->transcribe(surf);
}
......@@ -1133,11 +1152,11 @@ bool Foam::MeshedSurface<Face>::read
template<class Face>
void Foam::MeshedSurface<Face>::write
(
const Time& d,
const Time& t,
const word& surfName
) const
{
write(findMeshFile(d, surfName));
MeshedSurfaceProxy<Face>(*this).write(t, surfName);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
......@@ -1153,6 +1172,18 @@ void Foam::MeshedSurface<Face>::operator=(const MeshedSurface& surf)
}
template<class Face>
Foam::MeshedSurface<Face>::operator
Foam::MeshedSurfaceProxy<Face>() const
{
return MeshedSurfaceProxy<Face>
(
this->points(),
this->faces(),
this->surfZones()
);
}
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
......
......@@ -85,14 +85,9 @@ class MeshedSurface
public PrimitivePatch<Face, ::Foam::List, pointField, point>,
public fileFormats::surfaceFormatsCore
{
// friends despite different faces
template<class Face2>
friend class MeshedSurface;
// friends despite different faces
template<class Face2>
friend class UnsortedMeshedSurface;
// friends - despite different face representationsx
template<class Face2> friend class MeshedSurface;
template<class Face2> friend class UnsortedMeshedSurface;
friend class surfMesh;
private:
......@@ -232,7 +227,7 @@ public:
//- Construct from file name (uses extension to determine type)
MeshedSurface(const fileName&, const word& ext);
//- Construct from objectRegistry
//- Construct from database
MeshedSurface(const Time&, const word& surfName="");
// Declare run-time constructor selection table
......@@ -437,6 +432,9 @@ public:
void operator=(const MeshedSurface<Face>&);
//- Conversion operator to MeshedSurfaceProxy
operator MeshedSurfaceProxy<Face>() const;
};
......
......@@ -34,7 +34,7 @@ template<class Face>
void Foam::MeshedSurface<Face>::writeStats(Ostream& os) const
{
os << "points : " << this->points().size() << nl;
if (this->isTri())
if (MeshedSurface<Face>::isTri())
{
os << "triangles : " << this->size() << nl;
}
......
......@@ -51,7 +51,7 @@ Foam::MeshedSurface<Face>::New(const fileName& name, const word& ext)
if (supported.found(ext))
{
// create indirectly
autoPtr<MeshedSurface<Face> > surf(new MeshedSurface<Face>);
autoPtr< MeshedSurface<Face> > surf(new MeshedSurface<Face>);
surf().transfer(FriendType::New(name, ext)());
return surf;
......@@ -70,7 +70,7 @@ Foam::MeshedSurface<Face>::New(const fileName& name, const word& ext)
<< exit(FatalError);
}
return autoPtr<MeshedSurface<Face> >(cstrIter()(name));
return autoPtr< MeshedSurface<Face> >(cstrIter()(name));
}
......
......@@ -26,11 +26,6 @@ License
#include "MeshedSurface.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
template<class Face>
......@@ -38,7 +33,7 @@ void Foam::MeshedSurface<Face>::checkZones()
{
// extra safety, ensure we have at some zones
// and they cover all the faces - fix start silently
surfZoneList& zones = storedZones();
surfZoneList& zones = this->storedZones();
if (zones.size())
{
label count = 0;
......@@ -48,25 +43,25 @@ void Foam::MeshedSurface<Face>::checkZones()
count += zones[zoneI].size();
}
if (count < size())
if (count < this->size())
{
WarningIn
(
"MeshedSurface::checkZones()\n"
)
<< "more faces " << size() << " than zones " << count
<< "more faces " << this->size() << " than zones " << count
<< " ... extending final zone"
<< endl;
zones[zones.size()-1].size() += count - size();
zones[zones.size()-1].size() += count - this->size();
}
else if (count > size())
else if (count > this->size())
{
FatalErrorIn
(
"MeshedSurface::checkZones()\n"
)
<< "more zones " << count << " than faces " << size()
<< "more zones " << count << " than faces " << this->size()
<< exit(FatalError);
}
}
......@@ -101,7 +96,7 @@ void Foam::MeshedSurface<Face>::sortFacesAndStore
List<Face> newFaces(faceMap.size());
forAll(faceMap, faceI)
{
// use transfer to recover memory if possible
// use transfer to recover memory where possible
newFaces[faceI].transfer(oldFaces[faceMap[faceI]]);
}
this->storedFaces().transfer(newFaces);
......@@ -205,10 +200,4 @@ void Foam::MeshedSurface<Face>::removeZones()
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// ************************************************************************* //
......@@ -25,9 +25,11 @@ License
\*---------------------------------------------------------------------------*/
#include "MeshedSurfaceProxy.H"
#include "MeshedSurface.H"
#include "Time.H"
#include "surfMesh.H"
#include "MeshedSurface.H"
#include "OFstream.H"
#include "ListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -86,6 +88,127 @@ void Foam::MeshedSurfaceProxy<Face>::write
}
template<class Face>
void Foam::MeshedSurfaceProxy<Face>::write
(
const Time& t,
const word& surfName
) const
{
// the surface name to be used
word name(surfName.size() ? surfName : surfaceRegistry::defaultName);
if (debug)
{
Info<< "MeshedSurfaceProxy::write"
"(const Time&, const word&) : "
"writing to " << name
<< endl;
}
// the local location
const fileName objectDir
(
t.timePath()/surfaceRegistry::subInstance/name/surfMesh::meshSubDir
);
if (!isDir(objectDir))
{
mkDir(objectDir);
}
// write surfMesh/points
{
pointIOField io
(
IOobject
(
"points",
t.timeName(),
surfMesh::meshSubDir,
t,
IOobject::NO_READ,
IOobject::NO_WRITE,
false
)
);
OFstream os(objectDir/io.name());
io.writeHeader(os);
os << this->points();
os << "\n\n"
"// ************************************************************************* //\n";
}
// write surfMesh/faces
{
faceIOList io
(
IOobject
(
"faces",
t.timeName(),
surfMesh::meshSubDir,
t,
IOobject::NO_READ,
IOobject::NO_WRITE,
false
)
);
OFstream os(objectDir/io.name());
io.writeHeader(os);
if (this->useFaceMap())
{
// this is really a bit annoying (and wasteful) but no other way
os << reorder(this->faceMap(), this->faces());
}
else
{
os << this->faces();
}
os << "\n\n"
"// ************************************************************************* //\n";
}
// write surfMesh/surfZones
{
surfZoneIOList io
(
IOobject
(
"surfZones",
t.timeName(),
surfMesh::meshSubDir,
t,
IOobject::NO_READ,
IOobject::NO_WRITE,
false
)
);
OFstream os(objectDir/io.name());
io.writeHeader(os);
os << this->surfZones();
os << "\n\n"
"// ************************************************************************* //"
<< endl;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Face>
......@@ -104,19 +227,6 @@ Foam::MeshedSurfaceProxy<Face>::MeshedSurfaceProxy
{}
template<class Face>
Foam::MeshedSurfaceProxy<Face>::MeshedSurfaceProxy
(
const MeshedSurface<Face>& surf
)
:
points_(surf.points()),
faces_(surf.faces()),
zones_(surf.surfZones()),
faceMap_(List<label>())
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Face>
......@@ -131,16 +241,6 @@ Foam::MeshedSurfaceProxy<Face>::~MeshedSurfaceProxy()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// template<class Face>
// void Foam::MeshedSurfaceProxy<Face>::write
// (
// const Time& d,
// const word& surfName
// ) const
// {
// write(findMeshFile(d, surfName)());
// }
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
......
......@@ -76,15 +76,6 @@ class MeshedSurfaceProxy
const List<label>& faceMap_;
// Private Member Functions
//- Disallow default bitwise copy construct
MeshedSurfaceProxy(const MeshedSurfaceProxy&);
//- Disallow default bitwise assignment
void operator=(const MeshedSurfaceProxy&);
public:
//- Runtime type information
......@@ -108,12 +99,6 @@ public:
const List<label>& faceMap = List<label>()
);
//- Construct from MeshedSurface
explicit MeshedSurfaceProxy
(
const MeshedSurface<Face>&
);
// Destructor
virtual ~MeshedSurfaceProxy();
......@@ -176,8 +161,6 @@ public:
// Write
//?? void writeStats(Ostream& os) const;
//- Generic write routine. Chooses writer based on extension.
virtual void write(const fileName& name) const
{
......@@ -185,7 +168,9 @@ public:
}
//- Write to database
//?? void write(const Time&, const word& surfName="") const;
virtual void write(const Time&, const word& surfName = "") const;
//?? void writeStats(Ostream& os) const;
};
......
......@@ -26,6 +26,7 @@ License
#include "MeshedSurface.H"
#include "UnsortedMeshedSurface.H"
#include "MeshedSurfaceProxy.H"
#include "IFstream.H"
#include "OFstream.H"
#include "Time.H"
......@@ -127,16 +128,7 @@ void Foam::UnsortedMeshedSurface<Face>::write
if (supported.found(ext))
{
labelList faceMap;
List<surfZone> zoneLst = surf.sortedZones(faceMap);
MeshedSurfaceProxy<Face>
(
surf.points(),
surf.faces(),
zoneLst,
faceMap
).write(name);
MeshedSurfaceProxy<Face>(surf).write(name);
}
else
{
......@@ -291,13 +283,14 @@ Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface(const fileName& name)
template<class Face>
Foam::UnsortedMeshedSurface<Face>::UnsortedMeshedSurface
(
const Time& d,
const Time& t,
const word& surfName
)
:
ParentType()
{
read(this->findMeshFile(d, surfName));
MeshedSurface<Face> surf(t, surfName);
transfer(surf);
}
......@@ -473,7 +466,75 @@ Foam::surfZoneList Foam::UnsortedMeshedSurface<Face>::sortedZones
zoneNames.insert(zoneI, zoneToc_[zoneI].name());
}
return this->sortedZonesById(zoneIds_, zoneNames, faceMap);
// std::sort() really seems to mix up the order.
// and std::stable_sort() might take too long / too much memory
// Assuming that we have relatively fewer zones compared to the
// number of items, just do it ourselves
// step 1: get zone sizes and store (origId => zoneI)
Map<label> lookup;
forAll(zoneIds_, faceI)
{
const label origId = zoneIds_[faceI];
Map<label>::iterator fnd = lookup.find(origId);
if (fnd != lookup.end())
{
fnd()++;
}
else
{
lookup.insert(origId, 1);
}
}
// step 2: assign start/size (and name) to the newZones
// re-use the lookup to map (zoneId => zoneI)
surfZoneList zoneLst(lookup.size());
label start = 0;
label zoneI = 0;
forAllIter(Map<label>, lookup, iter)
{
label origId = iter.key();
word name;
Map<word>::const_iterator fnd = zoneNames.find(origId);
if (fnd != zoneNames.end())
{
name = fnd();
}
else
{
name = word("zone") + ::Foam::name(zoneI);
}
zoneLst[zoneI] = surfZone
(
name,
0, // initialize with zero size
start,
zoneI
);
// increment the start for the next zone
// and save the (zoneId => zoneI) mapping
start += iter();
iter() = zoneI++;
}
// step 3: build the re-ordering
faceMap.setSize(zoneIds_.size());
forAll(zoneIds_, faceI)
{
label zoneI = lookup[zoneIds_[faceI]];
faceMap[faceI] = zoneLst[zoneI].start() + zoneLst[zoneI].size()++;
}
// with reordered faces registered in faceMap
return zoneLst;
}
......@@ -670,11 +731,11 @@ bool Foam::UnsortedMeshedSurface<Face>::read
template<class Face>
void Foam::UnsortedMeshedSurface<Face>::write
(
const Time& d,
const Time& t,
const word& surfName