vtkWritePolyDataTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "vtkWritePolyData.H"
27 #include "OFstream.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 template<class Type, class DataType>
33 (
34  std::ostream& os,
35  const bool binary,
36  const wordList& fieldNames,
37  const boolList& fieldIsPointValues,
38  const UPtrList<const Field<Type>>& fieldTypeValues,
39  const bool writePointValues
40 )
41 {
42  forAll(fieldNames, fieldi)
43  {
44  if
45  (
46  fieldIsPointValues[fieldi] == writePointValues
47  && fieldTypeValues.set(fieldi)
48  )
49  {
50  const label nCmpt = pTraits<Type>::nComponents;
51 
52  os << fieldNames[fieldi] << ' ' << pTraits<Type>::nComponents
53  << ' ' << fieldTypeValues[fieldi].size() << ' '
54  << (std::is_integral<DataType>::value ? "int" : "float") << nl;
55 
56  List<DataType> data(nCmpt*fieldTypeValues[fieldi].size());
57  label i = 0;
58  forAll(fieldTypeValues[fieldi], fieldValuei)
59  {
60  for (direction cmpt = 0; cmpt < nCmpt; ++ cmpt)
61  {
62  data[i ++] =
63  component
64  (
65  fieldTypeValues[fieldi][fieldValuei],
66  cmpt
67  );
68  }
69  }
70 
71  vtkWriteOps::write(os, binary, data);
72  }
73  }
74 }
75 
76 
77 template<class PointField, class VertexList, class LineList, class FaceList>
79 (
80  const fileName& file,
81  const word& title,
82  const bool binary,
83  const PointField& points,
84  const VertexList& vertices,
85  const LineList& lines,
86  const FaceList& faces,
87  const wordList& fieldNames,
88  const boolList& fieldIsPointValues,
89  const UPtrList<const Field<label>>& fieldLabelValues
90  #define FieldTypeValuesConstArg(Type, nullArg) \
91  , const UPtrList<const Field<Type>>& field##Type##Values
92  FOR_ALL_FIELD_TYPES(FieldTypeValuesConstArg)
93  #undef FieldTypeValuesConstArg
94 )
95 {
96  // Open the file
97  std::ofstream os(file, std::ios::binary);
98 
99  // Write the header
100  vtkWriteOps::writeHeader(os, binary, title);
101  os << "DATASET POLYDATA" << nl;
102 
103  // Write the points
104  {
105  os << "POINTS " << points.size() << " float" << nl;
106  List<floatScalar> coordinates(points.size()*3);
107  forAll(points, pointi)
108  {
109  const point& p = points[pointi];
110  forAll(p, i)
111  {
112  coordinates[3*pointi + i] = float(p[i]);
113  }
114  }
115  vtkWriteOps::write(os, binary, coordinates);
116  }
117 
118  // Write the vertices
119  if (vertices.size())
120  {
121  os << "VERTICES " << vertices.size() << ' '
122  << 2*vertices.size() << nl;
123  labelList data(2*vertices.size());
124  forAll(vertices, vertexi)
125  {
126  data[2*vertexi] = 1;
127  data[2*vertexi + 1] = vertices[vertexi];
128  }
129  vtkWriteOps::write(os, binary, data);
130  }
131 
132  // Write the lines
133  if (lines.size())
134  {
135  label nLineNodes = 0;
136  forAll(lines, facei)
137  {
138  nLineNodes += lines[facei].size();
139  }
140  os << "LINES " << lines.size() << ' '
141  << lines.size() + nLineNodes << nl;
142  labelList data(lines.size() + nLineNodes);
143  label i = 0;
144  forAll(lines, linei)
145  {
146  data[i ++] = lines[linei].size();
147  forAll(lines[linei], linePointi)
148  {
149  data[i ++] = lines[linei][linePointi];
150  }
151  }
152  vtkWriteOps::write(os, binary, data);
153  }
154 
155  // Write the faces
156  if (faces.size())
157  {
158  label nFaceNodes = 0;
159  forAll(faces, facei)
160  {
161  nFaceNodes += faces[facei].size();
162  }
163  os << "POLYGONS " << faces.size() << ' '
164  << faces.size() + nFaceNodes << nl;
165  labelList data(faces.size() + nFaceNodes);
166  label i = 0;
167  forAll(faces, facei)
168  {
169  data[i ++] = faces[facei].size();
170  forAll(faces[facei], facePointi)
171  {
172  data[i ++] = faces[facei][facePointi];
173  }
174  }
175  vtkWriteOps::write(os, binary, data);
176  }
177 
178  // Write the fields
179  const label nPointFields = count(fieldIsPointValues, true);
180  const label nFaceFields = count(fieldIsPointValues, false);
181  if (nPointFields > 0)
182  {
183  os << "POINT_DATA " << points.size() << nl
184  << "FIELD attributes " << nPointFields << nl;
185  writeFieldTypeValues<label, label>
186  (
187  os,
188  binary,
189  fieldNames,
190  fieldIsPointValues,
191  fieldLabelValues,
192  true
193  );
194  #define WriteFieldTypeValues(Type, nullArg) \
195  writeFieldTypeValues<Type, floatScalar> \
196  ( \
197  os, \
198  binary, \
199  fieldNames, \
200  fieldIsPointValues, \
201  field##Type##Values, \
202  true \
203  );
205  #undef WriteFieldTypeValues
206  }
207  if (nFaceFields > 0)
208  {
209  os << "CELL_DATA "
210  << vertices.size() + lines.size() + faces.size() << nl
211  << "FIELD attributes " << nFaceFields << nl;
212  writeFieldTypeValues<label, label>
213  (
214  os,
215  binary,
216  fieldNames,
217  fieldIsPointValues,
218  fieldLabelValues,
219  false
220  );
221  #define WriteFieldTypeValues(Type, nullArg) \
222  writeFieldTypeValues<Type, floatScalar> \
223  ( \
224  os, \
225  binary, \
226  fieldNames, \
227  fieldIsPointValues, \
228  field##Type##Values, \
229  false \
230  );
232  #undef WriteFieldTypeValues
233  }
234 }
235 
236 
237 template
238 <
239  class PointField,
240  class VertexList,
241  class LineList,
242  class FaceList,
243  class ... Args
244 >
246 (
247  const fileName& file,
248  const word& title,
249  const bool binary,
250  const PointField& points,
251  const VertexList& vertices,
252  const LineList& lines,
253  const FaceList& faces,
254  const Args& ... args
255 )
256 {
257  const label nFields = sizeof...(Args)/3;
258 
259  wordList fieldNames(nFields);
260  boolList fieldIsPointValues(nFields);
261  UPtrList<const Field<label>> fieldLabelValues(nFields);
262  #define DeclareFieldTypeValues(Type, nullArg) \
263  UPtrList<const Field<Type>> field##Type##Values(nFields);
265  #undef DeclareFieldTypeValues
266 
268  (
269  fieldNames,
270  fieldIsPointValues,
271  fieldLabelValues
272  #define FieldTypeValuesParameter(Type, nullArg) , field##Type##Values
275  args ...
276  );
277 
278  write
279  (
280  file,
281  title,
282  binary,
283  points,
284  vertices,
285  lines,
286  faces,
287  fieldNames,
288  fieldIsPointValues,
289  fieldLabelValues
290  #define FieldTypeValuesParameter(Type, nullArg) , field##Type##Values
293  );
294 }
295 
296 
297 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
void unpackFieldTypeValues(wordList &fieldNames, boolList &fieldIsPointValues, UPtrList< const Field< label >> &fieldLabelValues #define FieldTypeValuesNonConstArg(Type, nullArg))
Helper for templated write.
uint8_t direction
Definition: direction.H:45
void writeFieldTypeValues(std::ostream &os, const bool binary, const wordList &fieldNames, const boolList &fieldIsPointValues, const UPtrList< const Field< Type >> &fieldTypeValues, const bool writePointValues)
Write the field values out for a type.
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
static List< word > fieldNames
Definition: globalFoam.H:46
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
#define DeclareFieldTypeValues(Type, nullArg)
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
GeometricField< Type, pointPatchField, pointMesh > PointField
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
List< label > labelList
A List of labels.
Definition: labelList.H:56
static const char nl
Definition: Ostream.H:260
void write(const fileName &file, const word &title, const bool binary, const PointField &points, const VertexList &vertices, const LineList &lines, const FaceList &faces, const wordList &fieldNames, const boolList &fieldIsPointValues, const UPtrList< const Field< label >> &fieldLabelValues #define FieldTypeValuesConstArg(Type, nullArg))
Write VTK polygonal data to a file. Takes a PtrList of fields of labels and.
#define WriteFieldTypeValues(Type, nullArg)
List< word > wordList
A List of words.
Definition: fileName.H:54
vector point
Point is a vector.
Definition: point.H:41
#define FieldTypeValuesConstArg(Type, nullArg)
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
rDeltaTY field()
volScalarField & p
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
#define FieldTypeValuesParameter(Type, nullArg)