math2D.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /// <reference path="../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Matrix2D {
  4. public m = new Float32Array(6);
  5. constructor(m00: number, m01: number, m10: number, m11: number, m20: number, m21: number) {
  6. this.fromValues(m00, m01, m10, m11, m20, m21);
  7. }
  8. public fromValues(m00: number, m01: number, m10: number, m11: number, m20: number, m21: number): Matrix2D {
  9. this.m[0] = m00; this.m[1] = m01;
  10. this.m[2] = m10; this.m[3] = m11;
  11. this.m[4] = m20; this.m[5] = m21;
  12. return this;
  13. }
  14. public determinant(): number {
  15. return this.m[0] * this.m[3] - this.m[1] * this.m[2];
  16. }
  17. public invertToRef(result: Matrix2D): Matrix2D {
  18. let l0 = this.m[0]; let l1 = this.m[1];
  19. let l2 = this.m[2]; let l3 = this.m[3];
  20. let l4 = this.m[4]; let l5 = this.m[5];
  21. let det = this.determinant();
  22. if (det < (Epsilon * Epsilon)) {
  23. result.m[0] = 0; result.m[1] = 0;
  24. result.m[2] = 0; result.m[3] = 0;
  25. result.m[4] = 0; result.m[5] = 0;
  26. return this;
  27. }
  28. let detDiv = 1 / det;
  29. let det4 = l2 * l5 - l3 * l4;
  30. let det5 = l1 * l4 - l0 * l5;
  31. result.m[0] = l3 * detDiv; result.m[1] = -l1 * detDiv;
  32. result.m[2] = -l2 * detDiv; result.m[3] = l0 * detDiv;
  33. result.m[4] = det4 * detDiv; result.m[5] = det5 * detDiv;
  34. return this;
  35. }
  36. public multiplyToRef(other: Matrix2D, result: Matrix2D): Matrix2D {
  37. let l0 = this.m[0]; let l1 = this.m[1];
  38. let l2 = this.m[2]; let l3 = this.m[3];
  39. let l4 = this.m[4]; let l5 = this.m[5];
  40. let r0 = other.m[0]; let r1 = other.m[1];
  41. let r2 = other.m[2]; let r3 = other.m[3];
  42. let r4 = other.m[4]; let r5 = other.m[5];
  43. result.m[0] = l0 * r0 + l1 * r2; result.m[1] = l0 * r1 + l1 * r3;
  44. result.m[2] = l2 * r0 + l3 * r2; result.m[3] = l2 * r1 + l3 * r3;
  45. result.m[4] = l4 * r0 + l5 * r2 + r4; result.m[5] = l4 * r1 + l5 * r3 + r5;
  46. return this;
  47. }
  48. public transformCoordinates(x: number, y: number, result: Vector2): Matrix2D {
  49. result.x = x * this.m[0] + y * this.m[2] + this.m[4];
  50. result.y = x * this.m[1] + y * this.m[3] + this.m[5];
  51. return this;
  52. }
  53. // Statics
  54. public static Identity(): Matrix2D {
  55. return new Matrix2D(1, 0, 0, 1, 0, 0);
  56. }
  57. public static TranslationToRef(x: number, y: number, result: Matrix2D): void {
  58. result.fromValues(1, 0, 0, 1, x, y);
  59. }
  60. public static ScalingToRef(x: number, y: number, result: Matrix2D): void {
  61. result.fromValues(x, 0, 0, y, 0, 0);
  62. }
  63. public static RotationToRef(angle: number, result: Matrix2D): void {
  64. var s = Math.sin(angle);
  65. var c = Math.cos(angle);
  66. result.fromValues(c, s, -s, c, 0, 0);
  67. }
  68. private static _TempPreTranslationMatrix = Matrix2D.Identity();
  69. private static _TempPostTranslationMatrix = Matrix2D.Identity();
  70. private static _TempRotationMatrix = Matrix2D.Identity();
  71. private static _TempScalingMatrix = Matrix2D.Identity();
  72. private static _TempCompose0 = Matrix2D.Identity();
  73. private static _TempCompose1 = Matrix2D.Identity();
  74. private static _TempCompose2 = Matrix2D.Identity();
  75. public static ComposeToRef(tx: number, ty: number, angle: number, scaleX: number, scaleY: number, parentMatrix: Matrix2D, result: Matrix2D): void {
  76. Matrix2D.TranslationToRef(tx, ty, Matrix2D._TempPreTranslationMatrix);
  77. Matrix2D.ScalingToRef(scaleX, scaleY, Matrix2D._TempScalingMatrix);
  78. Matrix2D.RotationToRef(angle, Matrix2D._TempRotationMatrix);
  79. Matrix2D.TranslationToRef(-tx, -ty, Matrix2D._TempPostTranslationMatrix);
  80. Matrix2D._TempPreTranslationMatrix.multiplyToRef(Matrix2D._TempScalingMatrix, Matrix2D._TempCompose0);
  81. Matrix2D._TempCompose0.multiplyToRef(Matrix2D._TempRotationMatrix, Matrix2D._TempCompose1);
  82. if (parentMatrix) {
  83. Matrix2D._TempCompose1.multiplyToRef(Matrix2D._TempPostTranslationMatrix, Matrix2D._TempCompose2);
  84. Matrix2D._TempCompose2.multiplyToRef(parentMatrix, result);
  85. } else {
  86. Matrix2D._TempCompose1.multiplyToRef(Matrix2D._TempPostTranslationMatrix, result);
  87. }
  88. }
  89. }
  90. }