math2D.ts 7.7 KB

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