math2D.ts 5.0 KB

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