|

楼主 |
发表于 2025-3-18 15:03:17
|
显示全部楼层
方案 1: 使用 CGAL 库
CGAL 支持 Bezier 曲线、NURBS 曲线以及 Mesh 生成,适合高精度的 CAD 和 BIM 应用。
🔧 安装 CGAL
sudo apt-get install libcgal-dev
🔧 代码实现:Spline 生成回转体 Mesh
以下代码展示如何使用 CGAL 生成 Spline 曲线的旋转体并 Mesh 化:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Surface_mesh_simplification/edge_collapse.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Count_stop_predicate.h>
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <cmath>
#include <iostream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point;
typedef CGAL::Polyhedron_3<K> Polyhedron;
// B样条曲线 (Spline) 生成函数
std::vector<Point> generate_spline_curve(double a, double b, double step) {
std::vector<Point> points;
for (double t = 0; t <= 1.0; t += step) {
double x = a * std::cos(M_PI * t);
double y = a * std::sin(M_PI * t);
double z = b * t;
points.emplace_back(x, y, z);
}
return points;
}
// 旋转体生成函数
void generate_revolution_mesh(Polyhedron &mesh, const std::vector<Point> &curve, int segments) {
typedef Polyhedron::HalfedgeDS HalfedgeDS;
class Builder : public CGAL::Modifier_base<HalfedgeDS> {
private:
const std::vector<Point> &curve;
int segments;
public:
Builder(const std::vector<Point> &curve, int segments) : curve(curve), segments(segments) {}
void operator()(HalfedgeDS &hds) {
CGAL::Polyhedron_incremental_builder_3<HalfedgeDS> builder(hds, true);
builder.begin_surface(curve.size() * segments, segments * (curve.size() - 1) * 2);
// 顶点生成
for (int i = 0; i < segments; ++i) {
double angle = 2 * M_PI * i / segments;
double cos_angle = std::cos(angle);
double sin_angle = std::sin(angle);
for (const auto &p : curve) {
double x = p.x() * cos_angle;
double y = p.x() * sin_angle;
double z = p.z();
builder.add_vertex(Point(x, y, z));
}
}
// 生成面片 (四边形拼接)
for (int i = 0; i < segments; ++i) {
int next = (i + 1) % segments;
for (int j = 0; j < curve.size() - 1; ++j) {
int idx0 = i * curve.size() + j;
int idx1 = next * curve.size() + j;
int idx2 = next * curve.size() + (j + 1);
int idx3 = i * curve.size() + (j + 1);
builder.begin_facet();
builder.add_vertex_to_facet(idx0);
builder.add_vertex_to_facet(idx1);
builder.add_vertex_to_facet(idx2);
builder.add_vertex_to_facet(idx3);
builder.end_facet();
}
}
builder.end_surface();
}
};
Builder builder(curve, segments);
mesh.delegate(builder);
}
int main() {
Polyhedron mesh;
auto curve = generate_spline_curve(1.0, 2.0, 0.1); // 生成 B样条曲线
generate_revolution_mesh(mesh, curve, 32); // 生成回转体网格 (32 段)
std::cout << "生成顶点数量: " << mesh.size_of_vertices() << std::endl;
std::cout << "生成面片数量: " << mesh.size_of_facets() << std::endl;
// 输出 Mesh 顶点
for (auto v = mesh.vertices_begin(); v != mesh.vertices_end(); ++v) {
std::cout << "顶点: (" << v->point() << ")\n";
}
return 0;
}
代码说明
Spline 曲线生成:
使用简单的 B 样条曲线生成 (x, y, z) 点。
Mesh 生成:
以 Spline 曲线为母线,绕 Z 轴旋转生成 Mesh。
使用 CGAL 的 Polyhedron_incremental_builder_3 进行顶点和面片构建。 |
|