VTPsurfaceFormat.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) 2017-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "VTPsurfaceFormat.H"
29 #include <fstream>
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Face>
35 (
36  vtk::formatter& format,
37  const UList<Face>& faces
38 )
39 {
40  format.tag(vtk::fileTag::POLYS);
41 
42  //
43  // 'connectivity'
44  //
45  {
46  label nVerts = 0;
47  for (const auto& f : faces)
48  {
49  nVerts += f.size();
50  }
51 
52  const uint64_t payLoad = vtk::sizeofData<label>(nVerts);
53 
54  format.beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
55  format.writeSize(payLoad);
56 
57  for (const auto& f : faces)
58  {
60  }
61 
62  format.flush();
63  format.endDataArray();
64  }
65 
66 
67  //
68  // 'offsets' (connectivity offsets)
69  //
70  {
71  const uint64_t payLoad = vtk::sizeofData<label>(faces.size());
72 
73  format.beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
74  format.writeSize(payLoad);
75 
76  label off = 0;
77  for (const auto& f : faces)
78  {
79  off += f.size();
80 
81  format.write(off);
82  }
83 
84  format.flush();
85  format.endDataArray();
86  }
87 
88  format.endTag(vtk::fileTag::POLYS);
89 }
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
94 template<class Face>
96 (
97  const fileName& filename,
98  const MeshedSurfaceProxy<Face>& surf,
100  const dictionary& options
101 )
102 {
103  const UList<point>& pointLst = surf.points();
104  const UList<Face>& faceLst = surf.surfFaces();
105  const UList<label>& faceMap = surf.faceMap();
106 
107  const surfZoneList zones =
108  (
109  surf.surfZones().empty()
110  ? surfaceFormatsCore::oneZone(faceLst)
111  : surf.surfZones()
112  );
113 
114  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
115 
116  vtk::outputOptions opts = formatOptions(options);
117 
118  std::ofstream os(filename, std::ios::binary);
119 
121 
122  writeHeader(format(), pointLst, faceLst.size());
123 
124  if (useFaceMap)
125  {
126  format().tag(vtk::fileTag::POLYS);
127 
128  //
129  // 'connectivity'
130  //
131  {
132  label nVerts = 0;
133  for (const auto& f : faceLst)
134  {
135  nVerts += f.size();
136  }
137 
138  const uint64_t payLoad = vtk::sizeofData<label>(nVerts);
139 
140  format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
141  format().writeSize(payLoad);
142 
143  label faceIndex = 0;
144  for (const surfZone& zone : zones)
145  {
146  forAll(zone, i)
147  {
148  const Face& f = faceLst[faceMap[faceIndex++]];
149 
150  vtk::writeList(format(), f);
151  }
152  }
153 
154  format().flush();
155  format().endDataArray();
156  }
157 
158 
159  //
160  // 'offsets' (connectivity offsets)
161  //
162  {
163  const uint64_t payLoad = vtk::sizeofData<label>(faceLst.size());
164 
165  format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
166  format().writeSize(payLoad);
167 
168  label off = 0, faceIndex = 0;
169  for (const surfZone& zone : zones)
170  {
171  forAll(zone, i)
172  {
173  const Face& f = faceLst[faceMap[faceIndex++]];
174 
175  off += f.size();
176 
177  format().write(off);
178  }
179  }
180 
181  format().flush();
182  format().endDataArray();
183  }
184 
185  format().endTag(vtk::fileTag::POLYS);
186  }
187  else
188  {
189  // Easy to write polys without a faceMap
190  writePolys(format(), faceLst);
191  }
192 
193  // Write regions (zones) as CellData
194  if (zones.size() > 1)
195  {
196  writeCellData(format(), zones);
197  }
198 
199  writeFooter(format());
200 }
201 
202 
203 template<class Face>
205 (
206  const fileName& filename,
207  const UnsortedMeshedSurface<Face>& surf,
209  const dictionary& options
210 )
211 {
212  vtk::outputOptions opts = formatOptions(options);
213 
214  std::ofstream os(filename, std::ios::binary);
215 
217 
218  const UList<Face>& faceLst = surf.surfFaces();
219 
220  writeHeader(format(), surf.points(), faceLst.size());
221 
222  // Easy to write polys without a faceMap
223  writePolys(format(), faceLst);
224 
225  // Write regions (zones) as CellData
226  writeCellData(format(), surf.zoneIds());
227 
228  writeFooter(format());
229 }
230 
231 
232 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::fileFormats::VTPsurfaceFormat
Write surfaces in VTP (xml) format. The default format is INLINE_BASE64.
Definition: VTPsurfaceFormat.H:83
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeTopological.C:94
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::MeshedSurfaceProxy::useFaceMap
bool useFaceMap() const
Can/should use faceMap?
Definition: MeshedSurfaceProxy.H:203
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::vtk::outputOptions::newFormatter
autoPtr< formatter > newFormatter(std::ostream &os) const
Return new formatter based on the selected output options.
Definition: foamVtkOutputOptionsI.H:63
Foam::MeshedSurfaceProxy::points
const pointField & points() const
Return const access to the points.
Definition: MeshedSurfaceProxy.H:171
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:82
formatOptions
const dictionary formatOptions
Definition: createFields.H:26
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:191
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::UnsortedMeshedSurface::zoneIds
virtual const labelList & zoneIds() const
Return const access to the zone ids.
Definition: UnsortedMeshedSurface.H:336
format
word format(conversionProperties.get< word >("format"))
Foam::UnsortedMeshedSurface
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
Definition: MeshedSurface.H:83
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::vtk::writeList
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:112
Foam::fileFormats::VTPsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, IOstreamOption=IOstreamOption(), const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: VTPsurfaceFormat.C:96
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
f
labelList f(nPoints)
Foam::List< surfZone >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:56
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
VTPsurfaceFormat.H
Foam::MeshedSurfaceProxy::surfFaces
const UList< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurfaceProxy.H:177
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:185