Question

I know two 3D-points in a line (top and bottom of an unsymmetrical object), and would like to find euler angles(rotation along x, y and z axis).

Example: Need reverse engineering of following code of OpenGL, below is the just an example to show the scenario.

//Translation
glTranslated(p1.x, p1.y, p1.z);

//Rotation
glRotatef(rot.x, 1.0f, 0.0f, 0.0f);
glRotatef(rot.y, 0.0f, 1.0f, 0.0f);
glRotatef(rot.z, 0.0f, 0.0f, 1.0f);

// Draw the object ALONG Y-AXIS
p2 = DrawMyObject(); //p2 is top of my object

Now in some situations I got only p1 and p2 and I need to know euler angles (roation along x, y and z axis). How?

This is what I tried and answer should be (Rx, Ry, Rz): (4, -3, -11),

cv::Point3d p1, p2;
p1.x = 0.0525498;
p1.y = 0.0798909;
p1.z = -1.20806;

p2.x = 0.0586557;
p2.y = 0.111226;
p2.z = -1.20587;

double dx, dy, dz;
double angle;

dx = p2.x - p1.x;
dy = p2.y - p1.y;
dz = p2.z - p1.z;

angle = std::atan2(dy, dz); angle = RAD2DEG(angle);
std::cout<<"\n atan2(dy, dz): "<<int(90 - angle);

angle = std::atan2(dx, dz); angle = RAD2DEG(angle);
std::cout<<"\n atan2(dz, dx): "<<angle;

angle = std::atan2(dy, dx); angle = RAD2DEG(angle);
std::cout<<"\n atan2(dy, dx): "<<int(angle -90);

std::cout<<std::endl;

I am not getting exactly correct answer, especially rotation along Y is not correct at all. I think p1 and p2 both lies in y-axis while rotating along y-axis so the problem is. Then what is best possible solution?

Was it helpful?

Solution

  1. As stated in the comments you will need either a 3rd point on your object or a constant world-space vector. Be aware that using a constant vector could introduce gimbal problems depending on your specific application and the orientation of the line relative to that vector so a third point might be preferable if you have it.
  2. Construct an orthonormalized 3x3 rotation matrix:
    1. Use the Grahm-Schmit method to orthonormalize the first two rows where:
      • u1 = p2 - p1
      • u2 = p3 - p1 (or a constant vector)
      • After applying Grahm-Schmit, these vectors will become the first 2 rows of your 3x3 matrix.
    2. The third row of your matrix is just the cross-product of those first two rows.
  3. Decompose the resulting matrix into euler angles.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top