UNPKG

@turbox3d/math

Version:

Large-scale graphics application math library

181 lines 6.04 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import { Vector2 } from './Vector2'; import { Tolerance } from './Tolerance'; import { MathUtils } from '../MathUtils'; var _vector = new Vector2(); var Box2 = /*#__PURE__*/function () { function Box2(min, max) { _classCallCheck(this, Box2); this.isBox2 = true; this.min = min !== undefined ? min : new Vector2(+Infinity, +Infinity); this.max = max !== undefined ? max : new Vector2(-Infinity, -Infinity); } return _createClass(Box2, [{ key: "set", value: function set(min, max) { this.min.copy(min); this.max.copy(max); return this; } }, { key: "setFromPoints", value: function setFromPoints(points) { this.makeEmpty(); for (var i = 0, il = points.length; i < il; i++) { this.expandByPoint(points[i]); } return this; } }, { key: "setFromCenterAndSize", value: function setFromCenterAndSize(center, size) { var halfSize = _vector.copy(size).multiplyScalar(0.5); this.min.copy(center).sub(halfSize); this.max.copy(center).add(halfSize); return this; } }, { key: "clone", value: function clone() { return new Box2().copy(this); } }, { key: "copy", value: function copy(box) { this.min.copy(box.min); this.max.copy(box.max); return this; } }, { key: "makeEmpty", value: function makeEmpty() { // eslint-disable-next-line no-multi-assign this.min.x = this.min.y = +Infinity; // eslint-disable-next-line no-multi-assign this.max.x = this.max.y = -Infinity; return this; } }, { key: "isEmpty", value: function isEmpty() { // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes return this.max.x < this.min.x || this.max.y < this.min.y; } }, { key: "equals", value: function equals(box) { var distTol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Tolerance.global.distTol; return this.min.equals(box.min, distTol) && this.max.equals(box.max, distTol); } }, { key: "getCenter", value: function getCenter(target) { return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5); } }, { key: "getSize", value: function getSize(target) { return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min); } }, { key: "expandByPoint", value: function expandByPoint(point) { this.min.min(point); this.max.max(point); return this; } }, { key: "expandByVector", value: function expandByVector(vector) { this.min.sub(vector); this.max.add(vector); return this; } }, { key: "expandByScalar", value: function expandByScalar(scalar) { this.min.addScalar(-scalar); this.max.addScalar(scalar); return this; } }, { key: "containsPoint", value: function containsPoint(point) { var distTol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Tolerance.global.distTol; return !(MathUtils.isSmaller(point.x, this.min.x, distTol) || MathUtils.isBigger(point.x, this.max.x, distTol) || MathUtils.isSmaller(point.y, this.min.y, distTol) || MathUtils.isBigger(point.y, this.max.y, distTol)); } }, { key: "containsBox", value: function containsBox(box) { var distTol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Tolerance.global.distTol; return MathUtils.isSmallerOrEqual(this.min.x, box.min.x, distTol) && MathUtils.isSmallerOrEqual(box.max.x, this.max.x, distTol) && MathUtils.isSmallerOrEqual(this.min.y, box.min.y, distTol) && MathUtils.isSmallerOrEqual(box.max.y, this.max.y, distTol); } }, { key: "isValid", value: function isValid() { return this.min.x <= this.max.x && this.min.y <= this.max.y; } }, { key: "isOverlapping", value: function isOverlapping(box) { var distTol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Tolerance.global.distTol; if (!box.isValid()) { return false; } if (MathUtils.isSmaller(this.max.x, box.min.x, distTol) || MathUtils.isBigger(this.min.x, box.max.x, distTol)) { return false; } if (MathUtils.isSmaller(this.max.y, box.min.y, distTol) || MathUtils.isBigger(this.min.y, box.max.y, distTol)) { return false; } return true; } }, { key: "getParameter", value: function getParameter(point, target) { // This can potentially have a divide by zero if the box // has a size dimension of 0. return target.set((point.x - this.min.x) / (this.max.x - this.min.x), (point.y - this.min.y) / (this.max.y - this.min.y)); } }, { key: "intersectsBox", value: function intersectsBox(box) { // using 4 splitting planes to rule out intersections return !(box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y); } }, { key: "clampPoint", value: function clampPoint(point, target) { return target.copy(point).clamp(this.min, this.max); } }, { key: "distanceToPoint", value: function distanceToPoint(point) { var clampedPoint = _vector.copy(point).clamp(this.min, this.max); return clampedPoint.sub(point).length; } }, { key: "intersect", value: function intersect(box) { this.min.max(box.min); this.max.min(box.max); return this; } }, { key: "union", value: function union(box) { this.min.min(box.min); this.max.max(box.max); return this; } }, { key: "translate", value: function translate(offset) { this.min.add(offset); this.max.add(offset); return this; } }]); }(); export { Box2 };