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) 2011-2012 OpenFOAM Foundation
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 "OFstream.H"
29 #include "OSspecific.H"
30 
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  makeSurfaceWriterType(vtkSurfaceWriter);
38 }
39 
40 
41 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 
43 void Foam::vtkSurfaceWriter::writeGeometry
44 (
45  Ostream& os,
46  const pointField& points,
47  const faceList& faces
48 )
49 {
50  // header
51  os
52  << "# vtk DataFile Version 2.0" << nl
53  << "sampleSurface" << nl
54  << "ASCII" << nl
55  << "DATASET POLYDATA" << nl;
56 
57  // Write vertex coords
58  os << "POINTS " << points.size() << " float" << nl;
59  forAll(points, pointI)
60  {
61  const point& pt = points[pointI];
62  os << float(pt.x()) << ' '
63  << float(pt.y()) << ' '
64  << float(pt.z()) << nl;
65  }
66  os << nl;
67 
68 
69  // Write faces
70  label nNodes = 0;
71  forAll(faces, faceI)
72  {
73  nNodes += faces[faceI].size();
74  }
75 
76  os << "POLYGONS " << faces.size() << ' '
77  << faces.size() + nNodes << nl;
78 
79  forAll(faces, faceI)
80  {
81  const face& f = faces[faceI];
82 
83  os << f.size();
84  forAll(f, fp)
85  {
86  os << ' ' << f[fp];
87  }
88  os << nl;
89  }
90 }
91 
92 
93 namespace Foam
94 {
95 
96  template<>
97  void Foam::vtkSurfaceWriter::writeData
98  (
99  Ostream& os,
100  const Field<scalar>& values
101  )
102  {
103  os << "1 " << values.size() << " float" << nl;
104 
105  forAll(values, elemI)
106  {
107  if (elemI)
108  {
109  if (elemI % 10)
110  {
111  os << ' ';
112  }
113  else
114  {
115  os << nl;
116  }
117  }
118 
119  os << float(values[elemI]);
120  }
121  os << nl;
122  }
123 
124 
125  template<>
126  void Foam::vtkSurfaceWriter::writeData
127  (
128  Ostream& os,
129  const Field<vector>& values
130  )
131  {
132  os << "3 " << values.size() << " float" << nl;
133 
134  forAll(values, elemI)
135  {
136  const vector& v = values[elemI];
137  os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
138  << nl;
139  }
140  }
141 
142 
143  template<>
144  void Foam::vtkSurfaceWriter::writeData
145  (
146  Ostream& os,
147  const Field<sphericalTensor>& values
148  )
149  {
150  os << "1 " << values.size() << " float" << nl;
151 
152  forAll(values, elemI)
153  {
154  const sphericalTensor& v = values[elemI];
155  os << float(v[0]) << nl;
156  }
157  }
158 
159 
160  template<>
161  void Foam::vtkSurfaceWriter::writeData
162  (
163  Ostream& os,
164  const Field<symmTensor>& values
165  )
166  {
167  os << "6 " << values.size() << " float" << nl;
168 
169  forAll(values, elemI)
170  {
171  const symmTensor& v = values[elemI];
172  os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
173  << ' '
174  << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
175  << nl;
176 
177  }
178  }
179 
180 
181  template<>
182  void Foam::vtkSurfaceWriter::writeData
183  (
184  Ostream& os,
185  const Field<tensor>& values
186  )
187  {
188  os << "9 " << values.size() << " float" << nl;
189 
190  forAll(values, elemI)
191  {
192  const tensor& v = values[elemI];
193  os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
194  << ' '
195  << float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
196  << ' '
197  << float(v[6]) << ' ' << float(v[7]) << ' ' << float(v[8])
198  << nl;
199  }
200  }
201 
202 }
203 
204 
205 // Write generic field in vtk format
206 template<class Type>
207 void Foam::vtkSurfaceWriter::writeData
208 (
209  Ostream& os,
210  const Field<Type>& values
211 )
212 {
213  os << "1 " << values.size() << " float" << nl;
214 
215  forAll(values, elemI)
216  {
217  os << float(0) << nl;
218  }
219 }
220 
221 
222 template<class Type>
223 void Foam::vtkSurfaceWriter::writeTemplate
224 (
225  const fileName& outputDir,
226  const fileName& surfaceName,
227  const pointField& points,
228  const faceList& faces,
229  const word& fieldName,
230  const Field<Type>& values,
231  const bool isNodeValues,
232  const bool verbose
233 ) const
234 {
235  if (!isDir(outputDir))
236  {
237  mkDir(outputDir);
238  }
239 
240  OFstream os(outputDir/fieldName + '_' + surfaceName + ".vtk");
241 
242  if (verbose)
243  {
244  Info<< "Writing field " << fieldName << " to " << os.name() << endl;
245  }
246 
247  writeGeometry(os, points, faces);
248 
249  // start writing data
250  if (isNodeValues)
251  {
252  os << "POINT_DATA ";
253  }
254  else
255  {
256  os << "CELL_DATA ";
257  }
258 
259  os << values.size() << nl
260  << "FIELD attributes 1" << nl
261  << fieldName << " ";
262 
263  // Write data
264  writeData(os, values);
265 }
266 
267 
268 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
269 
271 :
272  surfaceWriter()
273 {}
274 
275 
276 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
277 
279 {}
280 
281 
282 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
283 
285 (
286  const fileName& outputDir,
287  const fileName& surfaceName,
288  const pointField& points,
289  const faceList& faces,
290  const bool verbose
291 ) const
292 {
293  if (!isDir(outputDir))
294  {
295  mkDir(outputDir);
296  }
297 
298  OFstream os(outputDir/surfaceName + ".vtk");
299 
300  if (verbose)
301  {
302  Info<< "Writing geometry to " << os.name() << endl;
303  }
304 
305  writeGeometry(os, points, faces);
306 }
307 
308 
309 // create write methods
311 
312 
313 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
virtual void write(const fileName &outputDir, const fileName &surfaceName, const pointField &points, const faceList &faces, const bool verbose=false) const
Write single surface geometry to file.
vector point
Point is a vector.
Definition: point.H:41
labelList f(nPoints)
A surfaceWriter for VTK legacy format.
Templated 3D symmetric tensor derived from VectorSpace adding construction from 6 components...
Definition: SymmTensor.H:53
virtual ~vtkSurfaceWriter()
Destructor.
Convenience macros for instantiating writer methods for surfaceWriter classes.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A class for handling words, derived from string.
Definition: word.H:59
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
defineSurfaceWriterWriteFields(nastranSurfaceWriter)
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
messageStream Info
List< face > faceList
Definition: faceListFwd.H:43
Templated 3D SphericalTensor derived from VectorSpace adding construction from 1 component, element access using th ii() member function and the inner-product (dot-product) and outer-product operators.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Namespace for OpenFOAM.
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:118
Base class for surface writers.
Definition: surfaceWriter.H:54
#define forAll(list, i)
Definition: UList.H:421
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:616
vtkSurfaceWriter()
Construct null.
A class for handling file names.
Definition: fileName.H:69
makeSurfaceWriterType(dxSurfaceWriter)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:420