VTKsurfaceFormat.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "VTKsurfaceFormat.H"
30 #include "vtkUnstructuredReader.H"
31 #include "scalarIOField.H"
32 #include "faceTraits.H"
33 #include <fstream>
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 template<class Face>
39 (
40  vtk::formatter& format,
41  const UList<Face>& faces
42 )
43 {
44  // connectivity count without additional storage (done internally)
45  label nConnectivity = 0;
46  for (const Face& f : faces)
47  {
48  nConnectivity += f.size();
49  }
50 
52  (
53  format.os(),
54  faces.size(),
55  nConnectivity
56  );
57 
58 
59  // legacy: size + connectivity together
60  // [nPts, id1, id2, ..., nPts, id1, id2, ...]
61 
62  for (const Face& f : faces)
63  {
64  format.write(f.size()); // The size prefix
66  }
67 
68  format.flush();
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
73 
74 template<class Face>
76 (
77  const fileName& filename
78 )
79 {
80  read(filename);
81 }
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
86 template<class Face>
88 (
89  const fileName& filename
90 )
91 {
92  this->clear();
93 
94  IFstream is(filename);
95  if (!is.good())
96  {
98  << "Cannot read file " << filename
99  << exit(FatalError);
100  }
101 
102  // Assume groups are not intermixed
103  bool sorted = true;
104 
105 
106  // Use dummy Time for objectRegistry
107  autoPtr<Time> dummyTimePtr(Time::New());
108 
109  objectRegistry obr
110  (
111  IOobject
112  (
113  "vtk::surfaceFormat",
114  *dummyTimePtr,
115  IOobject::NO_READ,
116  IOobject::NO_WRITE,
117  false
118  )
119  );
120 
121  // Read all
122  vtkUnstructuredReader reader(obr, is);
123  const faceList& faces = reader.faces();
124 
125  // Assume all faces in zone0 unless a region field is present
126  labelList zones(faces.size(), Zero);
127 
128  for (auto fieldName : { "region", "STLSolidLabeling" })
129  {
130  const labelIOField* lptr =
131  reader.cellData().findObject<labelIOField>(fieldName);
132 
133  if (lptr)
134  {
135  label i = 0;
136  for (const auto& region : *lptr)
137  {
138  zones[i++] = label(region);
139  }
140  break;
141  }
142 
143  const scalarIOField* sptr =
144  reader.cellData().findObject<scalarIOField>(fieldName);
145 
146  if (sptr)
147  {
148  label i = 0;
149  for (const auto& region : *sptr)
150  {
151  zones[i++] = label(region);
152  }
153  break;
154  }
155  }
156 
157 
158  // Create zone names
159  const label nZones = max(zones)+1;
160  wordList zoneNames(nZones);
161  forAll(zoneNames, i)
162  {
163  zoneNames[i] = "zone" + Foam::name(i);
164  }
165 
166 
167  // Check if it needs triangulation
168  label nTri = 0;
170  {
171  for (const face& f : faces)
172  {
173  nTri += f.nTriangles();
174  }
175  }
176 
177  if (nTri > faces.size())
178  {
179  // We are here if the target surface needs triangles and
180  // the source surface has non-triangles
181 
182  DynamicList<Face> dynFaces(nTri);
183  DynamicList<label> dynZones(nTri);
184 
185  forAll(faces, facei)
186  {
187  const face& f = faces[facei];
188  for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
189  {
190  const label fp2 = f.fcIndex(fp1);
191 
192  dynFaces.append(Face{f[0], f[fp1], f[fp2]});
193  dynZones.append(zones[facei]);
194  }
195  }
196  zones.clear();
197 
198  // Count
199  labelList zoneSizes(nZones, Zero);
200  for (const label zonei : dynZones)
201  {
202  zoneSizes[zonei]++;
203  }
204 
205  this->sortFacesAndStore(dynFaces, dynZones, sorted);
206 
207  // Add zones (retaining empty ones)
208  this->addZones(zoneSizes, zoneNames);
209  }
210  else
211  {
212  DynamicList<Face> dynFaces(faces.size());
213  DynamicList<label> dynZones(std::move(zones));
214 
215  for (const face& f : faces)
216  {
217  dynFaces.append(Face(f));
218  }
219 
220  // Count
221  labelList zoneSizes(nZones, Zero);
222  for (const label zonei : zones)
223  {
224  zoneSizes[zonei]++;
225  }
226 
227  this->sortFacesAndStore(dynFaces, dynZones, sorted);
228 
229  // Add zones (retaining empty ones)
230  this->addZones(zoneSizes, zoneNames);
231  }
232  this->addZonesToFaces(); // for labelledTri
233 
234  // transfer to normal lists
235  this->storedPoints().transfer(reader.points());
236 
237  return true;
238 }
239 
240 
241 template<class Face>
243 (
244  const fileName& filename,
245  const MeshedSurfaceProxy<Face>& surf,
246  const dictionary& options
247 )
248 {
249  const UList<point>& pointLst = surf.points();
250  const UList<Face>& faceLst = surf.surfFaces();
251  const UList<label>& faceMap = surf.faceMap();
252 
253  const surfZoneList zones =
254  (
255  surf.surfZones().empty()
256  ? surfaceFormatsCore::oneZone(faceLst)
257  : surf.surfZones()
258  );
259 
260  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
261 
262  vtk::outputOptions opts = formatOptions(options);
263 
264  std::ofstream os(filename, std::ios::binary);
265 
267 
268  writeHeader(format(), pointLst);
269 
270  if (useFaceMap)
271  {
272  // connectivity count without additional storage (done internally)
273  label nConnectivity = 0;
274  for (const Face& f : faceLst)
275  {
276  nConnectivity += f.size();
277  }
278 
280  (
281  format().os(),
282  faceLst.size(),
283  nConnectivity
284  );
285 
286  label faceIndex = 0;
287  for (const surfZone& zone : zones)
288  {
289  forAll(zone, i)
290  {
291  const Face& f = faceLst[faceMap[faceIndex++]];
292 
293  format().write(f.size()); // The size prefix
294  vtk::writeList(format(), f);
295  }
296  }
297 
298  format().flush();
299  }
300  else
301  {
302  // Easy to write polys without a faceMap
303  writePolys(format(), faceLst);
304  }
305 
306  // Write regions (zones) as CellData
307  if (zones.size() > 1)
308  {
309  writeCellData(format(), zones);
310  }
311 }
312 
313 
314 template<class Face>
316 (
317  const fileName& filename,
318  const UnsortedMeshedSurface<Face>& surf,
319  const dictionary& options
320 )
321 {
322  vtk::outputOptions opts = formatOptions(options);
323 
324  std::ofstream os(filename, std::ios::binary);
325 
327 
328  writeHeader(format(), surf.points());
329 
330  // Easy to write polys without a faceMap
331  writePolys(format(), surf.surfFaces());
332 
333  // Write regions (zones) as CellData
334  writeCellData(format(), surf.zoneIds());
335 }
336 
337 
338 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
nZones
label nZones
Definition: interpolatedFaces.H:24
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::fileFormats::VTKsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: VTKsurfaceFormat.C:243
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:94
VTKsurfaceFormat.H
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::vtkUnstructuredReader::faces
const faceList & faces() const
2D cells (=faces)
Definition: vtkUnstructuredReader.H:272
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::vtkUnstructuredReader::cellData
const objectRegistry & cellData() const
Cell based fields.
Definition: vtkUnstructuredReader.H:304
Foam::MeshedSurfaceProxy::useFaceMap
bool useFaceMap() const
Use faceMap?
Definition: MeshedSurfaceProxy.H:186
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:97
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:57
vtkUnstructuredReader.H
Foam::vtkUnstructuredReader
Reader for vtk UNSTRUCTURED_GRID legacy files. Supports single CELLS, POINTS etc. entry only.
Definition: vtkUnstructuredReader.H:66
Foam::vtkUnstructuredReader::points
const pointField & points() const
Points.
Definition: vtkUnstructuredReader.H:245
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::vtk::legacy::beginPolys
void beginPolys(std::ostream &os, label nPolys, label nConnectivity)
Emit header for POLYGONS (with trailing newline).
Definition: foamVtkOutputI.H:121
Foam::vtk::outputOptions::newFormatter
autoPtr< formatter > newFormatter(std::ostream &os) const
Return new formatter based on the selected output options.
Definition: foamVtkOutputOptionsI.H:63
Foam::MeshedSurfaceProxy::points
const pointField & points() const
Return const access to the points.
Definition: MeshedSurfaceProxy.H:160
Foam::fileFormats::VTKsurfaceFormat::VTKsurfaceFormat
VTKsurfaceFormat(const fileName &filename)
Construct from file name.
Definition: VTKsurfaceFormat.C:76
scalarIOField.H
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:78
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:180
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::UnsortedMeshedSurface::zoneIds
virtual const labelList & zoneIds() const
Return const access to the zone ids.
Definition: UnsortedMeshedSurface.H:300
format
word format(conversionProperties.get< word >("format"))
Foam::faceTraits
Traits class for faces.
Definition: faceTraits.H:50
Foam::label
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:62
Foam::UnsortedMeshedSurface
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
Definition: MeshedSurface.H:79
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:472
Foam::fileFormats::VTKsurfaceFormat::read
virtual bool read(const fileName &filename)
Read from file.
Definition: VTKsurfaceFormat.C:88
Foam::vtk::writeList
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:112
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::objectRegistry::findObject
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:401
clear
patchWriters clear()
f
labelList f(nPoints)
Foam::List< face >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::fileFormats::VTKsurfaceFormat
Read/write VTK legacy format (ASCII) for surfaces.
Definition: VTKsurfaceFormat.H:81
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:74
Foam::MeshedSurfaceProxy::surfFaces
const UList< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurfaceProxy.H:166
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
faceTraits.H
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:174