vecks
Version:
2D and 3D Vector Algebra library
106 lines (88 loc) • 3.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _V = _interopRequireDefault(require("./V3"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Box3 = /*#__PURE__*/function () {
function Box3(min, max) {
_classCallCheck(this, Box3);
if (_typeof(min) === 'object' && _typeof(max) === 'object' && min.x !== undefined && min.y !== undefined && min.z !== undefined && max.x !== undefined && max.y !== undefined && max.z !== undefined) {
this.min = new _V["default"](min);
this.max = new _V["default"](max);
this.valid = true;
} else if (min === undefined && max === undefined) {
this.min = new _V["default"](Infinity, Infinity, Infinity);
this.max = new _V["default"](-Infinity, -Infinity, -Infinity);
this.valid = false;
} else {
throw Error('Illegal construction - must use { x, y, z } objects');
}
}
_createClass(Box3, [{
key: "equals",
value: function equals(other) {
if (!this.valid) {
throw Error('Box3 is invalid');
}
return this.min.equals(other.min) && this.max.equals(other.max);
}
}, {
key: "expandByPoint",
value: function expandByPoint(p) {
this.min = new _V["default"](Math.min(this.min.x, p.x), Math.min(this.min.y, p.y), Math.min(this.min.z, p.z));
this.max = new _V["default"](Math.max(this.max.x, p.x), Math.max(this.max.y, p.y), Math.max(this.max.z, p.z));
this.valid = true;
return this;
}
}, {
key: "expandByPoints",
value: function expandByPoints(points) {
var _this = this;
points.forEach(function (point) {
_this.expandByPoint(point);
}, this);
return this;
}
}, {
key: "isPointInside",
value: function isPointInside(p) {
return p.x >= this.min.x && p.y >= this.min.y && p.z >= this.min.z && p.x <= this.max.x && p.y <= this.max.y && p.z <= this.max.z;
}
}, {
key: "width",
get: function get() {
if (!this.valid) {
throw Error('Box3 is invalid');
}
return this.max.x - this.min.x;
}
}, {
key: "depth",
get: function get() {
if (!this.valid) {
throw Error('Box3 is invalid');
}
return this.max.y - this.min.y;
}
}, {
key: "height",
get: function get() {
if (!this.valid) {
throw Error('Box3 is invalid');
}
return this.max.z - this.min.z;
}
}]);
return Box3;
}();
Box3.fromPoints = function (points) {
return new Box3().expandByPoints(points);
};
var _default = Box3;
exports["default"] = _default;