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-------------------------------------------------------------------------------
10License
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
33template<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 {
59 vtk::writeList(format, f);
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
94template<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
203template<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// ************************************************************************* //
The IOstreamOption is a simple container for options an IOstream can normally have.
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
const UList< surfZone > & surfZones() const
Const access to the surface zones.
const UList< Face > & surfFaces() const
Return const access to the faces.
bool useFaceMap() const
Can/should use faceMap?
const pointField & points() const
Return const access to the points.
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
const List< Face > & surfFaces() const
Return const access to the faces.
const Field< point_type > & points() const noexcept
Return reference to global points.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
virtual const labelList & zoneIds() const
Return const access to the zone ids.
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Write surfaces in VTP (xml) format. The default format is INLINE_BASE64.
A class for handling file names.
Definition: fileName.H:76
virtual bool write()
Write the output fields.
A surface zone on a MeshedSurface.
Definition: surfZone.H:59
Encapsulated combinations of output format options. This is primarily useful when defining the output...
autoPtr< formatter > newFormatter(std::ostream &os) const
Return new formatter based on the selected output options.
Base class for mesh zones.
Definition: zone.H:67
OBJstream os(runTime.globalPath()/outputName)
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
word format(conversionProperties.get< word >("format"))
labelList f(nPoints)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
const dictionary formatOptions(propsDict.subOrEmptyDict("formatOptions", keyType::LITERAL))