babylon.bounding2d.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /**
  14. * Create a BoundingInfo2D object from a given size
  15. * @param size the size that will be used to set the extend, radius will be computed from it.
  16. */
  17. BoundingInfo2D.CreateFromSize = function (size) {
  18. var r = new BoundingInfo2D();
  19. BoundingInfo2D.CreateFromSizeToRef(size, r);
  20. return r;
  21. };
  22. /**
  23. * Create a BoundingInfo2D object from a given radius
  24. * @param radius the radius to use, the extent will be computed from it.
  25. */
  26. BoundingInfo2D.CreateFromRadius = function (radius) {
  27. var r = new BoundingInfo2D();
  28. BoundingInfo2D.CreateFromRadiusToRef(radius, r);
  29. return r;
  30. };
  31. /**
  32. * Create a BoundingInfo2D object from a list of points.
  33. * The resulted object will be the smallest bounding area that includes all the given points.
  34. * @param points an array of points to compute the bounding object from.
  35. */
  36. BoundingInfo2D.CreateFromPoints = function (points) {
  37. var r = new BoundingInfo2D();
  38. BoundingInfo2D.CreateFromPointsToRef(points, r);
  39. return r;
  40. };
  41. /**
  42. * Update a BoundingInfo2D object using the given Size as input
  43. * @param size the bounding data will be computed from this size.
  44. * @param b must be a valid/allocated object, it will contain the result of the operation
  45. */
  46. BoundingInfo2D.CreateFromSizeToRef = function (size, b) {
  47. if (!size) {
  48. size = BABYLON.Size.Zero();
  49. }
  50. b.center.x = +size.width / 2;
  51. b.center.y = +size.height / 2;
  52. b.extent.x = b.center.x;
  53. b.extent.y = b.center.y;
  54. b.radius = b.extent.length();
  55. };
  56. /**
  57. * Update a BoundingInfo2D object using the given radius as input
  58. * @param radius the bounding data will be computed from this radius
  59. * @param b must be a valid/allocated object, it will contain the result of the operation
  60. */
  61. BoundingInfo2D.CreateFromRadiusToRef = function (radius, b) {
  62. b.center.x = b.center.y = 0;
  63. var r = +radius;
  64. b.extent.x = r;
  65. b.extent.y = r;
  66. b.radius = r;
  67. };
  68. /**
  69. * Update a BoundingInfo2D object using the given points array as input
  70. * @param points the point array to use to update the bounding data
  71. * @param b must be a valid/allocated object, it will contain the result of the operation
  72. */
  73. BoundingInfo2D.CreateFromPointsToRef = function (points, b) {
  74. var xmin = Number.MAX_VALUE, ymin = Number.MAX_VALUE, xmax = Number.MIN_VALUE, ymax = Number.MIN_VALUE;
  75. for (var _i = 0; _i < points.length; _i++) {
  76. var p = points[_i];
  77. xmin = Math.min(p.x, xmin);
  78. xmax = Math.max(p.x, xmax);
  79. ymin = Math.min(p.y, ymin);
  80. ymax = Math.max(p.y, ymax);
  81. }
  82. BoundingInfo2D.CreateFromMinMaxToRef(xmin, xmax, ymin, ymax, b);
  83. };
  84. /**
  85. * Update a BoundingInfo2D object using the given min/max values as input
  86. * @param xmin the smallest x coordinate
  87. * @param xmax the biggest x coordinate
  88. * @param ymin the smallest y coordinate
  89. * @param ymax the buggest y coordinate
  90. * @param b must be a valid/allocated object, it will contain the result of the operation
  91. */
  92. BoundingInfo2D.CreateFromMinMaxToRef = function (xmin, xmax, ymin, ymax, b) {
  93. var w = xmax - xmin;
  94. var h = ymax - ymin;
  95. b.center = new BABYLON.Vector2(xmin + w / 2, ymin + h / 2);
  96. b.extent = new BABYLON.Vector2(xmax - b.center.x, ymax - b.center.y);
  97. b.radius = b.extent.length();
  98. };
  99. /**
  100. * Duplicate this instance and return a new one
  101. * @return the duplicated instance
  102. */
  103. BoundingInfo2D.prototype.clone = function () {
  104. var r = new BoundingInfo2D();
  105. r.center = this.center.clone();
  106. r.radius = this.radius;
  107. r.extent = this.extent.clone();
  108. return r;
  109. };
  110. BoundingInfo2D.prototype.clear = function () {
  111. this.center.copyFromFloats(0, 0);
  112. this.radius = 0;
  113. this.extent.copyFromFloats(0, 0);
  114. };
  115. BoundingInfo2D.prototype.copyFrom = function (src) {
  116. this.center.copyFrom(src.center);
  117. this.radius = src.radius;
  118. this.extent.copyFrom(src.extent);
  119. };
  120. /**
  121. * return the max extend of the bounding info
  122. */
  123. BoundingInfo2D.prototype.max = function () {
  124. var r = BABYLON.Vector2.Zero();
  125. this.maxToRef(r);
  126. return r;
  127. };
  128. /**
  129. * Update a vector2 with the max extend of the bounding info
  130. * @param result must be a valid/allocated vector2 that will contain the result of the operation
  131. */
  132. BoundingInfo2D.prototype.maxToRef = function (result) {
  133. result.x = this.center.x + this.extent.x;
  134. result.y = this.center.y + this.extent.y;
  135. };
  136. /**
  137. * Apply a transformation matrix to this BoundingInfo2D and return a new instance containing the result
  138. * @param matrix the transformation matrix to apply
  139. * @return the new instance containing the result of the transformation applied on this BoundingInfo2D
  140. */
  141. BoundingInfo2D.prototype.transform = function (matrix) {
  142. var r = new BoundingInfo2D();
  143. this.transformToRef(matrix, r);
  144. return r;
  145. };
  146. /**
  147. * Compute the union of this BoundingInfo2D with a given one, returns a new BoundingInfo2D as a result
  148. * @param other the second BoundingInfo2D to compute the union with this one
  149. * @return a new instance containing the result of the union
  150. */
  151. BoundingInfo2D.prototype.union = function (other) {
  152. var r = new BoundingInfo2D();
  153. this.unionToRef(other, r);
  154. return r;
  155. };
  156. /**
  157. * Transform this BoundingInfo2D with a given matrix and store the result in an existing BoundingInfo2D instance.
  158. * 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.
  159. * @param matrix The matrix to use to compute the transformation
  160. * @param result A VALID (i.e. allocated) BoundingInfo2D object where the result will be stored
  161. */
  162. BoundingInfo2D.prototype.transformToRef = function (matrix, result) {
  163. // Construct a bounding box based on the extent values
  164. var p = BoundingInfo2D._transform;
  165. p[0].x = this.center.x + this.extent.x;
  166. p[0].y = this.center.y + this.extent.y;
  167. p[1].x = this.center.x + this.extent.x;
  168. p[1].y = this.center.y - this.extent.y;
  169. p[2].x = this.center.x - this.extent.x;
  170. p[2].y = this.center.y - this.extent.y;
  171. p[3].x = this.center.x - this.extent.x;
  172. p[3].y = this.center.y + this.extent.y;
  173. // Transform the four points of the bounding box with the matrix
  174. for (var i = 0; i < 4; i++) {
  175. BABYLON.Vector2.TransformToRef(p[i], matrix, p[i]);
  176. }
  177. BoundingInfo2D.CreateFromPointsToRef(p, result);
  178. };
  179. /**
  180. * Compute the union of this BoundingInfo2D with another one and store the result in a third valid BoundingInfo2D object
  181. * 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.
  182. * @param other the second object used to compute the union
  183. * @param result a VALID BoundingInfo2D instance (i.e. allocated) where the result will be stored
  184. */
  185. BoundingInfo2D.prototype.unionToRef = function (other, result) {
  186. var xmax = Math.max(this.center.x + this.extent.x, other.center.x + other.extent.x);
  187. var ymax = Math.max(this.center.y + this.extent.y, other.center.y + other.extent.y);
  188. var xmin = Math.min(this.center.x - this.extent.x, other.center.x - other.extent.x);
  189. var ymin = Math.min(this.center.y - this.extent.y, other.center.y - other.extent.y);
  190. BoundingInfo2D.CreateFromMinMaxToRef(xmin, xmax, ymin, ymax, result);
  191. };
  192. /**
  193. * Check if the given point is inside the BoundingInfo.
  194. * The test is first made on the radius, then inside the rectangle described by the extent
  195. * @param pickPosition the position to test
  196. * @return true if the point is inside, false otherwise
  197. */
  198. BoundingInfo2D.prototype.doesIntersect = function (pickPosition) {
  199. // is it inside the radius?
  200. var pickLocal = pickPosition.subtract(this.center);
  201. if (pickLocal.lengthSquared() <= (this.radius * this.radius)) {
  202. // is it inside the rectangle?
  203. return ((Math.abs(pickLocal.x) <= this.extent.x) && (Math.abs(pickLocal.y) <= this.extent.y));
  204. }
  205. return false;
  206. };
  207. BoundingInfo2D._transform = new Array(BABYLON.Vector2.Zero(), BABYLON.Vector2.Zero(), BABYLON.Vector2.Zero(), BABYLON.Vector2.Zero());
  208. return BoundingInfo2D;
  209. })();
  210. BABYLON.BoundingInfo2D = BoundingInfo2D;
  211. })(BABYLON || (BABYLON = {}));