0
votes

i have object that are receiving 4 transformations step by step at different location in the software. We use a CAD engine library so i have custom objects to do transformation.

The Situation

i need to recuperate from the matrix of one of the object translation and rotation so that i can create a new matrix later one from those rotation.

The Code (sample)

// create a transformation object, very simple 1 translation and 3 rotations
var  t = new Translation(10, 15, 20)
         * new Rotation(10d.ToRadian(), Vector3D.AxisX)
         * new Rotation(10d.ToRadian(), Vector3D.AxisY)
         * new Rotation(10d.ToRadian(), Vector3D.AxisZ);

// convert to the windows Matrix3D
var m1 = ToWindowsMatrix(t);

// decompose the matrix to get one random possibility of combinaison of rotation x, y and Z to obtain the same result
var rx = Math.Atan2(m1.M32, m1.M33).ToDegree();
var ry = Math.Atan2(-m1.M31, Math.Sqrt(Math.Pow(m1.M32, 2) + Math.Pow(m1.M33, 2))).ToDegree();
var rz = Math.Atan2(m1.M21, m1.M11).ToDegree();

Source for formula to get rotation x,y and z:

// create a transformation object, in the same order, hardcoded translate but use the computed equivalent rotation x,y and z
var  t2 = new Translation(10, 15, 20)
         * new Rotation(rx.ToRadian(), Vector3D.AxisX)
         * new Rotation(ry.ToRadian(), Vector3D.AxisY)
         * new Rotation(rz.ToRadian(), Vector3D.AxisZ);

// convert to the windows Matrix3D
var m2 = ToWindowsMatrix(t2);

The Problem

if i take a 3d object and apply the transformation of t2 the rotation of the object is completely off compared to what t1 gives.

I absolutely need to get decomposed value as the whole system display the objects using 6 parameters (origin x,y,z and rotation x,y,z) which then create an identity matrix and apply a translate and 3 rotate.

I have been working of this for around 7 hours and i figured out that if i ONLY apply a single rotation on t1 ie "rotation X 90" and on t2 i still apply ALL rotations with the computed value the rotation DO match. So i have the feeling that the order of transformation need to be in a specific order but i cannot change it. So it mean i need to get the rotation x,y and z so that by applying Translate * RotX * RotY * RotZ it matches.

Anything i might have missed ? i have found some mathlab samples too but formula are all the same so far.

1

1 Answers

0
votes

Figured out after a while how to get the XYZ rotation of a matrix. The problem is that the Euler angles return in a ZYX rotation. More operation need to be performed in order to extract the XYZ from it.

public static double[] GetXYZRotation(this Matrix3D matrix)
    {
        // get the euler angles
        var eulerX = Math.Atan2(matrix.M32, matrix.M33).ToDegree();
        var eulerY = Math.Atan2(-matrix.M31, Math.Sqrt(Math.Pow(matrix.M32, 2) + Math.Pow(matrix.M33, 2))).ToDegree();
        var eulerZ = Math.Atan2(matrix.M21, matrix.M11).ToDegree();

        // create transformation of the euler angle to get a matrix. euler rotation is ZYX
        var eulerMatrix = (new Translation(matrix.OffsetX,matrix.OffsetY,matrix.OffsetZ)
              * new Rotation(eulerZ.ToRadian(), Vector3D.AxisZ)
              * new Rotation(eulerY.ToRadian(), Vector3D.AxisY)
              * new Rotation(eulerX.ToRadian(), Vector3D.AxisX)).ToWindowsMatrix();

        // get the Alpha Beta and Gamma of the euler matrix
        var beta = Math.Atan2(eulerMatrix.M13, Math.Sqrt(Math.Pow(eulerMatrix.M11, 2d) + Math.Pow(-eulerMatrix.M12, 2d)));
        var alpha = Math.Atan2(-(eulerMatrix.M23 / Math.Cos(beta)), eulerMatrix.M33 / Math.Cos(beta));
        var gamma = Math.Atan2(-(eulerMatrix.M12 / Math.Cos(beta)), eulerMatrix.M11 / Math.Cos(beta));

        // get the XYZ rotation per euler computed alpha beta gamma
        var rotationX = alpha.ToDegree();
        var rotationY = beta.ToDegree();
        var rotationZ = gamma.ToDegree();

        return new[] { rotationX, rotationY, rotationZ };
    }

So i needed to compute the matrix applying the euler angle in the reverse order : Z then Y then X and from there calculate the Alpha, Beta and Gamma which retreive a XYZ rotation values