why use homogeneous coordinates?

Started by
22 comments, last by staticVoid2 16 years, 3 months ago
I know this isn't really to do with homogeneous coordinates but would this function be correct for performing a z axis rotation on a matrix:
   void set_z_rotation(double z_rot) {      if(z_rot >= 0 && z_rot <= 360) {         matrix[0][0] = matrix[0][0] * cos(z_rot) +                        matrix[0][0] * sin(z_rot);         matrix[0][1] = matrix[0][1] * cos(z_rot) +                        matrix[0][1] * sin(z_rot);         matrix[0][2] = matrix[0][2] * cos(z_rot) +                        matrix[0][2] * sin(z_rot);         matrix[1][0] = matrix[1][0] * -sin(z_rot) +                        matrix[1][0] *  cos(z_rot);         matrix[1][1] = matrix[1][1] * -sin(z_rot) +                        matrix[1][1] *  cos(z_rot);         matrix[1][2] = matrix[1][2] * -sin(z_rot) +                        matrix[1][2] *  cos(z_rot);      }   }

where the matrix contains row vectors.?
Advertisement
You will probably get a better response if you asked this question as a new thread but I tried to work this out on paper and I think the proper function would be:

void set_z_rotation(double z_rot) {      if(z_rot >= 0 && z_rot <= 360) {         matrix[0][0] = matrix[0][0] * cos(z_rot) +                         matrix[1][0] * sin(z_rot);         matrix[0][1] = matrix[0][1] * cos(z_rot) +                         matrix[1][1] * sin(z_rot);         matrix[0][2] = matrix[0][2] * cos(z_rot) +                         matrix[1][2] * sin(z_rot);         matrix[0][3] = matrix[0][3] * cos(z_rot) +                         matrix[1][3] * sin(z_rot);         matrix[1][0] = matrix[0][0] * -sin(z_rot) +                         matrix[1][0] * cos(z_rot);         matrix[1][1] = matrix[0][1] * -sin(z_rot) +                         matrix[1][1] * cos(z_rot);         matrix[1][2] = matrix[0][2] * -sin(z_rot) +                         matrix[1][2] * cos(z_rot);         matrix[1][3] = matrix[0][3] * -sin(z_rot) +                         matrix[1][3] * cos(z_rot);      }   }

Although I could be wrong.
yeh, it took me a while to work that out but i think it looks right. thanx.
I take it this is what a y rotation might look like (as a function):
   void set_y_rotation(double y_rot) {      if(y_rot >= 0 && y_rot <= 360) {         matrix[0][0] = matrix[0][0] * cos(y_rot) +                        matrix[0][2] * -sin(y_rot);         matrix[0][2] = matrix[0][0] * sin(y_rot) +                        matrix[0][2] * cos(y_rot);         matrix[2][0] = matrix[2][0] * cos(y_rot) +                        matrix[2][2] * -sin(y_rot);         matrix[2][2] = matrix[2][0] * sin(y_rot) +                        matrix[2][2] * cos(y_rot);      }   }
No you need to do a full matrix multiplication. If you do you will notice that when you multiply a 4x4 matrix with a rotation matrix more than just 4 members change. You really should look into downloading or implementing a matrix class (or set of functions if you are using C). You don't gain much by implementing the multiplication this way and the code readability is a huge gain.

Also replacing all the cos(y_rot) and sin(y_rot) with temporary variables will be beneficial as well.
I thought implementing it this way was a lot fater than looping though the matrix because whatever axis you are rotating will stay the same so there is no point in multiplying by 1. And does it matter if the matrix contains row or column vectors when multiplying?

[Edited by - staticVoid2 on January 9, 2008 3:53:20 AM]
In that sense I think u're right.
If u have H' = H * Rot_x(angle), the 1-st and 4-th columns of H don't change, so u can update only the 2-nd and 3-rd columns.
would this be right when multiplying the full 3x3 matrix with the y rotation matrix.
As you can see there are bits that can be cancelled out but this is just to make things simpler.
         matrix[0][0] = matrix[0][0] * cos(y_rot) +                        matrix[0][1] * 0          +                        matrix[0][2] * -sin(y_rot);         matrix[0][1] = matrix[0][0] * 0 +                        matrix[0][1] * 1 +                        matrix[0][2] * 0;         matrix[0][2] = matrix[0][0] * sin(y_rot) +                        matrix[0][1] * 0          +                        matrix[0][2] * cos(y_rot);         matrix[1][0] = matrix[1][0] * cos(y_rot) +                        matrix[1][1] * 0          +                        matrix[1][2] * -sin(y_rot);         matrix[1][1] = matrix[1][0] * 0 +                        matrix[1][1] * 1 +                        matrix[1][2] * 0;         matrix[1][2] = matrix[1][0] * cos(y_rot) +                        matrix[1][1] * 0          +                        matrix[1][2] * sin(y_rot);         matrix[2][0] = matrix[2][0] * cos(y_rot) +                        matrix[2][1] * 0          +                        matrix[2][2] * -sin(y_rot);         matrix[2][0] = matrix[2][0] * 0 +                        matrix[2][1] * 1 +                        matrix[2][2] * 0;         matrix[2][0] = matrix[2][0] * cos(y_rot) +                        matrix[2][1] * 0          +                        matrix[2][2] * sin(y_rot);


would this be right?

It would be infinitely better for you to simply implement generic 4x4 matrix operation. The algorithm is provided in the link I gave you in an earlier post.

Special-casing it in this fashion is brittle and error-prone, and in practice there is no good reason for it.
but would it not make a difference when you multiply a column vector matrix by this rotation matrix than when you multiply a row vector matrix by the rotation matrix?

This topic is closed to new replies.

Advertisement