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-2018 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  "system",
109  "constant",
110  false // enableFunctionObjects
111  );
112 
113  // Make dummy object registry
114  objectRegistry obr
115  (
116  IOobject
117  (
118  "dummy",
119  dummyTime,
120  IOobject::NO_READ,
121  IOobject::NO_WRITE,
122  false
123  )
124  );
125 
126  // Construct reader to read file
127  vtkUnstructuredReader reader(obr, is);
128 
129 
130  // Extract lines
131  storedPoints().transfer(reader.points());
132 
133  label nEdges = 0;
134  forAll(reader.lines(), lineI)
135  {
136  nEdges += reader.lines()[lineI].size()-1;
137  }
138  storedEdges().setSize(nEdges);
139 
140  nEdges = 0;
141  forAll(reader.lines(), lineI)
142  {
143  const labelList& verts = reader.lines()[lineI];
144  for (label i = 1; i < verts.size(); i++)
145  {
146  storedEdges()[nEdges++] = edge(verts[i-1], verts[i]);
147  }
148  }
149 
150  return true;
151 }
152 
153 
155 (
156  const fileName& filename,
157  const edgeMesh& eMesh
158 )
159 {
160  OFstream os(filename);
161  if (!os.good())
162  {
164  << "Cannot open file for writing " << filename
165  << exit(FatalError);
166  }
167 
168  writeHeader(os, eMesh.points());
169  writeEdges(os, eMesh.edges());
170 }
171 
172 
173 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
static void writeEdges(Ostream &, const UList< edge > &)
Write edges.
Definition: VTKedgeFormat.C:60
Reader for vtk unstructured_grid legacy files. Supports single CELLS, POINTS etc. entry only...
const Cmpt & z() const
Definition: VectorI.H:87
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
const Cmpt & y() const
Definition: VectorI.H:81
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
static void writeHeader(Ostream &, const pointField &)
Write header information with points.
Definition: VTKedgeFormat.C:36
virtual bool read(const fileName &)
Read from file.
Definition: VTKedgeFormat.C:90
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:68
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
const Cmpt & x() const
Definition: VectorI.H:75
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
const pointField & points() const
Return points.
Definition: edgeMeshI.H:62
Points connected by edges.
Definition: edgeMesh.H:69
Input from file stream.
Definition: IFstream.H:81
VTKedgeFormat(const fileName &)
Construct from file name.
Definition: VTKedgeFormat.C:79
static void write(const fileName &, const edgeMesh &)
Write surface mesh components by proxy.
const labelListList & lines() const
1D cells (=open lines)
Registry of regIOobjects.
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
const pointField & points() const
Points.
const dimensionedScalar e
Elementary charge.
Definition: doubleScalar.H:105