math2D.ts 7.6 KB

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