zoukankan      html  css  js  c++  java
  • 生成球 使用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;
    }
    
    
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    C# 简单的 Job 作业~
    反射反射,程序员的快乐+反射案例:打印和Excel导出
    设计模式:装饰模式(decorate)
    UML类图应该怎么看?
    WebApi 异步请求(HttpClient)
    设计模式:单一职责原则,开放封闭原则,依赖倒转原则,理氏代换原则
    【BOOM】一款有趣的Javascript动画效果
    【深入浅出jQuery】源码浅析2--奇技淫巧
    【深入浅出jQuery】源码浅析--整体架构
    【CSS进阶】原生JS getComputedStyle等方法解析
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/11134253.html
Copyright © 2011-2022 走看看