内容出自236329 - Digital Geometry Processing, Spring2013 - Announcements上的OpenMesh Assignment感觉比较好,适合刚开始学习的同学。
练习要求:
这次的练习主要是写两个函数,calc_valences()和color_coding(),函数主要完成了计算一个Vertex的valence以及根据这个valence给图形上色,其中vertex的valence是保存在customer的property中。
关键代码:
写的时候主要在两个地方出现问题,一个是将customer的property保存在mesh_上之后怎么找到PropertyHandle,以及代码的效率有点低,没有充分利用OpenMeshSTL的威力。
添加Property的时候增加一个Property的名字,再通过名字返回PropertyHandle像这样,然后根据valence的值给Mesh上色。
mesh_.add_property(Valence,"Valence");
void ValenceViewer::calc_valences() { // EXERCISE 1.2 ///////////////////////////////////////////////////////////// // Compute valence of every vertex of "mesh_" and store them in each vertex // using for example custom attributes via dynamic customization // (hint: use the Mesh::VertexIter iterator) // Implement something here Mesh::VertexIter v_end=mesh_.vertices_end(); //Mesh::Scalar TempValence=0; OpenMesh::VPropHandleT<int> Valence; mesh_.add_property(Valence,"Valence"); for (Mesh::VertexIter v_it=mesh_.vertices_begin();v_it!=v_end;++v_it) { mesh_.property(Valence,v_it); for (Mesh::VertexVertexIter vv_it=mesh_.vv_iter(v_it);vv_it;++vv_it) { mesh_.property(Valence,v_it)+=1; } //cout<<mesh_.property(Valence,v_it)<<endl; } ///////////////////////////////////////////////////////////////////////////// } //----------------------------------------------------------------------------- void ValenceViewer::color_coding() { // EXERCISE 1.3 ///////////////////////////////////////////////////////////// // Implement a color visualization of your choice that shows the valence of // each vertex of "mesh_". // (hint: use Mesh::Color color type) // Implement something here Mesh::VertexIter v_end=mesh_.vertices_end(); //OpenMesh::VPropHandleT<int> Valence; Mesh::Color Green= Mesh::Color(0,255,0); Mesh::Color Red=Mesh::Color(255,0,0); Mesh::Color Blue=Mesh::Color(0,0,255); int Valence; OpenMesh::VPropHandleT<int> vPH; for (Mesh::VertexIter v_it=mesh_.vertices_begin();v_it!=v_end;++v_it) { mesh_.get_property_handle(vPH,"Valence"); Valence=mesh_.property(vPH,v_it.handle()); if (Valence>=4&&Valence<6) { mesh_.set_color(v_it.handle(),Blue); } else if (Valence>=7) { mesh_.set_color(v_it.handle(),Red); } else { mesh_.set_color(v_it.handle(),Green); } //mesh_.set_color(v_it.handle(),Green); } cout<<"Edge:"<<mesh_.n_edges()<<"Face"<<mesh_.n_faces()<<"Vertex"<<mesh_.n_vertices()<<endl; ///////////////////////////////////////////////////////////////////////////// }