math2D.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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,
  13. /** defines the current mouse button index */
  14. public buttonIndex: number = 0) {
  15. super(source.x, source.y);
  16. }
  17. }
  18. /** Class used to provide 2D matrix features */
  19. export class Matrix2D {
  20. /** Gets the internal array of 6 floats used to store matrix data */
  21. public m = new Float32Array(6);
  22. /**
  23. * Creates a new matrix
  24. * @param m00 defines value for (0, 0)
  25. * @param m01 defines value for (0, 1)
  26. * @param m10 defines value for (1, 0)
  27. * @param m11 defines value for (1, 1)
  28. * @param m20 defines value for (2, 0)
  29. * @param m21 defines value for (2, 1)
  30. */
  31. constructor(m00: number, m01: number, m10: number, m11: number, m20: number, m21: number) {
  32. this.fromValues(m00, m01, m10, m11, m20, m21);
  33. }
  34. /**
  35. * Fills the matrix from direct values
  36. * @param m00 defines value for (0, 0)
  37. * @param m01 defines value for (0, 1)
  38. * @param m10 defines value for (1, 0)
  39. * @param m11 defines value for (1, 1)
  40. * @param m20 defines value for (2, 0)
  41. * @param m21 defines value for (2, 1)
  42. * @returns the current modified matrix
  43. */
  44. public fromValues(m00: number, m01: number, m10: number, m11: number, m20: number, m21: number): Matrix2D {
  45. this.m[0] = m00; this.m[1] = m01;
  46. this.m[2] = m10; this.m[3] = m11;
  47. this.m[4] = m20; this.m[5] = m21;
  48. return this;
  49. }
  50. /**
  51. * Gets matrix determinant
  52. * @returns the determinant
  53. */
  54. public determinant(): number {
  55. return this.m[0] * this.m[3] - this.m[1] * this.m[2];
  56. }
  57. /**
  58. * Inverses the matrix and stores it in a target matrix
  59. * @param result defines the target matrix
  60. * @returns the current matrix
  61. */
  62. public invertToRef(result: Matrix2D): Matrix2D {
  63. let l0 = this.m[0]; let l1 = this.m[1];
  64. let l2 = this.m[2]; let l3 = this.m[3];
  65. let l4 = this.m[4]; let l5 = this.m[5];
  66. let det = this.determinant();
  67. if (det < (Epsilon * Epsilon)) {
  68. result.m[0] = 0; result.m[1] = 0;
  69. result.m[2] = 0; result.m[3] = 0;
  70. result.m[4] = 0; result.m[5] = 0;
  71. return this;
  72. }
  73. let detDiv = 1 / det;
  74. let det4 = l2 * l5 - l3 * l4;
  75. let det5 = l1 * l4 - l0 * l5;
  76. result.m[0] = l3 * detDiv; result.m[1] = -l1 * detDiv;
  77. result.m[2] = -l2 * detDiv; result.m[3] = l0 * detDiv;
  78. result.m[4] = det4 * detDiv; result.m[5] = det5 * detDiv;
  79. return this;
  80. }
  81. /**
  82. * Multiplies the current matrix with another one
  83. * @param other defines the second operand
  84. * @param result defines the target matrix
  85. * @returns the current matrix
  86. */
  87. public multiplyToRef(other: Matrix2D, result: Matrix2D): Matrix2D {
  88. let l0 = this.m[0]; let l1 = this.m[1];
  89. let l2 = this.m[2]; let l3 = this.m[3];
  90. let l4 = this.m[4]; let l5 = this.m[5];
  91. let r0 = other.m[0]; let r1 = other.m[1];
  92. let r2 = other.m[2]; let r3 = other.m[3];
  93. let r4 = other.m[4]; let r5 = other.m[5];
  94. result.m[0] = l0 * r0 + l1 * r2; result.m[1] = l0 * r1 + l1 * r3;
  95. result.m[2] = l2 * r0 + l3 * r2; result.m[3] = l2 * r1 + l3 * r3;
  96. result.m[4] = l4 * r0 + l5 * r2 + r4; result.m[5] = l4 * r1 + l5 * r3 + r5;
  97. return this;
  98. }
  99. /**
  100. * Apply the current matrix to a set of 2 floats and stores the result in a vector2
  101. * @param x defines the x coordinate to transform
  102. * @param y defines the x coordinate to transform
  103. * @param result defines the target vector2
  104. * @returns the current matrix
  105. */
  106. public transformCoordinates(x: number, y: number, result: Vector2): Matrix2D {
  107. result.x = x * this.m[0] + y * this.m[2] + this.m[4];
  108. result.y = x * this.m[1] + y * this.m[3] + this.m[5];
  109. return this;
  110. }
  111. // Statics
  112. /**
  113. * Creates an identity matrix
  114. * @returns a new matrix
  115. */
  116. public static Identity(): Matrix2D {
  117. return new Matrix2D(1, 0, 0, 1, 0, 0);
  118. }
  119. /**
  120. * Creates a translation matrix and stores it in a target matrix
  121. * @param x defines the x coordinate of the translation
  122. * @param y defines the y coordinate of the translation
  123. * @param result defines the target matrix
  124. */
  125. public static TranslationToRef(x: number, y: number, result: Matrix2D): void {
  126. result.fromValues(1, 0, 0, 1, x, y);
  127. }
  128. /**
  129. * Creates a scaling matrix and stores it in a target matrix
  130. * @param x defines the x coordinate of the scaling
  131. * @param y defines the y coordinate of the scaling
  132. * @param result defines the target matrix
  133. */
  134. public static ScalingToRef(x: number, y: number, result: Matrix2D): void {
  135. result.fromValues(x, 0, 0, y, 0, 0);
  136. }
  137. /**
  138. * Creates a rotation matrix and stores it in a target matrix
  139. * @param angle defines the rotation angle
  140. * @param result defines the target matrix
  141. */
  142. public static RotationToRef(angle: number, result: Matrix2D): void {
  143. var s = Math.sin(angle);
  144. var c = Math.cos(angle);
  145. result.fromValues(c, s, -s, c, 0, 0);
  146. }
  147. private static _TempPreTranslationMatrix = Matrix2D.Identity();
  148. private static _TempPostTranslationMatrix = Matrix2D.Identity();
  149. private static _TempRotationMatrix = Matrix2D.Identity();
  150. private static _TempScalingMatrix = Matrix2D.Identity();
  151. private static _TempCompose0 = Matrix2D.Identity();
  152. private static _TempCompose1 = Matrix2D.Identity();
  153. private static _TempCompose2 = Matrix2D.Identity();
  154. /**
  155. * Compose a matrix from translation, rotation, scaling and parent matrix and stores it in a target matrix
  156. * @param tx defines the x coordinate of the translation
  157. * @param ty defines the y coordinate of the translation
  158. * @param angle defines the rotation angle
  159. * @param scaleX defines the x coordinate of the scaling
  160. * @param scaleY defines the y coordinate of the scaling
  161. * @param parentMatrix defines the parent matrix to multiply by (can be null)
  162. * @param result defines the target matrix
  163. */
  164. public static ComposeToRef(tx: number, ty: number, angle: number, scaleX: number, scaleY: number, parentMatrix: Nullable<Matrix2D>, result: Matrix2D): void {
  165. Matrix2D.TranslationToRef(tx, ty, Matrix2D._TempPreTranslationMatrix);
  166. Matrix2D.ScalingToRef(scaleX, scaleY, Matrix2D._TempScalingMatrix);
  167. Matrix2D.RotationToRef(angle, Matrix2D._TempRotationMatrix);
  168. Matrix2D.TranslationToRef(-tx, -ty, Matrix2D._TempPostTranslationMatrix);
  169. Matrix2D._TempPreTranslationMatrix.multiplyToRef(Matrix2D._TempScalingMatrix, Matrix2D._TempCompose0);
  170. Matrix2D._TempCompose0.multiplyToRef(Matrix2D._TempRotationMatrix, Matrix2D._TempCompose1);
  171. if (parentMatrix) {
  172. Matrix2D._TempCompose1.multiplyToRef(Matrix2D._TempPostTranslationMatrix, Matrix2D._TempCompose2);
  173. Matrix2D._TempCompose2.multiplyToRef(parentMatrix, result);
  174. } else {
  175. Matrix2D._TempCompose1.multiplyToRef(Matrix2D._TempPostTranslationMatrix, result);
  176. }
  177. }
  178. }
  179. }