no quotes around field name
[OpenFOAM-1.6.x.git] / src / sampling / sampledSurface / writers / vtk / vtkSurfaceWriter.C
blobd8c3d03ea226639f9bd5e38c3ca26c0cfd78498a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
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 "vtkSurfaceWriter.H"
29 #include "OFstream.H"
30 #include "OSspecific.H"
32 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
34 template<class Type>
35 void Foam::vtkSurfaceWriter<Type>::writeGeometry
37     Ostream& os,
38     const pointField& points,
39     const faceList& faces
42     // header
43     os
44         << "# vtk DataFile Version 2.0" << nl
45         << "sampleSurface" << nl
46         << "ASCII" << nl
47         << "DATASET POLYDATA" << nl;
49     // Write vertex coords
50     os  << "POINTS " << points.size() << " float" << nl;
51     forAll(points, pointI)
52     {
53         const point& pt = points[pointI];
54         os  << float(pt.x()) << ' '
55             << float(pt.y()) << ' '
56             << float(pt.z()) << nl;
57     }
58     os  << nl;
61     // Write faces
62     label nNodes = 0;
63     forAll(faces, faceI)
64     {
65         nNodes += faces[faceI].size();
66     }
68     os  << "POLYGONS " << faces.size() << ' '
69         << faces.size() + nNodes << nl;
71     forAll(faces, faceI)
72     {
73         const face& f = faces[faceI];
75         os << f.size();
76         forAll(f, fp)
77         {
78             os << ' ' << f[fp];
79         }
80         os << nl;
81     }
85 namespace Foam
88     // Write scalarField in vtk format
89     template<>
90     void Foam::vtkSurfaceWriter<Foam::scalar>::writeData
91     (
92         Ostream& os,
93         const Field<Foam::scalar>& values
94     )
95     {
96         os << "1 " << values.size() << " float" << nl;
98         forAll(values, elemI)
99         {
100             if (elemI)
101             {
102                 if (elemI % 10)
103                 {
104                     os << ' ';
105                 }
106                 else
107                 {
108                     os << nl;
109                 }
110             }
112             const scalar& v = values[elemI];
113             os << float(v);
114         }
115         os << nl;
116     }
118     // Write vectorField in vtk format
119     template<>
120     void Foam::vtkSurfaceWriter<Foam::vector>::writeData
121     (
122         Ostream& os,
123         const Field<Foam::vector>& values
124     )
125     {
126         os << "3 " << values.size() << " float" << nl;
128         forAll(values, elemI)
129         {
130             const vector& v = values[elemI];
131             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
132                 << nl;
133         }
134     }
137     // Write sphericalTensorField in vtk format
138     template<>
139     void Foam::vtkSurfaceWriter<Foam::sphericalTensor>::writeData
140     (
141         Ostream& os,
142         const Field<sphericalTensor>& values
143     )
144     {
145         os << "1 " << values.size() << " float" << nl;
147         forAll(values, elemI)
148         {
149             const sphericalTensor& v = values[elemI];
150             os << float(v[0]) << nl;
151         }
152     }
155     // Write symmTensorField in vtk format
156     template<>
157     void Foam::vtkSurfaceWriter<Foam::symmTensor>::writeData
158     (
159         Ostream& os,
160         const Field<symmTensor>& values
161     )
162     {
163         os << "6 " << values.size() << " float" << nl;
165         forAll(values, elemI)
166         {
167             const symmTensor& v = values[elemI];
168             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
169                 << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
170                 << nl;
172         }
173     }
176     // Write tensorField in vtk format
177     template<>
178     void Foam::vtkSurfaceWriter<Foam::tensor>::writeData
179     (
180         Ostream& os,
181         const Field<tensor>& values
182     )
183     {
184         os << "9 " << values.size() << " float" << nl;
186         forAll(values, elemI)
187         {
188             const tensor& v = values[elemI];
189             os  << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
190                 << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
191                 << float(v[6]) << ' ' << float(v[7]) << ' ' << float(v[8])
192                 << nl;
193         }
194     }
199 // Write generic field in vtk format
200 template<class Type>
201 void Foam::vtkSurfaceWriter<Type>::writeData
203     Ostream& os,
204     const Field<Type>& values
207     os << "1 " << values.size() << " float" << nl;
209     forAll(values, elemI)
210     {
211         os << float(0) << nl;
212     }
216 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
218 // Construct from components
219 template<class Type>
220 Foam::vtkSurfaceWriter<Type>::vtkSurfaceWriter()
222     surfaceWriter<Type>()
226 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
228 template<class Type>
229 Foam::vtkSurfaceWriter<Type>::~vtkSurfaceWriter()
233 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
235 template<class Type>
236 void Foam::vtkSurfaceWriter<Type>::write
238     const fileName& outputDir,
239     const fileName& surfaceName,
240     const pointField& points,
241     const faceList& faces,
242     const bool verbose
243 ) const
245     if (!isDir(outputDir))
246     {
247         mkDir(outputDir);
248     }
250     fileName fName(outputDir/surfaceName + ".vtk");
252     if (verbose)
253     {
254         Info<< "Writing geometry to " << fName << endl;
255     }
257     OFstream os(fName);
258     writeGeometry(os, points, faces);
262 template<class Type>
263 void Foam::vtkSurfaceWriter<Type>::write
265     const fileName& outputDir,
266     const fileName& surfaceName,
267     const pointField& points,
268     const faceList& faces,
269     const fileName& fieldName,
270     const Field<Type>& values,
271     const bool verbose
272 ) const
274     if (!isDir(outputDir))
275     {
276         mkDir(outputDir);
277     }
279     OFstream os
280     (
281         outputDir/fieldName + '_' + surfaceName + ".vtk"
282     );
284     if (verbose)
285     {
286         Info<< "Writing field " << fieldName << " to " << os.name() << endl;
287     }
289     writeGeometry(os, points, faces);
291     // start writing data
292     if (values.size() == points.size())
293     {
294         os  << "POINT_DATA ";
295     }
296     else
297     {
298         os  << "CELL_DATA ";
299     }
301     os  << values.size() << nl
302         << "FIELD attributes 1" << nl
303         << fieldName.c_str() << " ";
305     // Write data
306     writeData(os, values);
311 // ************************************************************************* //