Chris Collins
Chris Collins

Reputation: 89

Axis rotation with an oriented bounding box

I'm trying to put an oriented bounding box around the stamford rabbit for a project. To do this, I create a covariance matrix with the vertices and use the eigenvector columns as the new axis vectors for the OBB.

To draw the OBB, I take the cross product of the vector columns with the x,y and z axes to find the vector perpendicular both, then I use the dot product to find the angle between them.

//rv,uv,fv are the normalised column vectors from the eigenvector matrix.
// Calculate cross product for normal
crossv1x[0] = xaxis[1]*rv[2] - xaxis[2]*rv[1];
crossv1x[1] = xaxis[2]*rv[0] - xaxis[0]*rv[2];
crossv1x[2] = xaxis[0]*rv[1] - xaxis[1]*rv[0];

// Calculate cross product for normal
crossv2y[0] = yaxis[1]*uv[2] - yaxis[2]*uv[1];
crossv2y[1] = yaxis[2]*uv[0] - yaxis[0]*uv[2];
crossv2y[2] = yaxis[0]*uv[1] - yaxis[1]*uv[0];

// Calculate cross product for normal
crossv3z[0] = zaxis[1]*fv[2] - zaxis[2]*fv[1];
crossv3z[1] = zaxis[2]*fv[0] - zaxis[0]*fv[2];
crossv3z[2] = zaxis[0]*fv[1] - zaxis[1]*fv[0];

//dot product:
thetaX = dot(xaxis,rv,1)*180/PI;
thetaY = dot(yaxis,uv,1)*180/PI;
thetaZ = dot(zaxis,fv,1)*180/PI;

I then apply a rotation around the cross product vector with an angle determined by the dot product (glRotatef(angle,cross[0],cross1,cross[2]) for each axis). I then draw an axis aligned bounding box, then to the inverse rotation back to the original position.

        glRotatef(thetaY,crossv2y[0],crossv2y[1],crossv2y[2]);
        glRotatef(thetaZ,crossv3z[0],crossv3z[1],crossv3z[2]);
        glRotatef(thetaX,crossv1x[0],crossv1x[1],crossv1x[2]);

        glTranslatef(-meanX, -meanY, -meanZ);
        glColor3f(1.0f,0.0f,0.0f);
        AOBB(1); //Creates an axis aligned box.

        glRotatef(-thetaX,crossv1x[0],crossv1x[1],crossv1x[2]);
        glRotatef(-thetaZ,crossv3z[0],crossv3z[1],crossv3z[2]);
        glRotatef(-thetaY,crossv2y[0],crossv2y[1],crossv2y[2]);

As you can see below, the box does not fit exactly onto the rabbit, nor does it align with the axis I have drawn... Am I missing something here? Ive fried my brain trying to find the solution but to no avail...

enter image description here

Upvotes: 2

Views: 1042

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473407

To draw the OBB, I take the cross product of the vector columns with the x,y and z axes to find the vector perpendicular both, then I use the dot product to find the angle between them.

Those "vector columns?" Those are actually the columns of the rotation matrix you want to generate.

So instead of using these vectors to compute rotation angles, just build a matrix with those (normalized) vectors as the columns. Upload it with glMultMatrix, and you should be fine.

Upvotes: 2

Related Questions