132 const Header & header, std::ostream & os,
const std::string & filename,
134 inline static void writeObjectHeader( std::ostream & os,
int timestep,
136 const std::string & obj_filename,
155 template <
int U,
typename T>
162 const Header & header, std::ostream & os,
const std::string & filename,
164 inline static void writeObjectHeader(
174 template <
int D,
class T>
180 writer.
write( thing );
185 template <
int D,
class T>
inline
187 const std::string& name )
193 template <
int D,
class T>
inline
195 const Header & header, std::ostream & os,
const std::string & filename,
198 return WavefrontMeshWriter<D, Void>::writeHeader( header, os, filename,
203 template <
int D,
class T>
inline
204 void WavefrontMeshWriter<D,T>::writeObjectHeader(
205 std::ostream & os,
int timestep,
206 PythonHeader *hdr,
const std::string & obj_filename,
209 WavefrontMeshWriter<D, Void>::writeObjectHeader( os, timestep, hdr,
210 obj_filename, options );
214 template <
int D,
class T>
inline
215 void WavefrontMeshWriter<D,T>::writeMtl(
PythonHeader *hdr,
216 carto::Object options )
218 WavefrontMeshWriter<D, Void>::writeMtl( hdr, options );
223 template <
int D>
inline
225 const std::string& name )
227 std::string res = name;
229 if( res.length() > 4 )
230 ext = res.substr(
int(res.length() - 4), 4 );
232 res = res.substr( 0, res.length() - 4 );
238 PythonHeader* WavefrontMeshWriter<D, Void>::writeHeader(
239 const Header & header, std::ostream & os,
const std::string & filename,
240 const std::string & dtype, carto::Object )
248 if( hdr->hasProperty(
"nb_t_pos" ) )
249 hdr->removeProperty(
"nb_t_pos" );
250 hdr->setProperty(
"file_type", std::string(
"WAVEFRONT" ) );
251 hdr->setProperty(
"object_type",
253 AimsTimeSurface<D, Void> >::objectType() );
255 hdr->setProperty(
"data_type", dtype );
257 os <<
"# Wavefront OBJ\n" << std::endl;
259 carto::Object material;
262 material = hdr->getProperty(
"material" );
265 std::string mtlfilename = removeExtension( filename ) +
".mtl";
267 << std::endl << std::endl;
270 carto::Object mtl = hdr->getProperty(
"mtl" );
271 mtl->setProperty(
"filename", mtlfilename );
283 void WavefrontMeshWriter<D, Void>::writeObjectHeader(
285 const std::string & obj_filename, carto::Object )
287 std::stringstream namess;
289 obj_filename ) ) <<
"_" << timestep;
290 std::string name = namess.str();
291 os <<
"o " << name << std::endl << std::endl;
292 os <<
"s 1\n" << std::endl;
294 if( hdr->hasProperty(
"material" ) )
296 carto::Object material, mtl;
297 material = hdr->getProperty(
"material" );
298 mtl = hdr->getProperty(
"mtl" );
300 os <<
"usemtl " << name << std::endl << std::endl;
306 mtldict = mtl->getProperty( name );
310 mtl->setProperty( name, mtldict );
315 carto::Object ambient = material->getProperty(
"ambient" );
316 carto::Object it = ambient->objectIterator();
318 for(
int i=0; it->isValid() && i < 3; it->next(), ++i )
322 s << it->currentValue()->getScalar();
324 mtldict->setProperty(
"Ka", s.str() );
332 carto::Object diffuse = material->getProperty(
"diffuse" );
333 carto::Object it = diffuse->objectIterator();
336 for( i=0; it->isValid() && i < 3; it->next(), ++i )
340 s << it->currentValue()->getScalar();
342 mtldict->setProperty(
"Kd", s.str() );
343 if( i == 3 && it->isValid() )
345 std::stringstream sd;
346 sd << it->currentValue()->getScalar();
347 mtldict->setProperty(
"d", sd.str() );
356 carto::Object emission = material->getProperty(
"emission" );
357 carto::Object it = emission->objectIterator();
360 for( i=0; it->isValid() && i < 3; it->next(), ++i )
364 s << it->currentValue()->getScalar();
366 mtldict->setProperty(
"Ke", s.str() );
374 carto::Object specular = material->getProperty(
"specular" );
375 carto::Object it = specular->objectIterator();
377 for(
int i=0; it->isValid() && i < 3; it->next(), ++i )
381 s << it->currentValue()->getScalar();
383 mtldict->setProperty(
"Ks", s.str() );
391 carto::Object shininess = material->getProperty(
"shininess" );
393 s << shininess->getScalar();
394 mtldict->setProperty(
"Ns", s.str() );
402 if( hdr->hasProperty(
"_texture_palettes" ) )
404 std::map<int, carto::Object> palettes;
405 hdr->getProperty(
"_texture_palettes", palettes );
406 std::map<int, carto::Object>::iterator ip = palettes.find( timestep );
407 if( ip != palettes.end() )
409 std::string pal_fname = name +
".png";
411 carto::Object mtl = hdr->getProperty(
"mtl" );
416 mtldict = mtl->getProperty( name );
420 mtl->setProperty( name, mtldict );
425 std::stringstream mapkd_val_s;
426 std::vector<float> texoffset;
427 if( hdr->getProperty(
"texture_offset", texoffset )
428 && texoffset.size() == 3 )
430 mapkd_val_s <<
"-o " << texoffset[0] <<
" " << texoffset[1] <<
" "
431 << texoffset[2] <<
" ";
433 std::vector<float> texscale;
434 if( hdr->getProperty(
"texture_scale", texscale )
435 && texscale.size() == 3 )
437 mapkd_val_s <<
"-s " << texscale[0] <<
" " << texscale[1] <<
" "
438 << texscale[2] <<
" ";
440 mtldict->setProperty(
"map_Kd", mapkd_val_s.str() + pal_fname );
445 ip->second->value<carto::rc_ptr<carto::Volume<AimsRGBA> > >() );
452 void WavefrontMeshWriter<D, Void>::writeMtl(
PythonHeader *hdr,
458 mtl = hdr->getProperty(
"mtl" );
465 std::string mtlfilename = mtl->getProperty(
"filename" )->getString();
466 std::ofstream ms( mtlfilename.c_str() );
467 carto::Object oit = mtl->objectIterator();
468 for( ; oit->isValid(); oit->next() )
470 if( oit->key() ==
"filename" )
472 std::string oname = oit->key();
473 ms <<
"newmtl " << oname << std::endl << std::endl;
474 carto::Object it = oit->currentValue()->objectIterator();
475 for( ; it->isValid(); it->next() )
477 ms << it->key() <<
" " << it->currentValue()->getString() << std::endl;
480 hdr->removeProperty(
"mtl" );
485 template <
int D,
class T>
489 std::string fname = _name;
490 std::ofstream os( fname.c_str() );
495 thing.
header(), os, _name,
501 for( is=thing.begin(); is!=es; ++is, ++timestep )
503 writeObjectHeader( os, timestep, hdr, fname, options );
505 const std::vector<Point3df> &vert = (*is).second.vertex();
506 const std::vector<Point3df> &
norm = (*is).second.normal();
507 const std::vector<AimsVector<uint,D> > &poly= is->second.polygon();
510 for( std::vector<Point3df>::const_iterator iv = vert.begin();
511 iv != vert.end(); ++iv )
512 os <<
"v " << (*iv)[0] <<
" " << (*iv)[1] <<
" " << (*iv)[2]
517 for( std::vector<Point3df>::const_iterator in =
norm.begin();
518 in !=
norm.end(); ++in )
519 os <<
"vn " << (*in)[0] <<
" " << (*in)[1] <<
" " << (*in)[2]
524 typename std::vector<T>::const_iterator it,
525 et=is->second.texture().end();
533 for( it=is->second.texture().begin(); it!=et; ++it )
535 os <<
"vt " << _printTextureCoord<T>::p( *it, m ) << std::endl;
541 poly.begin(); ip != poly.end(); ++ip )
547 for(
int p=0; p<D; ++p )
549 os <<
" " << (*ip)[p] + 1 <<
"/" << (*ip)[p] + 1 <<
"/"
557 writeMtl( hdr, options );
568 std::string fname = _name;
569 std::ofstream os( fname.c_str() );
579 for( is=thing.begin(); is!=es; ++is, ++timestep )
581 writeObjectHeader( os, timestep, hdr, fname, options );
583 const std::vector<Point3df> &vert = (*is).second.vertex();
584 const std::vector<Point3df> &
norm = (*is).second.normal();
585 const std::vector<AimsVector<uint,D> > &poly= is->second.polygon();
588 for( std::vector<Point3df>::const_iterator iv = vert.begin();
589 iv != vert.end(); ++iv )
590 os <<
"v " << (*iv)[0] <<
" " << (*iv)[1] <<
" " << (*iv)[2]
595 for( std::vector<Point3df>::const_iterator in =
norm.begin();
596 in !=
norm.end(); ++in )
597 os <<
"vn " << (*in)[0] <<
" " << (*in)[1] <<
" " << (*in)[2]
603 poly.begin(); ip != poly.end(); ++ip )
609 for(
int p=0; p<D; ++p )
611 os <<
" " << (*ip)[p] + 1 <<
"//" << (*ip)[p] + 1;
618 writeMtl( hdr, options );