matrix - Rotating arbitrary plane to be z-axis-aligned -
correct me if i'm wrong, but...
given normal of arbitrary source plane, , normal of plane after applying desired rotation:
vector3f sourcenormal = (x, y, z).normalize() vector3f desirednormal = (0, 0, 1).normalize()
1) can find "axis of rotation" through cross-product of 2 normals
vector3f rotationaxis = vector3f.cross(sourcenormal, desirednormal).normalize()
2) can find "angle of rotation" through arc-cosine of dot product of 2 normals.
// nico - there in project source, omitted here. float theta = math.acos(vector3f.dot(sourcenormal, desirednormal))
3) can apply rotation set of points in order orient source plane our desired plane.
float[] rotationmatrix = new float[16]; // x component rotationmatrix[5] = rotationmatrix[10] = (float)math.cos(theta); rotationmatrix[9] = (float)math.sin(theta); rotationmatrix[6] = -rotationmatrix[9]; // y component rotationmatrix[0] = rotationmatrix[10] = (float)math.cos(theta); rotationmatrix[2] = (float)math.sin(theta); rotationmatrix[8] = -rotationmatrix[2]; // z component rotationmatrix[0] = rotationmatrix[5] = (float)math.cos(theta); rotationmatrix[1] = (float)math.sin(theta); rotationmatrix[4] = -rotationmatrix[1]; for(point3f point : polygon) { float x = pt.getx(); float y = pt.gety(); float z = pt.getz(); float[] xs = new float[3]; float[] ys = new float[3]; float[] zs = new float[3]; for(int j = 0; j < 3; ++j) { xs[j] = rotationmatrix[j] * x; ys[j] = rotationmatrix[j + 4] * y; zs[j] = rotationmatrix[j + 8] * z; } x = 0; y = 0; z = 0; for(int j = 0; j < 3; ++j) { x += xs[j]; y += ys[j]; z += zs[j]; } pt.set(x, y, z); }
my output incorrect.
in points:
(-56.00, 72.01, 48.02) (-48.00, 72.01, 48.02) (-48.00, 86.01, 24.02) (-56.00, 86.01, 24.02)
out points:
(-124.960010, -88.105451, 24.185812) (-107.108590, -88.105451, 24.185812) (-107.108590, -105.237051, 12.0929052) (-124.960010, -105.237051, 12.0929052)
if had guess, i'd applying rotations points incorrectly...perhaps i've interpreted rotation matrix found in article ( http://en.wikipedia.org/wiki/rotation_matrix ) incorrectly?
thanks input.
...assuming correct way set rotation matrix, output still incorrect:
vector3f axis = vector3f.cross(sourcenormal, desirednormal).normalize(); float angle = (float) math.acos(p.normal.dot(new vector3f(0, 0, 1))); float s = (float)math.sin(angle); float c = (float)math.cos(angle); float x = axis.getx(), y = axis.gety(), z = axis.getz(); float[] matrix = new float[16]; matrix[0] = x * x * (1 - c) + c; matrix[1] = x * y * (1 - c) - (z * s); matrix[2] = x * z * (1 - c) + (y * s); matrix[4] = y * x * (1 - c) + (z * s); matrix[5] = y * y * (1 - c) + c; matrix[6] = y * z * (1 - c) - (x * s); matrix[8] = x * z * (1 - c) - (y * s); matrix[9] = y * z * (1 - c) + (x * s); matrix[10] = z * z * (1 - c) + c; float nx = x * matrix[0] + y * matrix[1] + z * matrix[2]; float ny = x * matrix[4] + y * matrix[5] + z * matrix[6]; float nz = x * matrix[8] + y * matrix[9] + z * matrix[10]; in: (-56.00, 56.01, -16.02) in: (-48.00, 56.01, -16.02) in: (-48.00, 72.01, -8.02) in: (-56.00, 72.01, -8.02) out: (-51.270340, 46.887921, -25.5108342) out: (-43.9460070, 46.887921, -25.5108342) out: (-43.9460070, 62.798761, -21.5554182) out: (-51.270340, 62.798761, -21.5554182)
your rotation matrix wrong.
the rotations need combined per formula in section 10 of wikipedia article you've linked ("rotation matrix axis , angle").
a simplified version of matrix can found in manual page glrotate
function.
nb: unless you're repeating rotation on , over, there's no need take acos(dotp)
take cos
, sin
of again. use dot product directly cos(theta)
, , use relationship sin(theta)^2 = 1 - cos(theta)^2
work out sin(theta)
.
Comments
Post a Comment