math2D.ts 5.2 KB

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