下列代码出现nan int错误,请解决: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; //cout << valence<<"1" << endl; 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_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; //cout << cot_weight<<"2" << endl; } cog += cot_weight *( mesh.point(*vv_it)-mesh.point(*v_it)); valence += cot_weight; //cout << valence<<"3" << endl; } 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); //*cog_it += mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }
时间: 2024-03-23 20:37:36 浏览: 63
在这段代码中,出现nan和inf通常是由于除以0或开根号时计算负数引起的。你可以在运行代码之前添加一些检查,比如检查cot_weight是否为0,以避免除以0的情况。另外,你还可以在计算cot_alpha和cot_beta之前,添加一个检查,确保两个点之间的距离不为0,以避免计算负数的情况。具体来说,你可以修改代码如下:
```
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_pi = mesh.point(*v_it);
MyMesh::Point curr_pj = mesh.point(*vv_it);
MyMesh::Point next_p = mesh.point(next_vh);
double dist1 = (curr_pi - prev_p).norm();
double dist2 = (curr_pj - prev_p).norm();
double dist3 = (curr_pi - next_p).norm();
double dist4 = (curr_pj - next_p).norm();
if (dist1 > 0 && dist2 > 0 && dist3 > 0 && dist4 > 0) {
double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p);
double cot_beta = cot(curr_pi - next_p, curr_pj - next_p);
cot_weight = cot_alpha + cot_beta;
}
}
cog += cot_weight * (mesh.point(*vv_it) - mesh.point(*v_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;
}
```
阅读全文