mesh_conv::MC_io_obj Class Reference

Helper class to read and write mesh in a obj format. More...

#include <MC_io_obj.hpp>

List of all members.

Static Public Member Functions

static std::pair
< MC_v3d_vector,
MC_connectivity_index
read_obj (std::istream &stream)
 read an input stream in a obj format
static std::pair
< MC_v3d_vector,
MC_connectivity_index
read_obj_texture (std::istream &stream)
 read an input stream in a obj format for the texture
static std::pair
< MC_v3d_vector,
MC_connectivity_index
read_obj_texture_file (const std::string &file)
 read an input file in a obj format for the texture
static std::pair
< MC_v3d_vector,
MC_connectivity_index
read_obj_normal (std::istream &stream)
 read an input stream in a obj format for the normal
static std::pair
< MC_v3d_vector,
MC_connectivity_index
read_obj_file (const std::string &filename)
 read an input file in a obj format
static std::ostream & write_obj (std::ostream &stream, const MC_mesh_index_vector &to_write)
 write input stream in obj format
static std::ostream & write_obj (std::ostream &stream, const MC_mesh_index_vector_draw &to_write)
 write input stream in obj format
template<typename T >
static const MC_mesh_index_vectorwrite_obj_file (const std::string &string_file, const T &to_write)
 write input stream in obj format

Detailed Description

Helper class to read and write mesh in a obj format.

Definition at line 38 of file MC_io_obj.hpp.


Member Function Documentation

std::pair< MC_v3d_vector, MC_connectivity_index > mesh_conv::MC_io_obj::read_obj ( std::istream &  stream  )  [static]

read an input stream in a obj format

Returns:
<point_set,connectivity>

Definition at line 11 of file MC_io_obj.cpp.

References mesh_conv::MC_int_vector_vector::add(), mesh_conv::MC_int_vector::add(), mesh_conv::MC_v3d_vector::add(), mesh_conv::MC_string_converter::delete_empty(), and mesh_conv::MC_string_tokenizer::tokenize().

Referenced by read_obj_file().

00012     {
00013 
00014         MC_v3d_vector vertices;
00015         MC_connectivity_index connectivity;
00016 
00017         if(stream.good()==false)
00018         {std::cout<<"Error in MC_io_off::read_obj(istream), file cannot be read"<<std::endl;exit(-1);}
00019 
00020         std::string buffer;
00021         while(stream.good()==true)
00022         {
00023             if(stream.good())
00024                 stream>>buffer;
00025 
00026 
00027             if(buffer=="v")//vertex
00028             {
00029                 MC_v3d temp;
00030                 for(int k_dim=0;k_dim<3;++k_dim){
00031                     if(stream.good()){
00032                         stream>>buffer;
00033                         temp[k_dim]=MC_string_converter::value_of<double>(buffer);
00034                     }
00035                 }
00036                 vertices.add(temp);
00037             }
00038             //face
00039             else if(buffer=="f")
00040             {
00041                 MC_int_vector index;
00042 
00043                 //take the whole line
00044                 std::getline(stream,buffer);
00045 
00046                 std::vector<std::string> vec=MC_string_converter::delete_empty(MC_string_tokenizer::tokenize(buffer," "));
00047                 for(unsigned int k=0;k<vec.size();++k)
00048                 {
00049                     std::vector<std::string> poly=MC_string_tokenizer::tokenize(vec[k],"/");
00050 
00051                     //vertex are in first place
00052                     index.add(MC_string_converter::value_of<int>(poly[0])-1);
00053                 }
00054                 connectivity.add(index);
00055             }
00056             else
00057                 std::getline(stream,buffer);
00058         }
00059 
00060 
00061         return std::make_pair(vertices,connectivity);
00062     }

Here is the call graph for this function:

Here is the caller graph for this function:

std::pair< MC_v3d_vector, MC_connectivity_index > mesh_conv::MC_io_obj::read_obj_file ( const std::string &  filename  )  [static]

read an input file in a obj format

Returns:
<point_set,connectivity>

Definition at line 124 of file MC_io_obj.cpp.

References read_obj().

Referenced by mesh_conv::MC_mesh_index_vector_draw::load_mesh_file(), and mesh_conv::MC_mesh_index_vector::load_mesh_file().

00125     {
00126         std::ifstream ifile(string_file.c_str());
00127         if(ifile.good()!=true)
00128         {std::cout<<"Error in MC_io_obj::read_obj_file("<<string_file<<"), cannot open the file"<<std::endl;exit(-1);}
00129 
00130         std::pair <MC_v3d_vector,MC_connectivity_index> input=MC_io_obj::read_obj(ifile);
00131         return input;
00132     }

Here is the call graph for this function:

Here is the caller graph for this function:

static std::pair<MC_v3d_vector,MC_connectivity_index> mesh_conv::MC_io_obj::read_obj_normal ( std::istream &  stream  )  [static]

read an input stream in a obj format for the normal

Returns:
<normal_vector,normal_connectivity>
std::pair< MC_v3d_vector, MC_connectivity_index > mesh_conv::MC_io_obj::read_obj_texture ( std::istream &  stream  )  [static]

read an input stream in a obj format for the texture

Returns:
<texture_vector,texture_connectivity>

Definition at line 65 of file MC_io_obj.cpp.

References mesh_conv::MC_int_vector_vector::add(), mesh_conv::MC_int_vector::add(), mesh_conv::MC_v3d_vector::add(), mesh_conv::MC_string_converter::delete_empty(), and mesh_conv::MC_string_tokenizer::tokenize().

Referenced by read_obj_texture_file().

00066     {
00067 
00068         MC_v3d_vector tex;
00069         MC_connectivity_index index_texture;
00070 
00071         if(stream.good()==false)
00072         {std::cout<<"Error in MC_io_off::read_obj_texture(istream), file cannot be read"<<std::endl;exit(-1);}
00073 
00074         std::string buffer;
00075         while(stream.good()==true)
00076         {
00077             if(stream.good())
00078                 stream>>buffer;
00079 
00080             if(buffer[0]=='#')
00081                 std::getline(stream,buffer);
00082             if(buffer=="vt")//vertex
00083             {
00084                 MC_v3d temp;
00085                 for(int k_dim=0;k_dim<2;++k_dim){
00086                     if(stream.good()){
00087                         stream>>buffer;
00088                         temp[k_dim]=MC_string_converter::value_of<double>(buffer);
00089                     }
00090                 }
00091                 //std::cout<<temp<<std::endl;
00092                 tex.add(temp);
00093             }
00094 
00095             //face
00096             if(buffer=="f")
00097             {
00098                 MC_int_vector index;
00099 
00100                 //take the whole line
00101                 std::getline(stream,buffer);
00102 
00103                 std::vector<std::string> vec=MC_string_converter::delete_empty(MC_string_tokenizer::tokenize(buffer," "));
00104                 for(unsigned int k=0;k<vec.size();++k)
00105                 {
00106                     std::vector<std::string> poly=MC_string_converter::delete_empty(MC_string_tokenizer::tokenize(vec[k],"/"));
00107 
00108                     if(poly.size()==3)
00109                         index.add(MC_string_converter::value_of<int>(poly[1])-1);
00110                     else if(vec[k].find("/")!=std::string::npos)
00111                     {
00112                         poly=MC_string_converter::delete_empty(MC_string_tokenizer::tokenize(vec[k],"/"));
00113                         index.add(MC_string_converter::value_of<int>(poly[1])-1);
00114                     }
00115                 }
00116 
00117                 index_texture.add(index);
00118             }
00119         }
00120 
00121         return std::make_pair(tex,index_texture);
00122     }

Here is the call graph for this function:

Here is the caller graph for this function:

std::pair< MC_v3d_vector, MC_connectivity_index > mesh_conv::MC_io_obj::read_obj_texture_file ( const std::string &  file  )  [static]

read an input file in a obj format for the texture

Returns:
<texture_vector,texture_connectivity>

Definition at line 225 of file MC_io_obj.cpp.

References read_obj_texture().

Referenced by mesh_conv::MC_mesh_index_vector_draw::load_mesh_file().

00226     {
00227         std::ifstream fid;
00228         fid.open(file.c_str(),std::ifstream::in);
00229         std::pair <MC_v3d_vector,MC_connectivity_index> tex=read_obj_texture(fid);
00230         fid.close();
00231         return tex;
00232     }

Here is the call graph for this function:

Here is the caller graph for this function:

std::ostream & mesh_conv::MC_io_obj::write_obj ( std::ostream &  stream,
const MC_mesh_index_vector_draw to_write 
) [static]

write input stream in obj format

Definition at line 134 of file MC_io_obj.cpp.

References mesh_conv::MC_mesh_index_vector::connectivity(), mesh_conv::MC_mesh_index_vector_draw::normal(), mesh_conv::MC_mesh_index_vector::point_set(), mesh_conv::MC_mesh_index_vector::polygon_number(), mesh_conv::MC_int_vector::size(), mesh_conv::MC_int_vector_vector::size(), mesh_conv::MC_v3d_vector::size(), mesh_conv::MC_mesh_index_vector_draw::texture(), mesh_conv::MC_mesh_index_vector_draw::texture_connectivity(), and mesh_conv::MC_mesh_index_vector::vertex_number().

00135     {
00136         bool is_normal=to_write.normal().size()==to_write.vertex_number();
00137         bool is_texture=to_write.texture_connectivity().size()>0;
00138 
00139         //vertices
00140         for(int k=0,N=to_write.vertex_number();k<N;++k)
00141         {
00142             MC_v3d x=to_write.point_set()(k);
00143             stream<<"v "<<x[0]<<" "<<x[1]<<" "<<x[2]<<std::endl;
00144         }
00145         //normal
00146         if(is_normal==true)
00147         {
00148             for(int k=0,N=to_write.normal().size();k<N;++k)
00149             {
00150                 MC_v3d n=to_write.normal()(k);
00151                 stream<<"vn "<<n[0]<<" "<<n[1]<<" "<<n[2]<<std::endl;
00152             }
00153         }
00154         //texture
00155         if(is_texture==true)
00156         {
00157             for(int k=0,N=to_write.texture().size();k<N;++k)
00158             {
00159                 MC_v3d t=to_write.texture()(k);
00160                 stream<<"vt "<<t[0]<<" "<<t[1]<<std::endl;
00161             }
00162         }
00163 
00164 
00165 
00166         //connectivity
00167         for(int k=0,N=to_write.polygon_number();k<N;++k)
00168         {
00169             MC_int_vector index=to_write.connectivity()(k)+1;
00170             stream<<"f ";
00171 
00172             if(is_normal==false && is_texture==false)
00173             {
00174                 for(int k_v=0,N_v=index.size();k_v<N_v;++k_v)
00175                     stream<<index[k_v]<<" ";
00176             }
00177             if(is_normal==true && is_texture==false)
00178             {
00179                 for(int k_v=0,N_v=index.size();k_v<N_v;++k_v)
00180                     stream<<index[k_v]<<"//"<<index[k_v]<<" ";
00181             }
00182             if(is_normal==false && is_texture==true)
00183             {
00184                 MC_int_vector index_texture=to_write.texture_connectivity()(k)+1;
00185                 for(int k_v=0,N_v=index.size();k_v<N_v;++k_v)
00186                 {
00187                     stream<<index[k_v]<<"/"<<index_texture[k_v]<<" ";
00188                 }
00189             }
00190             if(is_normal==true && is_texture==true)
00191             {
00192                 MC_int_vector index_texture=to_write.texture_connectivity()(k)+1;
00193                 for(int k_v=0,N_v=index.size();k_v<N_v;++k_v)
00194                 {
00195                     stream<<index[k_v]<<"/"<<index_texture[k_v]<<"/"<<index[k_v]<<" ";
00196                 }
00197             }
00198 
00199             stream<<std::endl;
00200         }
00201         return stream;
00202     }

std::ostream & mesh_conv::MC_io_obj::write_obj ( std::ostream &  stream,
const MC_mesh_index_vector to_write 
) [static]

write input stream in obj format

Definition at line 204 of file MC_io_obj.cpp.

References mesh_conv::MC_mesh_index_vector::connectivity(), mesh_conv::MC_mesh_index_vector::point_set(), mesh_conv::MC_mesh_index_vector::polygon_number(), mesh_conv::MC_int_vector::size(), and mesh_conv::MC_mesh_index_vector::vertex_number().

Referenced by write_obj_file().

00205     {
00206         //vertices
00207         for(int k=0,N=to_write.vertex_number();k<N;++k)
00208         {
00209             MC_v3d x=to_write.point_set()(k);
00210             stream<<"v "<<x[0]<<" "<<x[1]<<" "<<x[2]<<std::endl;
00211         }
00212         //connectivity
00213         for(int k=0,N=to_write.polygon_number();k<N;++k)
00214         {
00215             MC_int_vector index=to_write.connectivity()(k)+1;
00216             stream<<"f ";
00217             for(int k_v=0,N_v=index.size();k_v<N_v;++k_v)
00218                 stream<<index[k_v]<<" ";
00219             stream<<std::endl;
00220         }
00221         return stream;
00222     }

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
static const MC_mesh_index_vector& mesh_conv::MC_io_obj::write_obj_file ( const std::string &  string_file,
const T &  to_write 
) [inline, static]

write input stream in obj format

Definition at line 69 of file MC_io_obj.hpp.

References write_obj().

00070         {
00071             std::ofstream ofile(string_file.c_str());
00072             if(ofile.good()!=true)
00073             {std::cout<<"Error in MC_io_obj::write_obj_file("<<string_file<<",MC_mesh_index_vector), cannot open the file"<<std::endl;exit(-1);}
00074 
00075             MC_io_obj::write_obj(ofile,to_write);
00076             ofile.close();
00077             return to_write;
00078         }

Here is the call graph for this function:


The documentation for this class was generated from the following files:

Generated on Sun Apr 18 20:24:48 2010 by  doxygen 1.6.1