VTKedgeFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "VTKedgeFormat.H"
27 #include "OFstream.H"
28 #include "clock.H"
29 #include "IFstream.H"
30 #include "vtkUnstructuredReader.H"
31 #include "Time.H"
32 
33 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
34 
36 (
37  Ostream& os,
38  const pointField& pointLst
39 )
40 {
41  // Write header
42  os << "# vtk DataFile Version 2.0" << nl
43  << "featureEdgeMesh written " << clock::dateTime().c_str() << nl
44  << "ASCII" << nl
45  << nl
46  << "DATASET POLYDATA" << nl;
47 
48  // Write vertex coords
49  os << "POINTS " << pointLst.size() << " float" << nl;
50  forAll(pointLst, ptI)
51  {
52  const point& pt = pointLst[ptI];
53 
54  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
55  }
56 }
57 
58 
60 (
61  Ostream& os,
62  const UList<edge>& edgeLst
63 )
64 {
65  os << "LINES " << edgeLst.size() << ' ' << 3*edgeLst.size() << nl;
66 
67  forAll(edgeLst, edgeI)
68  {
69  const edge& e = edgeLst[edgeI];
70 
71  os << "2 " << e[0] << ' ' << e[1] << nl;
72  }
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77 
79 (
80  const fileName& filename
81 )
82 {
83  read(filename);
84 }
85 
86 
87 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
88 
90 (
91  const fileName& filename
92 )
93 {
94  IFstream is(filename);
95  if (!is.good())
96  {
98  << "Cannot read file " << filename
99  << exit(FatalError);
100  }
101 
102  // Construct dummy time so we have something to create an objectRegistry
103  // from
104  Time dummyTime
105  (
106  "dummyRoot",
107  "dummyCase",
108  false // enableFunctionObjects
109  );
110 
111  // Make dummy object registry
112  objectRegistry obr
113  (
114  IOobject
115  (
116  "dummy",
117  dummyTime,
120  false
121  )
122  );
123 
124  // Construct reader to read file
125  vtkUnstructuredReader reader(obr, is);
126 
127 
128  // Extract lines
129  storedPoints().transfer(reader.points());
130 
131  label nEdges = 0;
132  forAll(reader.lines(), lineI)
133  {
134  nEdges += reader.lines()[lineI].size()-1;
135  }
136  storedEdges().setSize(nEdges);
137 
138  nEdges = 0;
139  forAll(reader.lines(), lineI)
140  {
141  const labelList& verts = reader.lines()[lineI];
142  for (label i = 1; i < verts.size(); i++)
143  {
144  storedEdges()[nEdges++] = edge(verts[i-1], verts[i]);
145  }
146  }
147 
148  return true;
149 }
150 
151 
153 (
154  const fileName& filename,
155  const edgeMesh& eMesh
156 )
157 {
158  OFstream os(filename);
159  if (!os.good())
160  {
162  << "Cannot open file for writing " << filename
163  << exit(FatalError);
164  }
165 
166  writeHeader(os, eMesh.points());
167  writeEdges(os, eMesh.edges());
168 }
169 
170 
171 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Input from file stream.
Definition: IFstream.H:85
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Output to file stream.
Definition: OFstream.H:86
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
const Cmpt & z() const
Definition: VectorI.H:87
const Cmpt & y() const
Definition: VectorI.H:81
const Cmpt & x() const
Definition: VectorI.H:75
static string dateTime()
Return the current wall-clock date/time as a string.
Definition: clock.C:57
Points connected by edges.
Definition: edgeMesh.H:72
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:68
const pointField & points() const
Return points.
Definition: edgeMeshI.H:62
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:61
static void writeEdges(Ostream &, const UList< edge > &)
Write edges.
Definition: VTKedgeFormat.C:60
virtual bool read(const fileName &)
Read from file.
Definition: VTKedgeFormat.C:90
static void writeHeader(Ostream &, const pointField &)
Write header information with points.
Definition: VTKedgeFormat.C:36
VTKedgeFormat(const fileName &)
Construct from file name.
Definition: VTKedgeFormat.C:79
static void write(const fileName &, const edgeMesh &)
Write surface mesh components by proxy.
A class for handling file names.
Definition: fileName.H:82
Registry of regIOobjects.
Reader for vtk unstructured_grid legacy files. Supports single CELLS, POINTS etc. entry only.
const labelListList & lines() const
1D cells (=open lines)
const pointField & points() const
Points.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const doubleScalar e
Definition: doubleScalar.H:105
bool read(const char *, int32_t &)
Definition: int32IO.C:85
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
error FatalError
static const char nl
Definition: Ostream.H:260