FreeFOAM The Cross-Platform CFD Toolkit
vtkSurfaceWriter.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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 "vtkSurfaceWriter.H"
27 
28 #include <OpenFOAM/OFstream.H>
29 #include <OpenFOAM/OSspecific.H>
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  Ostream& os,
37  const pointField& points,
38  const faceList& faces
39 )
40 {
41  // header
42  os
43  << "# vtk DataFile Version 2.0" << nl
44  << "sampleSurface" << nl
45  << "ASCII" << nl
46  << "DATASET POLYDATA" << nl;
47 
48  // Write vertex coords
49  os << "POINTS " << points.size() << " float" << nl;
50  forAll(points, pointI)
51  {
52  const point& pt = points[pointI];
53  os << float(pt.x()) << ' '
54  << float(pt.y()) << ' '
55  << float(pt.z()) << nl;
56  }
57  os << nl;
58 
59 
60  // Write faces
61  label nNodes = 0;
62  forAll(faces, faceI)
63  {
64  nNodes += faces[faceI].size();
65  }
66 
67  os << "POLYGONS " << faces.size() << ' '
68  << faces.size() + nNodes << nl;
69 
70  forAll(faces, faceI)
71  {
72  const face& f = faces[faceI];
73 
74  os << f.size();
75  forAll(f, fp)
76  {
77  os << ' ' << f[fp];
78  }
79  os << nl;
80  }
81 }
82 
83 
84 namespace Foam
85 {
86 
87  // Write scalarField in vtk format
88  template<>
90  (
91  Ostream& os,
92  const Field<Foam::scalar>& values
93  )
94  {
95  os << "1 " << values.size() << " float" << nl;
96 
97  forAll(values, elemI)
98  {
99  if (elemI)
100  {
101  if (elemI % 10)
102  {
103  os << ' ';
104  }
105  else
106  {
107  os << nl;
108  }
109  }
110 
111  const scalar& v = values[elemI];
112  os << float(v);
113  }
114  os << nl;
115  }
116 
117  // Write vectorField in vtk format
118  template<>
120  (
121  Ostream& os,
122  const Field<Foam::vector>& values
123  )
124  {
125  os << "3 " << values.size() << " float" << nl;
126 
127  forAll(values, elemI)
128  {
129  const vector& v = values[elemI];
130  os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
131  << nl;
132  }
133  }
134 
135 
136  // Write sphericalTensorField in vtk format
137  template<>
139  (
140  Ostream& os,
141  const Field<sphericalTensor>& values
142  )
143  {
144  os << "1 " << values.size() << " float" << nl;
145 
146  forAll(values, elemI)
147  {
148  const sphericalTensor& v = values[elemI];
149  os << float(v[0]) << nl;
150  }
151  }
152 
153 
154  // Write symmTensorField in vtk format
155  template<>
157  (
158  Ostream& os,
159  const Field<symmTensor>& values
160  )
161  {
162  os << "6 " << values.size() << " float" << nl;
163 
164  forAll(values, elemI)
165  {
166  const symmTensor& v = values[elemI];
167  os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
168  << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
169  << nl;
170 
171  }
172  }
173 
174 
175  // Write tensorField in vtk format
176  template<>
178  (
179  Ostream& os,
180  const Field<tensor>& values
181  )
182  {
183  os << "9 " << values.size() << " float" << nl;
184 
185  forAll(values, elemI)
186  {
187  const tensor& v = values[elemI];
188  os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
189  << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
190  << float(v[6]) << ' ' << float(v[7]) << ' ' << float(v[8])
191  << nl;
192  }
193  }
194 
195 }
196 
197 
198 // Write generic field in vtk format
199 template<class Type>
201 (
202  Ostream& os,
203  const Field<Type>& values
204 )
205 {
206  os << "1 " << values.size() << " float" << nl;
207 
208  forAll(values, elemI)
209  {
210  os << float(0) << nl;
211  }
212 }
213 
214 
215 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
216 
217 // Construct from components
218 template<class Type>
220 :
221  surfaceWriter<Type>()
222 {}
223 
224 
225 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
226 
227 template<class Type>
229 {}
230 
231 
232 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
233 
234 template<class Type>
236 (
237  const fileName& outputDir,
238  const fileName& surfaceName,
239  const pointField& points,
240  const faceList& faces,
241  const bool verbose
242 ) const
243 {
244  if (!isDir(outputDir))
245  {
246  mkDir(outputDir);
247  }
248 
249  fileName fName(outputDir/surfaceName + ".vtk");
250 
251  if (verbose)
252  {
253  Info<< "Writing geometry to " << fName << endl;
254  }
255 
256  OFstream os(fName);
257  writeGeometry(os, points, faces);
258 }
259 
260 
261 template<class Type>
263 (
264  const fileName& outputDir,
265  const fileName& surfaceName,
266  const pointField& points,
267  const faceList& faces,
268  const fileName& fieldName,
269  const Field<Type>& values,
270  const bool verbose
271 ) const
272 {
273  if (!isDir(outputDir))
274  {
275  mkDir(outputDir);
276  }
277 
278  OFstream os
279  (
280  outputDir/fieldName + '_' + surfaceName + ".vtk"
281  );
282 
283  if (verbose)
284  {
285  Info<< "Writing field " << fieldName << " to " << os.name() << endl;
286  }
287 
288  writeGeometry(os, points, faces);
289 
290  // start writing data
291  if (values.size() == points.size())
292  {
293  os << "POINT_DATA ";
294  }
295  else
296  {
297  os << "CELL_DATA ";
298  }
299 
300  os << values.size() << nl
301  << "FIELD attributes 1" << nl
302  << fieldName.c_str() << " ";
303 
304  // Write data
305  writeData(os, values);
306 
307 }
308 
309 
310 // ************************ vim: set sw=4 sts=4 et: ************************ //