UNPKG

xtorcga

Version:

Xtor Compute Geometry Algorithm Libary 计算几何算法库

1,900 lines (1,586 loc) 367 kB
/** * https://github.com/yszhao91/cga.js *CGA Lib |cga.js |alex Zhao | Zhao yaosheng *@license free for all */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.cga = factory()); }(this, (function () { 'use strict'; var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function unwrapExports (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var array = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.unique = exports.classify = exports.flat = exports.forall = void 0; Array.prototype.get = function (index) { if (index < 0) index = this.length + index; return this[index]; }; Array.prototype.last = function () { return this.get(-1); }; /** * 遍历多级数组中所有对象 * @param {Array} array * @param {Function} method */ function forall(array, method) { for (var i = 0; i < array.length; i++) { var ele = array[i]; method(ele, i, array); if (Array.isArray(ele)) forall(ele, method); } } exports.forall = forall; function flat(array) { if (array.flat) return array.flat(Infinity); return array.reduce(function (pre, cur) { return pre.concat(Array.isArray(cur) ? flat(cur) : cur); }); } exports.flat = flat; /** * 分类 * example: * var arry = [1,2,3,4,5,6] * var result = classify(array,(a)={return a%2===0}) * * @param {Array} array * @param {Function} classifyMethod 分类方法 */ function classify(array, classifyMethod) { var result = []; for (var i = 0; i < array.length; i++) { for (var j = 0; j < result.length; j++) { if (classifyMethod(array[i], result[j][0], result[j])) { result[j].push(array[i]); } else { result.push([array[i]]); } } } return result; } exports.classify = classify; /** * 去掉重复元素 * @param {Array} array * @param {Function} uniqueMethod 去重复 * @param {Function} sortMethod 排序 存在就先排序再去重复 */ function unique(array, uniqueMethod, sortMethod) { if (sortMethod) { array.sort(sortMethod); for (var i = 0; i < array.length; i++) { for (var j = i + 1; j < array.length; j++) { if (uniqueMethod(array[i], array[j]) === true) { array.splice(j, 1); j--; } else break; } } return array; } for (var i = 0; i < array.length; i++) { for (var j = i + 1; j < array.length; j++) { if (uniqueMethod(array[i], array[j]) === true) { array.splice(j, 1); j--; } } } return array; } exports.unique = unique; }); unwrapExports(array); var array_1 = array.unique; var array_2 = array.classify; var array_3 = array.flat; var array_4 = array.forall; var eventhandler = createCommonjsModule(function (module, exports) { var __spreadArrays = commonjsGlobal && commonjsGlobal.__spreadArrays || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventHandler = void 0; var EventHandler = /** @class */ function () { function EventHandler() { this._callbacks = {}; this._callbackActive = {}; } EventHandler.prototype._addCallback = function (name, callback, scope, once) { if (!name || typeof name !== 'string' || !callback) return; if (!this._callbacks[name]) this._callbacks[name] = []; if (this._callbackActive[name] && this._callbackActive[name] === this._callbacks[name]) this._callbackActive[name] = this._callbackActive[name].slice(); this._callbacks[name].push({ callback: callback, scope: scope || this, once: once || false }); }; /** * @function * @name EventHandler#on * @description Attach an event handler to an event. * @param {string} name - Name of the event to bind the callback to. * @param {callbacks.HandleEvent} callback - Function that is called when event is fired. Note the callback is limited to 8 arguments. * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to current this. * @returns {EventHandler} Self for chaining. * @example * obj.on('test', function (a, b) { * console.log(a + b); * }); * obj.fire('test', 1, 2); // prints 3 to the console */ EventHandler.prototype.on = function (name, callback, scope) { this._addCallback(name, callback, scope, false); return this; }; /** * @function * @name EventHandler#off * @description Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, * if scope is not provided then all events with the callback will be unbound. * @param {string} [name] - Name of the event to unbind. * @param {callbacks.HandleEvent} [callback] - Function to be unbound. * @param {object} [scope] - Scope that was used as the this when the event is fired. * @returns {EventHandler} Self for chaining. * @example * var handler = function () { * }; * obj.on('test', handler); * * obj.off(); // Removes all events * obj.off('test'); // Removes all events called 'test' * obj.off('test', handler); // Removes all handler functions, called 'test' * obj.off('test', handler, this); // Removes all hander functions, called 'test' with scope this */ EventHandler.prototype.off = function (name, callback, scope) { if (name) { if (this._callbackActive[name] && this._callbackActive[name] === this._callbacks[name]) this._callbackActive[name] = this._callbackActive[name].slice(); } else { for (var key in this._callbackActive) { if (!this._callbacks[key]) continue; if (this._callbacks[key] !== this._callbackActive[key]) continue; this._callbackActive[key] = this._callbackActive[key].slice(); } } if (!name) { this._callbacks = {}; } else if (!callback) { if (this._callbacks[name]) this._callbacks[name] = []; } else { var events = this._callbacks[name]; if (!events) return this; var count = events.length; for (var i = 0; i < count; i++) { if (events[i].callback !== callback) continue; if (scope && events[i].scope !== scope) continue; events[i--] = events[--count]; } events.length = count; } return this; }; // ESLint rule disabled here as documenting arg1, arg2...argN as [...] rest // arguments is preferable to documenting each one individually. /* eslint-disable valid-jsdoc */ /** * @function * @name EventHandler#fire * @description Fire an event, all additional arguments are passed on to the event listener. * @param {object} name - Name of event to fire. * @param {*} [args] - arguments that is passed to the event handler. * obj.fire('test', 'This is the message'); */ /* eslint-enable valid-jsdoc */ EventHandler.prototype.fire = function (name) { var _a; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } if (!name || !this._callbacks[name]) return this; var callbacks; if (!this._callbackActive[name]) { this._callbackActive[name] = this._callbacks[name]; } else { if (this._callbackActive[name] === this._callbacks[name]) this._callbackActive[name] = this._callbackActive[name].slice(); callbacks = this._callbacks[name].slice(); } // TODO: What does callbacks do here? // In particular this condition check looks wrong: (i < (callbacks || this._callbackActive[name]).length) // Because callbacks is not an integer // eslint-disable-next-line no-unmodified-loop-condition for (var i = 0; (callbacks || this._callbackActive[name]) && i < (callbacks || this._callbackActive[name]).length; i++) { var evt = (callbacks || this._callbackActive[name])[i]; (_a = evt.callback).call.apply(_a, __spreadArrays([evt.scope], args)); if (evt.once) { var ind = this._callbacks[name].indexOf(evt); if (ind !== -1) { if (this._callbackActive[name] === this._callbacks[name]) this._callbackActive[name] = this._callbackActive[name].slice(); this._callbacks[name].splice(ind, 1); } } } if (!callbacks) this._callbackActive[name] = null; return this; }; /** * @function * @name EventHandler#once * @description Attach an event handler to an event. This handler will be removed after being fired once. * @param {string} name - Name of the event to bind the callback to. * @param {callbacks.HandleEvent} callback - Function that is called when event is fired. Note the callback is limited to 8 arguments. * @param {object} [scope] - Object to use as 'this' when the event is fired, defaults to current this. * @returns {EventHandler} Self for chaining. * @example * obj.once('test', function (a, b) { * console.log(a + b); * }); * obj.fire('test', 1, 2); // prints 3 to the console * obj.fire('test', 1, 2); // not going to get handled */ EventHandler.prototype.once = function (name, callback, scope) { this._addCallback(name, callback, scope, true); return this; }; /** * @function * @name EventHandler#hasEvent * @description Test if there are any handlers bound to an event name. * @param {string} name - The name of the event to test. * @returns {boolean} True if the object has handlers bound to the specified event name. * @example * obj.on('test', function () { }); // bind an event to 'test' * obj.hasEvent('test'); // returns true * obj.hasEvent('hello'); // returns false */ EventHandler.prototype.hasEvent = function (name) { return this._callbacks[name] && this._callbacks[name].length !== 0 || false; }; return EventHandler; }(); exports.EventHandler = EventHandler; }); unwrapExports(eventhandler); var eventhandler_1 = eventhandler.EventHandler; var Vec2_1 = createCommonjsModule(function (module, exports) { var __extends = commonjsGlobal && commonjsGlobal.__extends || function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; } || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; }(); Object.defineProperty(exports, "__esModule", { value: true }); exports.v2 = exports.Vec2 = void 0; var Vec2 = /** @class */ function (_super) { __extends(Vec2, _super); function Vec2(_x, _y) { if (_x === void 0) { _x = 0; } if (_y === void 0) { _y = 0; } var _this = _super.call(this) || this; _this._x = _x; _this._y = _y; _this.isVec2 = true; return _this; } Object.defineProperty(Vec2.prototype, "x", { get: function () { return this._x; }, set: function (value) { if (this._x !== value) { this._x = value; this.fire('change', 'x', this._x, value); } }, enumerable: false, configurable: true }); Object.defineProperty(Vec2.prototype, "y", { get: function () { return this._y; }, set: function (value) { if (this._y !== value) { this._y = value; this.fire('change', 'y', this._y, value); } }, enumerable: false, configurable: true }); Vec2.isVec2 = function (v) { return !isNaN(v.x) && !isNaN(v.y) && isNaN(v.z) && isNaN(v.w); }; Object.defineProperty(Vec2.prototype, "width", { get: function () { return this._x; }, set: function (value) { this._x = value; }, enumerable: false, configurable: true }); Object.defineProperty(Vec2.prototype, "height", { get: function () { return this._y; }, set: function (value) { this._y = value; }, enumerable: false, configurable: true }); Object.defineProperty(Vec2, "UnitX", { get: function () { return new Vec2(1, 0); }, enumerable: false, configurable: true }); Object.defineProperty(Vec2, "UnitY", { get: function () { return new Vec2(0, 1); }, enumerable: false, configurable: true }); Vec2.prototype.set = function (x, y) { this._x = x; this._y = y; return this; }; Vec2.prototype.setScalar = function (scalar) { this._x = scalar; this._y = scalar; return this; }; Vec2.prototype.setX = function (x) { this._x = x; return this; }; Vec2.prototype.setY = function (y) { this._y = y; return this; }; Vec2.prototype.setComponent = function (index, value) { switch (index) { case 0: this._x = value; break; case 1: this._y = value; break; default: throw new Error("index is out of range: " + index); } return this; }; Vec2.prototype.getComponent = function (index) { switch (index) { case 0: return this._x; case 1: return this._y; default: throw new Error("index is out of range: " + index); } }; Vec2.prototype.clone = function () { return new Vec2(this._x, this._y); }; Vec2.prototype.copy = function (v) { this._x = v.x; this._y = v.y; return this; }; Vec2.prototype.add = function (v, w) { if (w !== undefined) { console.warn("Vec2: .add() now only accepts one argument. Use .addVecs( a, b ) instead."); return this.addVecs(v, w); } this._x += v.x; this._y += v.y; return this; }; Vec2.prototype.addScalar = function (s) { this._x += s; this._y += s; return this; }; Vec2.prototype.addVecs = function (a, b) { this._x = a.x + b.x; this._y = a.y + b.y; return this; }; Vec2.prototype.addScaledVec = function (v, s) { this._x += v.x * s; this._y += v.y * s; return this; }; Vec2.prototype.sub = function (v, w) { if (w !== undefined) { console.warn("Vec2: .sub() now only accepts one argument. Use .subVecs( a, b ) instead."); return this.subVecs(v, w); } this._x -= v.x; this._y -= v.y; return this; }; Vec2.prototype.subScalar = function (s) { this._x -= s; this._y -= s; return this; }; Vec2.prototype.subVecs = function (a, b) { this._x = a.x - b.x; this._y = a.y - b.y; return this; }; Vec2.prototype.multiply = function (v) { this._x *= v.x; this._y *= v.y; return this; }; Vec2.prototype.multiplyScalar = function (scalar) { this._x *= scalar; this._y *= scalar; return this; }; Vec2.prototype.divide = function (v) { this._x /= v.x; this._y /= v.y; return this; }; Vec2.prototype.divideScalar = function (scalar) { return this.multiplyScalar(1 / scalar); }; Vec2.prototype.applyMat3 = function (m) { var x = this._x, y = this._y; var e = m.elements; this._x = e[0] * x + e[3] * y + e[6]; this._y = e[1] * x + e[4] * y + e[7]; return this; }; Vec2.prototype.min = function (v) { this._x = Math.min(this._x, v.x); this._y = Math.min(this._y, v.y); return this; }; Vec2.prototype.max = function (v) { this._x = Math.max(this._x, v.x); this._y = Math.max(this._y, v.y); return this; }; Vec2.prototype.clamp = function (min, max) { // assumes min < max, componentwise this._x = Math.max(min.x, Math.min(max.x, this._x)); this._y = Math.max(min.y, Math.min(max.y, this._y)); return this; }; Vec2.prototype.clampScalar = function (minVal, maxVal) { this._x = Math.max(minVal, Math.min(maxVal, this._x)); this._y = Math.max(minVal, Math.min(maxVal, this._y)); return this; }; Vec2.prototype.clampLength = function (min, max) { var length = this.length(); return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length))); }; Vec2.prototype.floor = function () { this._x = Math.floor(this._x); this._y = Math.floor(this._y); return this; }; Vec2.prototype.ceil = function () { this._x = Math.ceil(this._x); this._y = Math.ceil(this._y); return this; }; Vec2.prototype.round = function () { this._x = Math.round(this._x); this._y = Math.round(this._y); return this; }; Vec2.prototype.roundToZero = function () { this._x = this._x < 0 ? Math.ceil(this._x) : Math.floor(this._x); this._y = this._y < 0 ? Math.ceil(this._y) : Math.floor(this._y); return this; }; Vec2.prototype.negate = function () { this._x = -this._x; this._y = -this._y; return this; }; Vec2.prototype.dot = function (v) { return this._x * v.x + this._y * v.y; }; Vec2.prototype.cross = function (v) { return this._x * v.y - this._y * v.x; }; Vec2.prototype.lengthSq = function () { return this._x * this._x + this._y * this._y; }; Vec2.prototype.length = function () { return Math.sqrt(this._x * this._x + this._y * this._y); }; Vec2.prototype.manhattanLength = function () { return Math.abs(this._x) + Math.abs(this._y); }; Vec2.prototype.normalize = function () { return this.divideScalar(this.length() || 1); }; Vec2.prototype.angle = function () { // computes the angle in radians with respect to the positive x-axis var angle = Math.atan2(this._y, this._x); if (angle < 0) angle += 2 * Math.PI; return angle; }; Vec2.prototype.distanceTo = function (v) { return Math.sqrt(this.distanceToSquared(v)); }; Vec2.prototype.distanceToSquared = function (v) { var dx = this._x - v.x, dy = this._y - v.y; return dx * dx + dy * dy; }; Vec2.prototype.manhattanDistanceTo = function (v) { return Math.abs(this._x - v.x) + Math.abs(this._y - v.y); }; Vec2.prototype.setLength = function (length) { return this.normalize().multiplyScalar(length); }; Vec2.prototype.lerp = function (v, alpha) { this._x += (v.x - this._x) * alpha; this._y += (v.y - this._y) * alpha; return this; }; Vec2.prototype.lerpVecs = function (v1, v2, alpha) { return this.subVecs(v2, v1).multiplyScalar(alpha).add(v1); }; Vec2.prototype.equals = function (v) { return v.x === this._x && v.y === this._y; }; Vec2.prototype.fromArray = function (array, offset) { if (offset === void 0) { offset = 0; } this._x = array[offset]; this._y = array[offset + 1]; return this; }; Vec2.prototype.toArray = function (array, offset) { if (array === void 0) { array = []; } if (offset === void 0) { offset = 0; } array[offset] = this._x; array[offset + 1] = this._y; return array; }; Vec2.prototype.fromBufferAttribute = function (attribute, index, offset) { if (offset !== undefined) { console.warn("Vec2: offset has been removed from .fromBufferAttribute()."); } this._x = attribute.getX(index); this._y = attribute.getY(index); return this; }; Vec2.prototype.rotateAround = function (center, angle) { var c = Math.cos(angle), s = Math.sin(angle); var x = this._x - center.x; var y = this._y - center.y; this._x = x * c - y * s + center.x; this._y = x * s + y * c + center.y; return this; }; return Vec2; }(eventhandler.EventHandler); exports.Vec2 = Vec2; function v2() { return new Vec2(); } exports.v2 = v2; }); unwrapExports(Vec2_1); var Vec2_2 = Vec2_1.v2; var Vec2_3 = Vec2_1.Vec2; var _Math = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.toFixedAry = exports.toFixed = exports.ToDegrees = exports.toRadians = exports.floorPowerOfTwo = exports.ceilPowerOfTwo = exports.isPowerOfTwo = exports.randFloat = exports.randInt = exports.smootherstep = exports.smoothstep = exports.lerp = exports.clamp = exports.approximateEqual = exports.sign = exports.RADIANS_PER_ARCSECOND = exports.DEGREES_PER_RADIAN = exports.RADIANS_PER_DEGREE = exports.ONE_OVER_TWO_PI = exports.PI_TWO = exports.THREE_PI_OVER_TWO = exports.PI_OVER_SIX = exports.PI_OVER_FOUR = exports.PI_OVER_THREE = exports.PI_OVER_TWO = exports.ONE_OVER_PI = exports.PI = exports.gPrecision = void 0; exports.gPrecision = 1e-4; /** * pi * * @type {Number} * @constant */ exports.PI = Math.PI; /** * 1/pi * * @type {Number} * @constant */ exports.ONE_OVER_PI = 1.0 / Math.PI; /** * pi/2 * * @type {Number} * @constant */ exports.PI_OVER_TWO = Math.PI / 2.0; /** * pi/3 * * @type {Number} * @constant */ exports.PI_OVER_THREE = Math.PI / 3.0; /** * pi/4 * * @type {Number} * @constant */ exports.PI_OVER_FOUR = Math.PI / 4.0; /** * pi/6 * * @type {Number} * @constant */ exports.PI_OVER_SIX = Math.PI / 6.0; /** * 3pi/2 * * @type {Number} * @constant */ exports.THREE_PI_OVER_TWO = 3.0 * Math.PI / 2.0; /** * 2pi * * @type {Number} * @constant */ exports.PI_TWO = 2.0 * Math.PI; /** * 1/2pi * * @type {Number} * @constant */ exports.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI); /** * The number of radians in a degree. * * @type {Number} * @constant */ exports.RADIANS_PER_DEGREE = Math.PI / 180.0; /** * The number of degrees in a radian. * * @type {Number} * @constant */ exports.DEGREES_PER_RADIAN = 180.0 / Math.PI; /** * The number of radians in an arc second. * * @type {Number} * @constant */ exports.RADIANS_PER_ARCSECOND = exports.RADIANS_PER_DEGREE / 3600.0; function sign(value) { return value >= 0 ? 1 : -1; } exports.sign = sign; function approximateEqual(v1, v2, precision) { if (precision === void 0) { precision = exports.gPrecision; } return Math.abs(v1 - v2) < precision; } exports.approximateEqual = approximateEqual; function clamp(value, min, max) { return Math.max(min, Math.min(max, value)); } exports.clamp = clamp; function lerp(x, y, t) { return (1 - t) * x + t * y; } exports.lerp = lerp; function smoothstep(x, min, max) { if (x <= min) return 0; if (x >= max) return 1; x = (x - min) / (max - min); return x * x * (3 - 2 * x); } exports.smoothstep = smoothstep; function smootherstep(x, min, max) { if (x <= min) return 0; if (x >= max) return 1; x = (x - min) / (max - min); return x * x * x * (x * (x * 6 - 15) + 10); } exports.smootherstep = smootherstep; // Random integer from <low, high> interval function randInt(low, high) { return low + Math.floor(Math.random() * (high - low + 1)); } exports.randInt = randInt; // Random float from <low, high> interval /** * 生成一个low~high之间的浮点数 * @param {*} low * @param {*} high */ function randFloat(low, high) { return low + Math.random() * (high - low); } exports.randFloat = randFloat; function isPowerOfTwo(value) { return (value & value - 1) === 0 && value !== 0; } exports.isPowerOfTwo = isPowerOfTwo; function ceilPowerOfTwo(value) { return Math.pow(2, Math.ceil(Math.log(value) / Math.LN2)); } exports.ceilPowerOfTwo = ceilPowerOfTwo; function floorPowerOfTwo(value) { return Math.pow(2, Math.floor(Math.log(value) / Math.LN2)); } exports.floorPowerOfTwo = floorPowerOfTwo; function toRadians(degrees) { return degrees * exports.RADIANS_PER_DEGREE; } exports.toRadians = toRadians; function ToDegrees(radians) { return radians * exports.DEGREES_PER_RADIAN; } exports.ToDegrees = ToDegrees; /** * 数字或者向量固定位数 * @param {Object} obj 数字或者向量 * @param {*} fractionDigits */ function toFixed(obj, fractionDigits) { if (obj instanceof Number) return parseFloat(obj.toFixed(fractionDigits));else { if (obj.x !== undefined) obj.x = parseFloat(obj.x.toFixed(fractionDigits)); if (obj.y !== undefined) obj.y = parseFloat(obj.y.toFixed(fractionDigits)); if (obj.z !== undefined) obj.z = parseFloat(obj.z.toFixed(fractionDigits)); } return obj; } exports.toFixed = toFixed; /** * 数组中所有数字或者向量固定位数 * @param {Array} array * @param {Number} precision */ function toFixedAry(array, precision) { if (precision === void 0) { precision = exports.gPrecision; } for (var i = 0; i < array.length; i++) { var e = array[i]; if (e instanceof Array) toFixedAry(e);else array[i] = toFixed(e, precision); } } exports.toFixedAry = toFixedAry; }); unwrapExports(_Math); var _Math_1 = _Math.toFixedAry; var _Math_2 = _Math.toFixed; var _Math_3 = _Math.ToDegrees; var _Math_4 = _Math.toRadians; var _Math_5 = _Math.floorPowerOfTwo; var _Math_6 = _Math.ceilPowerOfTwo; var _Math_7 = _Math.isPowerOfTwo; var _Math_8 = _Math.randFloat; var _Math_9 = _Math.randInt; var _Math_10 = _Math.smootherstep; var _Math_11 = _Math.smoothstep; var _Math_12 = _Math.lerp; var _Math_13 = _Math.clamp; var _Math_14 = _Math.approximateEqual; var _Math_15 = _Math.sign; var _Math_16 = _Math.RADIANS_PER_ARCSECOND; var _Math_17 = _Math.DEGREES_PER_RADIAN; var _Math_18 = _Math.RADIANS_PER_DEGREE; var _Math_19 = _Math.ONE_OVER_TWO_PI; var _Math_20 = _Math.PI_TWO; var _Math_21 = _Math.THREE_PI_OVER_TWO; var _Math_22 = _Math.PI_OVER_SIX; var _Math_23 = _Math.PI_OVER_FOUR; var _Math_24 = _Math.PI_OVER_THREE; var _Math_25 = _Math.PI_OVER_TWO; var _Math_26 = _Math.ONE_OVER_PI; var _Math_27 = _Math.PI; var _Math_28 = _Math.gPrecision; var Quat_1 = createCommonjsModule(function (module, exports) { var __extends = commonjsGlobal && commonjsGlobal.__extends || function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; } || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; }(); Object.defineProperty(exports, "__esModule", { value: true }); exports.quat = exports.Quat = void 0; var Quat = /** @class */ function (_super) { __extends(Quat, _super); function Quat(_x, _y, _z, _w) { if (_x === void 0) { _x = 0; } if (_y === void 0) { _y = 0; } if (_z === void 0) { _z = 0; } if (_w === void 0) { _w = 1; } var _this = _super.call(this) || this; _this._x = _x; _this._y = _y; _this._z = _z; _this._w = _w; _this.isQuat = true; return _this; } Object.defineProperty(Quat.prototype, "x", { get: function () { return this._x; }, set: function (value) { if (this._x !== value) { this._x = value; this.fire('change', 'x', this._x, value); } }, enumerable: false, configurable: true }); Object.defineProperty(Quat.prototype, "y", { get: function () { return this._y; }, set: function (value) { if (this._y !== value) { this._y = value; this.fire('change', 'y', this._y, value); } }, enumerable: false, configurable: true }); Object.defineProperty(Quat.prototype, "z", { get: function () { return this._z; }, set: function (value) { if (this._z !== value) { this._z = value; this.fire('change', 'z', this._z, value); } }, enumerable: false, configurable: true }); Object.defineProperty(Quat.prototype, "w", { get: function () { return this._w; }, set: function (value) { if (this._w !== value) { this._w = value; this.fire('change', 'w', this._w, value); } }, enumerable: false, configurable: true }); Quat.slerp = function (qa, qb, qm, t) { return qm.copy(qa).slerp(qb, t); }; Quat.slerpFlat = function (dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t) { // fuzz-free, array-based Quat SLERP operation var x0 = src0[srcOffset0 + 0], y0 = src0[srcOffset0 + 1], z0 = src0[srcOffset0 + 2], w0 = src0[srcOffset0 + 3], x1 = src1[srcOffset1 + 0], y1 = src1[srcOffset1 + 1], z1 = src1[srcOffset1 + 2], w1 = src1[srcOffset1 + 3]; if (w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1) { var s = 1 - t, cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1, dir = cos >= 0 ? 1 : -1, sqrSin = 1 - cos * cos; // Skip the Slerp for tiny steps to avoid numeric problems: if (sqrSin > Number.EPSILON) { var sin = Math.sqrt(sqrSin), len = Math.atan2(sin, cos * dir); s = Math.sin(s * len) / sin; t = Math.sin(t * len) / sin; } var tDir = t * dir; x0 = x0 * s + x1 * tDir; y0 = y0 * s + y1 * tDir; z0 = z0 * s + z1 * tDir; w0 = w0 * s + w1 * tDir; // Normalize in case we just did a lerp: if (s === 1 - t) { var f = 1 / Math.sqrt(x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0); x0 *= f; y0 *= f; z0 *= f; w0 *= f; } } dst[dstOffset] = x0; dst[dstOffset + 1] = y0; dst[dstOffset + 2] = z0; dst[dstOffset + 3] = w0; }; Quat.multiplyQuatsFlat = function (dst, dstOffset, src0, srcOffset0, src1, srcOffset1) { var x0 = src0[srcOffset0]; var y0 = src0[srcOffset0 + 1]; var z0 = src0[srcOffset0 + 2]; var w0 = src0[srcOffset0 + 3]; var x1 = src1[srcOffset1]; var y1 = src1[srcOffset1 + 1]; var z1 = src1[srcOffset1 + 2]; var w1 = src1[srcOffset1 + 3]; dst[dstOffset] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1; dst[dstOffset + 1] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1; dst[dstOffset + 2] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1; dst[dstOffset + 3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1; return dst; }; Quat.prototype.set = function (x, y, z, w) { this._x = x; this._y = y; this._z = z; this._w = w; this.fire("change", this); return this; }; Quat.prototype.clone = function () { return new Quat(this._x, this._y, this._z, this._w); }; Quat.prototype.copy = function (quat) { this._x = quat.x; this._y = quat.y; this._z = quat.z; this._w = quat.w; this.fire("change", this); return this; }; Quat.prototype.setFromEuler = function (euler, update) { if (!(euler && euler.isEuler)) { throw new Error("Quat: .setFromEuler() now expects an Euler rotation rather than a Vec3 and order."); } var x = euler._x, y = euler._y, z = euler._z, order = euler.order; // http://www.mathworks.com/matlabcentral/fileexchange/ // 20696-function-to-convert-between-dcm-Euler-angles-Quats-and-Euler-Vecs/ // content/SpinCalc.m var cos = Math.cos; var sin = Math.sin; var c1 = cos(x / 2); var c2 = cos(y / 2); var c3 = cos(z / 2); var s1 = sin(x / 2); var s2 = sin(y / 2); var s3 = sin(z / 2); if (order === "XYZ") { this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; } else if (order === "YXZ") { this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; } else if (order === "ZXY") { this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; } else if (order === "ZYX") { this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; } else if (order === "YZX") { this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; } else if (order === "XZY") { this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; } if (update !== false) this.fire("change", this); return this; }; Quat.prototype.setFromAxisAngle = function (axis, angle) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuat/index.htm // assumes axis is normalized var halfAngle = angle / 2, s = Math.sin(halfAngle); this._x = axis.x * s; this._y = axis.y * s; this._z = axis.z * s; this._w = Math.cos(halfAngle); this.fire("change", this); return this; }; Quat.prototype.setFromRotationMat = function (m) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuat/index.htm // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) var te = m.elements, m11 = te[0], m12 = te[4], m13 = te[8], m21 = te[1], m22 = te[5], m23 = te[9], m31 = te[2], m32 = te[6], m33 = te[10], trace = m11 + m22 + m33, s; if (trace > 0) { s = 0.5 / Math.sqrt(trace + 1.0); this._w = 0.25 / s; this._x = (m32 - m23) * s; this._y = (m13 - m31) * s; this._z = (m21 - m12) * s; } else if (m11 > m22 && m11 > m33) { s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33); this._w = (m32 - m23) / s; this._x = 0.25 * s; this._y = (m12 + m21) / s; this._z = (m13 + m31) / s; } else if (m22 > m33) { s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33); this._w = (m13 - m31) / s; this._x = (m12 + m21) / s; this._y = 0.25 * s; this._z = (m23 + m32) / s; } else { s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22); this._w = (m21 - m12) / s; this._x = (m13 + m31) / s; this._y = (m23 + m32) / s; this._z = 0.25 * s; } this.fire("change", this); return this; }; Quat.prototype.setFromUnitVecs = function (vFrom, vTo) { // assumes direction Vecs vFrom and vTo are normalized var EPS = 0.000001; var r = vFrom.dot(vTo) + 1; if (r < EPS) { r = 0; if (Math.abs(vFrom.x) > Math.abs(vFrom.z)) { this._x = -vFrom.y; this._y = vFrom.x; this._z = 0; this._w = r; } else { this._x = 0; this._y = -vFrom.z; this._z = vFrom.y; this._w = r; } } else { // crossVecs( vFrom, vTo ); // inlined to avoid cyclic dependency on Vec3 this._x = vFrom.y * vTo.z - vFrom.z * vTo.y; this._y = vFrom.z * vTo.x - vFrom.x * vTo.z; this._z = vFrom.x * vTo.y - vFrom.y * vTo.x; this._w = r; } return this.normalize(); }; Quat.prototype.angleTo = function (q) { return 2 * Math.acos(Math.abs(_Math.clamp(this.dot(q), -1, 1))); }; Quat.prototype.rotateTowards = function (q, step) { var angle = this.angleTo(q); if (angle === 0) return this; var t = Math.min(1, step / angle); this.slerp(q, t); return this; }; Quat.prototype.inverse = function () { // Quat is assumed to have unit length return this.conjugate(); }; Quat.prototype.invert = function () { // quaternion is assumed to have unit length return this.conjugate(); }; Quat.prototype.conjugate = function () { this._x *= -1; this._y *= -1; this._z *= -1; this.fire("change", this); return this; }; Quat.prototype.dot = function (v) { return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w; }; Quat.prototype.lengthSq = function () { return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w; }; Quat.prototype.length = function () { return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w); }; Quat.prototype.normalize = function () { var l = this.length(); if (l === 0) { this._x = 0; this._y = 0; this._z = 0; this._w = 1; } else { l = 1 / l; this._x = this._x * l; this._y = this._y * l; this._z = this._z * l; this._w = this._w * l; } this.fire("change", this); return this; }; Quat.prototype.multiply = function (q, p) { if (p !== undefined) { return this.multiplyQuats(q, p); } return this.multiplyQuats(this, q); }; Quat.prototype.premultiply = function (q) { return this.multiplyQuats(q, this); }; Quat.prototype.multiplyQuats = function (a, b) { // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/Quats/code/index.htm var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w; var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w; this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby; this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz; this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx; this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz; this.fire("change", this); return this; }; Quat.prototype.slerp = function (qb, t) { if (t === 0) return this; if (t === 1) return this.copy(qb); var x = this._x, y = this._y, z = this._z, w = this._w; // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/Quats/slerp/ var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z; if (cosHalfTheta < 0) { this._w = -qb._w; this._x = -qb._x; this._y = -qb._y; this._z = -qb._z; cosHalfTheta = -cosHalfTheta; } else { this.copy(qb); } if (cosHalfTheta >= 1.0) { this._w = w; this._x = x; this._y = y; this._z = z; return this; } var sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta; if (sqrSinHalfTheta <= Number.EPSILON) { var s = 1 - t; this._w = s * w + t * this._w; this._x = s * x + t * this._x; this._y = s * y + t * this._y; this._z = s * z + t * this._z; this.normalize(); this.fire("change", this); return this; } var sinHalfTheta = Math.sqrt(sqrSinHalfTheta); var halfTheta = Math.atan2(sinHalfTheta, cosHalfTheta); var ratioA = Math.sin((1 - t) * halfTheta) / sinHalfTheta, ratioB = Math.sin(t * halfTheta) / sinHalfTheta; this._w = w * ratioA + this._w * ratioB; this._x = x * ratioA + this._x * ratioB; this._y = y * ratioA + this._y * ratioB; this._z = z * ratioA + this._z * ratioB; this.fire("change", this); return this; }; Quat.prototype.equals = function (quat) { return quat._x === this._x && quat._y === this._y && quat._z === this._z && quat._w === this._w; }; Quat.prototype.fromArray = function (array, offset) { if (offset === undefined) offset = 0; this._x = array[offset]; this._y = array[offset + 1]; this._z = array[offset + 2]; this._w = array[offset + 3]; this.fire("change", this); return this; }; Quat.prototype.toArray = function (array, offset) { if (array === void 0) { array = []; } if (offset === void 0) { offset = 0; } array[offset] = this._x; array[offset + 1] = this._y; array[offset + 2] = this._z; array[offset + 3] = this._w; return array; }; return Quat; }(eventhandler.EventHandler); exports.Quat = Quat; function quat(x, y, z, w) { return new Quat(x, y, z, w); } exports.quat = quat; }); unwrapExports(Quat_1); var Quat_2 = Quat_1.quat; var Quat_3 = Quat_1.Quat; var Segment_1 = createCommonjsModule(function (module, exports) { var __extends = commonjsGlobal && commonjsGlobal.__extends || function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; } || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; }(); Object.defineProperty(exports, "__esModule", { value: true }); exports.segment = exports.Segment = void 0; var Segment = /** @class */ function (_super) { __extends(Segment, _super); /** * 线段 * @param {Point|Vec3} p0 * @param {Point|Vec3} p1 */ function Segment(_p0, _p1) { if (_p0 === void 0) { _p0 = Vec3_1.v3(); } if (_p1 === void 0) { _p1 = Vec3_1.v3(); } var _this = _super.call(this) || this; Object.setPrototypeOf(_this, Segment.prototype); _this.push(_p0, _p1); _this.center = _p0.clone().add(_p1).multiplyScalar(0.5); _this.extentDirection = _p1.clone().sub(_p0); _this.extentSqr = _this.extentDirection.lengthSq(); _this.extent = Math.sqrt(_this.extentSqr); _this.direction = _this.extentDirection.clone().normalize(); return _this; } Object.defineProperty(Segment.prototype, "p0", { get: function () { return this[0]; }, set: function (v) { this[0].copy(v); }, enumerable: false, configurable: true }); Object.defineProperty(Segment.prototype, "p1", { get: function () { return this[1]; }, set: function (v) { this[1].copy(v); }, enumerable: false, configurable: true }); Segment.prototype.offset = function (distance, normal) { if (normal === void 0) { normal = Vec3_1.Vec3.UnitY; } var vdir = this.direction.clone().applyAxisAngle(normal, Math.PI / 2); vdir.normalize().multiplyScalar(distance); this.p0.add(vdir); this.p1.add(vdir); }; /** * 线段到线段的距离 * @param {Segment} segment */ Segment.prototype.distanceSegment = function (segment) { var result = { parameters: [], closests: [] }; function GetClampedRoot(slope, h0, h1) { var r; if (h0 < 0) { if (h1 > 0) { r = -h0 / slope; if (r > 1) { r = 0.5; } // The slope is positive and -h0 is positive, so there is no // need to test for a negative value and clamp it. } else { r = 1; } } else { r = 0; } return r; } function ComputevarIntersection(sValue, classify, edge, end) { if (classify[0] < 0) { edge[0] = 0; end[0][0] = 0; end[0][1] = mF00 / mB; if (end[0][1] < 0 || end[0][1] > 1) { end[0][1] = 0.5; } if (classify[1] == 0) { edge[1] = 3; end[1][0] = sValue[1]; end[1][1] = 1; } else // classify[1] > 0 { edge[1] = 1; end[1][0] = 1; end[1][1] = mF10 / mB; if (end[1][1] < 0 || end[1][1] > 1) { end[1][1] = 0.5; } } } else if (classify[0] == 0) { edge[0] = 2; end[0][0] = sValue[0]; end[0][1] = 0; if (classify[1] < 0) { edge[1] = 0; end[1][0] = 0; end[1][1] = mF00 / mB; if (end[1][1] < 0 || end[1][1] > 1) { end[1][1] = 0.5; } } else if (classify[1] == 0) { edge[1] = 3; end[1][0] = sValue[1]; end[1][1] = 1; } else { edge[1] = 1; end[1][0] = 1; end[1][1] = mF10 / mB; if (end[1][1] < 0 || end[1][1] > 1) { end[1][1] = 0.5; } } } else // classify[0] > 0 { edge[0] = 1; end[0][0] = 1; end[0][1] = mF10 / mB; if (end[0][1] < 0 || end[0][1] > 1) { end[0][1] = 0.5; } if (classify[1] == 0) { edge[1] = 3; end[1][0] = sValue[1]; end[1][1] = 1; } else { edge[1] = 0; end[1][0] = 0; end[1][1] = mF00 / mB; if (end[1][1] < 0 || end[1][1] > 1) { end[1][1] = 0.5; } } } } function ComputeMinimumParameters(edge, end, parameters) { var delta = end[1][1] - end[0][1]; var h0 = delta * (-mB * end[0][0] + mC * end[0][1] - mE); if (h0 >= 0) { if (edge[0] == 0) { parameters[0] = 0; parameters[1] = GetClampedRoot(mC, mG00, mG01); } else if (edge[0] == 1) { parameters[0] = 1; parameters[1] = GetClampedRoot(mC, mG10, mG11); } else { parameters[0] = end[0][0]; parameters[1] = end[0][1]; } } else { var h1 = delta * (-mB * end[1][0] + mC * end[1][1] - mE); if (h1 <= 0) { if (edge[1] == 0) { parameters[0] = 0; parameters[1] = GetClampedRoot(mC, mG00, mG01); } else if (edge[1] == 1) { parameters[0] = 1; parameters[1] = GetClampedRoot(mC, mG10, mG11); } else { parameters[0] = end[1][0]; parameters[1] = end[1][1]; } } else // h0 < 0 and h1 > 0 { var z = Math.min(Math.max(h0 / (h0 - h1), 0), 1); var omz = 1 - z; parameters[0] = omz * end[0][0] + z * end[1][0]; parameters[1] = omz * end[0][1] + z * end[1][1]; } } } var seg0Dir = this.p1.clone().sub(this.p0); var seg1Dir = segment.p1.clone().sub(segment.p0); var segDiff = this.p0.clone().sub(segment.p0); var mA = seg0Dir.dot(seg0Dir); var mB = seg0Dir.dot(seg1Dir); var mC = seg1Dir.dot(seg1Dir); var mD = seg0Dir.dot(segDiff); var mE = seg1Dir.dot(segDiff); var mF00 = mD; var mF10 = mF00 + mA; var mF01 = mF00 - mB; var mF11 = mF10 - mB; var mG00 = -mE; var mG10 = mG00 - mB; var mG01 = mG00 + mC; var mG11 = mG10 + mC; if (mA > 0 && mC > 0) { var sValue = []; sValue[0] = GetClampedRoot(mA, mF00, mF10); sValue[1] = GetClampedRoot(mA, mF01, mF11); var classify = []; for (var i = 0; i < 2; ++i) { if (sValue[i] <= 0) { classify[i] = -1; } else if (sValue[i] >= 1) { classify[i] = +1; } else { classify[i] = 0; } } if (classify[0] == -1 && classify[1] == -1) { // The minimum must occur on s = 0 for 0 <= t <= 1. result.parameters[0] = 0; result.parameters[1] = GetClampedRoot(mC, mG00, mG01); } else if (classify[0] == +1 && classify[1] == +1) { // The minimum must occur on s = 1 for 0 <= t <= 1. result.parameters[0] = 1; result.parameters[1] = GetClampedRoot(mC, mG10, mG11); } else { // The line dR/ds = 0 varersects the domain [0,1]^2 in a // nondegenerate segment. Compute the endpoints of that segment, // end[0] and end[1]. The edge[i] flag tells you on which domain // edge end[i] lives: 0 (s=0), 1 (s=1), 2 (t=0), 3 (t=1). var edge = []; var end = new Array(2); for (var i_1 = 0; i_1 < end.length; i_1++) end[i_1] = new Array(2); ComputevarIntersection(sValue, classify, edge, end); // The directional derivative of R along the