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