UNPKG

ol-owm

Version:

Weather layer for OpenLayers and Leaflet using OpenWeatherMap

1,790 lines (1,738 loc) 608 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('leaflet')) : typeof define === 'function' && define.amd ? define(['exports', 'leaflet'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.leafletWind = {}, global.L)); })(this, (function (exports, L) { 'use strict'; function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var L__namespace = /*#__PURE__*/_interopNamespaceDefault(L); const hasOwnProperty = Object.prototype.hasOwnProperty; const symToStringTag = typeof Symbol !== "undefined" ? Symbol.toStringTag : void 0; function baseGetTag(value) { if (value === null) { return value === void 0 ? "[object Undefined]" : "[object Null]"; } if (!(symToStringTag && symToStringTag in Object(value))) { return toString.call(value); } const isOwn = hasOwnProperty.call(value, symToStringTag); const tag = value[symToStringTag]; let unmasked = false; try { value[symToStringTag] = void 0; unmasked = true; } catch (e) { } const result = Object.prototype.toString.call(value); if (unmasked) { if (isOwn) { value[symToStringTag] = tag; } else { delete value[symToStringTag]; } } return result; } function isFunction$1(value) { if (!isObject$1(value)) { return false; } const tag = baseGetTag(value); return tag === "[object Function]" || tag === "[object AsyncFunction]" || tag === "[object GeneratorFunction]" || tag === "[object Proxy]"; } function isObject$1(value) { const type = typeof value; return value !== null && (type === "object" || type === "function"); } function isString$1(value) { if (value == null) { return false; } return typeof value === "string" || value.constructor !== null && value.constructor === String; } function isNumber$1(value) { return Object.prototype.toString.call(value) === "[object Number]" && !isNaN(value); } function isArray(arr) { return Array.isArray(arr); } function assign(target, ...sources) { return Object.assign(target, ...sources); } function warnLog(msg, n) { console.warn(`${n || "wind-layer"}: ${msg}`); } const warnings = {}; function warnOnce(namespaces, msg) { if (!warnings[msg]) { warnLog(msg, namespaces); warnings[msg] = true; } } function floorMod(a, n) { return a - n * Math.floor(a / n); } function isValide(val) { return val !== void 0 && val !== null && !isNaN(val); } function formatData(data, options = {}) { let uComp = void 0; let vComp = void 0; data.forEach(function(record) { switch (record.header.parameterCategory + "," + record.header.parameterNumber) { case "1,2": case "2,2": uComp = record; break; case "1,3": case "2,3": vComp = record; break; } }); if (!vComp || !uComp) { return void 0; } const header = uComp.header; const vectorField = new Field({ xmin: header.lo1, // 一般格点数据是按照矩形范围来切割,所以定义其经纬度范围 ymin: header.la1, xmax: header.lo2, ymax: header.la2, deltaX: header.dx, // x(经度)增量 deltaY: header.dy, // y(维度)增量 cols: header.nx, // 列(可由 `(xmax - xmin) / deltaX` 得到) rows: header.ny, // 行 us: uComp.data, // U分量 vs: vComp.data, // V分量 ...options }); return vectorField; } function createCanvas(width, height, retina, Canvas) { if (typeof document !== "undefined") { const canvas = document.createElement("canvas"); canvas.width = width * retina; canvas.height = height * retina; return canvas; } else { return new Canvas(width * retina, height * retina); } } let Vector$1 = class Vector { constructor(u, v) { this.u = u; this.v = v; this.m = this.magnitude(); } /** * 向量值(这里指风速) * @returns {Number} */ magnitude() { return Math.sqrt(this.u ** 2 + this.v ** 2); } /** * 流体方向 (这里指风向,范围为0-360º) * N is 0º and E is 90º * @returns {Number} */ directionTo() { const verticalAngle = Math.atan2(this.u, this.v); let inDegrees = verticalAngle * (180 / Math.PI); if (inDegrees < 0) { inDegrees += 360; } return inDegrees; } /** * Angle in degrees (0 to 360º) From x--> * N is 0º and E is 90º * @returns {Number} */ directionFrom() { const a = this.directionTo(); return (a + 180) % 360; } }; class Field { constructor(params) { this.grid = []; this.xmin = params.xmin; this.xmax = params.xmax; this.ymin = params.ymin; this.ymax = params.ymax; this.cols = params.cols; this.rows = params.rows; this.us = params.us; this.vs = params.vs; this.deltaX = params.deltaX; this.deltaY = params.deltaY; this.flipY = Boolean(params.flipY); this.ymin = Math.min(params.ymax, params.ymin); this.ymax = Math.max(params.ymax, params.ymin); if (!(this.deltaY < 0 && this.ymin < this.ymax)) { if (params.flipY === void 0) { this.flipY = true; } console.warn("[wind-core]: The data is flipY"); } this.isFields = true; const cols = Math.ceil((this.xmax - this.xmin) / params.deltaX); const rows = Math.ceil((this.ymax - this.ymin) / params.deltaY); if (cols !== this.cols || rows !== this.rows) { console.warn("[wind-core]: The data grid not equal"); } this.isContinuous = Math.floor(this.cols * params.deltaX) >= 360; this.translateX = "translateX" in params ? params.translateX : this.xmax > 180; if ("wrappedX" in params) { warnOnce("[wind-core]: ", "`wrappedX` namespace will deprecated please use `translateX` instead\uFF01"); } this.wrapX = Boolean(params.wrapX); this.grid = this.buildGrid(); this.range = this.calculateRange(); } // from https://github.com/sakitam-fdd/wind-layer/blob/95368f9433/src/windy/windy.js#L110 buildGrid() { const grid = []; let p = 0; const { rows, cols, us, vs } = this; for (let j = 0; j < rows; j++) { const row = []; for (let i = 0; i < cols; i++, p++) { const u = us[p]; const v = vs[p]; const valid = this.isValid(u) && this.isValid(v); row[i] = valid ? new Vector$1(u, v) : null; } if (this.isContinuous) { row.push(row[0]); } grid[j] = row; } return grid; } /** * release data */ release() { this.grid = []; } /** * grib data extent * 格点数据范围 */ extent() { return [this.xmin, this.ymin, this.xmax, this.ymax]; } /** * Bilinear interpolation for Vector * 针对向量进行双线性插值 * https://en.wikipedia.org/wiki/Bilinear_interpolation * @param {Number} x * @param {Number} y * @param {Number[]} g00 * @param {Number[]} g10 * @param {Number[]} g01 * @param {Number[]} g11 * @returns {Vector} */ bilinearInterpolateVector(x, y, g00, g10, g01, g11) { const rx = 1 - x; const ry = 1 - y; const a = rx * ry; const b = x * ry; const c = rx * y; const d = x * y; const u = g00.u * a + g10.u * b + g01.u * c + g11.u * d; const v = g00.v * a + g10.v * b + g01.v * c + g11.v * d; return new Vector$1(u, v); } /** * calculate vector value range */ calculateRange() { if (!this.grid || !this.grid[0]) return; const rows = this.grid.length; const cols = this.grid[0].length; let min; let max; for (let j = 0; j < rows; j++) { for (let i = 0; i < cols; i++) { const vec = this.grid[j][i]; if (vec !== null) { const val = vec.m || vec.magnitude(); if (min === void 0) { min = val; } else if (max === void 0) { max = val; min = Math.min(min, max); max = Math.max(min, max); } else { min = Math.min(val, min); max = Math.max(val, max); } } } } return [min, max]; } /** * 检查 uv是否合法 * @param x * @private */ isValid(x) { return x !== null && x !== void 0; } getWrappedLongitudes() { let xmin = this.xmin; let xmax = this.xmax; if (this.translateX) { if (this.isContinuous) { xmin = -180; xmax = 180; } else { xmax = this.xmax - 360; xmin = this.xmin - 360; } } return [xmin, xmax]; } contains(lon, lat) { const [xmin, xmax] = this.getWrappedLongitudes(); if (xmax > 180 && lon >= -180 && lon <= xmax - 360) { lon += 360; } else if (xmin < -180 && lon <= 180 && lon >= xmin + 360) { lon -= 360; } const longitudeIn = lon >= xmin && lon <= xmax; let latitudeIn; if (this.deltaY >= 0) { latitudeIn = lat >= this.ymin && lat <= this.ymax; } else { latitudeIn = lat >= this.ymax && lat <= this.ymin; } return longitudeIn && latitudeIn; } /** * 获取经纬度所在的位置索引 * @param lon * @param lat */ getDecimalIndexes(lon, lat) { const i = floorMod(lon - this.xmin, 360) / this.deltaX; if (this.flipY) { const j = (this.ymax - lat) / this.deltaY; return [i, j]; } else { const j = (this.ymin + lat) / this.deltaY; return [i, j]; } } /** * Nearest value at lon-lat coordinates * 线性插值 * @param lon * @param lat */ valueAt(lon, lat) { let flag = false; if (this.wrapX) { flag = true; } else if (this.contains(lon, lat)) { flag = true; } if (!flag) return null; const indexes = this.getDecimalIndexes(lon, lat); const ii = Math.floor(indexes[0]); const jj = Math.floor(indexes[1]); const ci = this.clampColumnIndex(ii); const cj = this.clampRowIndex(jj); return this.valueAtIndexes(ci, cj); } /** * Get interpolated grid value lon-lat coordinates * 双线性插值 * @param lon * @param lat */ interpolatedValueAt(lon, lat) { let flag = false; if (this.wrapX) { flag = true; } else if (this.contains(lon, lat)) { flag = true; } if (!flag) return null; const [i, j] = this.getDecimalIndexes(lon, lat); return this.interpolatePoint(i, j); } hasValueAt(lon, lat) { const value = this.valueAt(lon, lat); return value !== null; } /** * 基于向量的双线性插值 * @param i * @param j */ interpolatePoint(i, j) { const indexes = this.getFourSurroundingIndexes(i, j); const [fi, ci, fj, cj] = indexes; const values = this.getFourSurroundingValues(fi, ci, fj, cj); if (values) { const [g00, g10, g01, g11] = values; return this.bilinearInterpolateVector(i - fi, j - fj, g00, g10, g01, g11); } return null; } /** * Check the column index is inside the field, * adjusting to min or max when needed * @private * @param {Number} ii - index * @returns {Number} i - inside the allowed indexes */ clampColumnIndex(ii) { let i = ii; if (ii < 0) { i = 0; } const maxCol = this.cols - 1; if (ii > maxCol) { i = maxCol; } return i; } /** * Check the row index is inside the field, * adjusting to min or max when needed * @private * @param {Number} jj index * @returns {Number} j - inside the allowed indexes */ clampRowIndex(jj) { let j = jj; if (jj < 0) { j = 0; } const maxRow = this.rows - 1; if (jj > maxRow) { j = maxRow; } return j; } /** * 计算索引位置周围的数据 * @private * @param {Number} i - decimal index * @param {Number} j - decimal index * @returns {Array} [fi, ci, fj, cj] */ getFourSurroundingIndexes(i, j) { const fi = Math.floor(i); let ci = fi + 1; if (this.isContinuous && ci >= this.cols) { ci = 0; } ci = this.clampColumnIndex(ci); const fj = this.clampRowIndex(Math.floor(j)); const cj = this.clampRowIndex(fj + 1); return [fi, ci, fj, cj]; } /** * Get four surrounding values or null if not available, * from 4 integer indexes * @private * @param {Number} fi * @param {Number} ci * @param {Number} fj * @param {Number} cj * @returns {Array} */ getFourSurroundingValues(fi, ci, fj, cj) { let row; if (row = this.grid[fj]) { const g00 = row[fi]; const g10 = row[ci]; if (this.isValid(g00) && this.isValid(g10) && (row = this.grid[cj])) { const g01 = row[fi]; const g11 = row[ci]; if (this.isValid(g01) && this.isValid(g11)) { return [g00, g10, g01, g11]; } } } return null; } /** * Value for grid indexes * @param {Number} i - column index (integer) * @param {Number} j - row index (integer) * @returns {Vector|Number} */ valueAtIndexes(i, j) { return this.grid[j][i]; } /** * Lon-Lat for grid indexes * @param {Number} i - column index (integer) * @param {Number} j - row index (integer) * @returns {Number[]} [lon, lat] */ lonLatAtIndexes(i, j) { const lon = this.longitudeAtX(i); const lat = this.latitudeAtY(j); return [lon, lat]; } /** * Longitude for grid-index * @param {Number} i - column index (integer) * @returns {Number} longitude at the center of the cell */ longitudeAtX(i) { const halfXPixel = this.deltaX / 2; let lon = this.xmin + halfXPixel + i * this.deltaX; if (this.translateX) { lon = lon > 180 ? lon - 360 : lon; } return lon; } /** * Latitude for grid-index * @param {Number} j - row index (integer) * @returns {Number} latitude at the center of the cell */ latitudeAtY(j) { const halfYPixel = this.deltaY / 2; return this.ymax - halfYPixel - j * this.deltaY; } /** * 生成粒子位置 * @param o * @param width * @param height * @param unproject * @return IPosition */ randomize(o = {}, width, height, unproject) { const i = Math.random() * (width || this.cols) | 0; const j = Math.random() * (height || this.rows) | 0; const coords = unproject([i, j]); if (coords !== null) { o.x = coords[0]; o.y = coords[1]; } else { o.x = this.longitudeAtX(i); o.y = this.latitudeAtY(j); } return o; } /** * 判断是否是 `Field` 的实例 * @return boolean */ checkFields() { return this.isFields; } } const defaultOptions$2 = { globalAlpha: 0.9, // 全局透明度 lineWidth: 1, // 线条宽度 colorScale: "#fff", velocityScale: 1 / 25, // particleAge: 90, maxAge: 90, // alias for particleAge // particleMultiplier: 1 / 300, // TODO: PATHS = Math.round(width * height * particleMultiplier); paths: 800, frameRate: 20, useCoordsDraw: true }; function indexFor(m, min, max, colorScale) { return Math.max(0, Math.min(colorScale.length - 1, Math.round((m - min) / (max - min) * (colorScale.length - 1)))); } class WindCore { constructor(ctx, options, field) { this.particles = []; this.generated = false; this.ctx = ctx; if (!this.ctx) { throw new Error("ctx error"); } this.animate = this.animate.bind(this); this.setOptions(options); if (field) { this.updateData(field); } } static { this.Field = Field; } /** * 设置配置项 * @param options */ setOptions(options) { this.options = { ...defaultOptions$2, ...options }; const { width, height } = this.ctx.canvas; if ("particleAge" in options && !("maxAge" in options) && isNumber$1(this.options.particleAge)) { this.options.maxAge = this.options.particleAge; } if ("particleMultiplier" in options && !("paths" in options) && isNumber$1(this.options.particleMultiplier)) { this.options.paths = Math.round(width * height * this.options.particleMultiplier); } this.prerender(); } /** * 获取配置项 */ getOptions() { return this.options; } /** * 更新数据 * @param field */ updateData(field) { this.field = field; if (!this.generated) { return; } this.particles = this.prepareParticlePaths(); } // @ts-ignore project(...args) { throw new Error("project must be overriden"); } // @ts-ignore unproject(...args) { throw new Error("unproject must be overriden"); } /** * 判断位置是否在当前视窗内 * @param coordinates */ intersectsCoordinate(coordinates) { throw new Error("must be overriden"); } /** * 清空当前画布 */ clearCanvas() { this.stop(); this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height); this.forceStop = false; } isStop() { return !this.starting; } /** * 启动粒子动画 */ start() { this.starting = true; this.forceStop = false; this.then = Date.now(); this.animate(); } /** * 停止粒子动画 */ stop() { cancelAnimationFrame(this.animationLoop); this.starting = false; this.forceStop = true; } animate() { if (this.animationLoop) { cancelAnimationFrame(this.animationLoop); } this.animationLoop = requestAnimationFrame(this.animate); const now = Date.now(); const delta = now - this.then; if (delta > this.options.frameRate) { this.then = now - delta % this.options.frameRate; this.render(); } } /** * 渲染前处理 */ prerender() { this.generated = false; if (!this.field) { return; } this.particles = this.prepareParticlePaths(); this.generated = true; if (!this.starting && !this.forceStop) { this.starting = true; this.then = Date.now(); this.animate(); } } /** * 开始渲染 */ render() { this.moveParticles(); this.drawParticles(); this.postrender(); } /** * each frame render end */ postrender() { } moveParticles() { const { width, height } = this.ctx.canvas; const particles = this.particles; const maxAge = this.options.maxAge; const velocityScale = isFunction$1(this.options.velocityScale) ? this.options.velocityScale() : this.options.velocityScale; let i = 0; const len = particles.length; for (; i < len; i++) { const particle = particles[i]; if (particle.age > maxAge) { particle.age = 0; this.field.randomize(particle, width, height, this.unproject); } const x = particle.x; const y = particle.y; const vector = this.field.interpolatedValueAt(x, y); if (vector === null) { particle.age = maxAge; } else { const xt = x + vector.u * velocityScale; const yt = y + vector.v * velocityScale; if (this.field.hasValueAt(xt, yt)) { particle.xt = xt; particle.yt = yt; particle.m = vector.m; } else { particle.x = xt; particle.y = yt; particle.age = maxAge; } } particle.age++; } } fadeIn() { const prev = this.ctx.globalCompositeOperation; this.ctx.globalCompositeOperation = "destination-in"; this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height); this.ctx.globalCompositeOperation = prev; } drawParticles() { const particles = this.particles; this.fadeIn(); this.ctx.globalAlpha = this.options.globalAlpha; this.ctx.fillStyle = `rgba(0, 0, 0, ${this.options.globalAlpha})`; this.ctx.lineWidth = isNumber$1(this.options.lineWidth) ? this.options.lineWidth : 1; this.ctx.strokeStyle = isString$1(this.options.colorScale) ? this.options.colorScale : "#fff"; let i = 0; const len = particles.length; if (this.field && len > 0) { let min; let max; if (isValide(this.options.minVelocity) && isValide(this.options.maxVelocity)) { min = this.options.minVelocity; max = this.options.maxVelocity; } else { [min, max] = this.field.range; } for (; i < len; i++) { this[this.options.useCoordsDraw ? "drawCoordsParticle" : "drawPixelParticle"](particles[i], min, max); } } } /** * 用于绘制像素粒子 * @param particle * @param min * @param max */ drawPixelParticle(particle, min, max) { const pointPrev = [particle.x, particle.y]; const pointNext = [particle.xt, particle.yt]; if (pointNext && pointPrev && isValide(pointNext[0]) && isValide(pointNext[1]) && isValide(pointPrev[0]) && isValide(pointPrev[1]) && particle.age <= this.options.maxAge) { this.ctx.beginPath(); this.ctx.moveTo(pointPrev[0], pointPrev[1]); this.ctx.lineTo(pointNext[0], pointNext[1]); if (isFunction$1(this.options.colorScale)) { this.ctx.strokeStyle = this.options.colorScale(particle.m); } else if (Array.isArray(this.options.colorScale)) { const colorIdx = indexFor(particle.m, min, max, this.options.colorScale); this.ctx.strokeStyle = this.options.colorScale[colorIdx]; } if (isFunction$1(this.options.lineWidth)) { this.ctx.lineWidth = this.options.lineWidth(particle.m); } particle.x = particle.xt; particle.y = particle.yt; this.ctx.stroke(); } } /** * 用于绘制坐标粒子 * @param particle * @param min * @param max */ drawCoordsParticle(particle, min, max) { const source = [particle.x, particle.y]; const target = [particle.xt, particle.yt]; if (target && source && isValide(target[0]) && isValide(target[1]) && isValide(source[0]) && isValide(source[1]) && this.intersectsCoordinate(target) && particle.age <= this.options.maxAge) { const pointPrev = this.project(source); const pointNext = this.project(target); if (pointPrev && pointNext) { this.ctx.beginPath(); this.ctx.moveTo(pointPrev[0], pointPrev[1]); this.ctx.lineTo(pointNext[0], pointNext[1]); particle.x = particle.xt; particle.y = particle.yt; if (isFunction$1(this.options.colorScale)) { this.ctx.strokeStyle = this.options.colorScale(particle.m); } else if (Array.isArray(this.options.colorScale)) { const colorIdx = indexFor(particle.m, min, max, this.options.colorScale); this.ctx.strokeStyle = this.options.colorScale[colorIdx]; } if (isFunction$1(this.options.lineWidth)) { this.ctx.lineWidth = this.options.lineWidth(particle.m); } this.ctx.stroke(); } } } prepareParticlePaths() { const { width, height } = this.ctx.canvas; const particleCount = typeof this.options.paths === "function" ? this.options.paths(this) : this.options.paths; const particles = []; if (!this.field) { return []; } let i = 0; for (; i < particleCount; i++) { particles.push( this.field.randomize( { age: this.randomize() }, width, height, this.unproject ) ); } return particles; } randomize() { return Math.floor(Math.random() * this.options.maxAge); } } /** * Common utilities * @module glMatrix */ // Configuration Constants var EPSILON = 0.000001; var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array; /** * Sets the type of array used when creating new vectors and matrices * * @param {Float32ArrayConstructor | ArrayConstructor} type Array type, such as Float32Array or Array */ function setMatrixArrayType(type) { ARRAY_TYPE = type; } if (!Math.hypot) Math.hypot = function () { var y = 0, i = arguments.length; while (i--) { y += arguments[i] * arguments[i]; } return Math.sqrt(y); }; /** * 3x3 Matrix * @module mat3 */ /** * Creates a new identity mat3 * * @returns {mat3} a new 3x3 matrix */ function create$4() { var out = new ARRAY_TYPE(9); if (ARRAY_TYPE != Float32Array) { out[1] = 0; out[2] = 0; out[3] = 0; out[5] = 0; out[6] = 0; out[7] = 0; } out[0] = 1; out[4] = 1; out[8] = 1; return out; } /** * Copies the upper-left 3x3 values into the given mat3. * * @param {mat3} out the receiving 3x3 matrix * @param {ReadonlyMat4} a the source 4x4 matrix * @returns {mat3} out */ function fromMat4(out, a) { out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; out[3] = a[4]; out[4] = a[5]; out[5] = a[6]; out[6] = a[8]; out[7] = a[9]; out[8] = a[10]; return out; } /** * Copy the values from one mat3 to another * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the source matrix * @returns {mat3} out */ function copy$3(out, a) { out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; out[3] = a[3]; out[4] = a[4]; out[5] = a[5]; out[6] = a[6]; out[7] = a[7]; out[8] = a[8]; return out; } /** * Set the components of a mat3 to the given values * * @param {mat3} out the receiving matrix * @param {Number} m00 Component in column 0, row 0 position (index 0) * @param {Number} m01 Component in column 0, row 1 position (index 1) * @param {Number} m02 Component in column 0, row 2 position (index 2) * @param {Number} m10 Component in column 1, row 0 position (index 3) * @param {Number} m11 Component in column 1, row 1 position (index 4) * @param {Number} m12 Component in column 1, row 2 position (index 5) * @param {Number} m20 Component in column 2, row 0 position (index 6) * @param {Number} m21 Component in column 2, row 1 position (index 7) * @param {Number} m22 Component in column 2, row 2 position (index 8) * @returns {mat3} out */ function set$4(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) { out[0] = m00; out[1] = m01; out[2] = m02; out[3] = m10; out[4] = m11; out[5] = m12; out[6] = m20; out[7] = m21; out[8] = m22; return out; } /** * Set a mat3 to the identity matrix * * @param {mat3} out the receiving matrix * @returns {mat3} out */ function identity$1(out) { out[0] = 1; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 1; out[5] = 0; out[6] = 0; out[7] = 0; out[8] = 1; return out; } /** * Transpose the values of a mat3 * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the source matrix * @returns {mat3} out */ function transpose$1(out, a) { // If we are transposing ourselves we can skip a few steps but have to cache some values if (out === a) { var a01 = a[1], a02 = a[2], a12 = a[5]; out[1] = a[3]; out[2] = a[6]; out[3] = a01; out[5] = a[7]; out[6] = a02; out[7] = a12; } else { out[0] = a[0]; out[1] = a[3]; out[2] = a[6]; out[3] = a[1]; out[4] = a[4]; out[5] = a[7]; out[6] = a[2]; out[7] = a[5]; out[8] = a[8]; } return out; } /** * Inverts a mat3 * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the source matrix * @returns {mat3} out */ function invert$2(out, a) { var a00 = a[0], a01 = a[1], a02 = a[2]; var a10 = a[3], a11 = a[4], a12 = a[5]; var a20 = a[6], a21 = a[7], a22 = a[8]; var b01 = a22 * a11 - a12 * a21; var b11 = -a22 * a10 + a12 * a20; var b21 = a21 * a10 - a11 * a20; // Calculate the determinant var det = a00 * b01 + a01 * b11 + a02 * b21; if (!det) { return null; } det = 1.0 / det; out[0] = b01 * det; out[1] = (-a22 * a01 + a02 * a21) * det; out[2] = (a12 * a01 - a02 * a11) * det; out[3] = b11 * det; out[4] = (a22 * a00 - a02 * a20) * det; out[5] = (-a12 * a00 + a02 * a10) * det; out[6] = b21 * det; out[7] = (-a21 * a00 + a01 * a20) * det; out[8] = (a11 * a00 - a01 * a10) * det; return out; } /** * Calculates the adjugate of a mat3 * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the source matrix * @returns {mat3} out */ function adjoint$1(out, a) { var a00 = a[0], a01 = a[1], a02 = a[2]; var a10 = a[3], a11 = a[4], a12 = a[5]; var a20 = a[6], a21 = a[7], a22 = a[8]; out[0] = a11 * a22 - a12 * a21; out[1] = a02 * a21 - a01 * a22; out[2] = a01 * a12 - a02 * a11; out[3] = a12 * a20 - a10 * a22; out[4] = a00 * a22 - a02 * a20; out[5] = a02 * a10 - a00 * a12; out[6] = a10 * a21 - a11 * a20; out[7] = a01 * a20 - a00 * a21; out[8] = a00 * a11 - a01 * a10; return out; } /** * Calculates the determinant of a mat3 * * @param {ReadonlyMat3} a the source matrix * @returns {Number} determinant of a */ function determinant$1(a) { var a00 = a[0], a01 = a[1], a02 = a[2]; var a10 = a[3], a11 = a[4], a12 = a[5]; var a20 = a[6], a21 = a[7], a22 = a[8]; return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20); } /** * Multiplies two mat3's * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the first operand * @param {ReadonlyMat3} b the second operand * @returns {mat3} out */ function multiply$5(out, a, b) { var a00 = a[0], a01 = a[1], a02 = a[2]; var a10 = a[3], a11 = a[4], a12 = a[5]; var a20 = a[6], a21 = a[7], a22 = a[8]; var b00 = b[0], b01 = b[1], b02 = b[2]; var b10 = b[3], b11 = b[4], b12 = b[5]; var b20 = b[6], b21 = b[7], b22 = b[8]; out[0] = b00 * a00 + b01 * a10 + b02 * a20; out[1] = b00 * a01 + b01 * a11 + b02 * a21; out[2] = b00 * a02 + b01 * a12 + b02 * a22; out[3] = b10 * a00 + b11 * a10 + b12 * a20; out[4] = b10 * a01 + b11 * a11 + b12 * a21; out[5] = b10 * a02 + b11 * a12 + b12 * a22; out[6] = b20 * a00 + b21 * a10 + b22 * a20; out[7] = b20 * a01 + b21 * a11 + b22 * a21; out[8] = b20 * a02 + b21 * a12 + b22 * a22; return out; } /** * Translate a mat3 by the given vector * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the matrix to translate * @param {ReadonlyVec2} v vector to translate by * @returns {mat3} out */ function translate$1(out, a, v) { var a00 = a[0], a01 = a[1], a02 = a[2], a10 = a[3], a11 = a[4], a12 = a[5], a20 = a[6], a21 = a[7], a22 = a[8], x = v[0], y = v[1]; out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a10; out[4] = a11; out[5] = a12; out[6] = x * a00 + y * a10 + a20; out[7] = x * a01 + y * a11 + a21; out[8] = x * a02 + y * a12 + a22; return out; } /** * Rotates a mat3 by the given angle * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the matrix to rotate * @param {Number} rad the angle to rotate the matrix by * @returns {mat3} out */ function rotate$1(out, a, rad) { var a00 = a[0], a01 = a[1], a02 = a[2], a10 = a[3], a11 = a[4], a12 = a[5], a20 = a[6], a21 = a[7], a22 = a[8], s = Math.sin(rad), c = Math.cos(rad); out[0] = c * a00 + s * a10; out[1] = c * a01 + s * a11; out[2] = c * a02 + s * a12; out[3] = c * a10 - s * a00; out[4] = c * a11 - s * a01; out[5] = c * a12 - s * a02; out[6] = a20; out[7] = a21; out[8] = a22; return out; } /** * Scales the mat3 by the dimensions in the given vec2 * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the matrix to rotate * @param {ReadonlyVec2} v the vec2 to scale the matrix by * @returns {mat3} out **/ function scale$4(out, a, v) { var x = v[0], y = v[1]; out[0] = x * a[0]; out[1] = x * a[1]; out[2] = x * a[2]; out[3] = y * a[3]; out[4] = y * a[4]; out[5] = y * a[5]; out[6] = a[6]; out[7] = a[7]; out[8] = a[8]; return out; } /** * Creates a matrix from a vector translation * This is equivalent to (but much faster than): * * mat3.identity(dest); * mat3.translate(dest, dest, vec); * * @param {mat3} out mat3 receiving operation result * @param {ReadonlyVec2} v Translation vector * @returns {mat3} out */ function fromTranslation$1(out, v) { out[0] = 1; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 1; out[5] = 0; out[6] = v[0]; out[7] = v[1]; out[8] = 1; return out; } /** * Creates a matrix from a given angle * This is equivalent to (but much faster than): * * mat3.identity(dest); * mat3.rotate(dest, dest, rad); * * @param {mat3} out mat3 receiving operation result * @param {Number} rad the angle to rotate the matrix by * @returns {mat3} out */ function fromRotation$1(out, rad) { var s = Math.sin(rad), c = Math.cos(rad); out[0] = c; out[1] = s; out[2] = 0; out[3] = -s; out[4] = c; out[5] = 0; out[6] = 0; out[7] = 0; out[8] = 1; return out; } /** * Creates a matrix from a vector scaling * This is equivalent to (but much faster than): * * mat3.identity(dest); * mat3.scale(dest, dest, vec); * * @param {mat3} out mat3 receiving operation result * @param {ReadonlyVec2} v Scaling vector * @returns {mat3} out */ function fromScaling$1(out, v) { out[0] = v[0]; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = v[1]; out[5] = 0; out[6] = 0; out[7] = 0; out[8] = 1; return out; } /** * Calculates a 3x3 matrix from the given quaternion * * @param {mat3} out mat3 receiving operation result * @param {ReadonlyQuat} q Quaternion to create matrix from * * @returns {mat3} out */ function fromQuat$1(out, q) { var x = q[0], y = q[1], z = q[2], w = q[3]; var x2 = x + x; var y2 = y + y; var z2 = z + z; var xx = x * x2; var yx = y * x2; var yy = y * y2; var zx = z * x2; var zy = z * y2; var zz = z * z2; var wx = w * x2; var wy = w * y2; var wz = w * z2; out[0] = 1 - yy - zz; out[3] = yx - wz; out[6] = zx + wy; out[1] = yx + wz; out[4] = 1 - xx - zz; out[7] = zy - wx; out[2] = zx - wy; out[5] = zy + wx; out[8] = 1 - xx - yy; return out; } /** * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix * * @param {mat3} out mat3 receiving operation result * @param {ReadonlyMat4} a Mat4 to derive the normal matrix from * * @returns {mat3} out */ function normalFromMat4(out, a) { var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; var b00 = a00 * a11 - a01 * a10; var b01 = a00 * a12 - a02 * a10; var b02 = a00 * a13 - a03 * a10; var b03 = a01 * a12 - a02 * a11; var b04 = a01 * a13 - a03 * a11; var b05 = a02 * a13 - a03 * a12; var b06 = a20 * a31 - a21 * a30; var b07 = a20 * a32 - a22 * a30; var b08 = a20 * a33 - a23 * a30; var b09 = a21 * a32 - a22 * a31; var b10 = a21 * a33 - a23 * a31; var b11 = a22 * a33 - a23 * a32; // Calculate the determinant var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; if (!det) { return null; } det = 1.0 / det; out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det; out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det; out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det; out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det; out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det; out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det; out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det; out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det; return out; } /** * Returns Frobenius norm of a mat3 * * @param {ReadonlyMat3} a the matrix to calculate Frobenius norm of * @returns {Number} Frobenius norm */ function frob(a) { return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]); } /** * Adds two mat3's * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the first operand * @param {ReadonlyMat3} b the second operand * @returns {mat3} out */ function add$4(out, a, b) { out[0] = a[0] + b[0]; out[1] = a[1] + b[1]; out[2] = a[2] + b[2]; out[3] = a[3] + b[3]; out[4] = a[4] + b[4]; out[5] = a[5] + b[5]; out[6] = a[6] + b[6]; out[7] = a[7] + b[7]; out[8] = a[8] + b[8]; return out; } /** * Subtracts matrix b from matrix a * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the first operand * @param {ReadonlyMat3} b the second operand * @returns {mat3} out */ function subtract$4(out, a, b) { out[0] = a[0] - b[0]; out[1] = a[1] - b[1]; out[2] = a[2] - b[2]; out[3] = a[3] - b[3]; out[4] = a[4] - b[4]; out[5] = a[5] - b[5]; out[6] = a[6] - b[6]; out[7] = a[7] - b[7]; out[8] = a[8] - b[8]; return out; } /** * Returns whether or not the matrices have approximately the same elements in the same position. * * @param {ReadonlyMat3} a The first matrix. * @param {ReadonlyMat3} b The second matrix. * @returns {Boolean} True if the matrices are equal, false otherwise. */ function equals$5(a, b) { var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], a6 = a[6], a7 = a[7], a8 = a[8]; var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], b8 = b[8]; return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)); } /** * Copy the values from one mat4 to another * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the source matrix * @returns {mat4} out */ function copy$2(out, a) { out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; out[3] = a[3]; out[4] = a[4]; out[5] = a[5]; out[6] = a[6]; out[7] = a[7]; out[8] = a[8]; out[9] = a[9]; out[10] = a[10]; out[11] = a[11]; out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15]; return out; } /** * Set the components of a mat4 to the given values * * @param {mat4} out the receiving matrix * @param {Number} m00 Component in column 0, row 0 position (index 0) * @param {Number} m01 Component in column 0, row 1 position (index 1) * @param {Number} m02 Component in column 0, row 2 position (index 2) * @param {Number} m03 Component in column 0, row 3 position (index 3) * @param {Number} m10 Component in column 1, row 0 position (index 4) * @param {Number} m11 Component in column 1, row 1 position (index 5) * @param {Number} m12 Component in column 1, row 2 position (index 6) * @param {Number} m13 Component in column 1, row 3 position (index 7) * @param {Number} m20 Component in column 2, row 0 position (index 8) * @param {Number} m21 Component in column 2, row 1 position (index 9) * @param {Number} m22 Component in column 2, row 2 position (index 10) * @param {Number} m23 Component in column 2, row 3 position (index 11) * @param {Number} m30 Component in column 3, row 0 position (index 12) * @param {Number} m31 Component in column 3, row 1 position (index 13) * @param {Number} m32 Component in column 3, row 2 position (index 14) * @param {Number} m33 Component in column 3, row 3 position (index 15) * @returns {mat4} out */ function set$3(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { out[0] = m00; out[1] = m01; out[2] = m02; out[3] = m03; out[4] = m10; out[5] = m11; out[6] = m12; out[7] = m13; out[8] = m20; out[9] = m21; out[10] = m22; out[11] = m23; out[12] = m30; out[13] = m31; out[14] = m32; out[15] = m33; return out; } /** * Set a mat4 to the identity matrix * * @param {mat4} out the receiving matrix * @returns {mat4} out */ function identity(out) { out[0] = 1; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 0; out[5] = 1; out[6] = 0; out[7] = 0; out[8] = 0; out[9] = 0; out[10] = 1; out[11] = 0; out[12] = 0; out[13] = 0; out[14] = 0; out[15] = 1; return out; } /** * Transpose the values of a mat4 * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the source matrix * @returns {mat4} out */ function transpose(out, a) { // If we are transposing ourselves we can skip a few steps but have to cache some values if (out === a) { var a01 = a[1], a02 = a[2], a03 = a[3]; var a12 = a[6], a13 = a[7]; var a23 = a[11]; out[1] = a[4]; out[2] = a[8]; out[3] = a[12]; out[4] = a01; out[6] = a[9]; out[7] = a[13]; out[8] = a02; out[9] = a12; out[11] = a[14]; out[12] = a03; out[13] = a13; out[14] = a23; } else { out[0] = a[0]; out[1] = a[4]; out[2] = a[8]; out[3] = a[12]; out[4] = a[1]; out[5] = a[5]; out[6] = a[9]; out[7] = a[13]; out[8] = a[2]; out[9] = a[6]; out[10] = a[10]; out[11] = a[14]; out[12] = a[3]; out[13] = a[7]; out[14] = a[11]; out[15] = a[15]; } return out; } /** * Inverts a mat4 * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the source matrix * @returns {mat4} out */ function invert$1(out, a) { var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; var b00 = a00 * a11 - a01 * a10; var b01 = a00 * a12 - a02 * a10; var b02 = a00 * a13 - a03 * a10; var b03 = a01 * a12 - a02 * a11; var b04 = a01 * a13 - a03 * a11; var b05 = a02 * a13 - a03 * a12; var b06 = a20 * a31 - a21 * a30; var b07 = a20 * a32 - a22 * a30; var b08 = a20 * a33 - a23 * a30; var b09 = a21 * a32 - a22 * a31; var b10 = a21 * a33 - a23 * a31; var b11 = a22 * a33 - a23 * a32; // Calculate the determinant var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; if (!det) { return null; } det = 1.0 / det; out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; return out; } /** * Calculates the adjugate of a mat4 * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the source matrix * @returns {mat4} out */ function adjoint(out, a) { var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; out[0] = a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22); out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)); out[2] = a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12); out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)); out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)); out[5] = a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22); out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)); out[7] = a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12); out[8] = a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21); out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)); out[10] = a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11); out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)); out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)); out[13] = a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21); out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)); out[15] = a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11); return out; } /** * Calculates the determinant of a mat4 * * @param {ReadonlyMat4} a the source matrix * @returns {Number} determinant of a */ function determinant(a) { var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; var b00 = a00 * a11 - a01 * a10; var b01 = a00 * a12 - a02 * a10; var b02 = a00 * a13 - a03 * a10; var b03 = a01 * a12 - a02 * a11; var b04 = a01 * a13 - a03 * a11; var b05 = a02 * a13 - a03 * a12; var b06 = a20 * a31 - a21 * a30; var b07 = a20 * a32 - a22 * a30; var b08 = a20 * a33 - a23 * a30; var b0