34 #ifndef AIMS_IO_WAVEFRONTMESHW_H 35 #define AIMS_IO_WAVEFRONTMESHW_H 52 T _clamp(
const T& value,
float m )
56 if( (
float) value < m )
58 else if( (
float) value > 1. - m )
59 new_value = (T) ( 1. - m );
76 class _printTextureCoord
80 std::string p(
const T & tex,
float m = 0.001 )
83 s << _clamp( tex, m ) <<
" 0";
89 template <
typename T,
int D>
97 for(
int i=0; i<3 && i<D; ++i )
101 s << _clamp( tex[i], m );
115 template<
int D,
class T>
127 inline static std::string removeExtension(
const std::string& name );
131 const Header & header, std::ostream & os,
const std::string & filename,
133 inline static void writeObjectHeader( std::ostream & os,
int timestep,
135 const std::string & obj_filename,
154 template <
int U,
typename T>
161 const Header & header, std::ostream & os,
const std::string & filename,
163 inline static void writeObjectHeader(
173 template <
int D,
class T>
176 operator << ( WavefrontMeshWriter<D,T> & writer,
179 writer.write( thing );
184 template <
int D,
class T>
inline 186 const std::string& name )
192 template <
int D,
class T>
inline 194 const Header & header, std::ostream & os,
const std::string & filename,
202 template <
int D,
class T>
inline 204 std::ostream & os,
int timestep,
209 obj_filename, options );
213 template <
int D,
class T>
inline 222 template <
int D>
inline 224 const std::string& name )
226 std::string res = name;
228 if( res.length() > 4 )
229 ext = res.substr(
int(res.length() - 4), 4 );
231 res = res.substr( 0, res.length() - 4 );
238 const Header & header, std::ostream & os,
const std::string & filename,
249 hdr->
setProperty(
"file_type", std::string(
"WAVEFRONT" ) );
256 os <<
"# Wavefront OBJ\n" << std::endl;
266 << std::endl << std::endl;
270 mtl->setProperty(
"filename", mtlfilename );
286 std::stringstream namess;
288 obj_filename ) ) <<
"_" << timestep;
289 std::string name = namess.str();
290 os <<
"o " << name << std::endl << std::endl;
291 os <<
"s 1\n" << std::endl;
299 os <<
"usemtl " << name << std::endl << std::endl;
305 mtldict = mtl->getProperty( name );
309 mtl->setProperty( name, mtldict );
317 for(
int i=0; it->isValid() && i < 3; it->next(), ++i )
321 s << it->currentValue()->getScalar();
323 mtldict->setProperty(
"Ka", s.str() );
335 for( i=0; it->isValid() && i < 3; it->next(), ++i )
339 s << it->currentValue()->getScalar();
341 mtldict->setProperty(
"Kd", s.str() );
342 if( i == 3 && it->isValid() )
344 std::stringstream sd;
345 sd << it->currentValue()->getScalar();
346 mtldict->setProperty(
"d", sd.str() );
355 carto::Object specular = material->getProperty(
"specular" );
358 for(
int i=0; it->isValid() && i < 3; it->next(), ++i )
362 s << it->currentValue()->getScalar();
364 mtldict->setProperty(
"Ks", s.str() );
372 carto::Object shininess = material->getProperty(
"shininess" );
374 s << shininess->getScalar();
375 mtldict->setProperty(
"Ns", s.str() );
385 std::map<int, carto::Object> palettes;
387 std::map<int, carto::Object>::iterator ip = palettes.find( timestep );
388 if( ip != palettes.end() )
390 std::string pal_fname = name +
".png";
397 mtldict = mtl->getProperty( name );
401 mtl->setProperty( name, mtldict );
406 std::stringstream mapkd_val_s;
407 std::vector<float> texoffset;
408 if( hdr->
getProperty(
"texture_offset", texoffset )
409 && texoffset.size() == 3 )
411 mapkd_val_s <<
"-o " << texoffset[0] <<
" " << texoffset[1] <<
" " 412 << texoffset[2] <<
" ";
414 std::vector<float> texscale;
416 && texscale.size() == 3 )
418 mapkd_val_s <<
"-s " << texscale[0] <<
" " << texscale[1] <<
" " 419 << texscale[2] <<
" ";
421 mtldict->setProperty(
"map_Kd", mapkd_val_s.str() + pal_fname );
446 std::string mtlfilename = mtl->getProperty(
"filename" )->getString();
447 std::ofstream ms( mtlfilename.c_str() );
449 for( ; oit->isValid(); oit->next() )
451 if( oit->key() ==
"filename" )
453 std::string oname = oit->key();
454 ms <<
"newmtl " << oname << std::endl << std::endl;
456 for( ; it->isValid(); it->next() )
458 ms << it->key() <<
" " << it->currentValue()->getString() << std::endl;
466 template <
int D,
class T>
470 std::string fname = _name;
471 std::ofstream os( fname.c_str() );
476 thing.
header(), os, _name,
482 for( is=thing.begin(); is!=es; ++is, ++timestep )
484 writeObjectHeader( os, timestep, hdr, fname, options );
486 const std::vector<Point3df> &vert = (*is).second.vertex();
487 const std::vector<Point3df> &
norm = (*is).second.normal();
488 const std::vector<AimsVector<uint,D> > &poly= is->second.polygon();
491 for( std::vector<Point3df>::const_iterator iv = vert.begin();
492 iv != vert.end(); ++iv )
493 os <<
"v " << (*iv)[0] <<
" " << (*iv)[1] <<
" " << (*iv)[2]
498 for( std::vector<Point3df>::const_iterator in = norm.begin();
499 in != norm.end(); ++in )
500 os <<
"vn " << (*in)[0] <<
" " << (*in)[1] <<
" " << (*in)[2]
505 typename std::vector<T>::const_iterator it,
506 et=is->second.texture().end();
514 for( it=is->second.texture().begin(); it!=et; ++it )
516 os <<
"vt " << _printTextureCoord<T>::p( *it, m ) << std::endl;
522 poly.begin(); ip != poly.end(); ++ip )
528 for(
int p=0; p<D; ++p )
530 os <<
" " << (*ip)[p] + 1 <<
"/" << (*ip)[p] + 1 <<
"/" 538 writeMtl( hdr, options );
549 std::string fname = _name;
550 std::ofstream os( fname.c_str() );
560 for( is=thing.begin(); is!=es; ++is, ++timestep )
562 writeObjectHeader( os, timestep, hdr, fname, options );
564 const std::vector<Point3df> &vert = (*is).second.vertex();
565 const std::vector<Point3df> &
norm = (*is).second.normal();
566 const std::vector<AimsVector<uint,D> > &poly= is->second.polygon();
569 for( std::vector<Point3df>::const_iterator iv = vert.begin();
570 iv != vert.end(); ++iv )
571 os <<
"v " << (*iv)[0] <<
" " << (*iv)[1] <<
" " << (*iv)[2]
576 for( std::vector<Point3df>::const_iterator in = norm.begin();
577 in != norm.end(); ++in )
578 os <<
"vn " << (*in)[0] <<
" " << (*in)[1] <<
" " << (*in)[2]
584 poly.begin(); ip != poly.end(); ++ip )
590 for(
int p=0; p<D; ++p )
592 os <<
" " << (*ip)[p] + 1 <<
"//" << (*ip)[p] + 1;
599 writeMtl( hdr, options );
virtual bool getProperty(const std::string &, Object &) const
static std::string basename(const std::string &)
The class for EcatSino data write operation.
static std::string removeExtension(const std::string &)
virtual bool removeProperty(const std::string &)
The template class to manage a mesh with time if needed.
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
Finds the correct format and writes the object.
void write(const AimsTimeSurface< D, T > &thing, carto::Object options=carto::none())
static std::string dirname(const std::string &)
WavefrontMeshWriter(const std::string &name)
WavefrontMeshWriter(const std::string &name)
static std::string removeExtension(const std::string &name)
Return a name without .obj extension.
GenericObject * get() const
Generic writer for every format of Aims object.
std::map< std::string, Object > Dictionary
virtual void setProperty(const std::string &, Object)
AIMSDATA_API float norm(const Tensor &thing)
Object none()
Pointer to an empty aims::StructuringElement.
std::map< int, AimsSurface< D, T > >::const_iterator const_iterator
virtual bool hasProperty(const std::string &) const
const aims::PythonHeader & header() const
Get the header.