解释代码for (Mesh::VertexIter v_it = mesh_.vertices_begin(); v_it != v_end; ++v_it)
时间: 2023-05-26 16:01:57 浏览: 56
这行代码使用了一个for循环来遍历一个Mesh对象中所有的顶点。具体来说,循环的初始化部分使用`mesh_.vertices_begin()`函数返回的迭代器作为起始值,循环条件使用`v_it != v_end`来判断当前迭代器是否达到循环的终止条件,循环体内部使用`v_it`来访问当前顶点的属性和操作。
相关问题
解释代码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; } / }
A:这段代码是一个函数"calc_valences()",通过迭代处理了一个Mesh模型中每一个顶点的度数,并将它们存储在每一个顶点的自定义属性中。具体实现方式是使用了内置的OpenMesh库提供的Mesh::VertexIter迭代器和VPropHandleT模板类,以及动态自定义属性。具体步骤是:首先创建一个自定义属性Valence,并添加到Mesh模型中;然后使用Mesh::VertexIter迭代器遍历每一个顶点,并通过Mesh::VertexVertexIter迭代器计算顶点的度数;最后将度数存储到自定义属性中,实现对每一个顶点的动态自定义属性。
将下列代码改为vs内可运行的代码:float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); valence += cot_weight; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }
请注意,代码中使用了 MyMesh 类型,需要先定义和引入该类型,以下是修改后的可运行代码:
```c++
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <vector>
typedef OpenMesh::TriMesh_ArrayKernelT<> MyMesh;
float smoothCot(MyMesh& mesh) {
float err = -1;
std::vector<MyMesh::Point> cogs;
cogs.reserve(mesh.n_vertices());
MyMesh::VertexIter v_it, v_end;
MyMesh::VertexVertexIter vv_it;
MyMesh::Point cog;
double valence;
v_end = mesh.vertices_end();
for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) {
cog[0] = cog[1] = cog[2] = valence = 0.0;
for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) {
double cot_weight = 0.0;
MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it);
if (!mesh.is_boundary(heh)) {
MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh);
MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh);
MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh);
MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh);
MyMesh::Point prev_p = mesh.point(prev_vh);
MyMesh::Point curr_p = mesh.point(*v_it);
MyMesh::Point next_p = mesh.point(next_vh);
double cot_alpha = cot(prev_p - curr_p, next_p - curr_p);
double cot_beta = cot(curr_p - prev_p, next_p - prev_p);
cot_weight = cot_alpha + cot_beta;
}
cog += cot_weight * mesh.point(*vv_it);
valence += cot_weight;
}
cogs.push_back(cog / valence);
}
std::vector<MyMesh::Point>::iterator cog_it = cogs.begin();
for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it, ++cog_it) {
if (!mesh.is_boundary(*v_it)) {
MyMesh::Point p = mesh.point(*v_it);
err = std::max(err, (p - *cog_it).norm());
mesh.set_point(*v_it, *cog_it);
}
}
return err;
}
int main(int argc, char **argv) {
MyMesh mesh;
if (!OpenMesh::IO::read_mesh(mesh, argv[1])) {
std::cerr << "read error\n";
return 1;
}
std::cout << "smoothCot error: " << smoothCot(mesh) << std::endl;
if (!OpenMesh::IO::write_mesh(mesh, "output.obj")) {
std::cerr << "write error\n";
return 1;
}
return 0;
}
```
其中,cot 函数需要自己实现,如下:
```c++
double cot(const MyMesh::Point& v1, const MyMesh::Point& v2) {
return std::tan(MyMesh::Angle(v1, v2));
}
```
请注意,以上代码仅供参考,可能存在错误或不足之处,需要结合具体情况进行修改和完善。