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-2021 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 "Time.H"
32 #include "foamVtkLineWriter.H"
33 #include "vtkUnstructuredReader.H"
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
38 (
39  const fileName& filename
40 )
41 {
42  read(filename);
43 }
44 
45 
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47 
49 (
50  const fileName& filename
51 )
52 {
53  IFstream is(filename);
54  if (!is.good())
55  {
57  << "Cannot read file " << filename
58  << exit(FatalError);
59  }
60 
61  // Use dummy Time for objectRegistry
62  autoPtr<Time> dummyTimePtr(Time::New());
63 
64  objectRegistry obr
65  (
66  IOobject
67  (
68  "vtk::edgeFormat",
69  *dummyTimePtr,
70  IOobject::NO_READ,
71  IOobject::NO_WRITE,
72  false
73  )
74  );
75 
76  // Construct reader to read file
77  vtkUnstructuredReader reader(obr, is);
78 
79 
80  // Extract lines
81  storedPoints().transfer(reader.points());
82 
83  label nEdges = 0;
84  for (const auto& lineVerts : reader.lines())
85  {
86  if (lineVerts.size() > 1)
87  {
88  nEdges += (lineVerts.size()-1);
89  }
90  }
91  storedEdges().resize(nEdges);
92 
93  nEdges = 0;
94  for (const auto& lineVerts : reader.lines())
95  {
96  for (label i = 1; i < lineVerts.size(); ++i)
97  {
98  storedEdges()[nEdges++] = edge(lineVerts[i-1], lineVerts[i]);
99  }
100  }
101 
102  return true;
103 }
104 
105 
107 (
108  const fileName& filename,
109  const edgeMesh& eMesh,
111  const dictionary& options
112 )
113 {
114  // NB: restrict output to legacy ascii so that we are still able
115  // to read it with vtkUnstructuredReader
116 
117  vtk::outputOptions opts(vtk::formatType::LEGACY_ASCII);
118 
119  opts.precision
120  (
121  options.getOrDefault("precision", IOstream::defaultPrecision())
122  );
123 
125  (
126  eMesh.points(),
127  eMesh.edges(),
128  opts,
129  filename,
130  false // non-parallel write (edgeMesh already serialized)
131  );
132 
133  writer.beginFile("OpenFOAM edgeMesh");
134  writer.writeGeometry();
135 }
136 
137 
138 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::edgeMesh::points
const pointField & points() const noexcept
Return points.
Definition: edgeMeshI.H:99
Foam::vtkUnstructuredReader::points
const pointField & points() const noexcept
Points.
Definition: vtkUnstructuredReader.H:255
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
vtkUnstructuredReader.H
Foam::vtk::outputOptions::precision
unsigned precision() const
Return the ASCII write precision.
Definition: foamVtkOutputOptionsI.H:126
Foam::vtkUnstructuredReader
Reader for vtk UNSTRUCTURED_GRID legacy files. Supports single CELLS, POINTS etc. entry only.
Definition: vtkUnstructuredReader.H:67
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::vtkUnstructuredReader::lines
const labelListList & lines() const noexcept
1D cells (=open lines)
Definition: vtkUnstructuredReader.H:298
Foam::fileFormats::VTKedgeFormat::read
virtual bool read(const fileName &name)
Read from file.
Definition: VTKedgeFormat.C:49
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::fileFormats::VTKedgeFormat::write
static void write(const fileName &filename, const edgeMesh &mesh, IOstreamOption=IOstreamOption(), const dictionary &options=dictionary::null)
Write edge mesh to file.
Definition: VTKedgeFormat.C:107
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::fileFormats::VTKedgeFormat::VTKedgeFormat
VTKedgeFormat(const fileName &filename)
Read construct from file name.
Definition: VTKedgeFormat.C:38
VTKedgeFormat.H
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::writer
Base class for graphics format writing. Entry points are.
Definition: writer.H:81
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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::vtk::lineWriter
Write edge/points (optionally with fields) as a vtp file or a legacy vtk file.
Definition: foamVtkLineWriter.H:65
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::edgeMesh::edges
const edgeList & edges() const noexcept
Return edges.
Definition: edgeMeshI.H:105
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Fstream.H
foamVtkLineWriter.H
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
writer
vtk::internalMeshWriter writer(topoMesh, topoCells, vtk::formatType::INLINE_ASCII, runTime.path()/"blockTopology")
Foam::edgeMesh
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:53