STLsurfaceFormat.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-2016 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 "STLsurfaceFormat.H"
27 #include "ListOps.H"
28 #include "triPointRef.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class Face>
34 (
35  Ostream& os,
36  const pointField& pointLst,
37  const Face& f
38 )
39 {
40  // calculate the normal ourselves, for flexibility and speed
41  vector norm = triPointRef
42  (
43  pointLst[f[0]],
44  pointLst[f[1]],
45  pointLst[f[2]]
46  ).normal();
47  norm /= mag(norm) + VSMALL;
48 
49  // simple triangulation about f[0].
50  // better triangulation should have been done before
51  const point& p0 = pointLst[f[0]];
52  for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
53  {
54  label fp2 = f.fcIndex(fp1);
55 
56  const point& p1 = pointLst[f[fp1]];
57  const point& p2 = pointLst[f[fp2]];
58 
59  // write STL triangle
60  os << " facet normal "
61  << norm.x() << ' ' << norm.y() << ' ' << norm.z() << nl
62  << " outer loop\n"
63  << " vertex " << p0.x() << ' ' << p0.y() << ' ' << p0.z() << nl
64  << " vertex " << p1.x() << ' ' << p1.y() << ' ' << p1.z() << nl
65  << " vertex " << p2.x() << ' ' << p2.y() << ' ' << p2.z() << nl
66  << " endloop\n"
67  << " endfacet" << endl;
68  }
69 }
70 
71 
72 template<class Face>
74 (
75  ostream& os,
76  const pointField& pointLst,
77  const Face& f,
78  const label zoneI
79 )
80 {
81  // calculate the normal ourselves, for flexibility and speed
82  vector norm = triPointRef
83  (
84  pointLst[f[0]],
85  pointLst[f[1]],
86  pointLst[f[2]]
87  ).normal();
88  norm /= mag(norm) + VSMALL;
89 
90  // simple triangulation about f[0].
91  // better triangulation should have been done before
92  const point& p0 = pointLst[f[0]];
93  for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
94  {
95  label fp2 = f.fcIndex(fp1);
96 
97  STLtriangle stlTri
98  (
99  norm,
100  p0,
101  pointLst[f[fp1]],
102  pointLst[f[fp2]],
103  zoneI
104  );
105 
106  stlTri.write(os);
107  }
108 }
109 
110 
111 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
112 
113 template<class Face>
115 (
116  const fileName& filename
117 )
118 {
119  read(filename);
120 }
121 
122 
123 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
124 
125 template<class Face>
127 (
128  const fileName& filename
129 )
130 {
131  this->clear();
132 
133  // read in the values
134  STLsurfaceFormatCore reader(filename);
135 
136  // transfer points
137  this->storedPoints().transfer(reader.points());
138 
139  // retrieve the original zone information
140  List<word> names(reader.names().xfer());
141  List<label> sizes(reader.sizes().xfer());
142  List<label> zoneIds(reader.zoneIds().xfer());
143 
144  // generate the (sorted) faces
145  List<Face> faceLst(zoneIds.size());
146 
147  if (reader.sorted())
148  {
149  // already sorted - generate directly
150  forAll(faceLst, facei)
151  {
152  const label startPt = 3*facei;
153  faceLst[facei] = triFace(startPt, startPt+1, startPt+2);
154  }
155  }
156  else
157  {
158  // unsorted - determine the sorted order:
159  // avoid SortableList since we discard the main list anyhow
161  sortedOrder(zoneIds, faceMap);
162 
163  // generate sorted faces
164  forAll(faceMap, facei)
165  {
166  const label startPt = 3*faceMap[facei];
167  faceLst[facei] = triFace(startPt, startPt+1, startPt+2);
168  }
169  }
170  zoneIds.clear();
171 
172  // transfer:
173  this->storedFaces().transfer(faceLst);
174 
175  if (names.size())
176  {
177  this->addZones(sizes, names);
178  }
179  else
180  {
181  this->addZones(sizes);
182  }
183 
184  this->stitchFaces(SMALL);
185  return true;
186 }
187 
188 
189 
190 template<class Face>
192 (
193  const fileName& filename,
194  const MeshedSurfaceProxy<Face>& surf
195 )
196 {
197  OFstream os(filename);
198  if (!os.good())
199  {
201  << "Cannot open file for writing " << filename
202  << exit(FatalError);
203  }
204 
205  const pointField& pointLst = surf.points();
206  const List<Face>& faceLst = surf.faces();
207  const List<label>& faceMap = surf.faceMap();
208 
209  const List<surfZone>& zones =
210  (
211  surf.surfZones().empty()
212  ? surfaceFormatsCore::oneZone(faceLst)
213  : surf.surfZones()
214  );
215 
216  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
217 
218  label faceIndex = 0;
219  forAll(zones, zoneI)
220  {
221  // Print all faces belonging to this zone
222  const surfZone& zone = zones[zoneI];
223 
224  os << "solid " << zone.name() << nl;
225 
226  if (useFaceMap)
227  {
228  forAll(zone, localFacei)
229  {
230  const label facei = faceMap[faceIndex++];
231  writeShell(os, pointLst, faceLst[facei]);
232  }
233  }
234  else
235  {
236  forAll(zone, localFacei)
237  {
238  writeShell(os, pointLst, faceLst[faceIndex++]);
239  }
240  }
241  os << "endsolid " << zone.name() << endl;
242  }
243 }
244 
245 
246 template<class Face>
248 (
249  const fileName& filename,
250  const MeshedSurfaceProxy<Face>& surf
251 )
252 {
253  std::ofstream os(filename.c_str(), std::ios::binary);
254  if (!os.good())
255  {
257  << "Cannot open file for writing " << filename
258  << exit(FatalError);
259  }
260 
261 
262  const pointField& pointLst = surf.points();
263  const List<Face>& faceLst = surf.faces();
264  const List<label>& faceMap = surf.faceMap();
265 
266  const List<surfZone>& zones =
267  (
268  surf.surfZones().size() > 1
269  ? surf.surfZones()
270  : surfaceFormatsCore::oneZone(faceLst)
271  );
272 
273  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
274 
275 
276  unsigned int nTris = 0;
278  {
279  nTris = faceLst.size();
280  }
281  else
282  {
283  // count triangles for on-the-fly triangulation
284  forAll(faceLst, facei)
285  {
286  nTris += faceLst[facei].size() - 2;
287  }
288  }
289 
290  // Write the STL header
291  STLsurfaceFormatCore::writeHeaderBINARY(os, nTris);
292 
293  label faceIndex = 0;
294  forAll(zones, zoneI)
295  {
296  const surfZone& zone = zones[zoneI];
297 
298  if (useFaceMap)
299  {
300  forAll(zone, localFacei)
301  {
302  writeShell
303  (
304  os,
305  pointLst,
306  faceLst[faceMap[faceIndex++]],
307  zoneI
308  );
309  }
310  }
311  else
312  {
313  forAll(zone, localFacei)
314  {
315  writeShell
316  (
317  os,
318  pointLst,
319  faceLst[faceIndex++],
320  zoneI
321  );
322  }
323  }
324  }
325 }
326 
327 
328 template<class Face>
330 (
331  const fileName& filename,
332  const UnsortedMeshedSurface<Face>& surf
333 )
334 {
335  OFstream os(filename);
336  if (!os.good())
337  {
339  << "Cannot open file for writing " << filename
340  << exit(FatalError);
341  }
342 
343  // a single zone - we can skip sorting
344  if (surf.zoneToc().size() == 1)
345  {
346  const pointField& pointLst = surf.points();
347  const List<Face>& faceLst = surf.faces();
348 
349  os << "solid " << surf.zoneToc()[0].name() << endl;
350  forAll(faceLst, facei)
351  {
352  writeShell(os, pointLst, faceLst[facei]);
353  }
354  os << "endsolid " << surf.zoneToc()[0].name() << endl;
355  }
356  else
357  {
359  List<surfZone> zoneLst = surf.sortedZones(faceMap);
360 
361  writeAscii
362  (
363  filename,
365  (
366  surf.points(),
367  surf.faces(),
368  zoneLst,
369  faceMap
370  )
371  );
372  }
373 }
374 
375 
376 template<class Face>
378 (
379  const fileName& filename,
380  const UnsortedMeshedSurface<Face>& surf
381 )
382 {
383  std::ofstream os(filename.c_str(), std::ios::binary);
384  if (!os.good())
385  {
387  << "Cannot open file for writing " << filename
388  << exit(FatalError);
389  }
390 
391  const pointField& pointLst = surf.points();
392  const List<Face>& faceLst = surf.faces();
393  const List<label>& zoneIds = surf.zoneIds();
394 
395  unsigned int nTris = 0;
397  {
398  nTris = faceLst.size();
399  }
400  else
401  {
402  // count triangles for on-the-fly triangulation
403  forAll(faceLst, facei)
404  {
405  nTris += faceLst[facei].size() - 2;
406  }
407  }
408 
409  // Write the STL header
410  STLsurfaceFormatCore::writeHeaderBINARY(os, nTris);
411 
412  // always write unsorted
413  forAll(faceLst, facei)
414  {
415  writeShell
416  (
417  os,
418  pointLst,
419  faceLst[facei],
420  zoneIds[facei]
421  );
422  }
423 }
424 
425 
426 template<class Face>
428 (
429  const fileName& filename,
430  const MeshedSurfaceProxy<Face>& surf
431 )
432 {
433  const word ext = filename.ext();
434 
435  // handle 'stlb' as binary directly
436  if (ext == "stlb")
437  {
438  writeBinary(filename, surf);
439  }
440  else
441  {
442  writeAscii(filename, surf);
443  }
444 }
445 
446 
447 template<class Face>
449 (
450  const fileName& filename,
451  const UnsortedMeshedSurface<Face>& surf
452 )
453 {
454  word ext = filename.ext();
455 
456  // handle 'stlb' as binary directly
457  if (ext == "stlb")
458  {
459  writeBinary(filename, surf);
460  }
461  else
462  {
463  writeAscii(filename, surf);
464  }
465 }
466 
467 
468 // ************************************************************************* //
A surface geometry mesh, in which the surface zone information is conveyed by the &#39;zoneId&#39; associated...
Definition: MeshedSurface.H:74
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const Cmpt & x() const
Definition: VectorI.H:75
const word & name() const
Return name.
error FatalError
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
bool sorted() const
File read was already sorted.
Provide a means of reading/writing STL files (ASCII and binary).
Xfer< List< T > > xfer()
Transfer contents to the Xfer container.
Definition: ListI.H:90
tUEqn clear()
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:72
Output to file stream.
Definition: OFstream.H:81
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
const List< Face > & faces() const
Return const access to the faces.
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const Field< point > & points() const
Return reference to global points.
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
List< label > & sizes()
The list of solid sizes in the order of their first appearance.
const List< Face > & faces() const
Return const access to the faces.
Internal class used by the STLsurfaceFormat.
const List< surfZone > & surfZones() const
Const access to the surface zones.
List< word > & names()
The list of solid names in the order of their first appearance.
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Various functions to operate on Lists.
const pointField & points() const
Return const access to the points.
static void writeAscii(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy (as ASCII)
bool read(const char *, int32_t &)
Definition: int32IO.C:85
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
bool useFaceMap() const
Use faceMap?
pointField & points()
Return full access to the points.
Base class for zones.
Definition: zone.H:57
A class for handling words, derived from string.
Definition: word.H:59
face triFace(3)
static const char nl
Definition: Ostream.H:262
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
virtual bool read(const fileName &)
Read from file.
List< label > & zoneIds()
Return full access to the zoneIds.
static void writeBinary(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy (as BINARY)
const List< surfZoneIdentifier > & zoneToc() const
Return const access to the zone table-of-contents.
vector point
Point is a vector.
Definition: point.H:41
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:283
triangle< point, const point & > triPointRef
Definition: triPointRef.H:44
dimensioned< scalar > mag(const dimensioned< Type > &)
const List< label > & zoneIds() const
Return const access to the zone ids.
surfZoneList sortedZones(labelList &faceMap) const
Sort faces according to zoneIds.