安装
环境 win10,qt5
源码下载编译
进入OpenMesh官网OpenMesh官网 https://www.graphics.rwth-aachen.de/software/openmesh/download/
使用cmake gui
注意:先安装qt5
-
使用 CMake-Gui 构建 vs 2019 项目
- 注意 where is the source code 是<project 路径>,下边的路径是 <project 路径>/build
- 首次点击 configure 默认就是 vs2019 和 x64,这两者都是必要的
第一次configure 会报错,需要你填写QT5_DIR,如上图
然后再次点击configure
接着点击 generate
然后用vs2019打开项目,生成
Hello World
vs2019 新建一个工程
第一步会生成lib,我的在这里
需要的头文件在这里
配置工程属性
添加附加库目录OpenMeshCored.lib和 OpenMeshToolsd.lib
打开项目属性-V/c+±预处理器,添加附加宏_USE_MATH_DEFINES
代码
#include
// -------------------- OpenMesh
#include <Core/IO/MeshIO.hh>
#include <Core/Mesh/PolyMesh_ArrayKernelT.hh>
using namespace std;
typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
int main()
{
MyMesh mesh;
MyMesh::VertexHandle vhandle[8];
vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1));
vhandle[1] = mesh.add_vertex(MyMesh::Point(1, -1, 1));
vhandle[2] = mesh.add_vertex(MyMesh::Point(1, 1, 1));
vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1, 1));
vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
vhandle[5] = mesh.add_vertex(MyMesh::Point(1, -1, -1));
vhandle[6] = mesh.add_vertex(MyMesh::Point(1, 1, -1));
vhandle[7] = mesh.add_vertex(MyMesh::Point(-1, 1, -1));
// generate (quadrilateral) faces
std::vectorMyMesh::VertexHandle face_vhandles;
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[3]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[4]);
face_vhandles.push_back(vhandle[5]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[6]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[7]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
// write mesh to output.obj
try
{
if (!OpenMesh::IO::write_mesh(mesh, "output.off"))
{
std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
return 1;
}
}
catch (std::exception& x)
{
std::cerr << x.what() << std::endl;
return 1;
}
return 0;
}
输出 "output.off 用meshlab打开
球的代码
#pragma once
// 生成球 n 最好输入偶数 10 或者 100
#include
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include
#define pi 3.1415926
using namespace std;
typedef OpenMesh::TriMesh_ArrayKernelT<> sphereMesh;
void sphereDemo()
{
sphereMesh mesh;
int n, count = 10000, k = -1;
cout << “n:”;
cin >> n;
sphereMesh::VertexHandle* vhandle = new sphereMesh::VertexHandle[count];
std::vectorsphereMesh::VertexHandleface_vhandles;
for (int j = 0; j < n; j++) {
double distance = cos(j * pi / n);// 原先是 sin
double r_circle = sin(j * pi / n);
for (int i = 0; i < n; i++) {
++k;
vhandle[k] = mesh.add_vertex(sphereMesh::Point(r_circle * cos(2 * i * pi / n), r_circle * sin(2 * i * pi / n), distance));
// 对网格添加顶点
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n; j++) {
int topRight = i * n + j;
int topLeft = i * n + (j + 1) % n;
int bottomRight = (i + 1) * n + j;
int bottomLeft = (i + 1) * n + (j + 1) % n;
face_vhandles.clear();
face_vhandles.push_back(vhandle[bottomRight]);
face_vhandles.push_back(vhandle[bottomLeft]);
face_vhandles.push_back(vhandle[topRight]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[bottomLeft]);
face_vhandles.push_back(vhandle[topLeft]);
face_vhandles.push_back(vhandle[topRight]);
mesh.add_face(face_vhandles);
}
}
//vhandle[k+1] = mesh.add_vertex(sphereMesh::Point(0, 0, 1));
vhandle[k + 1] = mesh.add_vertex(sphereMesh::Point(0, 0, -1));
for (int i = 0; i < n; i++) {
//face_vhandles.clear();
//
//
//face_vhandles.push_back(vhandle[i]);
//face_vhandles.push_back(vhandle[(i + 1) % n]);
//face_vhandles.push_back(vhandle[k + 2]);
//mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[k - i]);
face_vhandles.push_back(vhandle[k - (i + 1) % n]);
face_vhandles.push_back(vhandle[k + 1]);
mesh.add_face(face_vhandles);
}
try
{
if (!OpenMesh::IO::write_mesh(mesh, "sphereDemo .off")) {
std::cerr << "Cannot write mesh to file ' output5 .off ' " << std::endl;
return ;
}
}
catch (std::exception& x) {
std::cerr << x.what() << std::endl;
return ;
}
}
参考
https://blog.csdn.net/qq_43331089/article/details/125087965#%E5%BF%83%E5%BE%97
meshlab使用教程: https://blog.csdn.net/qq_15262755/article/details/80352867