Advertisement
Guest User

uselessWork

a guest
Jun 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Useless Work :'( RIP
  2.  
  3. interface TestData {
  4.     transformation: {
  5.         Tx: number;
  6.         Ty: number;
  7.         Tz: number;
  8.  
  9.         angle: number; //radian
  10.         Vx: number;
  11.         Vy: number;
  12.         Vz: number;
  13.  
  14.         S: number;
  15.     };
  16.     matrice_4x4: number[];
  17.     echelle_3x1: number[];
  18. }
  19.  
  20. function TransformationVersMatriceDigiTwin(tr: TestData): TestData {
  21.     //quaternion
  22.     var w = 0;
  23.     var x = 0;
  24.     var y = 0;
  25.     var z = 0;
  26.  
  27.     if (tr.transformation.angle != 0 || (tr.transformation.Vx != 0 && tr.transformation.Vy != 0 && tr.transformation.Vz != 0)) {
  28.         //cosinus directionnels
  29.         var V_norme = Math.sqrt(Math.pow(tr.transformation.Vx, 2) + Math.pow(tr.transformation.Vy, 2) + Math.pow(tr.transformation.Vz, 2));
  30.         var cosX = tr.transformation.Vx / V_norme;
  31.         var cosY = tr.transformation.Vy / V_norme;
  32.         var cosZ = tr.transformation.Vz / V_norme;
  33.  
  34.         //quaternion : q = a +ib + jc + kd
  35.         var sinA = Math.sin(tr.transformation.angle / 2);
  36.         w = Math.cos(tr.transformation.angle / 2);
  37.         x = sinA * cosX;
  38.         y = sinA * cosY;
  39.         z = sinA * cosZ;
  40.  
  41.         //quaternion normalisé
  42.         var q_norme = Math.sqrt(Math.pow(w, 2) + Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
  43.         w /= q_norme;
  44.         x /= q_norme;
  45.         y /= q_norme;
  46.         z /= q_norme;
  47.     }
  48.  
  49.     //construction de la matrice
  50.     var matrice_4x4: number[] = [
  51.         Math.pow(w, 2) + Math.pow(x, 2) - Math.pow(y, 2) - Math.pow(z, 2),
  52.         2 * x * y - 2 * w * z,
  53.         2 * x * z + 2 * w * y,
  54.         0,
  55.  
  56.         2 * x * y + 2 * w * z,
  57.         Math.pow(w, 2) - Math.pow(x, 2) + Math.pow(y, 2) - Math.pow(z, 2),
  58.         2 * y * z - 2 * w * x,
  59.         0,
  60.  
  61.         2 * x * z - 2 * w * y,
  62.         2 * y * z + 2 * w * x,
  63.         Math.pow(w, 2) - Math.pow(x, 2) - Math.pow(y, 2) + Math.pow(z, 2),
  64.         0,
  65.  
  66.         tr.transformation.Tx, tr.transformation.Ty, tr.transformation.Tz, 1];
  67.  
  68.     //échelle
  69.     var echelle_3x1: number[] = [tr.transformation.S, tr.transformation.S, tr.transformation.S];
  70.  
  71.     return { transformation: tr.transformation, matrice_4x4, echelle_3x1 };
  72. }
  73.  
  74. function MatriceDigiTwinVersTransformation(mtr: TestData): TestData {
  75.     //translation
  76.     var Tx = mtr.matrice_4x4[12];
  77.     var Ty = mtr.matrice_4x4[13];
  78.     var Tz = mtr.matrice_4x4[14];
  79.  
  80.     //composants de la matrice de rotation
  81.     var quu = mtr.matrice_4x4[0];
  82.     var quv = mtr.matrice_4x4[1];
  83.     var quw = mtr.matrice_4x4[2];
  84.  
  85.     var qvu = mtr.matrice_4x4[4];
  86.     var qvv = mtr.matrice_4x4[5];
  87.     var qvw = mtr.matrice_4x4[6];
  88.  
  89.     var qwu = mtr.matrice_4x4[8];
  90.     var qwv = mtr.matrice_4x4[9];
  91.     var qww = mtr.matrice_4x4[10];
  92.  
  93.     //reconstruction du quaternion
  94.     var w = 1 / 2 * Math.sqrt(1 + quu + qvv + qww);
  95.     var x = 1 / (4 * w) * (qwv - qvw);
  96.     var y = 1 / (4 * w) * (quw - qwu);
  97.     var z = 1 / (4 * w) * (qvu - quv);
  98.  
  99.     //calcul de l'angle et de l'axe de rotation
  100.     var angle = 0; var Vx = 0; var Vy = 0; var Vz = 0;
  101.     var xyz_norme = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
  102.     if (xyz_norme != 0) {
  103.         angle = 2 * Math.atan2(xyz_norme, w);
  104.         Vx = x / xyz_norme;
  105.         Vy = y / xyz_norme;
  106.         Vz = z / xyz_norme;
  107.     }
  108.  
  109.     //échelle
  110.     var S = (mtr.echelle_3x1[0] + mtr.echelle_3x1[1] + mtr.echelle_3x1[2]) / 3;
  111.  
  112.     return { transformation: { Tx, Ty, Tz, angle, Vx, Vy, Vz, S }, matrice_4x4: mtr.matrice_4x4, echelle_3x1: mtr.echelle_3x1 }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement