本帖最后由 枫叶棋语 于 2025-6-28 12:31 编辑
 - void createDT() {
- setFocusDoc();
- // 使用最快的Epick内核(现代C++语法)
- using Kernel = CGAL::Epick;
- using CGAL_Point = Kernel::Point_2;
- using Delaunay = CGAL::Delaunay_triangulation_2<Kernel>;
- DbTrans tr;
- auto btr = tr.currentSpace();
- int count = 1000;
- auto res = MapleGetInt();
- if (res.Ok) {
- count = res.Value;
- }
- // 生成随机点集
- std::vector<CGAL_Point> points;
- points.reserve(count + 1); // 预分配内存
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_real_distribution<> dis(0.0, 10000.0);
- for (int i = 0; i <= count; ++i) {
- points.emplace_back(dis(gen), dis(gen));
- }
- // 在CAD中绘制点
- for (const auto& pt : points) {
- AcDbPoint* dbpoint = new AcDbPoint({ pt.x(), pt.y(), 0 });
- dbpoint->setColorIndex(1);
- tr.addEntity(btr, dbpoint);
- }
- // CGAL三角剖分(带空间排序优化)
- TimeSpan span; //计时
- CGAL::spatial_sort(points.begin(), points.end()); // 加速关键步骤
- Delaunay dt(points.begin(), points.end());
- span.hasRuntime(L"CGAL 三角剖分耗时:"); //打印计时
- // 绘制三角形
- for (auto face = dt.finite_faces_begin(); face != dt.finite_faces_end(); ++face) {
- AcDbPolyline* poly = new AcDbPolyline();
- poly->setColorIndex(3);
- poly->setClosed(true);
- // 添加三角形三个顶点
- for (int i = 0; i < 3; ++i) {
- auto& pt = face->vertex(i)->point();
- poly->addVertexAt(i, { pt.x(), pt.y() });
- }
- tr.addEntity(btr, poly);
- }
- }
|