Helper class to read and write mesh in a off format. More...
#include <MC_io_off.hpp>
Static Public Member Functions | |
| static std::pair < MC_v3d_vector, MC_connectivity_index > | read_off (std::istream &stream) |
| read an input stream in a off format | |
| static std::pair < MC_v3d_vector, MC_connectivity_index > | read_off_file (const std::string &string_file) |
| read an input file in a off format | |
| static std::ostream & | write_off (std::ostream &stream, const MC_mesh_index_vector &to_write) |
| write input stream in off format | |
| static const MC_mesh_index_vector & | write_off_file (const std::string &string_file, const MC_mesh_index_vector &to_write) |
| write input stream into a file | |
Helper class to read and write mesh in a off format.
Definition at line 34 of file MC_io_off.hpp.
| std::pair< MC_v3d_vector, MC_connectivity_index > mesh_conv::MC_io_off::read_off | ( | std::istream & | stream | ) | [static] |
read an input stream in a off format
Definition at line 12 of file MC_io_off.cpp.
References mesh_conv::MC_int_vector_vector::add(), mesh_conv::MC_int_vector::add(), mesh_conv::MC_v3d_vector::add(), counter, mesh_conv::MC_string_converter::delete_empty(), and mesh_conv::MC_string_tokenizer::tokenize().
Referenced by read_off_file().
00013 { 00014 if(stream.good()==false) 00015 {std::cout<<"Error in MC_io_off::read_off(istream), file cannot be read"<<std::endl;exit(-1);} 00016 00017 const int SIZE_BUFFER=2048; 00018 std::vector <char> c_buffer(SIZE_BUFFER); 00019 std::string buffer; 00020 00021 int N_vertex=0,N_polygon=0,N_edge=0; 00022 00023 // look for *OFF* 00024 bool is_loop=true; 00025 while(stream.good() && is_loop==true) 00026 { 00027 std::getline(stream,buffer); 00028 if(buffer.length()>0 00029 && buffer[0]!='#' 00030 && buffer.find("OFF")!=std::string::npos) 00031 is_loop=false; 00032 } 00033 if(stream.good()==false && is_loop==true) 00034 {std::cout<<"ERROR in MC_io_off::read_off(istream), cannot find OFF header"<<std::endl;exit(-1);} 00035 00036 // look for N_vertex N_polygon N_edges 00037 is_loop=true; 00038 while(stream.good() && is_loop==true) 00039 { 00040 std::getline(stream,buffer); 00041 00042 //std::cout<<buffer<<std::endl; 00043 if(buffer.length()>0 && buffer[0]!='#') 00044 { 00045 std::vector <std::string> token=MC_string_tokenizer::tokenize(buffer); 00046 if(token.size()>=3) 00047 { 00048 bool vertex_converted=false; 00049 bool polygon_converted=false; 00050 bool edge_converted=false; 00051 N_vertex = MC_string_converter::value_of<int>(token[0],&vertex_converted); 00052 N_polygon = MC_string_converter::value_of<int>(token[1],&polygon_converted); 00053 N_edge = MC_string_converter::value_of<int>(token[2],&edge_converted); 00054 00055 if(vertex_converted==true && polygon_converted==true && edge_converted==true) 00056 is_loop=false; 00057 } 00058 } 00059 } 00060 if(stream.good()==false && is_loop==true) 00061 {std::cout<<"ERROR in MC_io_off::read_off(istream), cannot read N_vertex/N_polygon/N_edge <<"<<buffer<<std::endl;exit(-1);} 00062 00063 00064 if(N_vertex<=0 || N_polygon<=0) 00065 {std::cout<<"Something wrong in MC_io_off::read_off(istream), N_vertex="<<N_vertex<<", N_polygon="<<N_polygon<<std::endl;exit(-1);} 00066 00067 00068 MC_v3d_vector point_set; 00069 00070 //now read N_vertex positions 00071 for(int k_vertex=0;k_vertex<N_vertex;++k_vertex) 00072 { 00073 if(stream.good()==false) 00074 {std::cout<<"Error in MC_io_off::read_off(istream) at vertex"<<k_vertex<<" EOF found"<<std::endl;exit(-1);} 00075 00076 std::getline(stream,buffer); 00077 if(buffer.size()>0 && buffer[0]!='#') 00078 { 00079 std::vector<std::string> v_token=MC_string_converter::delete_empty(MC_string_tokenizer::tokenize(buffer)); 00080 if(v_token.size()<3) 00081 {std::cout<<"Error in MC_io_off::read_off(istream), something wrong at vertex "<<k_vertex<<", size of token="<<v_token.size()<<", "<<buffer<<std::endl;exit(-1);} 00082 00083 bool converted_x0=false,converted_x1=false,converted_x2=false; 00084 double x0=MC_string_converter::value_of<double>(v_token[0],&converted_x0); 00085 double x1=MC_string_converter::value_of<double>(v_token[1],&converted_x1); 00086 double x2=MC_string_converter::value_of<double>(v_token[2],&converted_x2); 00087 00088 if(converted_x0==false || converted_x1==false || converted_x2==false) 00089 {std::cout<<"Something wrong in MC_io_off::read_off(istream), cannot convert at vertex "<<k_vertex<<","<<buffer<<std::endl;exit(-1);} 00090 00091 point_set.add(MC_v3d(x0,x1,x2)); 00092 } 00093 else 00094 --k_vertex; 00095 } 00096 00097 00098 MC_connectivity_index connectivity; 00099 //now read N_polygons 00100 for(int k_polygon=0;k_polygon<N_polygon;++k_polygon) 00101 { 00102 std::getline(stream,buffer); 00103 if(buffer.size()>0 && buffer[0]!='#') 00104 { 00105 std::vector <std::string> v_token=MC_string_tokenizer::tokenize(buffer); 00106 if(v_token.size()>=4) 00107 { 00108 // get the size 00109 bool poly_size_convert=false; 00110 int poly_size=MC_string_converter::value_of<int>(v_token[0],&poly_size_convert); 00111 if(poly_size_convert==false) 00112 {std::cout<<"Something wrong in MC_io_off::read_off(istream), cannot load polygon size at polygon "<<k_polygon<<" polygon of size<3 "<<buffer<<std::endl;exit(-1);} 00113 00114 if(poly_size<3) 00115 {std::cout<<"Something wrong in MC_io_off::read_off(istream), cannot load polygon size at polygon "<<k_polygon<<" polygon size="<<poly_size<<"<3 "<<buffer<<std::endl;exit(-1);} 00116 00117 MC_int_vector temp_poly; 00118 if(int(v_token.size()-1)<poly_size) 00119 {std::cout<<"Error in MC_io_off::read_off(istream), incompatible token and poly size at k_poly="<<k_polygon<<", "<<buffer<<std::endl;exit(-1);} 00120 00121 int counter=0; 00122 for(int k_current_polygon=0;k_current_polygon<poly_size;++k_current_polygon) 00123 { 00124 ++counter; 00125 00126 bool index_converted=false; 00127 if(v_token[counter]!="") 00128 { 00129 temp_poly.add(MC_string_converter::value_of<int>(v_token[counter],&index_converted)); 00130 if(index_converted==false) 00131 {std::cout<<"Something wrong in MC_io_off::read_off(istream), cannot load polygon at polygon "<<k_polygon<<" polygon for index "<<k_current_polygon<<" ,"<<buffer<<std::endl;exit(-1);} 00132 } 00133 else 00134 k_current_polygon--; 00135 } 00136 connectivity.add(temp_poly); 00137 } 00138 else 00139 {std::cout<<"Something wrong in MC_io_off::read_off(istream), cannot load at polygon "<<k_polygon<<" polygon of size<3 "<<buffer<<std::endl;exit(-1);} 00140 00141 } 00142 else 00143 --k_polygon; 00144 00145 } 00146 00147 return std::pair <MC_v3d_vector,MC_connectivity_index> (point_set,connectivity); 00148 }


| std::pair< MC_v3d_vector, MC_connectivity_index > mesh_conv::MC_io_off::read_off_file | ( | const std::string & | string_file | ) | [static] |
read an input file in a off format
Definition at line 183 of file MC_io_off.cpp.
References read_off().
Referenced by mesh_conv::MC_mesh_index_vector_draw::load_mesh_file(), and mesh_conv::MC_mesh_index_vector::load_mesh_file().
00184 { 00185 std::ifstream ifile(string_file.c_str()); 00186 if(ifile.good()!=true) 00187 {std::cout<<"Error in MC_io_off::read_off_file("<<string_file<<"), cannot open the file"<<std::endl;exit(-1);} 00188 00189 std::pair <MC_v3d_vector,MC_connectivity_index> input=MC_io_off::read_off(ifile); 00190 ifile.close(); 00191 00192 return input; 00193 }


| std::ostream & mesh_conv::MC_io_off::write_off | ( | std::ostream & | stream, | |
| const MC_mesh_index_vector & | to_write | |||
| ) | [static] |
write input stream in off format
Definition at line 150 of file MC_io_off.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_off_file().
00151 { 00152 stream<<"OFF"<<std::endl; 00153 stream<<to_write.vertex_number()<<" "<<to_write.polygon_number()<<" 0"<<std::endl; 00154 int N_vertex=to_write.vertex_number(); 00155 for(int k_vertex=0;k_vertex<N_vertex;++k_vertex) 00156 stream<<to_write.point_set()(k_vertex)(0)<<" "<<to_write.point_set()(k_vertex)(1)<<" "<<to_write.point_set()(k_vertex)(2)<<std::endl; 00157 int N_polygon=to_write.polygon_number(); 00158 00159 for(int k_poly=0;k_poly<N_polygon;++k_poly) 00160 { 00161 MC_int_vector index=to_write.connectivity()(k_poly); 00162 int N_current=index.size(); 00163 00164 stream<<N_current<<" "; 00165 for(int k=0;k<N_current;++k) 00166 stream<<index[k]<<" "; 00167 stream<<std::endl; 00168 } 00169 00170 return stream; 00171 }


| const MC_mesh_index_vector & mesh_conv::MC_io_off::write_off_file | ( | const std::string & | string_file, | |
| const MC_mesh_index_vector & | to_write | |||
| ) | [static] |
write input stream into a file
Definition at line 172 of file MC_io_off.cpp.
References write_off().
Referenced by mesh_conv::MC_mesh_index_vector::intersection_curve().
00173 { 00174 std::ofstream ofile(string_file.c_str()); 00175 if(ofile.good()!=true) 00176 {std::cout<<"Error in MC_io_off::write_off_file("<<string_file<<",MC_mesh_index_vector), cannot open the file"<<std::endl;exit(-1);} 00177 00178 MC_io_off::write_off(ofile,to_write); 00179 ofile.close(); 00180 return to_write; 00181 }


1.6.1