Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_IO_MESH_VRML_WRITE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_IO_MESH_VRML_WRITE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <cassert>
00013 #include <string>
00014 #include <fstream>
00015 #include <iostream>
00016 #include <vector>
00017
00018 namespace OpenTissue
00019 {
00020 namespace mesh
00021 {
00031 template<typename mesh_type>
00032 bool vrml_write(std::string const & filename, mesh_type const & mesh)
00033 {
00034 typedef typename mesh_type::index_type index_type;
00035 typedef typename mesh_type::const_vertex_iterator const_vertex_iterator;
00036 typedef typename mesh_type::const_face_iterator const_face_iterator;
00037 typedef typename mesh_type::const_face_vertex_circulator const_face_vertex_circulator;
00038
00039 std::ofstream file(filename.c_str());
00040 if(!file.is_open())
00041 return false;
00042 file.precision(30);
00043 file << "#VRML V2.0 utf8" << std::endl << std::endl;;
00044 file << "Transform {" << std::endl;
00045 file << " children [" << std::endl;
00046 file << " Shape {"<< std::endl;
00047 file << " appearance Appearance {"<< std::endl;
00048 file << " material Material {"<< std::endl;
00049 file << " diffuseColor 0.3333 0.1098 0.6941"<< std::endl;
00050 file << " }" << std::endl;
00051 file << " }" << std::endl;
00052 file << " geometry IndexedFaceSet {" << std::endl;
00053 file << " coord Coordinate { point [" << std::endl;
00054 const_vertex_iterator vend = mesh.vertex_end();
00055 const_vertex_iterator v = mesh.vertex_begin();
00056 for(;v!=vend;++v)
00057 file << " " << v->m_coord(0) << ' ' << v->m_coord(1) << ' ' << v->m_coord(2) << std::endl;
00058 file << " ]}" << std::endl;
00059 file << " coordIndex [" << std::endl;
00060 const_face_iterator fend = mesh.face_end();
00061 const_face_iterator f = mesh.face_begin();
00062 for(;f!=fend;++f)
00063 {
00064 assert(valency(*f)==3 || !"Only triangular faces are supported");
00065 const_face_vertex_circulator i(*f);--i;
00066 const_face_vertex_circulator j(*f);
00067 const_face_vertex_circulator k(*f);++k;
00068 file << " "
00069 << i->get_handle().get_idx()
00070 << " "
00071 << j->get_handle().get_idx()
00072 << " "
00073 << k->get_handle().get_idx()
00074 << " -1"
00075 << std::endl;
00076 }
00077 file << " ]" << std::endl;
00078 file << " }" << std::endl;
00079 file << " }" << std::endl;
00080 file << " ]" << std::endl;
00081 file << "}" << std::endl;
00082 file.flush();
00083 file.close();
00084 std::cout << "vrml_write(...): written " << filename << std::endl;
00085 return true;
00086 }
00087
00088 }
00089 }
00090
00091
00092 #endif