CAD二次开发:objectarx + cgal 求凸包

求凸包视频

联系可加qq 3120210076


随机生成点方法

void Utils::RandomGeneratePoints(double xMin, double yMin, double xMax, double yMax, int pointNums)
{
    AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
    int times = 0;
    while (times < pointNums)
    {
        std::random_device rd;   // non-deterministic generator
        std::mt19937 gen(rd());  // to seed mersenne twister.
        std::uniform_real_distribution<double> u(0.00, 300.1); // 左闭右闭区间
        double x = u(gen);
        double y = u(gen);
        AcGePoint3d pos(x, y, 0);
        AcDbPoint* pPt = new AcDbPoint(pos);
        PostToModelSpace(pPt, pDb);
        pPt->close();
        times++;
    }
}

将实体插入到数据库

AcDbObjectId Utils::PostToModelSpace(AcDbEntity* pEnt, AcDbDatabase* pDb)
{
    assert(pEnt);
    //---获得当前图形数据库的块表---
    AcDbBlockTable* pBlkTbl = NULL;
    pDb->getBlockTable(pBlkTbl, AcDb::kForRead);
    //---获得模型空间对应的块表记录---
    AcDbBlockTableRecord* pBlkTblRcd = NULL;
    pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd, AcDb::kForWrite);
    pBlkTbl->close();
    //---将实体添加到模型空间的块表记录---
    AcDbObjectId entId;
    Acad::ErrorStatus es = pBlkTblRcd->appendAcDbEntity(entId, pEnt);
    if (es != Acad::eOk)
    {
        pBlkTblRcd->close();
        delete pEnt;
        pEnt = NULL;
        return AcDbObjectId::kNull;
    }
    pBlkTblRcd->close();
    pEnt->close();
    return entId;
}

获取随机点的凸包

#include "stdafx.h"
#include "Program.h"
#include "Utils.h"

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
#include <CGAL/Convex_hull_traits_adapter_2.h>
#include <CGAL/property_map.h>


typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef CGAL::Convex_hull_traits_adapter_2<K,
    CGAL::Pointer_property_map<Point_2>::type > Convex_hull_traits_2;

void Program::StartProgramCHA1()
{
    AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
    ads_point pt1, pt2;
    std::vector<AcDbEntity*> ents = Utils::GetEntitiesByTwoPoint(pt1, pt2);
    std::vector<Point_2> points;
    for (int i = 0; i < ents.size(); i++)
    {
        if (ents.at(i)->isKindOf(AcDbPoint::desc()))
        {
            AcDbPoint* pPt = AcDbPoint::cast(ents.at(i));
            points.push_back(Point_2(pPt->position().x, pPt->position().y));
        }
    }

    std::vector<std::size_t> indices(points.size()), out;
    std::iota(indices.begin(), indices.end(), 0);
    CGAL::convex_hull_2(indices.begin(), indices.end(), std::back_inserter(out),
        Convex_hull_traits_2(CGAL::make_property_map(points)));

    AcDbPolyline* pPoly = new AcDbPolyline();
    int time = 0;
    for (std::size_t i : out) {
        double x = points[i].x();
        double y = points[i].y();
        AcGePoint2d pos(x, y);
        pPoly->addVertexAt(time, pos);
        time++;
    }
    pPoly->setClosed(true);
    Utils::PostToModelSpace(pPoly, pDb);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值