STLAsciiParseI.H
Go to the documentation of this file.
1 /*--------------------------------*- C++ -*----------------------------------*\
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) 2018 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
29 
30 inline void Foam::Detail::STLAsciiParse::beginSolid(word solidName)
31 {
32  if (solidName.empty())
33  {
34  solidName = "solid"; // Could also use solid0, solid1, ...
35  }
36 
37  auto iter = nameLookup_.cfind(solidName);
38  if (iter.found())
39  {
40  if (groupId_ != iter.val())
41  {
42  sorted_ = false; // Group appeared out of order
43  groupId_ = iter.val();
44  }
45  }
46  else
47  {
48  groupId_ = sizes_.size();
49  if (nameLookup_.insert(solidName, groupId_))
50  {
51  names_.append(solidName);
52  sizes_.append(0);
53  }
54  else
55  {
56  FatalErrorInFunction<< "Duplicate solid-name: " << solidName
57  << exit(FatalError);
58  }
59  }
60 }
61 
62 
64 {
65  nFacetPoints_ = 0;
66  nVertexCmpt_ = 0;
67 }
68 
69 
71 {
72  nVertexCmpt_ = 0;
73 }
74 
75 
77 {
78  currVertex_[nVertexCmpt_] = val;
79 
80  if (++nVertexCmpt_ == 3)
81  {
82  points_.append(currVertex_);
83  nVertexCmpt_ = 0;
84  ++nFacetPoints_;
85  }
86 
87  return !nVertexCmpt_;
88 }
89 
90 
91 inline bool Foam::Detail::STLAsciiParse::addVertexComponent(const char* text)
92 {
93  //-> safer, but slower: readFloat(text, currVertex_[nVertexCmpt_]);
94  currVertex_[nVertexCmpt_] = ::atof(text);
95 
96  if (++nVertexCmpt_ == 3)
97  {
98  points_.append(currVertex_);
99  nVertexCmpt_ = 0;
100  ++nFacetPoints_;
101  }
103  return !nVertexCmpt_;
104 }
105 
106 
108 {
109  if (nFacetPoints_ == 3)
110  {
111  facets_.append(groupId_);
112  sizes_[groupId_]++;
113  }
114  else
115  {
116  if (nFacetPoints_ > 3)
117  {
118  nFacetPoints_ -= 3;
119  }
120 
121  if (nFacetPoints_)
122  {
123  points_.resize(points_.size() - nFacetPoints_);
124  }
125  }
126 
127  nFacetPoints_ = 0;
128 }
129 
130 
131 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
132 
133 inline Foam::Detail::STLAsciiParse::STLAsciiParse(const label approxNpoints)
134 :
135  sorted_(true),
136  groupId_(-1),
137  lineNum_(1),
138  nFacetPoints_(0),
139  nVertexCmpt_(0),
140  points_(approxNpoints),
141  facets_(approxNpoints/2)
142 {}
143 
144 
145 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
146 
148 {
149  sorted_ = true;
150  groupId_ = -1;
151  lineNum_ = 0;
152 
153  nFacetPoints_ = 0;
154  nVertexCmpt_ = 0;
155 
156  points_.clear();
157  facets_.clear();
158  names_.clear();
159  sizes_.clear();
160  nameLookup_.clear();
161 }
162 
163 
164 inline bool Foam::Detail::STLAsciiParse::sorted() const
165 {
166  return sorted_;
167 }
168 
169 
171 {
172  return points_;
173 }
174 
175 
177 {
178  return facets_;
179 }
180 
181 
183 {
184  return names_;
185 }
186 
187 
189 {
190  return sizes_;
191 }
192 
193 
194 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:118
DynamicList< label > & facets()
A list of facet IDs (group IDs) corresponds to the number of triangles.
void beginSolid(word solidName)
Action when entering &#39;solid&#39;.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
DynamicList< word > names_
Definition: STLAsciiParse.H:77
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
void beginFacet()
Action when entering &#39;facet&#39;.
STLAsciiParse(const STLAsciiParse &)=delete
No copy construct.
bool addVertexComponent(float val)
Add next vertex component. On each third call, adds the point.
void clear()
Reset stored values.
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:173
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:134
A class for handling words, derived from Foam::string.
Definition: word.H:63
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicList.H:558
DynamicList< word > & names()
Solid names in the order of their appearance.
DynamicList< label > sizes_
Definition: STLAsciiParse.H:78
DynamicList< label > & sizes()
Solid sizes in the order of their appearance.
void endFacet()
Action on &#39;endfacet&#39;.
void resetVertex()
Reset vertex component to zero.
DynamicList< STLpoint > & points()
A list of unstitched triangle points.
bool sorted() const
Do all the solid groups appear in order?
HashTable< label > nameLookup_
Definition: STLAsciiParse.H:79