qwertz
qwertz

Reputation: 459

How to rotate a point cloud in xy plane?


I want to rotate a given point cloud into the xy plane. Therefore I fitted a plane through the pointcloud. This way I want to calculate the angles that need to be rotated to get the plane and also the pointcloud in xy-plane. The center of the plane is located in the orign. So i need to rotate around x- and y-axis. But I am not sure how to continue. Any Ideas?  figure

Here is my code so far:

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]

Upvotes: 0

Views: 3880

Answers (1)

I would say it is the rotation that aligns the normal of the pointcloud plane with normal of XY plane (which is Z or -Z).

Checkout here for the math on how to do that and here for some python code.

Upvotes: 1

Related Questions