VTKedgeFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2012 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 // * * * * * * * * * * * * * Private 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 
78 Foam::fileFormats::VTKedgeFormat::VTKedgeFormat
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  (
99  "fileFormats::VTKedgeFormat::read(const fileName&)"
100  ) << "Cannot read file " << filename
101  << exit(FatalError);
102  }
103 
104  // Construct dummy time so we have something to create an objectRegistry
105  // from
106  Time dummyTime
107  (
108  "dummyRoot",
109  "dummyCase",
110  "system",
111  "constant",
112  false // enableFunctionObjects
113  );
114 
115  // Make dummy object registry
116  objectRegistry obr
117  (
118  IOobject
119  (
120  "dummy",
121  dummyTime,
122  IOobject::NO_READ,
123  IOobject::NO_WRITE,
124  false
125  )
126  );
127 
128  // Construct reader to read file
129  vtkUnstructuredReader reader(obr, is);
130 
131 
132  // Extract lines
133  storedPoints().transfer(reader.points());
134 
135  label nEdges = 0;
136  forAll(reader.lines(), lineI)
137  {
138  nEdges += reader.lines()[lineI].size()-1;
139  }
140  storedEdges().setSize(nEdges);
141 
142  nEdges = 0;
143  forAll(reader.lines(), lineI)
144  {
145  const labelList& verts = reader.lines()[lineI];
146  for (label i = 1; i < verts.size(); i++)
147  {
148  storedEdges()[nEdges++] = edge(verts[i-1], verts[i]);
149  }
150  }
151 
152  return true;
153 }
154 
155 
157 (
158  const fileName& filename,
159  const edgeMesh& eMesh
160 )
161 {
162  OFstream os(filename);
163  if (!os.good())
164  {
166  (
167  "fileFormats::VTKedgeFormat::write"
168  "(const fileName&, const edgeMesh&)"
169  ) << "Cannot open file for writing " << filename
170  << exit(FatalError);
171  }
172 
173  writeHeader(os, eMesh.points());
174  writeEdges(os, eMesh.edges());
175 }
176 
177 
178 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
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
const pointField & points() const
Return points.
Definition: edgeMeshI.H:39
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static void writeEdges(Ostream &, const UList< edge > &)
Write edges.
Definition: VTKedgeFormat.C:60
Input from file stream.
Definition: IFstream.H:81
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
static void write(const fileName &, const edgeMesh &)
Write surface mesh components by proxy.
static const char nl
Definition: Ostream.H:260
const Cmpt & y() const
Definition: VectorI.H:71
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
const labelListList & lines() const
1D cells (=open lines)
const dimensionedScalar e
Elementary charge.
Definition: doubleFloat.H:78
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
const Cmpt & x() const
Definition: VectorI.H:65
Reader for vtk unstructured_grid legacy files. Supports single CELLS, POINTS etc. entry only...
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
const Cmpt & z() const
Definition: VectorI.H:77
virtual bool read(const fileName &)
Read from file.
Definition: VTKedgeFormat.C:90
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
static void writeHeader(Ostream &, const pointField &)
Write header information with points.
Definition: VTKedgeFormat.C:36
Points connected by edges.
Definition: edgeMesh.H:69
error FatalError
Registry of regIOobjects.
A class for handling file names.
Definition: fileName.H:69
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool read(const char *, int32_t &)
Definition: int32IO.C:87
const pointField & points() const
Points.
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:45