diff --git a/applications/utilities/surface/README b/applications/utilities/surface/README index 292d8ca0b5d0bf154ae0f6aee5ddfa17227b2152..a66c2463a127dd9b859aede63a240429afe99e1a 100644 --- a/applications/utilities/surface/README +++ b/applications/utilities/surface/README @@ -21,7 +21,7 @@ surfaceFind - Finds nearest vertex and face to given point. surfaceMeshTriangulate -- Triangulate external facses of mesh and write as surface. +- Triangulate external faces of mesh and write as surface. surfacePointMerge - Explicit point merge of surface. diff --git a/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C b/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C index 046f9cefe8778808a4edd79c76870a8e5aade9dc..ee221f992aef843ce920c86629c0124a3ea1297b 100644 --- a/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C +++ b/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C @@ -26,266 +26,85 @@ Application surfaceFeatureConvert Description - Extracts and writes surface features to file + Convert between edgeMesh formats \*---------------------------------------------------------------------------*/ -#include "featureEdgeMesh.H" #include "argList.H" #include "Time.H" -#include "IFstream.H" -#include "IStringStream.H" -#include "OFstream.H" -#include "Map.H" + +#include "edgeMesh.H" using namespace Foam; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -void readNASEdges -( - const fileName& inFileName, - pointField& allPoints, - edgeList& allEdges -) -{ - IFstream is(inFileName); +// Main program: - if (!is.good()) +int main(int argc, char *argv[]) +{ + argList::addNote + ( + "Convert between edgeMesh formats" + ); + argList::noParallel(); + argList::validArgs.append("inputFile"); + argList::validArgs.append("outputFile"); + argList::addOption + ( + "scale", + "factor", + "specify a scaling factor for the points" + ); + + argList args(argc, argv); + Time runTime(args.rootPath(), args.caseName()); + const stringList& params = args.additionalArgs(); + + const fileName importName(params[0]); + const fileName exportName(params[1]); + + // disable inplace editing + if (importName == exportName) { - FatalErrorIn("readNASEdges") - << "Cannot read file " << inFileName + FatalErrorIn(args.executable()) + << "Output file " << exportName << " would overwrite input file." << exit(FatalError); } - // coordinates of point - DynamicList<point> points; - // Nastran index of point - DynamicList<label> pointIndices; - - // beams - DynamicList<edge> edges; - DynamicList<label> edgeIndices; - - - while (is.good()) + // check that reading/writing is supported + if + ( + !edgeMesh::canReadType(importName.ext(), true) + || !edgeMesh::canWriteType(exportName.ext(), true) + ) { - string line; - is.getLine(line); - - if (line.empty() || line[0] == '$') - { - // Skip empty and comment - continue; - } - - // Check if character 72 is continuation - if (line.size() > 72 && line[72] == '+') - { - line = line.substr(0, 72); - - while (true) - { - string buf; - is.getLine(buf); - - if (buf.size() > 72 && buf[72] == '+') - { - line += buf.substr(8, 64); - } - else - { - line += buf.substr(8, buf.size()-8); - break; - } - } - } - - // Read first word - IStringStream lineStream(line); - word cmd; - lineStream >> cmd; - - if (cmd == "GRID") - { - label index; - lineStream >> index; - pointIndices.append(index); - - scalar x = readScalar(IStringStream(line.substr(24, 8))()); - scalar y = readScalar(IStringStream(line.substr(32, 8))()); - scalar z = readScalar(IStringStream(line.substr(40, 8))()); - points.append(point(x, y, z)); - } - else if (cmd == "CBEAM") - { - // Read shell type since gives patchnames. - label index, group, v0, v1; - lineStream >> index >> group >> v0 >> v1; - - edgeIndices.append(index); - edges.append(edge(v0, v1)); - } + return 1; } - points.shrink(); - pointIndices.shrink(); - edges.shrink(); - edgeIndices.shrink(); - - Pout<< "Read from " << inFileName - << " edges:" << edges.size() << " points:" << points.size() - << endl; - - { - // Build inverse mapping (index to point) - Map<label> indexToPoint(2*pointIndices.size()); - forAll(pointIndices, i) - { - indexToPoint.insert(pointIndices[i], i); - } - - // Relabel edges - forAll(edges, i) - { - edge& e = edges[i]; - e[0] = indexToPoint[e[0]]; - e[1] = indexToPoint[e[1]]; - } - } + edgeMesh mesh(importName); - allPoints.transfer(points); - allEdges.transfer(edges); -} + Info<< "\nRead edgeMesh " << importName << nl; + mesh.writeStats(Info); + Info<< nl + << "\nwriting " << exportName; - - -void write -( - const Time& runTime, - const fileName& inFileName, - const fileName& outFileName, - const edgeMesh& eMesh -) -{ - if (outFileName.ext() == "eMesh") - { - featureEdgeMesh fem - ( - IOobject - ( - outFileName, // name - runTime.constant(), // instance - runTime, // registry - IOobject::NO_READ, - IOobject::AUTO_WRITE, - false - ), - eMesh.points(), - eMesh.edges() - ); - - Pout<< "Writing feature edge mesh to " << fem.objectPath() - << endl; - - fem.write(); - } - else if (outFileName.ext() == "vtk") + scalar scaleFactor = 0; + if (args.optionReadIfPresent("scale", scaleFactor) && scaleFactor > 0) { - OFstream str(outFileName); - - str << "# vtk DataFile Version 2.0" << nl - << "featureEdgeMesh " << inFileName << nl - << "ASCII" << nl - << "DATASET POLYDATA" << nl; - - str << "POINTS " << eMesh.points().size() << " float" << nl; - forAll(eMesh.points(), pointI) - { - const point& pt = eMesh.points()[pointI]; - - str << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl; - } - - str << "LINES " << eMesh.edges().size() << ' ' - << 3*eMesh.edges().size() << nl; - forAll(eMesh.edges(), edgeI) - { - const edge& e = eMesh.edges()[edgeI]; - - str << "2 " << e[0] << ' ' << e[1] << nl; - } + Info<< " with scaling " << scaleFactor << endl; + mesh.scalePoints(scaleFactor); } else { - FatalErrorIn("write") - << "Supported output formats: .eMesh, .vtk" - << exit(FatalError); + Info<< " without scaling" << endl; } -} - - -// Main program: - -int main(int argc, char *argv[]) -{ - argList::noParallel(); - argList::validArgs.append("input file"); - argList::validArgs.append("output file"); -# include "setRootCase.H" -# include "createTime.H" - - const fileName inFileName(args.additionalArgs()[0]); - const word outFileName(args.additionalArgs()[1]); - - Pout<< "Input features file : " << inFileName << nl - << "Output features file : " << outFileName << nl - << endl; + mesh.write(exportName); + mesh.writeStats(Info); + Info<< endl; - // Read - // ~~~~ - - if (inFileName.ext() == "nas") - { - pointField points; - edgeList edges; - readNASEdges(inFileName, points, edges); - - edgeMesh eMesh(points, edges); - - write(runTime, inFileName, outFileName, eMesh); - } - else if (inFileName.ext() == "eMesh") - { - featureEdgeMesh fem - ( - IOobject - ( - inFileName, // name - runTime.constant(), // instance - runTime, // registry - IOobject::MUST_READ, - IOobject::AUTO_WRITE, - false - ) - ); - - Pout<< "Read from " << inFileName - << " edges:" << fem.edges().size() - << " points:" << fem.points().size() - << endl; - - write(runTime, inFileName, outFileName, fem); - } - else - { - FatalErrorIn(args.executable()) - << "Can only handle NASTRAN data formats (.nas extension)." - << exit(FatalError); - } - - Pout<< "End\n" << endl; + Info<< "\nEnd\n" << endl; return 0; } diff --git a/src/edgeMesh/Make/files b/src/edgeMesh/Make/files index 4017257d8b48e5abebbfc58d34d84a173475b8fe..cdad2399991b81b82fda8df34d4d62a796a1363b 100644 --- a/src/edgeMesh/Make/files +++ b/src/edgeMesh/Make/files @@ -1,5 +1,26 @@ edgeMesh.C edgeMeshIO.C -featureEdgeMesh.C +edgeMeshNew.C + +edgeFormats = edgeFormats +$(edgeFormats)/edgeFormatsCore.C + +$(edgeFormats)/emesh/EMESHedgeFormat.C +$(edgeFormats)/emesh/EMESHedgeFormatRunTime.C + +$(edgeFormats)/nas/NASedgeFormat.C +$(edgeFormats)/nas/NASedgeFormatRunTime.C + +$(edgeFormats)/obj/OBJedgeFormat.C +$(edgeFormats)/obj/OBJedgeFormatRunTime.C + +$(edgeFormats)/starcd/STARCDedgeFormat.C +$(edgeFormats)/starcd/STARCDedgeFormatRunTime.C + +$(edgeFormats)/vtk/VTKedgeFormat.C +$(edgeFormats)/vtk/VTKedgeFormatRunTime.C + +featureEdgeMesh/featureEdgeMesh.C + LIB = $(FOAM_LIBBIN)/libedgeMesh diff --git a/src/edgeMesh/edgeFormats/edgeFormatsCore.C b/src/edgeMesh/edgeFormats/edgeFormatsCore.C new file mode 100644 index 0000000000000000000000000000000000000000..8af7666cff3e4176488b978ad8172bf79b40d4fb --- /dev/null +++ b/src/edgeMesh/edgeFormats/edgeFormatsCore.C @@ -0,0 +1,200 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "edgeFormatsCore.H" + +#include "Time.H" +#include "IFstream.H" +#include "OFstream.H" +#include "edgeMesh.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +Foam::word Foam::fileFormats::edgeFormatsCore::nativeExt("eMesh"); + + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +Foam::string Foam::fileFormats::edgeFormatsCore::getLineNoComment +( + IFstream& is +) +{ + string line; + do + { + is.getLine(line); + } + while ((line.empty() || line[0] == '#') && is.good()); + + return line; +} + + +#if 0 +Foam::fileName Foam::fileFormats::edgeFormatsCore::localMeshFileName +( + const word& meshName +) +{ + const word name(meshName.size() ? meshName : surfaceRegistry::defaultName); + + return fileName + ( + surfaceRegistry::prefix/name/surfMesh::meshSubDir + / name + "." + nativeExt + ); +} + + +Foam::fileName Foam::fileFormats::edgeFormatsCore::findMeshInstance +( + const Time& t, + const word& meshName +) +{ + fileName localName = localMeshFileName(meshName); + + // Search back through the time directories list to find the time + // closest to and lower than current time + + instantList ts = t.times(); + label instanceI; + + for (instanceI = ts.size()-1; instanceI >= 0; --instanceI) + { + if (ts[instanceI].value() <= t.timeOutputValue()) + { + break; + } + } + + // Noting that the current directory has already been searched + // for mesh data, start searching from the previously stored time directory + + if (instanceI >= 0) + { + for (label i = instanceI; i >= 0; --i) + { + if (isFile(t.path()/ts[i].name()/localName)) + { + return ts[i].name(); + } + } + } + + return "constant"; +} + + +Foam::fileName Foam::fileFormats::edgeFormatsCore::findMeshFile +( + const Time& t, + const word& meshName +) +{ + fileName localName = localMeshFileName(meshName); + + // Search back through the time directories list to find the time + // closest to and lower than current time + + instantList ts = t.times(); + label instanceI; + + for (instanceI = ts.size()-1; instanceI >= 0; --instanceI) + { + if (ts[instanceI].value() <= t.timeOutputValue()) + { + break; + } + } + + // Noting that the current directory has already been searched + // for mesh data, start searching from the previously stored time directory + + if (instanceI >= 0) + { + for (label i = instanceI; i >= 0; --i) + { + fileName testName(t.path()/ts[i].name()/localName); + + if (isFile(testName)) + { + return testName; + } + } + } + + // fallback to "constant" + return t.path()/"constant"/localName; +} +#endif + + +bool Foam::fileFormats::edgeFormatsCore::checkSupport +( + const wordHashSet& available, + const word& ext, + const bool verbose, + const word& functionName +) +{ + if (available.found(ext)) + { + return true; + } + else if (verbose) + { + wordList known = available.sortedToc(); + + Info<<"Unknown file extension for " << functionName + << " : " << ext << nl + <<"Valid types: ("; + // compact output: + forAll(known, i) + { + Info<<" " << known[i]; + } + Info<<" )" << endl; + } + + return false; +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fileFormats::edgeFormatsCore::edgeFormatsCore() +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::fileFormats::edgeFormatsCore::~edgeFormatsCore() +{} + + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/edgeFormatsCore.H b/src/edgeMesh/edgeFormats/edgeFormatsCore.H new file mode 100644 index 0000000000000000000000000000000000000000..a18f4f8a4bcf4d3b3ec7d83260ee16f9c551f337 --- /dev/null +++ b/src/edgeMesh/edgeFormats/edgeFormatsCore.H @@ -0,0 +1,118 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 + +Class + Foam::fileFormats::edgeFormatsCore + +Description + A collection of helper functions for reading/writing edge formats. + +SourceFiles + edgeFormatsCore.C + +\*---------------------------------------------------------------------------*/ + +#ifndef edgeFormatsCore_H +#define edgeFormatsCore_H + +#include "Map.H" +#include "HashSet.H" +#include "labelList.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of friend functions and operators + +class IFstream; +class Time; + +namespace fileFormats +{ + +/*---------------------------------------------------------------------------*\ + Class edgeFormatsCore Declaration +\*---------------------------------------------------------------------------*/ + +class edgeFormatsCore +{ +protected: + + //- Read non-comment line + static string getLineNoComment(IFstream&); + +public: + // Static Data + + //- The file extension corresponding to 'native' edge format + // Normally "eMesh" (edge-mesh) + static word nativeExt; + + // Static Member Functions + + static bool checkSupport + ( + const wordHashSet& available, + const word& ext, + const bool verbose, + const word& functionName + ); + +// //- Return the local file name (within time directory) +// // NEEDS FIXING +// static fileName localMeshFileName(const word& edgeName=""); +// +// //- Find instance with edgeName +// // NEEDS FIXING +// static fileName findMeshInstance(const Time&, const word& edgeName=""); +// +// //- Find mesh file with edgeName +// // NEEDS FIXING +// static fileName findMeshFile(const Time&, const word& edgeName=""); + + + // Constructors + + //- Construct null + edgeFormatsCore(); + + + //- Destructor + virtual ~edgeFormatsCore(); + +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fileFormats +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.C b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.C new file mode 100644 index 0000000000000000000000000000000000000000..b085542f1d82f3589fcb96fe145e21a31aaf9937 --- /dev/null +++ b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.C @@ -0,0 +1,195 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "EMESHedgeFormat.H" +#include "IOobject.H" +#include "IFstream.H" +#include "clock.H" + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fileFormats::EMESHedgeFormat::EMESHedgeFormat +( + const fileName& filename +) +{ + read(filename); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::fileFormats::EMESHedgeFormat::read +( + const fileName& filename +) +{ + clear(); + + IFstream is(filename); + if (!is.good()) + { + FatalErrorIn + ( + "fileFormats::EMESHedgeFormat::read(const fileName&)" + ) + << "Cannot read file " << filename + << exit(FatalError); + } + + return read(is, this->storedPoints(), this->storedEdges()); +} + + +bool Foam::fileFormats::EMESHedgeFormat::read +( + Istream& is, + pointField& pointLst, + edgeList& edgeLst +) +{ + if (!is.good()) + { + FatalErrorIn + ( + "fileFormats::EMESHedgeFormat::read" + "(Istream&, pointField&, edgeList&)" + ) + << "read error " + << exit(FatalError); + } + + token firstToken(is); + + // swallow IOobject header + if (!is.good()) + { + FatalIOErrorIn + ( + "fileFormats::EMESHedgeFormat::read" + "(Istream&, pointField&, edgeList&)", + is + ) + << "First token could not be read" << nl + << exit(FatalIOError); + + return false; + } + else if (firstToken.isWord() && firstToken.wordToken() == "FoamFile") + { + // read and discard + dictionary headerDict(is); + } + else + { + is.putBack(firstToken); + } + + // read points: + is >> pointLst; + + // read edges: + is >> edgeLst; + + return true; +} + + +Foam::Ostream& +Foam::fileFormats::EMESHedgeFormat::write +( + Ostream& os, + const pointField& pointLst, + const edgeList& edgeLst +) +{ + if (!os.good()) + { + FatalErrorIn + ( + "fileFormats::EMESHedgeFormat::write" + "(Ostream&, const fileName&, const edgeMesh&)" + ) + << "bad output stream " << os.name() + << exit(FatalError); + } + + os << "\n// points:" << nl << pointLst << nl + << "\n// edges:" << nl << edgeLst << nl; + + IOobject::writeDivider(os); + + // Check state of Ostream + os.check + ( + "EMESHedgeFormat::write" + "(Ostream&, const pointField&, const edgeList&)" + ); + + return os; +} + + +void Foam::fileFormats::EMESHedgeFormat::write +( + const fileName& filename, + const edgeMesh& mesh +) +{ + OFstream os(filename); + if (!os.good()) + { + FatalErrorIn + ( + "fileFormats::EMESHedgeFormat::write" + "(const fileName&, const edgeMesh&)" + ) + << "Cannot open file for writing " << filename + << exit(FatalError); + } + + + // just emit some information until we get a nicer IOobject + IOobject::writeBanner(os) + << "FoamFile\n{\n" + << " version " << os.version() << ";\n" + << " format " << os.format() << ";\n" + << " class " << "featureEdgeMesh" << ";\n" + << " note " << "written " + clock::dateTime() << nl + << " object " << filename.name() << ";\n" + << "}" << nl; + + IOobject::writeDivider(os); + + write(os, mesh.points(), mesh.edges()); + + // Check state of Ostream + os.check("EMESHedgeFormat::write(Ostream&)"); +} + + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.H b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.H new file mode 100644 index 0000000000000000000000000000000000000000..370b5e7ae14b9f4d1a661c48bfb117325b27b1f5 --- /dev/null +++ b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.H @@ -0,0 +1,148 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 + +Class + Foam::fileFormats::EMESHedgeFormat + +Description + Provide a means of reading/writing the single-file OpenFOAM edge format. + +Note + This class provides more methods than the regular edge format interface. + +SourceFiles + EMESHedgeFormat.C + +\*---------------------------------------------------------------------------*/ + +#ifndef EMESHedgeFormat_H +#define EMESHedgeFormat_H + +#include "edgeMesh.H" +#include "Ostream.H" +#include "OFstream.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +/*---------------------------------------------------------------------------*\ + Class EMESHedgeFormat Declaration +\*---------------------------------------------------------------------------*/ + +class EMESHedgeFormat +: + public edgeMesh +{ + // Private Member Functions + + //- Disallow default bitwise copy construct + EMESHedgeFormat(const EMESHedgeFormat&); + + //- Disallow default bitwise assignment + void operator=(const EMESHedgeFormat&); + + +protected: + + // Protected Member Functions + + //- Write header information + static void writeHeader + ( + Ostream&, + const pointField&, + const edgeList& + ); + +public: + + // Constructors + + //- Construct from file name + EMESHedgeFormat(const fileName&); + + + // Selectors + + //- Read file and return edgeMesh + static autoPtr<edgeMesh> New(const fileName& name) + { + return autoPtr<edgeMesh> + ( + new EMESHedgeFormat(name) + ); + } + + + //- Destructor + virtual ~EMESHedgeFormat() + {} + + + // Member Functions + + //- Read edgeMesh components from stream + static bool read + ( + Istream&, + pointField&, + edgeList& + ); + + //- Write edgeMesh components to stream + static Ostream& write + ( + Ostream&, + const pointField&, + const edgeList& + ); + + //- Write edgeMesh with a mimicked IOobject header + static void write(const fileName&, const edgeMesh&); + + //- Read from file + virtual bool read(const fileName&); + + //- Write object + virtual void write(const fileName& name) const + { + write(name, *this); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fileFormats +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormatRunTime.C b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormatRunTime.C new file mode 100644 index 0000000000000000000000000000000000000000..28e6ddd6d170c3daf17258be08409d78e3c88ac0 --- /dev/null +++ b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormatRunTime.C @@ -0,0 +1,61 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "EMESHedgeFormat.H" + +#include "addToRunTimeSelectionTable.H" +#include "addToMemberFunctionSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +// read edgeMesh +addNamedToRunTimeSelectionTable +( + edgeMesh, + EMESHedgeFormat, + fileExtension, + eMesh +); + +// write edgeMesh +addNamedToMemberFunctionSelectionTable +( + edgeMesh, + EMESHedgeFormat, + write, + fileExtension, + eMesh +); + +} +} + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/nas/NASedgeFormat.C b/src/edgeMesh/edgeFormats/nas/NASedgeFormat.C new file mode 100644 index 0000000000000000000000000000000000000000..4b178d1dbd31b3186973d3ebf24cfb65a362173e --- /dev/null +++ b/src/edgeMesh/edgeFormats/nas/NASedgeFormat.C @@ -0,0 +1,260 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "NASedgeFormat.H" +#include "IFstream.H" +#include "IStringStream.H" +#include "PackedBoolList.H" + +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +// Do weird things to extract a floating point number +Foam::scalar Foam::fileFormats::NASedgeFormat::parseNASCoord +( + const string& s +) +{ + size_t expSign = s.find_last_of("+-"); + + if (expSign != string::npos && expSign > 0 && !isspace(s[expSign-1])) + { + scalar mantissa = readScalar(IStringStream(s.substr(0, expSign))()); + scalar exponent = readScalar(IStringStream(s.substr(expSign+1))()); + + if (s[expSign] == '-') + { + exponent = -exponent; + } + return mantissa * pow(10, exponent); + } + else + { + return readScalar(IStringStream(s)()); + } +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fileFormats::NASedgeFormat::NASedgeFormat +( + const fileName& filename +) +{ + read(filename); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::fileFormats::NASedgeFormat::read +( + const fileName& filename +) +{ + clear(); + + IFstream is(filename); + if (!is.good()) + { + FatalErrorIn + ( + "fileFormats::NASedgeFormat::read(const fileName&)" + ) + << "Cannot read file " << filename + << exit(FatalError); + } + + DynamicList<point> dynPoints; + DynamicList<edge> dynEdges; + DynamicList<label> pointId; // Nastran index of points + + while (is.good()) + { + string line; + is.getLine(line); + + // Skip empty or comment + if (line.empty() || line[0] == '$') + { + continue; + } + + // Check if character 72 is continuation + if (line.size() > 72 && line[72] == '+') + { + line = line.substr(0, 72); + + while (true) + { + string buf; + is.getLine(buf); + + if (buf.size() > 72 && buf[72] == '+') + { + line += buf.substr(8, 64); + } + else + { + line += buf.substr(8, buf.size()-8); + break; + } + } + } + + + // Read first word + IStringStream lineStream(line); + word cmd; + lineStream >> cmd; + + if (cmd == "CBEAM" || cmd == "CROD") + { + edge e; + + // label groupId = readLabel(IStringStream(line.substr(16,8))()); + e[0] = readLabel(IStringStream(line.substr(24,8))()); + e[1] = readLabel(IStringStream(line.substr(32,8))()); + + // discard groupID + dynEdges.append(e); + } + else if (cmd == "GRID") + { + label index = readLabel(IStringStream(line.substr(8,8))()); + scalar x = parseNASCoord(line.substr(24, 8)); + scalar y = parseNASCoord(line.substr(32, 8)); + scalar z = parseNASCoord(line.substr(40, 8)); + + pointId.append(index); + dynPoints.append(point(x, y, z)); + } + else if (cmd == "GRID*") + { + // Long format is on two lines with '*' continuation symbol + // on start of second line. + // Typical line (spaces compacted) + // GRID* 126 0 -5.55999875E+02 -5.68730474E+02 + // * 2.14897901E+02 + + label index = readLabel(IStringStream(line.substr(8,16))()); + scalar x = parseNASCoord(line.substr(40, 16)); + scalar y = parseNASCoord(line.substr(56, 16)); + + is.getLine(line); + if (line[0] != '*') + { + FatalErrorIn + ( + "fileFormats::NASedgeFormat::read(const fileName&)" + ) + << "Expected continuation symbol '*' when reading GRID*" + << " (double precision coordinate) format" << nl + << "Read:" << line << nl + << "File:" << is.name() << " line:" << is.lineNumber() + << exit(FatalError); + } + scalar z = parseNASCoord(line.substr(8, 16)); + + pointId.append(index); + dynPoints.append(point(x, y, z)); + } + } + + // transfer to normal lists + storedPoints().transfer(dynPoints); + + pointId.shrink(); + dynEdges.shrink(); + + // Build inverse mapping (NASTRAN pointId -> index) + Map<label> mapPointId(2*pointId.size()); + forAll(pointId, i) + { + mapPointId.insert(pointId[i], i); + } + + // note which points were really used and which can be culled + PackedBoolList usedPoints(points().size()); + + + // Pass1: relabel edges + // ~~~~~~~~~~~~~~~~~~~~ + forAll(dynEdges, i) + { + edge& e = dynEdges[i]; + e[0] = mapPointId[e[0]]; + e[1] = mapPointId[e[1]]; + + usedPoints.set(e[0]); + usedPoints.set(e[1]); + } + pointId.clearStorage(); + mapPointId.clear(); + + // not all the points were used, cull them accordingly + if (unsigned(points().size()) != usedPoints.count()) + { + label nUsed = 0; + + pointField& pts = storedPoints(); + forAll(pts, pointI) + { + if (usedPoints.get(pointI)) + { + if (nUsed != pointI) + { + pts[nUsed] = pts[pointI]; + } + + // map prev -> new id + mapPointId[pointI] = nUsed; + + ++nUsed; + } + } + + pts.setSize(nUsed); + + // renumber edge vertices + forAll(dynEdges, edgeI) + { + edge& e = dynEdges[edgeI]; + + e[0] = mapPointId[e[0]]; + e[1] = mapPointId[e[1]]; + } + } + + + // transfer to normal lists + storedEdges().transfer(dynEdges); + + return true; +} + + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/nas/NASedgeFormat.H b/src/edgeMesh/edgeFormats/nas/NASedgeFormat.H new file mode 100644 index 0000000000000000000000000000000000000000..600b880cb790cdc23cf6c64a97ecdc5e4fc931c6 --- /dev/null +++ b/src/edgeMesh/edgeFormats/nas/NASedgeFormat.H @@ -0,0 +1,113 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 + +Class + Foam::fileFormats::NASedgeFormat + +Description + Nastran edge reader. + +SourceFiles + NASedgeFormat.C + +\*---------------------------------------------------------------------------*/ + +#ifndef NASedgeFormat_H +#define NASedgeFormat_H + +#include "edgeMesh.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +/*---------------------------------------------------------------------------*\ + Class NASedgeFormat Declaration +\*---------------------------------------------------------------------------*/ + +class NASedgeFormat +: + public edgeMesh +{ + // Private Member Functions + + //- Disallow default bitwise copy construct + NASedgeFormat(const NASedgeFormat&); + + //- Disallow default bitwise assignment + void operator=(const NASedgeFormat&); + +protected: + + // Protected Member Functions + + //- Do weird things to extract number + static scalar parseNASCoord(const string&); + +public: + + // Constructors + + //- Construct from file name + NASedgeFormat(const fileName&); + + + // Selectors + + //- Read file and return edge mesh + static autoPtr<edgeMesh> New(const fileName& name) + { + return autoPtr<edgeMesh> + ( + new NASedgeFormat(name) + ); + } + + + // Destructor + + virtual ~NASedgeFormat() + {} + + + // Member Functions + + //- Read from a file + virtual bool read(const fileName&); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fileFormats +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/nas/NASedgeFormatRunTime.C b/src/edgeMesh/edgeFormats/nas/NASedgeFormatRunTime.C new file mode 100644 index 0000000000000000000000000000000000000000..ec03e44bbb23b12afc9664230bf484d49ede0619 --- /dev/null +++ b/src/edgeMesh/edgeFormats/nas/NASedgeFormatRunTime.C @@ -0,0 +1,60 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "NASedgeFormat.H" + +#include "addToRunTimeSelectionTable.H" +#include "addToMemberFunctionSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +// read edgeMesh - .bdf (Bulk Data Format) +addNamedToRunTimeSelectionTable +( + edgeMesh, + NASedgeFormat, + fileExtension, + bdf +); + +// read edgeMesh - .nas (Nastran Data Format) +addNamedToRunTimeSelectionTable +( + edgeMesh, + NASedgeFormat, + fileExtension, + nas +); + +} +} + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/obj/OBJedgeFormat.C b/src/edgeMesh/edgeFormats/obj/OBJedgeFormat.C new file mode 100644 index 0000000000000000000000000000000000000000..530d2a2a97f579dcf36937d85a274b62ac30dff5 --- /dev/null +++ b/src/edgeMesh/edgeFormats/obj/OBJedgeFormat.C @@ -0,0 +1,245 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "OBJedgeFormat.H" +#include "clock.H" +#include "IFstream.H" +#include "IStringStream.H" +#include "Ostream.H" +#include "OFstream.H" +#include "ListOps.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fileFormats::OBJedgeFormat::OBJedgeFormat +( + const fileName& filename +) +{ + read(filename); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::fileFormats::OBJedgeFormat::read +( + const fileName& filename +) +{ + clear(); + + IFstream is(filename); + if (!is.good()) + { + FatalErrorIn + ( + "fileFormats::OBJedgeFormat::read(const fileName&)" + ) + << "Cannot read file " << filename + << exit(FatalError); + } + + DynamicList<point> dynPoints; + DynamicList<edge> dynEdges; + DynamicList<label> dynUsedPoints; + + while (is.good()) + { + string line = this->getLineNoComment(is); + + // handle continuations + if (line[line.size()-1] == '\\') + { + line.substr(0, line.size()-1); + line += this->getLineNoComment(is); + } + + // Read first word + IStringStream lineStream(line); + word cmd; + lineStream >> cmd; + + if (cmd == "v") + { + scalar x, y, z; + lineStream >> x >> y >> z; + + dynPoints.append(point(x, y, z)); + dynUsedPoints.append(-1); + } + else if (cmd == "l") + { + edge edgeRead; + + // Assume 'l' is followed by space. + string::size_type endNum = 1; + + int nVerts = 0; + for (int count = 0; count < 2; ++count) + { + string::size_type startNum = + line.find_first_not_of(' ', endNum); + + if (startNum == string::npos) + { + break; + } + + endNum = line.find(' ', startNum); + + string vertexSpec; + if (endNum != string::npos) + { + vertexSpec = line.substr(startNum, endNum-startNum); + } + else + { + vertexSpec = line.substr(startNum, line.size() - startNum); + } + + string::size_type slashPos = vertexSpec.find('/'); + + label vertI = 0; + if (slashPos != string::npos) + { + IStringStream intStream(vertexSpec.substr(0, slashPos)); + + intStream >> vertI; + } + else + { + IStringStream intStream(vertexSpec); + + intStream >> vertI; + } + + edgeRead[nVerts++] = (vertI - 1); // change to zero-offset + } + + if (nVerts >= 2) + { + dynUsedPoints[edgeRead[0]] = edgeRead[0]; + dynUsedPoints[edgeRead[1]] = edgeRead[1]; + + dynEdges.append(edgeRead); + } + } + } + + // cull unused points + label nUsed = 0; + + forAll(dynPoints, pointI) + { + if (dynUsedPoints[pointI] >= 0) + { + if (nUsed != pointI) + { + dynPoints[nUsed] = dynPoints[pointI]; + dynUsedPoints[pointI] = nUsed; // new position + } + ++nUsed; + } + } + + dynPoints.setSize(nUsed); + + // transfer to normal lists + storedPoints().transfer(dynPoints); + + // renumber edge vertices + if (nUsed != dynUsedPoints.size()) + { + forAll(dynEdges, edgeI) + { + edge& e = dynEdges[edgeI]; + + e[0] = dynUsedPoints[e[0]]; + e[1] = dynUsedPoints[e[1]]; + } + } + storedEdges().transfer(dynEdges); + + return true; +} + + +void Foam::fileFormats::OBJedgeFormat::write +( + const fileName& filename, + const edgeMesh& mesh +) +{ + const pointField& pointLst = mesh.points(); + const edgeList& edgeLst = mesh.edges(); + + OFstream os(filename); + if (!os.good()) + { + FatalErrorIn + ( + "fileFormats::OBJedgeFormat::write" + "(const fileName&, const edgeMesh&)" + ) + << "Cannot open file for writing " << filename + << exit(FatalError); + } + + + os << "# Wavefront OBJ file written " << clock::dateTime().c_str() << nl + << "o " << os.name().lessExt().name() << nl + << nl + << "# points : " << pointLst.size() << nl + << "# lines : " << edgeLst.size() << nl; + + os << nl + << "# <points count=\"" << pointLst.size() << "\">" << nl; + + // Write vertex coords + forAll(pointLst, ptI) + { + const point& p = pointLst[ptI]; + + os << "v " << p.x() << ' ' << p.y() << ' ' << p.z() << nl; + } + + os << "# </points>" << nl + << nl + << "# <edges count=\"" << edgeLst.size() << "\">" << endl; + + // Write line connectivity + forAll(edgeLst, edgeI) + { + const edge& e = edgeLst[edgeI]; + + os << "l " << (e[0] + 1) << " " << (e[1] + 1) << nl; + } + os << "# </edges>" << endl; +} + + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/obj/OBJedgeFormat.H b/src/edgeMesh/edgeFormats/obj/OBJedgeFormat.H new file mode 100644 index 0000000000000000000000000000000000000000..d6eab70b606930abf4d4de01e36bde8a6e1728e8 --- /dev/null +++ b/src/edgeMesh/edgeFormats/obj/OBJedgeFormat.H @@ -0,0 +1,121 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 + +Class + Foam::fileFormats::OBJedgeFormat + +Description + Provide a means of reading/writing Alias/Wavefront OBJ format. + + Does not handle negative vertex indices. + +SourceFiles + OBJedgeFormat.C + +\*---------------------------------------------------------------------------*/ + +#ifndef OBJedgeFormat_H +#define OBJedgeFormat_H + +#include "edgeMesh.H" +#include "IFstream.H" +#include "Ostream.H" +#include "OFstream.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +/*---------------------------------------------------------------------------*\ + Class OBJedgeFormat Declaration +\*---------------------------------------------------------------------------*/ + +class OBJedgeFormat +: + public edgeMesh +{ + // Private Member Functions + + //- Disallow default bitwise copy construct + OBJedgeFormat(const OBJedgeFormat&); + + //- Disallow default bitwise assignment + void operator=(const OBJedgeFormat&); + + +public: + + // Constructors + + //- Construct from file name + OBJedgeFormat(const fileName&); + + + // Selectors + + //- Read file and return surface + static autoPtr<edgeMesh> New(const fileName& name) + { + return autoPtr<edgeMesh> + ( + new OBJedgeFormat(name) + ); + } + + + // Destructor + + virtual ~OBJedgeFormat() + {} + + + // Member Functions + + //- Write surface mesh components by proxy + static void write(const fileName&, const edgeMesh&); + + //- Read from file + virtual bool read(const fileName&); + + //- Write object file + virtual void write(const fileName& name) const + { + write(name, *this); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fileFormats +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/obj/OBJedgeFormatRunTime.C b/src/edgeMesh/edgeFormats/obj/OBJedgeFormatRunTime.C new file mode 100644 index 0000000000000000000000000000000000000000..b223f44c69bb4c1b1879a6af489a5fae3c2e9884 --- /dev/null +++ b/src/edgeMesh/edgeFormats/obj/OBJedgeFormatRunTime.C @@ -0,0 +1,61 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "OBJedgeFormat.H" + +#include "addToRunTimeSelectionTable.H" +#include "addToMemberFunctionSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +// read edgeMesh +addNamedToRunTimeSelectionTable +( + edgeMesh, + OBJedgeFormat, + fileExtension, + obj +); + +// write edgeMesh +addNamedToMemberFunctionSelectionTable +( + edgeMesh, + OBJedgeFormat, + write, + fileExtension, + obj +); + +} +} + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.C b/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.C new file mode 100644 index 0000000000000000000000000000000000000000..b6ecd4d19e04eb8fe4f9956669bc9bc5486dfaec --- /dev/null +++ b/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.C @@ -0,0 +1,387 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "STARCDedgeFormat.H" +#include "ListOps.H" +#include "clock.H" +#include "PackedBoolList.H" +#include "IStringStream.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +inline void Foam::fileFormats::STARCDedgeFormat::writeLines +( + Ostream& os, + const edgeList& edges +) +{ + writeHeader(os, "CELL"); + + forAll(edges, edgeI) + { + const edge& e = edges[edgeI]; + const label cellId = edgeI + 1; + + os << cellId // includes 1 offset + << ' ' << starcdLineShape_ // 2(line) shape + << ' ' << e.size() + << ' ' << 401 // arbitrary value + << ' ' << starcdLineType_; // 5(line) + + os << nl << " " << cellId << " " + << (e[0]+1) << " " << (e[1]+1) << nl; + } +} + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +bool Foam::fileFormats::STARCDedgeFormat::readHeader +( + IFstream& is, + const word& signature +) +{ + if (!is.good()) + { + FatalErrorIn + ( + "fileFormats::STARCDedgeFormat::readHeader(...)" + ) + << "cannot read " << signature << " " << is.name() + << abort(FatalError); + } + + word header; + label majorVersion; + + string line; + + is.getLine(line); + IStringStream(line)() >> header; + + is.getLine(line); + IStringStream(line)() >> majorVersion; + + // add other checks ... + if (header != signature) + { + Info<< "header mismatch " << signature << " " << is.name() + << endl; + } + + return true; +} + + +void Foam::fileFormats::STARCDedgeFormat::writeHeader +( + Ostream& os, + const char* filetype +) +{ + os << "PROSTAR_" << filetype << nl + << 4000 + << " " << 0 + << " " << 0 + << " " << 0 + << " " << 0 + << " " << 0 + << " " << 0 + << " " << 0 + << endl; +} + + +bool Foam::fileFormats::STARCDedgeFormat::readPoints +( + IFstream& is, + pointField& points, + labelList& ids +) +{ + // + // read .vrt file + // ~~~~~~~~~~~~~~ + + if (!is.good()) + { + FatalErrorIn + ( + "fileFormats::STARCDedgeFormat::readPoints(...)" + ) + << "Cannot read file " << is.name() + << exit(FatalError); + } + + readHeader(is, "PROSTAR_VERTEX"); + + DynamicList<point> dynPoints; + DynamicList<label> dynPointId; // STAR-CD index of points + + label lineLabel; + while ((is >> lineLabel).good()) + { + scalar x, y, z; + + is >> x >> y >> z; + + dynPoints.append(point(x, y, z)); + dynPointId.append(lineLabel); + } + + points.transfer(dynPoints); + ids.transfer(dynPointId); + + return true; +} + + + +void Foam::fileFormats::STARCDedgeFormat::writePoints +( + Ostream& os, + const pointField& pointLst +) +{ + writeHeader(os, "VERTEX"); + + // Set the precision of the points data to 10 + os.precision(10); + + // force decimal point for Fortran input + os.setf(std::ios::showpoint); + + forAll(pointLst, ptI) + { + os + << ptI + 1 << " " + << pointLst[ptI].x() << " " + << pointLst[ptI].y() << " " + << pointLst[ptI].z() << nl; + } + os.flush(); +} + + +void Foam::fileFormats::STARCDedgeFormat::writeCase +( + Ostream& os, + const pointField& pointLst, + const label nEdges +) +{ + word caseName = os.name().lessExt().name(); + + os << "! STAR-CD file written " << clock::dateTime().c_str() << nl + << "! " << pointLst.size() << " points, " << nEdges << " lines" << nl + << "! case " << caseName << nl + << "! ------------------------------" << nl; + +// forAll(zoneLst, zoneI) +// { +// os << "ctable " << zoneI + 1 << " line" << nl +// << "ctname " << zoneI + 1 << " " +// << zoneLst[zoneI].name() << nl; +// } + + os << "! ------------------------------" << nl + << "*set icvo mxv - 1" << nl + << "vread " << caseName << ".vrt icvo,,,coded" << nl + << "cread " << caseName << ".cel icvo,,,add,coded" << nl + << "*set icvo" << nl + << "! end" << nl; + + os.flush(); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fileFormats::STARCDedgeFormat::STARCDedgeFormat +( + const fileName& filename +) +{ + read(filename); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::fileFormats::STARCDedgeFormat::read +( + const fileName& filename +) +{ + clear(); + + fileName baseName = filename.lessExt(); + + // STAR-CD index of points + List<label> pointId; + + // read points from .vrt file + readPoints + ( + IFstream(baseName + ".vrt")(), + storedPoints(), + pointId + ); + + // Build inverse mapping (STAR-CD pointId -> index) + Map<label> mapPointId(2*pointId.size()); + forAll(pointId, i) + { + mapPointId.insert(pointId[i], i); + } + pointId.clear(); + + // note which points were really used and which can be culled + PackedBoolList usedPoints(points().size()); + + // + // read .cel file + // ~~~~~~~~~~~~~~ + IFstream is(baseName + ".cel"); + if (!is.good()) + { + FatalErrorIn + ( + "fileFormats::STARCDedgeFormat::read(const fileName&)" + ) + << "Cannot read file " << is.name() + << exit(FatalError); + } + + readHeader(is, "PROSTAR_CELL"); + + DynamicList<edge> dynEdges; + + label lineLabel, shapeId, nLabels, cellTableId, typeId; + DynamicList<label> vertexLabels(64); + + while ((is >> lineLabel).good()) + { + is >> shapeId >> nLabels >> cellTableId >> typeId; + + vertexLabels.clear(); + vertexLabels.reserve(nLabels); + + // read indices - max 8 per line + for (label i = 0; i < nLabels; ++i) + { + label vrtId; + if ((i % 8) == 0) + { + is >> lineLabel; + } + is >> vrtId; + + // convert original vertex id to point label + vertexLabels.append(mapPointId[vrtId]); + } + + if (typeId == starcdLineType_) + { + if (vertexLabels.size() >= 2) + { + dynEdges.append(edge(vertexLabels[0], vertexLabels[1])); + + usedPoints.set(vertexLabels[0]); + usedPoints.set(vertexLabels[1]); + } + } + } + + mapPointId.clear(); + + // not all the points were used, cull them accordingly + if (unsigned(points().size()) != usedPoints.count()) + { + label nUsed = 0; + + pointField& pts = storedPoints(); + forAll(pts, pointI) + { + if (usedPoints.get(pointI)) + { + if (nUsed != pointI) + { + pts[nUsed] = pts[pointI]; + } + + // map prev -> new id + mapPointId.set(pointI, nUsed); + + ++nUsed; + } + } + + pts.setSize(nUsed); + + // renumber edge vertices + forAll(dynEdges, edgeI) + { + edge& e = dynEdges[edgeI]; + + e[0] = mapPointId[e[0]]; + e[1] = mapPointId[e[1]]; + } + } + + + storedEdges().transfer(dynEdges); + + return true; +} + + +void Foam::fileFormats::STARCDedgeFormat::write +( + const fileName& filename, + const edgeMesh& mesh +) +{ + const pointField& pointLst = mesh.points(); + const edgeList& edgeLst = mesh.edges(); + + fileName baseName = filename.lessExt(); + + writePoints(OFstream(baseName + ".vrt")(), pointLst); + writeLines(OFstream(baseName + ".cel")(), edgeLst); + + // write a simple .inp file + writeCase + ( + OFstream(baseName + ".inp")(), + pointLst, + edgeLst.size() + ); +} + + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.H b/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.H new file mode 100644 index 0000000000000000000000000000000000000000..31d4a8db5d957f0921391329135778dcba43b907 --- /dev/null +++ b/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.H @@ -0,0 +1,159 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 + +Class + Foam::fileFormats::STARCDedgeFormat + +Description + Read/write the lines from pro-STAR vrt/cel files. + +Note + Uses the extension @a .inp (input) to denote the format. + +See Also + Foam::meshReaders::STARCD + +SourceFiles + STARCDedgeFormat.C + +\*---------------------------------------------------------------------------*/ + +#ifndef STARCDedgeFormat_H +#define STARCDedgeFormat_H + +#include "edgeMesh.H" +#include "IFstream.H" +#include "Ostream.H" +#include "OFstream.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +/*---------------------------------------------------------------------------*\ + Class STARCDedgeFormat Declaration +\*---------------------------------------------------------------------------*/ + +class STARCDedgeFormat +: + public edgeMesh +{ + // Private Data + + //- STAR-CD identifier for line shapes (1d elements) + static const int starcdLineShape_ = 2; + + //- STAR-CD identifier for line type + static const int starcdLineType_ = 5; + + + // Private Member Functions + + static inline void writeLines + ( + Ostream&, + const edgeList& + ); + + //- Disallow default bitwise copy construct + STARCDedgeFormat(const STARCDedgeFormat&); + + //- Disallow default bitwise assignment + void operator=(const STARCDedgeFormat&); + + +protected: + + // Protected Member Functions + + static bool readHeader(IFstream&, const word&); + + static void writeHeader(Ostream&, const char* filetype); + + static bool readPoints(IFstream&, pointField&, labelList& ids); + + static void writePoints(Ostream&, const pointField&); + + static void writeCase + ( + Ostream&, + const pointField&, + const label nEdges + ); + +public: + + // Constructors + + //- Construct from file name + STARCDedgeFormat(const fileName&); + + + // Selectors + + //- Read file and return edgeMesh + static autoPtr<edgeMesh> New(const fileName& name) + { + return autoPtr<edgeMesh> + ( + new STARCDedgeFormat(name) + ); + } + + + //- Destructor + virtual ~STARCDedgeFormat() + {} + + + // Member Functions + + //- Write edge mesh + static void write(const fileName&, const edgeMesh&); + + //- Read from file + virtual bool read(const fileName&); + + //- Write object + virtual void write(const fileName& name) const + { + write(name, *this); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fileFormats +} // End namespace Foam + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormatRunTime.C b/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormatRunTime.C new file mode 100644 index 0000000000000000000000000000000000000000..192c0aeaab32e3ff6e2dc3e0a9885ffd8bd53a00 --- /dev/null +++ b/src/edgeMesh/edgeFormats/starcd/STARCDedgeFormatRunTime.C @@ -0,0 +1,61 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "STARCDedgeFormat.H" + +#include "addToRunTimeSelectionTable.H" +#include "addToMemberFunctionSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +// read edgeMesh +addNamedToRunTimeSelectionTable +( + edgeMesh, + STARCDedgeFormat, + fileExtension, + inp +); + +// write edgeMesh +addNamedToMemberFunctionSelectionTable +( + edgeMesh, + STARCDedgeFormat, + write, + fileExtension, + inp +); + +} +} + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/vtk/VTKedgeFormat.C b/src/edgeMesh/edgeFormats/vtk/VTKedgeFormat.C new file mode 100644 index 0000000000000000000000000000000000000000..4d78b20d4e913f727b06c908be7741e1a3d48196 --- /dev/null +++ b/src/edgeMesh/edgeFormats/vtk/VTKedgeFormat.C @@ -0,0 +1,107 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "VTKedgeFormat.H" +#include "OFstream.H" +#include "clock.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +void Foam::fileFormats::VTKedgeFormat::writeHeader +( + Ostream& os, + const pointField& pointLst +) +{ + // Write header + os << "# vtk DataFile Version 2.0" << nl + << "featureEdgeMesh written " << clock::dateTime().c_str() << nl + << "ASCII" << nl + << nl + << "DATASET POLYDATA" << nl; + + // Write vertex coords + os << "POINTS " << pointLst.size() << " float" << nl; + forAll(pointLst, ptI) + { + const point& pt = pointLst[ptI]; + + os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl; + } +} + + +void Foam::fileFormats::VTKedgeFormat::writeEdges +( + Ostream& os, + const UList<edge>& edgeLst +) +{ + os << "LINES " << edgeLst.size() << ' ' + << 3*edgeLst.size() << nl; + + forAll(edgeLst, edgeI) + { + const edge& e = edgeLst[edgeI]; + + os << "2 " << e[0] << ' ' << e[1] << nl; + } +} + + + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fileFormats::VTKedgeFormat::VTKedgeFormat() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void Foam::fileFormats::VTKedgeFormat::write +( + const fileName& filename, + const edgeMesh& eMesh +) +{ + OFstream os(filename); + if (!os.good()) + { + FatalErrorIn + ( + "fileFormats::VTKedgeFormat::write" + "(const fileName&, const edgeMesh&)" + ) + << "Cannot open file for writing " << filename + << exit(FatalError); + } + + writeHeader(os, eMesh.points()); + writeEdges(os, eMesh.edges()); +} + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/vtk/VTKedgeFormat.H b/src/edgeMesh/edgeFormats/vtk/VTKedgeFormat.H new file mode 100644 index 0000000000000000000000000000000000000000..af4b4b78eda1c45d2365a06560c643d22b20cd21 --- /dev/null +++ b/src/edgeMesh/edgeFormats/vtk/VTKedgeFormat.H @@ -0,0 +1,116 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 + +Class + Foam::fileFormats::VTKedgeFormat + +Description + Provide a means of writing VTK legacy format. + +SourceFiles + VTKedgeFormat.C + +\*---------------------------------------------------------------------------*/ + +#ifndef VTKedgeFormat_H +#define VTKedgeFormat_H + +#include "edgeMesh.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +/*---------------------------------------------------------------------------*\ + Class VTKedgeFormat Declaration +\*---------------------------------------------------------------------------*/ + +class VTKedgeFormat +: + public edgeMesh +{ + // Private Member Functions + + //- Disallow default bitwise copy construct + VTKedgeFormat(const VTKedgeFormat&); + + //- Disallow default bitwise assignment + void operator=(const VTKedgeFormat&); + + +protected: + + // Protected Member Functions + + //- Write header information with points + static void writeHeader + ( + Ostream&, + const pointField& + ); + + //- Write edges + static void writeEdges(Ostream&, const UList<edge>&); + +public: + + // Constructors + + //- Construct null + VTKedgeFormat(); + + + //- Destructor + virtual ~VTKedgeFormat() + {} + + + // Member Functions + + // Write + + //- Write edgeMesh + static void write(const fileName&, const edgeMesh&); + +// //- Write object +// virtual void write(Ostream& os) const +// { +// write(os, *this); +// } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fileFormats +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/vtk/VTKedgeFormatRunTime.C b/src/edgeMesh/edgeFormats/vtk/VTKedgeFormatRunTime.C new file mode 100644 index 0000000000000000000000000000000000000000..7a9f4b859f6a9ed2cef920af502c172c6cd16872 --- /dev/null +++ b/src/edgeMesh/edgeFormats/vtk/VTKedgeFormatRunTime.C @@ -0,0 +1,51 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "VTKedgeFormat.H" + +#include "addToRunTimeSelectionTable.H" +#include "addToMemberFunctionSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileFormats +{ + +addNamedToMemberFunctionSelectionTable +( + edgeMesh, + VTKedgeFormat, + write, + fileExtension, + vtk +); + +} +} + +// ************************************************************************* // diff --git a/src/edgeMesh/edgeMesh.C b/src/edgeMesh/edgeMesh.C index d39810a62d7223debef5b8eb2ae1b3b533dcc10c..06502ef282a18cfd1274846a726f0533ad11fd82 100644 --- a/src/edgeMesh/edgeMesh.C +++ b/src/edgeMesh/edgeMesh.C @@ -26,6 +26,76 @@ License #include "edgeMesh.H" #include "mergePoints.H" +#include "addToRunTimeSelectionTable.H" +#include "addToMemberFunctionSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineTypeNameAndDebug(edgeMesh, 0); + defineRunTimeSelectionTable(edgeMesh, fileExtension); + defineMemberFunctionSelectionTable(edgeMesh,write,fileExtension); +} + +Foam::wordHashSet Foam::edgeMesh::readTypes() +{ + return wordHashSet(*fileExtensionConstructorTablePtr_); +} + +Foam::wordHashSet Foam::edgeMesh::writeTypes() +{ + return wordHashSet(*writefileExtensionMemberFunctionTablePtr_); +} + + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +bool Foam::edgeMesh::canReadType +( + const word& ext, + const bool verbose +) +{ + return checkSupport + ( + readTypes(), + ext, + verbose, + "reading" + ); +} + +bool Foam::edgeMesh::canWriteType +( + const word& ext, + const bool verbose +) +{ + return checkSupport + ( + writeTypes(), + ext, + verbose, + "writing" + ); +} + + +bool Foam::edgeMesh::canRead +( + const fileName& name, + const bool verbose +) +{ + word ext = name.ext(); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + return canReadType(ext, verbose); +} + // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -63,10 +133,10 @@ void Foam::edgeMesh::calcPointEdges() const forAll(edges_, edgeI) { const edge& e = edges_[edgeI]; + const label p0 = e[0]; + const label p1 = e[1]; - label p0 = e[0]; pointEdges[p0][nEdgesPerPoint[p0]++] = edgeI; - label p1 = e[1]; pointEdges[p1][nEdgesPerPoint[p1]++] = edgeI; } } @@ -74,32 +144,102 @@ void Foam::edgeMesh::calcPointEdges() const // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// construct from components -Foam::edgeMesh::edgeMesh(const pointField& points, const edgeList& edges) +Foam::edgeMesh::edgeMesh() +: + points_(0), + edges_(0), + pointEdgesPtr_(NULL) +{} + + +Foam::edgeMesh::edgeMesh +( + const pointField& points, + const edgeList& edges +) : points_(points), - edges_(edges) + edges_(edges), + pointEdgesPtr_(NULL) {} -// construct as copy -Foam::edgeMesh::edgeMesh(const edgeMesh& em) +Foam::edgeMesh::edgeMesh +( + const Xfer<pointField>& pointLst, + const Xfer<edgeList>& edgeLst +) : - points_(em.points_), - edges_(em.edges_), + points_(0), + edges_(0), pointEdgesPtr_(NULL) +{ + points_.transfer(pointLst()); + edges_.transfer(edgeLst()); +} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::edgeMesh::~edgeMesh() {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +void Foam::edgeMesh::clear() +{ + points_.clear(); + edges_.clear(); + pointEdgesPtr_.clear(); +} + + +void Foam::edgeMesh::reset +( + const Xfer< pointField >& pointLst, + const Xfer< edgeList >& edgeLst +) +{ + // Take over new primitive data. + // Optimized to avoid overwriting data at all + if (&pointLst) + { + points_.transfer(pointLst()); + } + + if (&edgeLst) + { + edges_.transfer(edgeLst()); + + // connectivity likely changed + pointEdgesPtr_.clear(); + } + +} + + +void Foam::edgeMesh::transfer(edgeMesh& mesh) +{ + points_.transfer(mesh.points_); + edges_.transfer(mesh.edges_); + pointEdgesPtr_ = mesh.pointEdgesPtr_; +} + + +Foam::Xfer< Foam::edgeMesh > +Foam::edgeMesh::xfer() +{ + return xferMove(*this); +} + + Foam::label Foam::edgeMesh::regions(labelList& edgeRegion) const { edgeRegion.setSize(edges_.size()); edgeRegion = -1; label startEdgeI = 0; - label currentRegion = 0; while (true) @@ -159,6 +299,16 @@ Foam::label Foam::edgeMesh::regions(labelList& edgeRegion) const } +void Foam::edgeMesh::scalePoints(const scalar scaleFactor) +{ + // avoid bad scaling + if (scaleFactor > 0 && scaleFactor != 1.0) + { + points_ *= scaleFactor; + } +} + + void Foam::edgeMesh::mergePoints(const scalar mergeDist) { pointField newPoints; @@ -180,7 +330,7 @@ void Foam::edgeMesh::mergePoints(const scalar mergeDist) points_.transfer(newPoints); - // Renumber and make sure e[0] < e[1] (not really nessecary) + // Renumber and make sure e[0] < e[1] (not really necessary) forAll(edges_, edgeI) { edge& e = edges_[edgeI]; @@ -243,7 +393,7 @@ void Foam::edgeMesh::operator=(const edgeMesh& rhs) { points_ = rhs.points_; edges_ = rhs.edges_; - pointEdgesPtr_.reset(NULL); + pointEdgesPtr_.clear(); } diff --git a/src/edgeMesh/edgeMesh.H b/src/edgeMesh/edgeMesh.H index 55934f3824c5a628ee62177b430f246e2ae6f03c..952a5378ed3814bf1ea0d8242e5cefe700f1220a 100644 --- a/src/edgeMesh/edgeMesh.H +++ b/src/edgeMesh/edgeMesh.H @@ -26,7 +26,7 @@ Class Foam::edgeMesh Description - points connected by edges. + Points connected by edges. SourceFiles edgeMeshI.H @@ -40,17 +40,33 @@ SourceFiles #include "pointField.H" #include "edgeList.H" +#include "edgeFormatsCore.H" +#include "runTimeSelectionTables.H" +#include "memberFunctionSelectionTables.H" +#include "HashSet.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +// Forward declaration of classes +class Istream; +class Ostream; + +// Forward declaration of friend functions and operators +class edgeMesh; +Istream& operator>>(Istream&, edgeMesh&); +Ostream& operator<<(Ostream&, const edgeMesh&); + + /*---------------------------------------------------------------------------*\ Class edgeMesh Declaration \*---------------------------------------------------------------------------*/ class edgeMesh +: + public fileFormats::edgeFormatsCore { // Private data @@ -68,39 +84,179 @@ class edgeMesh //- Calculate point-edge addressing (inverse of edges) void calcPointEdges() const; + +protected: + + // Protected Member Functions + + //- Non-const access to global points + inline pointField& storedPoints(); + + //- Non-const access to the edges + inline edgeList& storedEdges(); + + public: + //- Runtime type information + TypeName("edgeMesh"); + + // Static + + //- Can we read this file format? + static bool canRead(const fileName&, const bool verbose=false); + + //- Can we read this file format? + static bool canReadType(const word& ext, const bool verbose=false); + + //- Can we write this file format type? + static bool canWriteType(const word& ext, const bool verbose=false); + + static wordHashSet readTypes(); + static wordHashSet writeTypes(); + // Constructors + //- Construct null + edgeMesh(); + //- Construct from components edgeMesh(const pointField&, const edgeList&); - //- Construct from file + //- Construct by transferring components (points, edges). + edgeMesh + ( + const Xfer< pointField >&, + const Xfer< edgeList >& + ); + + //- Construct as copy + edgeMesh(const edgeMesh&); + + //- Construct from file name (uses extension to determine type) edgeMesh(const fileName&); + //- Construct from file name (uses extension to determine type) + edgeMesh(const fileName&, const word& ext); + //- Construct from Istream edgeMesh(Istream&); - //- Construct as copy - edgeMesh(const edgeMesh&); + // Declare run-time constructor selection table + + declareRunTimeSelectionTable + ( + autoPtr, + edgeMesh, + fileExtension, + ( + const fileName& name + ), + (name) + ); + + // Selectors + + //- Select constructed from filename (explicit extension) + static autoPtr<edgeMesh> New + ( + const fileName&, + const word& ext + ); + //- Select constructed from filename (implicit extension) + static autoPtr<edgeMesh> New(const fileName&); + + + //- Destructor + virtual ~edgeMesh(); + + + // Member Function Selectors + + declareMemberFunctionSelectionTable + ( + void, + edgeMesh, + write, + fileExtension, + ( + const fileName& name, + const edgeMesh& mesh + ), + (name, mesh) + ); + + //- Write to file + static void write(const fileName&, const edgeMesh&); // Member Functions + //- Transfer the contents of the argument and annul the argument + void transfer(edgeMesh&); + + //- Transfer contents to the Xfer container + Xfer< edgeMesh > xfer(); + + // Read + + //- Read from file. Chooses reader based on explicit extension + bool read(const fileName&, const word& ext); + + //- Read from file. Chooses reader based on detected extension + virtual bool read(const fileName&); + + + // Access + + //- Return points inline const pointField& points() const; + //- Return edges inline const edgeList& edges() const; + //- Return edges inline const labelListList& pointEdges() const; //- Find connected regions. Set region number per edge. // Returns number of regions. label regions(labelList& edgeRegion) const; + + // Edit + + //- Clear all storage + virtual void clear(); + + //- Reset primitive data (points, edges) + // Note, optimized to avoid overwriting data (with Xfer::null) + virtual void reset + ( + const Xfer< pointField >& points, + const Xfer< edgeList >& edges + ); + + //- Scale points. A non-positive factor is ignored + virtual void scalePoints(const scalar); + //- Merge common points (points within mergeDist) void mergePoints(const scalar mergeDist); + + // Write + + void writeStats(Ostream&) const; + + // Write + + //- Generic write routine. Chooses writer based on extension. + virtual void write(const fileName& name) const + { + write(name, *this); + } + + // Member Operators inline void operator=(const edgeMesh&); @@ -109,6 +265,7 @@ public: friend Ostream& operator<<(Ostream&, const edgeMesh&); friend Istream& operator>>(Istream&, edgeMesh&); + }; diff --git a/src/edgeMesh/edgeMeshI.H b/src/edgeMesh/edgeMeshI.H index 85951e8735d0990b98744c7e16d2de089be35513..e0dbb30a08d2b6476fdd76c6780750bead5ee789 100644 --- a/src/edgeMesh/edgeMeshI.H +++ b/src/edgeMesh/edgeMeshI.H @@ -24,14 +24,14 @@ License \*---------------------------------------------------------------------------*/ - -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // - -// * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * // +inline Foam::edgeMesh::edgeMesh(const edgeMesh& em) +: + points_(em.points_), + edges_(em.edges_), + pointEdgesPtr_(NULL) +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -58,15 +58,16 @@ inline const Foam::labelListList& Foam::edgeMesh::pointEdges() const } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * * // - +inline Foam::pointField& Foam::edgeMesh::storedPoints() +{ + return points_; +} -// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // +inline Foam::edgeList& Foam::edgeMesh::storedEdges() +{ + return edges_; +} -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // ************************************************************************* // diff --git a/src/edgeMesh/edgeMeshIO.C b/src/edgeMesh/edgeMeshIO.C index e2dffb864bcd9f5f4fe8c95db0d28e273d286a6f..9b81c7b81f1467ae3b2ac7545c5fffcabe51f91b 100644 --- a/src/edgeMesh/edgeMeshIO.C +++ b/src/edgeMesh/edgeMeshIO.C @@ -25,42 +25,107 @@ License \*---------------------------------------------------------------------------*/ #include "edgeMesh.H" -#include "IFstream.H" - +#include "boundBox.H" +#include "EMESHedgeFormat.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// construct from file -Foam::edgeMesh::edgeMesh(const fileName& fname) +Foam::edgeMesh::edgeMesh +( + const fileName& name, + const word& ext +) +: + points_(0), + edges_(0), + pointEdgesPtr_(NULL) +{ + read(name, ext); +} + + +Foam::edgeMesh::edgeMesh(const fileName& name) : points_(0), edges_(0), pointEdgesPtr_(NULL) { - IFstream is(fname); + read(name); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - if (is.good()) +bool Foam::edgeMesh::read(const fileName& name) +{ + word ext = name.ext(); + if (ext == "gz") { - is >> points_ >> edges_; + fileName unzipName = name.lessExt(); + return read(unzipName, unzipName.ext()); } else { - FatalErrorIn("edgeMesh::edgeMesh(const fileName&)") - << "cannot open file " << fname - << abort(FatalError); + return read(name, ext); } } -// construct from Istream -Foam::edgeMesh::edgeMesh(Istream& is) -: - points_(is), - edges_(is), - pointEdgesPtr_(NULL) +// Read from file in given format +bool Foam::edgeMesh::read +( + const fileName& name, + const word& ext +) { - // Check state of Istream - is.check("edgeMesh::edgeMesh(Istream&)"); + // read via selector mechanism + transfer(New(name, ext)()); + return true; +} + + +void Foam::edgeMesh::write +( + const fileName& name, + const edgeMesh& mesh +) +{ + if (debug) + { + Info<< "edgeMesh::write" + "(const fileName&, const edgeMesh&) : " + "writing to " << name + << endl; + } + + const word ext = name.ext(); + + writefileExtensionMemberFunctionTable::iterator mfIter = + writefileExtensionMemberFunctionTablePtr_->find(ext); + + if (mfIter == writefileExtensionMemberFunctionTablePtr_->end()) + { + FatalErrorIn + ( + "MeshedSurface::write" + "(const fileName&, const MeshedSurface&)" + ) << "Unknown file extension " << ext << nl << nl + << "Valid types are :" << endl + << writefileExtensionMemberFunctionTablePtr_->sortedToc() + << exit(FatalError); + } + else + { + mfIter()(name, mesh); + } +} + + +void Foam::edgeMesh::writeStats(Ostream& os) const +{ + os << "points : " << points().size() << nl; + os << "edges : " << edges().size() << nl; + os << "boundingBox : " << boundBox(this->points()) << endl; } @@ -68,7 +133,7 @@ Foam::edgeMesh::edgeMesh(Istream& is) Foam::Ostream& Foam::operator<<(Ostream& os, const edgeMesh& em) { - os << em.points_ << nl << em.edges_ << endl; + fileFormats::EMESHedgeFormat::write(os, em.points_, em.edges_); // Check state of Ostream os.check("Ostream& operator<<(Ostream&, const edgeMesh&)"); @@ -79,7 +144,9 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const edgeMesh& em) Foam::Istream& Foam::operator>>(Istream& is, edgeMesh& em) { - is >> em.points_ >> em.edges_; + fileFormats::EMESHedgeFormat::read(is, em.points_, em.edges_); + + em.pointEdgesPtr_.clear(); // Check state of Istream is.check("Istream& operator>>(Istream&, edgeMesh&)"); diff --git a/src/edgeMesh/edgeMeshNew.C b/src/edgeMesh/edgeMeshNew.C new file mode 100644 index 0000000000000000000000000000000000000000..4d63a6153634099c90270228e88bbfecfe3b5acb --- /dev/null +++ b/src/edgeMesh/edgeMeshNew.C @@ -0,0 +1,65 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2009-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 "edgeMesh.H" + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + + +Foam::autoPtr< Foam::edgeMesh > +Foam::edgeMesh::New(const fileName& name, const word& ext) +{ + fileExtensionConstructorTable::iterator cstrIter = + fileExtensionConstructorTablePtr_->find(ext); + + if (cstrIter == fileExtensionConstructorTablePtr_->end()) + { + FatalErrorIn + ( + "edgeMesh<Face>::New(const fileName&, const word&) : " + "constructing edgeMesh" + ) << "Unknown file extension " << ext << nl << nl + << "Valid types are :" << nl + << fileExtensionConstructorTablePtr_->sortedToc() + << exit(FatalError); + } + + return autoPtr< edgeMesh >(cstrIter()(name)); +} + + +Foam::autoPtr< Foam::edgeMesh > +Foam::edgeMesh::New(const fileName& name) +{ + word ext = name.ext(); + if (ext == "gz") + { + ext = name.lessExt().ext(); + } + return New(name, ext); +} + +// ************************************************************************* // diff --git a/src/edgeMesh/featureEdgeMesh.C b/src/edgeMesh/featureEdgeMesh/featureEdgeMesh.C similarity index 81% rename from src/edgeMesh/featureEdgeMesh.C rename to src/edgeMesh/featureEdgeMesh/featureEdgeMesh.C index dab3c2cbb5ff836dd598c3d244d07b30929e3e6f..a579d4ff144dd4821bb76645f76013a55f300de7 100644 --- a/src/edgeMesh/featureEdgeMesh.C +++ b/src/edgeMesh/featureEdgeMesh/featureEdgeMesh.C @@ -28,21 +28,7 @@ License // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -namespace Foam -{ - -defineTypeNameAndDebug(featureEdgeMesh, 0); - -} - - -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // +defineTypeNameAndDebug(Foam::featureEdgeMesh, 0); // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -73,49 +59,54 @@ Foam::featureEdgeMesh::featureEdgeMesh(const IOobject& io) } -//- Construct from components Foam::featureEdgeMesh::featureEdgeMesh ( const IOobject& io, - const pointField& points, - const edgeList& edges + const featureEdgeMesh& em ) : regIOobject(io), - edgeMesh(points, edges) + edgeMesh(em) {} -// Construct as copy Foam::featureEdgeMesh::featureEdgeMesh ( const IOobject& io, - const featureEdgeMesh& em + const Xfer< pointField >& pointLst, + const Xfer< edgeList >& edgeLst ) : regIOobject(io), - edgeMesh(em) -{} + edgeMesh(pointLst, edgeLst) +{ + if + ( + io.readOpt() == IOobject::MUST_READ + || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk()) + ) + { + readStream(typeName) >> *this; + close(); + } +} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // bool Foam::featureEdgeMesh::readData(Istream& is) { - is >> *this; + is >> *this; return !is.bad(); } bool Foam::featureEdgeMesh::writeData(Ostream& os) const { - os << *this; + os << *this; return os.good(); } -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - - // ************************************************************************* // diff --git a/src/edgeMesh/featureEdgeMesh.H b/src/edgeMesh/featureEdgeMesh/featureEdgeMesh.H similarity index 85% rename from src/edgeMesh/featureEdgeMesh.H rename to src/edgeMesh/featureEdgeMesh/featureEdgeMesh.H index b22859e7993759dbf84b34b31036b5156530a620..8fc7b367440801764da6effabc1258e1bf5d1a92 100644 --- a/src/edgeMesh/featureEdgeMesh.H +++ b/src/edgeMesh/featureEdgeMesh/featureEdgeMesh.H @@ -45,7 +45,7 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class featureEdgeMesh Declaration + Class featureEdgeMesh Declaration \*---------------------------------------------------------------------------*/ class featureEdgeMesh @@ -56,25 +56,28 @@ class featureEdgeMesh public: + //- Runtime type information TypeName("featureEdgeMesh"); // Constructors //- Construct (read) given an IOobject - featureEdgeMesh(const IOobject&); + explicit featureEdgeMesh(const IOobject&); - //- Construct from featureEdgeMesh data + //- Construct as copy + explicit featureEdgeMesh(const IOobject&, const featureEdgeMesh&); + + //- Construct by transferring components (points, edges) featureEdgeMesh ( const IOobject&, - const pointField&, - const edgeList& + const Xfer<pointField>&, + const Xfer<edgeList>& ); - //- Construct as copy - featureEdgeMesh(const IOobject&, const featureEdgeMesh&); - + //- Give precedence to the regIOobject write + using regIOobject::write; //- ReadData function required for regIOobject read operation virtual bool readData(Istream&);