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

简介

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

三角形面片判断法线方向

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

代码

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

//#include "stdafx.h"
#include 
#include 
#include 
#include 
#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::vectorface_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++) {//这里应该没什么问题
            o = topOrBottom ? (i+1)%edge : (i+edge-1)%edge; // 当为 top 时  逆时针 法线方向向上
                                                            // 当为 Bottom 时 顺时针 ,法线方向朝下??
            
            face_vhandles.clear();
            face_vhandles.push_back(vhandle[edge]);
            face_vhandles.push_back(vhandle[i]);
            face_vhandles.push_back(vhandle[o]);
            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;
}

转载于:https://www.cnblogs.com/eat-too-much/p/11138234.html

你可能感兴趣的:(生成 一个多边形网格的函数)