Quaternion from transition matrix
Hi,
i need to calculate the quaternion from a transition matrix. What i name a transition matrix is the matrix that allow to express the coordinates of a vector in a first referential R1 (x,y,z) with the combinaison of vectors defining a second referential R2 => v2 = M . v1
I know coordinates of 3 points in R1 and the same points in R2, so i can calculate the matrix and it works fine : i can convert coordinates of any point from R1 to R2 and vice versa.
However, i need to convert this matrix in a transform, so in a translation and a rotation (the quaternion i'm searching).
The functions to convert one matrix4x4 to quaternion doesn't work because i think them works for rotation matrix, but here it's a transition matrix.
May be there is another solution from known coordinates of 3 points in two referential to calculate translation and rotation ?
I tryed with 2 successives rotations and even 3 successives rotations, but it's not exact each times : from the 3 points => calculate quaternion q1 to transform v1 to v1', calculate quaternion to transform q1.v2 to v2', and combine the 2 quaternions... and even tryed to do the same with v1,v2,v3...
Any idea please ?
here it's my code to calculate transition matrix : it's 3 équations systems with each 3 unkown variables, so i use matrix to solve each system in 3 times, result is matrix MatR1R2 (or MatR2R1) : (abc) (def) (ghi)
// résolution du systèmes de 3 x 3 équations à 3 inconnues pour connaitre les coeff de la matrice de passage
// matrix de passage : |abc0|
// |def0|
// |ghi0|
// |0001|
Matrix4x4 m;
Vector4 eq1,eq2,eq3;
Vector4 abc, def, ghi;
// les 3 coeff a,b,c
eq1 = new Vector4(x2, y2, z2, x5);
eq2 = new Vector4(x1, y1, z1, x4);
eq3 = new Vector4(x, y, z, x3);
m = assignMatrix(eq1, eq2, eq3);
//Debug.Log("m pour abc =" + m.ToString("F3"));
abc = solveSystem3(m);
// les 3 coeff d,e,f
eq1 = new Vector4(x2, y2, z2, y5);
eq2 = new Vector4(x1, y1, z1, y4);
eq3 = new Vector4(x, y, z, y3);
m = assignMatrix(eq1, eq2, eq3);
//Debug.Log("m pour def =" + m.ToString("F3"));
def = solveSystem3(m);
// les 3 coeff g,h,i
eq1 = new Vector4(x2, y2, z2, z5);
eq2 = new Vector4(x1, y1, z1, z4);
eq3 = new Vector4(x, y, z, z3);
m = assignMatrix(eq1, eq2, eq3);
//Debug.Log("m pour ghi =" + m.ToString("F3"));
ghi = solveSystem3(m);
//Calcul matrice de passage du nouveau référentiel à l'ancien
MatR2R1 = assignMatrix(abc, def, ghi);
//Calcul matrice de passage de l'ancien référentiel au nouveau
MatR1R2 = MatR2R1.inverse;
Debug.Log(MatR1R2.ToString("F3"));
// TEST
vDelta1 = new Vector4(x2,y2,z2,0.0f);
vDelta2 = new Vector4(x5, y5, z5, 0.0f);
Debug.Log("vDelta1 : x=" + vDelta1.x.ToString("F3") + " y=" + vDelta1.y.ToString("F3") + " z=" + vDelta1.z.ToString("F3"));
Debug.Log("vDelta2 : x=" + vDelta2.x.ToString("F3") + " y=" + vDelta2.y.ToString("F3") + " z=" + vDelta2.z.ToString("F3"));
vDelta2 = MatR1R2 * vDelta2;
Debug.Log("MatR1R2 * vDelta2 : x=" + vDelta2.x.ToString("F3") + " y=" + vDelta2.y.ToString("F3") + " z=" + vDelta2.z.ToString("F3"));
The function to solve one system
Vector4 solveSystem3 (Matrix4x4 system3)
{
Vector4 result = Vector3.zero;
Matrix4x4 m = system3;
Matrix4x4 m1 = system3;
Matrix4x4 m2 = system3;
Matrix4x4 m3 = system3;
float d = 0.0f;
float d1 = 0.0f;
float d2 = 0.0f;
float d3 = 0.0f;
// on fixe la dernière colone et dernière ligne de chaque matrice
m.SetColumn(3, new Vector4(0, 0, 0, 1));
m.SetRow(3, new Vector4(0, 0, 0, 1));
m1.SetColumn(3, new Vector4(0, 0, 0, 1));
m1.SetRow(3, new Vector4(0, 0, 0, 1));
m2.SetColumn(3, new Vector4(0, 0, 0, 1));
m2.SetRow(3, new Vector4(0, 0, 0, 1));
m3.SetColumn(3, new Vector4(0, 0, 0, 1));
m3.SetRow(3, new Vector4(0, 0, 0, 1));
// modifs de colone 0 pour la matrice m1
m1[0, 0] = system3[0, 3];
m1[1, 0] = system3[1, 3];
m1[2, 0] = system3[2, 3];
// modifs de colone 1 pour la matrice m2
m2[0, 1] = system3[0, 3];
m2[1, 1] = system3[1, 3];
m2[2, 1] = system3[2, 3];
// modifs de colone 2 pour la matrice m3
m3[0, 2] = system3[0, 3];
m3[1, 2] = system3[1, 3];
m3[2, 2] = system3[2, 3];
// calcul des déterminants
d = m.determinant;
d1 = m1.determinant;
d2 = m2.determinant;
d3 = m3.determinant;
// calcul des coefficients résultats a,b,c
result = new Vector4(d1 / d, d2 / d, d3 / d,0.0f);
return result;
}
i forget a code presicion : 3 points known => p3, p4, p5 in R1 => p, p1, p2 in R2