BUGFIX: Illegal use of uninitialised value (backport)
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToVTK / writeFuns.C
blob63a5869ae526f1782b199b19287be17f7225aa1c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "writeFuns.H"
28 #include "vtkTopo.H"
30 #if defined(__mips) && !defined(__SICORTEX__)
31 #include <standards.h>
32 #include <sys/endian.h>
33 #endif
35 // MacOSX
36 #ifdef __DARWIN_BYTE_ORDER
37 #if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN
38 #undef LITTLE_ENDIAN
39 #else
40 #undef BIG_ENDIAN
41 #endif
42 #endif
44 #if defined(LITTLE_ENDIAN) \
45  || defined(_LITTLE_ENDIAN) \
46  || defined(__LITTLE_ENDIAN)
47 #   define LITTLEENDIAN 1
48 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
49 #   undef LITTLEENDIAN
50 #else
51 #   error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
52 #   error "Please add to compilation options"
53 #endif
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 void Foam::writeFuns::swapWord(label& word32)
59     char* mem = reinterpret_cast<char*>(&word32);
61     char a = mem[0];
62     mem[0] = mem[3];
63     mem[3] = a;
65     a = mem[1];
66     mem[1] = mem[2];
67     mem[2] = a;
71 void Foam::writeFuns::swapWords(const label nWords, label* words32)
73     for (label i = 0; i < nWords; i++)
74     {
75         swapWord(words32[i]);
76     }
80 void Foam::writeFuns::write
82     std::ostream& os,
83     const bool binary,
84     List<floatScalar>& fField
87     if (binary)
88     {
89 #       ifdef LITTLEENDIAN
90         swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
91 #       endif
92         os.write
93         (
94             reinterpret_cast<char*>(fField.begin()),
95             fField.size()*sizeof(float)
96         );
98         os << std::endl;
99     }
100     else
101     {
102         forAll(fField, i)
103         {
104             os << fField[i] << ' ';
106             if (i > 0 && (i % 10) == 0)
107             {
108                 os << std::endl;
109             }
110         }
111         os << std::endl;
112     }
116 void Foam::writeFuns::write
118     std::ostream& os,
119     const bool binary,
120     DynamicList<floatScalar>& fField
123     List<floatScalar>& fld = fField.shrink();
125     write(os, binary, fld);
129 void Foam::writeFuns::write
131     std::ostream& os,
132     const bool binary,
133     labelList& elems
136     if (binary)
137     {
138 #       ifdef LITTLEENDIAN
139         swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
140 #       endif
141         os.write
142         (
143             reinterpret_cast<char*>(elems.begin()),
144             elems.size()*sizeof(label)
145         );
147         os << std::endl;
148     }
149     else
150     {
151         forAll(elems, i)
152         {
153             os << elems[i] << ' ';
155             if (i > 0 && (i % 10) == 0)
156             {
157                 os << std::endl;
158             }
159         }
160         os << std::endl;
161     }
165 void Foam::writeFuns::write
167     std::ostream& os,
168     const bool binary,
169     DynamicList<label>& elems
172     labelList& fld = elems.shrink();
174     write(os, binary, fld);
178 void Foam::writeFuns::writeHeader
180     std::ostream& os,
181     const bool binary,
182     const string& name
185     os  << "# vtk DataFile Version 2.0" << std::endl
186         << name << std::endl;
188     if (binary)
189     {
190         os << "BINARY" << std::endl;
191     }
192     else
193     {
194         os << "ASCII" << std::endl;
195     }
199 void Foam::writeFuns::writeCellDataHeader
201     std::ostream& os,
202     const label nCells,
203     const label nFields
206     os  << "CELL_DATA " << nCells << std::endl
207         << "FIELD attributes " << nFields << std::endl;
211 void Foam::writeFuns::writePointDataHeader
213     std::ostream& os,
214     const label nPoints,
215     const label nFields
218     os  << "POINT_DATA  " << nPoints << std::endl
219         << "FIELD attributes " << nFields << std::endl;
223 void Foam::writeFuns::insert(const scalar& pt, DynamicList<floatScalar>& dest)
225     dest.append(float(pt));
229 void Foam::writeFuns::insert(const vector& pt, DynamicList<floatScalar>& dest)
231     for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
232     {
233         dest.append(float(pt[cmpt]));
234     }
238 void Foam::writeFuns::insert
240     const sphericalTensor& pt,
241     DynamicList<floatScalar>& dest
244     for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; cmpt++)
245     {
246         dest.append(float(pt[cmpt]));
247     }
251 void Foam::writeFuns::insert
253     const symmTensor& pt,
254     DynamicList<floatScalar>& dest
257     for (direction cmpt = 0; cmpt < symmTensor::nComponents; cmpt++)
258     {
259         dest.append(float(pt[cmpt]));
260     }
264 void Foam::writeFuns::insert(const tensor& pt, DynamicList<floatScalar>& dest)
266     for (direction cmpt = 0; cmpt < tensor::nComponents; cmpt++)
267     {
268         dest.append(float(pt[cmpt]));
269     }
273 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
275     forAll(source, i)
276     {
277         dest.append(source[i]);
278     }
282 // ************************************************************************* //