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,
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,
110  IOstreamOption,
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 
124  vtk::lineWriter writer
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 // ************************************************************************* //
vtk::lineWriter writer(edgeCentres, edgeList::null(), fileName(aMesh.time().globalPath()/"finiteArea-edgesCentres"))
Input from file stream as an ISstream, normally using std::ifstream for the actual input.
Definition: IFstream.H:55
@ NO_REGISTER
Do not request registration (bool: false)
@ NO_READ
Nothing to be read.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject()
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:183
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:418
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: TimeNew.C:26
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:65
VTKedgeFormat(const fileName &filename)
Read construct from file name.
Definition: VTKedgeFormat.C:31
virtual bool read(const fileName &filename) override
Read from file.
Definition: VTKedgeFormat.C:42
static void write(const fileName &filename, const edgeMesh &mesh, IOstreamOption=IOstreamOption(), const dictionary &options=dictionary::null)
Write edge mesh to file in legacy VTK format.
A class for handling file names.
Definition: fileName.H:75
Registry of regIOobjects.
Reader for vtk UNSTRUCTURED_GRID legacy files. Supports single CELLS, POINTS etc. entry only.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...