Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_IO_MESH_OBJ_WRITE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_IO_MESH_OBJ_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 obj_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
00041 if(!file.is_open())
00042 return false;
00043
00044 file << "g group0" << std::endl;
00045
00046 file.precision(30);
00047 const_vertex_iterator vend = mesh.vertex_end();
00048 const_vertex_iterator v = mesh.vertex_begin();
00049 for(;v!=vend;++v)
00050 {
00051 file << "v " << v->m_coord(0) << ' ' << v->m_coord(1) << ' ' << v->m_coord(2) << std::endl;
00052 file << "vt " << v->m_u << ' ' << v->m_v << std::endl;
00053 file << "vn " << v->m_normal(0) << ' ' << v->m_normal(1) << ' ' << v->m_normal(2) << std::endl;
00054 }
00055 const_face_iterator fend = mesh.face_end();
00056 const_face_iterator f = mesh.face_begin();
00057 for(;f!=fend;++f)
00058 {
00059 file << "f";
00060 const_face_vertex_circulator fvc(*f),end;
00061 for(;fvc!=end;++fvc)
00062 {
00063 index_type idx = fvc->get_handle().get_idx() + 1;
00064 file << " " << idx << "/" << idx << "/" << idx;
00065
00066 }
00067 file << std::endl;
00068 }
00069 file.flush();
00070 file.close();
00071 std::cout << "obj_write(...): written " << filename << std::endl;
00072 return true;
00073 }
00074
00075 }
00076 }
00077
00078
00079 #endif