graham 算法计算平面投影点集的凸包

将三维空间中的点云使用 BEV 的方式多视角投影到某个平面之后,可能需要用到该平面投影图(光栅化之前)的点集的凸包,所以这里记录一下常见的 graham 凸包算法。

向量的内积(点乘)、外积(叉乘)

graham 算法模拟最外层点集包围的过程的关键思想是使用两个向量之间的外积来判断下一条连线的转角,如果向外拐了,那说明当前基点在本次连线之后会成为一块凹陷,注意“凸包”的定义,每个顶角的角度都小于 18 0 ∘ 180^\circ 180 才叫 “凸” ,如果有一个内凹顶点,那么它的内角是大于 18 0 ∘ 180^\circ 180 的,可以确定,它应该是包含在实际的最终计算出来的理想凸包之内才对,这个时候就需要调整连线的基点为上一个基点。

在二维平面上,叉积的结果与向量的顺时针或逆时针旋转方向有关。具体来说:

  • 对于二维平面上的两个向量 u = ( x 1 , y 1 ) u=(x_1,y_1) u=(x1,y1) v = ( x 2 , y 2 ) v=(x_2, y_2) v=(x2,y2) ,它们的叉积可以使用一个标量值来表示 u × v = x 1 y 2 − y 1 x 2 u\times v = x_1y_2 - y_1x_2 u×v=x1y2y1x2
  • 这个标量值表示了这两个向量所定义的平行四边形的有向面积,也可以用来判定向量的旋转方向。

确定旋转方向

  • 正值:当 叉积 的值为正时,向量 v v v 从向量 u u u 逆时针旋转到达 v v v,也就是说, v v v u u u 的左侧。
  • 负值:当 叉积 的值为负时,向量 v v v 从向量 u u u 顺时针旋转到达 v v v,也就是说, v v v u u u 的右侧。
  • 零值:当 叉积 的值为零时,两个向量是共线的,即它们之间没有旋转,或者说它们之间的旋转角度是 0 ∘ 0^\circ 0∘ 或 18 0 ∘ 180^\circ 180

numpy 的 cross 和 outer

示例 python 代码:

a = np.array([1, 1])
b = np.array([0, 1])

np.cross(a, b)

输出结果为 1 ,代表由向量 a 转动到向量 b 的转角是逆时针,符合右手螺旋。

numpy 库中有两个函数分别是 np.cross(a,b)np.outer(a,b) ,其中 np.cross 是我们常用所说的外积(叉乘),而 np.outer 实际的计算结果定义是一个张量中的每个元素对另一个张量中的每个元素的乘积。

np.inner 向量与矩阵计算示例

# Python Program illustrating 
# numpy.inner() method 
import numpy as np 
  
# Vectors 
a = np.array([2, 6]) 
b = np.array([3, 10]) 
print("Vectors :") 
print("a = ", a) 
print("\nb = ", b) 
  
# Inner Product of Vectors 
print("\nInner product of vectors a and b =") 
print(np.inner(a, b)) 
  
print("---------------------------------------") 
  
# Matrices 
x = np.array([[2, 3, 4], [3, 2, 9]]) 
y = np.array([[1, 5, 0], [5, 10, 3]]) 
print("\nMatrices :") 
print("x =", x) 
print("\ny =", y) 
  
# Inner product of matrices 
print("\nInner product of matrices x and y =") 
print(np.inner(x, y)) 

输出:

Vectors :
a =  [2  6]
b =  [3 10]

Inner product of vectors a and b =
66
---------------------------------------

Matrices :
x = [[2 3 4]
     [3 2 9]]

y = [[ 1  5  0]
     [ 5 10  3]]

Inner product of matrices x and y =
[[17 52]
 [13 62]]

可以看到对于向量来说,外积在 numpy 中的 outer 不是我们说常说的叉乘计算方式,而是一个向量中的每个元素对另一个向量中的每个元素的乘积结果。

np.outer 向量与矩阵计算示例

# Python Program illustrating  
# numpy.outer() method  
import numpy as np 
  
# Vectors 
a = np.array([2, 6]) 
b = np.array([3, 10]) 
print("Vectors :") 
print("a = ", a) 
print("\nb = ", b) 
  
# Outer product of vectors  
print("\nOuter product of vectors a and b =") 
print(np.outer(a, b)) 
  
print("------------------------------------") 
  
# Matrices 
x = np.array([[3, 6, 4], [9, 4, 6]]) 
y = np.array([[1, 15, 7], [3, 10, 8]]) 
print("\nMatrices :") 
print("x =", x) 
print("\ny =", y) 
  
# Outer product of matrices 
print("\nOuter product of matrices x and y =") 
print(np.outer(x, y)) 

输出:

Vectors :
a =  [2  6]
b =  [3 10]

Outer product of vectors a and b =
[[ 6 20]
 [18 60]]
------------------------------------

Matrices :
x = [[3 6 4]
     [9 4 6]]

y = [[ 1 15  7]
     [ 3 10  8]]

Outer product of matrices x and y =
[[  3  45  21   9  30  24]
 [  6  90  42  18  60  48]
 [  4  60  28  12  40  32]
 [  9 135  63  27  90  72]
 [  4  60  28  12  40  32]
 [  6  90  42  18  60  48]]

这说明在 graham 凸包算法中计算两个向量的旋转方向还是需要 np.cross 而不能使用 np.outer 来计算。

python 示例

生成样例散点数据图

# Test the algorithm with an example set of points
points = [(0, 3), (1, 1), (2, 2), (4, 4), (0, 0), (1, 2), (3, 1), (3, 3)]

start = min(points, key=lambda p: (p[1], p[0]))

print(*zip(*points))

fig1 = plt.figure()
plt.scatter(*zip(*points), color='blue')
plt.scatter(*start, color="red")
plt.show()

在这里插入图片描述

graham 算法一般以最下最左(Lowest Then Leftest)的点作为基准点,图中以红色的点作为标识。

显示按极角排序的结果

sorted_points = sorted(points, key=lambda p: (p[1] - start[1]) / (p[0] - start[0] + 1e-9), reverse=False)

fig, axs = plt.subplots(2, 4)
for point, ax in zip(sorted_points, axs.flatten()):
    ax.scatter(*zip(*points), color="blue")
    ax.scatter(*start, color="red")
    ax.plot(*zip(*[start, point]), marker="o")
plt.show()

在这里插入图片描述

根据排序点计算向量转向并连成凸包

def cross_product(o, a, b)
    return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
    
hull = []
for p in sorted_points:
    while len(hull) >= 2 and cross_product(hull[-2], hull[-1], p) <= 0:
        hull.pop()
    hull.append(p)
    

fig = plt.figure()
plt.scatter(*zip(*points), color="blue")
for i in range(len(hull)):
    p1 = hull[i]
    p2 = hull[(i + 1) % len(hull)]
    plt.plot([p1[0], p2[0]], [p1[1], p2[1]], 'r-')
plt.show()

在这里插入图片描述

基本思路

  1. 选取基点(最左最下)
  2. 所有点与基点形成的向量进行极角排序,从小到大
  3. 从当前点(初始时是基点 p 0 p_0 p0 p i p_i pi 出发连接极角排序好的点序列中的下一个点 p i + 1 p_{i+1} pi+1
  4. 从第一个点 p 1 p_1 p1 连接第二个点 p 2 p_2 p2 ,判断前一个向量 p 0 p 1 → \overrightarrow{p_0p_1} p0p1 与新的向量 p 1 p 2 → \overrightarrow{p_1p_2} p1p2 的转向是否是往内拐,如果是外拐的话说明这个地方会形成一个凹陷,不是凸包连线,所以弹出这个新加入的点 p 2 p_2 p2 ,准备下一个点的测试
平面点集凸包(Convex Hull)是指在一个平面上,所有点集中点的最小子集,构成一个不包含内部点的凸多边形。这个概念在计算机图形学、几何算法和数据分析中非常常见。要编写一个计算凸包的代码,通常会使用诸如 Graham 扫描法、快速 hull 算法(如 Jarvis March 或 Andrew 算法)或 Gift Wrapping 算法等高效的算法。 这里提供一个基于 Gift Wrapping 算法的简单 Python 示例,它假设你有一个二维列表(二维数组)存储了点集: ```python def convex_hull(points): if len(points) < 3: # 凸包至少需要3个点 return points # 将点按 X 坐标排序 points.sort(key=lambda x: x) hull = [points, points] # 初始化凸包,包含第一个点和第二个点 for i in range(2, len(points)): while len(hull) >= 2 and cross_product(hull[-2], hull[-1], points[i]) <= 0: hull.pop() # 如果当前点在凸包内部,则移除最后一个点 hull.append(points[i]) # 因为最后两个点可能共线,所以需要再次检查并添加最后一个点 if hull and cross_product(hull[-2], hull[-1], points) <= 0: hull.pop() return hull def cross_product(p1, p2, p3): return (p2 - p1) * (p3[1] - p1) - (p2 - p1) * (p3 - p1) # 使用示例 points = [[1, 1], [2, 2], [3, 3], [4, 4], [1, 4]] convex_hull_points = convex_hull(points) print("凸包为:", convex_hull_points) ``` 这个代码定义了一个 `convex_hull` 函数,其中包含 `cross_product` 函数用于计算向量的叉积,这在判断三点是否构成凸包方向上很重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值