基于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 1
select_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
else
isclose:=0
endif
if (|Row2|<=2)
concat_obj (EmptyObject2, ObjectSelected1, EmptyObject2)
continue
endif
indexics:=[]
*角度在100-150度被舍弃的点需要重新设置权重
m_angIndex:=[]
m_angdegree:=[]
ang_re:=[]
*加入点个数
m_indexnum:=0
*是否需要去掉倒数第二个点
isaddlastone := 0
*添加后的点坐标
Row:=[]
Col:=[]
for Index1 := 0 to |Row2|-2 by 1
if (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]])
else
angle_ll (Row2[Index1], Col2[Index1], Row2[Index1+1], Col2[Index1+1], \
Row2[Index1], Col2[Index1], Row2[Index1-1], Col2[Index1-1], ang1)
endif
if ((Index1==0)or(Index1==|Row2|-1))
if (isclose)
else
ang1:=3.14
endif
endif
gen_cross_contour_xld (Cross, Row2[Index1], Col2[Index1], 0.5, 0)
ang:=deg(ang1)
ang_re:=[ang_re,ang]
Isadd:=0
if ((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
* endif
elseif ((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:=1
else
* 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])/Dis2
mSin:=(Row2[|Row2|-2]-Row2[Index1])/Dis2
addx:=Col2[Index1]+mCos*dislim
addy:=Row2[Index1]+mSin*dislim
tuple_insert (Col2, |Row2|-1, addx, Col)
tuple_insert (Row2, |Row2|-1, addy, Row)
* m_indexnum:=m_indexnum+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)
endif
if (Dis1*0.5>dislim)
mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1
mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1
addx:=Col2[Index1]+mCos*dislim
addy:=Row2[Index1]+mSin*dislim
if (Col==[])
tuple_insert (Col2, 1, addx, Col)
tuple_insert (Row2, 1, addy, Row)
m_indexnum:=m_indexnum+1
else
tuple_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)
endif
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 ((Dis1>dispp)and(Dis2>dispp))
Isadd:=1
else
* 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])/Dis2
mSin:=(Row2[Index1-1]-Row2[Index1])/Dis2
addx:=Col2[Index1]+mCos*dislim
addy:=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+1
else
tuple_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)
endif
if (Dis1*0.5>dislim)
mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1
mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1
addx:=Col2[Index1]+mCos*dislim
addy:=Row2[Index1]+mSin*dislim
if (Col==[])
tuple_insert (Col2, Index1+m_indexnum+1, addx, Col)
tuple_insert (Row2, Index1+m_indexnum+1, addy, Row)
m_indexnum:=m_indexnum+1
else
tuple_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)
endif
endif
endif
elseif ((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])/Dis2
mSin:=(Row2[|Row2|-2]-Row2[Index1])/Dis2
addx:=Col2[Index1]+mCos*dislim
addy:=Row2[Index1]+mSin*dislim
tuple_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)
endif
if (Dis1*0.5>dislim)
mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1
mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1
addx:=Col2[Index1]+mCos*dislim
addy:=Row2[Index1]+mSin*dislim
if (Col==[])
tuple_insert (Col2, 1, addx, Col)
tuple_insert (Row2, 1, addy, Row)
m_indexnum:=m_indexnum+1
else
tuple_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)
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)
*判断是否需要添加点
dislim:=prece/cos(abs(ang1*0.5))
if (Dis2*0.5>dislim)
mCos:=(Col2[Index1-1]-Col2[Index1])/Dis2
mSin:=(Row2[Index1-1]-Row2[Index1])/Dis2
addx:=Col2[Index1]+mCos*dislim
addy:=Row2[Index1]+mSin*dislim
* addx:=Col2[Index1]-cos(abs(ang1*0.5))*prece
* addy:=Row2[Index1]+sin(abs(ang1*0.5))*prece
if (Col==[])
tuple_insert (Col2, Index1+m_indexnum, addx, Col)
tuple_insert (Row2, Index1+m_indexnum, addy, Row)
m_indexnum:=m_indexnum+1
else
tuple_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)
endif
if (Dis1*0.5>dislim)
mCos:=(Col2[Index1+1]-Col2[Index1])/Dis1
mSin:=(Row2[Index1+1]-Row2[Index1])/Dis1
addx:=Col2[Index1]+mCos*dislim
addy:=Row2[Index1]+mSin*dislim
if (Col==[])
tuple_insert (Col2, Index1+m_indexnum+1, addx, Col)
tuple_insert (Row2, Index1+m_indexnum+1, addy, Row)
m_indexnum:=m_indexnum+1
else
tuple_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)
endif
endif
endif
* 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]
else
if (|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]
else
if (abs(ang_re[Index1]<abs(ang_re[Index1-1])))
indexics[|indexics|-1]:=Index1
endif
endif
else
indexics:=[indexics,Index1+m_indexnum]
endif
else
indexics:=[indexics,Index1+m_indexnum]
endif
endif
endif
endfor
* continue
*平滑等级
degree:=0.1
gen_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]
endif
tuple_uniq (m_delIndex, m_delIndex)
tuple_intersection (m_delIndex, indexics, Intersection)
*删除指定坐标
tuple_difference (indexics, Intersection, indexics)
for Index5 := 1 to |m_delIndex| by 1
mmIndex:=m_delIndex[|m_delIndex|-Index5]
tuple_remove (Row, mmIndex, Row)
tuple_remove (Col, mmIndex, Col)
for Index7 := 0 to |indexics|-1 by 1
if (mmIndex<=indexics[Index7])
indexics[Index7]:=indexics[Index7]-1
endif
endfor
endfor
tuple_uniq (indexics, indexics)
Row2:=Row
Col2:=Col
endif
*生成需要平滑轮廓并处理
if (|indexics|<=1)
isok:=1
if (|indexics|==1and(isclose==0))
isok:=0
endif
else
isok:=0
endif
*平滑后的轮廓点坐标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_addwight
m_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:= 2
endif
gen_contour_nurbs_xld (Contour, m_Row,m_Col, 'auto',\
wight,2 , 0.001, 'omit')
get_contour_xld (Contour, Row4, Col4)
else
m_degree:=round(|Row2|*degree)
if (m_degree<2)
m_degree:= 2
endif
Row2[0]:=Row2[0]+0.000001
Col2[0]:=Col2[0]+0.000001
tuple_gen_const (|Row2|, 0.1, wights)
wights[m_angIndex]:= m_angdegree
gen_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)
else
if (isclose)
tuple_gen_const (|Row2|-1, 0.1, wights)
wights[m_angIndex]:= m_angdegree
else
tuple_gen_const (|Row2|, 0.1, wights)
wights[m_angIndex]:= m_angdegree
endif
m_degree:=round(|Row2|*degree)
if (m_degree<2)
m_degree:= 2
endif
gen_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)
continue
endif
one_R:=[]
one_C:=[]
for Index6 := 0 to |indexics| by 1
if (Index6==|indexics|)
if(isclose)
break
else
tuple_gen_sequence (indexics[|indexics|-1], |Row2|-1, 1, Sequence2)
gen_cross_contour_xld (Cross, Row2[0], Col2[0], 0.5, 0)
endif
else
if (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])
else
tuple_gen_sequence (0, indexics[0], 1, Sequence2)
endif
else
if (indexics[0]!=0)
tuple_gen_sequence (indexics[Index6-1], indexics[Index6], 1, Sequence2)
else
if (Index6==|indexics|-1)
tuple_gen_sequence (indexics[Index6], |Row2|-1, 1, Sequence2)
else
tuple_gen_sequence (indexics[Index6], indexics[Index6+1], 1, Sequence2)
endif
endif
* gen_cross_contour_xld (Cross, Row2[0], Col2[0], 0.05, 0)
endif
endif
if (|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)
continue
endif
* 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)
else
m_degree:=round(|Sequence2|*0.1)
if (m_degree>|Sequence2|-1)
m_degree:=|Sequence2|-1
endif
if (m_degree<2)
m_degree:= 2
endif
if (m_degree>20)
m_degree:=20
endif
* 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)
endif
get_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]
endfor
gen_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 ()
平滑效果