PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/applications/utilities/mesh/conversion/writeMeshObj/writeMeshObj.C

https://github.com/xyuan/OpenFOAM-1.7.x
C | 545 lines | 373 code | 122 blank | 50 comment | 25 complexity | 3b89c9c2a0be04bb3c16355ab3cc006d MD5 | raw file
  1. /*---------------------------------------------------------------------------*\
  2. ========= |
  3. \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
  4. \\ / O peration |
  5. \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
  6. \\/ M anipulation |
  7. -------------------------------------------------------------------------------
  8. License
  9. This file is part of OpenFOAM.
  10. OpenFOAM is free software: you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License as published by
  12. the Free Software Foundation, either version 3 of the License, or
  13. (at your option) any later version.
  14. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
  15. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
  20. Description
  21. For mesh debugging: writes mesh as three separate OBJ files which can
  22. be viewed with e.g. javaview.
  23. meshPoints_XXX.obj : all points and edges as lines.
  24. meshFaceCentres_XXX.obj : all face centres.
  25. meshCellCentres_XXX.obj : all cell centres.
  26. patch_YYY_XXX.obj : all face centres of patch YYY
  27. Optional: - patch faces (as polygons) : patchFaces_YYY_XXX.obj
  28. - non-manifold edges : patchEdges_YYY_XXX.obj
  29. \*---------------------------------------------------------------------------*/
  30. #include "argList.H"
  31. #include "timeSelector.H"
  32. #include "Time.H"
  33. #include "polyMesh.H"
  34. #include "OFstream.H"
  35. #include "meshTools.H"
  36. #include "cellSet.H"
  37. #include "faceSet.H"
  38. using namespace Foam;
  39. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  40. void writeOBJ(const point& pt, Ostream& os)
  41. {
  42. os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
  43. }
  44. // All edges of mesh
  45. void writePoints(const polyMesh& mesh, const fileName& timeName)
  46. {
  47. label vertI = 0;
  48. fileName pointFile(mesh.time().path()/"meshPoints_" + timeName + ".obj");
  49. Info << "Writing mesh points and edges to " << pointFile << endl;
  50. OFstream pointStream(pointFile);
  51. forAll(mesh.points(), pointI)
  52. {
  53. writeOBJ(mesh.points()[pointI], pointStream);
  54. vertI++;
  55. }
  56. forAll(mesh.edges(), edgeI)
  57. {
  58. const edge& e = mesh.edges()[edgeI];
  59. pointStream << "l " << e.start() + 1 << ' ' << e.end() + 1 << nl;
  60. }
  61. }
  62. // Edges for subset of cells
  63. void writePoints
  64. (
  65. const polyMesh& mesh,
  66. const labelList& cellLabels,
  67. const fileName& timeName
  68. )
  69. {
  70. fileName fName(mesh.time().path()/"meshPoints_" + timeName + ".obj");
  71. Info << "Writing mesh points and edges to " << fName << endl;
  72. OFstream str(fName);
  73. // OBJ file vertex
  74. label vertI = 0;
  75. // From point to OBJ file vertex
  76. Map<label> pointToObj(6*cellLabels.size());
  77. forAll(cellLabels, i)
  78. {
  79. label cellI = cellLabels[i];
  80. const labelList& cEdges = mesh.cellEdges()[cellI];
  81. forAll(cEdges, cEdgeI)
  82. {
  83. const edge& e = mesh.edges()[cEdges[cEdgeI]];
  84. label v0;
  85. Map<label>::iterator e0Fnd = pointToObj.find(e[0]);
  86. if (e0Fnd == pointToObj.end())
  87. {
  88. meshTools::writeOBJ(str, mesh.points()[e[0]]);
  89. v0 = vertI++;
  90. pointToObj.insert(e[0], v0);
  91. }
  92. else
  93. {
  94. v0 = e0Fnd();
  95. }
  96. label v1;
  97. Map<label>::iterator e1Fnd = pointToObj.find(e[1]);
  98. if (e1Fnd == pointToObj.end())
  99. {
  100. meshTools::writeOBJ(str, mesh.points()[e[1]]);
  101. v1 = vertI++;
  102. pointToObj.insert(e[1], v1);
  103. }
  104. else
  105. {
  106. v1 = e1Fnd();
  107. }
  108. str << "l " << v0+1 << ' ' << v1+1 << nl;
  109. }
  110. }
  111. }
  112. // Edges of single cell
  113. void writePoints
  114. (
  115. const polyMesh& mesh,
  116. const label cellI,
  117. const fileName& timeName
  118. )
  119. {
  120. fileName fName
  121. (
  122. mesh.time().path()
  123. / "meshPoints_" + timeName + '_' + name(cellI) + ".obj"
  124. );
  125. Info << "Writing mesh points and edges to " << fName << endl;
  126. OFstream pointStream(fName);
  127. const cell& cFaces = mesh.cells()[cellI];
  128. meshTools::writeOBJ(pointStream, mesh.faces(), mesh.points(), cFaces);
  129. }
  130. // All face centres
  131. void writeFaceCentres(const polyMesh& mesh,const fileName& timeName)
  132. {
  133. fileName faceFile
  134. (
  135. mesh.time().path()
  136. / "meshFaceCentres_" + timeName + ".obj"
  137. );
  138. Info << "Writing mesh face centres to " << faceFile << endl;
  139. OFstream faceStream(faceFile);
  140. forAll(mesh.faceCentres(), faceI)
  141. {
  142. writeOBJ(mesh.faceCentres()[faceI], faceStream);
  143. }
  144. }
  145. void writeCellCentres(const polyMesh& mesh, const fileName& timeName)
  146. {
  147. fileName cellFile
  148. (
  149. mesh.time().path()/"meshCellCentres_" + timeName + ".obj"
  150. );
  151. Info << "Writing mesh cell centres to " << cellFile << endl;
  152. OFstream cellStream(cellFile);
  153. forAll(mesh.cellCentres(), cellI)
  154. {
  155. writeOBJ(mesh.cellCentres()[cellI], cellStream);
  156. }
  157. }
  158. void writePatchCentres
  159. (
  160. const polyMesh& mesh,
  161. const fileName& timeName
  162. )
  163. {
  164. const polyBoundaryMesh& patches = mesh.boundaryMesh();
  165. forAll(patches, patchI)
  166. {
  167. const polyPatch& pp = patches[patchI];
  168. fileName faceFile
  169. (
  170. mesh.time().path()/"patch_" + pp.name() + '_' + timeName + ".obj"
  171. );
  172. Info << "Writing patch face centres to " << faceFile << endl;
  173. OFstream patchFaceStream(faceFile);
  174. forAll(pp.faceCentres(), faceI)
  175. {
  176. writeOBJ(pp.faceCentres()[faceI], patchFaceStream);
  177. }
  178. }
  179. }
  180. void writePatchFaces
  181. (
  182. const polyMesh& mesh,
  183. const fileName& timeName
  184. )
  185. {
  186. const polyBoundaryMesh& patches = mesh.boundaryMesh();
  187. forAll(patches, patchI)
  188. {
  189. const polyPatch& pp = patches[patchI];
  190. fileName faceFile
  191. (
  192. mesh.time().path()
  193. / "patchFaces_" + pp.name() + '_' + timeName + ".obj"
  194. );
  195. Info << "Writing patch faces to " << faceFile << endl;
  196. OFstream patchFaceStream(faceFile);
  197. forAll(pp.localPoints(), pointI)
  198. {
  199. writeOBJ(pp.localPoints()[pointI], patchFaceStream);
  200. }
  201. forAll(pp.localFaces(), faceI)
  202. {
  203. const face& f = pp.localFaces()[faceI];
  204. patchFaceStream<< 'f';
  205. forAll(f, fp)
  206. {
  207. patchFaceStream << ' ' << f[fp]+1;
  208. }
  209. patchFaceStream << nl;
  210. }
  211. }
  212. }
  213. void writePatchBoundaryEdges
  214. (
  215. const polyMesh& mesh,
  216. const fileName& timeName
  217. )
  218. {
  219. const polyBoundaryMesh& patches = mesh.boundaryMesh();
  220. forAll(patches, patchI)
  221. {
  222. const polyPatch& pp = patches[patchI];
  223. fileName edgeFile
  224. (
  225. mesh.time().path()
  226. / "patchEdges_" + pp.name() + '_' + timeName + ".obj"
  227. );
  228. Info << "Writing patch edges to " << edgeFile << endl;
  229. OFstream patchEdgeStream(edgeFile);
  230. forAll(pp.localPoints(), pointI)
  231. {
  232. writeOBJ(pp.localPoints()[pointI], patchEdgeStream);
  233. }
  234. for (label edgeI = pp.nInternalEdges(); edgeI < pp.nEdges(); edgeI++)
  235. {
  236. if (pp.edgeFaces()[edgeI].size() == 1)
  237. {
  238. const edge& e = pp.edges()[edgeI];
  239. patchEdgeStream<< "l " << e[0]+1 << ' ' << e[1]+1 << nl;
  240. }
  241. }
  242. }
  243. }
  244. void writePointCells
  245. (
  246. const polyMesh& mesh,
  247. const label pointI,
  248. const fileName& timeName
  249. )
  250. {
  251. const labelList& pCells = mesh.pointCells()[pointI];
  252. labelHashSet allEdges(6*pCells.size());
  253. forAll(pCells, i)
  254. {
  255. const labelList& cEdges = mesh.cellEdges()[pCells[i]];
  256. forAll(cEdges, i)
  257. {
  258. allEdges.insert(cEdges[i]);
  259. }
  260. }
  261. fileName pFile
  262. (
  263. mesh.time().path()
  264. / "pointEdges_" + timeName + '_' + name(pointI) + ".obj"
  265. );
  266. Info << "Writing pointEdges to " << pFile << endl;
  267. OFstream pointStream(pFile);
  268. label vertI = 0;
  269. for
  270. (
  271. labelHashSet::const_iterator iter = allEdges.begin();
  272. iter != allEdges.end();
  273. ++iter
  274. )
  275. {
  276. const edge& e = mesh.edges()[iter.key()];
  277. meshTools::writeOBJ(pointStream, mesh.points()[e[0]]); vertI++;
  278. meshTools::writeOBJ(pointStream, mesh.points()[e[1]]); vertI++;
  279. pointStream<< "l " << vertI-1 << ' ' << vertI << nl;
  280. }
  281. }
  282. // Main program:
  283. int main(int argc, char *argv[])
  284. {
  285. timeSelector::addOptions();
  286. argList::validOptions.insert("patchFaces", "");
  287. argList::validOptions.insert("patchEdges", "");
  288. argList::validOptions.insert("cell", "cellI");
  289. argList::validOptions.insert("face", "faceI");
  290. argList::validOptions.insert("point", "pointI");
  291. argList::validOptions.insert("cellSet", "setName");
  292. argList::validOptions.insert("faceSet", "setName");
  293. # include "addRegionOption.H"
  294. # include "setRootCase.H"
  295. # include "createTime.H"
  296. runTime.functionObjects().off();
  297. bool patchFaces = args.optionFound("patchFaces");
  298. bool patchEdges = args.optionFound("patchEdges");
  299. bool doCell = args.optionFound("cell");
  300. bool doPoint = args.optionFound("point");
  301. bool doFace = args.optionFound("face");
  302. bool doCellSet = args.optionFound("cellSet");
  303. bool doFaceSet = args.optionFound("faceSet");
  304. Info<< "Writing mesh objects as .obj files such that the object"
  305. << " numbering" << endl
  306. << "(for points, faces, cells) is consistent with"
  307. << " Foam numbering (starting from 0)." << endl << endl;
  308. instantList timeDirs = timeSelector::select0(runTime, args);
  309. # include "createNamedPolyMesh.H"
  310. forAll(timeDirs, timeI)
  311. {
  312. runTime.setTime(timeDirs[timeI], timeI);
  313. Info<< "Time = " << runTime.timeName() << endl;
  314. polyMesh::readUpdateState state = mesh.readUpdate();
  315. if (!timeI || state != polyMesh::UNCHANGED)
  316. {
  317. if (patchFaces)
  318. {
  319. writePatchFaces(mesh, runTime.timeName());
  320. }
  321. if (patchEdges)
  322. {
  323. writePatchBoundaryEdges(mesh, runTime.timeName());
  324. }
  325. if (doCell)
  326. {
  327. label cellI = args.optionRead<label>("cell");
  328. writePoints(mesh, cellI, runTime.timeName());
  329. }
  330. if (doPoint)
  331. {
  332. label pointI = args.optionRead<label>("point");
  333. writePointCells(mesh, pointI, runTime.timeName());
  334. }
  335. if (doFace)
  336. {
  337. label faceI = args.optionRead<label>("face");
  338. fileName fName
  339. (
  340. mesh.time().path()
  341. / "meshPoints_"
  342. + runTime.timeName()
  343. + '_'
  344. + name(faceI)
  345. + ".obj"
  346. );
  347. Info<< "Writing mesh points and edges to " << fName << endl;
  348. OFstream str(fName);
  349. const face& f = mesh.faces()[faceI];
  350. meshTools::writeOBJ(str, faceList(1, f), mesh.points());
  351. }
  352. if (doCellSet)
  353. {
  354. word setName(args.option("cellSet"));
  355. cellSet cells(mesh, setName);
  356. Info<< "Read " << cells.size() << " cells from set " << setName
  357. << endl;
  358. writePoints(mesh, cells.toc(), runTime.timeName());
  359. }
  360. if (doFaceSet)
  361. {
  362. word setName(args.option("faceSet"));
  363. faceSet faces(mesh, setName);
  364. Info<< "Read " << faces.size() << " faces from set " << setName
  365. << endl;
  366. fileName fName
  367. (
  368. mesh.time().path()
  369. / "meshPoints_"
  370. + runTime.timeName()
  371. + '_'
  372. + setName
  373. + ".obj"
  374. );
  375. Info << "Writing mesh points and edges to " << fName << endl;
  376. OFstream str(fName);
  377. meshTools::writeOBJ
  378. (
  379. str,
  380. mesh.faces(),
  381. mesh.points(),
  382. faces.toc()
  383. );
  384. }
  385. else if
  386. (
  387. !patchFaces
  388. && !patchEdges
  389. && !doCell
  390. && !doPoint
  391. && !doFace
  392. && !doCellSet
  393. && !doFaceSet
  394. )
  395. {
  396. // points & edges
  397. writePoints(mesh, runTime.timeName());
  398. // face centres
  399. writeFaceCentres(mesh, runTime.timeName());
  400. // cell centres
  401. writeCellCentres(mesh, runTime.timeName());
  402. // Patch face centres
  403. writePatchCentres(mesh, runTime.timeName());
  404. }
  405. }
  406. else
  407. {
  408. Info << "No mesh." << endl;
  409. }
  410. Info << nl << endl;
  411. }
  412. Info << "End\n" << endl;
  413. return 0;
  414. }
  415. // ************************************************************************* //