CGAL库基于正二十面体创建近似球体

#1 正二十面体生成(正二十面体的公式,可自行百度):

    Mesh mesh;
    //1 - 创建二十面体
    const double R = 1.0;
    const double m = sqrt(50 - 10 * sqrt(5)) / 10 * R;
    const double n = sqrt(50 + 10 * sqrt(5)) / 10 * R;
    //添加顶点:
    std::vector<Point> points = {
        Point(m,0,n),
        Point(m,0,-n),
        Point(-m,0,n),
        Point(-m,0,-n),
        Point(0,n,m),
        Point(0,-n,m),
        Point(0,n,-m),
        Point(0,-n,-m),
        Point(n,m,0),
        Point(-n,m,0),
        Point(n,-m,0),
        Point(-n,-m,0)
    };

    std::vector<std::vector<std::size_t>> faces = {
        {6, 4, 8}, {9, 4, 6}, {6, 3, 9}, {6, 1, 3}, {6, 8, 1},
        {8, 10, 1}, {8, 0, 10}, {8, 4, 0}, {4, 2, 0}, {4, 9, 2},
        {9, 11, 2}, {9, 3, 11}, {3, 1, 7}, {1, 10, 7}, {10, 0, 5},
        {0, 2, 5}, {2, 11, 5}, {3, 7, 11}, {5, 11, 7}, {10, 5, 7}
    };

#2 - Loop细分
这里迭代了四次:

//Loop细分
    for (int i = 0; i < 4; ++i) {
        CGAL::Subdivision_method_3::Loop_subdivision(mesh);
    }

源码:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Surface_mesh/IO/OFF.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
#include <CGAL/Subdivision_method_3/subdivision_methods_3.h>

#include <igl/readOFF.h>
#include <igl/opengl/glfw/Viewer.h>

#include <iostream>
#include <string>

Eigen::MatrixXd V;
Eigen::MatrixXi F;

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;

typedef K::Point_3     Point;
typedef K::Vector_3    Vector;

typedef CGAL::Surface_mesh<Point> Mesh;
typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<Mesh>::face_descriptor face_descripter;

namespace PMP = CGAL::Polygon_mesh_processing;

int main()
{
    Mesh mesh;
    //1 - 创建二十面体
    const double R = 1.0;
    const double m = sqrt(50 - 10 * sqrt(5)) / 10 * R;
    const double n = sqrt(50 + 10 * sqrt(5)) / 10 * R;
    //添加顶点:
    std::vector<Point> points = {
        Point(m,0,n),
        Point(m,0,-n),
        Point(-m,0,n),
        Point(-m,0,-n),
        Point(0,n,m),
        Point(0,-n,m),
        Point(0,n,-m),
        Point(0,-n,-m),
        Point(n,m,0),
        Point(-n,m,0),
        Point(n,-m,0),
        Point(-n,-m,0)
    };

    std::vector<std::vector<std::size_t>> faces = {
        {6, 4, 8}, {9, 4, 6}, {6, 3, 9}, {6, 1, 3}, {6, 8, 1},
        {8, 10, 1}, {8, 0, 10}, {8, 4, 0}, {4, 2, 0}, {4, 9, 2},
        {9, 11, 2}, {9, 3, 11}, {3, 1, 7}, {1, 10, 7}, {10, 0, 5},
        {0, 2, 5}, {2, 11, 5}, {3, 7, 11}, {5, 11, 7}, {10, 5, 7}
    };
 
    for (const auto& p : points)
        mesh.add_vertex(p);

    for (const auto& f : faces)
        mesh.add_face(Mesh::Vertex_index(f[0]), Mesh::Vertex_index(f[1]), Mesh::Vertex_index(f[2]));

    // Compute normals
    auto vnormals = mesh.add_property_map<vertex_descriptor, Vector>("v:normals", CGAL::NULL_VECTOR).first;
    auto fnormals = mesh.add_property_map<face_descripter, Vector>("f:normals", CGAL::NULL_VECTOR).first;


    PMP::compute_normals(mesh,vnormals,fnormals);

    // Access vertices and faces
    for (auto v : mesh.vertices())
    {
        std::cout << mesh.point(v) << std::endl;
        std::cout << "Vertex normals:" << vnormals[v] << std::endl;
    }
    std::cout << "Face normals :" << std::endl;
    for (auto f : mesh.faces())
    {
        std::cout << fnormals[f] << std::endl;
    }

    //Loop细分
    for (int i = 0; i < 4; ++i) {
        CGAL::Subdivision_method_3::Loop_subdivision(mesh);
    }

    const std::string file_saved = "test1.off";
    if (!CGAL::write_off(mesh, file_saved))
    {
        std::cout << "Write OFF file Error!" << std::endl;
    }
    igl::readOFF(file_saved, V, F);
     Plot the mesh
    igl::opengl::glfw::Viewer viewer;
    viewer.data().set_mesh(V, F);
    //viewer.data().set_points(V_p, Eigen::RowVector3d(1, 0, 0));
    //viewer.data().point_size = 1.0;
    viewer.launch();
    return 0;
}
### 使用CGAL创建多面C++中使用CGAL创建多面涉及多个步骤,包括定义几何内核、设置顶点和边的数据结构以及实际构建多面对象。下面是一个完整的例子来展示如何实现这一点。 #### 定义必要的头文件并引入命名空间 为了简化代码编写过程,通常会先导入所需的全部标准模板(STL)功能以及其他辅助函数[^1]: ```cpp #include <iostream> #include <CGAL/Simple_cartesian.h> #include <CGAL/Polyhedron_3.h> using namespace std; ``` 这里选择了`Simple_cartesian`作为默认的笛卡尔坐标系下的浮点数类型用于表示三维空间中的点和其他几何;而`Polyhedron_3`则是用来存储一个多面数据结构的对象类。 #### 创建的多面实例 接下来可以利用上述定义好的组件去建立具形状的多面模型。例如,要创建一个立方,则可以通过指定其八个角上的位置信息来进行初始化操作[^2]: ```cpp typedef CGAL::Simple_cartesian<double> Kernel; typedef Kernel::Point_3 Point_3; typedef CGAL::Polyhedron_3<Kernel> Polyhedron; int main() { // Define the eight vertices of a cube. Point_3 p[] = { Point_3(-1,-1,-1), Point_3( 1,-1,-1), Point_3( 1, 1,-1), Point_3(-1, 1,-1), Point_3(-1,-1, 1), Point_3( 1,-1, 1), Point_3( 1, 1, 1), Point_3(-1, 1, 1) }; // Create an empty polyhedron object. Polyhedron P; // Add facets (faces) one by one into this polyhedron. int indices[][4]= {{0,1,2,3},{4,5,6,7}, {0,1,5,4},{1,2,6,5}, {2,3,7,6},{3,0,4,7}}; for(int i=0;i<6;++i){ typename Polyhedron::Facet_iterator f_it=P.facets_begin(); while(f_it!=P.facets_end()) ++f_it; P.add_facet(p[indices[i][0]],p[indices[i][1]], p[indices[i][2]],p[indices[i][3]]); } cout << "A cube has been created."<< endl; } ``` 这段程序展示了怎样通过给定一组顶点的位置向量,在此基础上依次添加各个表面从而最终形成封闭的空间图形——即所谓的“多面”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值