高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
|
【转帖】计算几何中的凸包在arx中的实现
计算几何中的凸包在arx中的实现
计算几何中的凸包在arx中的实现
计算几何在很多方面有应用其重要性不在这里讨论。关于凸包的算法,有很多种,这里提供的算法是gramham的扫描法--一种比较经典的算法。
实现过程:
1、首先定义凸包点集类(动态类):对cad的点简化模型为point2d类。
然后对这个类的模型数学化,设计出求解的函数。
2、然后在cad对用户交互。
用户在cad中选择点(point)对象,这些点对象可能有很多个。
对这些点的位置用poin2d描述,并加入到一个点集类(动态类) 的对象中。
3、调用这个凸包对象,并对其求解。
4、得到凸包,最后转化到cad中,用polyline画出来。
因为代码较多,仅仅贴上了命令函数部分。其他的请参考附件。
static void convexhull_hull(void)
{
// add your code for command convexhull._hull here
//过滤选择cad的点(point)对象
ads_name sel;
resbuf filter;
filter.restype=0;
filter.resval.rstring=_t("point");
filter.rbnext = null;
int ret=acedssget(null,null,null,&filter,sel);
if( ret!=rtnorm)
return;
//得到选择的点数
long number;
acedsslength(sel,&number);
//如果选择的点数小于3,不构成凸包
if(number<3)
return;
//定义一个凸包对象,对其初始化
convex_hull pts;
acgepoint3d pt;
ads_name ent;
acdbobjectid objid;
acdbentity *pent;
for(int i = 0; i< number;i++)
{
acedssname(sel,i,ent);
acdbgetobjectid(objid,ent);
acdbopenobject(pent,objid,acdb::kforread);
pt=((acdbpoint*)pent)->position();
pts.add_point(pt.x,pt.y);
pent->close();
}
acutprintf(_t("\n你选择了%ld个点."),number);
//释放选择集,避免内存占用
acedssfree(sel);
//计时开始
resbuf starttime;
acedgetvar(_t("tdusrtimer"),&starttime);
//求解凸包
pts.solve();
//求解用时
gettimecost(starttime);
//剩下的画凸包用时
acedgetvar(_t("tdusrtimer"),&starttime);
//得到凸包的顶点数
int vexnum=pts.get_convex_points_count();
if(vexnum<3)
return;
point2d vertex;
//转化为acgepoint2darray;
acgepoint2darray points;
points.setlogicallength(vexnum);
for(int i= 0;i<vexnum;i++)
{
vertex=pts.get_convex_point(i);
//acutprintf(_t("\n第%d个点的坐标是: %lf,%lf."),i+1,vertex.x,vertex.y);
points[i].x=vertex.x;
points[i].y=vertex.y;
}
//画凸包
if(createpolyline(points)!=acad::eok)
acedalert(_t("创建凸包失败!"));
gettimecost(starttime);
acutprintf(_t("\n凸包创建完成!"));
return;
}
复制代码
对这个函数的运行速度跟lisp进行比较,这个比isp编译后的程序快10倍以上,如果点集愈多,则比它更快。
30万个点集合用arx处理,只需要2秒钟不到就能完成,但对于lisp可能要20-30秒。
下载次数:40
8-3-6 02:49 pm
谢谢楼主,正在学习这方面的
|