BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / applications / utilities / postProcessing / dataConversion / foamToVTK / writeFuns.C
blob312bf68182dc74912645e5b3f79f907fb506f2a3
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, 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             else
110             {
111                 os  << ' ';
112             }
113         }
114         os << std::endl;
115     }
119 void Foam::writeFuns::write
121     std::ostream& os,
122     const bool binary,
123     DynamicList<floatScalar>& fField
126     List<floatScalar>& fld = fField.shrink();
128     write(os, binary, fld);
132 void Foam::writeFuns::write
134     std::ostream& os,
135     const bool binary,
136     labelList& elems
139     if (binary)
140     {
141 #       ifdef LITTLEENDIAN
142         swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
143 #       endif
144         os.write
145         (
146             reinterpret_cast<char*>(elems.begin()),
147             elems.size()*sizeof(label)
148         );
150         os  << std::endl;
151     }
152     else
153     {
154         forAll(elems, i)
155         {
156             os  << elems[i];
158             if (i > 0 && (i % 10) == 0)
159             {
160                 os  << std::endl;
161             }
162             else
163             {
164                 os  << ' ';
165             }
166         }
167         os  << std::endl;
168     }
172 void Foam::writeFuns::write
174     std::ostream& os,
175     const bool binary,
176     DynamicList<label>& elems
179     labelList& fld = elems.shrink();
181     write(os, binary, fld);
185 void Foam::writeFuns::writeHeader
187     std::ostream& os,
188     const bool binary,
189     const std::string& title
192     os  << "# vtk DataFile Version 2.0" << std::endl
193         << title << std::endl;
195     if (binary)
196     {
197         os  << "BINARY" << std::endl;
198     }
199     else
200     {
201         os  << "ASCII" << std::endl;
202     }
206 void Foam::writeFuns::writeCellDataHeader
208     std::ostream& os,
209     const label nCells,
210     const label nFields
213     os  << "CELL_DATA " << nCells << std::endl
214         << "FIELD attributes " << nFields << std::endl;
218 void Foam::writeFuns::writePointDataHeader
220     std::ostream& os,
221     const label nPoints,
222     const label nFields
225     os  << "POINT_DATA  " << nPoints << std::endl
226         << "FIELD attributes " << nFields << std::endl;
230 void Foam::writeFuns::insert(const scalar src, DynamicList<floatScalar>& dest)
232     dest.append(float(src));
236 void Foam::writeFuns::insert(const vector& src, DynamicList<floatScalar>& dest)
238     for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
239     {
240         dest.append(float(src[cmpt]));
241     }
245 void Foam::writeFuns::insert
247     const sphericalTensor& src,
248     DynamicList<floatScalar>& dest
251     for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; ++cmpt)
252     {
253         dest.append(float(src[cmpt]));
254     }
258 void Foam::writeFuns::insert
260     const symmTensor& src,
261     DynamicList<floatScalar>& dest
264     dest.append(float(src.xx()));
265     dest.append(float(src.yy()));
266     dest.append(float(src.zz()));
267     dest.append(float(src.xy()));
268     dest.append(float(src.yz()));
269     dest.append(float(src.xz()));
273 void Foam::writeFuns::insert(const tensor& src, DynamicList<floatScalar>& dest)
275     for (direction cmpt = 0; cmpt < tensor::nComponents; ++cmpt)
276     {
277         dest.append(float(src[cmpt]));
278     }
282 void Foam::writeFuns::insert(const labelList& src, DynamicList<label>& dest)
284     dest.append(src);
288 // ************************************************************************* //