几何尺寸与公差论坛

 找回密码
 注册
查看: 248|回复: 3

如何用三角面片法离散圆柱表面?

  [复制链接]
发表于 2025-2-14 08:56:32 | 显示全部楼层 |阅读模式
遇到孔洞如何处理?
 楼主| 发表于 2025-3-18 14:17:38 | 显示全部楼层
将圆柱转成Mesh的基本思路

    几何参数设定:
        半径 R
        高度 H
        分段数 n(圆周上的分割数)
        高度上的层数 m

    顶点生成:
        底面圆上 n 个顶点
        顶面圆上 n 个顶点
        底面中心和顶面中心(用于封闭)

    网格生成:
        圆周相邻顶点间生成三角形或四边形面片。
        顶面和底面封闭网格。
 楼主| 发表于 2025-3-18 14:18:19 | 显示全部楼层
代码实现 1:使用CGAL库
如果你已经安装 CGAL,可以使用如下代码生成Mesh。
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <cmath>
#include <iostream>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point;
typedef CGAL::Surface_mesh<Point> Mesh;

// 生成圆柱Mesh
void generate_cylinder_mesh(Mesh &mesh, double radius, double height, int n_segments, int n_height) {
    double angle_step = 2 * M_PI / n_segments;
    double height_step = height / n_height;

    // 底面中心和顶面中心
    Mesh::Vertex_index bottom_center = mesh.add_vertex(Point(0, 0, 0));
    Mesh::Vertex_index top_center = mesh.add_vertex(Point(0, 0, height));

    // 生成圆周顶点
    std::vector<Mesh::Vertex_index> bottom_circle, top_circle;

    for (int i = 0; i < n_segments; ++i) {
        double theta = i * angle_step;
        double x = radius * cos(theta);
        double y = radius * sin(theta);

        // 底面和顶面圆周上的点
        bottom_circle.push_back(mesh.add_vertex(Point(x, y, 0)));
        top_circle.push_back(mesh.add_vertex(Point(x, y, height)));
    }

    // 连接底面
    for (int i = 0; i < n_segments; ++i) {
        int next_i = (i + 1) % n_segments;
        mesh.add_face(bottom_center, bottom_circle[next_i], bottom_circle);
    }

    // 连接顶面
    for (int i = 0; i < n_segments; ++i) {
        int next_i = (i + 1) % n_segments;
        mesh.add_face(top_center, top_circle, top_circle[next_i]);
    }

    // 连接侧面(生成四边形或两个三角形)
    for (int i = 0; i < n_segments; ++i) {
        int next_i = (i + 1) % n_segments;

        // 侧面分为上下两个三角形
        mesh.add_face(bottom_circle, bottom_circle[next_i], top_circle[next_i]);
        mesh.add_face(bottom_circle, top_circle[next_i], top_circle);
    }

    std::cout << "圆柱Mesh生成完成!顶点数量: " << mesh.num_vertices() << ",面片数量: " << mesh.num_faces() << std::endl;
}

int main() {
    Mesh mesh;
    double radius = 1.0;
    double height = 3.0;
    int n_segments = 32; // 圆周分割数
    int n_height = 1;    // 高度分割层数

    generate_cylinder_mesh(mesh, radius, height, n_segments, n_height);

    // 打印顶点和面片信息
    for (auto v : mesh.vertices()) {
        std::cout << "Vertex: " << mesh.point(v) << std::endl;
    }

    for (auto f : mesh.faces()) {
        std::cout << "Face: ";
        for (auto h : CGAL::halfedges_around_face(mesh.halfedge(f), mesh)) {
            std::cout << mesh.point(target(h, mesh)) << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}
 楼主| 发表于 2025-3-18 14:19:13 | 显示全部楼层
代码说明

    采用CGAL库的 Surface_mesh 数据结构生成Mesh。
    生成底面、顶面和侧面的顶点与面片。
    使用 add_face 方法添加三角形面片,实现Mesh生成。
    圆周分段数 n_segments 控制细节程度。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2025-4-2 05:41 , Processed in 0.038174 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表