如何在xy平面上旋转点云?

How to rotate a point cloud in xy plane?


我想将给定的点云旋转到 xy 平面中。因此,我通过点云安装了一架飞机。这样我想计算需要旋转的角度以获得平面以及 xy 平面中的点云。平面的中心位于原点。所以我需要绕 x 轴和 y 轴旋转。但我不确定如何继续。有任何想法吗?

到目前为止,这是我的代码:

def fitPlaneLTSQ(df):
    (rows, cols) = df.shape
    G = np.ones((rows, 3))
    G[:, 0] = df['X']
    G[:, 1] = df['Z']
    Z = df['Y']
    (a, b, c),resid,rank,s = np.linalg.lstsq(G, Z)
    normal = (a, b, -1)
    nn = np.linalg.norm(normal)
    normal = normal / nn
    return (c, normal)

#load data
data = pd.read_csv('data.csv', sep=';')

# calc middle of cloud
meanx = np.mean(data['X'])
meany = np.mean(data['Y'])
meanz = np.mean(data['Z'])

#translate cloud to orign
data['X'] = data['X'] - meanx
data['Y'] = data['Y'] - meany
data['Z'] = data['Z'] - meanz

#calc plane 
maxx = np.max(data['X'])
maxz = np.max(data['Z'])
minx = np.min(data['X'])
minz = np.min(data['Z'])

c, normal = fitPlaneLTSQ(data)
point = np.array([0.0, 0.0, c])
d = -point.dot(normal)

# compute needed points for plane plotting
xx, yy = np.meshgrid([minx, maxx], [minz, maxz])
z = (-normal[0]*xx - normal[1]*yy - d)*1. / normal[2]

我会说这是使点云平面的法线与 XY 平面(Z 或 -Z)的法线对齐的旋转。

检查 here for the math on how to do that and 一些 python 代码。