有两种所谓的最小外包矩形,第一种通过求所有节点的最小与最大xy来求的,这种叫与坐标轴平行的最小外包矩形;另外一种则是本文说的这种,与范围的形状与走势有关的,叫非坐标轴平行的最小外包矩形,效果如下图所示:

CGAL求最小外包矩形 > image2018-8-21_10-33-22.png

/// 这里做测试,只选中LWPOLYLINLE,也就是CAD中的多段线实体

struct resbuf* rb = ads_buildlist(RTDXF0, _T("LWPOLYLINE"), RTNONE);
ads_name ent, ssLines; if (RTNORM != ads_ssget(NULL, NULL, NULL, rb, ssLines))
{
ads_relrb(rb);
return;
}
ads_relrb(rb);
AcDbObjectId id;
Adesk::Int32 len = 0, l = 0;
ads_sslength(ssLines, &len);
std::vector<Point_2> nodesn,nodes_hull; ////////这里遍历选择集获取所有实体的节点
for (l = 0;l < len;l++)
{
ads_ssname(ssLines, l, ent);
if (Acad::eOk != acdbGetObjectId(id, ent))
{
continue;
}
AcDbEntity* pEnt = NULL;
if (acdbOpenAcDbEntity(pEnt, id, AcDb::kForRead) != Acad::eOk)
{
continue;
}
if (!pEnt->isKindOf(AcDbPolyline::desc()))
{
continue;
}
AcDbPolyline* pLine = AcDbPolyline::cast(pEnt);
AcGePoint3d ptS, ptE;
for (int idx = 0;idx < pLine->numVerts();idx++)
{
AcGePoint2d pt;
pLine->getPointAt(idx, pt);
nodesn.push_back(Point_2(pt.x, pt.y));
acutPrintf(_T("Random coordinate:%d: %.3lf %.3lf\n"), idx+1,pt.x, pt.y);
}
pEnt->close();
break;
}
ads_ssfree(ssLines);
///发现在求最小外接矩形的时候传入的点不能有坐标一直的点,而且点的方向必须是逆时针排列
///所以这里先求节点的凸包,然后通过Polygon_2来把非逆时针排列的节点纠正成逆时针
CGAL::convex_hull_2(nodesn.begin(), nodesn.end(), std::back_inserter(nodes_hull)); Polygon_2 p;
std::vector<Point_2>::iterator itn;
for (itn = nodes_hull.begin();itn != nodes_hull.end();itn++)
{
p.push_back(*itn);
}
AcGePoint2dArray nodes;
for (int i = 0;i < p.size();i++)
{
double x = CGAL::to_double(p.vertex(i).x());
double y = CGAL::to_double(p.vertex(i).y());
nodes.append(AcGePoint2d(x, y));
}
Common::DrawPolyline2D(nodes, true);
if (p.is_clockwise_oriented())
{
p.reverse_orientation();
}
Polygon_2 m_p;
CGAL::min_rectangle_2(p.vertices_begin(), p.vertices_end(), std::back_inserter(m_p));
Polygon_2::Vertex_const_iterator it;
nodes.removeAll();
for (it = m_p.vertices_begin(); it != m_p.vertices_end(); it++)
{
double x = CGAL::to_double((*it).x());
double y = CGAL::to_double((*it).y());
nodes.append(AcGePoint2d(x, y));
} Common::DrawPolyline2D(nodes, true);

CGAL求最小外包矩形的更多相关文章

  1. CAD在网页中返回当前图纸的最小外包矩形框

    主要用到函数说明: _DMxDrawX::GetMcDbDatabaseBound 返回当前图纸的最小外包矩形框,详细说明如下: 参数 说明 DOUBLE* pLbx 返回最小外包矩形框左下角X值 D ...

  2. TZOJ 2392 Bounding box(正n边形三点求最小矩形覆盖面积)

    描述 The Archeologists of the Current Millenium (ACM) now and then discover ancient artifacts located ...

  3. [hdu5251]矩形面积 旋转卡壳求最小矩形覆盖

    旋转卡壳求最小矩形覆盖的模板题. 因为最小矩形必定与凸包的一条边平行,则枚举凸包的边,通过旋转卡壳的思想去找到其他3个点,构成矩形,求出最小面积即可. #include<cstdio> # ...

  4. BZOJ 1185: [HNOI2007]最小矩形覆盖-旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标-备忘板子

    来源:旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标 BZOJ又崩了,直接贴一下人家的代码. 代码: #include"stdio.h" #include"str ...

  5. matlab练习程序(最小包围矩形)

    又是计算几何,我感觉最近对计算几何上瘾了. 当然,工作上也会用一些,不过工作上一般直接调用boost的geometry库. 上次写过最小包围圆,这次是最小包围矩形,要比最小包围圆复杂些. 最小包围矩形 ...

  6. UVA10173 Smallest Bounding Rectangle 最小面积矩形覆盖

    \(\color{#0066ff}{题目描述}\) 给定n(>0)二维点的笛卡尔坐标,编写一个程序,计算其最小边界矩形的面积(包含所有给定点的最小矩形). 输入文件可以包含多个测试样例.每个测试 ...

  7. poj 2185 Milking Grid(next数组求最小循环节)

    题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...

  8. Opencv绘制最小外接矩形、最小外接圆

    Opencv中求点集的最小外结矩使用方法minAreaRect,求点集的最小外接圆使用方法minEnclosingCircle. minAreaRect方法原型: RotatedRect minAre ...

  9. LeetCode939 最小面积矩形

    LeetCode939最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. Input [[1,1],[ ...

  10. opencv —— boundingRect、minAreaRect 寻找包裹轮廓的最小正矩形、最小斜矩形

    寻找包裹轮廓的最小正矩形:boundingRect 函数 返回矩阵应满足:① 轮廓上的点均在矩阵空间内.② 矩阵是正矩阵(矩形的边界与图像边界平行). Rect boundingRect(InputA ...

随机推荐

  1. 配置jupyter lab遇到 libffi.so.7缺失

    问题描述 使用 jupyter lab password 命令 配置jupyter 密码 报错 思路 去换一个conda环境 找到其lib内部的 libffi.so.7文件 解决方法 ln -s 其他 ...

  2. XCZU19EG板卡设计资料:610-基于6U VPX 的FPGA XCZU19EG存储阵列

    基于6U VPX 的FPGA XCZU19EG存储阵列 一.板卡概述         高性能存储板基于标准6U VPX架构,是基于Xilinx UltraScale+ 系列FPGA XCZU19EG架 ...

  3. ubuntu 22.04 网络配置ib网卡配置

    第一步:查看Ubuntu版本与内核版本 cat /etc/issue 这说明系统的版本为:Ubuntu 20.04.4 LTS \n \l uname -a Linux gacs-gm-11 5.4. ...

  4. mysql主从故障跳过错误

    mysql主从故障跳过错误1.从库报错 21,22,23,25无法执行Retrieved_Gtid_Set: 265c6c2a-86ca-11ed-b07a-0242ac120002:1-25Exec ...

  5. 使用Mockito 对方法中的私有void方法

    这里用一段代码举例 public void ruleJudgement(Long tenantId, Long productId, Long equipId, List<ModelAttrib ...

  6. 沁恒蓝牙系列芯片USB烧录故障排查

    目录 使用USB烧录时让ISP工具能够识别芯片的操作是按住评估板上的"Download"按键,或者将PB22短接到GND,同时给板子上电.若是第一次拿到芯片,codeflash中是 ...

  7. Jenkins Pipeline(一) - 创建一个新的pipeline

    jenkins pipeline: 使用Goovy语法,以代码形式更自由地编写jenkins job. 代码文本被称为Jenkinsfile IDE: vscode 推荐安装vscode插件: jen ...

  8. Android自动化用例编写

    java -jar appcrawler.jar<路径> --demo java -jar  appcrawler.jar<路径> \ -c example.yml \ --c ...

  9. allure-动态参数,报告优化方法。

    1.allure.title方法 #前置需要在源文件:\venv\Lib\site-packages\allure_pytest\listener.py#在该文件修改为这样:test_result.p ...

  10. 使用Libusb测试USB device

    一. 先准备好测试工具 -- Libusb: 在Linux中使用的话: 首先从 http://www.libusb.org/官网中下载libusb 然后解压之后./configure --> m ...