title: Rotating a 3d polygon into xy-plane while maintaining orientation
body: <p>I have a polygon oriented any way and positioned anywhere in 3d space. I need to transform the polygon into the xy-plane so that I can perform various operations on it (in particular generating a grid of points across the bounding box of the polygon) in 2d rather than 3d, then transform it back. </p>
  
  <p>The problem comes with the orientation of the transformed polygon. If I just wanted to rotate into the plane, I could take the angle between the polygon's normal and the xy-plane and rotate around an axis orthogonal to both (cross product). However, I require that the polygon's bounding box be oriented such that the bottom (lowest z-value) edge of the bounding box is coplanar with the xy-plane, both before and after transformation. In other words, the bounding box is resting flush on one side which is parallel to the ground. After transformation, this edge would be parallel to the x-axis. This is so that the grid of points I generate on the surface will always have rows running parallel to the ground, regardless of the polygon's orientation. </p>
  
  <p>My approach is to perform two rotations; first rotate around the z-axis by the angle between the line formed by the intersection of the polygon's plane and the xy-plane, and the x-axis. This ensures that the bottom of the bounding box is not moving out of the xy-plane. Then, rotate again around the x-axis by the angle between the polygon's (new) normal and the xz-plane. Here are the steps:</p>
  
  <ol>
  <li>Find the equation for the polygon's plane (from the normal).</li>
  <li>Find the intersection of the polygon's plane and the xy-plane. This is a line in the xy-plane.</li>
  <li>Find the angle between this line and the x-axis.</li>
  <li>Rotate the polygon by this angle around the z-axis.</li>
  <li>Determine new normal.</li>
  <li>Find the angle between new normal and xy-plane's normal.</li>
  <li>Rotate the polygon by this angle around the x-axis.</li>
  <li>The polygon should now be in the xy-plane; generate bounding box using max/min x and y values, generate point grid, etc., then transform everything back to where it started.</li>
  </ol>
  
  <p>I realize that two rotations should be combined to reduce the number of matrix multiplications, but this is the general algorithm. </p>
  
  <p>I'm not a graphics expert; can anyone offer advice on this technique? Is there a better way? Does my approach sound correct? I am developing in Java and am looking at using the Transform3D class for the rotations.</p>