Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToVTK / writeFuns.C
bloba1d021f5ab6feaea1154da3d22fdba0569a5914c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "writeFuns.H"
27 #include "vtkTopo.H"
29 #if defined(__mips) && !defined(__SICORTEX__)
30 #include <standards.h>
31 #include <sys/endian.h>
32 #endif
34 // MacOSX
35 #ifdef __DARWIN_BYTE_ORDER
36 #if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN
37 #undef LITTLE_ENDIAN
38 #else
39 #undef BIG_ENDIAN
40 #endif
41 #endif
43 #if defined(LITTLE_ENDIAN) \
44  || defined(_LITTLE_ENDIAN) \
45  || defined(__LITTLE_ENDIAN)
46 #   define LITTLEENDIAN 1
47 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
48 #   undef LITTLEENDIAN
49 #else
50 #   error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
51 #   error "Please add to compilation options"
52 #endif
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 void Foam::writeFuns::swapWord(label& word32)
58     char* mem = reinterpret_cast<char*>(&word32);
60     char a = mem[0];
61     mem[0] = mem[3];
62     mem[3] = a;
64     a = mem[1];
65     mem[1] = mem[2];
66     mem[2] = a;
70 void Foam::writeFuns::swapWords(const label nWords, label* words32)
72     for (label i = 0; i < nWords; i++)
73     {
74         swapWord(words32[i]);
75     }
79 void Foam::writeFuns::write
81     std::ostream& os,
82     const bool binary,
83     List<floatScalar>& fField
86     if (binary)
87     {
88 #       ifdef LITTLEENDIAN
89         swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
90 #       endif
91         os.write
92         (
93             reinterpret_cast<char*>(fField.begin()),
94             fField.size()*sizeof(float)
95         );
97         os << std::endl;
98     }
99     else
100     {
101         forAll(fField, i)
102         {
103             os << fField[i] << ' ';
105             if (i > 0 && (i % 10) == 0)
106             {
107                 os << std::endl;
108             }
109         }
110         os << std::endl;
111     }
115 void Foam::writeFuns::write
117     std::ostream& os,
118     const bool binary,
119     DynamicList<floatScalar>& fField
122     List<floatScalar>& fld = fField.shrink();
124     write(os, binary, fld);
128 void Foam::writeFuns::write
130     std::ostream& os,
131     const bool binary,
132     labelList& elems
135     if (binary)
136     {
137 #       ifdef LITTLEENDIAN
138         swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
139 #       endif
140         os.write
141         (
142             reinterpret_cast<char*>(elems.begin()),
143             elems.size()*sizeof(label)
144         );
146         os << std::endl;
147     }
148     else
149     {
150         forAll(elems, i)
151         {
152             os << elems[i] << ' ';
154             if (i > 0 && (i % 10) == 0)
155             {
156                 os << std::endl;
157             }
158         }
159         os << std::endl;
160     }
164 void Foam::writeFuns::write
166     std::ostream& os,
167     const bool binary,
168     DynamicList<label>& elems
171     labelList& fld = elems.shrink();
173     write(os, binary, fld);
177 void Foam::writeFuns::writeHeader
179     std::ostream& os,
180     const bool binary,
181     const string& name
184     os  << "# vtk DataFile Version 2.0" << std::endl
185         << name << std::endl;
187     if (binary)
188     {
189         os << "BINARY" << std::endl;
190     }
191     else
192     {
193         os << "ASCII" << std::endl;
194     }
198 void Foam::writeFuns::writeCellDataHeader
200     std::ostream& os,
201     const label nCells,
202     const label nFields
205     os  << "CELL_DATA " << nCells << std::endl
206         << "FIELD attributes " << nFields << std::endl;
210 void Foam::writeFuns::writePointDataHeader
212     std::ostream& os,
213     const label nPoints,
214     const label nFields
217     os  << "POINT_DATA  " << nPoints << std::endl
218         << "FIELD attributes " << nFields << std::endl;
222 void Foam::writeFuns::insert(const scalar& pt, DynamicList<floatScalar>& dest)
224     dest.append(float(pt));
228 void Foam::writeFuns::insert(const vector& pt, DynamicList<floatScalar>& dest)
230     for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
231     {
232         dest.append(float(pt[cmpt]));
233     }
237 void Foam::writeFuns::insert
239     const sphericalTensor& pt,
240     DynamicList<floatScalar>& dest
243     for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; cmpt++)
244     {
245         dest.append(float(pt[cmpt]));
246     }
250 void Foam::writeFuns::insert
252     const symmTensor& pt,
253     DynamicList<floatScalar>& dest
256     for (direction cmpt = 0; cmpt < symmTensor::nComponents; cmpt++)
257     {
258         dest.append(float(pt[cmpt]));
259     }
263 void Foam::writeFuns::insert(const tensor& pt, DynamicList<floatScalar>& dest)
265     for (direction cmpt = 0; cmpt < tensor::nComponents; cmpt++)
266     {
267         dest.append(float(pt[cmpt]));
268     }
272 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
274     forAll(source, i)
275     {
276         dest.append(source[i]);
277     }
281 // ************************************************************************* //