联系可加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);
}