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-2020 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  // Clear everything
93  this->clear();
94 
95  IFstream is(filename);
96  if (!is.good())
97  {
99  << "Cannot read file " << filename << nl
100  << exit(FatalError);
101  }
102 
103  // Assume groups are not intermixed
104  bool sorted = true;
105 
106 
107  // Use dummy Time for objectRegistry
108  autoPtr<Time> dummyTimePtr(Time::New());
109 
110  objectRegistry obr
111  (
112  IOobject
113  (
114  "vtk::surfaceFormat",
115  *dummyTimePtr,
116  IOobject::NO_READ,
117  IOobject::NO_WRITE,
118  false
119  )
120  );
121 
122  // Read all
123  vtkUnstructuredReader reader(obr, is);
124  const faceList& faces = reader.faces();
125 
126  // Assume all faces in zone0 unless a region field is present
127  labelList zones(faces.size(), Zero);
128 
129  for (auto fieldName : { "region", "STLSolidLabeling" })
130  {
131  const labelIOField* lptr =
132  reader.cellData().findObject<labelIOField>(fieldName);
133 
134  if (lptr)
135  {
136  label i = 0;
137  for (const auto& region : *lptr)
138  {
139  zones[i++] = label(region);
140  }
141  break;
142  }
143 
144  const scalarIOField* sptr =
145  reader.cellData().findObject<scalarIOField>(fieldName);
146 
147  if (sptr)
148  {
149  label i = 0;
150  for (const auto& region : *sptr)
151  {
152  zones[i++] = label(region);
153  }
154  break;
155  }
156  }
157 
158 
159  // Create zone names
160  const label nZones = max(zones)+1;
161  wordList zoneNames(nZones);
162  forAll(zoneNames, i)
163  {
164  zoneNames[i] = surfZone::defaultName(i);
165  }
166 
167 
168  // Check if it needs triangulation
169  label nTri = 0;
171  {
172  for (const face& f : faces)
173  {
174  nTri += f.nTriangles();
175  }
176  }
177 
178  DynamicList<label> dynElemId; // unused
179 
180  if (nTri > faces.size())
181  {
182  // We are here if the target surface needs triangles and
183  // the source surface has non-triangles
184 
185  DynamicList<Face> dynFaces(nTri);
186  DynamicList<label> dynZones(nTri);
187 
188  forAll(faces, facei)
189  {
190  const face& f = faces[facei];
191  for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
192  {
193  const label fp2 = f.fcIndex(fp1);
194 
195  dynFaces.append(Face{f[0], f[fp1], f[fp2]});
196  dynZones.append(zones[facei]);
197  }
198  }
199  zones.clear();
200 
201  // Count
202  labelList zoneSizes(nZones, Zero);
203  for (const label zonei : dynZones)
204  {
205  zoneSizes[zonei]++;
206  }
207 
208  this->sortFacesAndStore(dynFaces, dynZones, dynElemId, sorted);
209 
210  // Add zones (retaining empty ones)
211  this->addZones(zoneSizes, zoneNames);
212  }
213  else
214  {
215  DynamicList<Face> dynFaces(faces.size());
216  DynamicList<label> dynZones(std::move(zones));
217 
218  for (const face& f : faces)
219  {
220  dynFaces.append(Face(f));
221  }
222 
223  // Count
224  labelList zoneSizes(nZones, Zero);
225  for (const label zonei : zones)
226  {
227  zoneSizes[zonei]++;
228  }
229 
230  this->sortFacesAndStore(dynFaces, dynZones, dynElemId, sorted);
231 
232  // Add zones (retaining empty ones)
233  this->addZones(zoneSizes, zoneNames);
234  }
235  this->addZonesToFaces(); // for labelledTri
236 
237  // transfer to normal lists
238  this->storedPoints().transfer(reader.points());
239 
240  return true;
241 }
242 
243 
244 template<class Face>
246 (
247  const fileName& filename,
248  const MeshedSurfaceProxy<Face>& surf,
250  const dictionary& options
251 )
252 {
253  const UList<point>& pointLst = surf.points();
254  const UList<Face>& faceLst = surf.surfFaces();
255  const UList<label>& faceMap = surf.faceMap();
256 
257  const surfZoneList zones =
258  (
259  surf.surfZones().empty()
260  ? surfaceFormatsCore::oneZone(faceLst)
261  : surf.surfZones()
262  );
263 
264  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
265 
266  vtk::outputOptions opts = formatOptions(options);
267 
268  std::ofstream os(filename, std::ios::binary);
269 
271 
272  writeHeader(format(), pointLst);
273 
274  if (useFaceMap)
275  {
276  // connectivity count without additional storage (done internally)
277  label nConnectivity = 0;
278  for (const Face& f : faceLst)
279  {
280  nConnectivity += f.size();
281  }
282 
284  (
285  format().os(),
286  faceLst.size(),
287  nConnectivity
288  );
289 
290  label faceIndex = 0;
291  for (const surfZone& zone : zones)
292  {
293  forAll(zone, i)
294  {
295  const Face& f = faceLst[faceMap[faceIndex++]];
296 
297  format().write(f.size()); // The size prefix
298  vtk::writeList(format(), f);
299  }
300  }
301 
302  format().flush();
303  }
304  else
305  {
306  // Easy to write polys without a faceMap
307  writePolys(format(), faceLst);
308  }
309 
310  // Write regions (zones) as CellData
311  if (zones.size() > 1)
312  {
313  writeCellData(format(), zones);
314  }
315 }
316 
317 
318 template<class Face>
320 (
321  const fileName& filename,
322  const UnsortedMeshedSurface<Face>& surf,
324  const dictionary& options
325 )
326 {
327  vtk::outputOptions opts = formatOptions(options);
328 
329  std::ofstream os(filename, std::ios::binary);
330 
332 
333  writeHeader(format(), surf.points());
334 
335  // Easy to write polys without a faceMap
336  writePolys(format(), surf.surfFaces());
337 
338  // Write regions (zones) as CellData
339  writeCellData(format(), surf.zoneIds());
340 }
341 
342 
343 // ************************************************************************* //
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::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeTopological.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
Can/should use faceMap?
Definition: MeshedSurfaceProxy.H:203
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::DynamicList< label >
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:171
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:82
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:191
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
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:336
format
word format(conversionProperties.get< word >("format"))
Foam::faceTraits
Traits class for faces.
Definition: faceTraits.H:50
Foam::UnsortedMeshedSurface
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
Definition: MeshedSurface.H:83
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:474
Foam::fileFormats::VTKsurfaceFormat::read
virtual bool read(const fileName &filename)
Read from file.
Definition: VTKsurfaceFormat.C:88
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
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::defaultName
static const word defaultName("coeffs")
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:381
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()
Foam::nl
constexpr char nl
Definition: Ostream.H:385
f
labelList f(nPoints)
Foam::List< face >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:56
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:83
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
Foam::MeshedSurfaceProxy::surfFaces
const UList< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurfaceProxy.H:177
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
faceTraits.H
Foam::fileFormats::VTKsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, IOstreamOption=IOstreamOption(), const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: VTKsurfaceFormat.C:246
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:185