OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

writeMeshObj.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     For mesh debugging: writes mesh as three separate OBJ files which can
00027     be viewed with e.g. javaview.
00028 
00029     meshPoints_XXX.obj : all points and edges as lines.
00030     meshFaceCentres_XXX.obj : all face centres.
00031     meshCellCentres_XXX.obj : all cell centres.
00032 
00033     patch_YYY_XXX.obj : all face centres of patch YYY
00034 
00035     Optional: patch faces (as polygons) : patchFaces_YYY_XXX.obj
00036 
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #include "argList.H"
00040 #include "timeSelector.H"
00041 #include "Time.H"
00042 #include "polyMesh.H"
00043 #include "OFstream.H"
00044 #include "meshTools.H"
00045 #include "cellSet.H"
00046 #include "faceSet.H"
00047 
00048 using namespace Foam;
00049 
00050 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00051 
00052 void writeOBJ(const point& pt, Ostream& os)
00053 {
00054     os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00055 }
00056 
00057 // All edges of mesh
00058 void writePoints(const polyMesh& mesh, const fileName& timeName)
00059 {
00060     label vertI = 0;
00061 
00062     fileName pointFile(mesh.time().path()/"meshPoints_" + timeName + ".obj");
00063 
00064     Info << "Writing mesh points and edges to " << pointFile << endl;
00065 
00066     OFstream pointStream(pointFile);
00067 
00068     forAll(mesh.points(), pointI)
00069     {
00070         writeOBJ(mesh.points()[pointI], pointStream);
00071         vertI++;
00072     }
00073 
00074     forAll(mesh.edges(), edgeI)
00075     {
00076         const edge& e = mesh.edges()[edgeI];
00077 
00078         pointStream << "l " << e.start() + 1 << ' ' << e.end() + 1
00079             << endl;
00080     }
00081 }
00082 
00083 
00084 // Edges for subset of cells
00085 void writePoints
00086 (
00087     const polyMesh& mesh,
00088     const labelList& cellLabels,
00089     const fileName& timeName
00090 )
00091 {
00092     fileName fName(mesh.time().path()/"meshPoints_" + timeName + ".obj");
00093 
00094     Info << "Writing mesh points and edges to " << fName << endl;
00095 
00096     OFstream str(fName);
00097 
00098     // OBJ file vertex
00099     label vertI = 0;
00100 
00101     // From point to OBJ file vertex
00102     Map<label> pointToObj(6*cellLabels.size());
00103 
00104     forAll(cellLabels, i)
00105     {
00106         label cellI = cellLabels[i];
00107 
00108         const labelList& cEdges = mesh.cellEdges()[cellI];
00109 
00110         forAll(cEdges, cEdgeI)
00111         {
00112             const edge& e = mesh.edges()[cEdges[cEdgeI]];
00113 
00114             label v0;
00115 
00116             Map<label>::iterator e0Fnd = pointToObj.find(e[0]);
00117 
00118             if (e0Fnd == pointToObj.end())
00119             {
00120                 meshTools::writeOBJ(str, mesh.points()[e[0]]);
00121                 v0 = vertI++;
00122                 pointToObj.insert(e[0], v0);
00123             }
00124             else
00125             {
00126                 v0 = e0Fnd();
00127             }
00128 
00129             label v1;
00130 
00131             Map<label>::iterator e1Fnd = pointToObj.find(e[1]);
00132 
00133             if (e1Fnd == pointToObj.end())
00134             {
00135                 meshTools::writeOBJ(str, mesh.points()[e[1]]);
00136                 v1 = vertI++;
00137                 pointToObj.insert(e[1], v1);
00138             }
00139             else
00140             {
00141                 v1 = e1Fnd();
00142             }
00143 
00144 
00145             str << "l " << v0+1 << ' ' << v1+1 << nl;
00146         }
00147     }
00148 }
00149 
00150 
00151 // Edges of single cell
00152 void writePoints
00153 (
00154     const polyMesh& mesh,
00155     const label cellI,
00156     const fileName& timeName
00157 )
00158 {
00159     fileName fName
00160     (
00161         mesh.time().path()
00162       / "meshPoints_" + timeName + '_' + name(cellI) + ".obj"
00163     );
00164 
00165     Info << "Writing mesh points and edges to " << fName << endl;
00166 
00167     OFstream pointStream(fName);
00168 
00169     const cell& cFaces = mesh.cells()[cellI];
00170 
00171     meshTools::writeOBJ(pointStream, mesh.faces(), mesh.points(), cFaces);
00172 }
00173 
00174 
00175 
00176 // All face centres
00177 void writeFaceCentres(const polyMesh& mesh,const fileName& timeName)
00178 {
00179     fileName faceFile
00180     (
00181         mesh.time().path()
00182       / "meshFaceCentres_" + timeName + ".obj"
00183     );
00184 
00185     Info << "Writing mesh face centres to " << faceFile << endl;
00186 
00187     OFstream faceStream(faceFile);
00188 
00189     forAll(mesh.faceCentres(), faceI)
00190     {
00191         writeOBJ(mesh.faceCentres()[faceI], faceStream);
00192     }
00193 }
00194 
00195 
00196 void writeCellCentres(const polyMesh& mesh, const fileName& timeName)
00197 {
00198     fileName cellFile
00199     (
00200         mesh.time().path()/"meshCellCentres_" + timeName + ".obj"
00201     );
00202 
00203     Info << "Writing mesh cell centres to " << cellFile << endl;
00204 
00205     OFstream cellStream(cellFile);
00206 
00207     forAll(mesh.cellCentres(), cellI)
00208     {
00209         writeOBJ(mesh.cellCentres()[cellI], cellStream);
00210     }
00211 }
00212 
00213 
00214 void writePatchCentres
00215 (
00216     const polyMesh& mesh,
00217     const fileName& timeName
00218 )
00219 {
00220     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00221 
00222     forAll(patches, patchI)
00223     {
00224         const polyPatch& pp = patches[patchI];
00225 
00226         fileName faceFile
00227         (
00228             mesh.time().path()/"patch_" + pp.name() + '_' + timeName + ".obj"
00229         );
00230 
00231         Info << "Writing patch face centres to " << faceFile << endl;
00232 
00233         OFstream patchFaceStream(faceFile);
00234 
00235         forAll(pp.faceCentres(), faceI)
00236         {
00237             writeOBJ(pp.faceCentres()[faceI], patchFaceStream);
00238         }
00239     }
00240 }
00241 
00242 
00243 void writePatchFaces
00244 (
00245     const polyMesh& mesh,
00246     const fileName& timeName
00247 )
00248 {
00249     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00250 
00251     forAll(patches, patchI)
00252     {
00253         const polyPatch& pp = patches[patchI];
00254 
00255         fileName faceFile
00256         (
00257             mesh.time().path()
00258           / "patchFaces_" + pp.name() + '_' + timeName + ".obj"
00259         );
00260 
00261         Info << "Writing patch faces to " << faceFile << endl;
00262 
00263         OFstream patchFaceStream(faceFile);
00264 
00265         forAll(pp.localPoints(), pointI)
00266         {
00267             writeOBJ(pp.localPoints()[pointI], patchFaceStream);
00268         }
00269 
00270         forAll(pp.localFaces(), faceI)
00271         {
00272             const face& f = pp.localFaces()[faceI];
00273 
00274             patchFaceStream<< 'f';
00275 
00276             forAll(f, fp)
00277             {
00278                 patchFaceStream << ' ' << f[fp]+1;
00279             }
00280             patchFaceStream << endl;
00281         }
00282     }
00283 }
00284 
00285 
00286 void writePointCells
00287 (
00288     const polyMesh& mesh,
00289     const label pointI,
00290     const fileName& timeName
00291 )
00292 {
00293     const labelList& pCells = mesh.pointCells()[pointI];
00294 
00295     labelHashSet allEdges(6*pCells.size());
00296 
00297     forAll(pCells, i)
00298     {
00299         const labelList& cEdges = mesh.cellEdges()[pCells[i]];
00300 
00301         forAll(cEdges, i)
00302         {
00303             allEdges.insert(cEdges[i]);
00304         }
00305     }
00306 
00307 
00308     fileName pFile
00309     (
00310         mesh.time().path()
00311       / "pointEdges_" + timeName + '_' + name(pointI) + ".obj"
00312     );
00313 
00314     Info << "Writing pointEdges to " << pFile << endl;
00315 
00316     OFstream pointStream(pFile);
00317 
00318     label vertI = 0;
00319 
00320     for
00321     (
00322         labelHashSet::const_iterator iter = allEdges.begin();
00323         iter != allEdges.end();
00324         ++iter
00325     )
00326     {
00327         const edge& e = mesh.edges()[iter.key()];
00328 
00329         meshTools::writeOBJ(pointStream, mesh.points()[e[0]]); vertI++;
00330         meshTools::writeOBJ(pointStream, mesh.points()[e[1]]); vertI++;
00331         pointStream<< "l " << vertI-1 << ' ' << vertI << nl;
00332     }
00333 }
00334 
00335 
00336 // Main program:
00337 
00338 int main(int argc, char *argv[])
00339 {
00340     timeSelector::addOptions();
00341     argList::validOptions.insert("patchFaces", "");
00342     argList::validOptions.insert("cell", "cellI");
00343     argList::validOptions.insert("face", "faceI");
00344     argList::validOptions.insert("point", "pointI");
00345     argList::validOptions.insert("cellSet", "setName");
00346     argList::validOptions.insert("faceSet", "setName");
00347 #   include "addRegionOption.H"
00348 
00349 #   include "setRootCase.H"
00350 #   include "createTime.H"
00351     runTime.functionObjects().off();
00352 
00353     bool patchFaces = args.optionFound("patchFaces");
00354     bool doCell     = args.optionFound("cell");
00355     bool doPoint    = args.optionFound("point");
00356     bool doFace     = args.optionFound("face");
00357     bool doCellSet  = args.optionFound("cellSet");
00358     bool doFaceSet  = args.optionFound("faceSet");
00359 
00360 
00361     Info<< "Writing mesh objects as .obj files such that the object"
00362         << " numbering" << endl
00363         << "(for points, faces, cells) is consistent with"
00364         << " Foam numbering (starting from 0)." << endl << endl;
00365 
00366     instantList timeDirs = timeSelector::select0(runTime, args);
00367 
00368 #   include "createNamedPolyMesh.H"
00369 
00370     forAll(timeDirs, timeI)
00371     {
00372         runTime.setTime(timeDirs[timeI], timeI);
00373 
00374         Info<< "Time = " << runTime.timeName() << endl;
00375 
00376         polyMesh::readUpdateState state = mesh.readUpdate();
00377 
00378         if (!timeI || state != polyMesh::UNCHANGED)
00379         {
00380             if (patchFaces)
00381             {
00382                 writePatchFaces(mesh, runTime.timeName());
00383             }
00384             else if (doCell)
00385             {
00386                 label cellI = args.optionRead<label>("cell");
00387 
00388                 writePoints(mesh, cellI, runTime.timeName());
00389             }
00390             else if (doPoint)
00391             {
00392                 label pointI = args.optionRead<label>("point");
00393 
00394                 writePointCells(mesh, pointI, runTime.timeName());
00395             }
00396             else if (doFace)
00397             {
00398                 label faceI = args.optionRead<label>("face");
00399 
00400                 fileName fName
00401                 (
00402                     mesh.time().path()
00403                   / "meshPoints_"
00404                   + runTime.timeName()
00405                   + '_'
00406                   + name(faceI)
00407                   + ".obj"
00408                 );
00409 
00410                 Info<< "Writing mesh points and edges to " << fName << endl;
00411 
00412                 OFstream str(fName);
00413 
00414                 const face& f = mesh.faces()[faceI];
00415 
00416                 meshTools::writeOBJ(str, faceList(1, f), mesh.points());
00417             }
00418             else if (doCellSet)
00419             {
00420                 word setName(args.option("cellSet"));
00421 
00422                 cellSet cells(mesh, setName);
00423 
00424                 Info<< "Read " << cells.size() << " cells from set " << setName
00425                     << endl;
00426 
00427                 writePoints(mesh, cells.toc(), runTime.timeName());
00428 
00429             }
00430             else if (doFaceSet)
00431             {
00432                 word setName(args.option("faceSet"));
00433 
00434                 faceSet faces(mesh, setName);
00435 
00436                 Info<< "Read " << faces.size() << " faces from set " << setName
00437                     << endl;
00438 
00439                 fileName fName
00440                 (
00441                     mesh.time().path()
00442                   / "meshPoints_"
00443                   + runTime.timeName()
00444                   + '_'
00445                   + setName
00446                   + ".obj"
00447                 );
00448 
00449                 Info << "Writing mesh points and edges to " << fName << endl;
00450 
00451                 OFstream str(fName);
00452 
00453                 meshTools::writeOBJ
00454                 (
00455                     str,
00456                     mesh.faces(),
00457                     mesh.points(),
00458                     faces.toc()
00459                 );
00460             }
00461             else
00462             {
00463                 // points & edges
00464                 writePoints(mesh, runTime.timeName());
00465 
00466                 // face centres
00467                 writeFaceCentres(mesh, runTime.timeName());
00468 
00469                 // cell centres
00470                 writeCellCentres(mesh, runTime.timeName());
00471 
00472                 // Patch face centres
00473                 writePatchCentres(mesh, runTime.timeName());
00474             }
00475         }
00476         else
00477         {
00478             Info << "No mesh." << endl;
00479         }
00480 
00481         Info << nl << endl;
00482     }
00483 
00484 
00485     Info << "End\n" << endl;
00486 
00487     return 0;
00488 }
00489 
00490 
00491 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd