UNPKG

@turbox3d/math

Version:

Large-scale graphics application math library

187 lines (186 loc) 6.51 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Box2 = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _Vector = require("./Vector2"); var _Tolerance = require("./Tolerance"); var _MathUtils = require("../MathUtils"); var _vector = new _Vector.Vector2(); var Box2 = exports.Box2 = /*#__PURE__*/function () { function Box2(min, max) { (0, _classCallCheck2["default"])(this, Box2); this.isBox2 = true; this.min = min !== undefined ? min : new _Vector.Vector2(+Infinity, +Infinity); this.max = max !== undefined ? max : new _Vector.Vector2(-Infinity, -Infinity); } return (0, _createClass2["default"])(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.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.Tolerance.global.distTol; return !(_MathUtils.MathUtils.isSmaller(point.x, this.min.x, distTol) || _MathUtils.MathUtils.isBigger(point.x, this.max.x, distTol) || _MathUtils.MathUtils.isSmaller(point.y, this.min.y, distTol) || _MathUtils.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.Tolerance.global.distTol; return _MathUtils.MathUtils.isSmallerOrEqual(this.min.x, box.min.x, distTol) && _MathUtils.MathUtils.isSmallerOrEqual(box.max.x, this.max.x, distTol) && _MathUtils.MathUtils.isSmallerOrEqual(this.min.y, box.min.y, distTol) && _MathUtils.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.Tolerance.global.distTol; if (!box.isValid()) { return false; } if (_MathUtils.MathUtils.isSmaller(this.max.x, box.min.x, distTol) || _MathUtils.MathUtils.isBigger(this.min.x, box.max.x, distTol)) { return false; } if (_MathUtils.MathUtils.isSmaller(this.max.y, box.min.y, distTol) || _MathUtils.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; } }]); }();