将下列代码改为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; }
时间: 2024-03-23 09:36:30 浏览: 84
请注意,代码中使用了 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));
}
```
请注意,以上代码仅供参考,可能存在错误或不足之处,需要结合具体情况进行修改和完善。
阅读全文