title: How to rotate a point cloud in xy plane?
body: <hr />
  <p>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?
  <a href="https://i.stack.imgur.com/KbMbD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KbMbD.png" alt=" figure" /></a></p>
  <p>Here is my code so far:</p>
  <pre><code>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]
  </code></pre>