All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2020 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 #include "vtkWriteOps.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  makeSurfaceWriterType(vtkSurfaceWriter);
37 }
38 
39 
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 
42 void Foam::vtkSurfaceWriter::writeGeometry
43 (
44  std::ostream& os,
45  const pointField& points,
46  const faceList& faces
47 ) const
48 {
49  const bool binary = (writeFormat_ == IOstream::BINARY);
50 
51  // VTK header
52  vtkWriteOps::writeHeader(os, binary, "sampleSurface");
53  os << "DATASET POLYDATA" << nl;
54 
55  // Write vertex coords
56  os << "POINTS " << points.size() << " float" << nl;
57 
58  List<floatScalar> po(points.size()*3);
59  label ind = 0;
60  forAll(points, pointi)
61  {
62  const point& pt = points[pointi];
63  forAll(pt, cmpt)
64  {
65  po[ind++] = float(pt[cmpt]);
66  }
67  }
68  vtkWriteOps::write(os, binary, po);
69 
70  // Write faces
71  label nNodes = 0;
72  forAll(faces, facei)
73  {
74  nNodes += faces[facei].size();
75  }
76 
77  os << "POLYGONS " << faces.size() << ' '
78  << faces.size() + nNodes << nl;
79 
80  labelList polygons(faces.size() + nNodes);
81  ind = 0;
82  forAll(faces, facei)
83  {
84  const face& f = faces[facei];
85  polygons[ind++] = f.size();
86  forAll(f, fp)
87  {
88  polygons[ind++] = f[fp];
89  }
90  }
91  vtkWriteOps::write(os, binary, polygons);
92 }
93 
94 
95 template<class Type>
96 void Foam::vtkSurfaceWriter::Write
97 (
98  const fileName& outputDir,
99  const fileName& surfaceName,
100  const pointField& points,
101  const faceList& faces,
102  const word& fieldName,
103  const Field<Type>& values,
104  const bool isNodeValues
105 ) const
106 {
107  const bool binary = (writeFormat_ == IOstream::BINARY);
108 
109  if (!isDir(outputDir))
110  {
111  mkDir(outputDir);
112  }
113 
114  const word filePath = outputDir/fieldName + '_' + surfaceName + ".vtk";
115 
116  ofstream os(filePath, std::ios::binary);
117 
118  if (debug)
119  {
120  Info<< "Writing field " << fieldName << " to " << filePath << endl;
121  }
122 
123  writeGeometry(os, points, faces);
124 
125  // Write data
126  if (isNodeValues)
127  {
128  os << "POINT_DATA ";
129  }
130  else
131  {
132  os << "CELL_DATA ";
133  }
134 
135  os << values.size() << nl
136  << "FIELD attributes 1" << nl
137  << fieldName << " ";
138 
139  const label nComp = pTraits<Type>::nComponents;
140 
141  os << nComp << " " << values.size() << " float" << nl;
142 
143  List<floatScalar> vals(values.size()*nComp);
144  label ind = 0;
145  forAll(values, elemI)
146  {
147  for (direction cmpt=0; cmpt < nComp; ++cmpt)
148  {
149  vals[ind++] = component(values[elemI], cmpt);
150  }
151  }
152 
153  vtkWriteOps::write(os, binary, vals);
154 }
155 
156 
157 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
158 
160 (
161  const IOstream::streamFormat writeFormat
162 )
163 :
164  surfaceWriter(writeFormat)
165 {}
166 
167 
168 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
169 
171 {}
172 
173 
174 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
175 
177 (
178  const fileName& outputDir,
179  const fileName& surfaceName,
180  const pointField& points,
181  const faceList& faces
182 ) const
183 {
184  if (!isDir(outputDir))
185  {
186  mkDir(outputDir);
187  }
188 
189  word filePath = outputDir/surfaceName + ".vtk";
190  ofstream os(filePath, std::ios::binary);
191 
192  if (debug)
193  {
194  Info<< "Writing geometry to " << filePath << endl;
195  }
196 
197  writeGeometry(os, points, faces);
198 }
199 
200 
201 // Create write methods
203 
204 
205 // ************************************************************************* //
#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
uint8_t direction
Definition: direction.H:45
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual ~vtkSurfaceWriter()
Destructor.
virtual void write(const fileName &outputDir, const fileName &surfaceName, const pointField &points, const faceList &faces) const
Write single surface geometry to file.
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
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
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.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
defineSurfaceWriterWriteFields(nastranSurfaceWriter)
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
List< label > labelList
A List of labels.
Definition: labelList.H:56
static const char nl
Definition: Ostream.H:260
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
vector point
Point is a vector.
Definition: point.H:41
A surfaceWriter for VTK legacy format with support for writing ASCII or binary.
makeSurfaceWriterType(ensightSurfaceWriter)
messageStream Info
vtkSurfaceWriter(const IOstream::streamFormat writeFormat)
Construct given write format.
Base class for surface writers.
Definition: surfaceWriter.H:54
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.