其他分享
首页 > 其他分享> > 生成 一个多边形网格的函数

生成 一个多边形网格的函数

作者:互联网

简介

生成一个多边形网格的函数。

三角形面片判断法线方向

顺时针,表示平面方向向上。
逆时针,表示平面方向向下。

代码

// AddPolygon.cpp: 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#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;


MyMesh::VertexHandle* AddPolygon(MyMesh& mesh, int edge, double z, bool topOrBottom) {
    std::cout << edge << " " << z << " " << topOrBottom << std::endl;
    MyMesh::VertexHandle *vhandle = new MyMesh::VertexHandle[edge + 1];// 不知道为什么要多一个点,  对可能的原心
    for (int i = 0; i < edge; i++) {
        double x = 0.5*cos(pi * 2 / edge * i);
        double y = 0.5*sin(pi * 2 / edge * i);
        vhandle[i] = mesh.add_vertex(MyMesh::Point(x, y, z));
    }
    std::vector<MyMesh::VertexHandle>face_vhandles;
    int o = 0;
    switch (edge) {
    case 3:
        if (topOrBottom) {
            face_vhandles.clear();
            face_vhandles.push_back(vhandle[0]);
            face_vhandles.push_back(vhandle[1]);
            face_vhandles.push_back(vhandle[2]);
            mesh.add_face(face_vhandles);
        }
        else {
            face_vhandles.clear();
            face_vhandles.push_back(vhandle[0]);
            face_vhandles.push_back(vhandle[2]);
            face_vhandles.push_back(vhandle[1]);
            mesh.add_face(face_vhandles);
        }
        break;
    case 4:
        o = topOrBottom ? 3 : 1;
        face_vhandles.clear();
        face_vhandles.push_back(vhandle[0]);
        face_vhandles.push_back(vhandle[2]);
        face_vhandles.push_back(vhandle[o]);
        mesh.add_face(face_vhandles);
        face_vhandles.clear();
        face_vhandles.push_back(vhandle[2]);
        face_vhandles.push_back(vhandle[0]);
        face_vhandles.push_back(vhandle[(o+2)%4]);
        mesh.add_face(face_vhandles);
        break;
    default:
        vhandle[edge] = mesh.add_vertex(MyMesh::Point(0, 0, z));//加入圆心
        for (int i = 0; i < edge; i++) {//这里应该没什么问题
            int next = (i + 1) % edge;
            face_vhandles.clear();
            face_vhandles.push_back(vhandle[edge]);
            face_vhandles.push_back(vhandle[i]);
            face_vhandles.push_back(vhandle[next]);
            mesh.add_face(face_vhandles);
        }
        break;
    }
    return vhandle;
}

int main()
{
    MyMesh mesh;
    
    int edge;
    double z;
    bool topOrBottom;

    cout << "edge(int) z(double) topOrBottom(bool)" << std::endl;
    cin >> edge >> z >> topOrBottom;

    AddPolygon(mesh, edge, z, topOrBottom);
    try
    {
        if (!OpenMesh::IO::write_mesh(mesh, "output2 .off")) {
            std::cerr << "Cannot write mesh to file ' output2 .off ' " << std::endl;
            return 1;
        }
    }
    catch (std::exception&x) {
        std::cerr << x.what() << std::endl;
        return 1;
    }
    return 0;
}

标签:back,多边形,函数,vhandles,网格,mesh,face,vhandle,push
来源: https://www.cnblogs.com/eat-too-much/p/11138234.html