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's normal.
- Rotate the polygon by this angle around the x-axis.
- 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.
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.
// F0 changes x0,y0,z0 to world X,Y,Z
F0[0, 0] = X0.X;
F0[0, 1] = X0.Y;
F0[0, 2] = X0.Z;
F0[1, 0] = Y0.X;
F0[1, 1] = Y0.Y;
F0[1, 2] = Y0.Z;
F0[2, 0] = Z0.X;
F0[2, 1] = Z0.Y;
F0[2, 2] = Z0.Z;
F0[3, 3] = 1.0;
// F1 changes world X,Y,Z to x1,y1,z1
F1[0, 0] = X1.X;
F1[0, 1] = Y1.X;
F1[0, 2] = Z1.X;
F1[1, 0] = X1.Y;
F1[1, 1] = Y1.Y;
F1[1, 2] = Z1.Y;
F1[2, 0] = X1.Z;
F1[2, 1] = Y1.Z;
F1[2, 2] = Z1.Z;
F1[3, 3] = 1.0;
matrix = F1*F0
精彩评论