#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;
}