1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "STLsurfaceFormat.H"
29 #include "triPointRef.H"
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
37 const pointField& pointLst,
41 // calculate the normal ourselves, for flexibility and speed
42 vector norm = triPointRef
48 norm /= mag(norm) + VSMALL;
50 // simple triangulation about f[0].
51 // better triangulation should have been done before
52 const point& p0 = pointLst[f[0]];
53 for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
55 label fp2 = f.fcIndex(fp1);
57 const point& p1 = pointLst[f[fp1]];
58 const point& p2 = pointLst[f[fp2]];
61 os << " facet normal "
62 << norm.x() << ' ' << norm.y() << ' ' << norm.z() << nl
64 << " vertex " << p0.x() << ' ' << p0.y() << ' ' << p0.z() << nl
65 << " vertex " << p1.x() << ' ' << p1.y() << ' ' << p1.z() << nl
66 << " vertex " << p2.x() << ' ' << p2.y() << ' ' << p2.z() << nl
68 << " endfacet" << endl;
74 inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
77 const pointField& pointLst,
82 // calculate the normal ourselves, for flexibility and speed
83 vector norm = triPointRef
89 norm /= mag(norm) + VSMALL;
91 // simple triangulation about f[0].
92 // better triangulation should have been done before
93 const point& p0 = pointLst[f[0]];
94 for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
96 label fp2 = f.fcIndex(fp1);
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
115 Foam::fileFormats::STLsurfaceFormat<Face>::STLsurfaceFormat
117 const fileName& filename
124 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 bool Foam::fileFormats::STLsurfaceFormat<Face>::read
129 const fileName& filename
134 // read in the values
135 STLsurfaceFormatCore reader(filename);
138 this->storedPoints().transfer(reader.points());
140 // retrieve the original zone information
141 List<word> names(reader.names().xfer());
142 List<label> sizes(reader.sizes().xfer());
143 List<label> zoneIds(reader.zoneIds().xfer());
145 // generate the (sorted) faces
146 List<Face> faceLst(zoneIds.size());
150 // already sorted - generate directly
151 forAll(faceLst, faceI)
153 const label startPt = 3*faceI;
154 faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
159 // unsorted - determine the sorted order:
160 // avoid SortableList since we discard the main list anyhow
162 sortedOrder(zoneIds, faceMap);
164 // generate sorted faces
165 forAll(faceMap, faceI)
167 const label startPt = 3*faceMap[faceI];
168 faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
174 this->storedFaces().transfer(faceLst);
178 this->addZones(sizes, names);
182 this->addZones(sizes);
185 this->stitchFaces(SMALL);
192 void Foam::fileFormats::STLsurfaceFormat<Face>::writeAscii
194 const fileName& filename,
195 const MeshedSurfaceProxy<Face>& surf
198 OFstream os(filename);
203 "fileFormats::STLsurfaceFormat::writeAscii"
204 "(const fileName&, const MeshedSurfaceProxy<Face>&)"
206 << "Cannot open file for writing " << filename
210 const pointField& pointLst = surf.points();
211 const List<Face>& faceLst = surf.faces();
212 const List<label>& faceMap = surf.faceMap();
214 const List<surfZone>& zones =
216 surf.surfZones().size() > 1
221 const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
226 // Print all faces belonging to this zone
227 const surfZone& zone = zones[zoneI];
229 os << "solid " << zone.name() << nl;
233 forAll(zone, localFaceI)
235 const label faceI = faceMap[faceIndex++];
236 writeShell(os, pointLst, faceLst[faceI]);
241 forAll(zone, localFaceI)
243 writeShell(os, pointLst, faceLst[faceIndex++]);
246 os << "endsolid " << zone.name() << endl;
252 void Foam::fileFormats::STLsurfaceFormat<Face>::writeBinary
254 const fileName& filename,
255 const MeshedSurfaceProxy<Face>& surf
258 std::ofstream os(filename.c_str(), std::ios::binary);
263 "fileFormats::STLsurfaceFormat::writeBinary"
264 "(const fileName&, const MeshedSurfaceProxy<Face>&)"
266 << "Cannot open file for writing " << filename
271 const pointField& pointLst = surf.points();
272 const List<Face>& faceLst = surf.faces();
273 const List<label>& faceMap = surf.faceMap();
275 const List<surfZone>& zones =
277 surf.surfZones().size() > 1
282 const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
285 unsigned int nTris = 0;
286 if (MeshedSurface<Face>::isTri())
288 nTris = faceLst.size();
292 // count triangles for on-the-fly triangulation
293 forAll(faceLst, faceI)
295 nTris += faceLst[faceI].size() - 2;
299 // Write the STL header
300 STLsurfaceFormatCore::writeHeaderBINARY(os, nTris);
305 const surfZone& zone = zones[zoneI];
309 forAll(zone, localFaceI)
315 faceLst[faceMap[faceIndex++]],
322 forAll(zone, localFaceI)
328 faceLst[faceIndex++],
338 void Foam::fileFormats::STLsurfaceFormat<Face>::writeAscii
340 const fileName& filename,
341 const UnsortedMeshedSurface<Face>& surf
344 OFstream os(filename);
349 "fileFormats::STLsurfaceFormat::writeAscii"
350 "(const fileName&, const UnsortedMeshedSurface<Face>&)"
352 << "Cannot open file for writing " << filename
356 // a single zone - we can skip sorting
357 if (surf.zoneToc().size() == 1)
359 const pointField& pointLst = surf.points();
360 const List<Face>& faceLst = surf.faces();
362 os << "solid " << surf.zoneToc()[0].name() << endl;
363 forAll(faceLst, faceI)
365 writeShell(os, pointLst, faceLst[faceI]);
367 os << "endsolid " << surf.zoneToc()[0].name() << endl;
372 List<surfZone> zoneLst = surf.sortedZones(faceMap);
377 MeshedSurfaceProxy<Face>
390 void Foam::fileFormats::STLsurfaceFormat<Face>::writeBinary
392 const fileName& filename,
393 const UnsortedMeshedSurface<Face>& surf
396 std::ofstream os(filename.c_str(), std::ios::binary);
401 "fileFormats::STLsurfaceFormat::writeBinary"
402 "(const fileName&, const UnsortedMeshedSurface<Face>&)"
404 << "Cannot open file for writing " << filename
408 const pointField& pointLst = surf.points();
409 const List<Face>& faceLst = surf.faces();
410 const List<label>& zoneIds = surf.zoneIds();
412 unsigned int nTris = 0;
413 if (MeshedSurface<Face>::isTri())
415 nTris = faceLst.size();
419 // count triangles for on-the-fly triangulation
420 forAll(faceLst, faceI)
422 nTris += faceLst[faceI].size() - 2;
426 // Write the STL header
427 STLsurfaceFormatCore::writeHeaderBINARY(os, nTris);
429 // always write unsorted
430 forAll(faceLst, faceI)
444 void Foam::fileFormats::STLsurfaceFormat<Face>::write
446 const fileName& filename,
447 const MeshedSurfaceProxy<Face>& surf
450 const word ext = filename.ext();
452 // handle 'stlb' as binary directly
455 writeBinary(filename, surf);
459 writeAscii(filename, surf);
465 void Foam::fileFormats::STLsurfaceFormat<Face>::write
467 const fileName& filename,
468 const UnsortedMeshedSurface<Face>& surf
471 word ext = filename.ext();
473 // handle 'stlb' as binary directly
476 writeBinary(filename, surf);
480 writeAscii(filename, surf);
485 // ************************************************************************* //