一、思路
指定线段的起点与终点,给出需要在两点之间生成的点的个数。在两点之间等间距插入点,形成类似直线(线段)的效果
二、代码
main
* 第一个点
point1 := [99.2,-102.9,897]
* 第二个点
point2 := [-71,-105,889]
* 获取两点的距离
distance_3d_pp (point1, point2, Distance3DPP12)
* 两点之间插入点的数量
PointNum := 2*Distance3DPP12
* 从两个点生成直线
gen_line_from_two_point (point1, point2, PointNum, ObjectModel3DLine)
* 可视化显示
visualize_object_model_3d (WindowHandle, ObjectModel3DLine, [], [], ['lut','color_attrib'],['color1','coord_z'], [], [], [], PoseOut)
distance_3d_pp
Distance3DPP := sqrt((point1[0]-point2[0])*(point1[0]-point2[0])+\
(point1[1]-point2[1])*(point1[1]-point2[1])+\
(point1[2]-point2[2])*(point1[2]-point2[2]))
gen_line_from_two_point
* 1计算两点的X,Y,Z坐标偏差
diffX := point2[0] - point1[0]
diffY := point2[1] - point1[1]
diffZ := point2[2] - point1[2]
* 当X偏差为零时,生成包含PointNum个相同数值元素的数组
if (diffX == 0)
tuple_gen_const (PointNum, point1[0], X)
else
* 当X偏差不为零时,生成等差数组
stepX := diffX*1.0/PointNum
X := [point1[0]:stepX:point2[0]]
endif
* 同理
if (diffY == 0)
tuple_gen_const (PointNum, point1[1], Y)
else
stepY := diffY*1.0/PointNum
Y := [point1[1]:stepY:point2[1]]
endif
* 同理
if (diffZ == 0)
tuple_gen_const (PointNum, point1[2], Z)
else
stepZ := diffZ*1.0/PointNum
Z := [point1[2]:stepZ:point2[2]]
endif
* 计算X,Y,Z的最短数组长度,以避免运算bug
tuple_min ([|X|,|Y|,|Z|], MaxLength)
X := X[0:MaxLength-1]
Y := Y[0:MaxLength-1]
Z := Z[0:MaxLength-1]
* 通过X,Y,Z值生成点云对象
gen_object_model_3d_from_points (X, Y, Z, ObjectModel3DLine)
return ()
三、效果
四、总结
Halcon中生成三维直线,可以通过生成一系列点来模拟,已知直线上两个点,可以在这两个点之间插入N个点,形成一个线段上的离散点云,使得看起来像一条直线/线段。