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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值