00001
00002
00003 #include <MC_segment.hpp>
00004 #include <MC_io_off.hpp>
00005 #include <MC_io_obj.hpp>
00006 #include <MC_string_helper.hpp>
00007
00008 #include <MC_mesh_index_vector_draw.hpp>
00009
00010 namespace mesh_conv
00011 {
00012
00013 MC_mesh_index_vector_draw::MC_mesh_index_vector_draw()
00014 :MC_mesh_index_vector()
00015 {}
00016 MC_mesh_index_vector_draw::MC_mesh_index_vector_draw(const MC_v3d_vector& input_point_set,const MC_connectivity_index& input_connectivity)
00017 :MC_mesh_index_vector(input_point_set,MC_connectivity_index())
00018 {
00019 MC_connectivity_index temp_co=input_connectivity;
00020 temp_co.build_star();
00021 connectivity_mesh=temp_co;
00022
00023
00024 normal_mesh=MC_mesh_index_vector::normal_vertex(point_set_mesh,temp_co);
00025 }
00026 MC_mesh_index_vector_draw::MC_mesh_index_vector_draw(const MC_mesh_index_vector& mesh)
00027 :MC_mesh_index_vector(mesh.point_set(),MC_connectivity_index())
00028 {
00029 MC_connectivity_index temp_co=mesh.connectivity();
00030 temp_co.build_star();
00031 connectivity_mesh=temp_co;
00032
00033
00034
00035 normal_mesh=MC_mesh_index_vector::normal_vertex(point_set_mesh,temp_co);
00036 }
00037
00038 MC_v3d_vector& MC_mesh_index_vector_draw::normal(){return normal_mesh;}
00039 const MC_v3d_vector& MC_mesh_index_vector_draw::normal() const {return normal_mesh;}
00040 MC_v3d_vector& MC_mesh_index_vector_draw::color() {return color_mesh;}
00041 const MC_v3d_vector& MC_mesh_index_vector_draw::color() const {return color_mesh;}
00042 MC_v3d_vector& MC_mesh_index_vector_draw::texture() {return texture_mesh;}
00043 const MC_v3d_vector& MC_mesh_index_vector_draw::texture() const {return texture_mesh;}
00044 MC_double_vector& MC_mesh_index_vector_draw::alpha() {return alpha_mesh;}
00045 const MC_double_vector& MC_mesh_index_vector_draw::alpha() const {return alpha_mesh;}
00046
00047 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::fill_color(const MC_v3d& input_color)
00048 {
00049 int N_vertex=vertex_number();
00050 color_mesh.resize(N_vertex);
00051 for(int k_vertex=0;k_vertex<N_vertex;++k_vertex)
00052 color_mesh[k_vertex]=input_color;
00053 return *this;
00054 }
00055 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::fill_color(const MC_v3d_vector& input_color)
00056 {
00057 if(input_color.size()==1)
00058 return fill_color(input_color.first());
00059 else if(input_color.size()==vertex_number())
00060 {
00061 int N_vertex=vertex_number();
00062 color_mesh.resize(N_vertex);
00063 for(int k_vertex=0;k_vertex<N_vertex;++k_vertex)
00064 color_mesh[k_vertex]=input_color[k_vertex];
00065 return *this;
00066 }
00067 else
00068 {std::cout<<"Error in MC_mesh_index_vector_draw::fill_color(MC_v3d_vector), input_color has size "<<input_color.size()<<" , and vertex_number="<<vertex_number()<<std::endl;exit(-1);}
00069
00070 }
00071
00072
00073
00074 std::vector <MC_mesh_index_vector_draw> MC_mesh_index_vector_draw::build_local_basis()
00075 {
00076 MC_mesh_index_vector_draw a_x=MC_mesh_index_vector::build_arrow(MC_segment(MC_v3d(0,0,0),MC_v3d(1,0,0)),0.05,15,15,0.15,0.25,15,15);
00077 a_x.fill_color(MC_v3d(1,0,0));
00078
00079 MC_mesh_index_vector_draw a_y=MC_mesh_index_vector::build_arrow(MC_segment(MC_v3d(0,0,0),MC_v3d(0,1,0)),0.05,15,15,0.15,0.25,15,15);
00080 a_y.fill_color(MC_v3d(0,1,0));
00081
00082 MC_mesh_index_vector_draw a_z=MC_mesh_index_vector::build_arrow(MC_segment(MC_v3d(0,0,0),MC_v3d(0,0,1)),0.05,15,15,0.15,0.25,15,15);
00083 a_z.fill_color(MC_v3d(0,0,1));
00084
00085
00086
00087 MC_mesh_index_vector_draw sphere=MC_mesh_index_vector::build_sphere(3);
00088
00089
00090 int N=sphere.vertex_number();
00091 sphere.color().resize(N);
00092 for(int k=0;k<N;k++)
00093 {
00094 MC_v3d X=sphere.point_set()[k];
00095 double r=fabs(X[0]);
00096 double g=fabs(X[1]);
00097 double b=fabs(X[2]);
00098 sphere.color()(k)=MC_v3d(r,g,b);
00099 }
00100
00101 a_x*=0.2;a_y*=0.2;a_z*=0.2;
00102 sphere*=0.04;
00103
00104
00105 std::vector <MC_mesh_index_vector_draw> v_mesh;
00106 v_mesh.push_back(a_x);
00107 v_mesh.push_back(a_y);
00108 v_mesh.push_back(a_z);
00109 v_mesh.push_back(sphere);
00110 return v_mesh;
00111 }
00112
00113
00114 MC_mesh_index_vector_draw operator<<(const MC_mesh_index_vector_draw& vec0,const MC_mesh_index_vector_draw& vec1)
00115 {
00116 MC_mesh_index_vector_draw new_vec;
00117
00118 new_vec.point_set()=vec0.point_set()<<vec1.point_set();
00119 new_vec.color()=vec0.color()<<vec1.color();
00120 new_vec.texture()=vec0.texture()<<vec1.texture();
00121 new_vec.texture_connectivity()=vec0.texture_connectivity()<<(vec1.texture_connectivity()+vec0.texture().size());
00122 new_vec.alpha()=vec0.alpha()<<vec1.alpha();
00123 new_vec.normal()=vec0.normal()<<vec1.normal();
00124 new_vec.connectivity()=vec0.connectivity()<<(vec1.connectivity()+vec0.vertex_number());
00125
00126 return new_vec;
00127 }
00128
00129 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::concatenation(const MC_mesh_index_vector_draw& vec1)
00130 {
00131 int N_vertex=point_set().size();
00132 int N_texture=texture().size();
00133
00134 point_set().add(vec1.point_set());
00135 color_mesh.add(vec1.color());
00136 texture_mesh.add(vec1.texture());
00137 alpha_mesh.add(vec1.alpha());
00138 normal_mesh.add(vec1.normal());
00139 connectivity().add(vec1.connectivity()+N_vertex);
00140 texture_connectivity_mesh.add(vec1.texture_connectivity()+N_texture);
00141
00142 return *this;
00143 }
00144
00145 const MC_connectivity_index& MC_mesh_index_vector_draw::texture_connectivity() const
00146 {return texture_connectivity_mesh;}
00147
00148 MC_connectivity_index& MC_mesh_index_vector_draw::texture_connectivity()
00149 {return texture_connectivity_mesh;}
00150
00151 MC_mesh_index_vector_draw operator*(const MC_matrix& M,const MC_mesh_index_vector_draw& mesh)
00152 {
00153 MC_mesh_index_vector_draw m1=mesh;
00154 m1.point_set()=M*m1.point_set();
00155 return m1;
00156 }
00157 MC_mesh_index_vector_draw operator*(const MC_mesh_index_vector_draw& mesh,const MC_matrix& M)
00158 {return M*mesh;}
00159 MC_mesh_index_vector_draw operator*(const double& s,const MC_mesh_index_vector_draw& mesh)
00160 {
00161 MC_mesh_index_vector_draw m1=mesh;
00162 m1.point_set()=s*m1.point_set();
00163 return m1;
00164 }
00165 MC_mesh_index_vector_draw operator*(const MC_mesh_index_vector_draw& mesh,const double& s)
00166 {return s*mesh;}
00167
00168
00169 MC_mesh_index_vector_draw operator+(const MC_mesh_index_vector_draw& vec,const MC_v3d& to_add)
00170 {MC_mesh_index_vector_draw res=vec;res.point_set()+=to_add;return res;}
00171 MC_mesh_index_vector_draw operator-(const MC_mesh_index_vector_draw& vec,const MC_v3d& to_sub)
00172 {MC_mesh_index_vector_draw res=vec;res.point_set()-=to_sub;return res;}
00173 MC_mesh_index_vector_draw operator+(const MC_mesh_index_vector_draw& vec,const MC_v3d_vector& to_add)
00174 {MC_mesh_index_vector_draw res=vec;res.point_set()+=to_add;return res;}
00175 MC_mesh_index_vector_draw operator-(const MC_mesh_index_vector_draw& vec,const MC_v3d_vector& to_sub)
00176 {MC_mesh_index_vector_draw res=vec;res.point_set()-=to_sub;return res;}
00177
00178 MC_mesh_index_vector_draw operator+(const MC_v3d& to_add,const MC_mesh_index_vector_draw& vec)
00179 {return vec+to_add;}
00180 MC_mesh_index_vector_draw operator-(const MC_v3d& to_sub,const MC_mesh_index_vector_draw& vec)
00181 {return vec+to_sub;}
00182 MC_mesh_index_vector_draw operator+(const MC_v3d_vector& to_add,const MC_mesh_index_vector_draw& vec)
00183 {return vec+to_add;}
00184 MC_mesh_index_vector_draw operator-(const MC_v3d_vector& to_sub,const MC_mesh_index_vector_draw& vec)
00185 {return vec+to_sub;}
00186
00187 MC_mesh_index_vector_draw operator/(const MC_mesh_index_vector_draw& vec,const double& to_subdiv)
00188 {MC_mesh_index_vector_draw res=vec;res.point_set()/=to_subdiv;return res;}
00189
00190 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::operator+=(const MC_v3d& to_add)
00191 {
00192 point_set()+=to_add;
00193 return *this;
00194 }
00195
00196 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::operator-=(const MC_v3d& to_sub)
00197 {
00198 point_set()-=to_sub;
00199 return *this;
00200 }
00201 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::operator+=(const MC_v3d_vector& to_add)
00202 {
00203 point_set()+=to_add;
00204 return *this;
00205 }
00206 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::operator-=(const MC_v3d_vector& to_sub)
00207 {
00208 point_set()-=to_sub;
00209 return *this;
00210 }
00211 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::operator*=(const double& to_mult)
00212 {
00213 point_set()*=to_mult;
00214 return *this;
00215 }
00216 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::operator/=(const double& to_subdiv)
00217 {
00218 point_set()/=to_subdiv;
00219 return *this;
00220 }
00221
00222 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::normal_vertex_update()
00223 {
00224 int N=vertex_number();
00225 if(normal_mesh.size()!=N)
00226 normal_mesh.resize(N);
00227
00228 MC_v3d* p_normal=normal_mesh.pointer_unprotected();
00229 const MC_v3d* p_vertex=point_set().pointer();
00230 const MC_int_vector* p_connectivity=connectivity().pointer();
00231
00232 MC_v3d z(0,0,0);
00233 for(int k=0;k<N;++k)
00234 p_normal[k]=z;
00235
00236 int N_poly=polygon_number();
00237 for(int k=0;k<N_poly;++k)
00238 {
00239 MC_int_vector current_index=p_connectivity[k];
00240
00241 const MC_v3d* p_current_0=p_vertex+current_index[0];
00242 const MC_v3d* p_current_1=p_vertex+current_index[1];
00243 const MC_v3d* p_current_2=p_vertex+current_index[2];
00244
00245
00246
00247 MC_v3d u0=(*p_current_1-*p_current_0).normalized();
00248 MC_v3d u1=(*p_current_2-*p_current_0).normalized();
00249 MC_v3d n=u0.cross(u1).normalized();
00250
00251 for(int k_v=0,N_v=current_index.size();k_v<N_v;++k_v)
00252 {
00253 p_normal[current_index[k_v]] += n;
00254 }
00255
00256 }
00257 for(int k=0;k<N;++k)
00258 p_normal[k]=p_normal[k].normalized();
00259
00260 return *this;
00261 }
00262
00263 MC_mesh_index_vector_draw::MC_mesh_index_vector_draw(const MC_mesh_index_vector_draw& mesh)
00264 :MC_mesh_index_vector(mesh),normal_mesh(mesh.normal_mesh),color_mesh(mesh.color_mesh),texture_mesh(mesh.texture_mesh),texture_connectivity_mesh(mesh.texture_connectivity_mesh),alpha_mesh(mesh.alpha_mesh)
00265 {}
00266
00267
00268
00269 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::set_texture(const std::pair<MC_v3d_vector,MC_connectivity_index>& _texture)
00270 {
00271 texture()=_texture.first;
00272 texture_connectivity()=_texture.second;
00273 return *this;
00274 }
00275
00276 MC_mesh_index_vector_draw MC_mesh_index_vector_draw::load_mesh_file(const std::string& filename)
00277 {
00278 MC_mesh_index_vector_draw mesh;
00279
00280
00281 std::vector<std::string> token=MC_string_tokenizer::tokenize(filename,".");
00282 std::string extension=token[token.size()-1];
00283
00284 if(extension.find("off")!=std::string::npos)
00285 mesh=MC_mesh_index_vector_draw(MC_io_off::read_off_file(filename));
00286 else if(extension.find("obj")!=std::string::npos)
00287 {
00288 mesh=MC_mesh_index_vector_draw(MC_io_obj::read_obj_file(filename));
00289 mesh.set_texture(MC_io_obj::read_obj_texture_file(filename));
00290 }
00291 else
00292 std::cout<<"Warning in MC_mesh_index_vector_draw::load_mesh_file("<<filename<<"), extension "<<extension<<" not recognized"<<std::endl;
00293
00294 return mesh;
00295 }
00296
00297
00298 MC_mesh_index_vector_draw MC_mesh_index_vector_draw::subdivide_mixed_mid_edge(const std::set<int>& subdivided_polygon) const
00299 {
00300 MC_mesh_index_vector_draw mesh2;
00301 mesh2=MC_mesh_index_vector::subdivide_mixed_mid_edge(subdivided_polygon);
00302
00303 MC_mesh_index_vector mesh_tex(texture(),texture_connectivity());
00304 mesh_tex = mesh_tex.subdivide_mixed_mid_edge(subdivided_polygon);
00305
00306 mesh2.set_texture(std::make_pair(mesh_tex.point_set(),mesh_tex.connectivity()));
00307 return mesh2;
00308 }
00309 MC_mesh_index_vector_draw& MC_mesh_index_vector_draw::set_texture(const MC_mesh_index_vector& texture_mesh)
00310 {
00311 return set_texture(std::make_pair(texture_mesh.point_set(),texture_mesh.connectivity()));
00312 }
00313 }