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.
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.
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:
- Find the equation for the polygon's plane (from the normal).
- Find the intersection of the polygon's plane and the xy-plane. This is a line in the xy-plane.
- Find the angle between this line and the x-axis.
- Rotate the polygon by this angle around the z-axis.
- Determine new normal.
- Find the angle between new normal and xy-plane.
- Rotate the polygon by this angle around the x-axis.
- The polygon should now be in the xz-plane; generate bounding box using max/min x and y values, generate point grid, etc., then transform everything back to where it started.
I realize that two rotations should be combined to reduce the number of matrix multiplications, but this is the general algorithm.
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.
To handle 3D polygons it's common to simply ignore the Z coordinate (which effectively projects the plane directly into the XY plane) for your mappings, then just reinstate the Z coordinates later.
The only time this doesn't work is if the original polygon is perpendicular to the XY plane, since the resulting mapping degenerates to a line.
You need the following matrix to change vertex coordinates from frame x0,y0,z0, to x1,y1,z1 and eventually a translation to make origins coincide.