生成球 使用openMesh 库
Posted eat-too-much
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成球 使用openMesh 库相关的知识,希望对你有一定的参考价值。
简介
使用openmesh生成一个球,采用的是标准球坐标系。
// 生成球 n 最好输入偶数 10 或者 100
#include <iostream>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include<cmath>
#define pi 3.1415926
using namespace std;
typedef OpenMesh::TriMesh_ArrayKernelT<> MyMesh;
int main()
MyMesh mesh;
int n,count=10000,k=-1;
cout << "n:";
cin >> n;
MyMesh::VertexHandle *vhandle = new MyMesh::VertexHandle[count];
std::vector<MyMesh::VertexHandle>face_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(MyMesh::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(MyMesh::Point(0, 0, 1));
vhandle[k+1] = mesh.add_vertex(MyMesh::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, "output5 .off"))
std::cerr << "Cannot write mesh to file ' output5 .off ' " << std::endl;
return 1;
catch (std::exception&x)
std::cerr << x.what() << std::endl;
return 1;
return 0;
以上是关于生成球 使用openMesh 库的主要内容,如果未能解决你的问题,请参考以下文章
使用 CMake 构建后,如何在 Visual Studio 中创建项目以使用 OpenMesh?
<2x1;OpenMesh译稿:使用并理解OpenMesh-OpenMesh的功能和目标
Openmesh:用 Python 比用 C++ 更快地更新面法线?