writeSTL.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | foam-extend: Open Source CFD
4  \\ / O peration | Version: 3.2
5  \\ / A nd | Web: http://www.foam-extend.org
6  \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9  This file is part of foam-extend.
10 
11  foam-extend 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 3 of the License, or (at your
14  option) any later version.
15 
16  foam-extend is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "triSurface.H"
27 #include "STLtriangle.H"
28 #include "primitivePatch.H"
29 #include "HashTable.H"
30 #include "hashSignedLabel.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
38 
40 {
42 
44 
45  label faceIndex = 0;
46  forAll (myPatches, patchI)
47  {
48  // Print all faces belonging to this region
49  const surfacePatch& patch = myPatches[patchI];
50 
51  if (patch.size() == 0)
52  {
53  continue;
54  }
55 
56  os << "solid " << patch.name() << endl;
57 
58  for
59  (
60  label patchFaceI = 0;
61  patchFaceI < patch.size();
62  patchFaceI++
63  )
64  {
65  const label faceI = faceMap[faceIndex++];
66 
67  const vector& n = faceNormals()[faceI];
68 
69  os << " facet normal "
70  << n.x() << ' ' << n.y() << ' ' << n.z() << endl;
71  os << " outer loop" << endl;
72 
73  const labelledTri& f = (*this)[faceI];
74  const point& pa = points()[f[0]];
75  const point& pb = points()[f[1]];
76  const point& pc = points()[f[2]];
77 
78  os << " vertex "
79  << pa.x() << ' ' << pa.y() << ' ' << pa.z() << endl;
80  os << " vertex "
81  << pb.x() << ' ' << pb.y() << ' ' << pb.z() << endl;
82  os << " vertex "
83  << pc.x() << ' ' << pc.y() << ' ' << pc.z() << endl;
84  os
85  << " endloop" << endl;
86  os
87  << " endfacet" << endl;
88  }
89 
90  os << "endsolid " << patch.name() << endl;
91  }
92 }
93 
94 
95 void triSurface::writeSTLBINARY(std::ostream& os) const
96 {
97  // Write the STL header
98  string header("Foam binary STL", STLheaderSize);
99  os.write(header.c_str(), STLheaderSize);
100 
101  label nTris = size();
102  os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
103 
104  const vectorField& normals = faceNormals();
105 
106  forAll(*this, faceI)
107  {
108  const labelledTri& f = (*this)[faceI];
109 
110  // Convert vector into STL single precision
111  STLpoint n(normals[faceI]);
112  STLpoint pa(points()[f[0]]);
113  STLpoint pb(points()[f[1]]);
114  STLpoint pc(points()[f[2]]);
115 
116  STLtriangle stlTri(n, pa, pb, pc, f.region());
117 
118  stlTri.write(os);
119  }
120 }
121 
122 
123 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
124 
125 } // End namespace Foam
126 
127 // ************************************************************************* //
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:90
Foam::surfacePatch::size
label size() const
Return size of this patch in the polyMesh face list.
Definition: surfacePatch.H:114
Foam::PrimitivePatch< labelledTri, List, pointField, point >::points
const Field< point > & points() const
Return reference to global points.
Definition: PrimitivePatchTemplate.H:282
HashTable.H
Foam::triSurface::STLheaderSize
static const int STLheaderSize
The number of bytes in the STL header.
Definition: triSurface.H:79
hashSignedLabel.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::triSurface::writeSTLASCII
void writeSTLASCII(Ostream &) const
Write to Ostream in ASCII STL format.
Definition: writeSTL.C:39
STLtriangle.H
n
label n
Definition: TABSMDCalcMethod2.H:31
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:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::STLtriangle::write
void write(ostream &)
Write to istream (binary)
Definition: STLtriangleI.H:95
Foam::Vector::x
const Cmpt & x() const
Definition: VectorI.H:65
Foam::surfacePatch
'Patch' on surface as subset of triSurface.
Definition: surfacePatch.H:51
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
Foam::List< labelledTri >::size
label size() const
Return the number of elements in the UList.
Foam::triSurface::calcPatches
surfacePatchList calcPatches(labelList &faceMap) const
Sort faces according to region. Returns patch list.
Definition: triSurface.C:501
Foam::STLtriangle
A triangle representation for STL files.
Definition: STLtriangle.H:48
Foam::PrimitivePatch< labelledTri, List, pointField, point >::faceNormals
const Field< point > & faceNormals() const
Return face normals for patch.
Definition: PrimitivePatchTemplate.C:520
f
labelList f(nPoints)
Foam::STLpoint
A vertex point representation for STL files.
Definition: STLpoint.H:48
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
Foam::Vector::y
const Cmpt & y() const
Definition: VectorI.H:71
Foam::triSurface::writeSTLBINARY
void writeSTLBINARY(std::ostream &) const
Write to std::ostream in BINARY STL format.
Definition: writeSTL.C:95
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::geometricSurfacePatch::name
const word & name() const
Return name.
Definition: geometricSurfacePatch.H:101