Rotation of coordinates around an axis

I represent the form as a set of coordinates in 3D, I try to rotate the whole object around the axis (in this case, the Z axis, but I would like to rotate around all three as soon as I get it working).

I wrote the code for this using the rotation matrix:

//Coord is a 3D vector of floats //pos is a coordinate //angles is a 3d vector, each component is the angle of rotation around the component axis //in radians Coord<float> Polymers::rotateByMatrix(Coord<float> pos, const Coord<float> &angles) { float xrot = angles[0]; float yrot = angles[1]; float zrot = angles[2]; //z axis rotation pos[0] = (cosf(zrot) * pos[0] - (sinf(zrot) * pos[1])); pos[1] = (sinf(zrot) * pos[0] + cosf(zrot) * pos[1]); return pos; } 

The figure below shows the object that I am trying to rotate (looking down on the Z axis) before trying to rotate, each small sphere indicates one of the coordinates that I am trying to rotate

alt text http://www.cs.nott.ac.uk/~jqs/notsquashed.png

The rotation is performed for the object using the following code:

 //loop over each coordinate in the object for (int k=start; k<finish; ++k) { Coord<float> pos = mp[k-start]; //move object away from origin to test rotation around origin pos += Coord<float>(5.0,5.0,5.0); pos = rotateByMatrix(pos, rots); //wrap particle position //these bits of code just wrap the coordinates around if the are //outside of the volume, and write the results to the positions //array and so shouldn't affect the rotation. for (int l=0; l<3; ++l) { //wrap to ensure torroidal space if (pos[l] < origin[l]) pos[l] += dims[l]; if (pos[l] >= (origin[l] + dims[l])) pos[l] -= dims[l]; parts->m_hPos[k * 4 + l] = pos[l]; } } 

The problem is that when I perform the rotation in this way, with the angle parameter set to (0,0,0,0,1,0), it works (kind of), but the object is deformed, for example:

alt text http://www.cs.nott.ac.uk/~jqs/squashed.png

what I do not want. Can someone tell me what I'm doing wrong, and how can I rotate an entire object around an axis without deforming it?

thanks

nodlams

+4
source share
1 answer

When you do your rotation in rotateByMatrix, you evaluate the new pos [0], but then load it on the next line to calculate the new pos [1]. Thus, pos [0], which you use to calculate the new pos [1], is not an input, but an output. Save the result in temp var and return it.

 Coord<float> tmp; tmp[0] = (cosf(zrot) * pos[0] - (sinf(zrot) * pos[1])); tmp[1] = (sinf(zrot) * pos[0] + cosf(zrot) * pos[1]); return tmp; 

Also, pass pos to the function as a const reference.

 const Coord<float> &pos 

In addition, you must calculate the values โ€‹โ€‹of sin and cos once, store them in temporary ones and reuse them.

+7
source

All Articles