babylon.bounding2d.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var BABYLON;
  2. (function (BABYLON) {
  3. /**
  4. * Stores 2D Bounding Information.
  5. * This class handles a circle area and a bounding rectangle one.
  6. */
  7. var BoundingInfo2D = (function () {
  8. function BoundingInfo2D() {
  9. this.radius = 0;
  10. this.center = BABYLON.Vector2.Zero();
  11. this.extent = BABYLON.Vector2.Zero();
  12. }
  13. BoundingInfo2D.CreateFromSize = function (size, origin) {
  14. var r = new BoundingInfo2D();
  15. BoundingInfo2D.CreateFromSizeToRef(size, r, origin);
  16. return r;
  17. };
  18. BoundingInfo2D.CreateFromRadius = function (radius, origin) {
  19. var r = new BoundingInfo2D();
  20. BoundingInfo2D.CreateFromRadiusToRef(radius, r, origin);
  21. return r;
  22. };
  23. BoundingInfo2D.CreateFromPoints = function (points, origin) {
  24. var r = new BoundingInfo2D();
  25. BoundingInfo2D.CreateFromPointsToRef(points, r, origin);
  26. return r;
  27. };
  28. BoundingInfo2D.CreateFromSizeToRef = function (size, b, origin) {
  29. b.center = new BABYLON.Vector2(size.width / 2, size.height / 2);
  30. b.extent = b.center.clone();
  31. if (origin) {
  32. b.center.x -= size.width * origin.x;
  33. b.center.y -= size.height * origin.y;
  34. }
  35. b.radius = b.extent.length();
  36. };
  37. BoundingInfo2D.CreateFromRadiusToRef = function (radius, b, origin) {
  38. b.center = BABYLON.Vector2.Zero();
  39. if (origin) {
  40. b.center.x -= radius * origin.x;
  41. b.center.y -= radius * origin.y;
  42. }
  43. b.extent = new BABYLON.Vector2(radius, radius);
  44. b.radius = radius;
  45. };
  46. BoundingInfo2D.CreateFromPointsToRef = function (points, b, origin) {
  47. var xmin = Number.MAX_VALUE, ymin = Number.MAX_VALUE, xmax = Number.MIN_VALUE, ymax = Number.MIN_VALUE;
  48. for (var _i = 0; _i < points.length; _i++) {
  49. var p = points[_i];
  50. xmin = Math.min(p.x, xmin);
  51. xmax = Math.max(p.x, xmax);
  52. ymin = Math.min(p.y, ymin);
  53. ymax = Math.max(p.y, ymax);
  54. }
  55. BoundingInfo2D.CreateFromMinMaxToRef(xmin, xmax, ymin, ymax, b, origin);
  56. };
  57. BoundingInfo2D.CreateFromMinMaxToRef = function (xmin, xmax, ymin, ymax, b, origin) {
  58. var w = xmax - xmin;
  59. var h = ymax - ymin;
  60. b.center = new BABYLON.Vector2(xmin + w / 2, ymin + h / 2);
  61. if (origin) {
  62. b.center.x -= w * origin.x;
  63. b.center.y -= h * origin.y;
  64. }
  65. b.extent = new BABYLON.Vector2(xmax - b.center.x, ymax - b.center.y);
  66. b.radius = b.extent.length();
  67. };
  68. /**
  69. * Duplicate this instance and return a new one
  70. * @return the duplicated instance
  71. */
  72. BoundingInfo2D.prototype.clone = function () {
  73. var r = new BoundingInfo2D();
  74. r.center = this.center.clone();
  75. r.radius = this.radius;
  76. r.extent = this.extent.clone();
  77. return r;
  78. };
  79. BoundingInfo2D.prototype.max = function () {
  80. var r = BABYLON.Vector2.Zero();
  81. this.maxToRef(r);
  82. return r;
  83. };
  84. BoundingInfo2D.prototype.maxToRef = function (result) {
  85. result.x = this.center.x + this.extent.x;
  86. result.y = this.center.y + this.extent.y;
  87. };
  88. /**
  89. * Apply a transformation matrix to this BoundingInfo2D and return a new instance containing the result
  90. * @param matrix the transformation matrix to apply
  91. * @return the new instance containing the result of the transformation applied on this BoundingInfo2D
  92. */
  93. BoundingInfo2D.prototype.transform = function (matrix) {
  94. var r = new BoundingInfo2D();
  95. this.transformToRef(matrix, r);
  96. return r;
  97. };
  98. /**
  99. * Compute the union of this BoundingInfo2D with a given one, return a new BoundingInfo2D as a result
  100. * @param other the second BoundingInfo2D to compute the union with this one
  101. * @return a new instance containing the result of the union
  102. */
  103. BoundingInfo2D.prototype.union = function (other) {
  104. var r = new BoundingInfo2D();
  105. this.unionToRef(other, r);
  106. return r;
  107. };
  108. /**
  109. * Transform this BoundingInfo2D with a given matrix and store the result in an existing BoundingInfo2D instance.
  110. * This is a GC friendly version, try to use it as much as possible, specially if your transformation is inside a loop, allocate the result object once for good outside of the loop and use it every time.
  111. * @param matrix The matrix to use to compute the transformation
  112. * @param result A VALID (i.e. allocated) BoundingInfo2D object where the result will be stored
  113. */
  114. BoundingInfo2D.prototype.transformToRef = function (matrix, result) {
  115. // Construct a bounding box based on the extent values
  116. var p = BoundingInfo2D._transform;
  117. p[0].x = this.center.x + this.extent.x;
  118. p[0].y = this.center.y + this.extent.y;
  119. p[1].x = this.center.x + this.extent.x;
  120. p[1].y = this.center.y - this.extent.y;
  121. p[2].x = this.center.x - this.extent.x;
  122. p[2].y = this.center.y - this.extent.y;
  123. p[3].x = this.center.x - this.extent.x;
  124. p[3].y = this.center.y + this.extent.y;
  125. // Transform the four points of the bounding box with the matrix
  126. for (var i = 0; i < 4; i++) {
  127. BABYLON.Vector2.TransformToRef(p[i], matrix, p[i]);
  128. }
  129. BoundingInfo2D.CreateFromPointsToRef(p, result);
  130. };
  131. /**
  132. * Compute the union of this BoundingInfo2D with another one and store the result in a third valid BoundingInfo2D object
  133. * This is a GC friendly version, try to use it as much as possible, specially if your transformation is inside a loop, allocate the result object once for good outside of the loop and use it every time.
  134. * @param other the second object used to compute the union
  135. * @param result a VALID BoundingInfo2D instance (i.e. allocated) where the result will be stored
  136. */
  137. BoundingInfo2D.prototype.unionToRef = function (other, result) {
  138. var xmax = Math.max(this.center.x + this.extent.x, other.center.x + other.extent.x);
  139. var ymax = Math.max(this.center.y + this.extent.y, other.center.y + other.extent.y);
  140. var xmin = Math.min(this.center.x - this.extent.x, other.center.x - other.extent.x);
  141. var ymin = Math.min(this.center.y - this.extent.y, other.center.y - other.extent.y);
  142. BoundingInfo2D.CreateFromMinMaxToRef(xmin, xmax, ymin, ymax, result);
  143. };
  144. BoundingInfo2D.prototype.doesIntersect = function (pickPosition) {
  145. // is it inside the radius?
  146. var pickLocal = pickPosition.subtract(this.center);
  147. if (pickLocal.lengthSquared() <= (this.radius * this.radius)) {
  148. // is it inside the rectangle?
  149. return ((Math.abs(pickLocal.x) <= this.extent.x) && (Math.abs(pickLocal.y) <= this.extent.y));
  150. }
  151. return false;
  152. };
  153. BoundingInfo2D._transform = new Array(BABYLON.Vector2.Zero(), BABYLON.Vector2.Zero(), BABYLON.Vector2.Zero(), BABYLON.Vector2.Zero());
  154. return BoundingInfo2D;
  155. })();
  156. BABYLON.BoundingInfo2D = BoundingInfo2D;
  157. })(BABYLON || (BABYLON = {}));