stlFile.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #include "stlFile.hpp"
23 #include "iFstream.hpp"
24 #include "oFstream.hpp"
25 #include "error.hpp"
26 
27 namespace pFlow
28 {
29 inline bool badInput(iIstream& is, token& tok )
30 {
31  return is.bad() || !tok.good();
32 }
33 
34 inline bool checkWordToken(iIstream& is, token& tok, const word& check)
35 {
36 
37  if( badInput(is, tok) || is.eof() || !tok.isWord() || tok.wordToken() != check ) return false;
38  return true;
39 
40 }
41 
42 inline bool checkNumberToken(iIstream& is, token& tok, real& val)
43 {
44  if(badInput(is, tok) || is.eof() || !tok.isNumber() )return false;
45  val = tok.number();
46  return true;
47 }
48 
49 }
50 
52 (
53  iIstream& is,
54  realx3x3Vector & vertecies,
55  word & name
56 )
57 {
58 
59  token tok;
60  is>> tok;
61  if(!checkWordToken(is, tok, "solid")) return false;
62 
63  // check if there is a name associated with solid
64  name = "";
65 
66 
67  int32 nWords =0;
68  bool reachedFacet = false;
69  is >> tok;
70 
71  while (nWords < 20 )
72  {
73  if( badInput(is, tok) ) return false;
74  //if(!tok.isWord()) return false;
75  nWords++;
76  if(tok.isWord() && tok.wordToken() != "facet" )
77  {
78  name += tok.wordToken();
79  }
80  else if( tok.isNumber())
81  {
82  auto val = tok.number();
83  name += real2Word(val);
84  }
85  else if( tok.isPunctuation())
86  {
87  name += tok.pToken();
88  }
89  else if (tok.isWord() && tok.wordToken() == "facet")
90  {
91  is.putBack(tok);
92  reachedFacet = true;
93  break;
94  }
95  else
96  {
97  return false;
98  }
99  is >> tok;
100  }
101 
102  if(!reachedFacet) return false;
103 
104  vertecies.clear();
105  while(true )
106  {
107  is>>tok;
108  if( badInput(is,tok) || !tok.isWord() )return false;
109  word wTok = tok.wordToken();
110  if( wTok == "endsolid" ) return true; // end of solid
111  if( wTok != "facet" ) return false;
112 
113  // read facet
114  is.putBack(tok);
115  realx3x3 tri;
116  if( !readFacet(is, tri) ) return false;
117 
118  vertecies.push_back(tri);
119 
120  }
121 
122  return true;
123 }
124 
125 
127 (
128  iIstream& is,
129  realx3x3& tri
130 )
131 {
132  token tok;
133 
134  is>>tok;
135  if( !checkWordToken(is, tok, "facet") ) return false;
136 
137  is >> tok;
138  if( !checkWordToken(is, tok , "normal") ) return false;
139 
140  real val;
141  for(uint32 i=0; i<3; i++ )
142  {
143  is>>tok;
144  if( !checkNumberToken(is, tok, val))return false;
145  }
146 
147  is>> tok;
148  if( !checkWordToken(is, tok, "outer")) return false;
149 
150  is>> tok;
151  if(!checkWordToken(is, tok, "loop")) return false;
152 
153  realx3 v;
154 
155  for(uint32 i=0; i<3; i++)
156  {
157  is>>tok;
158  if(!checkWordToken(is, tok, "vertex")) return false;
159  is>>tok;
160  if(!checkNumberToken(is, tok, v.x()))return false;
161  is>>tok;
162  if(!checkNumberToken(is, tok, v.y()))return false;
163  is>>tok;
164  if(!checkNumberToken(is, tok, v.z()))return false;
165  if( i==0 ) tri.x() = v;
166  if( i==1 ) tri.y() = v;
167  if( i==2) tri.z() = v;
168  }
169  is>> tok;
170  if(!checkWordToken(is, tok, "endloop")) return false;
171  is>> tok;
172  if(!checkWordToken(is, tok, "endfacet")) return false;
173 
174  return true;
175 }
176 
178 (
179  iOstream& os,
180  const realx3x3& tri
181 )const
182 {
183  realx3 n = cross( tri.y() - tri.x(), tri.z()-tri.x());
184  n.normalize();
185  os.incrIndent();
186  indent(os) << "facet" << spaceToken()
187  << "normal"<<spaceToken()
188  << n.x() << spaceToken()
189  << n.y() << spaceToken()
190  << n.z() << endl;
191  os.incrIndent();
192  indent(os) << "outer loop"<<endl;
193  os.incrIndent();
194  indent(os) << "vertex"<< spaceToken()
195  << tri.x().x() << spaceToken()
196  << tri.x().y() << spaceToken()
197  << tri.x().z() << endl;
198 
199  indent(os) << "vertex"<< spaceToken()
200  << tri.y().x() << spaceToken()
201  << tri.y().y() << spaceToken()
202  << tri.y().z() << endl;
203 
204  indent(os) << "vertex"<< spaceToken()
205  << tri.z().x() << spaceToken()
206  << tri.z().y() << spaceToken()
207  << tri.z().z() << endl;
208  os.decrIndent();
209  indent(os) << "endloop"<<endl;
210 
211  os.decrIndent();
212  indent(os)<< "endfacet"<<endl;
213 
214  os.decrIndent();
215 
216  return true;
217 
218 }
219 
220 
222 (
223  iOstream& os,
224  const realx3x3Vector& vertecies,
225  const word& name
226 )const
227 {
228  os<< "solid"<<spaceToken()<<name<<endl;
229  for(const auto& tri: vertecies)
230  {
231  writeFacet(os, tri);
232  }
233  os<< "endsolid"<<endl;
234 
235  return true;
236 }
237 
239 :
240  file_(file)
241 {
242 }
243 
245 (
246  fileSystem file,
247  const word& name,
248  const realx3x3Vector& vertecies
249 )
250 :
251  stlFile(file)
252 {
253  addSolid(name, vertecies);
254 }
255 
256 
258 (
259  fileSystem file,
260  const word& name,
261  realx3x3Vector&& vertecies
262 )
263 :
264  stlFile(file)
265 {
266  addSolid(name, vertecies);
267 }
268 
270 (
271  const word& name,
272  const realx3x3Vector& vertecies
273 )
274 {
275  solids_.push_backSafe(vertecies);
276  solidNames_.push_back(name);
277 }
278 
279 
281 (
282  const word& name,
283  realx3x3Vector&& vertecies
284 )
285 {
286  solids_.push_backSafe(std::move(vertecies));
287  solidNames_.push_back(name);
288 }
289 
290 
291 
293 {
294  solids_.clear();
295  solidNames_.clear();
296 
297  // open file
298  iFstream is(file_);
299 
300  token tok;
301  while (true)
302  {
303 
304  realx3x3Vector vertecies;
305  word name;
306  if(!readSolid(is, vertecies, name))
307  {
308  ioErrorInFile(is.name(), is.lineNumber());
309  return false;
310  }
311 
312  addSolid(name, std::move(vertecies));
313 
314  is >> tok;
315  if( is.eof() || !tok.good())return true;
316  is.putBack(tok);
317 
318  }
319 
320  return true;
321 }
322 
323 
325 {
326  oFstream os(file_);
327  os.precision(8);
328  for(label i=0; i<size(); i++)
329  {
330  writeSolid(os, solids_[i], solidNames_[i]);
331  }
332 
333  return true;
334 }
335 
337 (
338  fileSystem file
339 ) const
340 {
341  file_ = file;
342 }
343 
345 {
346  return solidNames_;
347 }
348 
349 size_t pFlow::stlFile::size()const
350 {
351  return solids_.size();
352 }
353 
355 (
356  label i
357 )const
358 {
359  if(i >= size() )
360  {
362  "requested out of range solid from stlFile "<<
363  file_<<endl;
364  fatalExit;
365  }
366 
367  return solids_[i];
368 }
369 
371 (
372  label i
373 )const
374 {
375  if(i >= size() )
376  {
378  "requested out of range solid name from stlFile "<<
379  file_<<endl;
380  fatalExit;
381  }
382 
383  return solidNames_[i];
384 }
pFlow::IOstream::eof
bool eof() const
Definition: IOstream.hpp:156
pFlow::List< word >
pFlow::checkWordToken
bool checkWordToken(iIstream &is, token &tok, const word &check)
Definition: stlFile.cpp:34
pFlow::real
float real
Definition: builtinTypes.hpp:46
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::token
Definition: token.hpp:42
pFlow::stlFile::stlFile
stlFile(fileSystem file)
Definition: stlFile.cpp:238
pFlow::token::good
bool good() const
Definition: tokenI.hpp:372
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::stlFile::writeFacet
bool writeFacet(iOstream &os, const realx3x3 &tri) const
Definition: stlFile.cpp:178
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::token::number
real number() const
Definition: tokenI.hpp:568
pFlow::stlFile
Definition: stlFile.hpp:38
pFlow::Istream::name
virtual const word & name() const
Definition: Istream.hpp:78
pFlow::stlFile::writeSolid
bool writeSolid(iOstream &os, const realx3x3Vector &vertecies, const word &name) const
Definition: stlFile.cpp:222
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
oFstream.hpp
pFlow::checkNumberToken
bool checkNumberToken(iIstream &is, token &tok, real &val)
Definition: stlFile.cpp:42
pFlow::indent
iOstream & indent(iOstream &os)
Definition: iOstream.hpp:282
pFlow::iFstream
Definition: iFstream.hpp:35
pFlow::stlFile::solid
const realx3x3Vector & solid(label i) const
Definition: stlFile.cpp:355
pFlow::triple::y
INLINE_FUNCTION_HD T & y()
Definition: triple.hpp:141
pFlow
Definition: demComponent.hpp:28
pFlow::stlFile::size
size_t size() const
Definition: stlFile.cpp:349
pFlow::fileSystem
Definition: fileSystem.hpp:63
stlFile.hpp
pFlow::iOstream::incrIndent
void incrIndent()
Definition: iOstream.hpp:161
cross
INLINE_FUNCTION_HD triple< T > cross(const triple< T > &v1, const triple< T > &v2)
pFlow::Ostream::precision
virtual int precision() const
Definition: Ostream.cpp:295
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::IOstream::bad
bool bad() const
Definition: IOstream.hpp:168
pFlow::iOstream::decrIndent
void decrIndent()
Definition: iOstream.cpp:27
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::stlFile::addSolid
void addSolid(const word &name, const realx3x3Vector &vertecies)
Definition: stlFile.cpp:270
pFlow::iIstream::putBack
void putBack(const token &tok)
Definition: iIstream.cpp:5
pFlow::stlFile::readSolid
bool readSolid(iIstream &is, realx3x3Vector &vertecies, word &name)
Definition: stlFile.cpp:52
pFlow::spaceToken
token spaceToken()
Definition: token.hpp:519
pFlow::triple::z
INLINE_FUNCTION_HD T & z()
Definition: triple.hpp:144
pFlow::oFstream
Definition: oFstream.hpp:36
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::stlFile::names
const wordList & names() const
Definition: stlFile.cpp:344
pFlow::stlFile::name
const word & name(label i) const
Definition: stlFile.cpp:371
pFlow::stlFile::setFile
void setFile(fileSystem file) const
Definition: stlFile.cpp:337
pFlow::stlFile::readFacet
bool readFacet(iIstream &is, realx3x3 &tri)
Definition: stlFile.cpp:127
pFlow::real2Word
word real2Word(const real &v, int32 numPrecision=6)
Definition: bTypesFunctions.cpp:52
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::triple::x
INLINE_FUNCTION_HD T & x()
Definition: triple.hpp:138
pFlow::stlFile::read
bool read()
Definition: stlFile.cpp:292
pFlow::triple
Definition: triple.hpp:37
pFlow::token::isNumber
bool isNumber() const
Definition: tokenI.hpp:562
iFstream.hpp
pFlow::Vector
Definition: Vector.hpp:46
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
pFlow::stlFile::write
bool write() const
Definition: stlFile.cpp:324
pFlow::token::isWord
bool isWord() const
Definition: tokenI.hpp:584
pFlow::badInput
bool badInput(iIstream &is, token &tok)
Definition: stlFile.cpp:29
error.hpp