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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "VTKedgeFormat.H"
30 #include "Fstream.H"
31 #include "clock.H"
32 #include "vtkUnstructuredReader.H"
33 #include "Time.H"
34 
35 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
36 
38 (
39  Ostream& os,
40  const pointField& pointLst
41 )
42 {
43  // Write header
44  os << "# vtk DataFile Version 2.0" << nl
45  << "featureEdgeMesh written " << clock::dateTime().c_str() << nl
46  << "ASCII" << nl
47  << nl
48  << "DATASET POLYDATA" << nl;
49 
50  // Write vertex coords
51  os << "POINTS " << pointLst.size() << " double" << nl;
52  for (const point& pt : pointLst)
53  {
54  os << float(pt.x()) << ' '
55  << float(pt.y()) << ' '
56  << float(pt.z()) << nl;
57  }
58 }
59 
60 
62 (
63  Ostream& os,
64  const UList<edge>& edgeLst
65 )
66 {
67  os << "LINES " << edgeLst.size() << ' ' << 3*edgeLst.size() << nl;
68 
69  for (const edge& e : edgeLst)
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  << "Cannot read file " << filename
99  << exit(FatalError);
100  }
101 
102  // Use dummy Time for objectRegistry
103  autoPtr<Time> dummyTimePtr(Time::New());
104 
105  objectRegistry obr
106  (
107  IOobject
108  (
109  "vtk::edgeFormat",
110  *dummyTimePtr,
111  IOobject::NO_READ,
112  IOobject::NO_WRITE,
113  false
114  )
115  );
116 
117  // Construct reader to read file
118  vtkUnstructuredReader reader(obr, is);
119 
120 
121  // Extract lines
122  storedPoints().transfer(reader.points());
123 
124  label nEdges = 0;
125  forAll(reader.lines(), lineI)
126  {
127  nEdges += reader.lines()[lineI].size()-1;
128  }
129  storedEdges().setSize(nEdges);
130 
131  nEdges = 0;
132  forAll(reader.lines(), lineI)
133  {
134  const labelList& verts = reader.lines()[lineI];
135  for (label i = 1; i < verts.size(); i++)
136  {
137  storedEdges()[nEdges++] = edge(verts[i-1], verts[i]);
138  }
139  }
140 
141  return true;
142 }
143 
144 
146 (
147  const fileName& filename,
148  const edgeMesh& eMesh
149 )
150 {
151  OFstream os(filename);
152  if (!os.good())
153  {
155  << "Cannot open file for writing " << filename
156  << exit(FatalError);
157  }
158 
159  writeHeader(os, eMesh.points());
160  writeEdges(os, eMesh.edges());
161 }
162 
163 
164 // ************************************************************************* //
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
vtkUnstructuredReader.H
Foam::vtkUnstructuredReader
Reader for vtk UNSTRUCTURED_GRID legacy files. Supports single CELLS, POINTS etc. entry only.
Definition: vtkUnstructuredReader.H:66
Foam::vtkUnstructuredReader::points
const pointField & points() const
Points.
Definition: vtkUnstructuredReader.H:245
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:63
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
Foam::fileFormats::VTKedgeFormat::writeEdges
static void writeEdges(Ostream &, const UList< edge > &)
Write edges.
Definition: VTKedgeFormat.C:62
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::Field< vector >
VTKedgeFormat.H
clock.H
Foam::edgeMesh::points
const pointField & points() const
Return points.
Definition: edgeMeshI.H:99
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
Foam::edgeMesh::edges
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:105
Foam::FatalError
error FatalError
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Fstream.H
Foam::Vector< scalar >
Foam::fileFormats::VTKedgeFormat::write
static void write(const fileName &, const edgeMesh &)
Write surface mesh components by proxy.
Definition: VTKedgeFormat.C:146
Foam::List< label >
Foam::UList
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:103
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::vtkUnstructuredReader::lines
const labelListList & lines() const
1D cells (=open lines)
Definition: vtkUnstructuredReader.H:288
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::fileFormats::VTKedgeFormat::writeHeader
static void writeHeader(Ostream &, const pointField &)
Write header information with points.
Definition: VTKedgeFormat.C:38
Foam::edgeMesh
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:52
Foam::fileFormats::VTKedgeFormat::read
virtual bool read(const fileName &)
Read from file.
Definition: VTKedgeFormat.C:90