下列代码中的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 07:37:49 浏览: 73
在计算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;
```
相关问题
下列代码出现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; }
在这段代码中,出现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;
}
```
将下列代码改为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));
}
```
请注意,以上代码仅供参考,可能存在错误或不足之处,需要结合具体情况进行修改和完善。
阅读全文