基于最新的openmesh库的流形修复各种算法示例
时间: 2024-03-05 11:48:27 浏览: 115
以下是基于OpenMesh库的流形修复的几个示例:
1. EdgeFlip算法示例:
```
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/getopt.h>
#include <iostream>
using namespace OpenMesh;
using namespace std;
struct MyTraits : public DefaultTraits
{
HalfedgeAttributes(OpenMesh::Attributes::PrevHalfedge);
};
typedef TriMesh_ArrayKernelT<MyTraits> MyMesh;
int main(int argc, char **argv)
{
MyMesh mesh;
// 读入网格
if (argc != 2 || !OpenMesh::IO::read_mesh(mesh, argv[1]))
{
std::cerr << "Usage: " << argv[0] << " <input>" << std::endl;
return 1;
}
// 检查是否为流形
if (!mesh.is_valid())
{
std::cerr << "Input mesh is not a manifold!" << std::endl;
return 1;
}
// EdgeFlip 算法
OpenMesh::EPropHandleT<bool> e_flip(mesh, "e_flip");
for (MyMesh::EdgeIter e_it = mesh.edges_begin(); e_it != mesh.edges_end(); ++e_it)
{
if (!mesh.is_flip_ok(*e_it))
mesh.property(e_flip, *e_it) = true;
}
while (mesh.flip_edges(e_flip)) {}
// 输出修复后的网格
if (!OpenMesh::IO::write_mesh(mesh, "output.off"))
{
std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
return 1;
}
return 0;
}
```
2. ExactGeodesics算法示例:
```
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/getopt.h>
#include <OpenMesh/Tools/Utils/Timer.hh>
#include <OpenMesh/Tools/Utils/StopWatch.hh>
#include <OpenMesh/Tools/MeshFix.hh>
#include <iostream>
#include <vector>
#include <string>
using namespace OpenMesh;
using namespace std;
struct MyTraits : public DefaultTraits
{
HalfedgeAttributes(OpenMesh::Attributes::PrevHalfedge);
};
typedef TriMesh_ArrayKernelT<MyTraits> MyMesh;
int main(int argc, char **argv)
{
MyMesh mesh;
// 读入网格
if (argc != 2 || !OpenMesh::IO::read_mesh(mesh, argv[1]))
{
std::cerr << "Usage: " << argv[0] << " <input>" << std::endl;
return 1;
}
// 检查是否为流形
if (!mesh.is_valid())
{
std::cerr << "Input mesh is not a manifold!" << std::endl;
return 1;
}
// ExactGeodesics算法
OpenMesh::VPropHandleT<MyMesh::Point> v_points;
mesh.add_property(v_points);
for (MyMesh::VertexIter v_it = mesh.vertices_begin(); v_it != mesh.vertices_end(); ++v_it)
{
mesh.property(v_points, *v_it) = mesh.point(*v_it);
}
OpenMesh::Utils::Timer timer;
timer.start();
OpenMesh::Utils::StopWatch watch;
watch.start();
OpenMesh::MeshFix<MyMesh> fixer(mesh);
fixer.repair_non_manifold_vertices(v_points, 1e-6);
std::cout << "ExactGeodesics took " << watch.stop() << " seconds." << std::endl;
// 输出修复后的网格
if (!OpenMesh::IO::write_mesh(mesh, "output.off"))
{
std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
return 1;
}
return 0;
}
```
3. SnapRound算法示例:
```
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/getopt.h>
#include <OpenMesh/Tools/Utils/Timer.hh>
#include <OpenMesh/Tools/Utils/StopWatch.hh>
#include <OpenMesh/Tools/MeshFix.hh>
#include <iostream>
#include <vector>
#include <string>
using namespace OpenMesh;
using namespace std;
struct MyTraits : public DefaultTraits
{
HalfedgeAttributes(OpenMesh::Attributes::PrevHalfedge);
};
typedef TriMesh_ArrayKernelT<MyTraits> MyMesh;
int main(int argc, char **argv)
{
MyMesh mesh;
// 读入网格
if (argc != 2 || !OpenMesh::IO::read_mesh(mesh, argv[1]))
{
std::cerr << "Usage: " << argv[0] << " <input>" << std::endl;
return 1;
}
// 检查是否为流形
if (!mesh.is_valid())
{
std::cerr << "Input mesh is not a manifold!" << std::endl;
return 1;
}
// SnapRound算法
OpenMesh::Utils::Timer timer;
timer.start();
OpenMesh::Utils::StopWatch watch;
watch.start();
OpenMesh::MeshFix<MyMesh> fixer(mesh);
fixer.repair_non_manifold_vertices(0.0, true);
std::cout << "SnapRound took " << watch.stop() << " seconds." << std::endl;
// 输出修复后的网格
if (!OpenMesh::IO::write_mesh(mesh, "output.off"))
{
std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
return 1;
}
return 0;
}
```
以上是基于OpenMesh库的流形修复的几个示例,您可以根据您的具体需求选择适合的算法进行应用。
阅读全文