# PhasicFlow: src/phasicFlow/structuredData/trisurfaceStructure/stlFile.cpp Source File

stlFile.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#include "stlFile.hpp"
#include "iFstream.hpp"
#include "oFstream.hpp"
#include "error.hpp"
namespace pFlow
{
inline bool badInput(iIstream& is, token& tok )
{
return is.bad() || !tok.good();
}
inline bool checkWordToken(iIstream& is, token& tok, const word& check)
{
if( badInput(is, tok) || is.eof() || !tok.isWord() || tok.wordToken() != check ) return false;
return true;
}
inline bool checkNumberToken(iIstream& is, token& tok, real& val)
{
if(badInput(is, tok) || is.eof() || !tok.isNumber() )return false;
val = tok.number();
return true;
}
}
bool pFlow::stlFile::readSolid
(
iIstream& is,
realx3x3Vector & vertecies,
word & name
)
{
token tok;
is>> tok;
if(!checkWordToken(is, tok, "solid")) return false;
// check if there is a name associated with solid
name = "";
int32 nWords =0;
bool reachedFacet = false;
is >> tok;
while (nWords < 20 )
{
if( badInput(is, tok) ) return false;
//if(!tok.isWord()) return false;
nWords++;
if(tok.isWord() && tok.wordToken() != "facet" )
{
name += tok.wordToken();
}
else if( tok.isNumber())
{
auto val = tok.number();
name += real2Word(val);
}
else if( tok.isPunctuation())
{
name += tok.pToken();
}
else if (tok.isWord() && tok.wordToken() == "facet")
{
is.putBack(tok);
reachedFacet = true;
break;
}
else
{
return false;
}
is >> tok;
}
if(!reachedFacet) return false;
vertecies.clear();
while(true )
{
is>>tok;
if( badInput(is,tok) || !tok.isWord() )return false;
word wTok = tok.wordToken();
if( wTok == "endsolid" ) return true; // end of solid
if( wTok != "facet" ) return false;
// read facet
is.putBack(tok);
realx3x3 tri;
if( !readFacet(is, tri) ) return false;
vertecies.push_back(tri);
}
return true;
}
bool pFlow::stlFile::readFacet
(
iIstream& is,
realx3x3& tri
)
{
token tok;
is>>tok;
if( !checkWordToken(is, tok, "facet") ) return false;
is >> tok;
if( !checkWordToken(is, tok , "normal") ) return false;
real val;
for(uint32 i=0; i<3; i++ )
{
is>>tok;
if( !checkNumberToken(is, tok, val))return false;
}
is>> tok;
if( !checkWordToken(is, tok, "outer")) return false;
is>> tok;
if(!checkWordToken(is, tok, "loop")) return false;
realx3 v;
for(uint32 i=0; i<3; i++)
{
is>>tok;
if(!checkWordToken(is, tok, "vertex")) return false;
is>>tok;
if(!checkNumberToken(is, tok, v.x()))return false;
is>>tok;
if(!checkNumberToken(is, tok, v.y()))return false;
is>>tok;
if(!checkNumberToken(is, tok, v.z()))return false;
if( i==0 ) tri.x() = v;
if( i==1 ) tri.y() = v;
if( i==2) tri.z() = v;
}
is>> tok;
if(!checkWordToken(is, tok, "endloop")) return false;
is>> tok;
if(!checkWordToken(is, tok, "endfacet")) return false;
return true;
}
bool pFlow::stlFile::writeFacet
(
iOstream& os,
const realx3x3& tri
)const
{
realx3 n = cross( tri.y() - tri.x(), tri.z()-tri.x());
n.normalize();
os.incrIndent();
indent(os) << "facet" << spaceToken()
<< "normal"<<spaceToken()
<< n.x() << spaceToken()
<< n.y() << spaceToken()
<< n.z() << endl;
os.incrIndent();
indent(os) << "outer loop"<<endl;
os.incrIndent();
indent(os) << "vertex"<< spaceToken()
<< tri.x().x() << spaceToken()
<< tri.x().y() << spaceToken()
<< tri.x().z() << endl;
indent(os) << "vertex"<< spaceToken()
<< tri.y().x() << spaceToken()
<< tri.y().y() << spaceToken()
<< tri.y().z() << endl;
indent(os) << "vertex"<< spaceToken()
<< tri.z().x() << spaceToken()
<< tri.z().y() << spaceToken()
<< tri.z().z() << endl;
os.decrIndent();
indent(os) << "endloop"<<endl;
os.decrIndent();
indent(os)<< "endfacet"<<endl;
os.decrIndent();
return true;
}
bool pFlow::stlFile::writeSolid
(
iOstream& os,
const realx3x3Vector& vertecies,
const word& name
)const
{
os<< "solid"<<spaceToken()<<name<<endl;
for(const auto& tri: vertecies)
{
writeFacet(os, tri);
}
os<< "endsolid"<<endl;
return true;
}
pFlow::stlFile::stlFile( fileSystem file )
:
file_(file)
{
}
pFlow::stlFile::stlFile
(
fileSystem file,
const word& name,
const realx3x3Vector& vertecies
)
:
stlFile(file)
{
addSolid(name, vertecies);
}
pFlow::stlFile::stlFile
(
fileSystem file,
const word& name,
realx3x3Vector&& vertecies
)
:
stlFile(file)
{
addSolid(name, vertecies);
}
void pFlow::stlFile::addSolid
(
const word& name,
const realx3x3Vector& vertecies
)
{
solids_.push_backSafe(vertecies);
solidNames_.push_back(name);
}
void pFlow::stlFile::addSolid
(
const word& name,
realx3x3Vector&& vertecies
)
{
solids_.push_backSafe(std::move(vertecies));
solidNames_.push_back(name);
}
bool pFlow::stlFile::read()
{
solids_.clear();
solidNames_.clear();
// open file
iFstream is(file_);
token tok;
while (true)
{
realx3x3Vector vertecies;
word name;
if(!readSolid(is, vertecies, name))
{
ioErrorInFile(is.name(), is.lineNumber());
return false;
}
addSolid(name, std::move(vertecies));
is >> tok;
if( is.eof() || !tok.good())return true;
is.putBack(tok);
}
return true;
}
bool pFlow::stlFile::write()const
{
oFstream os(file_);
os.precision(8);
for(label i=0; i<size(); i++)
{
writeSolid(os, solids_[i], solidNames_[i]);
}
return true;
}
void pFlow::stlFile::setFile
(
fileSystem file
) const
{
file_ = file;
}
const pFlow::wordList& pFlow::stlFile::names()const
{
return solidNames_;
}
size_t pFlow::stlFile::size()const
{
return solids_.size();
}
const pFlow::realx3x3Vector& pFlow::stlFile::solid
(
label i
)const
{
if(i >= size() )
{
fatalErrorInFunction<<
"requested out of range solid from stlFile "<<
file_<<endl;
fatalExit;
}
return solids_[i];
}
const pFlow::word& pFlow::stlFile::name
(
label i
)const
{
if(i >= size() )
{
fatalErrorInFunction<<
"requested out of range solid name from stlFile "<<
file_<<endl;
fatalExit;
}
return solidNames_[i];
}
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