measure.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { Matrix2D } from "./math2D";
  2. import { Vector2 } from "babylonjs/Maths/math.vector";
  3. let tmpRect = [
  4. new Vector2(0, 0),
  5. new Vector2(0, 0),
  6. new Vector2(0, 0),
  7. new Vector2(0, 0),
  8. ];
  9. let tmpRect2 = [
  10. new Vector2(0, 0),
  11. new Vector2(0, 0),
  12. new Vector2(0, 0),
  13. new Vector2(0, 0),
  14. ];
  15. let tmpV1 = new Vector2(0, 0);
  16. let tmpV2 = new Vector2(0, 0);
  17. /**
  18. * Class used to store 2D control sizes
  19. */
  20. export class Measure {
  21. /**
  22. * Creates a new measure
  23. * @param left defines left coordinate
  24. * @param top defines top coordinate
  25. * @param width defines width dimension
  26. * @param height defines height dimension
  27. */
  28. public constructor(
  29. /** defines left coordinate */
  30. public left: number,
  31. /** defines top coordinate */
  32. public top: number,
  33. /** defines width dimension */
  34. public width: number,
  35. /** defines height dimension */
  36. public height: number) {
  37. }
  38. /**
  39. * Copy from another measure
  40. * @param other defines the other measure to copy from
  41. */
  42. public copyFrom(other: Measure): void {
  43. this.left = other.left;
  44. this.top = other.top;
  45. this.width = other.width;
  46. this.height = other.height;
  47. }
  48. /**
  49. * Copy from a group of 4 floats
  50. * @param left defines left coordinate
  51. * @param top defines top coordinate
  52. * @param width defines width dimension
  53. * @param height defines height dimension
  54. */
  55. public copyFromFloats(left: number, top: number, width: number, height: number): void {
  56. this.left = left;
  57. this.top = top;
  58. this.width = width;
  59. this.height = height;
  60. }
  61. /**
  62. * Computes the axis aligned bounding box measure for two given measures
  63. * @param a Input measure
  64. * @param b Input measure
  65. * @param result the resulting bounding measure
  66. */
  67. public static CombineToRef(a: Measure, b: Measure, result: Measure) {
  68. var left = Math.min(a.left, b.left);
  69. var top = Math.min(a.top, b.top);
  70. var right = Math.max(a.left + a.width, b.left + b.width);
  71. var bottom = Math.max(a.top + a.height, b.top + b.height);
  72. result.left = left;
  73. result.top = top;
  74. result.width = right - left;
  75. result.height = bottom - top;
  76. }
  77. /**
  78. * Computes the axis aligned bounding box of the measure after it is modified by a given transform
  79. * @param transform the matrix to transform the measure before computing the AABB
  80. * @param result the resulting AABB
  81. */
  82. public transformToRef(transform: Matrix2D, result: Measure) {
  83. tmpRect[0].copyFromFloats(this.left, this.top);
  84. tmpRect[1].copyFromFloats(this.left + this.width, this.top);
  85. tmpRect[2].copyFromFloats(this.left + this.width, this.top + this.height);
  86. tmpRect[3].copyFromFloats(this.left, this.top + this.height);
  87. tmpV1.copyFromFloats(Number.MAX_VALUE, Number.MAX_VALUE);
  88. tmpV2.copyFromFloats(0, 0);
  89. for (var i = 0; i < 4; i++) {
  90. transform.transformCoordinates(tmpRect[i].x, tmpRect[i].y, tmpRect2[i]);
  91. tmpV1.x = Math.floor(Math.min(tmpV1.x, tmpRect2[i].x));
  92. tmpV1.y = Math.floor(Math.min(tmpV1.y, tmpRect2[i].y));
  93. tmpV2.x = Math.ceil(Math.max(tmpV2.x, tmpRect2[i].x));
  94. tmpV2.y = Math.ceil(Math.max(tmpV2.y, tmpRect2[i].y));
  95. }
  96. result.left = tmpV1.x;
  97. result.top = tmpV1.y;
  98. result.width = tmpV2.x - tmpV1.x;
  99. result.height = tmpV2.y - tmpV1.y;
  100. }
  101. /**
  102. * Check equality between this measure and another one
  103. * @param other defines the other measures
  104. * @returns true if both measures are equals
  105. */
  106. public isEqualsTo(other: Measure): boolean {
  107. if (this.left !== other.left) {
  108. return false;
  109. }
  110. if (this.top !== other.top) {
  111. return false;
  112. }
  113. if (this.width !== other.width) {
  114. return false;
  115. }
  116. if (this.height !== other.height) {
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Creates an empty measure
  123. * @returns a new measure
  124. */
  125. public static Empty(): Measure {
  126. return new Measure(0, 0, 0, 0);
  127. }
  128. }