babylon.bounding2d.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.extent = new BABYLON.Size(0, 0);
  10. }
  11. /**
  12. * Duplicate this instance and return a new one
  13. * @return the duplicated instance
  14. */
  15. BoundingInfo2D.prototype.clone = function () {
  16. var r = new BoundingInfo2D();
  17. r.radius = this.radius;
  18. r.extent = this.extent.clone();
  19. return r;
  20. };
  21. /**
  22. * Apply a transformation matrix to this BoundingInfo2D and return a new instance containing the result
  23. * @param matrix the transformation matrix to apply
  24. * @return the new instance containing the result of the transformation applied on this BoundingInfo2D
  25. */
  26. BoundingInfo2D.prototype.transform = function (matrix) {
  27. var r = new BoundingInfo2D();
  28. this.transformToRef(matrix, r);
  29. return r;
  30. };
  31. /**
  32. * Compute the union of this BoundingInfo2D with a given one, return a new BoundingInfo2D as a result
  33. * @param other the second BoundingInfo2D to compute the union with this one
  34. * @return a new instance containing the result of the union
  35. */
  36. BoundingInfo2D.prototype.union = function (other) {
  37. var r = new BoundingInfo2D();
  38. this.unionToRef(other, r);
  39. return r;
  40. };
  41. /**
  42. * Transform this BoundingInfo2D with a given matrix and store the result in an existing BoundingInfo2D instance.
  43. * 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 everytime.
  44. * @param matrix The matrix to use to compute the transformation
  45. * @param result A VALID (i.e. allocated) BoundingInfo2D object where the result will be stored
  46. */
  47. BoundingInfo2D.prototype.transformToRef = function (matrix, result) {
  48. // Extract scale from matrix
  49. var xs = BABYLON.MathTools.Sign(matrix.m[0] * matrix.m[1] * matrix.m[2] * matrix.m[3]) < 0 ? -1 : 1;
  50. var ys = BABYLON.MathTools.Sign(matrix.m[4] * matrix.m[5] * matrix.m[6] * matrix.m[7]) < 0 ? -1 : 1;
  51. var scaleX = xs * Math.sqrt(matrix.m[0] * matrix.m[0] + matrix.m[1] * matrix.m[1] + matrix.m[2] * matrix.m[2]);
  52. var scaleY = ys * Math.sqrt(matrix.m[4] * matrix.m[4] + matrix.m[5] * matrix.m[5] + matrix.m[6] * matrix.m[6]);
  53. // Get translation
  54. var trans = matrix.getTranslation();
  55. var transLength = trans.length();
  56. if (transLength < BABYLON.Epsilon) {
  57. result.radius = this.radius * Math.max(scaleX, scaleY);
  58. }
  59. else {
  60. // Compute the radius vector by applying the transformation matrix manually
  61. var rx = (trans.x / transLength) * (transLength + this.radius) * scaleX;
  62. var ry = (trans.y / transLength) * (transLength + this.radius) * scaleY;
  63. // Store the vector length as the new radius
  64. result.radius = Math.sqrt(rx * rx + ry * ry);
  65. }
  66. // Construct a bounding box based on the extent values
  67. var p = new Array(4);
  68. p[0] = new BABYLON.Vector2(this.extent.width, this.extent.height);
  69. p[1] = new BABYLON.Vector2(this.extent.width, -this.extent.height);
  70. p[2] = new BABYLON.Vector2(-this.extent.width, -this.extent.height);
  71. p[3] = new BABYLON.Vector2(-this.extent.width, this.extent.height);
  72. // Transform the four points of the bounding box with the matrix
  73. for (var i = 0; i < 4; i++) {
  74. p[i] = BABYLON.Vector2.Transform(p[i], matrix);
  75. }
  76. // Take the first point as reference
  77. var maxW = Math.abs(p[0].x), maxH = Math.abs(p[0].y);
  78. // Parse the three others, compare them to the reference and keep the biggest
  79. for (var i = 1; i < 4; i++) {
  80. maxW = Math.max(Math.abs(p[i].x), maxW);
  81. maxH = Math.max(Math.abs(p[i].y), maxH);
  82. }
  83. // Store the new extent
  84. result.extent.width = maxW * scaleX;
  85. result.extent.height = maxH * scaleY;
  86. };
  87. /**
  88. * Compute the union of this BoundingInfo2D with another one and store the result in a third valid BoundingInfo2D object
  89. * 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 everytime.
  90. * @param other the second object used to compute the union
  91. * @param result a VALID BoundingInfo2D instance (i.e. allocated) where the result will be stored
  92. */
  93. BoundingInfo2D.prototype.unionToRef = function (other, result) {
  94. result.radius = Math.max(this.radius, other.radius);
  95. result.extent.width = Math.max(this.extent.width, other.extent.width);
  96. result.extent.height = Math.max(this.extent.height, other.extent.height);
  97. };
  98. return BoundingInfo2D;
  99. }());
  100. BABYLON.BoundingInfo2D = BoundingInfo2D;
  101. })(BABYLON || (BABYLON = {}));
  102. //# sourceMappingURL=babylon.bounding2d.js.map