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-2018 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,
99  const dictionary& options
100 )
101 {
102  const UList<point>& pointLst = surf.points();
103  const UList<Face>& faceLst = surf.surfFaces();
104  const UList<label>& faceMap = surf.faceMap();
105 
106  const surfZoneList zones =
107  (
108  surf.surfZones().empty()
109  ? surfaceFormatsCore::oneZone(faceLst)
110  : surf.surfZones()
111  );
112 
113  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
114 
115  vtk::outputOptions opts = formatOptions(options);
116 
117  std::ofstream os(filename, std::ios::binary);
118 
120 
121  writeHeader(format(), pointLst, faceLst.size());
122 
123  if (useFaceMap)
124  {
125  format().tag(vtk::fileTag::POLYS);
126 
127  //
128  // 'connectivity'
129  //
130  {
131  label nVerts = 0;
132  for (const auto& f : faceLst)
133  {
134  nVerts += f.size();
135  }
136 
137  const uint64_t payLoad = vtk::sizeofData<label>(nVerts);
138 
139  format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
140  format().writeSize(payLoad);
141 
142  label faceIndex = 0;
143  for (const surfZone& zone : zones)
144  {
145  forAll(zone, i)
146  {
147  const Face& f = faceLst[faceMap[faceIndex++]];
148 
149  vtk::writeList(format(), f);
150  }
151  }
152 
153  format().flush();
154  format().endDataArray();
155  }
156 
157 
158  //
159  // 'offsets' (connectivity offsets)
160  //
161  {
162  const uint64_t payLoad = vtk::sizeofData<label>(faceLst.size());
163 
164  format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
165  format().writeSize(payLoad);
166 
167  label off = 0, faceIndex = 0;
168  for (const surfZone& zone : zones)
169  {
170  forAll(zone, i)
171  {
172  const Face& f = faceLst[faceMap[faceIndex++]];
173 
174  off += f.size();
175 
176  format().write(off);
177  }
178  }
179 
180  format().flush();
181  format().endDataArray();
182  }
183 
184  format().endTag(vtk::fileTag::POLYS);
185  }
186  else
187  {
188  // Easy to write polys without a faceMap
189  writePolys(format(), faceLst);
190  }
191 
192  // Write regions (zones) as CellData
193  if (zones.size() > 1)
194  {
195  writeCellData(format(), zones);
196  }
197 
198  writeFooter(format());
199 }
200 
201 
202 template<class Face>
204 (
205  const fileName& filename,
206  const UnsortedMeshedSurface<Face>& surf,
207  const dictionary& options
208 )
209 {
210  vtk::outputOptions opts = formatOptions(options);
211 
212  std::ofstream os(filename, std::ios::binary);
213 
215 
216  const UList<Face>& faceLst = surf.surfFaces();
217 
218  writeHeader(format(), surf.points(), faceLst.size());
219 
220  // Easy to write polys without a faceMap
221  writePolys(format(), faceLst);
222 
223  // Write regions (zones) as CellData
224  writeCellData(format(), surf.zoneIds());
225 
226  writeFooter(format());
227 }
228 
229 
230 // ************************************************************************* //
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:81
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:94
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::MeshedSurfaceProxy::useFaceMap
bool useFaceMap() const
Use faceMap?
Definition: MeshedSurfaceProxy.H:186
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:160
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:78
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:180
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::UnsortedMeshedSurface::zoneIds
virtual const labelList & zoneIds() const
Return const access to the zone ids.
Definition: UnsortedMeshedSurface.H:300
format
word format(conversionProperties.get< word >("format"))
Foam::fileFormats::VTPsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: VTPsurfaceFormat.C:96
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::UnsortedMeshedSurface
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
Definition: MeshedSurface.H:79
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::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
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:65
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::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
VTPsurfaceFormat.H
Foam::MeshedSurfaceProxy::surfFaces
const UList< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurfaceProxy.H:166
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:174