Matric and various vector math hints and tips. ------------------------------------------------------------------------------- General Matrix calculation is by [x,y,z] * M => [x',y',z'] |x| |x'| or M * |y| => |y'| |z| |z'| The result is the same in either case. As long as the matrix was created using row vectors OR column vectors to match! Which style is used is up to the matrix library version you use. I prefer the former, which The perl Math::MatrixReal library seems to like the later. Just note the order. ------------------------------------------------------------------------------- Matrix and what the terms actually mean... From the povray matrix page (vanished).. http://enphilistor.users4.50megs.com/matrix.htm Generally a matrix is defined (at least in Povray) as... | A.x A.y A.z 0 | | B.x B.y B.z 0 | | C.x C.y C.z 0 | | D.x D.y D.z 1 | This can be thought of a a matrix of four (row) vectors A, B, C, D where A = | A.x, A.y, A.z, 0 | etc. (NB: A.x is the x component of A) What this matrix does is wrap the 3D space so that the X axis moves (rotates, scales, shear, etc) to vector A, ditto for Y -> B and Z -> C. The last vector D is the new location for the Origin (translate). As the page above specifies this means that if you create a set of orthogonal vectors A,B,C general rotations are very easy to do, and goes on to provide a two part solution to rotating any general vector to any other general vector (while retaining an general up direction). ------------------------------------------------------------------------------- Orthogonal Matrices A matrix is orthogonal if each of the three vectors making up the matrix are orthogonal. That is they are all perpenducular to each other. This is true of all rotation matrices (no scaling or shear). What is interesting is that the inverse of such matrices is also its transpose. And that matrix in itself is also orthogonal. This means if we have a rotation matix | A.x A.y A.z | | B.x B.y B.z | | C.x C.y C.z | To rotate the x,y,z axises to the A,B,C vectors, then it so happens that the inverse (rotate A,B,C vectors to the x,y,z axises) is the matrixes transpose (diagonal mirror). | A.x B.x C.x | | A.y B.y C.y | | A.z B.z C.z | ------------------------------------------------------------------------------- Rotation Matrices are... Normilized or Each row or column are unit vectors (vector length = 1) Orthogonal or Dot product of any row to to any other row or column to column is 0 EG: All row or column vectors are perpendicular to each other Its Inverse is also its Transpose (due to orthogonality) ------------------------------------------------------------------------------- Rotation Matrix about axis (x,y,z) and angle a Given that s = sin(a) c = cos(a) t = (1 - cos(a)) and x, y, z defines the axis you want to rotate around then, the rotation matrix is... | txx+c txy+sz txz-sy | R = | txy-sz tyy+c tyz+sx | | txz+sy tyz-sx tzz+c | For very small angles (and you later occasionally re-normalize) A technique often used for interactive rotations using a mouse. Then s = sin(a) => a c = cos(a) => 1 t => 0 and thus rotation becomes | 1 za -ya | R = | -za 1 xa | | ya -xa 1 | Given a orthogonal rotation matrix Then the axis and angle of rotation can be found using... cos(a) = ( R[0][0] + R[1][1] + R[2][2] - 1 ) / 2 axis = ( R[1][2]-R[2][1], R[2][0]-R[0][2], R[0][1]-R[1][0] ) / 2*sin(a) ------------------------------------------------------------------------------- General Rotatation of a unit vector V to unit vector W (rotation in same plain as both vectors - ie: the minimal rotation) This is complex and difficult if you try to generate it using the above general axis and angle method. The following generates it faster and more simply, without need for sin() and cos() functions, using the meaning of the three vectors in a roatation matrix. To do this requires three steps... 1/ Matrix to rotate the Vector V onto some arbatary axis (eg: X) but such that the vector W also rotates to W' in the XY plan! 2/ One to rotate about the Z axis so that the rotated vector V' (which is now at the X axis) is rotated to W' in that frame work. 3/ Rotate back to the original axis frame work by using the inverse (transpose) of the original rotation. First Step It is easier to calculate the last step and invert the matrix, so... We need to find two more unit vectors orthogonal to the original vector V. One of these vectors must be the axis of rotation so it can be moved to the Z axis for the second step. Find the axis of rotation N N = normalised ( V x W ) And the other orthogonal axis to complete the axis frame work. M = N x V (note M is unit length as N and V are orthoginal) alturnativally... M = normailzed( V x W x V ) to rotate the X Y Z axis to this frame work would thus be... | Vx, Vy, Vz | Q = | Mx, My, Mz | (NB: a matrix of row vectors) | Nx, Ny, Nz | But for the first step we want to do the oppisite! So inverse the orthogonal matrix by transposing (diagonal flip). So the first step is.... transpose(Q) Second Step The new location of V (or V') which is now the X axis, needs to be rotated around Z (now the axis of rotation), to the new location of W or W' W' = W * transpose(Q) As the Z axis does not rotate we only need the new location of the Y axis in this step to complete this step. Y' = W' Zrotate(90deg) = Z x W' The actual rotaton matrix in this axis framework is thus... | W'x, W'y, W'z | T = | Y'x, Y'y, Y'z | | Zx, Zy, Zz | <- This is just (0, 0, 1) Third Step We just rotate back to the original coordinate system... This was the original matrix Q calculated in step one. The completed rotation matrix is thus.. Rotate_V_to_W = R = transpose(Q) * T * Q =============================================================================== The following is vector stuff, not matrix... ------------------------------------------------------------------------------- Distance of a point to a line. Given line AB, Point C some distance away. Minimim distance of C to line (perpendicular to line) r = (AB . AC) / |AB|^2 WRONG: that is the distance of C from A in the direction of A to B! That is length along the line! -------------------------------------------------------------------------------