请将下列代码改为可在vs中运行的版本:double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } //cot平滑 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; } void smoothCot(float threshold) { float err; do { err = smoothCot(); cout << "err:" << err << endl; } while (err >= threshold); }
时间: 2024-03-23 22:36:36 浏览: 151
ASDU中各关键词的含义-COT-103规约心得
这段代码需要引入MyMesh库,你需要提供该库的头文件和实现文件。同时,需要将该代码放到合适的函数中,比如main函数中。下面是改为可在VS中运行的版本:
```c++
#include <iostream>
#include "MyMesh.h" // MyMesh库的头文件
using namespace std;
MyMesh mesh; // 定义一个MyMesh对象
MyMesh::VertexIter v_it, v_end;
MyMesh::VertexVertexIter vv_it;
vector<MyMesh::Point> cogs;
vector<MyMesh::Point>::iterator cog_it;
// 计算cot值
double cot(MyMesh::Point a, MyMesh::Point b) {
return dot(a, b) / cross(a, b).norm();
}
// 平滑cot
float smoothCot() {
float err = -1;
cogs.clear();
v_end = mesh.vertices_end();
for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) {
MyMesh::Point cog(0, 0, 0); // 初始化为0
float 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;
}
// 迭代平滑cot值,直到err小于阈值
void smoothCot(float threshold) {
float err;
do {
err = smoothCot();
cout << "err:" << err << endl;
} while (err >= threshold);
}
int main() {
// 读取.obj文件,这里需要提供.obj文件的路径
OpenMesh::IO::read_mesh(mesh, "path/to/your/file.obj");
// 平滑cot值
smoothCot(0.1);
// 保存平滑后的模型到.obj文件,这里需要提供.obj文件的保存路径
OpenMesh::IO::write_mesh(mesh, "path/to/your/smoothed/file.obj");
return 0;
}
```
阅读全文