UNPKG

@xtor/cga.js

Version:

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

1,871 lines (1,520 loc) 1.52 MB
/** * https://github.com/yszhao91/cga.js *CGA Lib |cga.js |alex Zhao | Zhao yaosheng *@license free for all */ var cga = (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 _Math = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.toFixed = exports.radToDeg = exports.degToRad = exports.floorPowerOfTwo = exports.ceilPowerOfTwo = exports.isPowerOfTwo = exports.randFloat = exports.randInt = exports.smootherstep = exports.smoothstep = exports.lerp = exports.clamp = exports.approximateEqual = exports.sign = exports.gPrecision = void 0; exports.gPrecision = 1e-4; var DEG2RAD = Math.PI / 180; var RAD2DEG = 180 / Math.PI; 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 degToRad(degrees) { return degrees * DEG2RAD; } exports.degToRad = degToRad; function radToDeg(radians) { return radians * RAD2DEG; } exports.radToDeg = radToDeg; /** * 数字或者向量固定位数 * @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; }); unwrapExports(_Math); var _Math_1 = _Math.toFixed; var _Math_2 = _Math.radToDeg; var _Math_3 = _Math.degToRad; var _Math_4 = _Math.floorPowerOfTwo; var _Math_5 = _Math.ceilPowerOfTwo; var _Math_6 = _Math.isPowerOfTwo; var _Math_7 = _Math.randFloat; var _Math_8 = _Math.randInt; var _Math_9 = _Math.smootherstep; var _Math_10 = _Math.smoothstep; var _Math_11 = _Math.lerp; var _Math_12 = _Math.clamp; var _Math_13 = _Math.approximateEqual; var _Math_14 = _Math.sign; var _Math_15 = _Math.gPrecision; var array = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.unique = exports.classify = exports.flat = exports.forall = exports.toFixed = exports.clone = 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 */ function clone(array) { var result = new Array(); for (var i = 0; i < array.length; i++) { var ele = array[i]; if (ele instanceof Number || ele instanceof String) result[i] = ele;else if (ele.clone) { result[i] = ele.clone(); } else if (ele instanceof Array) result[i] = clone(ele);else throw "数组有元素不能clone"; } return result; } exports.clone = clone; /** * 数组中所有数字或者向量固定位数 * @param {Array} array * @param {Number} precision */ function toFixed(array, precision) { if (precision === void 0) { precision = _Math.gPrecision; } for (var i = 0; i < array.length; i++) { var e = array[i]; if (e instanceof Array) toFixed(e);else array[i] = _Math.toFixed(e, precision); } } exports.toFixed = toFixed; /** * 遍历多级数组中所有对象 * @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 array_5 = array.toFixed; var array_6 = array.clone; 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() { // super(); 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 rngBrowser = createCommonjsModule(function (module) { // Unique ID creation requires a high quality random # generator. In the // browser this is a little complicated due to unknown quality of Math.random() // and inconsistent support for the `crypto` API. We do the best we can via // feature-detection // getRandomValues needs to be invoked in a context where "this" is a Crypto // implementation. Also, find the complete implementation of crypto on IE11. var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto)) || (typeof(msCrypto) != 'undefined' && typeof window.msCrypto.getRandomValues == 'function' && msCrypto.getRandomValues.bind(msCrypto)); if (getRandomValues) { // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef module.exports = function whatwgRNG() { getRandomValues(rnds8); return rnds8; }; } else { // Math.random()-based (RNG) // // If all else fails, use Math.random(). It's fast, but is of unspecified // quality. var rnds = new Array(16); module.exports = function mathRNG() { for (var i = 0, r; i < 16; i++) { if ((i & 0x03) === 0) r = Math.random() * 0x100000000; rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; } return rnds; }; } }); /** * Convert array of 16 byte values to UUID string format of the form: * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */ var byteToHex = []; for (var i = 0; i < 256; ++i) { byteToHex[i] = (i + 0x100).toString(16).substr(1); } function bytesToUuid(buf, offset) { var i = offset || 0; var bth = byteToHex; // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4 return ([ bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]] ]).join(''); } var bytesToUuid_1 = bytesToUuid; // **`v1()` - Generate time-based UUID** // // Inspired by https://github.com/LiosK/UUID.js // and http://docs.python.org/library/uuid.html var _nodeId; var _clockseq; // Previous uuid creation time var _lastMSecs = 0; var _lastNSecs = 0; // See https://github.com/uuidjs/uuid for API details function v1(options, buf, offset) { var i = buf && offset || 0; var b = buf || []; options = options || {}; var node = options.node || _nodeId; var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; // node and clockseq need to be initialized to random values if they're not // specified. We do this lazily to minimize issues related to insufficient // system entropy. See #189 if (node == null || clockseq == null) { var seedBytes = rngBrowser(); if (node == null) { // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1) node = _nodeId = [ seedBytes[0] | 0x01, seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5] ]; } if (clockseq == null) { // Per 4.2.2, randomize (14 bit) clockseq clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff; } } // UUID timestamps are 100 nano-second units since the Gregorian epoch, // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs' // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00. var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime(); // Per 4.2.1.2, use count of uuid's generated during the current clock // cycle to simulate higher resolution clock var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; // Time since last uuid creation (in msecs) var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000; // Per 4.2.1.2, Bump clockseq on clock regression if (dt < 0 && options.clockseq === undefined) { clockseq = clockseq + 1 & 0x3fff; } // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new // time interval if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) { nsecs = 0; } // Per 4.2.1.2 Throw error if too many uuids are requested if (nsecs >= 10000) { throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec'); } _lastMSecs = msecs; _lastNSecs = nsecs; _clockseq = clockseq; // Per 4.1.4 - Convert from unix epoch to Gregorian epoch msecs += 12219292800000; // `time_low` var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000; b[i++] = tl >>> 24 & 0xff; b[i++] = tl >>> 16 & 0xff; b[i++] = tl >>> 8 & 0xff; b[i++] = tl & 0xff; // `time_mid` var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff; b[i++] = tmh >>> 8 & 0xff; b[i++] = tmh & 0xff; // `time_high_and_version` b[i++] = tmh >>> 24 & 0xf | 0x10; // include version b[i++] = tmh >>> 16 & 0xff; // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant) b[i++] = clockseq >>> 8 | 0x80; // `clock_seq_low` b[i++] = clockseq & 0xff; // `node` for (var n = 0; n < 6; ++n) { b[i + n] = node[n]; } return buf ? buf : bytesToUuid_1(b); } var v1_1 = v1; function v4(options, buf, offset) { var i = buf && offset || 0; if (typeof(options) == 'string') { buf = options === 'binary' ? new Array(16) : null; options = null; } options = options || {}; var rnds = options.random || (options.rng || rngBrowser)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` rnds[6] = (rnds[6] & 0x0f) | 0x40; rnds[8] = (rnds[8] & 0x3f) | 0x80; // Copy bytes to buffer, if provided if (buf) { for (var ii = 0; ii < 16; ++ii) { buf[i + ii] = rnds[ii]; } } return buf || bytesToUuid_1(rnds); } var v4_1 = v4; var uuid = v4_1; uuid.v1 = v1_1; uuid.v4 = v4_1; var _uuid_3_4_0_uuid = uuid; var thing = 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.buildAccessors = exports.buildAccessor = exports.Thing = void 0; var Thing = /** @class */ function (_super) { __extends(Thing, _super); function Thing(opts) { var _this = _super.call(this) || this; _this.cache = {}; opts = opts || {}; _this.uuid = _uuid_3_4_0_uuid.v4(); _this.id = _this.uuid; _this.name = opts.name || "未命名"; _this.alias = opts.alias; _this.isThing = true; _this.parent = null; _this.children = []; _this.meta = undefined; _this.needsUpdate = false; _this._renderObject = null; _this._useData = {}; _this.tag = "untagged"; _this.on("set", function (name, oldValue, newValue) { _this.fire("set_" + name, name, oldValue, newValue); }); for (var key in opts) { if (opts.hasOwnProperty(key)) { if (!_this[key]) { _this[key] = opts[key]; } } } return _this; } Thing.prototype.add = function (thing, force) { if (force === void 0) { force = false; } if (arguments.length > 1) { for (var i = 0; i < arguments.length; i++) { this.add(arguments[i]); } return this; } if (thing === this) { console.error("Thing.add: 自己不能作为自己的子节点", thing); return this; } if (thing && this.isThing) { if (thing.parent) { thing.parent.remove(thing); } thing.parent = this; this.children.push(thing); } else if (thing && force) { if (thing.parent) { thing.parent.remove(thing); } thing.parent = this; this.children.push(thing); } else { console.error("Thing.add:不是Thing类型", thing); } return this; }; Thing.prototype.remove = function (thing) { if (arguments.length > 1) { for (var i = 0; i < arguments.length; i++) { this.remove(arguments[i]); } return this; } else { //自身从父节点移除 this.parent.remove(this); } var index = this.children.indexOf(thing); if (index !== -1) { thing.parent = null; // thing.dispatchEvent( { type: 'removed' } ); this.children.splice(index, 1); } return this; }; Thing.prototype.foreach = function (cb) { cb(this); var children = this.children; for (var i = 0; i < children.length; i++) { children[i].foreach(cb); } }; Thing.prototype.getObjectByProperty = function (name, value) { if (this[name] === value) return this; for (var i = 0, l = this.children.length; i < l; i++) { var child = this.children[i]; if (!child.getObjectByProperty) continue; var object = child.getObjectByProperty(name, value); if (object !== undefined) { return object; } } return undefined; }; Thing.prototype.getObjectById = function (id) { return this.getObjectByProperty('id', id); }; Thing.prototype.getObjectByName = function (name) { return this.getObjectByProperty('name', name); }; /** * 生成属性的set/get方法 * @param {string} name * @param {function} setFunc * @param {boolean} skipEqualsCheck */ Thing.prototype.defineProperty = function (name, setFunc, skipEqualsCheck) { var _this = this; if (skipEqualsCheck === void 0) { skipEqualsCheck = true; } Object.defineProperty(this, name, { get: function () { return _this._useData[name]; }, set: function (value) { var data = _this._useData; var oldValue = data[name]; if (!skipEqualsCheck && oldValue === value) return; data[name] = value; if (setFunc) setFunc.call(_this, value, oldValue); }, configurable: true }); }; Thing.prototype.buildAccessor = function (name, bindObject) { if (bindObject === void 0) { bindObject = this; } if (!bindObject) return; Object.defineProperty(bindObject, name, { get: function () { return bindObject["_" + name]; }, set: function (value) { var oldValue = bindObject["_" + name]; bindObject["_" + name] = value; bindObject.fire('set', name, oldValue, value); }, configurable: true }); }; Thing.prototype.buildAccessors = function (schema, bindObject) { var _this = this; schema.forEach(function (descriptor) { _this.buildAccessor(descriptor, bindObject); }); }; return Thing; }(eventhandler.EventHandler); exports.Thing = Thing; function buildAccessor(name, bindObject) { if (!bindObject) return; Object.defineProperty(bindObject, name, { get: function () { return bindObject["_" + name]; }, set: function (value) { var oldValue = bindObject["_" + name]; bindObject["_" + name] = value; bindObject.fire('set', name, oldValue, value); }, configurable: true }); } exports.buildAccessor = buildAccessor; function buildAccessors(schema, bindObject) { schema.forEach(function (descriptor) { buildAccessor(descriptor, bindObject); }); } exports.buildAccessors = buildAccessors; }); unwrapExports(thing); var thing_1 = thing.buildAccessors; var thing_2 = thing.buildAccessor; var thing_3 = thing.Thing; 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; thing.buildAccessors(['x', 'y'], _this); return _this; } 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 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; thing.buildAccessors(['x', 'y', 'z', 'w'], _this); return _this; } 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.setFromRotationMatrix = 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.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) {