下列代码中的cot_alpha为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); cout << cot_alpha << "alpha" << endl; double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cout << cot_beta << "beta" << endl; 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); }
时间: 2024-03-23 20:37:49 浏览: 70
我写的代码,谁能帮我解决问题
3星 · 编辑精心推荐
在计算cot_alpha和cot_beta时,可能会出现除以0的情况,从而导致cot_alpha和cot_beta的值为无穷大或NaN。可以在计算前判断分母是否为0,如果为0则将cot_alpha和cot_beta的值设为一个较大的数,比如10000。代码示例如下:
```
double cot_alpha = 0.0;
if (prev_p != curr_pi && prev_p != curr_pj) {
cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p);
if (isnan(cot_alpha) || isinf(cot_alpha)) {
cot_alpha = 10000;
}
}
cout << cot_alpha << "alpha" << endl;
double cot_beta = 0.0;
if (next_p != curr_pi && next_p != curr_pj) {
cot_beta = cot(curr_pi - next_p, curr_pj - next_p);
if (isnan(cot_beta) || isinf(cot_beta)) {
cot_beta = 10000;
}
}
cout << cot_beta << "beta" << endl;
```
阅读全文