vtkWriteOps.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) 2011-2020 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 "vtkWriteOps.H"
27 
28 #if defined(__mips)
29  #include <standards.h>
30  #include <sys/endian.h>
31 #endif
32 
33 // MacOSX
34 #ifdef __DARWIN_BYTE_ORDER
35  #if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN
36  #undef LITTLE_ENDIAN
37  #else
38  #undef BIG_ENDIAN
39  #endif
40 #endif
41 
42 #if defined(LITTLE_ENDIAN) \
43  || defined(_LITTLE_ENDIAN) \
44  || defined(__LITTLE_ENDIAN)
45  #define LITTLEENDIAN 1
46 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
47  #undef LITTLEENDIAN
48 #else
49  #error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
50  #error "Please add to compilation options"
51 #endif
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
56 {
57  char* mem = reinterpret_cast<char*>(&word32);
58 
59  char a = mem[0];
60  mem[0] = mem[3];
61  mem[3] = a;
62 
63  a = mem[1];
64  mem[1] = mem[2];
65  mem[2] = a;
66 }
67 
68 
69 void Foam::vtkWriteOps::swapWords(const label nWords, label* words32)
70 {
71  for (label i = 0; i < nWords; i++)
72  {
73  swapWord(words32[i]);
74  }
75 }
76 
77 
79 (
80  std::ostream& os,
81  const bool binary,
82  List<floatScalar>& fField
83 )
84 {
85  if (binary)
86  {
87  #ifdef LITTLEENDIAN
88  swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
89  #endif
90  os.write
91  (
92  reinterpret_cast<char*>(fField.begin()),
93  fField.size()*sizeof(float)
94  );
95 
96  os << std::endl;
97  }
98  else
99  {
100  forAll(fField, i)
101  {
102  os << fField[i];
103 
104  if (i > 0 && (i % 10) == 0)
105  {
106  os << std::endl;
107  }
108  else
109  {
110  os << ' ';
111  }
112  }
113  os << std::endl;
114  }
115 }
116 
117 
119 (
120  std::ostream& os,
121  const bool binary,
122  DynamicList<floatScalar>& fField
123 )
124 {
125  List<floatScalar>& fld = fField.shrink();
126 
127  write(os, binary, fld);
128 }
129 
130 
132 (
133  std::ostream& os,
134  const bool binary,
135  labelList& elems
136 )
137 {
138  if (binary)
139  {
140  #ifdef LITTLEENDIAN
141  swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
142  #endif
143  os.write
144  (
145  reinterpret_cast<char*>(elems.begin()),
146  elems.size()*sizeof(label)
147  );
148 
149  os << std::endl;
150  }
151  else
152  {
153  forAll(elems, i)
154  {
155  os << elems[i];
156 
157  if (i > 0 && (i % 10) == 0)
158  {
159  os << std::endl;
160  }
161  else
162  {
163  os << ' ';
164  }
165  }
166  os << std::endl;
167  }
168 }
169 
170 
172 (
173  std::ostream& os,
174  const bool binary,
175  DynamicList<label>& elems
176 )
177 {
178  labelList& fld = elems.shrink();
179 
180  write(os, binary, fld);
181 }
182 
183 
185 (
186  std::ostream& os,
187  const bool binary,
188  const std::string& title
189 )
190 {
191  os << "# vtk DataFile Version 2.0" << std::endl
192  << title << std::endl;
193 
194  if (binary)
195  {
196  os << "BINARY" << std::endl;
197  }
198  else
199  {
200  os << "ASCII" << std::endl;
201  }
202 }
203 
204 
206 (
207  std::ostream& os,
208  const label nCells,
209  const label nFields
210 )
211 {
212  os << "CELL_DATA " << nCells << std::endl
213  << "FIELD attributes " << nFields << std::endl;
214 }
215 
216 
218 (
219  std::ostream& os,
220  const label nPoints,
221  const label nFields
222 )
223 {
224  os << "POINT_DATA " << nPoints << std::endl
225  << "FIELD attributes " << nFields << std::endl;
226 }
227 
228 
229 void Foam::vtkWriteOps::insert(const scalar src, DynamicList<floatScalar>& dest)
230 {
231  dest.append(float(src));
232 }
233 
234 
236 (
237  const vector& src,
238  DynamicList<floatScalar>& dest
239 )
240 {
241  for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
242  {
243  dest.append(float(src[cmpt]));
244  }
245 }
246 
247 
249 (
250  const sphericalTensor& src,
251  DynamicList<floatScalar>& dest
252 )
253 {
254  for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; ++cmpt)
255  {
256  dest.append(float(src[cmpt]));
257  }
258 }
259 
260 
262 (
263  const symmTensor& src,
264  DynamicList<floatScalar>& dest
265 )
266 {
267  dest.append(float(src.xx()));
268  dest.append(float(src.yy()));
269  dest.append(float(src.zz()));
270  dest.append(float(src.xy()));
271  dest.append(float(src.yz()));
272  dest.append(float(src.xz()));
273 }
274 
275 
277 (
278  const tensor& src,
279  DynamicList<floatScalar>& dest
280 )
281 {
282  for (direction cmpt = 0; cmpt < tensor::nComponents; ++cmpt)
283  {
284  dest.append(float(src[cmpt]));
285  }
286 }
287 
288 
289 void Foam::vtkWriteOps::insert(const labelList& src, DynamicList<label>& dest)
290 {
291  dest.append(src);
292 }
293 
294 
296 (
297  const labelList& map,
298  const List<scalar>& source,
299  DynamicList<floatScalar>& dest
300 )
301 {
302  forAll(map, i)
303  {
304  dest.append(float(source[map[i]]));
305  }
306 }
307 
308 
310 (
311  const List<point>& source,
312  DynamicList<floatScalar>& dest
313 )
314 {
315  forAll(source, i)
316  {
317  insert(source[i], dest);
318  }
319 }
320 
321 
323 (
324  const labelList& map,
325  const List<point>& source,
326  DynamicList<floatScalar>& dest
327 )
328 {
329  forAll(map, i)
330  {
331  insert(source[map[i]], dest);
332  }
333 }
334 
335 
336 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:59
uint8_t direction
Definition: direction.H:45
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void writeCellDataHeader(std::ostream &, const label nCells, const label nFields)
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:99
void swapWords(const label nWords, label *words32)
Swap halves of word.
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
SymmTensor< scalar > symmTensor
SymmTensor of scalars.
Definition: symmTensor.H:48
label nPoints
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
void writePointDataHeader(std::ostream &, const label nPoints, const label nFields)
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars.
Tensor< scalar > tensor
Tensor of scalars.
Definition: tensor.H:51
void swapWord(label &word32)
Swap halves of word.