本文主要是介绍xld曲线平滑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于halcon样条曲线的xld轮廓平滑
1,主要方法:根据轮廓点的角度判断是否需要平滑,由不需要平滑的点将轮廓分段平滑。
对于大角度的轮廓需要在角点周围新添加节点,以便更好的平滑。
*求角点--方法3--求当前点角度---先平滑轮廓后均分轮廓--精确
dev_update_off ()
dev_update_time ('off')
count_seconds (Seconds)
MaxCha:=0.01
JunV:=0.1
count_obj (Contours, Number)
x:=[]
y:=[]
* dev_update_on ()
limAng:=150
*所有角度
ang_re:=[]
*单个轮廓角点下标
indexics:=[]
*设定边长--角点角度小于该值直接平滑
dispp:=0.5
*限定长度
Dislimt:=1
*根据线长舍弃的角度下标
* m_indexs:=[]
*是否是闭合轮廓标志
isclose:=0
*离散精度
prece:=0.02
*需要删除的下标
m_delIndex:=[]
*平滑后轮廓
gen_empty_obj (EmptyObject2)
for Index4 := 1 to Number by 1select_obj (Contours, ObjectSelected1, 102)
* write_contour_xld_dxf (ObjectSelected1, 'C:/Users/zysk/Desktop/测试')length_xld (ObjectSelected1, Length)get_contour_xld (ObjectSelected1, Row2, Col2)if (Row2[0]==Row2[|Row2|-1]and(Col2[0]==Col2[|Col2|-1]))isclose:=1 elseisclose:=0endifif (|Row2|<=2)concat_obj (EmptyObject2, ObjectSelected1, EmptyObject2)continueendifindexics:=[]*角度在100-150度被舍弃的点需要重新设置权重m_angIndex:=[]m_angdegree:=[]ang_re:=[]*加入点个数m_indexnum:=0*是否需要去掉倒数第二个点isaddlastone := 0*添加后的点坐标Row:=[]Col:=[]for Index1 := 0 to |Row2|-2 by 1if (Index1==0)angle_ll (Row2[Index1], Col2[Index1], Row2[Index1+1], Col2[Index1+1], \Row2[Index1], Col2[Index1], Row2[|Row2|-2], Col2[|Row2|-2], ang1)
* gen_cross_contour_xld (Cross, Row2[|Row2|-1], Col2[|Row2|-1], 0.5, 0)
* gen_contour_polygon_xld (Contour6, [Row2[Index1], Row2[Index1+1]], [Col2[Index1], Col2[Index1+1]])elseangle_ll (Row2[Index1], Col2[Index1], Row2[Index1+1], Col2[Index1+1], \Row2[Index1], Col2[Index1], Row2[Index1-1], Col2[Index1-1], ang1)endifif ((Index1==0)or(Index1==|Row2|-1))if (isclose)elseang1:=3.14endifendifgen_cross_contour_xld (Cross, Row2[Index1], Col2[Index1], 0.5, 0)ang:=deg(ang1)ang_re:=[ang_re,ang]Isadd:=0if ((0<=abs(ang))and(abs(ang)<=100))Isadd:=1*当线长小于设定值时,去掉该点
* sheDinV:=0.03
* if (Index1==0)
* distance_pp (Row2[Index1], Col2[Index1], Row2[Index1+1], \Col2[Index1+1], Dis1)
* distance_pp (Row2[Index1], Col2[Index1], Row2[|Row2|-2], \Col2[|Row2|-2], Dis2)
* if ((sheDinV>Dis1)and(sheDinV>Dis2))
* m_delIndex:=[m_delIndex,Index1+m_indexnum]
* elseif(sheDinV>Dis1)
* m_delIndex:=[m_delIndex,Index1+1+m_indexnum]
* elseif((sheDinV>Dis2))
* isaddlastone:=1
* endif
* else
* distance_pp (Row2[Index1], Col2[Index1], Row2[Index1+1],\Col2[Index1+1], Dis1)
* distance_pp (Row2[Index1], Col2[Index1], Row2[Index1-1], \Col2[Index1-1], Dis2)
* if ((sheDinV>Dis1)and(sheDinV>Dis2))
* m_delIndex:=[m_delIndex,Index1+m_indexnum]
* elseif(sheDinV>Dis1)
* m_delIndex:=[m_delIndex,Index1+1+m_indexnum]
* elseif((sheDinV>Dis2))
* m_delIndex:=[m_delIndex,Index1-1+m_indexnum]
* endif
* endifelseif ((100<abs(ang))and(abs(ang)<limAng))if (Index1==0)distance_pp (Row2[Index1], Col2[Index1], Row2[Index1+1], \Col2[Index1+1], Dis1)distance_pp (Row2[Index1], Col2[Index1], Row2[|Row2|-2], \Col2[|Row2|-2], Dis2)if ((Dis1>dispp)and(Dis2>dispp))Isadd:=1else
* Wig:=((limAng-abs(ang))*2.0)*0.01
* if (Wig>0.3)
* m_angIndex:=[m_angIndex,Index1]
* m_angdegree:=[m_angdegree,Wig]
* endif*判断是否需要添加点dislim:=prece/cos(abs(ang1*0.5))if (Dis2*0.5>dislim)mCos:=(Col2[|Row2|-2]-Col2[Index1])/Dis2mSin:=(Row2[|Row2|-2]-Row2[Index1])/Dis2addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislimtuple_insert (Col2, |Row2|-1, addx, Col)tuple_insert (Row2, |Row2|-1, addy, Row)
* m_indexnum:=m_indexnum+1gen_cross_contour_xld (Cross, addy, addx, 0.05, 0)gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)gen_contour_polygon_xld (Contour5, Row, Col)endifif (Dis1*0.5>dislim)mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislimif (Col==[])tuple_insert (Col2, 1, addx, Col)tuple_insert (Row2, 1, addy, Row)m_indexnum:=m_indexnum+1elsetuple_insert (Col, 1, addx, Col)tuple_insert (Row, 1, addy, Row)m_indexnum:=m_indexnum+1 endifgen_cross_contour_xld (Cross, addy, addx, 0.05, 0)gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)gen_contour_polygon_xld (Contour5, Row, Col)endifendifelsedistance_pp (Row2[Index1], Col2[Index1], Row2[Index1+1],\Col2[Index1+1], Dis1)distance_pp (Row2[Index1], Col2[Index1], Row2[Index1-1], \Col2[Index1-1], Dis2)if ((Dis1>dispp)and(Dis2>dispp))Isadd:=1else
* Wig:=((limAng-abs(ang))*2.0)*0.01
* if (Wig>0.3)
* m_angIndex:=[m_angIndex,Index1]
* m_angdegree:=[m_angdegree,Wig]
* endif*判断是否需要添加点dislim:=prece/cos(abs(ang1*0.5))if (Dis2*0.5>dislim)mCos:=(Col2[Index1-1]-Col2[Index1])/Dis2mSin:=(Row2[Index1-1]-Row2[Index1])/Dis2addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislim* addxs:=[addxs,addx]
* addys:=[addys,addy]if (Col==[])tuple_insert (Col2, Index1+m_indexnum, addx, Col)tuple_insert (Row2, Index1+m_indexnum, addy, Row)m_indexnum:=m_indexnum+1elsetuple_insert (Col, Index1+m_indexnum, addx, Col)tuple_insert (Row, Index1+m_indexnum, addy, Row)m_indexnum:=m_indexnum+1 endif
* m_indexnums:=m_indexnums+1
* gen_cross_contour_xld (Cross, addy, addx, 0.05, 0)
* gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)
* gen_contour_polygon_xld (Contour5, Row, Col)endifif (Dis1*0.5>dislim)mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislimif (Col==[])tuple_insert (Col2, Index1+m_indexnum+1, addx, Col)tuple_insert (Row2, Index1+m_indexnum+1, addy, Row)m_indexnum:=m_indexnum+1elsetuple_insert (Col, Index1+m_indexnum+1, addx, Col)tuple_insert (Row, Index1+m_indexnum+1, addy, Row)m_indexnum:=m_indexnum+1 endif
* m_indexnums:=m_indexnums+1
* gen_cross_contour_xld (Cross, addy, addx, 0.05, 0)
* gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)
* gen_contour_polygon_xld (Contour5, Row, Col)endifendifendifelseif ((150<=abs(ang))and(abs(ang)<175))gen_cross_contour_xld (Cross, Row2[Index1], Col2[Index1], 0.08, 0)if (Index1==0)distance_pp (Row2[Index1], Col2[Index1], Row2[Index1+1], \Col2[Index1+1], Dis1)distance_pp (Row2[Index1], Col2[Index1], Row2[|Row2|-2], \Col2[|Row2|-2], Dis2)*判断是否需要添加点dislim:=prece/cos(abs(ang1*0.5))if (Dis2*0.5>dislim)mCos:=(Col2[|Row2|-2]-Col2[Index1])/Dis2mSin:=(Row2[|Row2|-2]-Row2[Index1])/Dis2addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislimtuple_insert (Col2, |Row2|-1, addx, Col)tuple_insert (Row2, |Row2|-1, addy, Row)* gen_cross_contour_xld (Cross, addy, addx, 0.05, 0)
* gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)
* gen_contour_polygon_xld (Contour5, Row, Col)endifif (Dis1*0.5>dislim)mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislimif (Col==[])tuple_insert (Col2, 1, addx, Col)tuple_insert (Row2, 1, addy, Row)m_indexnum:=m_indexnum+1elsetuple_insert (Col, 1, addx, Col)tuple_insert (Row, 1, addy, Row)m_indexnum:=m_indexnum+1 endif* gen_cross_contour_xld (Cross, addy, addx, 0.05, 0)
* gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)
* gen_contour_polygon_xld (Contour5, Row, Col)endifelsedistance_pp (Row2[Index1], Col2[Index1], Row2[Index1+1],\Col2[Index1+1], Dis1)distance_pp (Row2[Index1], Col2[Index1], Row2[Index1-1], \Col2[Index1-1], Dis2)*判断是否需要添加点dislim:=prece/cos(abs(ang1*0.5))if (Dis2*0.5>dislim)mCos:=(Col2[Index1-1]-Col2[Index1])/Dis2mSin:=(Row2[Index1-1]-Row2[Index1])/Dis2addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislim* addx:=Col2[Index1]-cos(abs(ang1*0.5))*prece
* addy:=Row2[Index1]+sin(abs(ang1*0.5))*preceif (Col==[])tuple_insert (Col2, Index1+m_indexnum, addx, Col)tuple_insert (Row2, Index1+m_indexnum, addy, Row)m_indexnum:=m_indexnum+1elsetuple_insert (Col, Index1+m_indexnum, addx, Col)tuple_insert (Row, Index1+m_indexnum, addy, Row)m_indexnum:=m_indexnum+1 endif
* m_indexnums:=m_indexnums+1gen_cross_contour_xld (Cross, addy, addx, 0.05, 0)
* gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)
* gen_contour_polygon_xld (Contour5, Row, Col)endifif (Dis1*0.5>dislim)mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1addx:=Col2[Index1]+mCos*dislimaddy:=Row2[Index1]+mSin*dislimif (Col==[])tuple_insert (Col2, Index1+m_indexnum+1, addx, Col)tuple_insert (Row2, Index1+m_indexnum+1, addy, Row)m_indexnum:=m_indexnum+1elsetuple_insert (Col, Index1+m_indexnum+1, addx, Col)tuple_insert (Row, Index1+m_indexnum+1, addy, Row)m_indexnum:=m_indexnum+1 endif
* m_indexnums:=m_indexnums+1
* gen_cross_contour_xld (Cross, addy, addx, 0.05, 0)
* gen_cross_contour_xld (Cross, Row[Index1+m_indexnum], Col[Index1+m_indexnum], 0.05, 0)
* gen_contour_polygon_xld (Contour5, Row, Col)endifendifendif* gen_cross_contour_xld (Cross, Row2[Index1], Col2[Index1], 0.08, 0)
*/添加序号有问题
* gen_cross_contour_xld (Cross, Row[indexics[3]], Col[indexics[3]], 0.08, 0)if (Isadd==1)x:=[x,Col2[Index1]]y:=[y,Row2[Index1]]if ((0<=abs(ang))and(abs(ang)<=100))indexics:=[indexics,Index1+m_indexnum]elseif (|indexics|>0)if (Index1-indexics[|indexics|-1]==1)distance_pp (Row2[Index1], Col2[Index1], Row2[Index1-1],\Col2[Index1-1], Dis1)if (Dis1>dispp)indexics:=[indexics,Index1+m_indexnum]elseif (abs(ang_re[Index1]<abs(ang_re[Index1-1])))indexics[|indexics|-1]:=Index1endifendifelseindexics:=[indexics,Index1+m_indexnum]endifelseindexics:=[indexics,Index1+m_indexnum]endifendifendifendfor
* continue*平滑等级degree:=0.1gen_cross_contour_xld (Cross, y, x, 0.05, 0)gen_cross_contour_xld (Cross, Row[m_delIndex], Col[m_delIndex], 0.05, 0)gen_cross_contour_xld (Cross, Row[indexics], Col[indexics], 0.5, 0)gen_cross_contour_xld (Cross, Row2, Col2, 0.5, 0)gen_cross_contour_xld (Cross, Row, Col, 0.05, 0)gen_contour_polygon_xld (Contour, Row, Col)* read_tuple ('C:/Users/zysk/Desktop/hv_m_delIndex.tup', Tuple)if (Row!=[])if (isaddlastone==1)m_delIndex:=[m_delIndex,|Row|-2]endiftuple_uniq (m_delIndex, m_delIndex)tuple_intersection (m_delIndex, indexics, Intersection)*删除指定坐标tuple_difference (indexics, Intersection, indexics)for Index5 := 1 to |m_delIndex| by 1mmIndex:=m_delIndex[|m_delIndex|-Index5]tuple_remove (Row, mmIndex, Row)tuple_remove (Col, mmIndex, Col)for Index7 := 0 to |indexics|-1 by 1if (mmIndex<=indexics[Index7])indexics[Index7]:=indexics[Index7]-1endifendforendfortuple_uniq (indexics, indexics)Row2:=RowCol2:=Colendif*生成需要平滑轮廓并处理if (|indexics|<=1)isok:=1if (|indexics|==1and(isclose==0))isok:=0endifelseisok:=0endif*平滑后的轮廓点坐标and(isclose)if (isok)**没有角点的轮廓
* m_angdegree:=[10,0.1,0.1,0.1,0.1]if ((|indexics|==1))if (indexics[0]!=0)tuple_gen_const (|Row2|, 0.1, wights)wights[m_angIndex]:= m_angdegree
* m_addwight:=(m_angdegree+0.1)/2
* wights[m_angIndex+1]:=m_addwight
* wights[m_angIndex-1]:=m_addwightm_Row:=[]m_Col:=[]wight:=[]tuple_gen_sequence (0, indexics[0], 1, Sequence)m_Row:=[m_Row,Row2[Sequence]]m_Col:=[m_Col,Col2[Sequence]]wight:=[wight,wights[Sequence]]tuple_gen_sequence (indexics[0], |Row2|-2, 1, Sequence)m_Row:=[Row2[indexics[0]]+0.000001,Row2[Sequence],m_Row]m_Col:=[Col2[indexics[0]]+0.000001,Col2[Sequence],m_Col]wight:=[0.1,wights[Sequence],wight]m_degree:=round(|m_Row|*0.05)if (m_degree<2)m_degree:= 2endifgen_contour_nurbs_xld (Contour, m_Row,m_Col, 'auto',\wight,2 , 0.001, 'omit')get_contour_xld (Contour, Row4, Col4)elsem_degree:=round(|Row2|*degree)if (m_degree<2)m_degree:= 2endifRow2[0]:=Row2[0]+0.000001Col2[0]:=Col2[0]+0.000001tuple_gen_const (|Row2|, 0.1, wights)wights[m_angIndex]:= m_angdegreegen_contour_nurbs_xld (Contour, Row2, Col2, 'auto',\wights,2 , 0.0005, 'omit')get_contour_xld (Contour, Row4, Col4)endif* gen_contour_polygon_xld (Contour5, Row4, Col4)elseif (isclose)tuple_gen_const (|Row2|-1, 0.1, wights)wights[m_angIndex]:= m_angdegreeelsetuple_gen_const (|Row2|, 0.1, wights)wights[m_angIndex]:= m_angdegreeendifm_degree:=round(|Row2|*degree)if (m_degree<2)m_degree:= 2endifgen_contour_nurbs_xld (Contour, Row2, Col2, 'auto',\'auto', 5 , 0.001 , 'omit')write_contour_xld_dxf (Contour, 'D:/项目/线平滑处理/图形/111.dxf')write_contour_xld_dxf (ObjectSelected1, 'D:/项目/线平滑处理/图形/222.dxf')get_contour_xld (Contour, Row4, Col4)endif
* write_contour_xld_dxf (Contour, 'C:/Users/zysk/Desktop/2.dxf')gen_cross_contour_xld (Cross, Row4, Col4, 0.1, 0)concat_obj (EmptyObject2, Contour, EmptyObject2)continueendifone_R:=[]one_C:=[]for Index6 := 0 to |indexics| by 1if (Index6==|indexics|)if(isclose)breakelsetuple_gen_sequence (indexics[|indexics|-1], |Row2|-1, 1, Sequence2)gen_cross_contour_xld (Cross, Row2[0], Col2[0], 0.5, 0)endifelseif (indexics[0]!=0and(Index6==0))if (Row2[0]==Row2[|Row2|-1]and(Col2[0]==Col2[|Col2|-1]))tuple_gen_sequence (indexics[|indexics|-1], |Row2|-1, 1, Sequence2)tuple_gen_sequence (1, indexics[0], 1, Sequence3)tuple_concat (Sequence2, Sequence3, Sequence2)
* gen_contour_polygon_xld (Contour3, Row2[Sequence2], Col2[Sequence2])elsetuple_gen_sequence (0, indexics[0], 1, Sequence2)endifelseif (indexics[0]!=0)tuple_gen_sequence (indexics[Index6-1], indexics[Index6], 1, Sequence2)elseif (Index6==|indexics|-1)tuple_gen_sequence (indexics[Index6], |Row2|-1, 1, Sequence2)elsetuple_gen_sequence (indexics[Index6], indexics[Index6+1], 1, Sequence2)endifendif
* gen_cross_contour_xld (Cross, Row2[0], Col2[0], 0.05, 0)endifendifif (|Sequence2|<=2)
* distance_pp (Row2[Sequence2[0]], Col2[Sequence2[0]], Row2[Sequence2[1]],\Col2[Sequence2[1]], Dis)
* contour_mean (Row2[Sequence2], Col2[Sequence2], JunV, Row_end, col_end)
* one_R:=[one_R,Row_end]
* one_C:=[one_C,col_end]one_R:=[one_R,Row2[Sequence2]]one_C:=[one_C,Col2[Sequence2]]gen_cross_contour_xld (Cross, one_R, one_C, 0.05, 0)continueendif
* gen_contour_nurbs_xld (Contour, Row2[Sequence2], Col2[Sequence2], 'auto', 'auto', 2, MaxCha, 'omit')if (|Sequence2|==3)wight:=[0.1,0.9,0.1]gen_contour_nurbs_xld (Contour, Row2[Sequence2], Col2[Sequence2], \'auto', wight, 2, 0.001,'omit')gen_cross_contour_xld (Cross, Row2[Sequence2], Col2[Sequence2], 0.5, 0)elsem_degree:=round(|Sequence2|*0.1)if (m_degree>|Sequence2|-1)m_degree:=|Sequence2|-1endifif (m_degree<2)m_degree:= 2endifif (m_degree>20)m_degree:=20endif
* gen_contour_polygon_xld (Contour6, Row2[Sequence2], Col2[Sequence2])
* a:=int((Length/|Row2|)*10)
* m_degree:=min([m_degree,a])gen_contour_nurbs_xld (Contour, Row2[Sequence2], Col2[Sequence2], \'auto', 'auto', m_degree, 'omit',0.1)endifget_contour_xld (Contour, Row, Col)
* gen_cross_contour_xld (Cross, Row2[Sequence2], Col2[Sequence2], 0.05, 0)
* gen_cross_contour_xld (Cross, Row4, Col4, 0.05, 0)one_R:=[one_R,Row]one_C:=[one_C,Col]endforgen_cross_contour_xld (Cross, one_R, one_C, 0.05, 0)gen_contour_polygon_xld (Contour3, one_R, one_C)concat_obj (EmptyObject2, Contour3, EmptyObject2)
* get_contour_angle_xld (Contour3, 'abs', 'range', 2, Angles)
* stop ()
endfor
count_seconds (Seconds1)
time:=Seconds1-Seconds
gen_cross_contour_xld (Cross, y, x, 0.5, 0)dev_display (Contours)
dev_display (Cross)
stop ()
平滑效果
这篇关于xld曲线平滑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!