From bf4a264ed9220788fe3e2bdbcdb19c5a59d16121 Mon Sep 17 00:00:00 2001 From: "Chinmay Pandhare (CCD)" <1000011C1010000@gmail.com> Date: Tue, 14 Mar 2017 21:01:43 +0530 Subject: [PATCH 1/5] Added Crop Module (#8) * ImageThreshold.js and ImageSelect.js bugfixes * Added Crop Module * lowercase parameters * Added usage notes, changed a variable name. --- dist/image-sequencer.js | 170544 ++++++++++++++++--------------- index.html | 5 +- src/ImageSequencer.js | 4 +- src/Modules.js | 4 +- src/modules/Crop.js | 61 + src/modules/ImageThreshold.js | 1 - 6 files changed, 85398 insertions(+), 85221 deletions(-) create mode 100644 src/modules/Crop.js diff --git a/dist/image-sequencer.js b/dist/image-sequencer.js index caf3c802..a879db02 100644 --- a/dist/image-sequencer.js +++ b/dist/image-sequencer.js @@ -1,2650 +1,672 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o Math.abs(dy)) { + view.rotate(t, 0, 0, -dx * flipX * Math.PI * camera.rotateSpeed / window.innerWidth) + } else { + var kzoom = camera.zoomSpeed * flipY * dy / window.innerHeight * (t - view.lastT()) / 100.0 + view.pan(t, 0, 0, distance * (Math.exp(kzoom) - 1)) + } + }, true) + + return camera +} -module.exports = { - Encode : Base64Encode, - encode : Base64Encode, - Decode : Base64Decode, - decode : Base64Decode -}; +},{"3d-view":2,"mouse-change":428,"mouse-event-offset":429,"mouse-wheel":431,"right-now":948}],2:[function(require,module,exports){ +'use strict' -},{"./lib/decode":2,"./lib/encode":3}],2:[function(require,module,exports){ -(function (Buffer){ -module.exports = Base64Decode; +module.exports = createViewController -var Transform = require('stream').Transform || require('readable-stream').Transform; -require('util').inherits(Base64Decode, Transform); +var createTurntable = require('turntable-camera-controller') +var createOrbit = require('orbit-camera-controller') +var createMatrix = require('matrix-camera-controller') -/** - * Decodes a Base64 data stream, coming in as a string or Buffer of UTF-8 text, into binary Buffers. - * @returns {Base64Decode} - * @constructor - */ -function Base64Decode() { - if ( !(this instanceof Base64Decode) ) - return new Base64Decode(); - - Transform.call(this, { - // The input is converted to strings, so no need to transform input strings to buffers - decodeStrings : false - }); - - // Any extra chars from the last chunk - this.extra = ''; +function ViewController(controllers, mode) { + this._controllerNames = Object.keys(controllers) + this._controllerList = this._controllerNames.map(function(n) { + return controllers[n] + }) + this._mode = mode + this._active = controllers[mode] + if(!this._active) { + this._mode = 'turntable' + this._active = controllers.turntable + } + this.modes = this._controllerNames + this.computedMatrix = this._active.computedMatrix + this.computedEye = this._active.computedEye + this.computedUp = this._active.computedUp + this.computedCenter = this._active.computedCenter + this.computedRadius = this._active.computedRadius } -/** - * Decodes a Base64 data stream, coming in as a string or Buffer of UTF-8 text, into binary Buffers. - * @param {Buffer|string} chunk - * @param encoding - * @param cb - * @private - */ -Base64Decode.prototype._transform = function (chunk, encoding, cb) { - // Convert chunk to a string - chunk = '' + chunk; +var proto = ViewController.prototype - // Add previous extra and remove any newline characters - chunk = this.extra + chunk.replace(/(\r\n|\n|\r)/gm, ''); +var COMMON_METHODS = [ + ['flush', 1], + ['idle', 1], + ['lookAt', 4], + ['rotate', 4], + ['pan', 4], + ['translate', 4], + ['setMatrix', 2], + ['setDistanceLimits', 2], + ['setDistance', 2] +] - // 4 characters represent 3 bytes, so we can only decode in groups of 4 chars - var remaining = chunk.length % 4; +COMMON_METHODS.forEach(function(method) { + var name = method[0] + var argNames = [] + for(var i=0; i*/ +var arraytools = function () { -var objectKeys = Object.keys || function (obj) { - var keys = []; - for (var key in obj) { - keys.push(key); - }return keys; -}; -/**/ + var that = {}; -module.exports = Duplex; + var RGB_REGEX = /^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,.*)?\)$/; + var RGB_GROUP_REGEX = /^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,?\s*(.*)?\)$/; -/**/ -var processNextTick = require('process-nextick-args'); -/**/ - -/**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); -/**/ - -var Readable = require('./_stream_readable'); -var Writable = require('./_stream_writable'); - -util.inherits(Duplex, Readable); - -var keys = objectKeys(Writable.prototype); -for (var v = 0; v < keys.length; v++) { - var method = keys[v]; - if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; -} - -function Duplex(options) { - if (!(this instanceof Duplex)) return new Duplex(options); - - Readable.call(this, options); - Writable.call(this, options); - - if (options && options.readable === false) this.readable = false; - - if (options && options.writable === false) this.writable = false; - - this.allowHalfOpen = true; - if (options && options.allowHalfOpen === false) this.allowHalfOpen = false; - - this.once('end', onend); -} - -// the no-half-open enforcer -function onend() { - // if we allow half-open state, or if the writable side ended, - // then we're ok. - if (this.allowHalfOpen || this._writableState.ended) return; - - // no more data can be written. - // But allow more writes to happen in this tick. - processNextTick(onEndNT, this); -} - -function onEndNT(self) { - self.end(); -} - -function forEach(xs, f) { - for (var i = 0, l = xs.length; i < l; i++) { - f(xs[i], i); - } -} -},{"./_stream_readable":6,"./_stream_writable":8,"core-util-is":11,"inherits":12,"process-nextick-args":14}],5:[function(require,module,exports){ -// a passthrough stream. -// basically just the most minimal sort of Transform stream. -// Every written chunk gets output as-is. - -'use strict'; - -module.exports = PassThrough; - -var Transform = require('./_stream_transform'); - -/**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); -/**/ - -util.inherits(PassThrough, Transform); - -function PassThrough(options) { - if (!(this instanceof PassThrough)) return new PassThrough(options); - - Transform.call(this, options); -} - -PassThrough.prototype._transform = function (chunk, encoding, cb) { - cb(null, chunk); -}; -},{"./_stream_transform":7,"core-util-is":11,"inherits":12}],6:[function(require,module,exports){ -(function (process){ -'use strict'; - -module.exports = Readable; - -/**/ -var processNextTick = require('process-nextick-args'); -/**/ - -/**/ -var isArray = require('isarray'); -/**/ - -/**/ -var Duplex; -/**/ - -Readable.ReadableState = ReadableState; - -/**/ -var EE = require('events').EventEmitter; - -var EElistenerCount = function (emitter, type) { - return emitter.listeners(type).length; -}; -/**/ - -/**/ -var Stream; -(function () { - try { - Stream = require('st' + 'ream'); - } catch (_) {} finally { - if (!Stream) Stream = require('events').EventEmitter; - } -})(); -/**/ - -var Buffer = require('buffer').Buffer; -/**/ -var bufferShim = require('buffer-shims'); -/**/ - -/**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); -/**/ - -/**/ -var debugUtil = require('util'); -var debug = void 0; -if (debugUtil && debugUtil.debuglog) { - debug = debugUtil.debuglog('stream'); -} else { - debug = function () {}; -} -/**/ - -var BufferList = require('./internal/streams/BufferList'); -var StringDecoder; - -util.inherits(Readable, Stream); - -function prependListener(emitter, event, fn) { - // Sadly this is not cacheable as some libraries bundle their own - // event emitter implementation with them. - if (typeof emitter.prependListener === 'function') { - return emitter.prependListener(event, fn); - } else { - // This is a hack to make sure that our error handler is attached before any - // userland ones. NEVER DO THIS. This is here only because this code needs - // to continue to work with older versions of Node.js that do not include - // the prependListener() method. The goal is to eventually remove this hack. - if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; - } -} - -function ReadableState(options, stream) { - Duplex = Duplex || require('./_stream_duplex'); - - options = options || {}; - - // object stream flag. Used to make read(n) ignore n and to - // make all the buffer merging and length checks go away - this.objectMode = !!options.objectMode; - - if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode; - - // the point at which it stops calling _read() to fill the buffer - // Note: 0 is a valid value, means "don't call _read preemptively ever" - var hwm = options.highWaterMark; - var defaultHwm = this.objectMode ? 16 : 16 * 1024; - this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; - - // cast to ints. - this.highWaterMark = ~ ~this.highWaterMark; - - // A linked list is used to store data chunks instead of an array because the - // linked list can remove elements from the beginning faster than - // array.shift() - this.buffer = new BufferList(); - this.length = 0; - this.pipes = null; - this.pipesCount = 0; - this.flowing = null; - this.ended = false; - this.endEmitted = false; - this.reading = false; - - // a flag to be able to tell if the onwrite cb is called immediately, - // or on a later tick. We set this to true at first, because any - // actions that shouldn't happen until "later" should generally also - // not happen before the first write call. - this.sync = true; - - // whenever we return null, then we set a flag to say - // that we're awaiting a 'readable' event emission. - this.needReadable = false; - this.emittedReadable = false; - this.readableListening = false; - this.resumeScheduled = false; - - // Crypto is kind of old and crusty. Historically, its default string - // encoding is 'binary' so we have to make this configurable. - // Everything else in the universe uses 'utf8', though. - this.defaultEncoding = options.defaultEncoding || 'utf8'; - - // when piping, we only care about 'readable' events that happen - // after read()ing all the bytes and not getting any pushback. - this.ranOut = false; - - // the number of writers that are awaiting a drain event in .pipe()s - this.awaitDrain = 0; - - // if true, a maybeReadMore has been scheduled - this.readingMore = false; - - this.decoder = null; - this.encoding = null; - if (options.encoding) { - if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; - this.decoder = new StringDecoder(options.encoding); - this.encoding = options.encoding; - } -} - -function Readable(options) { - Duplex = Duplex || require('./_stream_duplex'); - - if (!(this instanceof Readable)) return new Readable(options); - - this._readableState = new ReadableState(options, this); - - // legacy - this.readable = true; - - if (options && typeof options.read === 'function') this._read = options.read; - - Stream.call(this); -} - -// Manually shove something into the read() buffer. -// This returns true if the highWaterMark has not been hit yet, -// similar to how Writable.write() returns true if you should -// write() some more. -Readable.prototype.push = function (chunk, encoding) { - var state = this._readableState; - - if (!state.objectMode && typeof chunk === 'string') { - encoding = encoding || state.defaultEncoding; - if (encoding !== state.encoding) { - chunk = bufferShim.from(chunk, encoding); - encoding = ''; - } + function isPlainObject (v) { + return !Array.isArray(v) && v !== null && typeof v === 'object'; } - return readableAddChunk(this, state, chunk, encoding, false); -}; + function linspace (start, end, num) { + var inc = (end - start) / Math.max(num - 1, 1); + var a = []; + for( var ii = 0; ii < num; ii++) + a.push(start + ii*inc); + return a; + } -// Unshift should *always* be something directly out of read() -Readable.prototype.unshift = function (chunk) { - var state = this._readableState; - return readableAddChunk(this, state, chunk, '', true); -}; - -Readable.prototype.isPaused = function () { - return this._readableState.flowing === false; -}; - -function readableAddChunk(stream, state, chunk, encoding, addToFront) { - var er = chunkInvalid(state, chunk); - if (er) { - stream.emit('error', er); - } else if (chunk === null) { - state.reading = false; - onEofChunk(stream, state); - } else if (state.objectMode || chunk && chunk.length > 0) { - if (state.ended && !addToFront) { - var e = new Error('stream.push() after EOF'); - stream.emit('error', e); - } else if (state.endEmitted && addToFront) { - var _e = new Error('stream.unshift() after end event'); - stream.emit('error', _e); - } else { - var skipAdd; - if (state.decoder && !addToFront && !encoding) { - chunk = state.decoder.write(chunk); - skipAdd = !state.objectMode && chunk.length === 0; + function zip () { + var arrays = [].slice.call(arguments); + var lengths = arrays.map(function (a) {return a.length;}); + var len = Math.min.apply(null, lengths); + var zipped = []; + for (var i = 0; i < len; i++) { + zipped[i] = []; + for (var j = 0; j < arrays.length; ++j) { + zipped[i][j] = arrays[j][i]; + } } + return zipped; + } - if (!addToFront) state.reading = false; - - // Don't add to the buffer if we've decoded to an empty string chunk and - // we're not in object mode - if (!skipAdd) { - // if we want the data now, just emit it. - if (state.flowing && state.length === 0 && !state.sync) { - stream.emit('data', chunk); - stream.read(0); - } else { - // update the buffer info. - state.length += state.objectMode ? 1 : chunk.length; - if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); - - if (state.needReadable) emitReadable(stream); - } + function zip3 (a, b, c) { + var len = Math.min.apply(null, [a.length, b.length, c.length]); + var result = []; + for (var n = 0; n < len; n++) { + result.push([a[n], b[n], c[n]]); } - - maybeReadMore(stream, state); - } - } else if (!addToFront) { - state.reading = false; + return result; } - return needMoreData(state); -} - -// if it's past the high water mark, we can push in some more. -// Also, if we have no data yet, we can stand some -// more bytes. This is to work around cases where hwm=0, -// such as the repl. Also, if the push() triggered a -// readable event, and the user called read(largeNumber) such that -// needReadable was set, then we ought to push more, so that another -// 'readable' event will be triggered. -function needMoreData(state) { - return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); -} - -// backwards compatibility. -Readable.prototype.setEncoding = function (enc) { - if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; - this._readableState.decoder = new StringDecoder(enc); - this._readableState.encoding = enc; - return this; -}; - -// Don't raise the hwm > 8MB -var MAX_HWM = 0x800000; -function computeNewHighWaterMark(n) { - if (n >= MAX_HWM) { - n = MAX_HWM; - } else { - // Get the next highest power of 2 to prevent increasing hwm excessively in - // tiny amounts - n--; - n |= n >>> 1; - n |= n >>> 2; - n |= n >>> 4; - n |= n >>> 8; - n |= n >>> 16; - n++; - } - return n; -} - -// This function is designed to be inlinable, so please take care when making -// changes to the function body. -function howMuchToRead(n, state) { - if (n <= 0 || state.length === 0 && state.ended) return 0; - if (state.objectMode) return 1; - if (n !== n) { - // Only flow one buffer at a time - if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; - } - // If we're asking for more than the current hwm, then raise the hwm. - if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); - if (n <= state.length) return n; - // Don't have enough - if (!state.ended) { - state.needReadable = true; - return 0; - } - return state.length; -} - -// you can override either this method, or the async _read(n) below. -Readable.prototype.read = function (n) { - debug('read', n); - n = parseInt(n, 10); - var state = this._readableState; - var nOrig = n; - - if (n !== 0) state.emittedReadable = false; - - // if we're doing read(0) to trigger a readable event, but we - // already have a bunch of data in the buffer, then just trigger - // the 'readable' event and move on. - if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { - debug('read: emitReadable', state.length, state.ended); - if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this); - return null; - } - - n = howMuchToRead(n, state); - - // if we've ended, and we're now clear, then finish it up. - if (n === 0 && state.ended) { - if (state.length === 0) endReadable(this); - return null; - } - - // All the actual chunk generation logic needs to be - // *below* the call to _read. The reason is that in certain - // synthetic stream cases, such as passthrough streams, _read - // may be a completely synchronous operation which may change - // the state of the read buffer, providing enough data when - // before there was *not* enough. - // - // So, the steps are: - // 1. Figure out what the state of things will be after we do - // a read from the buffer. - // - // 2. If that resulting state will trigger a _read, then call _read. - // Note that this may be asynchronous, or synchronous. Yes, it is - // deeply ugly to write APIs this way, but that still doesn't mean - // that the Readable class should behave improperly, as streams are - // designed to be sync/async agnostic. - // Take note if the _read call is sync or async (ie, if the read call - // has returned yet), so that we know whether or not it's safe to emit - // 'readable' etc. - // - // 3. Actually pull the requested chunks out of the buffer and return. - - // if we need a readable event, then we need to do some reading. - var doRead = state.needReadable; - debug('need readable', doRead); - - // if we currently have less than the highWaterMark, then also read some - if (state.length === 0 || state.length - n < state.highWaterMark) { - doRead = true; - debug('length less than watermark', doRead); - } - - // however, if we've ended, then there's no point, and if we're already - // reading, then it's unnecessary. - if (state.ended || state.reading) { - doRead = false; - debug('reading or ended', doRead); - } else if (doRead) { - debug('do read'); - state.reading = true; - state.sync = true; - // if the length is currently zero, then we *need* a readable event. - if (state.length === 0) state.needReadable = true; - // call internal read method - this._read(state.highWaterMark); - state.sync = false; - // If _read pushed data synchronously, then `reading` will be false, - // and we need to re-evaluate how much data we can return to the user. - if (!state.reading) n = howMuchToRead(nOrig, state); - } - - var ret; - if (n > 0) ret = fromList(n, state);else ret = null; - - if (ret === null) { - state.needReadable = true; - n = 0; - } else { - state.length -= n; - } - - if (state.length === 0) { - // If we have nothing in the buffer, then we want to know - // as soon as we *do* get something into the buffer. - if (!state.ended) state.needReadable = true; - - // If we tried to read() past the EOF, then emit end on the next tick. - if (nOrig !== n && state.ended) endReadable(this); - } - - if (ret !== null) this.emit('data', ret); - - return ret; -}; - -function chunkInvalid(state, chunk) { - var er = null; - if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) { - er = new TypeError('Invalid non-string/buffer chunk'); - } - return er; -} - -function onEofChunk(stream, state) { - if (state.ended) return; - if (state.decoder) { - var chunk = state.decoder.end(); - if (chunk && chunk.length) { - state.buffer.push(chunk); - state.length += state.objectMode ? 1 : chunk.length; - } - } - state.ended = true; - - // emit 'readable' now to make sure it gets picked up. - emitReadable(stream); -} - -// Don't emit readable right away in sync mode, because this can trigger -// another read() call => stack overflow. This way, it might trigger -// a nextTick recursion warning, but that's not so bad. -function emitReadable(stream) { - var state = stream._readableState; - state.needReadable = false; - if (!state.emittedReadable) { - debug('emitReadable', state.flowing); - state.emittedReadable = true; - if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream); - } -} - -function emitReadable_(stream) { - debug('emit readable'); - stream.emit('readable'); - flow(stream); -} - -// at this point, the user has presumably seen the 'readable' event, -// and called read() to consume some data. that may have triggered -// in turn another _read(n) call, in which case reading = true if -// it's in progress. -// However, if we're not ended, or reading, and the length < hwm, -// then go ahead and try to read some more preemptively. -function maybeReadMore(stream, state) { - if (!state.readingMore) { - state.readingMore = true; - processNextTick(maybeReadMore_, stream, state); - } -} - -function maybeReadMore_(stream, state) { - var len = state.length; - while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { - debug('maybeReadMore read 0'); - stream.read(0); - if (len === state.length) - // didn't get any data, stop spinning. - break;else len = state.length; - } - state.readingMore = false; -} - -// abstract method. to be overridden in specific implementation classes. -// call cb(er, data) where data is <= n in length. -// for virtual (non-string, non-buffer) streams, "length" is somewhat -// arbitrary, and perhaps not very meaningful. -Readable.prototype._read = function (n) { - this.emit('error', new Error('_read() is not implemented')); -}; - -Readable.prototype.pipe = function (dest, pipeOpts) { - var src = this; - var state = this._readableState; - - switch (state.pipesCount) { - case 0: - state.pipes = dest; - break; - case 1: - state.pipes = [state.pipes, dest]; - break; - default: - state.pipes.push(dest); - break; - } - state.pipesCount += 1; - debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); - - var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; - - var endFn = doEnd ? onend : cleanup; - if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn); - - dest.on('unpipe', onunpipe); - function onunpipe(readable) { - debug('onunpipe'); - if (readable === src) { - cleanup(); - } - } - - function onend() { - debug('onend'); - dest.end(); - } - - // when the dest drains, it reduces the awaitDrain counter - // on the source. This would be more elegant with a .once() - // handler in flow(), but adding and removing repeatedly is - // too slow. - var ondrain = pipeOnDrain(src); - dest.on('drain', ondrain); - - var cleanedUp = false; - function cleanup() { - debug('cleanup'); - // cleanup event handlers once the pipe is broken - dest.removeListener('close', onclose); - dest.removeListener('finish', onfinish); - dest.removeListener('drain', ondrain); - dest.removeListener('error', onerror); - dest.removeListener('unpipe', onunpipe); - src.removeListener('end', onend); - src.removeListener('end', cleanup); - src.removeListener('data', ondata); - - cleanedUp = true; - - // if the reader is waiting for a drain event from this - // specific writer, then it would cause it to never start - // flowing again. - // So, if this is awaiting a drain, then we just call it now. - // If we don't know, then assume that we are waiting for one. - if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); - } - - // If the user pushes more data while we're writing to dest then we'll end up - // in ondata again. However, we only want to increase awaitDrain once because - // dest will only emit one 'drain' event for the multiple writes. - // => Introduce a guard on increasing awaitDrain. - var increasedAwaitDrain = false; - src.on('data', ondata); - function ondata(chunk) { - debug('ondata'); - increasedAwaitDrain = false; - var ret = dest.write(chunk); - if (false === ret && !increasedAwaitDrain) { - // If the user unpiped during `dest.write()`, it is possible - // to get stuck in a permanently paused state if that write - // also returned false. - // => Check whether `dest` is still a piping destination. - if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { - debug('false write response, pause', src._readableState.awaitDrain); - src._readableState.awaitDrain++; - increasedAwaitDrain = true; - } - src.pause(); - } - } - - // if the dest has an error, then stop piping into it. - // however, don't suppress the throwing behavior for this. - function onerror(er) { - debug('onerror', er); - unpipe(); - dest.removeListener('error', onerror); - if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); - } - - // Make sure our error handler is attached before userland ones. - prependListener(dest, 'error', onerror); - - // Both close and finish should trigger unpipe, but only once. - function onclose() { - dest.removeListener('finish', onfinish); - unpipe(); - } - dest.once('close', onclose); - function onfinish() { - debug('onfinish'); - dest.removeListener('close', onclose); - unpipe(); - } - dest.once('finish', onfinish); - - function unpipe() { - debug('unpipe'); - src.unpipe(dest); - } - - // tell the dest that it's being piped to - dest.emit('pipe', src); - - // start the flow if it hasn't been started already. - if (!state.flowing) { - debug('pipe resume'); - src.resume(); - } - - return dest; -}; - -function pipeOnDrain(src) { - return function () { - var state = src._readableState; - debug('pipeOnDrain', state.awaitDrain); - if (state.awaitDrain) state.awaitDrain--; - if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { - state.flowing = true; - flow(src); - } - }; -} - -Readable.prototype.unpipe = function (dest) { - var state = this._readableState; - - // if we're not piping anywhere, then do nothing. - if (state.pipesCount === 0) return this; - - // just one destination. most common case. - if (state.pipesCount === 1) { - // passed in one, but it's not the right one. - if (dest && dest !== state.pipes) return this; - - if (!dest) dest = state.pipes; - - // got a match. - state.pipes = null; - state.pipesCount = 0; - state.flowing = false; - if (dest) dest.emit('unpipe', this); - return this; - } - - // slow case. multiple pipe destinations. - - if (!dest) { - // remove all. - var dests = state.pipes; - var len = state.pipesCount; - state.pipes = null; - state.pipesCount = 0; - state.flowing = false; - - for (var i = 0; i < len; i++) { - dests[i].emit('unpipe', this); - }return this; - } - - // try to find the right one. - var index = indexOf(state.pipes, dest); - if (index === -1) return this; - - state.pipes.splice(index, 1); - state.pipesCount -= 1; - if (state.pipesCount === 1) state.pipes = state.pipes[0]; - - dest.emit('unpipe', this); - - return this; -}; - -// set up data events if they are asked for -// Ensure readable listeners eventually get something -Readable.prototype.on = function (ev, fn) { - var res = Stream.prototype.on.call(this, ev, fn); - - if (ev === 'data') { - // Start flowing on next tick if stream isn't explicitly paused - if (this._readableState.flowing !== false) this.resume(); - } else if (ev === 'readable') { - var state = this._readableState; - if (!state.endEmitted && !state.readableListening) { - state.readableListening = state.needReadable = true; - state.emittedReadable = false; - if (!state.reading) { - processNextTick(nReadingNextTick, this); - } else if (state.length) { - emitReadable(this, state); + function sum (A) { + var acc = 0; + accumulate(A, acc); + function accumulate(x) { + for (var i = 0; i < x.length; i++) { + if (Array.isArray(x[i])) + accumulate(x[i], acc); + else + acc += x[i]; } } + return acc; } - return res; -}; -Readable.prototype.addListener = Readable.prototype.on; - -function nReadingNextTick(self) { - debug('readable nexttick read 0'); - self.read(0); -} - -// pause() and resume() are remnants of the legacy readable stream API -// If the user uses them, then switch into old mode. -Readable.prototype.resume = function () { - var state = this._readableState; - if (!state.flowing) { - debug('resume'); - state.flowing = true; - resume(this, state); - } - return this; -}; - -function resume(stream, state) { - if (!state.resumeScheduled) { - state.resumeScheduled = true; - processNextTick(resume_, stream, state); - } -} - -function resume_(stream, state) { - if (!state.reading) { - debug('resume read 0'); - stream.read(0); - } - - state.resumeScheduled = false; - state.awaitDrain = 0; - stream.emit('resume'); - flow(stream); - if (state.flowing && !state.reading) stream.read(0); -} - -Readable.prototype.pause = function () { - debug('call pause flowing=%j', this._readableState.flowing); - if (false !== this._readableState.flowing) { - debug('pause'); - this._readableState.flowing = false; - this.emit('pause'); - } - return this; -}; - -function flow(stream) { - var state = stream._readableState; - debug('flow', state.flowing); - while (state.flowing && stream.read() !== null) {} -} - -// wrap an old-style stream as the async data source. -// This is *not* part of the readable stream interface. -// It is an ugly unfortunate mess of history. -Readable.prototype.wrap = function (stream) { - var state = this._readableState; - var paused = false; - - var self = this; - stream.on('end', function () { - debug('wrapped end'); - if (state.decoder && !state.ended) { - var chunk = state.decoder.end(); - if (chunk && chunk.length) self.push(chunk); - } - - self.push(null); - }); - - stream.on('data', function (chunk) { - debug('wrapped data'); - if (state.decoder) chunk = state.decoder.write(chunk); - - // don't skip over falsy values in objectMode - if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; - - var ret = self.push(chunk); - if (!ret) { - paused = true; - stream.pause(); - } - }); - - // proxy all the other methods. - // important when wrapping filters and duplexes. - for (var i in stream) { - if (this[i] === undefined && typeof stream[i] === 'function') { - this[i] = function (method) { - return function () { - return stream[method].apply(stream, arguments); - }; - }(i); - } - } - - // proxy certain important events. - var events = ['error', 'close', 'destroy', 'pause', 'resume']; - forEach(events, function (ev) { - stream.on(ev, self.emit.bind(self, ev)); - }); - - // when we try to consume some more bytes, simply unpause the - // underlying stream. - self._read = function (n) { - debug('wrapped _read', n); - if (paused) { - paused = false; - stream.resume(); - } - }; - - return self; -}; - -// exposed for testing purposes only. -Readable._fromList = fromList; - -// Pluck off n bytes from an array of buffers. -// Length is the combined lengths of all the buffers in the list. -// This function is designed to be inlinable, so please take care when making -// changes to the function body. -function fromList(n, state) { - // nothing buffered - if (state.length === 0) return null; - - var ret; - if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { - // read it all, truncate the list - if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length); - state.buffer.clear(); - } else { - // read part of list - ret = fromListPartial(n, state.buffer, state.decoder); - } - - return ret; -} - -// Extracts only enough buffered data to satisfy the amount requested. -// This function is designed to be inlinable, so please take care when making -// changes to the function body. -function fromListPartial(n, list, hasStrings) { - var ret; - if (n < list.head.data.length) { - // slice is the same for buffers and strings - ret = list.head.data.slice(0, n); - list.head.data = list.head.data.slice(n); - } else if (n === list.head.data.length) { - // first chunk is a perfect match - ret = list.shift(); - } else { - // result spans more than one buffer - ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); - } - return ret; -} - -// Copies a specified amount of characters from the list of buffered data -// chunks. -// This function is designed to be inlinable, so please take care when making -// changes to the function body. -function copyFromBufferString(n, list) { - var p = list.head; - var c = 1; - var ret = p.data; - n -= ret.length; - while (p = p.next) { - var str = p.data; - var nb = n > str.length ? str.length : n; - if (nb === str.length) ret += str;else ret += str.slice(0, n); - n -= nb; - if (n === 0) { - if (nb === str.length) { - ++c; - if (p.next) list.head = p.next;else list.head = list.tail = null; - } else { - list.head = p; - p.data = str.slice(nb); - } - break; - } - ++c; - } - list.length -= c; - return ret; -} - -// Copies a specified amount of bytes from the list of buffered data chunks. -// This function is designed to be inlinable, so please take care when making -// changes to the function body. -function copyFromBuffer(n, list) { - var ret = bufferShim.allocUnsafe(n); - var p = list.head; - var c = 1; - p.data.copy(ret); - n -= p.data.length; - while (p = p.next) { - var buf = p.data; - var nb = n > buf.length ? buf.length : n; - buf.copy(ret, ret.length - n, 0, nb); - n -= nb; - if (n === 0) { - if (nb === buf.length) { - ++c; - if (p.next) list.head = p.next;else list.head = list.tail = null; - } else { - list.head = p; - p.data = buf.slice(nb); - } - break; - } - ++c; - } - list.length -= c; - return ret; -} - -function endReadable(stream) { - var state = stream._readableState; - - // If we get here before consuming all the bytes, then that is a - // bug in node. Should never happen. - if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); - - if (!state.endEmitted) { - state.ended = true; - processNextTick(endReadableNT, state, stream); - } -} - -function endReadableNT(state, stream) { - // Check that we didn't get one last unshift. - if (!state.endEmitted && state.length === 0) { - state.endEmitted = true; - stream.readable = false; - stream.emit('end'); - } -} - -function forEach(xs, f) { - for (var i = 0, l = xs.length; i < l; i++) { - f(xs[i], i); - } -} - -function indexOf(xs, x) { - for (var i = 0, l = xs.length; i < l; i++) { - if (xs[i] === x) return i; - } - return -1; -} -}).call(this,require('_process')) -},{"./_stream_duplex":4,"./internal/streams/BufferList":9,"_process":41,"buffer":33,"buffer-shims":10,"core-util-is":11,"events":37,"inherits":12,"isarray":13,"process-nextick-args":14,"string_decoder/":15,"util":19}],7:[function(require,module,exports){ -// a transform stream is a readable/writable stream where you do -// something with the data. Sometimes it's called a "filter", -// but that's not a great name for it, since that implies a thing where -// some bits pass through, and others are simply ignored. (That would -// be a valid example of a transform, of course.) -// -// While the output is causally related to the input, it's not a -// necessarily symmetric or synchronous transformation. For example, -// a zlib stream might take multiple plain-text writes(), and then -// emit a single compressed chunk some time in the future. -// -// Here's how this works: -// -// The Transform stream has all the aspects of the readable and writable -// stream classes. When you write(chunk), that calls _write(chunk,cb) -// internally, and returns false if there's a lot of pending writes -// buffered up. When you call read(), that calls _read(n) until -// there's enough pending readable data buffered up. -// -// In a transform stream, the written data is placed in a buffer. When -// _read(n) is called, it transforms the queued up data, calling the -// buffered _write cb's as it consumes chunks. If consuming a single -// written chunk would result in multiple output chunks, then the first -// outputted bit calls the readcb, and subsequent chunks just go into -// the read buffer, and will cause it to emit 'readable' if necessary. -// -// This way, back-pressure is actually determined by the reading side, -// since _read has to be called to start processing a new chunk. However, -// a pathological inflate type of transform can cause excessive buffering -// here. For example, imagine a stream where every byte of input is -// interpreted as an integer from 0-255, and then results in that many -// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in -// 1kb of data being output. In this case, you could write a very small -// amount of input, and end up with a very large amount of output. In -// such a pathological inflating mechanism, there'd be no way to tell -// the system to stop doing the transform. A single 4MB write could -// cause the system to run out of memory. -// -// However, even in such a pathological case, only a single written chunk -// would be consumed, and then the rest would wait (un-transformed) until -// the results of the previous transformed chunk were consumed. - -'use strict'; - -module.exports = Transform; - -var Duplex = require('./_stream_duplex'); - -/**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); -/**/ - -util.inherits(Transform, Duplex); - -function TransformState(stream) { - this.afterTransform = function (er, data) { - return afterTransform(stream, er, data); - }; - - this.needTransform = false; - this.transforming = false; - this.writecb = null; - this.writechunk = null; - this.writeencoding = null; -} - -function afterTransform(stream, er, data) { - var ts = stream._transformState; - ts.transforming = false; - - var cb = ts.writecb; - - if (!cb) return stream.emit('error', new Error('no writecb in Transform class')); - - ts.writechunk = null; - ts.writecb = null; - - if (data !== null && data !== undefined) stream.push(data); - - cb(er); - - var rs = stream._readableState; - rs.reading = false; - if (rs.needReadable || rs.length < rs.highWaterMark) { - stream._read(rs.highWaterMark); - } -} - -function Transform(options) { - if (!(this instanceof Transform)) return new Transform(options); - - Duplex.call(this, options); - - this._transformState = new TransformState(this); - - var stream = this; - - // start out asking for a readable event once data is transformed. - this._readableState.needReadable = true; - - // we have implemented the _read method, and done the other things - // that Readable wants before the first _read call, so unset the - // sync guard flag. - this._readableState.sync = false; - - if (options) { - if (typeof options.transform === 'function') this._transform = options.transform; - - if (typeof options.flush === 'function') this._flush = options.flush; - } - - // When the writable side finishes, then flush out anything remaining. - this.once('prefinish', function () { - if (typeof this._flush === 'function') this._flush(function (er, data) { - done(stream, er, data); - });else done(stream); - }); -} - -Transform.prototype.push = function (chunk, encoding) { - this._transformState.needTransform = false; - return Duplex.prototype.push.call(this, chunk, encoding); -}; - -// This is the part where you do stuff! -// override this function in implementation classes. -// 'chunk' is an input chunk. -// -// Call `push(newChunk)` to pass along transformed output -// to the readable side. You may call 'push' zero or more times. -// -// Call `cb(err)` when you are done with this chunk. If you pass -// an error, then that'll put the hurt on the whole operation. If you -// never call cb(), then you'll never get another chunk. -Transform.prototype._transform = function (chunk, encoding, cb) { - throw new Error('_transform() is not implemented'); -}; - -Transform.prototype._write = function (chunk, encoding, cb) { - var ts = this._transformState; - ts.writecb = cb; - ts.writechunk = chunk; - ts.writeencoding = encoding; - if (!ts.transforming) { - var rs = this._readableState; - if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); - } -}; - -// Doesn't matter what the args are here. -// _transform does all the work. -// That we got here means that the readable side wants more data. -Transform.prototype._read = function (n) { - var ts = this._transformState; - - if (ts.writechunk !== null && ts.writecb && !ts.transforming) { - ts.transforming = true; - this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); - } else { - // mark that we need a transform, so that any data that comes in - // will get processed, now that we've asked for it. - ts.needTransform = true; - } -}; - -function done(stream, er, data) { - if (er) return stream.emit('error', er); - - if (data !== null && data !== undefined) stream.push(data); - - // if there's nothing in the write buffer, then that means - // that nothing more will ever be provided - var ws = stream._writableState; - var ts = stream._transformState; - - if (ws.length) throw new Error('Calling transform done when ws.length != 0'); - - if (ts.transforming) throw new Error('Calling transform done when still transforming'); - - return stream.push(null); -} -},{"./_stream_duplex":4,"core-util-is":11,"inherits":12}],8:[function(require,module,exports){ -(function (process){ -// A bit simpler than readable streams. -// Implement an async ._write(chunk, encoding, cb), and it'll handle all -// the drain event emission and buffering. - -'use strict'; - -module.exports = Writable; - -/**/ -var processNextTick = require('process-nextick-args'); -/**/ - -/**/ -var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : processNextTick; -/**/ - -/**/ -var Duplex; -/**/ - -Writable.WritableState = WritableState; - -/**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); -/**/ - -/**/ -var internalUtil = { - deprecate: require('util-deprecate') -}; -/**/ - -/**/ -var Stream; -(function () { - try { - Stream = require('st' + 'ream'); - } catch (_) {} finally { - if (!Stream) Stream = require('events').EventEmitter; - } -})(); -/**/ - -var Buffer = require('buffer').Buffer; -/**/ -var bufferShim = require('buffer-shims'); -/**/ - -util.inherits(Writable, Stream); - -function nop() {} - -function WriteReq(chunk, encoding, cb) { - this.chunk = chunk; - this.encoding = encoding; - this.callback = cb; - this.next = null; -} - -function WritableState(options, stream) { - Duplex = Duplex || require('./_stream_duplex'); - - options = options || {}; - - // object stream flag to indicate whether or not this stream - // contains buffers or objects. - this.objectMode = !!options.objectMode; - - if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode; - - // the point at which write() starts returning false - // Note: 0 is a valid value, means that we always return false if - // the entire buffer is not flushed immediately on write() - var hwm = options.highWaterMark; - var defaultHwm = this.objectMode ? 16 : 16 * 1024; - this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; - - // cast to ints. - this.highWaterMark = ~ ~this.highWaterMark; - - // drain event flag. - this.needDrain = false; - // at the start of calling end() - this.ending = false; - // when end() has been called, and returned - this.ended = false; - // when 'finish' is emitted - this.finished = false; - - // should we decode strings into buffers before passing to _write? - // this is here so that some node-core streams can optimize string - // handling at a lower level. - var noDecode = options.decodeStrings === false; - this.decodeStrings = !noDecode; - - // Crypto is kind of old and crusty. Historically, its default string - // encoding is 'binary' so we have to make this configurable. - // Everything else in the universe uses 'utf8', though. - this.defaultEncoding = options.defaultEncoding || 'utf8'; - - // not an actual buffer we keep track of, but a measurement - // of how much we're waiting to get pushed to some underlying - // socket or file. - this.length = 0; - - // a flag to see when we're in the middle of a write. - this.writing = false; - - // when true all writes will be buffered until .uncork() call - this.corked = 0; - - // a flag to be able to tell if the onwrite cb is called immediately, - // or on a later tick. We set this to true at first, because any - // actions that shouldn't happen until "later" should generally also - // not happen before the first write call. - this.sync = true; - - // a flag to know if we're processing previously buffered items, which - // may call the _write() callback in the same tick, so that we don't - // end up in an overlapped onwrite situation. - this.bufferProcessing = false; - - // the callback that's passed to _write(chunk,cb) - this.onwrite = function (er) { - onwrite(stream, er); - }; - - // the callback that the user supplies to write(chunk,encoding,cb) - this.writecb = null; - - // the amount that is being written when _write is called. - this.writelen = 0; - - this.bufferedRequest = null; - this.lastBufferedRequest = null; - - // number of pending user-supplied write callbacks - // this must be 0 before 'finish' can be emitted - this.pendingcb = 0; - - // emit prefinish if the only thing we're waiting for is _write cbs - // This is relevant for synchronous Transform streams - this.prefinished = false; - - // True if the error was already emitted and should not be thrown again - this.errorEmitted = false; - - // count buffered requests - this.bufferedRequestCount = 0; - - // allocate the first CorkedRequest, there is always - // one allocated and free to use, and we maintain at most two - this.corkedRequestsFree = new CorkedRequest(this); -} - -WritableState.prototype.getBuffer = function getBuffer() { - var current = this.bufferedRequest; - var out = []; - while (current) { - out.push(current); - current = current.next; - } - return out; -}; - -(function () { - try { - Object.defineProperty(WritableState.prototype, 'buffer', { - get: internalUtil.deprecate(function () { - return this.getBuffer(); - }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.') - }); - } catch (_) {} -})(); - -// Test _writableState for inheritance to account for Duplex streams, -// whose prototype chain only points to Readable. -var realHasInstance; -if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { - realHasInstance = Function.prototype[Symbol.hasInstance]; - Object.defineProperty(Writable, Symbol.hasInstance, { - value: function (object) { - if (realHasInstance.call(this, object)) return true; - - return object && object._writableState instanceof WritableState; - } - }); -} else { - realHasInstance = function (object) { - return object instanceof this; - }; -} - -function Writable(options) { - Duplex = Duplex || require('./_stream_duplex'); - - // Writable ctor is applied to Duplexes, too. - // `realHasInstance` is necessary because using plain `instanceof` - // would return false, as no `_writableState` property is attached. - - // Trying to use the custom `instanceof` for Writable here will also break the - // Node.js LazyTransform implementation, which has a non-trivial getter for - // `_writableState` that would lead to infinite recursion. - if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) { - return new Writable(options); - } - - this._writableState = new WritableState(options, this); - - // legacy. - this.writable = true; - - if (options) { - if (typeof options.write === 'function') this._write = options.write; - - if (typeof options.writev === 'function') this._writev = options.writev; - } - - Stream.call(this); -} - -// Otherwise people can pipe Writable streams, which is just wrong. -Writable.prototype.pipe = function () { - this.emit('error', new Error('Cannot pipe, not readable')); -}; - -function writeAfterEnd(stream, cb) { - var er = new Error('write after end'); - // TODO: defer error events consistently everywhere, not just the cb - stream.emit('error', er); - processNextTick(cb, er); -} - -// If we get something that is not a buffer, string, null, or undefined, -// and we're not in objectMode, then that's an error. -// Otherwise stream chunks are all considered to be of length=1, and the -// watermarks determine how many objects to keep in the buffer, rather than -// how many bytes or characters. -function validChunk(stream, state, chunk, cb) { - var valid = true; - var er = false; - // Always throw error if a null is written - // if we are not in object mode then throw - // if it is not a buffer, string, or undefined. - if (chunk === null) { - er = new TypeError('May not write null values to stream'); - } else if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { - er = new TypeError('Invalid non-string/buffer chunk'); - } - if (er) { - stream.emit('error', er); - processNextTick(cb, er); - valid = false; - } - return valid; -} - -Writable.prototype.write = function (chunk, encoding, cb) { - var state = this._writableState; - var ret = false; - - if (typeof encoding === 'function') { - cb = encoding; - encoding = null; - } - - if (Buffer.isBuffer(chunk)) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding; - - if (typeof cb !== 'function') cb = nop; - - if (state.ended) writeAfterEnd(this, cb);else if (validChunk(this, state, chunk, cb)) { - state.pendingcb++; - ret = writeOrBuffer(this, state, chunk, encoding, cb); - } - - return ret; -}; - -Writable.prototype.cork = function () { - var state = this._writableState; - - state.corked++; -}; - -Writable.prototype.uncork = function () { - var state = this._writableState; - - if (state.corked) { - state.corked--; - - if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); - } -}; - -Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { - // node::ParseEncoding() requires lower case. - if (typeof encoding === 'string') encoding = encoding.toLowerCase(); - if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding); - this._writableState.defaultEncoding = encoding; - return this; -}; - -function decodeChunk(state, chunk, encoding) { - if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { - chunk = bufferShim.from(chunk, encoding); - } - return chunk; -} - -// if we're already writing something, then just put this -// in the queue, and wait our turn. Otherwise, call _write -// If we return false, then we need a drain event, so set that flag. -function writeOrBuffer(stream, state, chunk, encoding, cb) { - chunk = decodeChunk(state, chunk, encoding); - - if (Buffer.isBuffer(chunk)) encoding = 'buffer'; - var len = state.objectMode ? 1 : chunk.length; - - state.length += len; - - var ret = state.length < state.highWaterMark; - // we must ensure that previous needDrain will not be reset to false. - if (!ret) state.needDrain = true; - - if (state.writing || state.corked) { - var last = state.lastBufferedRequest; - state.lastBufferedRequest = new WriteReq(chunk, encoding, cb); - if (last) { - last.next = state.lastBufferedRequest; - } else { - state.bufferedRequest = state.lastBufferedRequest; - } - state.bufferedRequestCount += 1; - } else { - doWrite(stream, state, false, len, chunk, encoding, cb); - } - - return ret; -} - -function doWrite(stream, state, writev, len, chunk, encoding, cb) { - state.writelen = len; - state.writecb = cb; - state.writing = true; - state.sync = true; - if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite); - state.sync = false; -} - -function onwriteError(stream, state, sync, er, cb) { - --state.pendingcb; - if (sync) processNextTick(cb, er);else cb(er); - - stream._writableState.errorEmitted = true; - stream.emit('error', er); -} - -function onwriteStateUpdate(state) { - state.writing = false; - state.writecb = null; - state.length -= state.writelen; - state.writelen = 0; -} - -function onwrite(stream, er) { - var state = stream._writableState; - var sync = state.sync; - var cb = state.writecb; - - onwriteStateUpdate(state); - - if (er) onwriteError(stream, state, sync, er, cb);else { - // Check if we're actually ready to finish, but don't emit yet - var finished = needFinish(state); - - if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { - clearBuffer(stream, state); - } - - if (sync) { - /**/ - asyncWrite(afterWrite, stream, state, finished, cb); - /**/ - } else { - afterWrite(stream, state, finished, cb); - } - } -} - -function afterWrite(stream, state, finished, cb) { - if (!finished) onwriteDrain(stream, state); - state.pendingcb--; - cb(); - finishMaybe(stream, state); -} - -// Must force callback to be called on nextTick, so that we don't -// emit 'drain' before the write() consumer gets the 'false' return -// value, and has a chance to attach a 'drain' listener. -function onwriteDrain(stream, state) { - if (state.length === 0 && state.needDrain) { - state.needDrain = false; - stream.emit('drain'); - } -} - -// if there's something in the buffer waiting, then process it -function clearBuffer(stream, state) { - state.bufferProcessing = true; - var entry = state.bufferedRequest; - - if (stream._writev && entry && entry.next) { - // Fast case, write everything using _writev() - var l = state.bufferedRequestCount; - var buffer = new Array(l); - var holder = state.corkedRequestsFree; - holder.entry = entry; - - var count = 0; - while (entry) { - buffer[count] = entry; - entry = entry.next; - count += 1; - } - - doWrite(stream, state, true, state.length, buffer, '', holder.finish); - - // doWrite is almost always async, defer these to save a bit of time - // as the hot path ends with doWrite - state.pendingcb++; - state.lastBufferedRequest = null; - if (holder.next) { - state.corkedRequestsFree = holder.next; - holder.next = null; - } else { - state.corkedRequestsFree = new CorkedRequest(state); - } - } else { - // Slow case, write chunks one-by-one - while (entry) { - var chunk = entry.chunk; - var encoding = entry.encoding; - var cb = entry.callback; - var len = state.objectMode ? 1 : chunk.length; - - doWrite(stream, state, false, len, chunk, encoding, cb); - entry = entry.next; - // if we didn't call the onwrite immediately, then - // it means that we need to wait until it does. - // also, that means that the chunk and cb are currently - // being processed, so move the buffer counter past them. - if (state.writing) { - break; + function copy2D (arr) { + var carr = []; + for (var i = 0; i < arr.length; ++i) { + carr[i] = []; + for (var j = 0; j < arr[i].length; ++j) { + carr[i][j] = arr[i][j]; } } - if (entry === null) state.lastBufferedRequest = null; + return carr; } - state.bufferedRequestCount = 0; - state.bufferedRequest = entry; - state.bufferProcessing = false; -} -Writable.prototype._write = function (chunk, encoding, cb) { - cb(new Error('_write() is not implemented')); -}; - -Writable.prototype._writev = null; - -Writable.prototype.end = function (chunk, encoding, cb) { - var state = this._writableState; - - if (typeof chunk === 'function') { - cb = chunk; - chunk = null; - encoding = null; - } else if (typeof encoding === 'function') { - cb = encoding; - encoding = null; - } - - if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); - - // .end() fully uncorks - if (state.corked) { - state.corked = 1; - this.uncork(); - } - - // ignore unnecessary end() calls. - if (!state.ending && !state.finished) endWritable(this, state, cb); -}; - -function needFinish(state) { - return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; -} - -function prefinish(stream, state) { - if (!state.prefinished) { - state.prefinished = true; - stream.emit('prefinish'); - } -} - -function finishMaybe(stream, state) { - var need = needFinish(state); - if (need) { - if (state.pendingcb === 0) { - prefinish(stream, state); - state.finished = true; - stream.emit('finish'); - } else { - prefinish(stream, state); - } - } - return need; -} - -function endWritable(stream, state, cb) { - state.ending = true; - finishMaybe(stream, state); - if (cb) { - if (state.finished) processNextTick(cb);else stream.once('finish', cb); - } - state.ended = true; - stream.writable = false; -} - -// It seems a linked list but it is not -// there will be only 2 of these for each stream -function CorkedRequest(state) { - var _this = this; - - this.next = null; - this.entry = null; - - this.finish = function (err) { - var entry = _this.entry; - _this.entry = null; - while (entry) { - var cb = entry.callback; - state.pendingcb--; - cb(err); - entry = entry.next; - } - if (state.corkedRequestsFree) { - state.corkedRequestsFree.next = _this; - } else { - state.corkedRequestsFree = _this; - } - }; -} -}).call(this,require('_process')) -},{"./_stream_duplex":4,"_process":41,"buffer":33,"buffer-shims":10,"core-util-is":11,"events":37,"inherits":12,"process-nextick-args":14,"util-deprecate":16}],9:[function(require,module,exports){ -'use strict'; - -var Buffer = require('buffer').Buffer; -/**/ -var bufferShim = require('buffer-shims'); -/**/ - -module.exports = BufferList; - -function BufferList() { - this.head = null; - this.tail = null; - this.length = 0; -} - -BufferList.prototype.push = function (v) { - var entry = { data: v, next: null }; - if (this.length > 0) this.tail.next = entry;else this.head = entry; - this.tail = entry; - ++this.length; -}; - -BufferList.prototype.unshift = function (v) { - var entry = { data: v, next: this.head }; - if (this.length === 0) this.tail = entry; - this.head = entry; - ++this.length; -}; - -BufferList.prototype.shift = function () { - if (this.length === 0) return; - var ret = this.head.data; - if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next; - --this.length; - return ret; -}; - -BufferList.prototype.clear = function () { - this.head = this.tail = null; - this.length = 0; -}; - -BufferList.prototype.join = function (s) { - if (this.length === 0) return ''; - var p = this.head; - var ret = '' + p.data; - while (p = p.next) { - ret += s + p.data; - }return ret; -}; - -BufferList.prototype.concat = function (n) { - if (this.length === 0) return bufferShim.alloc(0); - if (this.length === 1) return this.head.data; - var ret = bufferShim.allocUnsafe(n >>> 0); - var p = this.head; - var i = 0; - while (p) { - p.data.copy(ret, i); - i += p.data.length; - p = p.next; - } - return ret; -}; -},{"buffer":33,"buffer-shims":10}],10:[function(require,module,exports){ -(function (global){ -'use strict'; - -var buffer = require('buffer'); -var Buffer = buffer.Buffer; -var SlowBuffer = buffer.SlowBuffer; -var MAX_LEN = buffer.kMaxLength || 2147483647; -exports.alloc = function alloc(size, fill, encoding) { - if (typeof Buffer.alloc === 'function') { - return Buffer.alloc(size, fill, encoding); - } - if (typeof encoding === 'number') { - throw new TypeError('encoding must not be number'); - } - if (typeof size !== 'number') { - throw new TypeError('size must be a number'); - } - if (size > MAX_LEN) { - throw new RangeError('size is too large'); - } - var enc = encoding; - var _fill = fill; - if (_fill === undefined) { - enc = undefined; - _fill = 0; - } - var buf = new Buffer(size); - if (typeof _fill === 'string') { - var fillBuf = new Buffer(_fill, enc); - var flen = fillBuf.length; - var i = -1; - while (++i < size) { - buf[i] = fillBuf[i % flen]; - } - } else { - buf.fill(_fill); - } - return buf; -} -exports.allocUnsafe = function allocUnsafe(size) { - if (typeof Buffer.allocUnsafe === 'function') { - return Buffer.allocUnsafe(size); - } - if (typeof size !== 'number') { - throw new TypeError('size must be a number'); - } - if (size > MAX_LEN) { - throw new RangeError('size is too large'); - } - return new Buffer(size); -} -exports.from = function from(value, encodingOrOffset, length) { - if (typeof Buffer.from === 'function' && (!global.Uint8Array || Uint8Array.from !== Buffer.from)) { - return Buffer.from(value, encodingOrOffset, length); - } - if (typeof value === 'number') { - throw new TypeError('"value" argument must not be a number'); - } - if (typeof value === 'string') { - return new Buffer(value, encodingOrOffset); - } - if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) { - var offset = encodingOrOffset; - if (arguments.length === 1) { - return new Buffer(value); - } - if (typeof offset === 'undefined') { - offset = 0; - } - var len = length; - if (typeof len === 'undefined') { - len = value.byteLength - offset; - } - if (offset >= value.byteLength) { - throw new RangeError('\'offset\' is out of bounds'); - } - if (len > value.byteLength - offset) { - throw new RangeError('\'length\' is out of bounds'); - } - return new Buffer(value.slice(offset, offset + len)); - } - if (Buffer.isBuffer(value)) { - var out = new Buffer(value.length); - value.copy(out, 0, 0, value.length); - return out; - } - if (value) { - if (Array.isArray(value) || (typeof ArrayBuffer !== 'undefined' && value.buffer instanceof ArrayBuffer) || 'length' in value) { - return new Buffer(value); - } - if (value.type === 'Buffer' && Array.isArray(value.data)) { - return new Buffer(value.data); - } - } - - throw new TypeError('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.'); -} -exports.allocUnsafeSlow = function allocUnsafeSlow(size) { - if (typeof Buffer.allocUnsafeSlow === 'function') { - return Buffer.allocUnsafeSlow(size); - } - if (typeof size !== 'number') { - throw new TypeError('size must be a number'); - } - if (size >= MAX_LEN) { - throw new RangeError('size is too large'); - } - return new SlowBuffer(size); -} - -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{"buffer":33}],11:[function(require,module,exports){ -(function (Buffer){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -// NOTE: These type checking functions intentionally don't use `instanceof` -// because it is fragile and can be easily faked with `Object.create()`. - -function isArray(arg) { - if (Array.isArray) { - return Array.isArray(arg); - } - return objectToString(arg) === '[object Array]'; -} -exports.isArray = isArray; - -function isBoolean(arg) { - return typeof arg === 'boolean'; -} -exports.isBoolean = isBoolean; - -function isNull(arg) { - return arg === null; -} -exports.isNull = isNull; - -function isNullOrUndefined(arg) { - return arg == null; -} -exports.isNullOrUndefined = isNullOrUndefined; - -function isNumber(arg) { - return typeof arg === 'number'; -} -exports.isNumber = isNumber; - -function isString(arg) { - return typeof arg === 'string'; -} -exports.isString = isString; - -function isSymbol(arg) { - return typeof arg === 'symbol'; -} -exports.isSymbol = isSymbol; - -function isUndefined(arg) { - return arg === void 0; -} -exports.isUndefined = isUndefined; - -function isRegExp(re) { - return objectToString(re) === '[object RegExp]'; -} -exports.isRegExp = isRegExp; - -function isObject(arg) { - return typeof arg === 'object' && arg !== null; -} -exports.isObject = isObject; - -function isDate(d) { - return objectToString(d) === '[object Date]'; -} -exports.isDate = isDate; - -function isError(e) { - return (objectToString(e) === '[object Error]' || e instanceof Error); -} -exports.isError = isError; - -function isFunction(arg) { - return typeof arg === 'function'; -} -exports.isFunction = isFunction; - -function isPrimitive(arg) { - return arg === null || - typeof arg === 'boolean' || - typeof arg === 'number' || - typeof arg === 'string' || - typeof arg === 'symbol' || // ES6 symbol - typeof arg === 'undefined'; -} -exports.isPrimitive = isPrimitive; - -exports.isBuffer = Buffer.isBuffer; - -function objectToString(o) { - return Object.prototype.toString.call(o); -} - -}).call(this,{"isBuffer":require("../../../../../../browserify/node_modules/insert-module-globals/node_modules/is-buffer/index.js")}) -},{"../../../../../../browserify/node_modules/insert-module-globals/node_modules/is-buffer/index.js":39}],12:[function(require,module,exports){ -if (typeof Object.create === 'function') { - // implementation from standard node.js 'util' module - module.exports = function inherits(ctor, superCtor) { - ctor.super_ = superCtor - ctor.prototype = Object.create(superCtor.prototype, { - constructor: { - value: ctor, - enumerable: false, - writable: true, - configurable: true - } - }); - }; -} else { - // old school shim for old browsers - module.exports = function inherits(ctor, superCtor) { - ctor.super_ = superCtor - var TempCtor = function () {} - TempCtor.prototype = superCtor.prototype - ctor.prototype = new TempCtor() - ctor.prototype.constructor = ctor - } -} - -},{}],13:[function(require,module,exports){ -var toString = {}.toString; - -module.exports = Array.isArray || function (arr) { - return toString.call(arr) == '[object Array]'; -}; - -},{}],14:[function(require,module,exports){ -(function (process){ -'use strict'; - -if (!process.version || - process.version.indexOf('v0.') === 0 || - process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { - module.exports = nextTick; -} else { - module.exports = process.nextTick; -} - -function nextTick(fn, arg1, arg2, arg3) { - if (typeof fn !== 'function') { - throw new TypeError('"callback" argument must be a function'); - } - var len = arguments.length; - var args, i; - switch (len) { - case 0: - case 1: - return process.nextTick(fn); - case 2: - return process.nextTick(function afterTickOne() { - fn.call(null, arg1); - }); - case 3: - return process.nextTick(function afterTickTwo() { - fn.call(null, arg1, arg2); - }); - case 4: - return process.nextTick(function afterTickThree() { - fn.call(null, arg1, arg2, arg3); - }); - default: - args = new Array(len - 1); - i = 0; - while (i < args.length) { - args[i++] = arguments[i]; - } - return process.nextTick(function afterTick() { - fn.apply(null, args); - }); - } -} - -}).call(this,require('_process')) -},{"_process":41}],15:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -var Buffer = require('buffer').Buffer; - -var isBufferEncoding = Buffer.isEncoding - || function(encoding) { - switch (encoding && encoding.toLowerCase()) { - case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': case 'raw': return true; - default: return false; - } - } - - -function assertEncoding(encoding) { - if (encoding && !isBufferEncoding(encoding)) { - throw new Error('Unknown encoding: ' + encoding); - } -} - -// StringDecoder provides an interface for efficiently splitting a series of -// buffers into a series of JS strings without breaking apart multi-byte -// characters. CESU-8 is handled as part of the UTF-8 encoding. -// -// @TODO Handling all encodings inside a single object makes it very difficult -// to reason about this code, so it should be split up in the future. -// @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code -// points as used by CESU-8. -var StringDecoder = exports.StringDecoder = function(encoding) { - this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); - assertEncoding(encoding); - switch (this.encoding) { - case 'utf8': - // CESU-8 represents each of Surrogate Pair by 3-bytes - this.surrogateSize = 3; - break; - case 'ucs2': - case 'utf16le': - // UTF-16 represents each of Surrogate Pair by 2-bytes - this.surrogateSize = 2; - this.detectIncompleteChar = utf16DetectIncompleteChar; - break; - case 'base64': - // Base-64 stores 3 bytes in 4 chars, and pads the remainder. - this.surrogateSize = 3; - this.detectIncompleteChar = base64DetectIncompleteChar; - break; - default: - this.write = passThroughWrite; - return; - } - - // Enough space to store all bytes of a single character. UTF-8 needs 4 - // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate). - this.charBuffer = new Buffer(6); - // Number of bytes received for the current incomplete multi-byte character. - this.charReceived = 0; - // Number of bytes expected for the current incomplete multi-byte character. - this.charLength = 0; -}; - - -// write decodes the given buffer and returns it as JS string that is -// guaranteed to not contain any partial multi-byte characters. Any partial -// character found at the end of the buffer is buffered up, and will be -// returned when calling write again with the remaining bytes. -// -// Note: Converting a Buffer containing an orphan surrogate to a String -// currently works, but converting a String to a Buffer (via `new Buffer`, or -// Buffer#write) will replace incomplete surrogates with the unicode -// replacement character. See https://codereview.chromium.org/121173009/ . -StringDecoder.prototype.write = function(buffer) { - var charStr = ''; - // if our last write ended with an incomplete multibyte character - while (this.charLength) { - // determine how many remaining bytes this buffer has to offer for this char - var available = (buffer.length >= this.charLength - this.charReceived) ? - this.charLength - this.charReceived : - buffer.length; - - // add the new bytes to the char buffer - buffer.copy(this.charBuffer, this.charReceived, 0, available); - this.charReceived += available; - - if (this.charReceived < this.charLength) { - // still not enough chars in this buffer? wait for more ... - return ''; + function copy1D (arr) { + var carr = []; + for (var i = 0; i < arr.length; ++i) { + carr[i] = arr[i]; } - // remove bytes belonging to the current character from the buffer - buffer = buffer.slice(available, buffer.length); - - // get the character that was split - charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding); - - // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character - var charCode = charStr.charCodeAt(charStr.length - 1); - if (charCode >= 0xD800 && charCode <= 0xDBFF) { - this.charLength += this.surrogateSize; - charStr = ''; - continue; - } - this.charReceived = this.charLength = 0; - - // if there are no more bytes in this buffer, just emit our char - if (buffer.length === 0) { - return charStr; - } - break; + return carr; } - // determine and set charLength / charReceived - this.detectIncompleteChar(buffer); - var end = buffer.length; - if (this.charLength) { - // buffer the incomplete character bytes we got - buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end); - end -= this.charReceived; - } - - charStr += buffer.toString(this.encoding, 0, end); - - var end = charStr.length - 1; - var charCode = charStr.charCodeAt(end); - // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character - if (charCode >= 0xD800 && charCode <= 0xDBFF) { - var size = this.surrogateSize; - this.charLength += size; - this.charReceived += size; - this.charBuffer.copy(this.charBuffer, size, 0, size); - buffer.copy(this.charBuffer, 0, 0, size); - return charStr.substring(0, end); - } - - // or just emit the charStr - return charStr; -}; - -// detectIncompleteChar determines if there is an incomplete UTF-8 character at -// the end of the given buffer. If so, it sets this.charLength to the byte -// length that character, and sets this.charReceived to the number of bytes -// that are available for this character. -StringDecoder.prototype.detectIncompleteChar = function(buffer) { - // determine how many bytes we have to check at the end of this buffer - var i = (buffer.length >= 3) ? 3 : buffer.length; - - // Figure out if one of the last i bytes of our buffer announces an - // incomplete char. - for (; i > 0; i--) { - var c = buffer[buffer.length - i]; - - // See http://en.wikipedia.org/wiki/UTF-8#Description - - // 110XXXXX - if (i == 1 && c >> 5 == 0x06) { - this.charLength = 2; - break; + function isEqual(arr1, arr2) { + if(arr1.length !== arr2.length) + return false; + for(var i = arr1.length; i--;) { + if(arr1[i] !== arr2[i]) + return false; } - // 1110XXXX - if (i <= 2 && c >> 4 == 0x0E) { - this.charLength = 3; - break; + return true; + } + + + function str2RgbArray(str, twoFiftySix) { + // convert hex or rbg strings to 0->1 or 0->255 rgb array + var rgb, + match; + + if (typeof str !== 'string') return str; + + rgb = []; + // hex notation + if (str[0] === '#') { + str = str.substr(1) // remove hash + if (str.length === 3) str += str // fff -> ffffff + match = parseInt(str, 16); + rgb[0] = ((match >> 16) & 255); + rgb[1] = ((match >> 8) & 255); + rgb[2] = (match & 255); } - // 11110XXX - if (i <= 3 && c >> 3 == 0x1E) { - this.charLength = 4; - break; + // rgb(34, 34, 127) or rgba(34, 34, 127, 0.1) notation + else if (RGB_REGEX.test(str)) { + match = str.match(RGB_GROUP_REGEX); + rgb[0] = parseInt(match[1]); + rgb[1] = parseInt(match[2]); + rgb[2] = parseInt(match[3]); } - } - this.charReceived = i; -}; -StringDecoder.prototype.end = function(buffer) { - var res = ''; - if (buffer && buffer.length) - res = this.write(buffer); - - if (this.charReceived) { - var cr = this.charReceived; - var buf = this.charBuffer; - var enc = this.encoding; - res += buf.slice(0, cr).toString(enc); - } - - return res; -}; - -function passThroughWrite(buffer) { - return buffer.toString(this.encoding); -} - -function utf16DetectIncompleteChar(buffer) { - this.charReceived = buffer.length % 2; - this.charLength = this.charReceived ? 2 : 0; -} - -function base64DetectIncompleteChar(buffer) { - this.charReceived = buffer.length % 3; - this.charLength = this.charReceived ? 3 : 0; -} - -},{"buffer":33}],16:[function(require,module,exports){ -(function (global){ - -/** - * Module exports. - */ - -module.exports = deprecate; - -/** - * Mark that a method should not be used. - * Returns a modified function which warns once by default. - * - * If `localStorage.noDeprecation = true` is set, then it is a no-op. - * - * If `localStorage.throwDeprecation = true` is set, then deprecated functions - * will throw an Error when invoked. - * - * If `localStorage.traceDeprecation = true` is set, then deprecated functions - * will invoke `console.trace()` instead of `console.error()`. - * - * @param {Function} fn - the function to deprecate - * @param {String} msg - the string to print to the console when `fn` is invoked - * @returns {Function} a new "deprecated" version of `fn` - * @api public - */ - -function deprecate (fn, msg) { - if (config('noDeprecation')) { - return fn; - } - - var warned = false; - function deprecated() { - if (!warned) { - if (config('throwDeprecation')) { - throw new Error(msg); - } else if (config('traceDeprecation')) { - console.trace(msg); - } else { - console.warn(msg); - } - warned = true; + if (!twoFiftySix) { + for (var j=0; j<3; ++j) rgb[j] = rgb[j]/255 } - return fn.apply(this, arguments); + + + return rgb; } - return deprecated; -} -/** - * Checks `localStorage` for boolean values for the given `name`. - * - * @param {String} name - * @returns {Boolean} - * @api private - */ + function str2RgbaArray(str, twoFiftySix) { + // convert hex or rbg strings to 0->1 or 0->255 rgb array + var rgb, + match; -function config (name) { - // accessing global.localStorage can trigger a DOMException in sandboxed iframes - try { - if (!global.localStorage) return false; - } catch (_) { - return false; + if (typeof str !== 'string') return str; + + rgb = []; + // hex notation + if (str[0] === '#') { + str = str.substr(1) // remove hash + if (str.length === 3) str += str // fff -> ffffff + match = parseInt(str, 16); + rgb[0] = ((match >> 16) & 255); + rgb[1] = ((match >> 8) & 255); + rgb[2] = (match & 255); + } + + // rgb(34, 34, 127) or rgba(34, 34, 127, 0.1) notation + else if (RGB_REGEX.test(str)) { + match = str.match(RGB_GROUP_REGEX); + rgb[0] = parseInt(match[1]); + rgb[1] = parseInt(match[2]); + rgb[2] = parseInt(match[3]); + if (match[4]) rgb[3] = parseFloat(match[4]); + else rgb[3] = 1.0; + } + + + + if (!twoFiftySix) { + for (var j=0; j<3; ++j) rgb[j] = rgb[j]/255 + } + + + return rgb; } - var val = global.localStorage[name]; - if (null == val) return false; - return String(val).toLowerCase() === 'true'; + + + + + + that.isPlainObject = isPlainObject; + that.linspace = linspace; + that.zip3 = zip3; + that.sum = sum; + that.zip = zip; + that.isEqual = isEqual; + that.copy2D = copy2D; + that.copy1D = copy1D; + that.str2RgbArray = str2RgbArray; + that.str2RgbaArray = str2RgbaArray; + + return that + } -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{}],17:[function(require,module,exports){ -(function (process){ -var Stream = (function (){ - try { - return require('st' + 'ream'); // hack to fix a circular dependency issue when used with browserify - } catch(_){} -}()); -exports = module.exports = require('./lib/_stream_readable.js'); -exports.Stream = Stream || exports; -exports.Readable = exports; -exports.Writable = require('./lib/_stream_writable.js'); -exports.Duplex = require('./lib/_stream_duplex.js'); -exports.Transform = require('./lib/_stream_transform.js'); -exports.PassThrough = require('./lib/_stream_passthrough.js'); -if (!process.browser && process.env.READABLE_STREAM === 'disable' && Stream) { - module.exports = Stream; -} +module.exports = arraytools(); -}).call(this,require('_process')) -},{"./lib/_stream_duplex.js":4,"./lib/_stream_passthrough.js":5,"./lib/_stream_readable.js":6,"./lib/_stream_transform.js":7,"./lib/_stream_writable.js":8,"_process":41}],18:[function(require,module,exports){ +},{}],9:[function(require,module,exports){ // http://wiki.commonjs.org/wiki/Unit_Testing/1.0 // // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! @@ -3005,5546 +1027,6007 @@ var objectKeys = Object.keys || function (obj) { return keys; }; -},{"util/":68}],19:[function(require,module,exports){ +},{"util/":1001}],10:[function(require,module,exports){ +module.exports = function _atob(str) { + return atob(str) +} -},{}],20:[function(require,module,exports){ -'use strict'; +},{}],11:[function(require,module,exports){ +'use strict' +module.exports = barycentric -var TYPED_OK = (typeof Uint8Array !== 'undefined') && - (typeof Uint16Array !== 'undefined') && - (typeof Int32Array !== 'undefined'); +var solve = require('robust-linear-solve') +function reduce(x) { + var r = 0 + for(var i=0; i 0) { + throw new Error('Invalid string. Length must be a multiple of 4') + } + + // the number of equal signs (place holders) + // if there are two placeholders, than the two characters before it + // represent one byte + // if there is only one, then the three characters before it represent 2 bytes + // this is just a cheap hack to not do indexOf twice + return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 +} + +function byteLength (b64) { + // base64 is 4/3 + up to two characters of the original data + return b64.length * 3 / 4 - placeHoldersCount(b64) +} + +function toByteArray (b64) { + var i, j, l, tmp, placeHolders, arr + var len = b64.length + placeHolders = placeHoldersCount(b64) + + arr = new Arr(len * 3 / 4 - placeHolders) + + // if there are placeholders, only get up to the last complete 4 chars + l = placeHolders > 0 ? len - 4 : len + + var L = 0 + + for (i = 0, j = 0; i < l; i += 4, j += 3) { + tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] + arr[L++] = (tmp >> 16) & 0xFF + arr[L++] = (tmp >> 8) & 0xFF + arr[L++] = tmp & 0xFF + } + + if (placeHolders === 2) { + tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) + arr[L++] = tmp & 0xFF + } else if (placeHolders === 1) { + tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) + arr[L++] = (tmp >> 8) & 0xFF + arr[L++] = tmp & 0xFF + } + + return arr +} + +function tripletToBase64 (num) { + return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] +} + +function encodeChunk (uint8, start, end) { + var tmp + var output = [] + for (var i = start; i < end; i += 3) { + tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) + output.push(tripletToBase64(tmp)) + } + return output.join('') +} + +function fromByteArray (uint8) { + var tmp + var len = uint8.length + var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes + var output = '' + var parts = [] + var maxChunkLength = 16383 // must be multiple of 3 + + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) + } + + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1] + output += lookup[tmp >> 2] + output += lookup[(tmp << 4) & 0x3F] + output += '==' + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) + output += lookup[tmp >> 10] + output += lookup[(tmp >> 4) & 0x3F] + output += lookup[(tmp << 2) & 0x3F] + output += '=' + } + + parts.push(output) + + return parts.join('') +} + +},{}],13:[function(require,module,exports){ +var Base64Encode = require('./lib/encode'); +var Base64Decode = require('./lib/decode'); + +module.exports = { + Encode : Base64Encode, + encode : Base64Encode, + Decode : Base64Decode, + decode : Base64Decode }; +},{"./lib/decode":14,"./lib/encode":15}],14:[function(require,module,exports){ +(function (Buffer){ +module.exports = Base64Decode; -// reduce buffer size, avoiding mem copy -exports.shrinkBuf = function (buf, size) { - if (buf.length === size) { return buf; } - if (buf.subarray) { return buf.subarray(0, size); } - buf.length = size; - return buf; +var Transform = require('stream').Transform || require('readable-stream').Transform; +require('util').inherits(Base64Decode, Transform); + +/** + * Decodes a Base64 data stream, coming in as a string or Buffer of UTF-8 text, into binary Buffers. + * @returns {Base64Decode} + * @constructor + */ +function Base64Decode() { + if ( !(this instanceof Base64Decode) ) + return new Base64Decode(); + + Transform.call(this, { + // The input is converted to strings, so no need to transform input strings to buffers + decodeStrings : false + }); + + // Any extra chars from the last chunk + this.extra = ''; +} + +/** + * Decodes a Base64 data stream, coming in as a string or Buffer of UTF-8 text, into binary Buffers. + * @param {Buffer|string} chunk + * @param encoding + * @param cb + * @private + */ +Base64Decode.prototype._transform = function (chunk, encoding, cb) { + // Convert chunk to a string + chunk = '' + chunk; + + // Add previous extra and remove any newline characters + chunk = this.extra + chunk.replace(/(\r\n|\n|\r)/gm, ''); + + // 4 characters represent 3 bytes, so we can only decode in groups of 4 chars + var remaining = chunk.length % 4; + + // Store the extra chars for later + this.extra = chunk.slice(chunk.length - remaining); + chunk = chunk.slice(0, chunk.length - remaining); + + // Create the new buffer and push + var buf = new Buffer(chunk, 'base64'); + this.push(buf); + cb(); }; +/** + * Emits 1, 2, or 3 extra characters of base64 data. + * @param cb + * @private + */ +Base64Decode.prototype._flush = function (cb) { + if ( this.extra.length ) + this.push(new Buffer(this.extra, 'base64')); -var fnTyped = { - arraySet: function (dest, src, src_offs, len, dest_offs) { - if (src.subarray && dest.subarray) { - dest.set(src.subarray(src_offs, src_offs + len), dest_offs); + cb(); +}; + +}).call(this,require("buffer").Buffer) +},{"buffer":46,"readable-stream":942,"stream":978,"util":1001}],15:[function(require,module,exports){ +(function (Buffer){ +module.exports = Base64Encode; + +var Transform = require('stream').Transform || require('readable-stream').Transform; +require('util').inherits(Base64Encode, Transform); + +/** + * Transforms a Buffer stream of binary data to a stream of Base64 text. Note that this will + * also work on a stream of pure strings, as the Writeable base class will automatically decode + * text string chunks into Buffers. + * @returns {Base64Encode} + * @constructor + */ +function Base64Encode(lineLength) { + if ( !(this instanceof Base64Encode) ) + return new Base64Encode(lineLength); + + Transform.call(this); + + // Any extra chars from the last chunk + this.extra = null; + + this.lineLength = lineLength; + + this.currLineLength = 0; +} + +/** + * Adds \r\n as needed to the data chunk to ensure that the output Base64 string meets + * the maximum line length requirement. + * @param {string} chunk + * @returns {string} + * @private + */ +Base64Encode.prototype._fixLineLength = function (chunk) { + // If we care about line length, add line breaks + if ( !this.lineLength ) + return chunk; + + var size = chunk.length; + var needed = this.lineLength - this.currLineLength; + var start, end; + + var _chunk = ''; + for ( start = 0, end = needed; end < size; start = end, end += this.lineLength ) { + _chunk += chunk.slice(start, end); + _chunk += '\r\n'; + } + + var left = chunk.slice(start); + this.currLineLength = left.length; + + _chunk += left; + + return _chunk; +}; + +/** + * Transforms a Buffer chunk of data to a Base64 string chunk. + * @param {Buffer} chunk + * @param {string} encoding - unused since chunk is always a Buffer + * @param cb + * @private + */ +Base64Encode.prototype._transform = function (chunk, encoding, cb) { + // Add any previous extra bytes to the chunk + if ( this.extra ) { + chunk = Buffer.concat([this.extra, chunk]); + this.extra = null; + } + + // 3 bytes are represented by 4 characters, so we can only encode in groups of 3 bytes + var remaining = chunk.length % 3; + + if ( remaining !== 0 ) { + // Store the extra bytes for later + this.extra = chunk.slice(chunk.length - remaining); + chunk = chunk.slice(0, chunk.length - remaining); + } + + // Convert chunk to a base 64 string + chunk = chunk.toString('base64'); + + // Push the chunk + this.push(this._fixLineLength(chunk)); + cb(); +}; + +/** + * Emits 0 or 4 extra characters of Base64 data. + * @param cb + * @private + */ +Base64Encode.prototype._flush = function (cb) { + if ( this.extra ) + this.push(this._fixLineLength(this.extra.toString('base64'))); + + cb(); +}; + +}).call(this,require("buffer").Buffer) +},{"buffer":46,"readable-stream":942,"stream":978,"util":1001}],16:[function(require,module,exports){ +'use strict' + +var rationalize = require('./lib/rationalize') + +module.exports = add + +function add(a, b) { + return rationalize( + a[0].mul(b[1]).add(b[0].mul(a[1])), + a[1].mul(b[1])) +} + +},{"./lib/rationalize":26}],17:[function(require,module,exports){ +'use strict' + +module.exports = cmp + +function cmp(a, b) { + return a[0].mul(b[1]).cmp(b[0].mul(a[1])) +} + +},{}],18:[function(require,module,exports){ +'use strict' + +var rationalize = require('./lib/rationalize') + +module.exports = div + +function div(a, b) { + return rationalize(a[0].mul(b[1]), a[1].mul(b[0])) +} + +},{"./lib/rationalize":26}],19:[function(require,module,exports){ +'use strict' + +var isRat = require('./is-rat') +var isBN = require('./lib/is-bn') +var num2bn = require('./lib/num-to-bn') +var str2bn = require('./lib/str-to-bn') +var rationalize = require('./lib/rationalize') +var div = require('./div') + +module.exports = makeRational + +function makeRational(numer, denom) { + if(isRat(numer)) { + if(denom) { + return div(numer, makeRational(denom)) + } + return [numer[0].clone(), numer[1].clone()] + } + var shift = 0 + var a, b + if(isBN(numer)) { + a = numer.clone() + } else if(typeof numer === 'string') { + a = str2bn(numer) + } else if(numer === 0) { + return [num2bn(0), num2bn(1)] + } else if(numer === Math.floor(numer)) { + a = num2bn(numer) + } else { + while(numer !== Math.floor(numer)) { + numer = numer * Math.pow(2, 256) + shift -= 256 + } + a = num2bn(numer) + } + if(isRat(denom)) { + a.mul(denom[1]) + b = denom[0].clone() + } else if(isBN(denom)) { + b = denom.clone() + } else if(typeof denom === 'string') { + b = str2bn(denom) + } else if(!denom) { + b = num2bn(1) + } else if(denom === Math.floor(denom)) { + b = num2bn(denom) + } else { + while(denom !== Math.floor(denom)) { + denom = denom * Math.pow(2, 256) + shift += 256 + } + b = num2bn(denom) + } + if(shift > 0) { + a = a.ushln(shift) + } else if(shift < 0) { + b = b.ushln(-shift) + } + return rationalize(a, b) +} + +},{"./div":18,"./is-rat":20,"./lib/is-bn":24,"./lib/num-to-bn":25,"./lib/rationalize":26,"./lib/str-to-bn":27}],20:[function(require,module,exports){ +'use strict' + +var isBN = require('./lib/is-bn') + +module.exports = isRat + +function isRat(x) { + return Array.isArray(x) && x.length === 2 && isBN(x[0]) && isBN(x[1]) +} + +},{"./lib/is-bn":24}],21:[function(require,module,exports){ +'use strict' + +var BN = require('bn.js') + +module.exports = sign + +function sign (x) { + return x.cmp(new BN(0)) +} + +},{"bn.js":34}],22:[function(require,module,exports){ +'use strict' + +const sign = require('./bn-sign') + +module.exports = bn2num + +//TODO: Make this better +function bn2num(b) { + var l = b.length + var words = b.words + var out = 0 + if (l === 1) { + out = words[0] + } else if (l === 2) { + out = words[0] + (words[1] * 0x4000000) + } else { + for (var i = 0; i < l; i++) { + var w = words[i] + out += w * Math.pow(0x4000000, i) + } + } + return sign(b) * out +} + +},{"./bn-sign":21}],23:[function(require,module,exports){ +'use strict' + +var db = require('double-bits') +var ctz = require('bit-twiddle').countTrailingZeros + +module.exports = ctzNumber + +//Counts the number of trailing zeros +function ctzNumber(x) { + var l = ctz(db.lo(x)) + if(l < 32) { + return l + } + var h = ctz(db.hi(x)) + if(h > 20) { + return 52 + } + return h + 32 +} + +},{"bit-twiddle":33,"double-bits":89}],24:[function(require,module,exports){ +'use strict' + +var BN = require('bn.js') + +module.exports = isBN + +//Test if x is a bignumber +//FIXME: obviously this is the wrong way to do it +function isBN(x) { + return x && typeof x === 'object' && Boolean(x.words) +} + +},{"bn.js":34}],25:[function(require,module,exports){ +'use strict' + +var BN = require('bn.js') +var db = require('double-bits') + +module.exports = num2bn + +function num2bn(x) { + var e = db.exponent(x) + if(e < 52) { + return new BN(x) + } else { + return (new BN(x * Math.pow(2, 52-e))).ushln(e-52) + } +} + +},{"bn.js":34,"double-bits":89}],26:[function(require,module,exports){ +'use strict' + +var num2bn = require('./num-to-bn') +var sign = require('./bn-sign') + +module.exports = rationalize + +function rationalize(numer, denom) { + var snumer = sign(numer) + var sdenom = sign(denom) + if(snumer === 0) { + return [num2bn(0), num2bn(1)] + } + if(sdenom === 0) { + return [num2bn(0), num2bn(0)] + } + if(sdenom < 0) { + numer = numer.neg() + denom = denom.neg() + } + var d = numer.gcd(denom) + if(d.cmpn(1)) { + return [ numer.div(d), denom.div(d) ] + } + return [ numer, denom ] +} + +},{"./bn-sign":21,"./num-to-bn":25}],27:[function(require,module,exports){ +'use strict' + +var BN = require('bn.js') + +module.exports = str2BN + +function str2BN(x) { + return new BN(x) +} + +},{"bn.js":34}],28:[function(require,module,exports){ +'use strict' + +var rationalize = require('./lib/rationalize') + +module.exports = mul + +function mul(a, b) { + return rationalize(a[0].mul(b[0]), a[1].mul(b[1])) +} + +},{"./lib/rationalize":26}],29:[function(require,module,exports){ +'use strict' + +var bnsign = require('./lib/bn-sign') + +module.exports = sign + +function sign(x) { + return bnsign(x[0]) * bnsign(x[1]) +} + +},{"./lib/bn-sign":21}],30:[function(require,module,exports){ +'use strict' + +var rationalize = require('./lib/rationalize') + +module.exports = sub + +function sub(a, b) { + return rationalize(a[0].mul(b[1]).sub(a[1].mul(b[0])), a[1].mul(b[1])) +} + +},{"./lib/rationalize":26}],31:[function(require,module,exports){ +'use strict' + +var bn2num = require('./lib/bn-to-num') +var ctz = require('./lib/ctz') + +module.exports = roundRat + +// Round a rational to the closest float +function roundRat (f) { + var a = f[0] + var b = f[1] + if (a.cmpn(0) === 0) { + return 0 + } + var h = a.abs().divmod(b.abs()) + var iv = h.div + var x = bn2num(iv) + var ir = h.mod + var sgn = (a.negative !== b.negative) ? -1 : 1 + if (ir.cmpn(0) === 0) { + return sgn * x + } + if (x) { + var s = ctz(x) + 4 + var y = bn2num(ir.ushln(s).divRound(b)) + return sgn * (x + y * Math.pow(2, -s)) + } else { + var ybits = b.bitLength() - ir.bitLength() + 53 + var y = bn2num(ir.ushln(ybits).divRound(b)) + if (ybits < 1023) { + return sgn * y * Math.pow(2, -ybits) + } + y *= Math.pow(2, -1023) + return sgn * y * Math.pow(2, 1023 - ybits) + } +} + +},{"./lib/bn-to-num":22,"./lib/ctz":23}],32:[function(require,module,exports){ +"use strict" + +function compileSearch(funcName, predicate, reversed, extraArgs, useNdarray, earlyOut) { + var code = [ + "function ", funcName, "(a,l,h,", extraArgs.join(","), "){", +earlyOut ? "" : "var i=", (reversed ? "l-1" : "h+1"), +";while(l<=h){\ +var m=(l+h)>>>1,x=a", useNdarray ? ".get(m)" : "[m]"] + if(earlyOut) { + if(predicate.indexOf("c") < 0) { + code.push(";if(x===y){return m}else if(x<=y){") + } else { + code.push(";var p=c(x,y);if(p===0){return m}else if(p<=0){") + } + } else { + code.push(";if(", predicate, "){i=m;") + } + if(reversed) { + code.push("l=m+1}else{h=m-1}") + } else { + code.push("h=m-1}else{l=m+1}") + } + code.push("}") + if(earlyOut) { + code.push("return -1};") + } else { + code.push("return i};") + } + return code.join("") +} + +function compileBoundsSearch(predicate, reversed, suffix, earlyOut) { + var result = new Function([ + compileSearch("A", "x" + predicate + "y", reversed, ["y"], false, earlyOut), + compileSearch("B", "x" + predicate + "y", reversed, ["y"], true, earlyOut), + compileSearch("P", "c(x,y)" + predicate + "0", reversed, ["y", "c"], false, earlyOut), + compileSearch("Q", "c(x,y)" + predicate + "0", reversed, ["y", "c"], true, earlyOut), +"function dispatchBsearch", suffix, "(a,y,c,l,h){\ +if(a.shape){\ +if(typeof(c)==='function'){\ +return Q(a,(l===undefined)?0:l|0,(h===undefined)?a.shape[0]-1:h|0,y,c)\ +}else{\ +return B(a,(c===undefined)?0:c|0,(l===undefined)?a.shape[0]-1:l|0,y)\ +}}else{\ +if(typeof(c)==='function'){\ +return P(a,(l===undefined)?0:l|0,(h===undefined)?a.length-1:h|0,y,c)\ +}else{\ +return A(a,(c===undefined)?0:c|0,(l===undefined)?a.length-1:l|0,y)\ +}}}\ +return dispatchBsearch", suffix].join("")) + return result() +} + +module.exports = { + ge: compileBoundsSearch(">=", false, "GE"), + gt: compileBoundsSearch(">", false, "GT"), + lt: compileBoundsSearch("<", true, "LT"), + le: compileBoundsSearch("<=", true, "LE"), + eq: compileBoundsSearch("-", true, "EQ", true) +} + +},{}],33:[function(require,module,exports){ +/** + * Bit twiddling hacks for JavaScript. + * + * Author: Mikola Lysenko + * + * Ported from Stanford bit twiddling hack library: + * http://graphics.stanford.edu/~seander/bithacks.html + */ + +"use strict"; "use restrict"; + +//Number of bits in an integer +var INT_BITS = 32; + +//Constants +exports.INT_BITS = INT_BITS; +exports.INT_MAX = 0x7fffffff; +exports.INT_MIN = -1<<(INT_BITS-1); + +//Returns -1, 0, +1 depending on sign of x +exports.sign = function(v) { + return (v > 0) - (v < 0); +} + +//Computes absolute value of integer +exports.abs = function(v) { + var mask = v >> (INT_BITS-1); + return (v ^ mask) - mask; +} + +//Computes minimum of integers x and y +exports.min = function(x, y) { + return y ^ ((x ^ y) & -(x < y)); +} + +//Computes maximum of integers x and y +exports.max = function(x, y) { + return x ^ ((x ^ y) & -(x < y)); +} + +//Checks if a number is a power of two +exports.isPow2 = function(v) { + return !(v & (v-1)) && (!!v); +} + +//Computes log base 2 of v +exports.log2 = function(v) { + var r, shift; + r = (v > 0xFFFF) << 4; v >>>= r; + shift = (v > 0xFF ) << 3; v >>>= shift; r |= shift; + shift = (v > 0xF ) << 2; v >>>= shift; r |= shift; + shift = (v > 0x3 ) << 1; v >>>= shift; r |= shift; + return r | (v >> 1); +} + +//Computes log base 10 of v +exports.log10 = function(v) { + return (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 : + (v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 : + (v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0; +} + +//Counts number of bits +exports.popCount = function(v) { + v = v - ((v >>> 1) & 0x55555555); + v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); + return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; +} + +//Counts number of trailing zeros +function countTrailingZeros(v) { + var c = 32; + v &= -v; + if (v) c--; + if (v & 0x0000FFFF) c -= 16; + if (v & 0x00FF00FF) c -= 8; + if (v & 0x0F0F0F0F) c -= 4; + if (v & 0x33333333) c -= 2; + if (v & 0x55555555) c -= 1; + return c; +} +exports.countTrailingZeros = countTrailingZeros; + +//Rounds to next power of 2 +exports.nextPow2 = function(v) { + v += v === 0; + --v; + v |= v >>> 1; + v |= v >>> 2; + v |= v >>> 4; + v |= v >>> 8; + v |= v >>> 16; + return v + 1; +} + +//Rounds down to previous power of 2 +exports.prevPow2 = function(v) { + v |= v >>> 1; + v |= v >>> 2; + v |= v >>> 4; + v |= v >>> 8; + v |= v >>> 16; + return v - (v>>>1); +} + +//Computes parity of word +exports.parity = function(v) { + v ^= v >>> 16; + v ^= v >>> 8; + v ^= v >>> 4; + v &= 0xf; + return (0x6996 >>> v) & 1; +} + +var REVERSE_TABLE = new Array(256); + +(function(tab) { + for(var i=0; i<256; ++i) { + var v = i, r = i, s = 7; + for (v >>>= 1; v; v >>>= 1) { + r <<= 1; + r |= v & 1; + --s; + } + tab[i] = (r << s) & 0xff; + } +})(REVERSE_TABLE); + +//Reverse bits in a 32 bit word +exports.reverse = function(v) { + return (REVERSE_TABLE[ v & 0xff] << 24) | + (REVERSE_TABLE[(v >>> 8) & 0xff] << 16) | + (REVERSE_TABLE[(v >>> 16) & 0xff] << 8) | + REVERSE_TABLE[(v >>> 24) & 0xff]; +} + +//Interleave bits of 2 coordinates with 16 bits. Useful for fast quadtree codes +exports.interleave2 = function(x, y) { + x &= 0xFFFF; + x = (x | (x << 8)) & 0x00FF00FF; + x = (x | (x << 4)) & 0x0F0F0F0F; + x = (x | (x << 2)) & 0x33333333; + x = (x | (x << 1)) & 0x55555555; + + y &= 0xFFFF; + y = (y | (y << 8)) & 0x00FF00FF; + y = (y | (y << 4)) & 0x0F0F0F0F; + y = (y | (y << 2)) & 0x33333333; + y = (y | (y << 1)) & 0x55555555; + + return x | (y << 1); +} + +//Extracts the nth interleaved component +exports.deinterleave2 = function(v, n) { + v = (v >>> n) & 0x55555555; + v = (v | (v >>> 1)) & 0x33333333; + v = (v | (v >>> 2)) & 0x0F0F0F0F; + v = (v | (v >>> 4)) & 0x00FF00FF; + v = (v | (v >>> 16)) & 0x000FFFF; + return (v << 16) >> 16; +} + + +//Interleave bits of 3 coordinates, each with 10 bits. Useful for fast octree codes +exports.interleave3 = function(x, y, z) { + x &= 0x3FF; + x = (x | (x<<16)) & 4278190335; + x = (x | (x<<8)) & 251719695; + x = (x | (x<<4)) & 3272356035; + x = (x | (x<<2)) & 1227133513; + + y &= 0x3FF; + y = (y | (y<<16)) & 4278190335; + y = (y | (y<<8)) & 251719695; + y = (y | (y<<4)) & 3272356035; + y = (y | (y<<2)) & 1227133513; + x |= (y << 1); + + z &= 0x3FF; + z = (z | (z<<16)) & 4278190335; + z = (z | (z<<8)) & 251719695; + z = (z | (z<<4)) & 3272356035; + z = (z | (z<<2)) & 1227133513; + + return x | (z << 2); +} + +//Extracts nth interleaved component of a 3-tuple +exports.deinterleave3 = function(v, n) { + v = (v >>> n) & 1227133513; + v = (v | (v>>>2)) & 3272356035; + v = (v | (v>>>4)) & 251719695; + v = (v | (v>>>8)) & 4278190335; + v = (v | (v>>>16)) & 0x3FF; + return (v<<22)>>22; +} + +//Computes next combination in colexicographic order (this is mistakenly called nextPermutation on the bit twiddling hacks page) +exports.nextCombination = function(v) { + var t = v | (v - 1); + return (t + 1) | (((~t & -~t) - 1) >>> (countTrailingZeros(v) + 1)); +} + + +},{}],34:[function(require,module,exports){ +(function (module, exports) { + 'use strict'; + + // Utils + function assert (val, msg) { + if (!val) throw new Error(msg || 'Assertion failed'); + } + + // Could use `inherits` module, but don't want to move from single file + // architecture yet. + function inherits (ctor, superCtor) { + ctor.super_ = superCtor; + var TempCtor = function () {}; + TempCtor.prototype = superCtor.prototype; + ctor.prototype = new TempCtor(); + ctor.prototype.constructor = ctor; + } + + // BN + + function BN (number, base, endian) { + if (BN.isBN(number)) { + return number; + } + + this.negative = 0; + this.words = null; + this.length = 0; + + // Reduction context + this.red = null; + + if (number !== null) { + if (base === 'le' || base === 'be') { + endian = base; + base = 10; + } + + this._init(number || 0, base || 10, endian || 'be'); + } + } + if (typeof module === 'object') { + module.exports = BN; + } else { + exports.BN = BN; + } + + BN.BN = BN; + BN.wordSize = 26; + + var Buffer; + try { + Buffer = require('buf' + 'fer').Buffer; + } catch (e) { + } + + BN.isBN = function isBN (num) { + if (num instanceof BN) { + return true; + } + + return num !== null && typeof num === 'object' && + num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); + }; + + BN.max = function max (left, right) { + if (left.cmp(right) > 0) return left; + return right; + }; + + BN.min = function min (left, right) { + if (left.cmp(right) < 0) return left; + return right; + }; + + BN.prototype._init = function init (number, base, endian) { + if (typeof number === 'number') { + return this._initNumber(number, base, endian); + } + + if (typeof number === 'object') { + return this._initArray(number, base, endian); + } + + if (base === 'hex') { + base = 16; + } + assert(base === (base | 0) && base >= 2 && base <= 36); + + number = number.toString().replace(/\s+/g, ''); + var start = 0; + if (number[0] === '-') { + start++; + } + + if (base === 16) { + this._parseHex(number, start); + } else { + this._parseBase(number, base, start); + } + + if (number[0] === '-') { + this.negative = 1; + } + + this.strip(); + + if (endian !== 'le') return; + + this._initArray(this.toArray(), base, endian); + }; + + BN.prototype._initNumber = function _initNumber (number, base, endian) { + if (number < 0) { + this.negative = 1; + number = -number; + } + if (number < 0x4000000) { + this.words = [ number & 0x3ffffff ]; + this.length = 1; + } else if (number < 0x10000000000000) { + this.words = [ + number & 0x3ffffff, + (number / 0x4000000) & 0x3ffffff + ]; + this.length = 2; + } else { + assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) + this.words = [ + number & 0x3ffffff, + (number / 0x4000000) & 0x3ffffff, + 1 + ]; + this.length = 3; + } + + if (endian !== 'le') return; + + // Reverse the bytes + this._initArray(this.toArray(), base, endian); + }; + + BN.prototype._initArray = function _initArray (number, base, endian) { + // Perhaps a Uint8Array + assert(typeof number.length === 'number'); + if (number.length <= 0) { + this.words = [ 0 ]; + this.length = 1; + return this; + } + + this.length = Math.ceil(number.length / 3); + this.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + this.words[i] = 0; + } + + var j, w; + var off = 0; + if (endian === 'be') { + for (i = number.length - 1, j = 0; i >= 0; i -= 3) { + w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); + this.words[j] |= (w << off) & 0x3ffffff; + this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; + off += 24; + if (off >= 26) { + off -= 26; + j++; + } + } + } else if (endian === 'le') { + for (i = 0, j = 0; i < number.length; i += 3) { + w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); + this.words[j] |= (w << off) & 0x3ffffff; + this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; + off += 24; + if (off >= 26) { + off -= 26; + j++; + } + } + } + return this.strip(); + }; + + function parseHex (str, start, end) { + var r = 0; + var len = Math.min(str.length, end); + for (var i = start; i < len; i++) { + var c = str.charCodeAt(i) - 48; + + r <<= 4; + + // 'a' - 'f' + if (c >= 49 && c <= 54) { + r |= c - 49 + 0xa; + + // 'A' - 'F' + } else if (c >= 17 && c <= 22) { + r |= c - 17 + 0xa; + + // '0' - '9' + } else { + r |= c & 0xf; + } + } + return r; + } + + BN.prototype._parseHex = function _parseHex (number, start) { + // Create possibly bigger array to ensure that it fits the number + this.length = Math.ceil((number.length - start) / 6); + this.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + this.words[i] = 0; + } + + var j, w; + // Scan 24-bit chunks and add them to the number + var off = 0; + for (i = number.length - 6, j = 0; i >= start; i -= 6) { + w = parseHex(number, i, i + 6); + this.words[j] |= (w << off) & 0x3ffffff; + // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb + this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; + off += 24; + if (off >= 26) { + off -= 26; + j++; + } + } + if (i + 6 !== start) { + w = parseHex(number, start, i + 6); + this.words[j] |= (w << off) & 0x3ffffff; + this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; + } + this.strip(); + }; + + function parseBase (str, start, end, mul) { + var r = 0; + var len = Math.min(str.length, end); + for (var i = start; i < len; i++) { + var c = str.charCodeAt(i) - 48; + + r *= mul; + + // 'a' + if (c >= 49) { + r += c - 49 + 0xa; + + // 'A' + } else if (c >= 17) { + r += c - 17 + 0xa; + + // '0' - '9' + } else { + r += c; + } + } + return r; + } + + BN.prototype._parseBase = function _parseBase (number, base, start) { + // Initialize as zero + this.words = [ 0 ]; + this.length = 1; + + // Find length of limb in base + for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { + limbLen++; + } + limbLen--; + limbPow = (limbPow / base) | 0; + + var total = number.length - start; + var mod = total % limbLen; + var end = Math.min(total, total - mod) + start; + + var word = 0; + for (var i = start; i < end; i += limbLen) { + word = parseBase(number, i, i + limbLen, base); + + this.imuln(limbPow); + if (this.words[0] + word < 0x4000000) { + this.words[0] += word; + } else { + this._iaddn(word); + } + } + + if (mod !== 0) { + var pow = 1; + word = parseBase(number, i, number.length, base); + + for (i = 0; i < mod; i++) { + pow *= base; + } + + this.imuln(pow); + if (this.words[0] + word < 0x4000000) { + this.words[0] += word; + } else { + this._iaddn(word); + } + } + }; + + BN.prototype.copy = function copy (dest) { + dest.words = new Array(this.length); + for (var i = 0; i < this.length; i++) { + dest.words[i] = this.words[i]; + } + dest.length = this.length; + dest.negative = this.negative; + dest.red = this.red; + }; + + BN.prototype.clone = function clone () { + var r = new BN(null); + this.copy(r); + return r; + }; + + BN.prototype._expand = function _expand (size) { + while (this.length < size) { + this.words[this.length++] = 0; + } + return this; + }; + + // Remove leading `0` from `this` + BN.prototype.strip = function strip () { + while (this.length > 1 && this.words[this.length - 1] === 0) { + this.length--; + } + return this._normSign(); + }; + + BN.prototype._normSign = function _normSign () { + // -0 = 0 + if (this.length === 1 && this.words[0] === 0) { + this.negative = 0; + } + return this; + }; + + BN.prototype.inspect = function inspect () { + return (this.red ? ''; + }; + + /* + + var zeros = []; + var groupSizes = []; + var groupBases = []; + + var s = ''; + var i = -1; + while (++i < BN.wordSize) { + zeros[i] = s; + s += '0'; + } + groupSizes[0] = 0; + groupSizes[1] = 0; + groupBases[0] = 0; + groupBases[1] = 0; + var base = 2 - 1; + while (++base < 36 + 1) { + var groupSize = 0; + var groupBase = 1; + while (groupBase < (1 << BN.wordSize) / base) { + groupBase *= base; + groupSize += 1; + } + groupSizes[base] = groupSize; + groupBases[base] = groupBase; + } + + */ + + var zeros = [ + '', + '0', + '00', + '000', + '0000', + '00000', + '000000', + '0000000', + '00000000', + '000000000', + '0000000000', + '00000000000', + '000000000000', + '0000000000000', + '00000000000000', + '000000000000000', + '0000000000000000', + '00000000000000000', + '000000000000000000', + '0000000000000000000', + '00000000000000000000', + '000000000000000000000', + '0000000000000000000000', + '00000000000000000000000', + '000000000000000000000000', + '0000000000000000000000000' + ]; + + var groupSizes = [ + 0, 0, + 25, 16, 12, 11, 10, 9, 8, + 8, 7, 7, 7, 7, 6, 6, + 6, 6, 6, 6, 6, 5, 5, + 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5 + ]; + + var groupBases = [ + 0, 0, + 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, + 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, + 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, + 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, + 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 + ]; + + BN.prototype.toString = function toString (base, padding) { + base = base || 10; + padding = padding | 0 || 1; + + var out; + if (base === 16 || base === 'hex') { + out = ''; + var off = 0; + var carry = 0; + for (var i = 0; i < this.length; i++) { + var w = this.words[i]; + var word = (((w << off) | carry) & 0xffffff).toString(16); + carry = (w >>> (24 - off)) & 0xffffff; + if (carry !== 0 || i !== this.length - 1) { + out = zeros[6 - word.length] + word + out; + } else { + out = word + out; + } + off += 2; + if (off >= 26) { + off -= 26; + i--; + } + } + if (carry !== 0) { + out = carry.toString(16) + out; + } + while (out.length % padding !== 0) { + out = '0' + out; + } + if (this.negative !== 0) { + out = '-' + out; + } + return out; + } + + if (base === (base | 0) && base >= 2 && base <= 36) { + // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); + var groupSize = groupSizes[base]; + // var groupBase = Math.pow(base, groupSize); + var groupBase = groupBases[base]; + out = ''; + var c = this.clone(); + c.negative = 0; + while (!c.isZero()) { + var r = c.modn(groupBase).toString(base); + c = c.idivn(groupBase); + + if (!c.isZero()) { + out = zeros[groupSize - r.length] + r + out; + } else { + out = r + out; + } + } + if (this.isZero()) { + out = '0' + out; + } + while (out.length % padding !== 0) { + out = '0' + out; + } + if (this.negative !== 0) { + out = '-' + out; + } + return out; + } + + assert(false, 'Base should be between 2 and 36'); + }; + + BN.prototype.toNumber = function toNumber () { + var ret = this.words[0]; + if (this.length === 2) { + ret += this.words[1] * 0x4000000; + } else if (this.length === 3 && this.words[2] === 0x01) { + // NOTE: at this stage it is known that the top bit is set + ret += 0x10000000000000 + (this.words[1] * 0x4000000); + } else if (this.length > 2) { + assert(false, 'Number can only safely store up to 53 bits'); + } + return (this.negative !== 0) ? -ret : ret; + }; + + BN.prototype.toJSON = function toJSON () { + return this.toString(16); + }; + + BN.prototype.toBuffer = function toBuffer (endian, length) { + assert(typeof Buffer !== 'undefined'); + return this.toArrayLike(Buffer, endian, length); + }; + + BN.prototype.toArray = function toArray (endian, length) { + return this.toArrayLike(Array, endian, length); + }; + + BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) { + var byteLength = this.byteLength(); + var reqLength = length || Math.max(1, byteLength); + assert(byteLength <= reqLength, 'byte array longer than desired length'); + assert(reqLength > 0, 'Requested array length <= 0'); + + this.strip(); + var littleEndian = endian === 'le'; + var res = new ArrayType(reqLength); + + var b, i; + var q = this.clone(); + if (!littleEndian) { + // Assume big-endian + for (i = 0; i < reqLength - byteLength; i++) { + res[i] = 0; + } + + for (i = 0; !q.isZero(); i++) { + b = q.andln(0xff); + q.iushrn(8); + + res[reqLength - i - 1] = b; + } + } else { + for (i = 0; !q.isZero(); i++) { + b = q.andln(0xff); + q.iushrn(8); + + res[i] = b; + } + + for (; i < reqLength; i++) { + res[i] = 0; + } + } + + return res; + }; + + if (Math.clz32) { + BN.prototype._countBits = function _countBits (w) { + return 32 - Math.clz32(w); + }; + } else { + BN.prototype._countBits = function _countBits (w) { + var t = w; + var r = 0; + if (t >= 0x1000) { + r += 13; + t >>>= 13; + } + if (t >= 0x40) { + r += 7; + t >>>= 7; + } + if (t >= 0x8) { + r += 4; + t >>>= 4; + } + if (t >= 0x02) { + r += 2; + t >>>= 2; + } + return r + t; + }; + } + + BN.prototype._zeroBits = function _zeroBits (w) { + // Short-cut + if (w === 0) return 26; + + var t = w; + var r = 0; + if ((t & 0x1fff) === 0) { + r += 13; + t >>>= 13; + } + if ((t & 0x7f) === 0) { + r += 7; + t >>>= 7; + } + if ((t & 0xf) === 0) { + r += 4; + t >>>= 4; + } + if ((t & 0x3) === 0) { + r += 2; + t >>>= 2; + } + if ((t & 0x1) === 0) { + r++; + } + return r; + }; + + // Return number of used bits in a BN + BN.prototype.bitLength = function bitLength () { + var w = this.words[this.length - 1]; + var hi = this._countBits(w); + return (this.length - 1) * 26 + hi; + }; + + function toBitArray (num) { + var w = new Array(num.bitLength()); + + for (var bit = 0; bit < w.length; bit++) { + var off = (bit / 26) | 0; + var wbit = bit % 26; + + w[bit] = (num.words[off] & (1 << wbit)) >>> wbit; + } + + return w; + } + + // Number of trailing zero bits + BN.prototype.zeroBits = function zeroBits () { + if (this.isZero()) return 0; + + var r = 0; + for (var i = 0; i < this.length; i++) { + var b = this._zeroBits(this.words[i]); + r += b; + if (b !== 26) break; + } + return r; + }; + + BN.prototype.byteLength = function byteLength () { + return Math.ceil(this.bitLength() / 8); + }; + + BN.prototype.toTwos = function toTwos (width) { + if (this.negative !== 0) { + return this.abs().inotn(width).iaddn(1); + } + return this.clone(); + }; + + BN.prototype.fromTwos = function fromTwos (width) { + if (this.testn(width - 1)) { + return this.notn(width).iaddn(1).ineg(); + } + return this.clone(); + }; + + BN.prototype.isNeg = function isNeg () { + return this.negative !== 0; + }; + + // Return negative clone of `this` + BN.prototype.neg = function neg () { + return this.clone().ineg(); + }; + + BN.prototype.ineg = function ineg () { + if (!this.isZero()) { + this.negative ^= 1; + } + + return this; + }; + + // Or `num` with `this` in-place + BN.prototype.iuor = function iuor (num) { + while (this.length < num.length) { + this.words[this.length++] = 0; + } + + for (var i = 0; i < num.length; i++) { + this.words[i] = this.words[i] | num.words[i]; + } + + return this.strip(); + }; + + BN.prototype.ior = function ior (num) { + assert((this.negative | num.negative) === 0); + return this.iuor(num); + }; + + // Or `num` with `this` + BN.prototype.or = function or (num) { + if (this.length > num.length) return this.clone().ior(num); + return num.clone().ior(this); + }; + + BN.prototype.uor = function uor (num) { + if (this.length > num.length) return this.clone().iuor(num); + return num.clone().iuor(this); + }; + + // And `num` with `this` in-place + BN.prototype.iuand = function iuand (num) { + // b = min-length(num, this) + var b; + if (this.length > num.length) { + b = num; + } else { + b = this; + } + + for (var i = 0; i < b.length; i++) { + this.words[i] = this.words[i] & num.words[i]; + } + + this.length = b.length; + + return this.strip(); + }; + + BN.prototype.iand = function iand (num) { + assert((this.negative | num.negative) === 0); + return this.iuand(num); + }; + + // And `num` with `this` + BN.prototype.and = function and (num) { + if (this.length > num.length) return this.clone().iand(num); + return num.clone().iand(this); + }; + + BN.prototype.uand = function uand (num) { + if (this.length > num.length) return this.clone().iuand(num); + return num.clone().iuand(this); + }; + + // Xor `num` with `this` in-place + BN.prototype.iuxor = function iuxor (num) { + // a.length > b.length + var a; + var b; + if (this.length > num.length) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + for (var i = 0; i < b.length; i++) { + this.words[i] = a.words[i] ^ b.words[i]; + } + + if (this !== a) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + this.length = a.length; + + return this.strip(); + }; + + BN.prototype.ixor = function ixor (num) { + assert((this.negative | num.negative) === 0); + return this.iuxor(num); + }; + + // Xor `num` with `this` + BN.prototype.xor = function xor (num) { + if (this.length > num.length) return this.clone().ixor(num); + return num.clone().ixor(this); + }; + + BN.prototype.uxor = function uxor (num) { + if (this.length > num.length) return this.clone().iuxor(num); + return num.clone().iuxor(this); + }; + + // Not ``this`` with ``width`` bitwidth + BN.prototype.inotn = function inotn (width) { + assert(typeof width === 'number' && width >= 0); + + var bytesNeeded = Math.ceil(width / 26) | 0; + var bitsLeft = width % 26; + + // Extend the buffer with leading zeroes + this._expand(bytesNeeded); + + if (bitsLeft > 0) { + bytesNeeded--; + } + + // Handle complete words + for (var i = 0; i < bytesNeeded; i++) { + this.words[i] = ~this.words[i] & 0x3ffffff; + } + + // Handle the residue + if (bitsLeft > 0) { + this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft)); + } + + // And remove leading zeroes + return this.strip(); + }; + + BN.prototype.notn = function notn (width) { + return this.clone().inotn(width); + }; + + // Set `bit` of `this` + BN.prototype.setn = function setn (bit, val) { + assert(typeof bit === 'number' && bit >= 0); + + var off = (bit / 26) | 0; + var wbit = bit % 26; + + this._expand(off + 1); + + if (val) { + this.words[off] = this.words[off] | (1 << wbit); + } else { + this.words[off] = this.words[off] & ~(1 << wbit); + } + + return this.strip(); + }; + + // Add `num` to `this` in-place + BN.prototype.iadd = function iadd (num) { + var r; + + // negative + positive + if (this.negative !== 0 && num.negative === 0) { + this.negative = 0; + r = this.isub(num); + this.negative ^= 1; + return this._normSign(); + + // positive + negative + } else if (this.negative === 0 && num.negative !== 0) { + num.negative = 0; + r = this.isub(num); + num.negative = 1; + return r._normSign(); + } + + // a.length > b.length + var a, b; + if (this.length > num.length) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + var carry = 0; + for (var i = 0; i < b.length; i++) { + r = (a.words[i] | 0) + (b.words[i] | 0) + carry; + this.words[i] = r & 0x3ffffff; + carry = r >>> 26; + } + for (; carry !== 0 && i < a.length; i++) { + r = (a.words[i] | 0) + carry; + this.words[i] = r & 0x3ffffff; + carry = r >>> 26; + } + + this.length = a.length; + if (carry !== 0) { + this.words[this.length] = carry; + this.length++; + // Copy the rest of the words + } else if (a !== this) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + return this; + }; + + // Add `num` to `this` + BN.prototype.add = function add (num) { + var res; + if (num.negative !== 0 && this.negative === 0) { + num.negative = 0; + res = this.sub(num); + num.negative ^= 1; + return res; + } else if (num.negative === 0 && this.negative !== 0) { + this.negative = 0; + res = num.sub(this); + this.negative = 1; + return res; + } + + if (this.length > num.length) return this.clone().iadd(num); + + return num.clone().iadd(this); + }; + + // Subtract `num` from `this` in-place + BN.prototype.isub = function isub (num) { + // this - (-num) = this + num + if (num.negative !== 0) { + num.negative = 0; + var r = this.iadd(num); + num.negative = 1; + return r._normSign(); + + // -this - num = -(this + num) + } else if (this.negative !== 0) { + this.negative = 0; + this.iadd(num); + this.negative = 1; + return this._normSign(); + } + + // At this point both numbers are positive + var cmp = this.cmp(num); + + // Optimization - zeroify + if (cmp === 0) { + this.negative = 0; + this.length = 1; + this.words[0] = 0; + return this; + } + + // a > b + var a, b; + if (cmp > 0) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + var carry = 0; + for (var i = 0; i < b.length; i++) { + r = (a.words[i] | 0) - (b.words[i] | 0) + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; + } + for (; carry !== 0 && i < a.length; i++) { + r = (a.words[i] | 0) + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; + } + + // Copy rest of the words + if (carry === 0 && i < a.length && a !== this) { + for (; i < a.length; i++) { + this.words[i] = a.words[i]; + } + } + + this.length = Math.max(this.length, i); + + if (a !== this) { + this.negative = 1; + } + + return this.strip(); + }; + + // Subtract `num` from `this` + BN.prototype.sub = function sub (num) { + return this.clone().isub(num); + }; + + function smallMulTo (self, num, out) { + out.negative = num.negative ^ self.negative; + var len = (self.length + num.length) | 0; + out.length = len; + len = (len - 1) | 0; + + // Peel one iteration (compiler can't do it, because of code complexity) + var a = self.words[0] | 0; + var b = num.words[0] | 0; + var r = a * b; + + var lo = r & 0x3ffffff; + var carry = (r / 0x4000000) | 0; + out.words[0] = lo; + + for (var k = 1; k < len; k++) { + // Sum all words with the same `i + j = k` and accumulate `ncarry`, + // note that ncarry could be >= 0x3ffffff + var ncarry = carry >>> 26; + var rword = carry & 0x3ffffff; + var maxJ = Math.min(k, num.length - 1); + for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { + var i = (k - j) | 0; + a = self.words[i] | 0; + b = num.words[j] | 0; + r = a * b + rword; + ncarry += (r / 0x4000000) | 0; + rword = r & 0x3ffffff; + } + out.words[k] = rword | 0; + carry = ncarry | 0; + } + if (carry !== 0) { + out.words[k] = carry | 0; + } else { + out.length--; + } + + return out.strip(); + } + + // TODO(indutny): it may be reasonable to omit it for users who don't need + // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit + // multiplication (like elliptic secp256k1). + var comb10MulTo = function comb10MulTo (self, num, out) { + var a = self.words; + var b = num.words; + var o = out.words; + var c = 0; + var lo; + var mid; + var hi; + var a0 = a[0] | 0; + var al0 = a0 & 0x1fff; + var ah0 = a0 >>> 13; + var a1 = a[1] | 0; + var al1 = a1 & 0x1fff; + var ah1 = a1 >>> 13; + var a2 = a[2] | 0; + var al2 = a2 & 0x1fff; + var ah2 = a2 >>> 13; + var a3 = a[3] | 0; + var al3 = a3 & 0x1fff; + var ah3 = a3 >>> 13; + var a4 = a[4] | 0; + var al4 = a4 & 0x1fff; + var ah4 = a4 >>> 13; + var a5 = a[5] | 0; + var al5 = a5 & 0x1fff; + var ah5 = a5 >>> 13; + var a6 = a[6] | 0; + var al6 = a6 & 0x1fff; + var ah6 = a6 >>> 13; + var a7 = a[7] | 0; + var al7 = a7 & 0x1fff; + var ah7 = a7 >>> 13; + var a8 = a[8] | 0; + var al8 = a8 & 0x1fff; + var ah8 = a8 >>> 13; + var a9 = a[9] | 0; + var al9 = a9 & 0x1fff; + var ah9 = a9 >>> 13; + var b0 = b[0] | 0; + var bl0 = b0 & 0x1fff; + var bh0 = b0 >>> 13; + var b1 = b[1] | 0; + var bl1 = b1 & 0x1fff; + var bh1 = b1 >>> 13; + var b2 = b[2] | 0; + var bl2 = b2 & 0x1fff; + var bh2 = b2 >>> 13; + var b3 = b[3] | 0; + var bl3 = b3 & 0x1fff; + var bh3 = b3 >>> 13; + var b4 = b[4] | 0; + var bl4 = b4 & 0x1fff; + var bh4 = b4 >>> 13; + var b5 = b[5] | 0; + var bl5 = b5 & 0x1fff; + var bh5 = b5 >>> 13; + var b6 = b[6] | 0; + var bl6 = b6 & 0x1fff; + var bh6 = b6 >>> 13; + var b7 = b[7] | 0; + var bl7 = b7 & 0x1fff; + var bh7 = b7 >>> 13; + var b8 = b[8] | 0; + var bl8 = b8 & 0x1fff; + var bh8 = b8 >>> 13; + var b9 = b[9] | 0; + var bl9 = b9 & 0x1fff; + var bh9 = b9 >>> 13; + + out.negative = self.negative ^ num.negative; + out.length = 19; + /* k = 0 */ + lo = Math.imul(al0, bl0); + mid = Math.imul(al0, bh0); + mid = (mid + Math.imul(ah0, bl0)) | 0; + hi = Math.imul(ah0, bh0); + var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0; + w0 &= 0x3ffffff; + /* k = 1 */ + lo = Math.imul(al1, bl0); + mid = Math.imul(al1, bh0); + mid = (mid + Math.imul(ah1, bl0)) | 0; + hi = Math.imul(ah1, bh0); + lo = (lo + Math.imul(al0, bl1)) | 0; + mid = (mid + Math.imul(al0, bh1)) | 0; + mid = (mid + Math.imul(ah0, bl1)) | 0; + hi = (hi + Math.imul(ah0, bh1)) | 0; + var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0; + w1 &= 0x3ffffff; + /* k = 2 */ + lo = Math.imul(al2, bl0); + mid = Math.imul(al2, bh0); + mid = (mid + Math.imul(ah2, bl0)) | 0; + hi = Math.imul(ah2, bh0); + lo = (lo + Math.imul(al1, bl1)) | 0; + mid = (mid + Math.imul(al1, bh1)) | 0; + mid = (mid + Math.imul(ah1, bl1)) | 0; + hi = (hi + Math.imul(ah1, bh1)) | 0; + lo = (lo + Math.imul(al0, bl2)) | 0; + mid = (mid + Math.imul(al0, bh2)) | 0; + mid = (mid + Math.imul(ah0, bl2)) | 0; + hi = (hi + Math.imul(ah0, bh2)) | 0; + var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0; + w2 &= 0x3ffffff; + /* k = 3 */ + lo = Math.imul(al3, bl0); + mid = Math.imul(al3, bh0); + mid = (mid + Math.imul(ah3, bl0)) | 0; + hi = Math.imul(ah3, bh0); + lo = (lo + Math.imul(al2, bl1)) | 0; + mid = (mid + Math.imul(al2, bh1)) | 0; + mid = (mid + Math.imul(ah2, bl1)) | 0; + hi = (hi + Math.imul(ah2, bh1)) | 0; + lo = (lo + Math.imul(al1, bl2)) | 0; + mid = (mid + Math.imul(al1, bh2)) | 0; + mid = (mid + Math.imul(ah1, bl2)) | 0; + hi = (hi + Math.imul(ah1, bh2)) | 0; + lo = (lo + Math.imul(al0, bl3)) | 0; + mid = (mid + Math.imul(al0, bh3)) | 0; + mid = (mid + Math.imul(ah0, bl3)) | 0; + hi = (hi + Math.imul(ah0, bh3)) | 0; + var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0; + w3 &= 0x3ffffff; + /* k = 4 */ + lo = Math.imul(al4, bl0); + mid = Math.imul(al4, bh0); + mid = (mid + Math.imul(ah4, bl0)) | 0; + hi = Math.imul(ah4, bh0); + lo = (lo + Math.imul(al3, bl1)) | 0; + mid = (mid + Math.imul(al3, bh1)) | 0; + mid = (mid + Math.imul(ah3, bl1)) | 0; + hi = (hi + Math.imul(ah3, bh1)) | 0; + lo = (lo + Math.imul(al2, bl2)) | 0; + mid = (mid + Math.imul(al2, bh2)) | 0; + mid = (mid + Math.imul(ah2, bl2)) | 0; + hi = (hi + Math.imul(ah2, bh2)) | 0; + lo = (lo + Math.imul(al1, bl3)) | 0; + mid = (mid + Math.imul(al1, bh3)) | 0; + mid = (mid + Math.imul(ah1, bl3)) | 0; + hi = (hi + Math.imul(ah1, bh3)) | 0; + lo = (lo + Math.imul(al0, bl4)) | 0; + mid = (mid + Math.imul(al0, bh4)) | 0; + mid = (mid + Math.imul(ah0, bl4)) | 0; + hi = (hi + Math.imul(ah0, bh4)) | 0; + var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0; + w4 &= 0x3ffffff; + /* k = 5 */ + lo = Math.imul(al5, bl0); + mid = Math.imul(al5, bh0); + mid = (mid + Math.imul(ah5, bl0)) | 0; + hi = Math.imul(ah5, bh0); + lo = (lo + Math.imul(al4, bl1)) | 0; + mid = (mid + Math.imul(al4, bh1)) | 0; + mid = (mid + Math.imul(ah4, bl1)) | 0; + hi = (hi + Math.imul(ah4, bh1)) | 0; + lo = (lo + Math.imul(al3, bl2)) | 0; + mid = (mid + Math.imul(al3, bh2)) | 0; + mid = (mid + Math.imul(ah3, bl2)) | 0; + hi = (hi + Math.imul(ah3, bh2)) | 0; + lo = (lo + Math.imul(al2, bl3)) | 0; + mid = (mid + Math.imul(al2, bh3)) | 0; + mid = (mid + Math.imul(ah2, bl3)) | 0; + hi = (hi + Math.imul(ah2, bh3)) | 0; + lo = (lo + Math.imul(al1, bl4)) | 0; + mid = (mid + Math.imul(al1, bh4)) | 0; + mid = (mid + Math.imul(ah1, bl4)) | 0; + hi = (hi + Math.imul(ah1, bh4)) | 0; + lo = (lo + Math.imul(al0, bl5)) | 0; + mid = (mid + Math.imul(al0, bh5)) | 0; + mid = (mid + Math.imul(ah0, bl5)) | 0; + hi = (hi + Math.imul(ah0, bh5)) | 0; + var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0; + w5 &= 0x3ffffff; + /* k = 6 */ + lo = Math.imul(al6, bl0); + mid = Math.imul(al6, bh0); + mid = (mid + Math.imul(ah6, bl0)) | 0; + hi = Math.imul(ah6, bh0); + lo = (lo + Math.imul(al5, bl1)) | 0; + mid = (mid + Math.imul(al5, bh1)) | 0; + mid = (mid + Math.imul(ah5, bl1)) | 0; + hi = (hi + Math.imul(ah5, bh1)) | 0; + lo = (lo + Math.imul(al4, bl2)) | 0; + mid = (mid + Math.imul(al4, bh2)) | 0; + mid = (mid + Math.imul(ah4, bl2)) | 0; + hi = (hi + Math.imul(ah4, bh2)) | 0; + lo = (lo + Math.imul(al3, bl3)) | 0; + mid = (mid + Math.imul(al3, bh3)) | 0; + mid = (mid + Math.imul(ah3, bl3)) | 0; + hi = (hi + Math.imul(ah3, bh3)) | 0; + lo = (lo + Math.imul(al2, bl4)) | 0; + mid = (mid + Math.imul(al2, bh4)) | 0; + mid = (mid + Math.imul(ah2, bl4)) | 0; + hi = (hi + Math.imul(ah2, bh4)) | 0; + lo = (lo + Math.imul(al1, bl5)) | 0; + mid = (mid + Math.imul(al1, bh5)) | 0; + mid = (mid + Math.imul(ah1, bl5)) | 0; + hi = (hi + Math.imul(ah1, bh5)) | 0; + lo = (lo + Math.imul(al0, bl6)) | 0; + mid = (mid + Math.imul(al0, bh6)) | 0; + mid = (mid + Math.imul(ah0, bl6)) | 0; + hi = (hi + Math.imul(ah0, bh6)) | 0; + var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0; + w6 &= 0x3ffffff; + /* k = 7 */ + lo = Math.imul(al7, bl0); + mid = Math.imul(al7, bh0); + mid = (mid + Math.imul(ah7, bl0)) | 0; + hi = Math.imul(ah7, bh0); + lo = (lo + Math.imul(al6, bl1)) | 0; + mid = (mid + Math.imul(al6, bh1)) | 0; + mid = (mid + Math.imul(ah6, bl1)) | 0; + hi = (hi + Math.imul(ah6, bh1)) | 0; + lo = (lo + Math.imul(al5, bl2)) | 0; + mid = (mid + Math.imul(al5, bh2)) | 0; + mid = (mid + Math.imul(ah5, bl2)) | 0; + hi = (hi + Math.imul(ah5, bh2)) | 0; + lo = (lo + Math.imul(al4, bl3)) | 0; + mid = (mid + Math.imul(al4, bh3)) | 0; + mid = (mid + Math.imul(ah4, bl3)) | 0; + hi = (hi + Math.imul(ah4, bh3)) | 0; + lo = (lo + Math.imul(al3, bl4)) | 0; + mid = (mid + Math.imul(al3, bh4)) | 0; + mid = (mid + Math.imul(ah3, bl4)) | 0; + hi = (hi + Math.imul(ah3, bh4)) | 0; + lo = (lo + Math.imul(al2, bl5)) | 0; + mid = (mid + Math.imul(al2, bh5)) | 0; + mid = (mid + Math.imul(ah2, bl5)) | 0; + hi = (hi + Math.imul(ah2, bh5)) | 0; + lo = (lo + Math.imul(al1, bl6)) | 0; + mid = (mid + Math.imul(al1, bh6)) | 0; + mid = (mid + Math.imul(ah1, bl6)) | 0; + hi = (hi + Math.imul(ah1, bh6)) | 0; + lo = (lo + Math.imul(al0, bl7)) | 0; + mid = (mid + Math.imul(al0, bh7)) | 0; + mid = (mid + Math.imul(ah0, bl7)) | 0; + hi = (hi + Math.imul(ah0, bh7)) | 0; + var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0; + w7 &= 0x3ffffff; + /* k = 8 */ + lo = Math.imul(al8, bl0); + mid = Math.imul(al8, bh0); + mid = (mid + Math.imul(ah8, bl0)) | 0; + hi = Math.imul(ah8, bh0); + lo = (lo + Math.imul(al7, bl1)) | 0; + mid = (mid + Math.imul(al7, bh1)) | 0; + mid = (mid + Math.imul(ah7, bl1)) | 0; + hi = (hi + Math.imul(ah7, bh1)) | 0; + lo = (lo + Math.imul(al6, bl2)) | 0; + mid = (mid + Math.imul(al6, bh2)) | 0; + mid = (mid + Math.imul(ah6, bl2)) | 0; + hi = (hi + Math.imul(ah6, bh2)) | 0; + lo = (lo + Math.imul(al5, bl3)) | 0; + mid = (mid + Math.imul(al5, bh3)) | 0; + mid = (mid + Math.imul(ah5, bl3)) | 0; + hi = (hi + Math.imul(ah5, bh3)) | 0; + lo = (lo + Math.imul(al4, bl4)) | 0; + mid = (mid + Math.imul(al4, bh4)) | 0; + mid = (mid + Math.imul(ah4, bl4)) | 0; + hi = (hi + Math.imul(ah4, bh4)) | 0; + lo = (lo + Math.imul(al3, bl5)) | 0; + mid = (mid + Math.imul(al3, bh5)) | 0; + mid = (mid + Math.imul(ah3, bl5)) | 0; + hi = (hi + Math.imul(ah3, bh5)) | 0; + lo = (lo + Math.imul(al2, bl6)) | 0; + mid = (mid + Math.imul(al2, bh6)) | 0; + mid = (mid + Math.imul(ah2, bl6)) | 0; + hi = (hi + Math.imul(ah2, bh6)) | 0; + lo = (lo + Math.imul(al1, bl7)) | 0; + mid = (mid + Math.imul(al1, bh7)) | 0; + mid = (mid + Math.imul(ah1, bl7)) | 0; + hi = (hi + Math.imul(ah1, bh7)) | 0; + lo = (lo + Math.imul(al0, bl8)) | 0; + mid = (mid + Math.imul(al0, bh8)) | 0; + mid = (mid + Math.imul(ah0, bl8)) | 0; + hi = (hi + Math.imul(ah0, bh8)) | 0; + var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0; + w8 &= 0x3ffffff; + /* k = 9 */ + lo = Math.imul(al9, bl0); + mid = Math.imul(al9, bh0); + mid = (mid + Math.imul(ah9, bl0)) | 0; + hi = Math.imul(ah9, bh0); + lo = (lo + Math.imul(al8, bl1)) | 0; + mid = (mid + Math.imul(al8, bh1)) | 0; + mid = (mid + Math.imul(ah8, bl1)) | 0; + hi = (hi + Math.imul(ah8, bh1)) | 0; + lo = (lo + Math.imul(al7, bl2)) | 0; + mid = (mid + Math.imul(al7, bh2)) | 0; + mid = (mid + Math.imul(ah7, bl2)) | 0; + hi = (hi + Math.imul(ah7, bh2)) | 0; + lo = (lo + Math.imul(al6, bl3)) | 0; + mid = (mid + Math.imul(al6, bh3)) | 0; + mid = (mid + Math.imul(ah6, bl3)) | 0; + hi = (hi + Math.imul(ah6, bh3)) | 0; + lo = (lo + Math.imul(al5, bl4)) | 0; + mid = (mid + Math.imul(al5, bh4)) | 0; + mid = (mid + Math.imul(ah5, bl4)) | 0; + hi = (hi + Math.imul(ah5, bh4)) | 0; + lo = (lo + Math.imul(al4, bl5)) | 0; + mid = (mid + Math.imul(al4, bh5)) | 0; + mid = (mid + Math.imul(ah4, bl5)) | 0; + hi = (hi + Math.imul(ah4, bh5)) | 0; + lo = (lo + Math.imul(al3, bl6)) | 0; + mid = (mid + Math.imul(al3, bh6)) | 0; + mid = (mid + Math.imul(ah3, bl6)) | 0; + hi = (hi + Math.imul(ah3, bh6)) | 0; + lo = (lo + Math.imul(al2, bl7)) | 0; + mid = (mid + Math.imul(al2, bh7)) | 0; + mid = (mid + Math.imul(ah2, bl7)) | 0; + hi = (hi + Math.imul(ah2, bh7)) | 0; + lo = (lo + Math.imul(al1, bl8)) | 0; + mid = (mid + Math.imul(al1, bh8)) | 0; + mid = (mid + Math.imul(ah1, bl8)) | 0; + hi = (hi + Math.imul(ah1, bh8)) | 0; + lo = (lo + Math.imul(al0, bl9)) | 0; + mid = (mid + Math.imul(al0, bh9)) | 0; + mid = (mid + Math.imul(ah0, bl9)) | 0; + hi = (hi + Math.imul(ah0, bh9)) | 0; + var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0; + w9 &= 0x3ffffff; + /* k = 10 */ + lo = Math.imul(al9, bl1); + mid = Math.imul(al9, bh1); + mid = (mid + Math.imul(ah9, bl1)) | 0; + hi = Math.imul(ah9, bh1); + lo = (lo + Math.imul(al8, bl2)) | 0; + mid = (mid + Math.imul(al8, bh2)) | 0; + mid = (mid + Math.imul(ah8, bl2)) | 0; + hi = (hi + Math.imul(ah8, bh2)) | 0; + lo = (lo + Math.imul(al7, bl3)) | 0; + mid = (mid + Math.imul(al7, bh3)) | 0; + mid = (mid + Math.imul(ah7, bl3)) | 0; + hi = (hi + Math.imul(ah7, bh3)) | 0; + lo = (lo + Math.imul(al6, bl4)) | 0; + mid = (mid + Math.imul(al6, bh4)) | 0; + mid = (mid + Math.imul(ah6, bl4)) | 0; + hi = (hi + Math.imul(ah6, bh4)) | 0; + lo = (lo + Math.imul(al5, bl5)) | 0; + mid = (mid + Math.imul(al5, bh5)) | 0; + mid = (mid + Math.imul(ah5, bl5)) | 0; + hi = (hi + Math.imul(ah5, bh5)) | 0; + lo = (lo + Math.imul(al4, bl6)) | 0; + mid = (mid + Math.imul(al4, bh6)) | 0; + mid = (mid + Math.imul(ah4, bl6)) | 0; + hi = (hi + Math.imul(ah4, bh6)) | 0; + lo = (lo + Math.imul(al3, bl7)) | 0; + mid = (mid + Math.imul(al3, bh7)) | 0; + mid = (mid + Math.imul(ah3, bl7)) | 0; + hi = (hi + Math.imul(ah3, bh7)) | 0; + lo = (lo + Math.imul(al2, bl8)) | 0; + mid = (mid + Math.imul(al2, bh8)) | 0; + mid = (mid + Math.imul(ah2, bl8)) | 0; + hi = (hi + Math.imul(ah2, bh8)) | 0; + lo = (lo + Math.imul(al1, bl9)) | 0; + mid = (mid + Math.imul(al1, bh9)) | 0; + mid = (mid + Math.imul(ah1, bl9)) | 0; + hi = (hi + Math.imul(ah1, bh9)) | 0; + var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0; + w10 &= 0x3ffffff; + /* k = 11 */ + lo = Math.imul(al9, bl2); + mid = Math.imul(al9, bh2); + mid = (mid + Math.imul(ah9, bl2)) | 0; + hi = Math.imul(ah9, bh2); + lo = (lo + Math.imul(al8, bl3)) | 0; + mid = (mid + Math.imul(al8, bh3)) | 0; + mid = (mid + Math.imul(ah8, bl3)) | 0; + hi = (hi + Math.imul(ah8, bh3)) | 0; + lo = (lo + Math.imul(al7, bl4)) | 0; + mid = (mid + Math.imul(al7, bh4)) | 0; + mid = (mid + Math.imul(ah7, bl4)) | 0; + hi = (hi + Math.imul(ah7, bh4)) | 0; + lo = (lo + Math.imul(al6, bl5)) | 0; + mid = (mid + Math.imul(al6, bh5)) | 0; + mid = (mid + Math.imul(ah6, bl5)) | 0; + hi = (hi + Math.imul(ah6, bh5)) | 0; + lo = (lo + Math.imul(al5, bl6)) | 0; + mid = (mid + Math.imul(al5, bh6)) | 0; + mid = (mid + Math.imul(ah5, bl6)) | 0; + hi = (hi + Math.imul(ah5, bh6)) | 0; + lo = (lo + Math.imul(al4, bl7)) | 0; + mid = (mid + Math.imul(al4, bh7)) | 0; + mid = (mid + Math.imul(ah4, bl7)) | 0; + hi = (hi + Math.imul(ah4, bh7)) | 0; + lo = (lo + Math.imul(al3, bl8)) | 0; + mid = (mid + Math.imul(al3, bh8)) | 0; + mid = (mid + Math.imul(ah3, bl8)) | 0; + hi = (hi + Math.imul(ah3, bh8)) | 0; + lo = (lo + Math.imul(al2, bl9)) | 0; + mid = (mid + Math.imul(al2, bh9)) | 0; + mid = (mid + Math.imul(ah2, bl9)) | 0; + hi = (hi + Math.imul(ah2, bh9)) | 0; + var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0; + w11 &= 0x3ffffff; + /* k = 12 */ + lo = Math.imul(al9, bl3); + mid = Math.imul(al9, bh3); + mid = (mid + Math.imul(ah9, bl3)) | 0; + hi = Math.imul(ah9, bh3); + lo = (lo + Math.imul(al8, bl4)) | 0; + mid = (mid + Math.imul(al8, bh4)) | 0; + mid = (mid + Math.imul(ah8, bl4)) | 0; + hi = (hi + Math.imul(ah8, bh4)) | 0; + lo = (lo + Math.imul(al7, bl5)) | 0; + mid = (mid + Math.imul(al7, bh5)) | 0; + mid = (mid + Math.imul(ah7, bl5)) | 0; + hi = (hi + Math.imul(ah7, bh5)) | 0; + lo = (lo + Math.imul(al6, bl6)) | 0; + mid = (mid + Math.imul(al6, bh6)) | 0; + mid = (mid + Math.imul(ah6, bl6)) | 0; + hi = (hi + Math.imul(ah6, bh6)) | 0; + lo = (lo + Math.imul(al5, bl7)) | 0; + mid = (mid + Math.imul(al5, bh7)) | 0; + mid = (mid + Math.imul(ah5, bl7)) | 0; + hi = (hi + Math.imul(ah5, bh7)) | 0; + lo = (lo + Math.imul(al4, bl8)) | 0; + mid = (mid + Math.imul(al4, bh8)) | 0; + mid = (mid + Math.imul(ah4, bl8)) | 0; + hi = (hi + Math.imul(ah4, bh8)) | 0; + lo = (lo + Math.imul(al3, bl9)) | 0; + mid = (mid + Math.imul(al3, bh9)) | 0; + mid = (mid + Math.imul(ah3, bl9)) | 0; + hi = (hi + Math.imul(ah3, bh9)) | 0; + var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0; + w12 &= 0x3ffffff; + /* k = 13 */ + lo = Math.imul(al9, bl4); + mid = Math.imul(al9, bh4); + mid = (mid + Math.imul(ah9, bl4)) | 0; + hi = Math.imul(ah9, bh4); + lo = (lo + Math.imul(al8, bl5)) | 0; + mid = (mid + Math.imul(al8, bh5)) | 0; + mid = (mid + Math.imul(ah8, bl5)) | 0; + hi = (hi + Math.imul(ah8, bh5)) | 0; + lo = (lo + Math.imul(al7, bl6)) | 0; + mid = (mid + Math.imul(al7, bh6)) | 0; + mid = (mid + Math.imul(ah7, bl6)) | 0; + hi = (hi + Math.imul(ah7, bh6)) | 0; + lo = (lo + Math.imul(al6, bl7)) | 0; + mid = (mid + Math.imul(al6, bh7)) | 0; + mid = (mid + Math.imul(ah6, bl7)) | 0; + hi = (hi + Math.imul(ah6, bh7)) | 0; + lo = (lo + Math.imul(al5, bl8)) | 0; + mid = (mid + Math.imul(al5, bh8)) | 0; + mid = (mid + Math.imul(ah5, bl8)) | 0; + hi = (hi + Math.imul(ah5, bh8)) | 0; + lo = (lo + Math.imul(al4, bl9)) | 0; + mid = (mid + Math.imul(al4, bh9)) | 0; + mid = (mid + Math.imul(ah4, bl9)) | 0; + hi = (hi + Math.imul(ah4, bh9)) | 0; + var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0; + w13 &= 0x3ffffff; + /* k = 14 */ + lo = Math.imul(al9, bl5); + mid = Math.imul(al9, bh5); + mid = (mid + Math.imul(ah9, bl5)) | 0; + hi = Math.imul(ah9, bh5); + lo = (lo + Math.imul(al8, bl6)) | 0; + mid = (mid + Math.imul(al8, bh6)) | 0; + mid = (mid + Math.imul(ah8, bl6)) | 0; + hi = (hi + Math.imul(ah8, bh6)) | 0; + lo = (lo + Math.imul(al7, bl7)) | 0; + mid = (mid + Math.imul(al7, bh7)) | 0; + mid = (mid + Math.imul(ah7, bl7)) | 0; + hi = (hi + Math.imul(ah7, bh7)) | 0; + lo = (lo + Math.imul(al6, bl8)) | 0; + mid = (mid + Math.imul(al6, bh8)) | 0; + mid = (mid + Math.imul(ah6, bl8)) | 0; + hi = (hi + Math.imul(ah6, bh8)) | 0; + lo = (lo + Math.imul(al5, bl9)) | 0; + mid = (mid + Math.imul(al5, bh9)) | 0; + mid = (mid + Math.imul(ah5, bl9)) | 0; + hi = (hi + Math.imul(ah5, bh9)) | 0; + var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0; + w14 &= 0x3ffffff; + /* k = 15 */ + lo = Math.imul(al9, bl6); + mid = Math.imul(al9, bh6); + mid = (mid + Math.imul(ah9, bl6)) | 0; + hi = Math.imul(ah9, bh6); + lo = (lo + Math.imul(al8, bl7)) | 0; + mid = (mid + Math.imul(al8, bh7)) | 0; + mid = (mid + Math.imul(ah8, bl7)) | 0; + hi = (hi + Math.imul(ah8, bh7)) | 0; + lo = (lo + Math.imul(al7, bl8)) | 0; + mid = (mid + Math.imul(al7, bh8)) | 0; + mid = (mid + Math.imul(ah7, bl8)) | 0; + hi = (hi + Math.imul(ah7, bh8)) | 0; + lo = (lo + Math.imul(al6, bl9)) | 0; + mid = (mid + Math.imul(al6, bh9)) | 0; + mid = (mid + Math.imul(ah6, bl9)) | 0; + hi = (hi + Math.imul(ah6, bh9)) | 0; + var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0; + w15 &= 0x3ffffff; + /* k = 16 */ + lo = Math.imul(al9, bl7); + mid = Math.imul(al9, bh7); + mid = (mid + Math.imul(ah9, bl7)) | 0; + hi = Math.imul(ah9, bh7); + lo = (lo + Math.imul(al8, bl8)) | 0; + mid = (mid + Math.imul(al8, bh8)) | 0; + mid = (mid + Math.imul(ah8, bl8)) | 0; + hi = (hi + Math.imul(ah8, bh8)) | 0; + lo = (lo + Math.imul(al7, bl9)) | 0; + mid = (mid + Math.imul(al7, bh9)) | 0; + mid = (mid + Math.imul(ah7, bl9)) | 0; + hi = (hi + Math.imul(ah7, bh9)) | 0; + var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0; + w16 &= 0x3ffffff; + /* k = 17 */ + lo = Math.imul(al9, bl8); + mid = Math.imul(al9, bh8); + mid = (mid + Math.imul(ah9, bl8)) | 0; + hi = Math.imul(ah9, bh8); + lo = (lo + Math.imul(al8, bl9)) | 0; + mid = (mid + Math.imul(al8, bh9)) | 0; + mid = (mid + Math.imul(ah8, bl9)) | 0; + hi = (hi + Math.imul(ah8, bh9)) | 0; + var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0; + w17 &= 0x3ffffff; + /* k = 18 */ + lo = Math.imul(al9, bl9); + mid = Math.imul(al9, bh9); + mid = (mid + Math.imul(ah9, bl9)) | 0; + hi = Math.imul(ah9, bh9); + var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; + c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0; + w18 &= 0x3ffffff; + o[0] = w0; + o[1] = w1; + o[2] = w2; + o[3] = w3; + o[4] = w4; + o[5] = w5; + o[6] = w6; + o[7] = w7; + o[8] = w8; + o[9] = w9; + o[10] = w10; + o[11] = w11; + o[12] = w12; + o[13] = w13; + o[14] = w14; + o[15] = w15; + o[16] = w16; + o[17] = w17; + o[18] = w18; + if (c !== 0) { + o[19] = c; + out.length++; + } + return out; + }; + + // Polyfill comb + if (!Math.imul) { + comb10MulTo = smallMulTo; + } + + function bigMulTo (self, num, out) { + out.negative = num.negative ^ self.negative; + out.length = self.length + num.length; + + var carry = 0; + var hncarry = 0; + for (var k = 0; k < out.length - 1; k++) { + // Sum all words with the same `i + j = k` and accumulate `ncarry`, + // note that ncarry could be >= 0x3ffffff + var ncarry = hncarry; + hncarry = 0; + var rword = carry & 0x3ffffff; + var maxJ = Math.min(k, num.length - 1); + for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { + var i = k - j; + var a = self.words[i] | 0; + var b = num.words[j] | 0; + var r = a * b; + + var lo = r & 0x3ffffff; + ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; + lo = (lo + rword) | 0; + rword = lo & 0x3ffffff; + ncarry = (ncarry + (lo >>> 26)) | 0; + + hncarry += ncarry >>> 26; + ncarry &= 0x3ffffff; + } + out.words[k] = rword; + carry = ncarry; + ncarry = hncarry; + } + if (carry !== 0) { + out.words[k] = carry; + } else { + out.length--; + } + + return out.strip(); + } + + function jumboMulTo (self, num, out) { + var fftm = new FFTM(); + return fftm.mulp(self, num, out); + } + + BN.prototype.mulTo = function mulTo (num, out) { + var res; + var len = this.length + num.length; + if (this.length === 10 && num.length === 10) { + res = comb10MulTo(this, num, out); + } else if (len < 63) { + res = smallMulTo(this, num, out); + } else if (len < 1024) { + res = bigMulTo(this, num, out); + } else { + res = jumboMulTo(this, num, out); + } + + return res; + }; + + // Cooley-Tukey algorithm for FFT + // slightly revisited to rely on looping instead of recursion + + function FFTM (x, y) { + this.x = x; + this.y = y; + } + + FFTM.prototype.makeRBT = function makeRBT (N) { + var t = new Array(N); + var l = BN.prototype._countBits(N) - 1; + for (var i = 0; i < N; i++) { + t[i] = this.revBin(i, l, N); + } + + return t; + }; + + // Returns binary-reversed representation of `x` + FFTM.prototype.revBin = function revBin (x, l, N) { + if (x === 0 || x === N - 1) return x; + + var rb = 0; + for (var i = 0; i < l; i++) { + rb |= (x & 1) << (l - i - 1); + x >>= 1; + } + + return rb; + }; + + // Performs "tweedling" phase, therefore 'emulating' + // behaviour of the recursive algorithm + FFTM.prototype.permute = function permute (rbt, rws, iws, rtws, itws, N) { + for (var i = 0; i < N; i++) { + rtws[i] = rws[rbt[i]]; + itws[i] = iws[rbt[i]]; + } + }; + + FFTM.prototype.transform = function transform (rws, iws, rtws, itws, N, rbt) { + this.permute(rbt, rws, iws, rtws, itws, N); + + for (var s = 1; s < N; s <<= 1) { + var l = s << 1; + + var rtwdf = Math.cos(2 * Math.PI / l); + var itwdf = Math.sin(2 * Math.PI / l); + + for (var p = 0; p < N; p += l) { + var rtwdf_ = rtwdf; + var itwdf_ = itwdf; + + for (var j = 0; j < s; j++) { + var re = rtws[p + j]; + var ie = itws[p + j]; + + var ro = rtws[p + j + s]; + var io = itws[p + j + s]; + + var rx = rtwdf_ * ro - itwdf_ * io; + + io = rtwdf_ * io + itwdf_ * ro; + ro = rx; + + rtws[p + j] = re + ro; + itws[p + j] = ie + io; + + rtws[p + j + s] = re - ro; + itws[p + j + s] = ie - io; + + /* jshint maxdepth : false */ + if (j !== l) { + rx = rtwdf * rtwdf_ - itwdf * itwdf_; + + itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_; + rtwdf_ = rx; + } + } + } + } + }; + + FFTM.prototype.guessLen13b = function guessLen13b (n, m) { + var N = Math.max(m, n) | 1; + var odd = N & 1; + var i = 0; + for (N = N / 2 | 0; N; N = N >>> 1) { + i++; + } + + return 1 << i + 1 + odd; + }; + + FFTM.prototype.conjugate = function conjugate (rws, iws, N) { + if (N <= 1) return; + + for (var i = 0; i < N / 2; i++) { + var t = rws[i]; + + rws[i] = rws[N - i - 1]; + rws[N - i - 1] = t; + + t = iws[i]; + + iws[i] = -iws[N - i - 1]; + iws[N - i - 1] = -t; + } + }; + + FFTM.prototype.normalize13b = function normalize13b (ws, N) { + var carry = 0; + for (var i = 0; i < N / 2; i++) { + var w = Math.round(ws[2 * i + 1] / N) * 0x2000 + + Math.round(ws[2 * i] / N) + + carry; + + ws[i] = w & 0x3ffffff; + + if (w < 0x4000000) { + carry = 0; + } else { + carry = w / 0x4000000 | 0; + } + } + + return ws; + }; + + FFTM.prototype.convert13b = function convert13b (ws, len, rws, N) { + var carry = 0; + for (var i = 0; i < len; i++) { + carry = carry + (ws[i] | 0); + + rws[2 * i] = carry & 0x1fff; carry = carry >>> 13; + rws[2 * i + 1] = carry & 0x1fff; carry = carry >>> 13; + } + + // Pad with zeroes + for (i = 2 * len; i < N; ++i) { + rws[i] = 0; + } + + assert(carry === 0); + assert((carry & ~0x1fff) === 0); + }; + + FFTM.prototype.stub = function stub (N) { + var ph = new Array(N); + for (var i = 0; i < N; i++) { + ph[i] = 0; + } + + return ph; + }; + + FFTM.prototype.mulp = function mulp (x, y, out) { + var N = 2 * this.guessLen13b(x.length, y.length); + + var rbt = this.makeRBT(N); + + var _ = this.stub(N); + + var rws = new Array(N); + var rwst = new Array(N); + var iwst = new Array(N); + + var nrws = new Array(N); + var nrwst = new Array(N); + var niwst = new Array(N); + + var rmws = out.words; + rmws.length = N; + + this.convert13b(x.words, x.length, rws, N); + this.convert13b(y.words, y.length, nrws, N); + + this.transform(rws, _, rwst, iwst, N, rbt); + this.transform(nrws, _, nrwst, niwst, N, rbt); + + for (var i = 0; i < N; i++) { + var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i]; + iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i]; + rwst[i] = rx; + } + + this.conjugate(rwst, iwst, N); + this.transform(rwst, iwst, rmws, _, N, rbt); + this.conjugate(rmws, _, N); + this.normalize13b(rmws, N); + + out.negative = x.negative ^ y.negative; + out.length = x.length + y.length; + return out.strip(); + }; + + // Multiply `this` by `num` + BN.prototype.mul = function mul (num) { + var out = new BN(null); + out.words = new Array(this.length + num.length); + return this.mulTo(num, out); + }; + + // Multiply employing FFT + BN.prototype.mulf = function mulf (num) { + var out = new BN(null); + out.words = new Array(this.length + num.length); + return jumboMulTo(this, num, out); + }; + + // In-place Multiplication + BN.prototype.imul = function imul (num) { + return this.clone().mulTo(num, this); + }; + + BN.prototype.imuln = function imuln (num) { + assert(typeof num === 'number'); + assert(num < 0x4000000); + + // Carry + var carry = 0; + for (var i = 0; i < this.length; i++) { + var w = (this.words[i] | 0) * num; + var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); + carry >>= 26; + carry += (w / 0x4000000) | 0; + // NOTE: lo is 27bit maximum + carry += lo >>> 26; + this.words[i] = lo & 0x3ffffff; + } + + if (carry !== 0) { + this.words[i] = carry; + this.length++; + } + + return this; + }; + + BN.prototype.muln = function muln (num) { + return this.clone().imuln(num); + }; + + // `this` * `this` + BN.prototype.sqr = function sqr () { + return this.mul(this); + }; + + // `this` * `this` in-place + BN.prototype.isqr = function isqr () { + return this.imul(this.clone()); + }; + + // Math.pow(`this`, `num`) + BN.prototype.pow = function pow (num) { + var w = toBitArray(num); + if (w.length === 0) return new BN(1); + + // Skip leading zeroes + var res = this; + for (var i = 0; i < w.length; i++, res = res.sqr()) { + if (w[i] !== 0) break; + } + + if (++i < w.length) { + for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { + if (w[i] === 0) continue; + + res = res.mul(q); + } + } + + return res; + }; + + // Shift-left in-place + BN.prototype.iushln = function iushln (bits) { + assert(typeof bits === 'number' && bits >= 0); + var r = bits % 26; + var s = (bits - r) / 26; + var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); + var i; + + if (r !== 0) { + var carry = 0; + + for (i = 0; i < this.length; i++) { + var newCarry = this.words[i] & carryMask; + var c = ((this.words[i] | 0) - newCarry) << r; + this.words[i] = c | carry; + carry = newCarry >>> (26 - r); + } + + if (carry) { + this.words[i] = carry; + this.length++; + } + } + + if (s !== 0) { + for (i = this.length - 1; i >= 0; i--) { + this.words[i + s] = this.words[i]; + } + + for (i = 0; i < s; i++) { + this.words[i] = 0; + } + + this.length += s; + } + + return this.strip(); + }; + + BN.prototype.ishln = function ishln (bits) { + // TODO(indutny): implement me + assert(this.negative === 0); + return this.iushln(bits); + }; + + // Shift-right in-place + // NOTE: `hint` is a lowest bit before trailing zeroes + // NOTE: if `extended` is present - it will be filled with destroyed bits + BN.prototype.iushrn = function iushrn (bits, hint, extended) { + assert(typeof bits === 'number' && bits >= 0); + var h; + if (hint) { + h = (hint - (hint % 26)) / 26; + } else { + h = 0; + } + + var r = bits % 26; + var s = Math.min((bits - r) / 26, this.length); + var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); + var maskedWords = extended; + + h -= s; + h = Math.max(0, h); + + // Extended mode, copy masked part + if (maskedWords) { + for (var i = 0; i < s; i++) { + maskedWords.words[i] = this.words[i]; + } + maskedWords.length = s; + } + + if (s === 0) { + // No-op, we should not move anything at all + } else if (this.length > s) { + this.length -= s; + for (i = 0; i < this.length; i++) { + this.words[i] = this.words[i + s]; + } + } else { + this.words[0] = 0; + this.length = 1; + } + + var carry = 0; + for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { + var word = this.words[i] | 0; + this.words[i] = (carry << (26 - r)) | (word >>> r); + carry = word & mask; + } + + // Push carried bits as a mask + if (maskedWords && carry !== 0) { + maskedWords.words[maskedWords.length++] = carry; + } + + if (this.length === 0) { + this.words[0] = 0; + this.length = 1; + } + + return this.strip(); + }; + + BN.prototype.ishrn = function ishrn (bits, hint, extended) { + // TODO(indutny): implement me + assert(this.negative === 0); + return this.iushrn(bits, hint, extended); + }; + + // Shift-left + BN.prototype.shln = function shln (bits) { + return this.clone().ishln(bits); + }; + + BN.prototype.ushln = function ushln (bits) { + return this.clone().iushln(bits); + }; + + // Shift-right + BN.prototype.shrn = function shrn (bits) { + return this.clone().ishrn(bits); + }; + + BN.prototype.ushrn = function ushrn (bits) { + return this.clone().iushrn(bits); + }; + + // Test if n bit is set + BN.prototype.testn = function testn (bit) { + assert(typeof bit === 'number' && bit >= 0); + var r = bit % 26; + var s = (bit - r) / 26; + var q = 1 << r; + + // Fast case: bit is much higher than all existing words + if (this.length <= s) return false; + + // Check bit and return + var w = this.words[s]; + + return !!(w & q); + }; + + // Return only lowers bits of number (in-place) + BN.prototype.imaskn = function imaskn (bits) { + assert(typeof bits === 'number' && bits >= 0); + var r = bits % 26; + var s = (bits - r) / 26; + + assert(this.negative === 0, 'imaskn works only with positive numbers'); + + if (this.length <= s) { + return this; + } + + if (r !== 0) { + s++; + } + this.length = Math.min(s, this.length); + + if (r !== 0) { + var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); + this.words[this.length - 1] &= mask; + } + + return this.strip(); + }; + + // Return only lowers bits of number + BN.prototype.maskn = function maskn (bits) { + return this.clone().imaskn(bits); + }; + + // Add plain number `num` to `this` + BN.prototype.iaddn = function iaddn (num) { + assert(typeof num === 'number'); + assert(num < 0x4000000); + if (num < 0) return this.isubn(-num); + + // Possible sign change + if (this.negative !== 0) { + if (this.length === 1 && (this.words[0] | 0) < num) { + this.words[0] = num - (this.words[0] | 0); + this.negative = 0; + return this; + } + + this.negative = 0; + this.isubn(num); + this.negative = 1; + return this; + } + + // Add without checks + return this._iaddn(num); + }; + + BN.prototype._iaddn = function _iaddn (num) { + this.words[0] += num; + + // Carry + for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { + this.words[i] -= 0x4000000; + if (i === this.length - 1) { + this.words[i + 1] = 1; + } else { + this.words[i + 1]++; + } + } + this.length = Math.max(this.length, i + 1); + + return this; + }; + + // Subtract plain number `num` from `this` + BN.prototype.isubn = function isubn (num) { + assert(typeof num === 'number'); + assert(num < 0x4000000); + if (num < 0) return this.iaddn(-num); + + if (this.negative !== 0) { + this.negative = 0; + this.iaddn(num); + this.negative = 1; + return this; + } + + this.words[0] -= num; + + if (this.length === 1 && this.words[0] < 0) { + this.words[0] = -this.words[0]; + this.negative = 1; + } else { + // Carry + for (var i = 0; i < this.length && this.words[i] < 0; i++) { + this.words[i] += 0x4000000; + this.words[i + 1] -= 1; + } + } + + return this.strip(); + }; + + BN.prototype.addn = function addn (num) { + return this.clone().iaddn(num); + }; + + BN.prototype.subn = function subn (num) { + return this.clone().isubn(num); + }; + + BN.prototype.iabs = function iabs () { + this.negative = 0; + + return this; + }; + + BN.prototype.abs = function abs () { + return this.clone().iabs(); + }; + + BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) { + var len = num.length + shift; + var i; + + this._expand(len); + + var w; + var carry = 0; + for (i = 0; i < num.length; i++) { + w = (this.words[i + shift] | 0) + carry; + var right = (num.words[i] | 0) * mul; + w -= right & 0x3ffffff; + carry = (w >> 26) - ((right / 0x4000000) | 0); + this.words[i + shift] = w & 0x3ffffff; + } + for (; i < this.length - shift; i++) { + w = (this.words[i + shift] | 0) + carry; + carry = w >> 26; + this.words[i + shift] = w & 0x3ffffff; + } + + if (carry === 0) return this.strip(); + + // Subtraction overflow + assert(carry === -1); + carry = 0; + for (i = 0; i < this.length; i++) { + w = -(this.words[i] | 0) + carry; + carry = w >> 26; + this.words[i] = w & 0x3ffffff; + } + this.negative = 1; + + return this.strip(); + }; + + BN.prototype._wordDiv = function _wordDiv (num, mode) { + var shift = this.length - num.length; + + var a = this.clone(); + var b = num; + + // Normalize + var bhi = b.words[b.length - 1] | 0; + var bhiBits = this._countBits(bhi); + shift = 26 - bhiBits; + if (shift !== 0) { + b = b.ushln(shift); + a.iushln(shift); + bhi = b.words[b.length - 1] | 0; + } + + // Initialize quotient + var m = a.length - b.length; + var q; + + if (mode !== 'mod') { + q = new BN(null); + q.length = m + 1; + q.words = new Array(q.length); + for (var i = 0; i < q.length; i++) { + q.words[i] = 0; + } + } + + var diff = a.clone()._ishlnsubmul(b, 1, m); + if (diff.negative === 0) { + a = diff; + if (q) { + q.words[m] = 1; + } + } + + for (var j = m - 1; j >= 0; j--) { + var qj = (a.words[b.length + j] | 0) * 0x4000000 + + (a.words[b.length + j - 1] | 0); + + // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max + // (0x7ffffff) + qj = Math.min((qj / bhi) | 0, 0x3ffffff); + + a._ishlnsubmul(b, qj, j); + while (a.negative !== 0) { + qj--; + a.negative = 0; + a._ishlnsubmul(b, 1, j); + if (!a.isZero()) { + a.negative ^= 1; + } + } + if (q) { + q.words[j] = qj; + } + } + if (q) { + q.strip(); + } + a.strip(); + + // Denormalize + if (mode !== 'div' && shift !== 0) { + a.iushrn(shift); + } + + return { + div: q || null, + mod: a + }; + }; + + // NOTE: 1) `mode` can be set to `mod` to request mod only, + // to `div` to request div only, or be absent to + // request both div & mod + // 2) `positive` is true if unsigned mod is requested + BN.prototype.divmod = function divmod (num, mode, positive) { + assert(!num.isZero()); + + if (this.isZero()) { + return { + div: new BN(0), + mod: new BN(0) + }; + } + + var div, mod, res; + if (this.negative !== 0 && num.negative === 0) { + res = this.neg().divmod(num, mode); + + if (mode !== 'mod') { + div = res.div.neg(); + } + + if (mode !== 'div') { + mod = res.mod.neg(); + if (positive && mod.negative !== 0) { + mod.iadd(num); + } + } + + return { + div: div, + mod: mod + }; + } + + if (this.negative === 0 && num.negative !== 0) { + res = this.divmod(num.neg(), mode); + + if (mode !== 'mod') { + div = res.div.neg(); + } + + return { + div: div, + mod: res.mod + }; + } + + if ((this.negative & num.negative) !== 0) { + res = this.neg().divmod(num.neg(), mode); + + if (mode !== 'div') { + mod = res.mod.neg(); + if (positive && mod.negative !== 0) { + mod.isub(num); + } + } + + return { + div: res.div, + mod: mod + }; + } + + // Both numbers are positive at this point + + // Strip both numbers to approximate shift value + if (num.length > this.length || this.cmp(num) < 0) { + return { + div: new BN(0), + mod: this + }; + } + + // Very short reduction + if (num.length === 1) { + if (mode === 'div') { + return { + div: this.divn(num.words[0]), + mod: null + }; + } + + if (mode === 'mod') { + return { + div: null, + mod: new BN(this.modn(num.words[0])) + }; + } + + return { + div: this.divn(num.words[0]), + mod: new BN(this.modn(num.words[0])) + }; + } + + return this._wordDiv(num, mode); + }; + + // Find `this` / `num` + BN.prototype.div = function div (num) { + return this.divmod(num, 'div', false).div; + }; + + // Find `this` % `num` + BN.prototype.mod = function mod (num) { + return this.divmod(num, 'mod', false).mod; + }; + + BN.prototype.umod = function umod (num) { + return this.divmod(num, 'mod', true).mod; + }; + + // Find Round(`this` / `num`) + BN.prototype.divRound = function divRound (num) { + var dm = this.divmod(num); + + // Fast case - exact division + if (dm.mod.isZero()) return dm.div; + + var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; + + var half = num.ushrn(1); + var r2 = num.andln(1); + var cmp = mod.cmp(half); + + // Round down + if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div; + + // Round up + return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); + }; + + BN.prototype.modn = function modn (num) { + assert(num <= 0x3ffffff); + var p = (1 << 26) % num; + + var acc = 0; + for (var i = this.length - 1; i >= 0; i--) { + acc = (p * acc + (this.words[i] | 0)) % num; + } + + return acc; + }; + + // In-place division by number + BN.prototype.idivn = function idivn (num) { + assert(num <= 0x3ffffff); + + var carry = 0; + for (var i = this.length - 1; i >= 0; i--) { + var w = (this.words[i] | 0) + carry * 0x4000000; + this.words[i] = (w / num) | 0; + carry = w % num; + } + + return this.strip(); + }; + + BN.prototype.divn = function divn (num) { + return this.clone().idivn(num); + }; + + BN.prototype.egcd = function egcd (p) { + assert(p.negative === 0); + assert(!p.isZero()); + + var x = this; + var y = p.clone(); + + if (x.negative !== 0) { + x = x.umod(p); + } else { + x = x.clone(); + } + + // A * x + B * y = x + var A = new BN(1); + var B = new BN(0); + + // C * x + D * y = y + var C = new BN(0); + var D = new BN(1); + + var g = 0; + + while (x.isEven() && y.isEven()) { + x.iushrn(1); + y.iushrn(1); + ++g; + } + + var yp = y.clone(); + var xp = x.clone(); + + while (!x.isZero()) { + for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1); + if (i > 0) { + x.iushrn(i); + while (i-- > 0) { + if (A.isOdd() || B.isOdd()) { + A.iadd(yp); + B.isub(xp); + } + + A.iushrn(1); + B.iushrn(1); + } + } + + for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); + if (j > 0) { + y.iushrn(j); + while (j-- > 0) { + if (C.isOdd() || D.isOdd()) { + C.iadd(yp); + D.isub(xp); + } + + C.iushrn(1); + D.iushrn(1); + } + } + + if (x.cmp(y) >= 0) { + x.isub(y); + A.isub(C); + B.isub(D); + } else { + y.isub(x); + C.isub(A); + D.isub(B); + } + } + + return { + a: C, + b: D, + gcd: y.iushln(g) + }; + }; + + // This is reduced incarnation of the binary EEA + // above, designated to invert members of the + // _prime_ fields F(p) at a maximal speed + BN.prototype._invmp = function _invmp (p) { + assert(p.negative === 0); + assert(!p.isZero()); + + var a = this; + var b = p.clone(); + + if (a.negative !== 0) { + a = a.umod(p); + } else { + a = a.clone(); + } + + var x1 = new BN(1); + var x2 = new BN(0); + + var delta = b.clone(); + + while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { + for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1); + if (i > 0) { + a.iushrn(i); + while (i-- > 0) { + if (x1.isOdd()) { + x1.iadd(delta); + } + + x1.iushrn(1); + } + } + + for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); + if (j > 0) { + b.iushrn(j); + while (j-- > 0) { + if (x2.isOdd()) { + x2.iadd(delta); + } + + x2.iushrn(1); + } + } + + if (a.cmp(b) >= 0) { + a.isub(b); + x1.isub(x2); + } else { + b.isub(a); + x2.isub(x1); + } + } + + var res; + if (a.cmpn(1) === 0) { + res = x1; + } else { + res = x2; + } + + if (res.cmpn(0) < 0) { + res.iadd(p); + } + + return res; + }; + + BN.prototype.gcd = function gcd (num) { + if (this.isZero()) return num.abs(); + if (num.isZero()) return this.abs(); + + var a = this.clone(); + var b = num.clone(); + a.negative = 0; + b.negative = 0; + + // Remove common factor of two + for (var shift = 0; a.isEven() && b.isEven(); shift++) { + a.iushrn(1); + b.iushrn(1); + } + + do { + while (a.isEven()) { + a.iushrn(1); + } + while (b.isEven()) { + b.iushrn(1); + } + + var r = a.cmp(b); + if (r < 0) { + // Swap `a` and `b` to make `a` always bigger than `b` + var t = a; + a = b; + b = t; + } else if (r === 0 || b.cmpn(1) === 0) { + break; + } + + a.isub(b); + } while (true); + + return b.iushln(shift); + }; + + // Invert number in the field F(num) + BN.prototype.invm = function invm (num) { + return this.egcd(num).a.umod(num); + }; + + BN.prototype.isEven = function isEven () { + return (this.words[0] & 1) === 0; + }; + + BN.prototype.isOdd = function isOdd () { + return (this.words[0] & 1) === 1; + }; + + // And first word and num + BN.prototype.andln = function andln (num) { + return this.words[0] & num; + }; + + // Increment at the bit position in-line + BN.prototype.bincn = function bincn (bit) { + assert(typeof bit === 'number'); + var r = bit % 26; + var s = (bit - r) / 26; + var q = 1 << r; + + // Fast case: bit is much higher than all existing words + if (this.length <= s) { + this._expand(s + 1); + this.words[s] |= q; + return this; + } + + // Add bit and propagate, if needed + var carry = q; + for (var i = s; carry !== 0 && i < this.length; i++) { + var w = this.words[i] | 0; + w += carry; + carry = w >>> 26; + w &= 0x3ffffff; + this.words[i] = w; + } + if (carry !== 0) { + this.words[i] = carry; + this.length++; + } + return this; + }; + + BN.prototype.isZero = function isZero () { + return this.length === 1 && this.words[0] === 0; + }; + + BN.prototype.cmpn = function cmpn (num) { + var negative = num < 0; + + if (this.negative !== 0 && !negative) return -1; + if (this.negative === 0 && negative) return 1; + + this.strip(); + + var res; + if (this.length > 1) { + res = 1; + } else { + if (negative) { + num = -num; + } + + assert(num <= 0x3ffffff, 'Number is too big'); + + var w = this.words[0] | 0; + res = w === num ? 0 : w < num ? -1 : 1; + } + if (this.negative !== 0) return -res | 0; + return res; + }; + + // Compare two numbers and return: + // 1 - if `this` > `num` + // 0 - if `this` == `num` + // -1 - if `this` < `num` + BN.prototype.cmp = function cmp (num) { + if (this.negative !== 0 && num.negative === 0) return -1; + if (this.negative === 0 && num.negative !== 0) return 1; + + var res = this.ucmp(num); + if (this.negative !== 0) return -res | 0; + return res; + }; + + // Unsigned comparison + BN.prototype.ucmp = function ucmp (num) { + // At this point both numbers have the same sign + if (this.length > num.length) return 1; + if (this.length < num.length) return -1; + + var res = 0; + for (var i = this.length - 1; i >= 0; i--) { + var a = this.words[i] | 0; + var b = num.words[i] | 0; + + if (a === b) continue; + if (a < b) { + res = -1; + } else if (a > b) { + res = 1; + } + break; + } + return res; + }; + + BN.prototype.gtn = function gtn (num) { + return this.cmpn(num) === 1; + }; + + BN.prototype.gt = function gt (num) { + return this.cmp(num) === 1; + }; + + BN.prototype.gten = function gten (num) { + return this.cmpn(num) >= 0; + }; + + BN.prototype.gte = function gte (num) { + return this.cmp(num) >= 0; + }; + + BN.prototype.ltn = function ltn (num) { + return this.cmpn(num) === -1; + }; + + BN.prototype.lt = function lt (num) { + return this.cmp(num) === -1; + }; + + BN.prototype.lten = function lten (num) { + return this.cmpn(num) <= 0; + }; + + BN.prototype.lte = function lte (num) { + return this.cmp(num) <= 0; + }; + + BN.prototype.eqn = function eqn (num) { + return this.cmpn(num) === 0; + }; + + BN.prototype.eq = function eq (num) { + return this.cmp(num) === 0; + }; + + // + // A reduce context, could be using montgomery or something better, depending + // on the `m` itself. + // + BN.red = function red (num) { + return new Red(num); + }; + + BN.prototype.toRed = function toRed (ctx) { + assert(!this.red, 'Already a number in reduction context'); + assert(this.negative === 0, 'red works only with positives'); + return ctx.convertTo(this)._forceRed(ctx); + }; + + BN.prototype.fromRed = function fromRed () { + assert(this.red, 'fromRed works only with numbers in reduction context'); + return this.red.convertFrom(this); + }; + + BN.prototype._forceRed = function _forceRed (ctx) { + this.red = ctx; + return this; + }; + + BN.prototype.forceRed = function forceRed (ctx) { + assert(!this.red, 'Already a number in reduction context'); + return this._forceRed(ctx); + }; + + BN.prototype.redAdd = function redAdd (num) { + assert(this.red, 'redAdd works only with red numbers'); + return this.red.add(this, num); + }; + + BN.prototype.redIAdd = function redIAdd (num) { + assert(this.red, 'redIAdd works only with red numbers'); + return this.red.iadd(this, num); + }; + + BN.prototype.redSub = function redSub (num) { + assert(this.red, 'redSub works only with red numbers'); + return this.red.sub(this, num); + }; + + BN.prototype.redISub = function redISub (num) { + assert(this.red, 'redISub works only with red numbers'); + return this.red.isub(this, num); + }; + + BN.prototype.redShl = function redShl (num) { + assert(this.red, 'redShl works only with red numbers'); + return this.red.shl(this, num); + }; + + BN.prototype.redMul = function redMul (num) { + assert(this.red, 'redMul works only with red numbers'); + this.red._verify2(this, num); + return this.red.mul(this, num); + }; + + BN.prototype.redIMul = function redIMul (num) { + assert(this.red, 'redMul works only with red numbers'); + this.red._verify2(this, num); + return this.red.imul(this, num); + }; + + BN.prototype.redSqr = function redSqr () { + assert(this.red, 'redSqr works only with red numbers'); + this.red._verify1(this); + return this.red.sqr(this); + }; + + BN.prototype.redISqr = function redISqr () { + assert(this.red, 'redISqr works only with red numbers'); + this.red._verify1(this); + return this.red.isqr(this); + }; + + // Square root over p + BN.prototype.redSqrt = function redSqrt () { + assert(this.red, 'redSqrt works only with red numbers'); + this.red._verify1(this); + return this.red.sqrt(this); + }; + + BN.prototype.redInvm = function redInvm () { + assert(this.red, 'redInvm works only with red numbers'); + this.red._verify1(this); + return this.red.invm(this); + }; + + // Return negative clone of `this` % `red modulo` + BN.prototype.redNeg = function redNeg () { + assert(this.red, 'redNeg works only with red numbers'); + this.red._verify1(this); + return this.red.neg(this); + }; + + BN.prototype.redPow = function redPow (num) { + assert(this.red && !num.red, 'redPow(normalNum)'); + this.red._verify1(this); + return this.red.pow(this, num); + }; + + // Prime numbers with efficient reduction + var primes = { + k256: null, + p224: null, + p192: null, + p25519: null + }; + + // Pseudo-Mersenne prime + function MPrime (name, p) { + // P = 2 ^ N - K + this.name = name; + this.p = new BN(p, 16); + this.n = this.p.bitLength(); + this.k = new BN(1).iushln(this.n).isub(this.p); + + this.tmp = this._tmp(); + } + + MPrime.prototype._tmp = function _tmp () { + var tmp = new BN(null); + tmp.words = new Array(Math.ceil(this.n / 13)); + return tmp; + }; + + MPrime.prototype.ireduce = function ireduce (num) { + // Assumes that `num` is less than `P^2` + // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) + var r = num; + var rlen; + + do { + this.split(r, this.tmp); + r = this.imulK(r); + r = r.iadd(this.tmp); + rlen = r.bitLength(); + } while (rlen > this.n); + + var cmp = rlen < this.n ? -1 : r.ucmp(this.p); + if (cmp === 0) { + r.words[0] = 0; + r.length = 1; + } else if (cmp > 0) { + r.isub(this.p); + } else { + r.strip(); + } + + return r; + }; + + MPrime.prototype.split = function split (input, out) { + input.iushrn(this.n, 0, out); + }; + + MPrime.prototype.imulK = function imulK (num) { + return num.imul(this.k); + }; + + function K256 () { + MPrime.call( + this, + 'k256', + 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); + } + inherits(K256, MPrime); + + K256.prototype.split = function split (input, output) { + // 256 = 9 * 26 + 22 + var mask = 0x3fffff; + + var outLen = Math.min(input.length, 9); + for (var i = 0; i < outLen; i++) { + output.words[i] = input.words[i]; + } + output.length = outLen; + + if (input.length <= 9) { + input.words[0] = 0; + input.length = 1; return; } - // Fallback to ordinary array - for (var i = 0; i < len; i++) { - dest[dest_offs + i] = src[src_offs + i]; - } - }, - // Join array of chunks to single array. - flattenChunks: function (chunks) { - var i, l, len, pos, chunk, result; - // calculate data length - len = 0; - for (i = 0, l = chunks.length; i < l; i++) { - len += chunks[i].length; + // Shift by 9 limbs + var prev = input.words[9]; + output.words[output.length++] = prev & mask; + + for (i = 10; i < input.length; i++) { + var next = input.words[i] | 0; + input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22); + prev = next; + } + prev >>>= 22; + input.words[i - 10] = prev; + if (prev === 0 && input.length > 10) { + input.length -= 10; + } else { + input.length -= 9; + } + }; + + K256.prototype.imulK = function imulK (num) { + // K = 0x1000003d1 = [ 0x40, 0x3d1 ] + num.words[num.length] = 0; + num.words[num.length + 1] = 0; + num.length += 2; + + // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 + var lo = 0; + for (var i = 0; i < num.length; i++) { + var w = num.words[i] | 0; + lo += w * 0x3d1; + num.words[i] = lo & 0x3ffffff; + lo = w * 0x40 + ((lo / 0x4000000) | 0); } - // join chunks - result = new Uint8Array(len); - pos = 0; - for (i = 0, l = chunks.length; i < l; i++) { - chunk = chunks[i]; - result.set(chunk, pos); - pos += chunk.length; - } - - return result; - } -}; - -var fnUntyped = { - arraySet: function (dest, src, src_offs, len, dest_offs) { - for (var i = 0; i < len; i++) { - dest[dest_offs + i] = src[src_offs + i]; - } - }, - // Join array of chunks to single array. - flattenChunks: function (chunks) { - return [].concat.apply([], chunks); - } -}; - - -// Enable/Disable typed arrays use, for testing -// -exports.setTyped = function (on) { - if (on) { - exports.Buf8 = Uint8Array; - exports.Buf16 = Uint16Array; - exports.Buf32 = Int32Array; - exports.assign(exports, fnTyped); - } else { - exports.Buf8 = Array; - exports.Buf16 = Array; - exports.Buf32 = Array; - exports.assign(exports, fnUntyped); - } -}; - -exports.setTyped(TYPED_OK); - -},{}],21:[function(require,module,exports){ -'use strict'; - -// Note: adler32 takes 12% for level 0 and 2% for level 6. -// It doesn't worth to make additional optimizationa as in original. -// Small size is preferable. - -function adler32(adler, buf, len, pos) { - var s1 = (adler & 0xffff) |0, - s2 = ((adler >>> 16) & 0xffff) |0, - n = 0; - - while (len !== 0) { - // Set limit ~ twice less than 5552, to keep - // s2 in 31-bits, because we force signed ints. - // in other case %= will fail. - n = len > 2000 ? 2000 : len; - len -= n; - - do { - s1 = (s1 + buf[pos++]) |0; - s2 = (s2 + s1) |0; - } while (--n); - - s1 %= 65521; - s2 %= 65521; - } - - return (s1 | (s2 << 16)) |0; -} - - -module.exports = adler32; - -},{}],22:[function(require,module,exports){ -'use strict'; - - -module.exports = { - - /* Allowed flush values; see deflate() and inflate() below for details */ - Z_NO_FLUSH: 0, - Z_PARTIAL_FLUSH: 1, - Z_SYNC_FLUSH: 2, - Z_FULL_FLUSH: 3, - Z_FINISH: 4, - Z_BLOCK: 5, - Z_TREES: 6, - - /* Return codes for the compression/decompression functions. Negative values - * are errors, positive values are used for special but normal events. - */ - Z_OK: 0, - Z_STREAM_END: 1, - Z_NEED_DICT: 2, - Z_ERRNO: -1, - Z_STREAM_ERROR: -2, - Z_DATA_ERROR: -3, - //Z_MEM_ERROR: -4, - Z_BUF_ERROR: -5, - //Z_VERSION_ERROR: -6, - - /* compression levels */ - Z_NO_COMPRESSION: 0, - Z_BEST_SPEED: 1, - Z_BEST_COMPRESSION: 9, - Z_DEFAULT_COMPRESSION: -1, - - - Z_FILTERED: 1, - Z_HUFFMAN_ONLY: 2, - Z_RLE: 3, - Z_FIXED: 4, - Z_DEFAULT_STRATEGY: 0, - - /* Possible values of the data_type field (though see inflate()) */ - Z_BINARY: 0, - Z_TEXT: 1, - //Z_ASCII: 1, // = Z_TEXT (deprecated) - Z_UNKNOWN: 2, - - /* The deflate compression method */ - Z_DEFLATED: 8 - //Z_NULL: null // Use -1 or null inline, depending on var type -}; - -},{}],23:[function(require,module,exports){ -'use strict'; - -// Note: we can't get significant speed boost here. -// So write code to minimize size - no pregenerated tables -// and array tools dependencies. - - -// Use ordinary array, since untyped makes no boost here -function makeTable() { - var c, table = []; - - for (var n = 0; n < 256; n++) { - c = n; - for (var k = 0; k < 8; k++) { - c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); - } - table[n] = c; - } - - return table; -} - -// Create table on load. Just 255 signed longs. Not a problem. -var crcTable = makeTable(); - - -function crc32(crc, buf, len, pos) { - var t = crcTable, - end = pos + len; - - crc ^= -1; - - for (var i = pos; i < end; i++) { - crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF]; - } - - return (crc ^ (-1)); // >>> 0; -} - - -module.exports = crc32; - -},{}],24:[function(require,module,exports){ -'use strict'; - -var utils = require('../utils/common'); -var trees = require('./trees'); -var adler32 = require('./adler32'); -var crc32 = require('./crc32'); -var msg = require('./messages'); - -/* Public constants ==========================================================*/ -/* ===========================================================================*/ - - -/* Allowed flush values; see deflate() and inflate() below for details */ -var Z_NO_FLUSH = 0; -var Z_PARTIAL_FLUSH = 1; -//var Z_SYNC_FLUSH = 2; -var Z_FULL_FLUSH = 3; -var Z_FINISH = 4; -var Z_BLOCK = 5; -//var Z_TREES = 6; - - -/* Return codes for the compression/decompression functions. Negative values - * are errors, positive values are used for special but normal events. - */ -var Z_OK = 0; -var Z_STREAM_END = 1; -//var Z_NEED_DICT = 2; -//var Z_ERRNO = -1; -var Z_STREAM_ERROR = -2; -var Z_DATA_ERROR = -3; -//var Z_MEM_ERROR = -4; -var Z_BUF_ERROR = -5; -//var Z_VERSION_ERROR = -6; - - -/* compression levels */ -//var Z_NO_COMPRESSION = 0; -//var Z_BEST_SPEED = 1; -//var Z_BEST_COMPRESSION = 9; -var Z_DEFAULT_COMPRESSION = -1; - - -var Z_FILTERED = 1; -var Z_HUFFMAN_ONLY = 2; -var Z_RLE = 3; -var Z_FIXED = 4; -var Z_DEFAULT_STRATEGY = 0; - -/* Possible values of the data_type field (though see inflate()) */ -//var Z_BINARY = 0; -//var Z_TEXT = 1; -//var Z_ASCII = 1; // = Z_TEXT -var Z_UNKNOWN = 2; - - -/* The deflate compression method */ -var Z_DEFLATED = 8; - -/*============================================================================*/ - - -var MAX_MEM_LEVEL = 9; -/* Maximum value for memLevel in deflateInit2 */ -var MAX_WBITS = 15; -/* 32K LZ77 window */ -var DEF_MEM_LEVEL = 8; - - -var LENGTH_CODES = 29; -/* number of length codes, not counting the special END_BLOCK code */ -var LITERALS = 256; -/* number of literal bytes 0..255 */ -var L_CODES = LITERALS + 1 + LENGTH_CODES; -/* number of Literal or Length codes, including the END_BLOCK code */ -var D_CODES = 30; -/* number of distance codes */ -var BL_CODES = 19; -/* number of codes used to transfer the bit lengths */ -var HEAP_SIZE = 2 * L_CODES + 1; -/* maximum heap size */ -var MAX_BITS = 15; -/* All codes must not exceed MAX_BITS bits */ - -var MIN_MATCH = 3; -var MAX_MATCH = 258; -var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1); - -var PRESET_DICT = 0x20; - -var INIT_STATE = 42; -var EXTRA_STATE = 69; -var NAME_STATE = 73; -var COMMENT_STATE = 91; -var HCRC_STATE = 103; -var BUSY_STATE = 113; -var FINISH_STATE = 666; - -var BS_NEED_MORE = 1; /* block not completed, need more input or more output */ -var BS_BLOCK_DONE = 2; /* block flush performed */ -var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */ -var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */ - -var OS_CODE = 0x03; // Unix :) . Don't detect, use this default. - -function err(strm, errorCode) { - strm.msg = msg[errorCode]; - return errorCode; -} - -function rank(f) { - return ((f) << 1) - ((f) > 4 ? 9 : 0); -} - -function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } - - -/* ========================================================================= - * Flush as much pending output as possible. All deflate() output goes - * through this function so some applications may wish to modify it - * to avoid allocating a large strm->output buffer and copying into it. - * (See also read_buf()). - */ -function flush_pending(strm) { - var s = strm.state; - - //_tr_flush_bits(s); - var len = s.pending; - if (len > strm.avail_out) { - len = strm.avail_out; - } - if (len === 0) { return; } - - utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out); - strm.next_out += len; - s.pending_out += len; - strm.total_out += len; - strm.avail_out -= len; - s.pending -= len; - if (s.pending === 0) { - s.pending_out = 0; - } -} - - -function flush_block_only(s, last) { - trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last); - s.block_start = s.strstart; - flush_pending(s.strm); -} - - -function put_byte(s, b) { - s.pending_buf[s.pending++] = b; -} - - -/* ========================================================================= - * Put a short in the pending buffer. The 16-bit value is put in MSB order. - * IN assertion: the stream state is correct and there is enough room in - * pending_buf. - */ -function putShortMSB(s, b) { -// put_byte(s, (Byte)(b >> 8)); -// put_byte(s, (Byte)(b & 0xff)); - s.pending_buf[s.pending++] = (b >>> 8) & 0xff; - s.pending_buf[s.pending++] = b & 0xff; -} - - -/* =========================================================================== - * Read a new buffer from the current input stream, update the adler32 - * and total number of bytes read. All deflate() input goes through - * this function so some applications may wish to modify it to avoid - * allocating a large strm->input buffer and copying from it. - * (See also flush_pending()). - */ -function read_buf(strm, buf, start, size) { - var len = strm.avail_in; - - if (len > size) { len = size; } - if (len === 0) { return 0; } - - strm.avail_in -= len; - - // zmemcpy(buf, strm->next_in, len); - utils.arraySet(buf, strm.input, strm.next_in, len, start); - if (strm.state.wrap === 1) { - strm.adler = adler32(strm.adler, buf, len, start); - } - - else if (strm.state.wrap === 2) { - strm.adler = crc32(strm.adler, buf, len, start); - } - - strm.next_in += len; - strm.total_in += len; - - return len; -} - - -/* =========================================================================== - * Set match_start to the longest match starting at the given string and - * return its length. Matches shorter or equal to prev_length are discarded, - * in which case the result is equal to prev_length and match_start is - * garbage. - * IN assertions: cur_match is the head of the hash chain for the current - * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 - * OUT assertion: the match length is not greater than s->lookahead. - */ -function longest_match(s, cur_match) { - var chain_length = s.max_chain_length; /* max hash chain length */ - var scan = s.strstart; /* current string */ - var match; /* matched string */ - var len; /* length of current match */ - var best_len = s.prev_length; /* best match length so far */ - var nice_match = s.nice_match; /* stop if match long enough */ - var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ? - s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/; - - var _win = s.window; // shortcut - - var wmask = s.w_mask; - var prev = s.prev; - - /* Stop when cur_match becomes <= limit. To simplify the code, - * we prevent matches with the string of window index 0. - */ - - var strend = s.strstart + MAX_MATCH; - var scan_end1 = _win[scan + best_len - 1]; - var scan_end = _win[scan + best_len]; - - /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. - * It is easy to get rid of this optimization if necessary. - */ - // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); - - /* Do not waste too much time if we already have a good match: */ - if (s.prev_length >= s.good_match) { - chain_length >>= 2; - } - /* Do not look for matches beyond the end of the input. This is necessary - * to make deflate deterministic. - */ - if (nice_match > s.lookahead) { nice_match = s.lookahead; } - - // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); - - do { - // Assert(cur_match < s->strstart, "no future"); - match = cur_match; - - /* Skip to next match if the match length cannot increase - * or if the match length is less than 2. Note that the checks below - * for insufficient lookahead only occur occasionally for performance - * reasons. Therefore uninitialized memory will be accessed, and - * conditional jumps will be made that depend on those values. - * However the length of the match is limited to the lookahead, so - * the output of deflate is not affected by the uninitialized values. - */ - - if (_win[match + best_len] !== scan_end || - _win[match + best_len - 1] !== scan_end1 || - _win[match] !== _win[scan] || - _win[++match] !== _win[scan + 1]) { - continue; - } - - /* The check at best_len-1 can be removed because it will be made - * again later. (This heuristic is not always a win.) - * It is not necessary to compare scan[2] and match[2] since they - * are always equal when the other bytes match, given that - * the hash keys are equal and that HASH_BITS >= 8. - */ - scan += 2; - match++; - // Assert(*scan == *match, "match[2]?"); - - /* We check for insufficient lookahead only every 8th comparison; - * the 256th check will be made at strstart+258. - */ - do { - /*jshint noempty:false*/ - } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && - _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && - _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && - _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && - scan < strend); - - // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); - - len = MAX_MATCH - (strend - scan); - scan = strend - MAX_MATCH; - - if (len > best_len) { - s.match_start = cur_match; - best_len = len; - if (len >= nice_match) { - break; + // Fast length reduction + if (num.words[num.length - 1] === 0) { + num.length--; + if (num.words[num.length - 1] === 0) { + num.length--; } - scan_end1 = _win[scan + best_len - 1]; - scan_end = _win[scan + best_len]; } - } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0); + return num; + }; - if (best_len <= s.lookahead) { - return best_len; + function P224 () { + MPrime.call( + this, + 'p224', + 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); } - return s.lookahead; -} + inherits(P224, MPrime); + function P192 () { + MPrime.call( + this, + 'p192', + 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); + } + inherits(P192, MPrime); -/* =========================================================================== - * Fill the window when the lookahead becomes insufficient. - * Updates strstart and lookahead. - * - * IN assertion: lookahead < MIN_LOOKAHEAD - * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD - * At least one byte has been read, or avail_in == 0; reads are - * performed for at least two bytes (required for the zip translate_eol - * option -- not supported here). - */ -function fill_window(s) { - var _w_size = s.w_size; - var p, n, m, more, str; + function P25519 () { + // 2 ^ 255 - 19 + MPrime.call( + this, + '25519', + '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); + } + inherits(P25519, MPrime); - //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); + P25519.prototype.imulK = function imulK (num) { + // K = 0x13 + var carry = 0; + for (var i = 0; i < num.length; i++) { + var hi = (num.words[i] | 0) * 0x13 + carry; + var lo = hi & 0x3ffffff; + hi >>>= 26; - do { - more = s.window_size - s.lookahead - s.strstart; + num.words[i] = lo; + carry = hi; + } + if (carry !== 0) { + num.words[num.length++] = carry; + } + return num; + }; - // JS ints have 32 bit, block below not needed - /* Deal with !@#$% 64K limit: */ - //if (sizeof(int) <= 2) { - // if (more == 0 && s->strstart == 0 && s->lookahead == 0) { - // more = wsize; + // Exported mostly for testing purposes, use plain name instead + BN._prime = function prime (name) { + // Cached version of prime + if (primes[name]) return primes[name]; + + var prime; + if (name === 'k256') { + prime = new K256(); + } else if (name === 'p224') { + prime = new P224(); + } else if (name === 'p192') { + prime = new P192(); + } else if (name === 'p25519') { + prime = new P25519(); + } else { + throw new Error('Unknown prime ' + name); + } + primes[name] = prime; + + return prime; + }; + + // + // Base reduction engine + // + function Red (m) { + if (typeof m === 'string') { + var prime = BN._prime(m); + this.m = prime.p; + this.prime = prime; + } else { + assert(m.gtn(1), 'modulus must be greater than 1'); + this.m = m; + this.prime = null; + } + } + + Red.prototype._verify1 = function _verify1 (a) { + assert(a.negative === 0, 'red works only with positives'); + assert(a.red, 'red works only with red numbers'); + }; + + Red.prototype._verify2 = function _verify2 (a, b) { + assert((a.negative | b.negative) === 0, 'red works only with positives'); + assert(a.red && a.red === b.red, + 'red works only with red numbers'); + }; + + Red.prototype.imod = function imod (a) { + if (this.prime) return this.prime.ireduce(a)._forceRed(this); + return a.umod(this.m)._forceRed(this); + }; + + Red.prototype.neg = function neg (a) { + if (a.isZero()) { + return a.clone(); + } + + return this.m.sub(a)._forceRed(this); + }; + + Red.prototype.add = function add (a, b) { + this._verify2(a, b); + + var res = a.add(b); + if (res.cmp(this.m) >= 0) { + res.isub(this.m); + } + return res._forceRed(this); + }; + + Red.prototype.iadd = function iadd (a, b) { + this._verify2(a, b); + + var res = a.iadd(b); + if (res.cmp(this.m) >= 0) { + res.isub(this.m); + } + return res; + }; + + Red.prototype.sub = function sub (a, b) { + this._verify2(a, b); + + var res = a.sub(b); + if (res.cmpn(0) < 0) { + res.iadd(this.m); + } + return res._forceRed(this); + }; + + Red.prototype.isub = function isub (a, b) { + this._verify2(a, b); + + var res = a.isub(b); + if (res.cmpn(0) < 0) { + res.iadd(this.m); + } + return res; + }; + + Red.prototype.shl = function shl (a, num) { + this._verify1(a); + return this.imod(a.ushln(num)); + }; + + Red.prototype.imul = function imul (a, b) { + this._verify2(a, b); + return this.imod(a.imul(b)); + }; + + Red.prototype.mul = function mul (a, b) { + this._verify2(a, b); + return this.imod(a.mul(b)); + }; + + Red.prototype.isqr = function isqr (a) { + return this.imul(a, a.clone()); + }; + + Red.prototype.sqr = function sqr (a) { + return this.mul(a, a); + }; + + Red.prototype.sqrt = function sqrt (a) { + if (a.isZero()) return a.clone(); + + var mod3 = this.m.andln(3); + assert(mod3 % 2 === 1); + + // Fast case + if (mod3 === 3) { + var pow = this.m.add(new BN(1)).iushrn(2); + return this.pow(a, pow); + } + + // Tonelli-Shanks algorithm (Totally unoptimized and slow) // - // } else if (more == (unsigned)(-1)) { - // /* Very unlikely, but possible on 16 bit machine if - // * strstart == 0 && lookahead == 1 (input done a byte at time) - // */ - // more--; - // } - //} - - - /* If the window is almost full and there is insufficient lookahead, - * move the upper half to the lower one to make room in the upper half. - */ - if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) { - - utils.arraySet(s.window, s.window, _w_size, _w_size, 0); - s.match_start -= _w_size; - s.strstart -= _w_size; - /* we now have strstart >= MAX_DIST */ - s.block_start -= _w_size; - - /* Slide the hash table (could be avoided with 32 bit values - at the expense of memory usage). We slide even when level == 0 - to keep the hash table consistent if we switch back to level > 0 - later. (Using level 0 permanently is not an optimal usage of - zlib, so we don't care about this pathological case.) - */ - - n = s.hash_size; - p = n; - do { - m = s.head[--p]; - s.head[p] = (m >= _w_size ? m - _w_size : 0); - } while (--n); - - n = _w_size; - p = n; - do { - m = s.prev[--p]; - s.prev[p] = (m >= _w_size ? m - _w_size : 0); - /* If n is not on any hash chain, prev[n] is garbage but - * its value will never be used. - */ - } while (--n); - - more += _w_size; + // Find Q and S, that Q * 2 ^ S = (P - 1) + var q = this.m.subn(1); + var s = 0; + while (!q.isZero() && q.andln(1) === 0) { + s++; + q.iushrn(1); } - if (s.strm.avail_in === 0) { - break; + assert(!q.isZero()); + + var one = new BN(1).toRed(this); + var nOne = one.redNeg(); + + // Find quadratic non-residue + // NOTE: Max is such because of generalized Riemann hypothesis. + var lpow = this.m.subn(1).iushrn(1); + var z = this.m.bitLength(); + z = new BN(2 * z * z).toRed(this); + + while (this.pow(z, lpow).cmp(nOne) !== 0) { + z.redIAdd(nOne); } - /* If there was no sliding: - * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && - * more == window_size - lookahead - strstart - * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) - * => more >= window_size - 2*WSIZE + 2 - * In the BIG_MEM or MMAP case (not yet supported), - * window_size == input_size + MIN_LOOKAHEAD && - * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. - * Otherwise, window_size == 2*WSIZE so more >= 2. - * If there was sliding, more >= WSIZE. So in all cases, more >= 2. - */ - //Assert(more >= 2, "more < 2"); - n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more); - s.lookahead += n; - - /* Initialize the hash value now that we have some input: */ - if (s.lookahead + s.insert >= MIN_MATCH) { - str = s.strstart - s.insert; - s.ins_h = s.window[str]; - - /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask; -//#if MIN_MATCH != 3 -// Call update_hash() MIN_MATCH-3 more times -//#endif - while (s.insert) { - /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask; - - s.prev[str & s.w_mask] = s.head[s.ins_h]; - s.head[s.ins_h] = str; - str++; - s.insert--; - if (s.lookahead + s.insert < MIN_MATCH) { - break; - } + var c = this.pow(z, q); + var r = this.pow(a, q.addn(1).iushrn(1)); + var t = this.pow(a, q); + var m = s; + while (t.cmp(one) !== 0) { + var tmp = t; + for (var i = 0; tmp.cmp(one) !== 0; i++) { + tmp = tmp.redSqr(); } - } - /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, - * but this is not important since only literal bytes will be emitted. - */ + assert(i < m); + var b = this.pow(c, new BN(1).iushln(m - i - 1)); - } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0); - - /* If the WIN_INIT bytes after the end of the current data have never been - * written, then zero those bytes in order to avoid memory check reports of - * the use of uninitialized (or uninitialised as Julian writes) bytes by - * the longest match routines. Update the high water mark for the next - * time through here. WIN_INIT is set to MAX_MATCH since the longest match - * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. - */ -// if (s.high_water < s.window_size) { -// var curr = s.strstart + s.lookahead; -// var init = 0; -// -// if (s.high_water < curr) { -// /* Previous high water mark below current data -- zero WIN_INIT -// * bytes or up to end of window, whichever is less. -// */ -// init = s.window_size - curr; -// if (init > WIN_INIT) -// init = WIN_INIT; -// zmemzero(s->window + curr, (unsigned)init); -// s->high_water = curr + init; -// } -// else if (s->high_water < (ulg)curr + WIN_INIT) { -// /* High water mark at or above current data, but below current data -// * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up -// * to end of window, whichever is less. -// */ -// init = (ulg)curr + WIN_INIT - s->high_water; -// if (init > s->window_size - s->high_water) -// init = s->window_size - s->high_water; -// zmemzero(s->window + s->high_water, (unsigned)init); -// s->high_water += init; -// } -// } -// -// Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, -// "not enough room for search"); -} - -/* =========================================================================== - * Copy without compression as much as possible from the input stream, return - * the current block state. - * This function does not insert new strings in the dictionary since - * uncompressible data is probably not useful. This function is used - * only for the level=0 compression option. - * NOTE: this function should be optimized to avoid extra copying from - * window to pending_buf. - */ -function deflate_stored(s, flush) { - /* Stored blocks are limited to 0xffff bytes, pending_buf is limited - * to pending_buf_size, and each stored block has a 5 byte header: - */ - var max_block_size = 0xffff; - - if (max_block_size > s.pending_buf_size - 5) { - max_block_size = s.pending_buf_size - 5; - } - - /* Copy as much as possible from input to output: */ - for (;;) { - /* Fill the window as much as possible: */ - if (s.lookahead <= 1) { - - //Assert(s->strstart < s->w_size+MAX_DIST(s) || - // s->block_start >= (long)s->w_size, "slide too late"); -// if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) || -// s.block_start >= s.w_size)) { -// throw new Error("slide too late"); -// } - - fill_window(s); - if (s.lookahead === 0 && flush === Z_NO_FLUSH) { - return BS_NEED_MORE; - } - - if (s.lookahead === 0) { - break; - } - /* flush the current block */ - } - //Assert(s->block_start >= 0L, "block gone"); -// if (s.block_start < 0) throw new Error("block gone"); - - s.strstart += s.lookahead; - s.lookahead = 0; - - /* Emit a stored block if pending_buf will be full: */ - var max_start = s.block_start + max_block_size; - - if (s.strstart === 0 || s.strstart >= max_start) { - /* strstart == 0 is possible when wraparound on 16-bit machine */ - s.lookahead = s.strstart - max_start; - s.strstart = max_start; - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - - - } - /* Flush if we may have to slide, otherwise block_start may become - * negative and the data will be gone: - */ - if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } - } - - s.insert = 0; - - if (flush === Z_FINISH) { - /*** FLUSH_BLOCK(s, 1); ***/ - flush_block_only(s, true); - if (s.strm.avail_out === 0) { - return BS_FINISH_STARTED; - } - /***/ - return BS_FINISH_DONE; - } - - if (s.strstart > s.block_start) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } - - return BS_NEED_MORE; -} - -/* =========================================================================== - * Compress as much as possible from the input stream, return the current - * block state. - * This function does not perform lazy evaluation of matches and inserts - * new strings in the dictionary only for unmatched strings or for short - * matches. It is used only for the fast compression options. - */ -function deflate_fast(s, flush) { - var hash_head; /* head of the hash chain */ - var bflush; /* set if current block must be flushed */ - - for (;;) { - /* Make sure that we always have enough lookahead, except - * at the end of the input file. We need MAX_MATCH bytes - * for the next match, plus MIN_MATCH bytes to insert the - * string following the next match. - */ - if (s.lookahead < MIN_LOOKAHEAD) { - fill_window(s); - if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) { - return BS_NEED_MORE; - } - if (s.lookahead === 0) { - break; /* flush the current block */ - } + r = r.redMul(b); + c = b.redSqr(); + t = t.redMul(c); + m = i; } - /* Insert the string window[strstart .. strstart+2] in the - * dictionary, and set hash_head to the head of the hash chain: - */ - hash_head = 0/*NIL*/; - if (s.lookahead >= MIN_MATCH) { - /*** INSERT_STRING(s, s.strstart, hash_head); ***/ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; - hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; - s.head[s.ins_h] = s.strstart; - /***/ - } + return r; + }; - /* Find the longest match, discarding those <= prev_length. - * At this point we have always match_length < MIN_MATCH - */ - if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) { - /* To simplify the code, we prevent matches with the string - * of window index 0 (in particular we have to avoid a match - * of the string with itself at the start of the input file). - */ - s.match_length = longest_match(s, hash_head); - /* longest_match() sets match_start */ - } - if (s.match_length >= MIN_MATCH) { - // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only - - /*** _tr_tally_dist(s, s.strstart - s.match_start, - s.match_length - MIN_MATCH, bflush); ***/ - bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH); - - s.lookahead -= s.match_length; - - /* Insert new strings in the hash table only if the match length - * is not too large. This saves time but degrades compression. - */ - if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) { - s.match_length--; /* string at strstart already in table */ - do { - s.strstart++; - /*** INSERT_STRING(s, s.strstart, hash_head); ***/ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; - hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; - s.head[s.ins_h] = s.strstart; - /***/ - /* strstart never exceeds WSIZE-MAX_MATCH, so there are - * always MIN_MATCH bytes ahead. - */ - } while (--s.match_length !== 0); - s.strstart++; - } else - { - s.strstart += s.match_length; - s.match_length = 0; - s.ins_h = s.window[s.strstart]; - /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask; - -//#if MIN_MATCH != 3 -// Call UPDATE_HASH() MIN_MATCH-3 more times -//#endif - /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not - * matter since it will be recomputed at next deflate call. - */ - } + Red.prototype.invm = function invm (a) { + var inv = a._invmp(this.m); + if (inv.negative !== 0) { + inv.negative = 0; + return this.imod(inv).redNeg(); } else { - /* No match, output a literal byte */ - //Tracevv((stderr,"%c", s.window[s.strstart])); - /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ - bflush = trees._tr_tally(s, 0, s.window[s.strstart]); + return this.imod(inv); + } + }; - s.lookahead--; - s.strstart++; + Red.prototype.pow = function pow (a, num) { + if (num.isZero()) return new BN(1); + if (num.cmpn(1) === 0) return a.clone(); + + var windowSize = 4; + var wnd = new Array(1 << windowSize); + wnd[0] = new BN(1).toRed(this); + wnd[1] = a; + for (var i = 2; i < wnd.length; i++) { + wnd[i] = this.mul(wnd[i - 1], a); } - if (bflush) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; + + var res = wnd[0]; + var current = 0; + var currentLen = 0; + var start = num.bitLength() % 26; + if (start === 0) { + start = 26; + } + + for (i = num.length - 1; i >= 0; i--) { + var word = num.words[i]; + for (var j = start - 1; j >= 0; j--) { + var bit = (word >> j) & 1; + if (res !== wnd[0]) { + res = this.sqr(res); + } + + if (bit === 0 && current === 0) { + currentLen = 0; + continue; + } + + current <<= 1; + current |= bit; + currentLen++; + if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue; + + res = this.mul(res, wnd[current]); + currentLen = 0; + current = 0; + } + start = 26; + } + + return res; + }; + + Red.prototype.convertTo = function convertTo (num) { + var r = num.umod(this.m); + + return r === num ? r.clone() : r; + }; + + Red.prototype.convertFrom = function convertFrom (num) { + var res = num.clone(); + res.red = null; + return res; + }; + + // + // Montgomery method engine + // + + BN.mont = function mont (num) { + return new Mont(num); + }; + + function Mont (m) { + Red.call(this, m); + + this.shift = this.m.bitLength(); + if (this.shift % 26 !== 0) { + this.shift += 26 - (this.shift % 26); + } + + this.r = new BN(1).iushln(this.shift); + this.r2 = this.imod(this.r.sqr()); + this.rinv = this.r._invmp(this.m); + + this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); + this.minv = this.minv.umod(this.r); + this.minv = this.r.sub(this.minv); + } + inherits(Mont, Red); + + Mont.prototype.convertTo = function convertTo (num) { + return this.imod(num.ushln(this.shift)); + }; + + Mont.prototype.convertFrom = function convertFrom (num) { + var r = this.imod(num.mul(this.rinv)); + r.red = null; + return r; + }; + + Mont.prototype.imul = function imul (a, b) { + if (a.isZero() || b.isZero()) { + a.words[0] = 0; + a.length = 1; + return a; + } + + var t = a.imul(b); + var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); + var u = t.isub(c).iushrn(this.shift); + var res = u; + + if (u.cmp(this.m) >= 0) { + res = u.isub(this.m); + } else if (u.cmpn(0) < 0) { + res = u.iadd(this.m); + } + + return res._forceRed(this); + }; + + Mont.prototype.mul = function mul (a, b) { + if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this); + + var t = a.mul(b); + var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); + var u = t.isub(c).iushrn(this.shift); + var res = u; + if (u.cmp(this.m) >= 0) { + res = u.isub(this.m); + } else if (u.cmpn(0) < 0) { + res = u.iadd(this.m); + } + + return res._forceRed(this); + }; + + Mont.prototype.invm = function invm (a) { + // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R + var res = this.imod(a._invmp(this.m).mul(this.r2)); + return res._forceRed(this); + }; +})(typeof module === 'undefined' || module, this); + +},{}],35:[function(require,module,exports){ +'use strict' + +module.exports = boundary + +function boundary (cells) { + var i, j, k + var n = cells.length + var sz = 0 + for (i = 0; i < n; ++i) { + sz += cells[i].length + } + var result = new Array(sz) + var ptr = 0 + for (i = 0; i < n; ++i) { + var c = cells[i] + var d = c.length + for (j = 0; j < d; ++j) { + var b = result[ptr++] = new Array(d - 1) + var p = 0 + for (k = 0; k < d; ++k) { + if (k === j) { + continue + } + b[p++] = c[k] + } + if (j & 1) { + var tmp = b[1] + b[1] = b[0] + b[0] = tmp } - /***/ } } - s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1); - if (flush === Z_FINISH) { - /*** FLUSH_BLOCK(s, 1); ***/ - flush_block_only(s, true); - if (s.strm.avail_out === 0) { - return BS_FINISH_STARTED; - } - /***/ - return BS_FINISH_DONE; - } - if (s.last_lit) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } - return BS_BLOCK_DONE; + return result } -/* =========================================================================== - * Same as above, but achieves better compression. We use a lazy - * evaluation for matches: a match is finally adopted only if there is - * no better match at the next window position. - */ -function deflate_slow(s, flush) { - var hash_head; /* head of hash chain */ - var bflush; /* set if current block must be flushed */ +},{}],36:[function(require,module,exports){ +'use strict' - var max_insert; +module.exports = boxIntersectWrapper - /* Process the input block. */ - for (;;) { - /* Make sure that we always have enough lookahead, except - * at the end of the input file. We need MAX_MATCH bytes - * for the next match, plus MIN_MATCH bytes to insert the - * string following the next match. - */ - if (s.lookahead < MIN_LOOKAHEAD) { - fill_window(s); - if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) { - return BS_NEED_MORE; - } - if (s.lookahead === 0) { break; } /* flush the current block */ +var pool = require('typedarray-pool') +var sweep = require('./lib/sweep') +var boxIntersectIter = require('./lib/intersect') + +function boxEmpty(d, box) { + for(var j=0; j= MIN_MATCH) { - /*** INSERT_STRING(s, s.strstart, hash_head); ***/ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; - hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; - s.head[s.ins_h] = s.strstart; - /***/ +//Unpack boxes into a flat typed array, remove empty boxes +function convertBoxes(boxes, d, data, ids) { + var ptr = 0 + var count = 0 + for(var i=0, n=boxes.length; i 4096/*TOO_FAR*/))) { - - /* If prev_match is also MIN_MATCH, match_start is garbage - * but we will ignore the current match anyway. - */ - s.match_length = MIN_MATCH - 1; - } + for(var j=0; j<2*d; ++j) { + data[ptr++] = b[j] } - /* If there was a match at the previous step and the current - * match is not better, output the previous match: - */ - if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) { - max_insert = s.strstart + s.lookahead - MIN_MATCH; - /* Do not insert strings in hash table beyond this. */ + ids[count++] = i + } + return count +} - //check_match(s, s.strstart-1, s.prev_match, s.prev_length); +//Perform type conversions, check bounds +function boxIntersect(red, blue, visit, full) { + var n = red.length + var m = blue.length - /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match, - s.prev_length - MIN_MATCH, bflush);***/ - bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH); - /* Insert in hash table all strings up to the end of the match. - * strstart-1 and strstart are already inserted. If there is not - * enough lookahead, the last two strings are not inserted in - * the hash table. - */ - s.lookahead -= s.prev_length - 1; - s.prev_length -= 2; - do { - if (++s.strstart <= max_insert) { - /*** INSERT_STRING(s, s.strstart, hash_head); ***/ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; - hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; - s.head[s.ins_h] = s.strstart; - /***/ - } - } while (--s.prev_length !== 0); - s.match_available = 0; - s.match_length = MIN_MATCH - 1; - s.strstart++; + //If either array is empty, then we can skip this whole thing + if(n <= 0 || m <= 0) { + return + } - if (bflush) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } + //Compute dimension, if it is 0 then we skip + var d = (red[0].length)>>>1 + if(d <= 0) { + return + } - } else if (s.match_available) { - /* If there was no match at the previous position, output a - * single literal. If there was a match but the current match - * is longer, truncate the previous match to a single literal. - */ - //Tracevv((stderr,"%c", s->window[s->strstart-1])); - /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ - bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]); + var retval - if (bflush) { - /*** FLUSH_BLOCK_ONLY(s, 0) ***/ - flush_block_only(s, false); - /***/ - } - s.strstart++; - s.lookahead--; - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } + //Convert red boxes + var redList = pool.mallocDouble(2*d*n) + var redIds = pool.mallocInt32(n) + n = convertBoxes(red, d, redList, redIds) + + if(n > 0) { + if(d === 1 && full) { + //Special case: 1d complete + sweep.init(n) + retval = sweep.sweepComplete( + d, visit, + 0, n, redList, redIds, + 0, n, redList, redIds) } else { - /* There is no previous match to compare with, wait for - * the next step to decide. - */ - s.match_available = 1; - s.strstart++; - s.lookahead--; - } - } - //Assert (flush != Z_NO_FLUSH, "no flush?"); - if (s.match_available) { - //Tracevv((stderr,"%c", s->window[s->strstart-1])); - /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ - bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]); - s.match_available = 0; - } - s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1; - if (flush === Z_FINISH) { - /*** FLUSH_BLOCK(s, 1); ***/ - flush_block_only(s, true); - if (s.strm.avail_out === 0) { - return BS_FINISH_STARTED; - } - /***/ - return BS_FINISH_DONE; - } - if (s.last_lit) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } + //Convert blue boxes + var blueList = pool.mallocDouble(2*d*m) + var blueIds = pool.mallocInt32(m) + m = convertBoxes(blue, d, blueList, blueIds) - return BS_BLOCK_DONE; -} + if(m > 0) { + sweep.init(n+m) - -/* =========================================================================== - * For Z_RLE, simply look for runs of bytes, generate matches only of distance - * one. Do not maintain a hash table. (It will be regenerated if this run of - * deflate switches away from Z_RLE.) - */ -function deflate_rle(s, flush) { - var bflush; /* set if current block must be flushed */ - var prev; /* byte at distance one to match */ - var scan, strend; /* scan goes up to strend for length of run */ - - var _win = s.window; - - for (;;) { - /* Make sure that we always have enough lookahead, except - * at the end of the input file. We need MAX_MATCH bytes - * for the longest run, plus one for the unrolled loop. - */ - if (s.lookahead <= MAX_MATCH) { - fill_window(s); - if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) { - return BS_NEED_MORE; - } - if (s.lookahead === 0) { break; } /* flush the current block */ - } - - /* See how many times the previous byte repeats */ - s.match_length = 0; - if (s.lookahead >= MIN_MATCH && s.strstart > 0) { - scan = s.strstart - 1; - prev = _win[scan]; - if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) { - strend = s.strstart + MAX_MATCH; - do { - /*jshint noempty:false*/ - } while (prev === _win[++scan] && prev === _win[++scan] && - prev === _win[++scan] && prev === _win[++scan] && - prev === _win[++scan] && prev === _win[++scan] && - prev === _win[++scan] && prev === _win[++scan] && - scan < strend); - s.match_length = MAX_MATCH - (strend - scan); - if (s.match_length > s.lookahead) { - s.match_length = s.lookahead; + if(d === 1) { + //Special case: 1d bipartite + retval = sweep.sweepBipartite( + d, visit, + 0, n, redList, redIds, + 0, m, blueList, blueIds) + } else { + //General case: d>1 + retval = boxIntersectIter( + d, visit, full, + n, redList, redIds, + m, blueList, blueIds) } - } - //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); - } - /* Emit match if have run of MIN_MATCH or longer, else emit literal */ - if (s.match_length >= MIN_MATCH) { - //check_match(s, s.strstart, s.strstart - 1, s.match_length); - - /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/ - bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH); - - s.lookahead -= s.match_length; - s.strstart += s.match_length; - s.match_length = 0; - } else { - /* No match, output a literal byte */ - //Tracevv((stderr,"%c", s->window[s->strstart])); - /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ - bflush = trees._tr_tally(s, 0, s.window[s.strstart]); - - s.lookahead--; - s.strstart++; - } - if (bflush) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } - } - s.insert = 0; - if (flush === Z_FINISH) { - /*** FLUSH_BLOCK(s, 1); ***/ - flush_block_only(s, true); - if (s.strm.avail_out === 0) { - return BS_FINISH_STARTED; - } - /***/ - return BS_FINISH_DONE; - } - if (s.last_lit) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } - return BS_BLOCK_DONE; -} - -/* =========================================================================== - * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. - * (It will be regenerated if this run of deflate switches away from Huffman.) - */ -function deflate_huff(s, flush) { - var bflush; /* set if current block must be flushed */ - - for (;;) { - /* Make sure that we have a literal to write. */ - if (s.lookahead === 0) { - fill_window(s); - if (s.lookahead === 0) { - if (flush === Z_NO_FLUSH) { - return BS_NEED_MORE; - } - break; /* flush the current block */ + pool.free(blueList) + pool.free(blueIds) } } - /* Output a literal byte */ - s.match_length = 0; - //Tracevv((stderr,"%c", s->window[s->strstart])); - /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ - bflush = trees._tr_tally(s, 0, s.window[s.strstart]); - s.lookahead--; - s.strstart++; - if (bflush) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } + pool.free(redList) + pool.free(redIds) } - s.insert = 0; - if (flush === Z_FINISH) { - /*** FLUSH_BLOCK(s, 1); ***/ - flush_block_only(s, true); - if (s.strm.avail_out === 0) { - return BS_FINISH_STARTED; - } - /***/ - return BS_FINISH_DONE; - } - if (s.last_lit) { - /*** FLUSH_BLOCK(s, 0); ***/ - flush_block_only(s, false); - if (s.strm.avail_out === 0) { - return BS_NEED_MORE; - } - /***/ - } - return BS_BLOCK_DONE; -} -/* Values for max_lazy_match, good_match and max_chain_length, depending on - * the desired pack level (0..9). The values given below have been tuned to - * exclude worst case performance for pathological files. Better values may be - * found for specific files. - */ -function Config(good_length, max_lazy, nice_length, max_chain, func) { - this.good_length = good_length; - this.max_lazy = max_lazy; - this.nice_length = nice_length; - this.max_chain = max_chain; - this.func = func; -} - -var configuration_table; - -configuration_table = [ - /* good lazy nice chain */ - new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */ - new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */ - new Config(4, 5, 16, 8, deflate_fast), /* 2 */ - new Config(4, 6, 32, 32, deflate_fast), /* 3 */ - - new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */ - new Config(8, 16, 32, 32, deflate_slow), /* 5 */ - new Config(8, 16, 128, 128, deflate_slow), /* 6 */ - new Config(8, 32, 128, 256, deflate_slow), /* 7 */ - new Config(32, 128, 258, 1024, deflate_slow), /* 8 */ - new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */ -]; - - -/* =========================================================================== - * Initialize the "longest match" routines for a new zlib stream - */ -function lm_init(s) { - s.window_size = 2 * s.w_size; - - /*** CLEAR_HASH(s); ***/ - zero(s.head); // Fill with NIL (= 0); - - /* Set the default configuration parameters: - */ - s.max_lazy_match = configuration_table[s.level].max_lazy; - s.good_match = configuration_table[s.level].good_length; - s.nice_match = configuration_table[s.level].nice_length; - s.max_chain_length = configuration_table[s.level].max_chain; - - s.strstart = 0; - s.block_start = 0; - s.lookahead = 0; - s.insert = 0; - s.match_length = s.prev_length = MIN_MATCH - 1; - s.match_available = 0; - s.ins_h = 0; + return retval } -function DeflateState() { - this.strm = null; /* pointer back to this zlib stream */ - this.status = 0; /* as the name implies */ - this.pending_buf = null; /* output still pending */ - this.pending_buf_size = 0; /* size of pending_buf */ - this.pending_out = 0; /* next pending byte to output to the stream */ - this.pending = 0; /* nb of bytes in the pending buffer */ - this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ - this.gzhead = null; /* gzip header information to write */ - this.gzindex = 0; /* where in extra, name, or comment */ - this.method = Z_DEFLATED; /* can only be DEFLATED */ - this.last_flush = -1; /* value of flush param for previous deflate call */ +var RESULT - this.w_size = 0; /* LZ77 window size (32K by default) */ - this.w_bits = 0; /* log2(w_size) (8..16) */ - this.w_mask = 0; /* w_size - 1 */ - - this.window = null; - /* Sliding window. Input bytes are read into the second half of the window, - * and move to the first half later to keep a dictionary of at least wSize - * bytes. With this organization, matches are limited to a distance of - * wSize-MAX_MATCH bytes, but this ensures that IO is always - * performed with a length multiple of the block size. - */ - - this.window_size = 0; - /* Actual size of window: 2*wSize, except when the user input buffer - * is directly used as sliding window. - */ - - this.prev = null; - /* Link to older string with same hash index. To limit the size of this - * array to 64K, this link is maintained only for the last 32K strings. - * An index in this array is thus a window index modulo 32K. - */ - - this.head = null; /* Heads of the hash chains or NIL. */ - - this.ins_h = 0; /* hash index of string to be inserted */ - this.hash_size = 0; /* number of elements in hash table */ - this.hash_bits = 0; /* log2(hash_size) */ - this.hash_mask = 0; /* hash_size-1 */ - - this.hash_shift = 0; - /* Number of bits by which ins_h must be shifted at each input - * step. It must be such that after MIN_MATCH steps, the oldest - * byte no longer takes part in the hash key, that is: - * hash_shift * MIN_MATCH >= hash_bits - */ - - this.block_start = 0; - /* Window position at the beginning of the current output block. Gets - * negative when the window is moved backwards. - */ - - this.match_length = 0; /* length of best match */ - this.prev_match = 0; /* previous match */ - this.match_available = 0; /* set if previous match exists */ - this.strstart = 0; /* start of string to insert */ - this.match_start = 0; /* start of matching string */ - this.lookahead = 0; /* number of valid bytes ahead in window */ - - this.prev_length = 0; - /* Length of the best match at previous step. Matches not greater than this - * are discarded. This is used in the lazy match evaluation. - */ - - this.max_chain_length = 0; - /* To speed up deflation, hash chains are never searched beyond this - * length. A higher limit improves compression ratio but degrades the - * speed. - */ - - this.max_lazy_match = 0; - /* Attempt to find a better match only when the current match is strictly - * smaller than this value. This mechanism is used only for compression - * levels >= 4. - */ - // That's alias to max_lazy_match, don't use directly - //this.max_insert_length = 0; - /* Insert new strings in the hash table only if the match length is not - * greater than this length. This saves time but degrades compression. - * max_insert_length is used only for compression levels <= 3. - */ - - this.level = 0; /* compression level (1..9) */ - this.strategy = 0; /* favor or force Huffman coding*/ - - this.good_match = 0; - /* Use a faster search when the previous match is longer than this */ - - this.nice_match = 0; /* Stop searching when current match exceeds this */ - - /* used by trees.c: */ - - /* Didn't use ct_data typedef below to suppress compiler warning */ - - // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ - // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ - // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ - - // Use flat array of DOUBLE size, with interleaved fata, - // because JS does not support effective - this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2); - this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2); - this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2); - zero(this.dyn_ltree); - zero(this.dyn_dtree); - zero(this.bl_tree); - - this.l_desc = null; /* desc. for literal tree */ - this.d_desc = null; /* desc. for distance tree */ - this.bl_desc = null; /* desc. for bit length tree */ - - //ush bl_count[MAX_BITS+1]; - this.bl_count = new utils.Buf16(MAX_BITS + 1); - /* number of codes at each bit length for an optimal tree */ - - //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ - this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */ - zero(this.heap); - - this.heap_len = 0; /* number of elements in the heap */ - this.heap_max = 0; /* element of largest frequency */ - /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. - * The same heap array is used to build all trees. - */ - - this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1]; - zero(this.depth); - /* Depth of each subtree used as tie breaker for trees of equal frequency - */ - - this.l_buf = 0; /* buffer index for literals or lengths */ - - this.lit_bufsize = 0; - /* Size of match buffer for literals/lengths. There are 4 reasons for - * limiting lit_bufsize to 64K: - * - frequencies can be kept in 16 bit counters - * - if compression is not successful for the first block, all input - * data is still in the window so we can still emit a stored block even - * when input comes from standard input. (This can also be done for - * all blocks if lit_bufsize is not greater than 32K.) - * - if compression is not successful for a file smaller than 64K, we can - * even emit a stored file instead of a stored block (saving 5 bytes). - * This is applicable only for zip (not gzip or zlib). - * - creating new Huffman trees less frequently may not provide fast - * adaptation to changes in the input data statistics. (Take for - * example a binary file with poorly compressible code followed by - * a highly compressible string table.) Smaller buffer sizes give - * fast adaptation but have of course the overhead of transmitting - * trees more frequently. - * - I can't count above 4 - */ - - this.last_lit = 0; /* running index in l_buf */ - - this.d_buf = 0; - /* Buffer index for distances. To simplify the code, d_buf and l_buf have - * the same number of elements. To use different lengths, an extra flag - * array would be necessary. - */ - - this.opt_len = 0; /* bit length of current block with optimal trees */ - this.static_len = 0; /* bit length of current block with static trees */ - this.matches = 0; /* number of string matches in current block */ - this.insert = 0; /* bytes at end of window left to insert */ - - - this.bi_buf = 0; - /* Output buffer. bits are inserted starting at the bottom (least - * significant bits). - */ - this.bi_valid = 0; - /* Number of valid bits in bi_buf. All bits above the last valid bit - * are always zero. - */ - - // Used for window memory init. We safely ignore it for JS. That makes - // sense only for pointers and memory check tools. - //this.high_water = 0; - /* High water mark offset in window for initialized bytes -- bytes above - * this are set to zero in order to avoid memory check warnings when - * longest match routines access bytes past the input. This is then - * updated to the new high water mark. - */ +function appendItem(i,j) { + RESULT.push([i,j]) } - -function deflateResetKeep(strm) { - var s; - - if (!strm || !strm.state) { - return err(strm, Z_STREAM_ERROR); - } - - strm.total_in = strm.total_out = 0; - strm.data_type = Z_UNKNOWN; - - s = strm.state; - s.pending = 0; - s.pending_out = 0; - - if (s.wrap < 0) { - s.wrap = -s.wrap; - /* was made negative by deflate(..., Z_FINISH); */ - } - s.status = (s.wrap ? INIT_STATE : BUSY_STATE); - strm.adler = (s.wrap === 2) ? - 0 // crc32(0, Z_NULL, 0) - : - 1; // adler32(0, Z_NULL, 0) - s.last_flush = Z_NO_FLUSH; - trees._tr_init(s); - return Z_OK; +function intersectFullArray(x) { + RESULT = [] + boxIntersect(x, x, appendItem, true) + return RESULT } - -function deflateReset(strm) { - var ret = deflateResetKeep(strm); - if (ret === Z_OK) { - lm_init(strm.state); - } - return ret; +function intersectBipartiteArray(x, y) { + RESULT = [] + boxIntersect(x, y, appendItem, false) + return RESULT } - -function deflateSetHeader(strm, head) { - if (!strm || !strm.state) { return Z_STREAM_ERROR; } - if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; } - strm.state.gzhead = head; - return Z_OK; -} - - -function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { - if (!strm) { // === Z_NULL - return Z_STREAM_ERROR; - } - var wrap = 1; - - if (level === Z_DEFAULT_COMPRESSION) { - level = 6; - } - - if (windowBits < 0) { /* suppress zlib wrapper */ - wrap = 0; - windowBits = -windowBits; - } - - else if (windowBits > 15) { - wrap = 2; /* write gzip wrapper instead */ - windowBits -= 16; - } - - - if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED || - windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || - strategy < 0 || strategy > Z_FIXED) { - return err(strm, Z_STREAM_ERROR); - } - - - if (windowBits === 8) { - windowBits = 9; - } - /* until 256-byte window bug fixed */ - - var s = new DeflateState(); - - strm.state = s; - s.strm = strm; - - s.wrap = wrap; - s.gzhead = null; - s.w_bits = windowBits; - s.w_size = 1 << s.w_bits; - s.w_mask = s.w_size - 1; - - s.hash_bits = memLevel + 7; - s.hash_size = 1 << s.hash_bits; - s.hash_mask = s.hash_size - 1; - s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH); - - s.window = new utils.Buf8(s.w_size * 2); - s.head = new utils.Buf16(s.hash_size); - s.prev = new utils.Buf16(s.w_size); - - // Don't need mem init magic for JS. - //s.high_water = 0; /* nothing written to s->window yet */ - - s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ - - s.pending_buf_size = s.lit_bufsize * 4; - - //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); - //s->pending_buf = (uchf *) overlay; - s.pending_buf = new utils.Buf8(s.pending_buf_size); - - // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`) - //s->d_buf = overlay + s->lit_bufsize/sizeof(ush); - s.d_buf = 1 * s.lit_bufsize; - - //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; - s.l_buf = (1 + 2) * s.lit_bufsize; - - s.level = level; - s.strategy = strategy; - s.method = method; - - return deflateReset(strm); -} - -function deflateInit(strm, level) { - return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); -} - - -function deflate(strm, flush) { - var old_flush, s; - var beg, val; // for gzip header write only - - if (!strm || !strm.state || - flush > Z_BLOCK || flush < 0) { - return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR; - } - - s = strm.state; - - if (!strm.output || - (!strm.input && strm.avail_in !== 0) || - (s.status === FINISH_STATE && flush !== Z_FINISH)) { - return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR); - } - - s.strm = strm; /* just in case */ - old_flush = s.last_flush; - s.last_flush = flush; - - /* Write the header */ - if (s.status === INIT_STATE) { - - if (s.wrap === 2) { // GZIP header - strm.adler = 0; //crc32(0L, Z_NULL, 0); - put_byte(s, 31); - put_byte(s, 139); - put_byte(s, 8); - if (!s.gzhead) { // s->gzhead == Z_NULL - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, 0); - put_byte(s, s.level === 9 ? 2 : - (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? - 4 : 0)); - put_byte(s, OS_CODE); - s.status = BUSY_STATE; - } - else { - put_byte(s, (s.gzhead.text ? 1 : 0) + - (s.gzhead.hcrc ? 2 : 0) + - (!s.gzhead.extra ? 0 : 4) + - (!s.gzhead.name ? 0 : 8) + - (!s.gzhead.comment ? 0 : 16) - ); - put_byte(s, s.gzhead.time & 0xff); - put_byte(s, (s.gzhead.time >> 8) & 0xff); - put_byte(s, (s.gzhead.time >> 16) & 0xff); - put_byte(s, (s.gzhead.time >> 24) & 0xff); - put_byte(s, s.level === 9 ? 2 : - (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? - 4 : 0)); - put_byte(s, s.gzhead.os & 0xff); - if (s.gzhead.extra && s.gzhead.extra.length) { - put_byte(s, s.gzhead.extra.length & 0xff); - put_byte(s, (s.gzhead.extra.length >> 8) & 0xff); - } - if (s.gzhead.hcrc) { - strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0); - } - s.gzindex = 0; - s.status = EXTRA_STATE; - } - } - else // DEFLATE header - { - var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8; - var level_flags = -1; - - if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) { - level_flags = 0; - } else if (s.level < 6) { - level_flags = 1; - } else if (s.level === 6) { - level_flags = 2; +//User-friendly wrapper, handle full input and no-visitor cases +function boxIntersectWrapper(arg0, arg1, arg2) { + var result + switch(arguments.length) { + case 1: + return intersectFullArray(arg0) + case 2: + if(typeof arg1 === 'function') { + return boxIntersect(arg0, arg0, arg1, true) } else { - level_flags = 3; + return intersectBipartiteArray(arg0, arg1) } - header |= (level_flags << 6); - if (s.strstart !== 0) { header |= PRESET_DICT; } - header += 31 - (header % 31); + case 3: + return boxIntersect(arg0, arg1, arg2, false) + default: + throw new Error('box-intersect: Invalid arguments') + } +} +},{"./lib/intersect":38,"./lib/sweep":42,"typedarray-pool":992}],37:[function(require,module,exports){ +'use strict' - s.status = BUSY_STATE; - putShortMSB(s, header); +var DIMENSION = 'd' +var AXIS = 'ax' +var VISIT = 'vv' +var FLIP = 'fp' - /* Save the adler32 of the preset dictionary: */ - if (s.strstart !== 0) { - putShortMSB(s, strm.adler >>> 16); - putShortMSB(s, strm.adler & 0xffff); - } - strm.adler = 1; // adler32(0L, Z_NULL, 0); - } +var ELEM_SIZE = 'es' + +var RED_START = 'rs' +var RED_END = 're' +var RED_BOXES = 'rb' +var RED_INDEX = 'ri' +var RED_PTR = 'rp' + +var BLUE_START = 'bs' +var BLUE_END = 'be' +var BLUE_BOXES = 'bb' +var BLUE_INDEX = 'bi' +var BLUE_PTR = 'bp' + +var RETVAL = 'rv' + +var INNER_LABEL = 'Q' + +var ARGS = [ + DIMENSION, + AXIS, + VISIT, + RED_START, + RED_END, + RED_BOXES, + RED_INDEX, + BLUE_START, + BLUE_END, + BLUE_BOXES, + BLUE_INDEX +] + +function generateBruteForce(redMajor, flip, full) { + var funcName = 'bruteForce' + + (redMajor ? 'Red' : 'Blue') + + (flip ? 'Flip' : '') + + (full ? 'Full' : '') + + var code = ['function ', funcName, '(', ARGS.join(), '){', + 'var ', ELEM_SIZE, '=2*', DIMENSION, ';'] + + var redLoop = + 'for(var i=' + RED_START + ',' + RED_PTR + '=' + ELEM_SIZE + '*' + RED_START + ';' + + 'i<' + RED_END +';' + + '++i,' + RED_PTR + '+=' + ELEM_SIZE + '){' + + 'var x0=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '],' + + 'x1=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '+' + DIMENSION + '],' + + 'xi=' + RED_INDEX + '[i];' + + var blueLoop = + 'for(var j=' + BLUE_START + ',' + BLUE_PTR + '=' + ELEM_SIZE + '*' + BLUE_START + ';' + + 'j<' + BLUE_END + ';' + + '++j,' + BLUE_PTR + '+=' + ELEM_SIZE + '){' + + 'var y0=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '],' + + (full ? 'y1=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '+' + DIMENSION + '],' : '') + + 'yi=' + BLUE_INDEX + '[j];' + + if(redMajor) { + code.push(redLoop, INNER_LABEL, ':', blueLoop) + } else { + code.push(blueLoop, INNER_LABEL, ':', redLoop) } -//#ifdef GZIP - if (s.status === EXTRA_STATE) { - if (s.gzhead.extra/* != Z_NULL*/) { - beg = s.pending; /* start of bytes to update crc */ + if(full) { + code.push('if(y1 beg) { - strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); - } - flush_pending(strm); - beg = s.pending; - if (s.pending === s.pending_buf_size) { - break; - } - } - put_byte(s, s.gzhead.extra[s.gzindex] & 0xff); - s.gzindex++; - } - if (s.gzhead.hcrc && s.pending > beg) { - strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); - } - if (s.gzindex === s.gzhead.extra.length) { - s.gzindex = 0; - s.status = NAME_STATE; + code.push('for(var k='+AXIS+'+1;k<'+DIMENSION+';++k){'+ + 'var r0='+RED_BOXES+'[k+'+RED_PTR+'],'+ + 'r1='+RED_BOXES+'[k+'+DIMENSION+'+'+RED_PTR+'],'+ + 'b0='+BLUE_BOXES+'[k+'+BLUE_PTR+'],'+ + 'b1='+BLUE_BOXES+'[k+'+DIMENSION+'+'+BLUE_PTR+'];'+ + 'if(r1' + + BLUE_END + '-' + BLUE_START + '){') + + if(full) { + invoke(true, false) + code.push('}else{') + invoke(false, false) + } else { + code.push('if(' + FLIP + '){') + invoke(true, true) + code.push('}else{') + invoke(true, false) + code.push('}}else{if(' + FLIP + '){') + invoke(false, true) + code.push('}else{') + invoke(false, false) + code.push('}') + } + code.push('}}return ' + funcName) + + var codeStr = prefix.join('') + code.join('') + var proc = new Function(codeStr) + return proc() +} + + +exports.partial = bruteForcePlanner(false) +exports.full = bruteForcePlanner(true) +},{}],38:[function(require,module,exports){ +'use strict' + +module.exports = boxIntersectIter + +var pool = require('typedarray-pool') +var bits = require('bit-twiddle') +var bruteForce = require('./brute') +var bruteForcePartial = bruteForce.partial +var bruteForceFull = bruteForce.full +var sweep = require('./sweep') +var findMedian = require('./median') +var genPartition = require('./partition') + +//Twiddle parameters +var BRUTE_FORCE_CUTOFF = 128 //Cut off for brute force search +var SCAN_CUTOFF = (1<<22) //Cut off for two way scan +var SCAN_COMPLETE_CUTOFF = (1<<22) + +//Partition functions +var partitionInteriorContainsInterval = genPartition( + '!(lo>=p0)&&!(p1>=hi)', + ['p0', 'p1']) + +var partitionStartEqual = genPartition( + 'lo===p0', + ['p0']) + +var partitionStartLessThan = genPartition( + 'lo beg) { - strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); - } - flush_pending(strm); - beg = s.pending; - if (s.pending === s.pending_buf_size) { - val = 1; - break; - } +//Special case: Intersect one point with list of intervals +function onePointFull( + d, axis, visit, + redStart, redEnd, red, redIndex, + blueOffset, blue, blueId) { + + var elemSize = 2 * d + var bluePtr = blueOffset * elemSize + var blueX = blue[bluePtr + axis] + +red_loop: + for(var i=redStart, redPtr=redStart*elemSize; i 0) { + top -= 1 + + var iptr = top * IFRAME_SIZE + var axis = BOX_ISTACK[iptr] + var redStart = BOX_ISTACK[iptr+1] + var redEnd = BOX_ISTACK[iptr+2] + var blueStart = BOX_ISTACK[iptr+3] + var blueEnd = BOX_ISTACK[iptr+4] + var state = BOX_ISTACK[iptr+5] + + var dptr = top * DFRAME_SIZE + var lo = BOX_DSTACK[dptr] + var hi = BOX_DSTACK[dptr+1] + + //Unpack state info + var flip = (state & 1) + var full = !!(state & 16) + + //Unpack indices + var red = xBoxes + var redIndex = xIndex + var blue = yBoxes + var blueIndex = yIndex + if(flip) { + red = yBoxes + redIndex = yIndex + blue = xBoxes + blueIndex = xIndex + } + + if(state & 2) { + redEnd = partitionStartLessThan( + d, axis, + redStart, redEnd, red, redIndex, + hi) + if(redStart >= redEnd) { + continue + } + } + if(state & 4) { + redStart = partitionEndLessThanEqual( + d, axis, + redStart, redEnd, red, redIndex, + lo) + if(redStart >= redEnd) { + continue + } + } + + var redCount = redEnd - redStart + var blueCount = blueEnd - blueStart + + if(full) { + if(d * redCount * (redCount + blueCount) < SCAN_COMPLETE_CUTOFF) { + retval = sweep.scanComplete( + d, axis, visit, + redStart, redEnd, red, redIndex, + blueStart, blueEnd, blue, blueIndex) + if(retval !== void 0) { + return retval } - // JS specific: little magic to add zero terminator to end of string - if (s.gzindex < s.gzhead.name.length) { - val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff; + continue + } + } else { + if(d * Math.min(redCount, blueCount) < BRUTE_FORCE_CUTOFF) { + //If input small, then use brute force + retval = bruteForcePartial( + d, axis, visit, flip, + redStart, redEnd, red, redIndex, + blueStart, blueEnd, blue, blueIndex) + if(retval !== void 0) { + return retval + } + continue + } else if(d * redCount * blueCount < SCAN_CUTOFF) { + //If input medium sized, then use sweep and prune + retval = sweep.scanBipartite( + d, axis, visit, flip, + redStart, redEnd, red, redIndex, + blueStart, blueEnd, blue, blueIndex) + if(retval !== void 0) { + return retval + } + continue + } + } + + //First, find all red intervals whose interior contains (lo,hi) + var red0 = partitionInteriorContainsInterval( + d, axis, + redStart, redEnd, red, redIndex, + lo, hi) + + //Lower dimensional case + if(redStart < red0) { + + if(d * (red0 - redStart) < BRUTE_FORCE_CUTOFF) { + //Special case for small inputs: use brute force + retval = bruteForceFull( + d, axis+1, visit, + redStart, red0, red, redIndex, + blueStart, blueEnd, blue, blueIndex) + if(retval !== void 0) { + return retval + } + } else if(axis === d-2) { + if(flip) { + retval = sweep.sweepBipartite( + d, visit, + blueStart, blueEnd, blue, blueIndex, + redStart, red0, red, redIndex) } else { - val = 0; + retval = sweep.sweepBipartite( + d, visit, + redStart, red0, red, redIndex, + blueStart, blueEnd, blue, blueIndex) } - put_byte(s, val); - } while (val !== 0); - - if (s.gzhead.hcrc && s.pending > beg) { - strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); - } - if (val === 0) { - s.gzindex = 0; - s.status = COMMENT_STATE; + if(retval !== void 0) { + return retval + } + } else { + iterPush(top++, + axis+1, + redStart, red0, + blueStart, blueEnd, + flip, + -Infinity, Infinity) + iterPush(top++, + axis+1, + blueStart, blueEnd, + redStart, red0, + flip^1, + -Infinity, Infinity) } } - else { - s.status = COMMENT_STATE; - } - } - if (s.status === COMMENT_STATE) { - if (s.gzhead.comment/* != Z_NULL*/) { - beg = s.pending; /* start of bytes to update crc */ - //int val; - do { - if (s.pending === s.pending_buf_size) { - if (s.gzhead.hcrc && s.pending > beg) { - strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); - } - flush_pending(strm); - beg = s.pending; - if (s.pending === s.pending_buf_size) { - val = 1; - break; - } - } - // JS specific: little magic to add zero terminator to end of string - if (s.gzindex < s.gzhead.comment.length) { - val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff; + //Divide and conquer phase + if(red0 < redEnd) { + + //Cut blue into 3 parts: + // + // Points < mid point + // Points = mid point + // Points > mid point + // + var blue0 = findMedian( + d, axis, + blueStart, blueEnd, blue, blueIndex) + var mid = blue[elemSize * blue0 + axis] + var blue1 = partitionStartEqual( + d, axis, + blue0, blueEnd, blue, blueIndex, + mid) + + //Right case + if(blue1 < blueEnd) { + iterPush(top++, + axis, + red0, redEnd, + blue1, blueEnd, + (flip|4) + (full ? 16 : 0), + mid, hi) + } + + //Left case + if(blueStart < blue0) { + iterPush(top++, + axis, + red0, redEnd, + blueStart, blue0, + (flip|2) + (full ? 16 : 0), + lo, mid) + } + + //Center case (the hard part) + if(blue0 + 1 === blue1) { + //Optimization: Range with exactly 1 point, use a brute force scan + if(full) { + retval = onePointFull( + d, axis, visit, + red0, redEnd, red, redIndex, + blue0, blue, blueIndex[blue0]) } else { - val = 0; + retval = onePointPartial( + d, axis, visit, flip, + red0, redEnd, red, redIndex, + blue0, blue, blueIndex[blue0]) } - put_byte(s, val); - } while (val !== 0); + if(retval !== void 0) { + return retval + } + } else if(blue0 < blue1) { + var red1 + if(full) { + //If full intersection, need to handle special case + red1 = partitionContainsPoint( + d, axis, + red0, redEnd, red, redIndex, + mid) + if(red0 < red1) { + var redX = partitionStartEqual( + d, axis, + red0, red1, red, redIndex, + mid) + if(axis === d-2) { + //Degenerate sweep intersection: + // [red0, redX] with [blue0, blue1] + if(red0 < redX) { + retval = sweep.sweepComplete( + d, visit, + red0, redX, red, redIndex, + blue0, blue1, blue, blueIndex) + if(retval !== void 0) { + return retval + } + } - if (s.gzhead.hcrc && s.pending > beg) { - strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); - } - if (val === 0) { - s.status = HCRC_STATE; - } - } - else { - s.status = HCRC_STATE; - } - } - if (s.status === HCRC_STATE) { - if (s.gzhead.hcrc) { - if (s.pending + 2 > s.pending_buf_size) { - flush_pending(strm); - } - if (s.pending + 2 <= s.pending_buf_size) { - put_byte(s, strm.adler & 0xff); - put_byte(s, (strm.adler >> 8) & 0xff); - strm.adler = 0; //crc32(0L, Z_NULL, 0); - s.status = BUSY_STATE; - } - } - else { - s.status = BUSY_STATE; - } - } -//#endif - - /* Flush as much pending output as possible */ - if (s.pending !== 0) { - flush_pending(strm); - if (strm.avail_out === 0) { - /* Since avail_out is 0, deflate will be called again with - * more output space, but possibly with both pending and - * avail_in equal to zero. There won't be anything to do, - * but this is not an error situation so make sure we - * return OK instead of BUF_ERROR at next call of deflate: - */ - s.last_flush = -1; - return Z_OK; - } - - /* Make sure there is something to do and avoid duplicate consecutive - * flushes. For repeated and useless calls with Z_FINISH, we keep - * returning Z_STREAM_END instead of Z_BUF_ERROR. - */ - } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) && - flush !== Z_FINISH) { - return err(strm, Z_BUF_ERROR); - } - - /* User must not provide more input after the first FINISH: */ - if (s.status === FINISH_STATE && strm.avail_in !== 0) { - return err(strm, Z_BUF_ERROR); - } - - /* Start a new block or continue the current one. - */ - if (strm.avail_in !== 0 || s.lookahead !== 0 || - (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) { - var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) : - (s.strategy === Z_RLE ? deflate_rle(s, flush) : - configuration_table[s.level].func(s, flush)); - - if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) { - s.status = FINISH_STATE; - } - if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) { - if (strm.avail_out === 0) { - s.last_flush = -1; - /* avoid BUF_ERROR next call, see above */ - } - return Z_OK; - /* If flush != Z_NO_FLUSH && avail_out == 0, the next call - * of deflate should use the same flush parameter to make sure - * that the flush is complete. So we don't have to output an - * empty block here, this will be done at next call. This also - * ensures that for a very small output buffer, we emit at most - * one empty block. - */ - } - if (bstate === BS_BLOCK_DONE) { - if (flush === Z_PARTIAL_FLUSH) { - trees._tr_align(s); - } - else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ - - trees._tr_stored_block(s, 0, 0, false); - /* For a full flush, this empty block will be recognized - * as a special marker by inflate_sync(). - */ - if (flush === Z_FULL_FLUSH) { - /*** CLEAR_HASH(s); ***/ /* forget history */ - zero(s.head); // Fill with NIL (= 0); - - if (s.lookahead === 0) { - s.strstart = 0; - s.block_start = 0; - s.insert = 0; + //Normal sweep intersection: + // [redX, red1] with [blue0, blue1] + if(redX < red1) { + retval = sweep.sweepBipartite( + d, visit, + redX, red1, red, redIndex, + blue0, blue1, blue, blueIndex) + if(retval !== void 0) { + return retval + } + } + } else { + if(red0 < redX) { + iterPush(top++, + axis+1, + red0, redX, + blue0, blue1, + 16, + -Infinity, Infinity) + } + if(redX < red1) { + iterPush(top++, + axis+1, + redX, red1, + blue0, blue1, + 0, + -Infinity, Infinity) + iterPush(top++, + axis+1, + blue0, blue1, + redX, red1, + 1, + -Infinity, Infinity) + } + } + } + } else { + if(flip) { + red1 = partitionContainsPointProper( + d, axis, + red0, redEnd, red, redIndex, + mid) + } else { + red1 = partitionContainsPoint( + d, axis, + red0, redEnd, red, redIndex, + mid) + } + if(red0 < red1) { + if(axis === d-2) { + if(flip) { + retval = sweep.sweepBipartite( + d, visit, + blue0, blue1, blue, blueIndex, + red0, red1, red, redIndex) + } else { + retval = sweep.sweepBipartite( + d, visit, + red0, red1, red, redIndex, + blue0, blue1, blue, blueIndex) + } + } else { + iterPush(top++, + axis+1, + red0, red1, + blue0, blue1, + flip, + -Infinity, Infinity) + iterPush(top++, + axis+1, + blue0, blue1, + red0, red1, + flip^1, + -Infinity, Infinity) + } } } } - flush_pending(strm); - if (strm.avail_out === 0) { - s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */ - return Z_OK; + } + } +} +},{"./brute":37,"./median":39,"./partition":40,"./sweep":42,"bit-twiddle":33,"typedarray-pool":992}],39:[function(require,module,exports){ +'use strict' + +module.exports = findMedian + +var genPartition = require('./partition') + +var partitionStartLessThan = genPartition('lostart && boxes[ptr+axis] > x; + --j, ptr-=elemSize) { + //Swap + var aPtr = ptr + var bPtr = ptr+elemSize + for(var k=0; k>> 1) + var elemSize = 2*d + var pivot = mid + var value = boxes[elemSize*mid+axis] + + while(lo < hi) { + if(hi - lo < PARTITION_THRESHOLD) { + insertionSort(d, axis, lo, hi, boxes, ids) + value = boxes[elemSize*mid+axis] + break + } + + //Select pivot using median-of-3 + var count = hi - lo + var pivot0 = (Math.random()*count+lo)|0 + var value0 = boxes[elemSize*pivot0 + axis] + var pivot1 = (Math.random()*count+lo)|0 + var value1 = boxes[elemSize*pivot1 + axis] + var pivot2 = (Math.random()*count+lo)|0 + var value2 = boxes[elemSize*pivot2 + axis] + if(value0 <= value1) { + if(value2 >= value1) { + pivot = pivot1 + value = value1 + } else if(value0 >= value2) { + pivot = pivot0 + value = value0 + } else { + pivot = pivot2 + value = value2 + } + } else { + if(value1 >= value2) { + pivot = pivot1 + value = value1 + } else if(value2 >= value0) { + pivot = pivot0 + value = value0 + } else { + pivot = pivot2 + value = value2 } } - } - //Assert(strm->avail_out > 0, "bug2"); - //if (strm.avail_out <= 0) { throw new Error("bug2");} - if (flush !== Z_FINISH) { return Z_OK; } - if (s.wrap <= 0) { return Z_STREAM_END; } - - /* Write the trailer */ - if (s.wrap === 2) { - put_byte(s, strm.adler & 0xff); - put_byte(s, (strm.adler >> 8) & 0xff); - put_byte(s, (strm.adler >> 16) & 0xff); - put_byte(s, (strm.adler >> 24) & 0xff); - put_byte(s, strm.total_in & 0xff); - put_byte(s, (strm.total_in >> 8) & 0xff); - put_byte(s, (strm.total_in >> 16) & 0xff); - put_byte(s, (strm.total_in >> 24) & 0xff); - } - else - { - putShortMSB(s, strm.adler >>> 16); - putShortMSB(s, strm.adler & 0xffff); - } - - flush_pending(strm); - /* If avail_out is zero, the application will call deflate again - * to flush the rest. - */ - if (s.wrap > 0) { s.wrap = -s.wrap; } - /* write the trailer only once! */ - return s.pending !== 0 ? Z_OK : Z_STREAM_END; -} - -function deflateEnd(strm) { - var status; - - if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) { - return Z_STREAM_ERROR; - } - - status = strm.state.status; - if (status !== INIT_STATE && - status !== EXTRA_STATE && - status !== NAME_STATE && - status !== COMMENT_STATE && - status !== HCRC_STATE && - status !== BUSY_STATE && - status !== FINISH_STATE - ) { - return err(strm, Z_STREAM_ERROR); - } - - strm.state = null; - - return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK; -} - - -/* ========================================================================= - * Initializes the compression dictionary from the given byte - * sequence without producing any compressed output. - */ -function deflateSetDictionary(strm, dictionary) { - var dictLength = dictionary.length; - - var s; - var str, n; - var wrap; - var avail; - var next; - var input; - var tmpDict; - - if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) { - return Z_STREAM_ERROR; - } - - s = strm.state; - wrap = s.wrap; - - if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) { - return Z_STREAM_ERROR; - } - - /* when using zlib wrappers, compute Adler-32 for provided dictionary */ - if (wrap === 1) { - /* adler32(strm->adler, dictionary, dictLength); */ - strm.adler = adler32(strm.adler, dictionary, dictLength, 0); - } - - s.wrap = 0; /* avoid computing Adler-32 in read_buf */ - - /* if dictionary would fill window, just replace the history */ - if (dictLength >= s.w_size) { - if (wrap === 0) { /* already empty otherwise */ - /*** CLEAR_HASH(s); ***/ - zero(s.head); // Fill with NIL (= 0); - s.strstart = 0; - s.block_start = 0; - s.insert = 0; + //Swap pivot to end of array + var aPtr = elemSize * (hi-1) + var bPtr = elemSize * pivot + for(var i=0; i= MIN_MATCH) { - str = s.strstart; - n = s.lookahead - (MIN_MATCH - 1); - do { - /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ - s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask; + var y = ids[hi-1] + ids[hi-1] = ids[pivot] + ids[pivot] = y - s.prev[str & s.w_mask] = s.head[s.ins_h]; + //Partition using pivot + pivot = partitionStartLessThan( + d, axis, + lo, hi-1, boxes, ids, + value) - s.head[s.ins_h] = str; - str++; - } while (--n); - s.strstart = str; - s.lookahead = MIN_MATCH - 1; - fill_window(s); + //Swap pivot back + var aPtr = elemSize * (hi-1) + var bPtr = elemSize * pivot + for(var i=0; i= 0) { + reads.push('lo=e[k+n]') + } + if(predicate.indexOf('hi') >= 0) { + reads.push('hi=e[k+o]') + } + fargs.push( + code.replace('_', reads.join()) + .replace('$', predicate)) + return Function.apply(void 0, fargs) +} +},{}],41:[function(require,module,exports){ 'use strict'; -// See state defs from inflate.js -var BAD = 30; /* got a data error -- remain here until reset */ -var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ +//This code is extracted from ndarray-sort +//It is inlined here as a temporary workaround -/* - Decode literal, length, and distance codes and write out the resulting - literal and match bytes until either not enough input or output is - available, an end-of-block is encountered, or a data error is encountered. - When large enough input and output buffers are supplied to inflate(), for - example, a 16K input buffer and a 64K output buffer, more than 95% of the - inflate execution time is spent in this routine. +module.exports = wrapper; - Entry assumptions: +var INSERT_SORT_CUTOFF = 32 - state.mode === LEN - strm.avail_in >= 6 - strm.avail_out >= 258 - start >= strm.avail_out - state.bits < 8 +function wrapper(data, n0) { + if (n0 <= 4*INSERT_SORT_CUTOFF) { + insertionSort(0, n0 - 1, data); + } else { + quickSort(0, n0 - 1, data); + } +} - On return, state.mode is one of: - - LEN -- ran out of enough output space or enough available input - TYPE -- reached end of block code, inflate() to interpret next block - BAD -- error in block data - - Notes: - - - The maximum input bits used by a length/distance pair is 15 bits for the - length code, 5 bits for the length extra, 15 bits for the distance code, - and 13 bits for the distance extra. This totals 48 bits, or six bytes. - Therefore if strm.avail_in >= 6, then there is enough input to avoid - checking for available input while decoding. - - - The maximum bytes that a single length/distance pair can output is 258 - bytes, which is the maximum length that can be coded. inflate_fast() - requires strm.avail_out >= 258 for each loop to avoid checking for - output space. - */ -module.exports = function inflate_fast(strm, start) { - var state; - var _in; /* local strm.input */ - var last; /* have enough input while in < last */ - var _out; /* local strm.output */ - var beg; /* inflate()'s initial strm.output */ - var end; /* while out < end, enough space available */ -//#ifdef INFLATE_STRICT - var dmax; /* maximum distance from zlib header */ -//#endif - var wsize; /* window size or zero if not using window */ - var whave; /* valid bytes in the window */ - var wnext; /* window write index */ - // Use `s_window` instead `window`, avoid conflict with instrumentation tools - var s_window; /* allocated sliding window, if wsize != 0 */ - var hold; /* local strm.hold */ - var bits; /* local strm.bits */ - var lcode; /* local strm.lencode */ - var dcode; /* local strm.distcode */ - var lmask; /* mask for first level of length codes */ - var dmask; /* mask for first level of distance codes */ - var here; /* retrieved table entry */ - var op; /* code bits, operation, extra bits, or */ - /* window position, window bytes to copy */ - var len; /* match length, unused bytes */ - var dist; /* match distance */ - var from; /* where to copy match from */ - var from_source; - - - var input, output; // JS specific, because we have no pointers - - /* copy state to local variables */ - state = strm.state; - //here = state.here; - _in = strm.next_in; - input = strm.input; - last = _in + (strm.avail_in - 5); - _out = strm.next_out; - output = strm.output; - beg = _out - (start - strm.avail_out); - end = _out + (strm.avail_out - 257); -//#ifdef INFLATE_STRICT - dmax = state.dmax; -//#endif - wsize = state.wsize; - whave = state.whave; - wnext = state.wnext; - s_window = state.window; - hold = state.hold; - bits = state.bits; - lcode = state.lencode; - dcode = state.distcode; - lmask = (1 << state.lenbits) - 1; - dmask = (1 << state.distbits) - 1; - - - /* decode literals and length/distances until end-of-block or not enough - input data or output space */ - - top: - do { - if (bits < 15) { - hold += input[_in++] << bits; - bits += 8; - hold += input[_in++] << bits; - bits += 8; +function insertionSort(left, right, data) { + var ptr = 2*(left+1) + for(var i=left+1; i<=right; ++i) { + var a = data[ptr++] + var b = data[ptr++] + var j = i + var jptr = ptr-2 + while(j-- > left) { + var x = data[jptr-2] + var y = data[jptr-1] + if(x < a) { + break + } else if(x === a && y < b) { + break + } + data[jptr] = x + data[jptr+1] = y + jptr -= 2 } - - here = lcode[hold & lmask]; - - dolen: - for (;;) { // Goto emulation - op = here >>> 24/*here.bits*/; - hold >>>= op; - bits -= op; - op = (here >>> 16) & 0xff/*here.op*/; - if (op === 0) { /* literal */ - //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? - // "inflate: literal '%c'\n" : - // "inflate: literal 0x%02x\n", here.val)); - output[_out++] = here & 0xffff/*here.val*/; - } - else if (op & 16) { /* length base */ - len = here & 0xffff/*here.val*/; - op &= 15; /* number of extra bits */ - if (op) { - if (bits < op) { - hold += input[_in++] << bits; - bits += 8; - } - len += hold & ((1 << op) - 1); - hold >>>= op; - bits -= op; - } - //Tracevv((stderr, "inflate: length %u\n", len)); - if (bits < 15) { - hold += input[_in++] << bits; - bits += 8; - hold += input[_in++] << bits; - bits += 8; - } - here = dcode[hold & dmask]; - - dodist: - for (;;) { // goto emulation - op = here >>> 24/*here.bits*/; - hold >>>= op; - bits -= op; - op = (here >>> 16) & 0xff/*here.op*/; - - if (op & 16) { /* distance base */ - dist = here & 0xffff/*here.val*/; - op &= 15; /* number of extra bits */ - if (bits < op) { - hold += input[_in++] << bits; - bits += 8; - if (bits < op) { - hold += input[_in++] << bits; - bits += 8; - } - } - dist += hold & ((1 << op) - 1); -//#ifdef INFLATE_STRICT - if (dist > dmax) { - strm.msg = 'invalid distance too far back'; - state.mode = BAD; - break top; - } -//#endif - hold >>>= op; - bits -= op; - //Tracevv((stderr, "inflate: distance %u\n", dist)); - op = _out - beg; /* max distance in output */ - if (dist > op) { /* see if copy from window */ - op = dist - op; /* distance back in window */ - if (op > whave) { - if (state.sane) { - strm.msg = 'invalid distance too far back'; - state.mode = BAD; - break top; - } - -// (!) This block is disabled in zlib defailts, -// don't enable it for binary compatibility -//#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR -// if (len <= op - whave) { -// do { -// output[_out++] = 0; -// } while (--len); -// continue top; -// } -// len -= op - whave; -// do { -// output[_out++] = 0; -// } while (--op > whave); -// if (op === 0) { -// from = _out - dist; -// do { -// output[_out++] = output[from++]; -// } while (--len); -// continue top; -// } -//#endif - } - from = 0; // window index - from_source = s_window; - if (wnext === 0) { /* very common case */ - from += wsize - op; - if (op < len) { /* some from window */ - len -= op; - do { - output[_out++] = s_window[from++]; - } while (--op); - from = _out - dist; /* rest from output */ - from_source = output; - } - } - else if (wnext < op) { /* wrap around window */ - from += wsize + wnext - op; - op -= wnext; - if (op < len) { /* some from end of window */ - len -= op; - do { - output[_out++] = s_window[from++]; - } while (--op); - from = 0; - if (wnext < len) { /* some from start of window */ - op = wnext; - len -= op; - do { - output[_out++] = s_window[from++]; - } while (--op); - from = _out - dist; /* rest from output */ - from_source = output; - } - } - } - else { /* contiguous in window */ - from += wnext - op; - if (op < len) { /* some from window */ - len -= op; - do { - output[_out++] = s_window[from++]; - } while (--op); - from = _out - dist; /* rest from output */ - from_source = output; - } - } - while (len > 2) { - output[_out++] = from_source[from++]; - output[_out++] = from_source[from++]; - output[_out++] = from_source[from++]; - len -= 3; - } - if (len) { - output[_out++] = from_source[from++]; - if (len > 1) { - output[_out++] = from_source[from++]; - } - } - } - else { - from = _out - dist; /* copy direct from output */ - do { /* minimum length is three */ - output[_out++] = output[from++]; - output[_out++] = output[from++]; - output[_out++] = output[from++]; - len -= 3; - } while (len > 2); - if (len) { - output[_out++] = output[from++]; - if (len > 1) { - output[_out++] = output[from++]; - } - } - } - } - else if ((op & 64) === 0) { /* 2nd level distance code */ - here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))]; - continue dodist; - } - else { - strm.msg = 'invalid distance code'; - state.mode = BAD; - break top; - } - - break; // need to emulate goto via "continue" - } - } - else if ((op & 64) === 0) { /* 2nd level length code */ - here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))]; - continue dolen; - } - else if (op & 32) { /* end-of-block */ - //Tracevv((stderr, "inflate: end of block\n")); - state.mode = TYPE; - break top; - } - else { - strm.msg = 'invalid literal/length code'; - state.mode = BAD; - break top; - } - - break; // need to emulate goto via "continue" - } - } while (_in < last && _out < end); - - /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ - len = bits >> 3; - _in -= len; - bits -= len << 3; - hold &= (1 << bits) - 1; - - /* update state and return */ - strm.next_in = _in; - strm.next_out = _out; - strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last)); - strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end)); - state.hold = hold; - state.bits = bits; - return; -}; - -},{}],26:[function(require,module,exports){ -'use strict'; - - -var utils = require('../utils/common'); -var adler32 = require('./adler32'); -var crc32 = require('./crc32'); -var inflate_fast = require('./inffast'); -var inflate_table = require('./inftrees'); - -var CODES = 0; -var LENS = 1; -var DISTS = 2; - -/* Public constants ==========================================================*/ -/* ===========================================================================*/ - - -/* Allowed flush values; see deflate() and inflate() below for details */ -//var Z_NO_FLUSH = 0; -//var Z_PARTIAL_FLUSH = 1; -//var Z_SYNC_FLUSH = 2; -//var Z_FULL_FLUSH = 3; -var Z_FINISH = 4; -var Z_BLOCK = 5; -var Z_TREES = 6; - - -/* Return codes for the compression/decompression functions. Negative values - * are errors, positive values are used for special but normal events. - */ -var Z_OK = 0; -var Z_STREAM_END = 1; -var Z_NEED_DICT = 2; -//var Z_ERRNO = -1; -var Z_STREAM_ERROR = -2; -var Z_DATA_ERROR = -3; -var Z_MEM_ERROR = -4; -var Z_BUF_ERROR = -5; -//var Z_VERSION_ERROR = -6; - -/* The deflate compression method */ -var Z_DEFLATED = 8; - - -/* STATES ====================================================================*/ -/* ===========================================================================*/ - - -var HEAD = 1; /* i: waiting for magic header */ -var FLAGS = 2; /* i: waiting for method and flags (gzip) */ -var TIME = 3; /* i: waiting for modification time (gzip) */ -var OS = 4; /* i: waiting for extra flags and operating system (gzip) */ -var EXLEN = 5; /* i: waiting for extra length (gzip) */ -var EXTRA = 6; /* i: waiting for extra bytes (gzip) */ -var NAME = 7; /* i: waiting for end of file name (gzip) */ -var COMMENT = 8; /* i: waiting for end of comment (gzip) */ -var HCRC = 9; /* i: waiting for header crc (gzip) */ -var DICTID = 10; /* i: waiting for dictionary check value */ -var DICT = 11; /* waiting for inflateSetDictionary() call */ -var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ -var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */ -var STORED = 14; /* i: waiting for stored size (length and complement) */ -var COPY_ = 15; /* i/o: same as COPY below, but only first time in */ -var COPY = 16; /* i/o: waiting for input or output to copy stored block */ -var TABLE = 17; /* i: waiting for dynamic block table lengths */ -var LENLENS = 18; /* i: waiting for code length code lengths */ -var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */ -var LEN_ = 20; /* i: same as LEN below, but only first time in */ -var LEN = 21; /* i: waiting for length/lit/eob code */ -var LENEXT = 22; /* i: waiting for length extra bits */ -var DIST = 23; /* i: waiting for distance code */ -var DISTEXT = 24; /* i: waiting for distance extra bits */ -var MATCH = 25; /* o: waiting for output space to copy string */ -var LIT = 26; /* o: waiting for output space to write literal */ -var CHECK = 27; /* i: waiting for 32-bit check value */ -var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */ -var DONE = 29; /* finished check, done -- remain here until reset */ -var BAD = 30; /* got a data error -- remain here until reset */ -var MEM = 31; /* got an inflate() memory error -- remain here until reset */ -var SYNC = 32; /* looking for synchronization bytes to restart inflate() */ - -/* ===========================================================================*/ - - - -var ENOUGH_LENS = 852; -var ENOUGH_DISTS = 592; -//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); - -var MAX_WBITS = 15; -/* 32K LZ77 window */ -var DEF_WBITS = MAX_WBITS; - - -function zswap32(q) { - return (((q >>> 24) & 0xff) + - ((q >>> 8) & 0xff00) + - ((q & 0xff00) << 8) + - ((q & 0xff) << 24)); + data[jptr] = a + data[jptr+1] = b + } } - -function InflateState() { - this.mode = 0; /* current inflate mode */ - this.last = false; /* true if processing last block */ - this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ - this.havedict = false; /* true if dictionary provided */ - this.flags = 0; /* gzip header method and flags (0 if zlib) */ - this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */ - this.check = 0; /* protected copy of check value */ - this.total = 0; /* protected copy of output count */ - // TODO: may be {} - this.head = null; /* where to save gzip header information */ - - /* sliding window */ - this.wbits = 0; /* log base 2 of requested window size */ - this.wsize = 0; /* window size or zero if not using window */ - this.whave = 0; /* valid bytes in the window */ - this.wnext = 0; /* window write index */ - this.window = null; /* allocated sliding window, if needed */ - - /* bit accumulator */ - this.hold = 0; /* input bit accumulator */ - this.bits = 0; /* number of bits in "in" */ - - /* for string and stored block copying */ - this.length = 0; /* literal or length of data to copy */ - this.offset = 0; /* distance back to copy string from */ - - /* for table and code decoding */ - this.extra = 0; /* extra bits needed */ - - /* fixed and dynamic code tables */ - this.lencode = null; /* starting table for length/literal codes */ - this.distcode = null; /* starting table for distance codes */ - this.lenbits = 0; /* index bits for lencode */ - this.distbits = 0; /* index bits for distcode */ - - /* dynamic table building */ - this.ncode = 0; /* number of code length code lengths */ - this.nlen = 0; /* number of length code lengths */ - this.ndist = 0; /* number of distance code lengths */ - this.have = 0; /* number of code lengths in lens[] */ - this.next = null; /* next available space in codes[] */ - - this.lens = new utils.Buf16(320); /* temporary storage for code lengths */ - this.work = new utils.Buf16(288); /* work area for code table building */ - - /* - because we don't have pointers in js, we use lencode and distcode directly - as buffers so we don't need codes - */ - //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */ - this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */ - this.distdyn = null; /* dynamic table for distance codes (JS specific) */ - this.sane = 0; /* if false, allow invalid distance too far */ - this.back = 0; /* bits back of last unprocessed length/lit */ - this.was = 0; /* initial length of match */ +function swap(i, j, data) { + i *= 2 + j *= 2 + var x = data[i] + var y = data[i+1] + data[i] = data[j] + data[i+1] = data[j+1] + data[j] = x + data[j+1] = y } -function inflateResetKeep(strm) { - var state; - - if (!strm || !strm.state) { return Z_STREAM_ERROR; } - state = strm.state; - strm.total_in = strm.total_out = state.total = 0; - strm.msg = ''; /*Z_NULL*/ - if (state.wrap) { /* to support ill-conceived Java test suite */ - strm.adler = state.wrap & 1; - } - state.mode = HEAD; - state.last = 0; - state.havedict = 0; - state.dmax = 32768; - state.head = null/*Z_NULL*/; - state.hold = 0; - state.bits = 0; - //state.lencode = state.distcode = state.next = state.codes; - state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS); - state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS); - - state.sane = 1; - state.back = -1; - //Tracev((stderr, "inflate: reset\n")); - return Z_OK; +function move(i, j, data) { + i *= 2 + j *= 2 + data[i] = data[j] + data[i+1] = data[j+1] } -function inflateReset(strm) { - var state; - - if (!strm || !strm.state) { return Z_STREAM_ERROR; } - state = strm.state; - state.wsize = 0; - state.whave = 0; - state.wnext = 0; - return inflateResetKeep(strm); - +function rotate(i, j, k, data) { + i *= 2 + j *= 2 + k *= 2 + var x = data[i] + var y = data[i+1] + data[i] = data[j] + data[i+1] = data[j+1] + data[j] = data[k] + data[j+1] = data[k+1] + data[k] = x + data[k+1] = y } -function inflateReset2(strm, windowBits) { - var wrap; - var state; - - /* get the state */ - if (!strm || !strm.state) { return Z_STREAM_ERROR; } - state = strm.state; - - /* extract wrap request from windowBits parameter */ - if (windowBits < 0) { - wrap = 0; - windowBits = -windowBits; - } - else { - wrap = (windowBits >> 4) + 1; - if (windowBits < 48) { - windowBits &= 15; - } - } - - /* set number of window bits, free window if different */ - if (windowBits && (windowBits < 8 || windowBits > 15)) { - return Z_STREAM_ERROR; - } - if (state.window !== null && state.wbits !== windowBits) { - state.window = null; - } - - /* update state and reset the rest of it */ - state.wrap = wrap; - state.wbits = windowBits; - return inflateReset(strm); +function shufflePivot(i, j, px, py, data) { + i *= 2 + j *= 2 + data[i] = data[j] + data[j] = px + data[i+1] = data[j+1] + data[j+1] = py } -function inflateInit2(strm, windowBits) { - var ret; - var state; - - if (!strm) { return Z_STREAM_ERROR; } - //strm.msg = Z_NULL; /* in case we return an error */ - - state = new InflateState(); - - //if (state === Z_NULL) return Z_MEM_ERROR; - //Tracev((stderr, "inflate: allocated\n")); - strm.state = state; - state.window = null/*Z_NULL*/; - ret = inflateReset2(strm, windowBits); - if (ret !== Z_OK) { - strm.state = null/*Z_NULL*/; +function compare(i, j, data) { + i *= 2 + j *= 2 + var x = data[i], + y = data[j] + if(x < y) { + return false + } else if(x === y) { + return data[i+1] > data[j+1] } - return ret; + return true } -function inflateInit(strm) { - return inflateInit2(strm, DEF_WBITS); +function comparePivot(i, y, b, data) { + i *= 2 + var x = data[i] + if(x < y) { + return true + } else if(x === y) { + return data[i+1] < b + } + return false } - -/* - Return state with length and distance decoding tables and index sizes set to - fixed code decoding. Normally this returns fixed tables from inffixed.h. - If BUILDFIXED is defined, then instead this routine builds the tables the - first time it's called, and returns those tables the first time and - thereafter. This reduces the size of the code by about 2K bytes, in - exchange for a little execution time. However, BUILDFIXED should not be - used for threaded applications, since the rewriting of the tables and virgin - may not be thread-safe. - */ -var virgin = true; - -var lenfix, distfix; // We have no pointers in JS, so keep tables separate - -function fixedtables(state) { - /* build fixed huffman tables if first call (may not be thread safe) */ - if (virgin) { - var sym; - - lenfix = new utils.Buf32(512); - distfix = new utils.Buf32(32); - - /* literal/length table */ - sym = 0; - while (sym < 144) { state.lens[sym++] = 8; } - while (sym < 256) { state.lens[sym++] = 9; } - while (sym < 280) { state.lens[sym++] = 7; } - while (sym < 288) { state.lens[sym++] = 8; } - - inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 }); - - /* distance table */ - sym = 0; - while (sym < 32) { state.lens[sym++] = 5; } - - inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 }); - - /* do this just once */ - virgin = false; +function quickSort(left, right, data) { + var sixth = (right - left + 1) / 6 | 0, + index1 = left + sixth, + index5 = right - sixth, + index3 = left + right >> 1, + index2 = index3 - sixth, + index4 = index3 + sixth, + el1 = index1, + el2 = index2, + el3 = index3, + el4 = index4, + el5 = index5, + less = left + 1, + great = right - 1, + tmp = 0 + if(compare(el1, el2, data)) { + tmp = el1 + el1 = el2 + el2 = tmp + } + if(compare(el4, el5, data)) { + tmp = el4 + el4 = el5 + el5 = tmp + } + if(compare(el1, el3, data)) { + tmp = el1 + el1 = el3 + el3 = tmp + } + if(compare(el2, el3, data)) { + tmp = el2 + el2 = el3 + el3 = tmp + } + if(compare(el1, el4, data)) { + tmp = el1 + el1 = el4 + el4 = tmp + } + if(compare(el3, el4, data)) { + tmp = el3 + el3 = el4 + el4 = tmp + } + if(compare(el2, el5, data)) { + tmp = el2 + el2 = el5 + el5 = tmp + } + if(compare(el2, el3, data)) { + tmp = el2 + el2 = el3 + el3 = tmp + } + if(compare(el4, el5, data)) { + tmp = el4 + el4 = el5 + el5 = tmp } - state.lencode = lenfix; - state.lenbits = 9; - state.distcode = distfix; - state.distbits = 5; -} + var pivot1X = data[2*el2] + var pivot1Y = data[2*el2+1] + var pivot2X = data[2*el4] + var pivot2Y = data[2*el4+1] - -/* - Update the window with the last wsize (normally 32K) bytes written before - returning. If window does not exist yet, create it. This is only called - when a window is already in use, or when output has been written during this - inflate call, but the end of the deflate stream has not been reached yet. - It is also called to create a window for dictionary data when a dictionary - is loaded. - - Providing output buffers larger than 32K to inflate() should provide a speed - advantage, since only the last 32K of output is copied to the sliding window - upon return from inflate(), and since all distances after the first 32K of - output will fall in the output data, making match copies simpler and faster. - The advantage may be dependent on the size of the processor's data caches. - */ -function updatewindow(strm, src, end, copy) { - var dist; - var state = strm.state; - - /* if it hasn't been done already, allocate space for the window */ - if (state.window === null) { - state.wsize = 1 << state.wbits; - state.wnext = 0; - state.whave = 0; - - state.window = new utils.Buf8(state.wsize); + var ptr0 = 2 * el1; + var ptr2 = 2 * el3; + var ptr4 = 2 * el5; + var ptr5 = 2 * index1; + var ptr6 = 2 * index3; + var ptr7 = 2 * index5; + for (var i1 = 0; i1 < 2; ++i1) { + var x = data[ptr0+i1]; + var y = data[ptr2+i1]; + var z = data[ptr4+i1]; + data[ptr5+i1] = x; + data[ptr6+i1] = y; + data[ptr7+i1] = z; } - /* copy state->wsize or less output bytes into the circular window */ - if (copy >= state.wsize) { - utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0); - state.wnext = 0; - state.whave = state.wsize; - } - else { - dist = state.wsize - state.wnext; - if (dist > copy) { - dist = copy; - } - //zmemcpy(state->window + state->wnext, end - copy, dist); - utils.arraySet(state.window, src, end - copy, dist, state.wnext); - copy -= dist; - if (copy) { - //zmemcpy(state->window, end - copy, copy); - utils.arraySet(state.window, src, end - copy, copy, 0); - state.wnext = copy; - state.whave = state.wsize; - } - else { - state.wnext += dist; - if (state.wnext === state.wsize) { state.wnext = 0; } - if (state.whave < state.wsize) { state.whave += dist; } - } - } - return 0; -} - -function inflate(strm, flush) { - var state; - var input, output; // input/output buffers - var next; /* next input INDEX */ - var put; /* next output INDEX */ - var have, left; /* available input and output */ - var hold; /* bit buffer */ - var bits; /* bits in bit buffer */ - var _in, _out; /* save starting available input and output */ - var copy; /* number of stored or match bytes to copy */ - var from; /* where to copy match bytes from */ - var from_source; - var here = 0; /* current decoding table entry */ - var here_bits, here_op, here_val; // paked "here" denormalized (JS specific) - //var last; /* parent table entry */ - var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) - var len; /* length to copy for repeats, bits to drop */ - var ret; /* return code */ - var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */ - var opts; - - var n; // temporary var for NEED_BITS - - var order = /* permutation of code lengths */ - [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]; - - - if (!strm || !strm.state || !strm.output || - (!strm.input && strm.avail_in !== 0)) { - return Z_STREAM_ERROR; - } - - state = strm.state; - if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */ - - - //--- LOAD() --- - put = strm.next_out; - output = strm.output; - left = strm.avail_out; - next = strm.next_in; - input = strm.input; - have = strm.avail_in; - hold = state.hold; - bits = state.bits; - //--- - - _in = have; - _out = left; - ret = Z_OK; - - inf_leave: // goto emulation - for (;;) { - switch (state.mode) { - case HEAD: - if (state.wrap === 0) { - state.mode = TYPEDO; - break; + move(index2, left, data) + move(index4, right, data) + for (var k = less; k <= great; ++k) { + if (comparePivot(k, pivot1X, pivot1Y, data)) { + if (k !== less) { + swap(k, less, data) } - //=== NEEDBITS(16); - while (bits < 16) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */ - state.check = 0/*crc32(0L, Z_NULL, 0)*/; - //=== CRC2(state.check, hold); - hbuf[0] = hold & 0xff; - hbuf[1] = (hold >>> 8) & 0xff; - state.check = crc32(state.check, hbuf, 2, 0); - //===// - - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - state.mode = FLAGS; - break; - } - state.flags = 0; /* expect zlib header */ - if (state.head) { - state.head.done = false; - } - if (!(state.wrap & 1) || /* check if zlib header allowed */ - (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) { - strm.msg = 'incorrect header check'; - state.mode = BAD; - break; - } - if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) { - strm.msg = 'unknown compression method'; - state.mode = BAD; - break; - } - //--- DROPBITS(4) ---// - hold >>>= 4; - bits -= 4; - //---// - len = (hold & 0x0f)/*BITS(4)*/ + 8; - if (state.wbits === 0) { - state.wbits = len; - } - else if (len > state.wbits) { - strm.msg = 'invalid window size'; - state.mode = BAD; - break; - } - state.dmax = 1 << len; - //Tracev((stderr, "inflate: zlib header ok\n")); - strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; - state.mode = hold & 0x200 ? DICTID : TYPE; - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - break; - case FLAGS: - //=== NEEDBITS(16); */ - while (bits < 16) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - state.flags = hold; - if ((state.flags & 0xff) !== Z_DEFLATED) { - strm.msg = 'unknown compression method'; - state.mode = BAD; - break; - } - if (state.flags & 0xe000) { - strm.msg = 'unknown header flags set'; - state.mode = BAD; - break; - } - if (state.head) { - state.head.text = ((hold >> 8) & 1); - } - if (state.flags & 0x0200) { - //=== CRC2(state.check, hold); - hbuf[0] = hold & 0xff; - hbuf[1] = (hold >>> 8) & 0xff; - state.check = crc32(state.check, hbuf, 2, 0); - //===// - } - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - state.mode = TIME; - /* falls through */ - case TIME: - //=== NEEDBITS(32); */ - while (bits < 32) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - if (state.head) { - state.head.time = hold; - } - if (state.flags & 0x0200) { - //=== CRC4(state.check, hold) - hbuf[0] = hold & 0xff; - hbuf[1] = (hold >>> 8) & 0xff; - hbuf[2] = (hold >>> 16) & 0xff; - hbuf[3] = (hold >>> 24) & 0xff; - state.check = crc32(state.check, hbuf, 4, 0); - //=== - } - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - state.mode = OS; - /* falls through */ - case OS: - //=== NEEDBITS(16); */ - while (bits < 16) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - if (state.head) { - state.head.xflags = (hold & 0xff); - state.head.os = (hold >> 8); - } - if (state.flags & 0x0200) { - //=== CRC2(state.check, hold); - hbuf[0] = hold & 0xff; - hbuf[1] = (hold >>> 8) & 0xff; - state.check = crc32(state.check, hbuf, 2, 0); - //===// - } - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - state.mode = EXLEN; - /* falls through */ - case EXLEN: - if (state.flags & 0x0400) { - //=== NEEDBITS(16); */ - while (bits < 16) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - state.length = hold; - if (state.head) { - state.head.extra_len = hold; - } - if (state.flags & 0x0200) { - //=== CRC2(state.check, hold); - hbuf[0] = hold & 0xff; - hbuf[1] = (hold >>> 8) & 0xff; - state.check = crc32(state.check, hbuf, 2, 0); - //===// - } - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - } - else if (state.head) { - state.head.extra = null/*Z_NULL*/; - } - state.mode = EXTRA; - /* falls through */ - case EXTRA: - if (state.flags & 0x0400) { - copy = state.length; - if (copy > have) { copy = have; } - if (copy) { - if (state.head) { - len = state.head.extra_len - state.length; - if (!state.head.extra) { - // Use untyped array for more conveniend processing later - state.head.extra = new Array(state.head.extra_len); - } - utils.arraySet( - state.head.extra, - input, - next, - // extra field is limited to 65536 bytes - // - no need for additional size check - copy, - /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ - len - ); - //zmemcpy(state.head.extra + len, next, - // len + copy > state.head.extra_max ? - // state.head.extra_max - len : copy); - } - if (state.flags & 0x0200) { - state.check = crc32(state.check, input, copy, next); - } - have -= copy; - next += copy; - state.length -= copy; - } - if (state.length) { break inf_leave; } - } - state.length = 0; - state.mode = NAME; - /* falls through */ - case NAME: - if (state.flags & 0x0800) { - if (have === 0) { break inf_leave; } - copy = 0; - do { - // TODO: 2 or 1 bytes? - len = input[next + copy++]; - /* use constant limit because in js we should not preallocate memory */ - if (state.head && len && - (state.length < 65536 /*state.head.name_max*/)) { - state.head.name += String.fromCharCode(len); - } - } while (len && copy < have); - - if (state.flags & 0x0200) { - state.check = crc32(state.check, input, copy, next); - } - have -= copy; - next += copy; - if (len) { break inf_leave; } - } - else if (state.head) { - state.head.name = null; - } - state.length = 0; - state.mode = COMMENT; - /* falls through */ - case COMMENT: - if (state.flags & 0x1000) { - if (have === 0) { break inf_leave; } - copy = 0; - do { - len = input[next + copy++]; - /* use constant limit because in js we should not preallocate memory */ - if (state.head && len && - (state.length < 65536 /*state.head.comm_max*/)) { - state.head.comment += String.fromCharCode(len); - } - } while (len && copy < have); - if (state.flags & 0x0200) { - state.check = crc32(state.check, input, copy, next); - } - have -= copy; - next += copy; - if (len) { break inf_leave; } - } - else if (state.head) { - state.head.comment = null; - } - state.mode = HCRC; - /* falls through */ - case HCRC: - if (state.flags & 0x0200) { - //=== NEEDBITS(16); */ - while (bits < 16) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - if (hold !== (state.check & 0xffff)) { - strm.msg = 'header crc mismatch'; - state.mode = BAD; - break; - } - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - } - if (state.head) { - state.head.hcrc = ((state.flags >> 9) & 1); - state.head.done = true; - } - strm.adler = state.check = 0; - state.mode = TYPE; - break; - case DICTID: - //=== NEEDBITS(32); */ - while (bits < 32) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - strm.adler = state.check = zswap32(hold); - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - state.mode = DICT; - /* falls through */ - case DICT: - if (state.havedict === 0) { - //--- RESTORE() --- - strm.next_out = put; - strm.avail_out = left; - strm.next_in = next; - strm.avail_in = have; - state.hold = hold; - state.bits = bits; - //--- - return Z_NEED_DICT; - } - strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; - state.mode = TYPE; - /* falls through */ - case TYPE: - if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; } - /* falls through */ - case TYPEDO: - if (state.last) { - //--- BYTEBITS() ---// - hold >>>= bits & 7; - bits -= bits & 7; - //---// - state.mode = CHECK; - break; - } - //=== NEEDBITS(3); */ - while (bits < 3) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - state.last = (hold & 0x01)/*BITS(1)*/; - //--- DROPBITS(1) ---// - hold >>>= 1; - bits -= 1; - //---// - - switch ((hold & 0x03)/*BITS(2)*/) { - case 0: /* stored block */ - //Tracev((stderr, "inflate: stored block%s\n", - // state.last ? " (last)" : "")); - state.mode = STORED; - break; - case 1: /* fixed block */ - fixedtables(state); - //Tracev((stderr, "inflate: fixed codes block%s\n", - // state.last ? " (last)" : "")); - state.mode = LEN_; /* decode codes */ - if (flush === Z_TREES) { - //--- DROPBITS(2) ---// - hold >>>= 2; - bits -= 2; - //---// - break inf_leave; - } - break; - case 2: /* dynamic block */ - //Tracev((stderr, "inflate: dynamic codes block%s\n", - // state.last ? " (last)" : "")); - state.mode = TABLE; - break; - case 3: - strm.msg = 'invalid block type'; - state.mode = BAD; - } - //--- DROPBITS(2) ---// - hold >>>= 2; - bits -= 2; - //---// - break; - case STORED: - //--- BYTEBITS() ---// /* go to byte boundary */ - hold >>>= bits & 7; - bits -= bits & 7; - //---// - //=== NEEDBITS(32); */ - while (bits < 32) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) { - strm.msg = 'invalid stored block lengths'; - state.mode = BAD; - break; - } - state.length = hold & 0xffff; - //Tracev((stderr, "inflate: stored length %u\n", - // state.length)); - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - state.mode = COPY_; - if (flush === Z_TREES) { break inf_leave; } - /* falls through */ - case COPY_: - state.mode = COPY; - /* falls through */ - case COPY: - copy = state.length; - if (copy) { - if (copy > have) { copy = have; } - if (copy > left) { copy = left; } - if (copy === 0) { break inf_leave; } - //--- zmemcpy(put, next, copy); --- - utils.arraySet(output, input, next, copy, put); - //---// - have -= copy; - next += copy; - left -= copy; - put += copy; - state.length -= copy; - break; - } - //Tracev((stderr, "inflate: stored end\n")); - state.mode = TYPE; - break; - case TABLE: - //=== NEEDBITS(14); */ - while (bits < 14) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257; - //--- DROPBITS(5) ---// - hold >>>= 5; - bits -= 5; - //---// - state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1; - //--- DROPBITS(5) ---// - hold >>>= 5; - bits -= 5; - //---// - state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4; - //--- DROPBITS(4) ---// - hold >>>= 4; - bits -= 4; - //---// -//#ifndef PKZIP_BUG_WORKAROUND - if (state.nlen > 286 || state.ndist > 30) { - strm.msg = 'too many length or distance symbols'; - state.mode = BAD; - break; - } -//#endif - //Tracev((stderr, "inflate: table sizes ok\n")); - state.have = 0; - state.mode = LENLENS; - /* falls through */ - case LENLENS: - while (state.have < state.ncode) { - //=== NEEDBITS(3); - while (bits < 3) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - state.lens[order[state.have++]] = (hold & 0x07);//BITS(3); - //--- DROPBITS(3) ---// - hold >>>= 3; - bits -= 3; - //---// - } - while (state.have < 19) { - state.lens[order[state.have++]] = 0; - } - // We have separate tables & no pointers. 2 commented lines below not needed. - //state.next = state.codes; - //state.lencode = state.next; - // Switch to use dynamic table - state.lencode = state.lendyn; - state.lenbits = 7; - - opts = { bits: state.lenbits }; - ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts); - state.lenbits = opts.bits; - - if (ret) { - strm.msg = 'invalid code lengths set'; - state.mode = BAD; - break; - } - //Tracev((stderr, "inflate: code lengths ok\n")); - state.have = 0; - state.mode = CODELENS; - /* falls through */ - case CODELENS: - while (state.have < state.nlen + state.ndist) { - for (;;) { - here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/ - here_bits = here >>> 24; - here_op = (here >>> 16) & 0xff; - here_val = here & 0xffff; - - if ((here_bits) <= bits) { break; } - //--- PULLBYTE() ---// - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - //---// - } - if (here_val < 16) { - //--- DROPBITS(here.bits) ---// - hold >>>= here_bits; - bits -= here_bits; - //---// - state.lens[state.have++] = here_val; - } - else { - if (here_val === 16) { - //=== NEEDBITS(here.bits + 2); - n = here_bits + 2; - while (bits < n) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - //--- DROPBITS(here.bits) ---// - hold >>>= here_bits; - bits -= here_bits; - //---// - if (state.have === 0) { - strm.msg = 'invalid bit length repeat'; - state.mode = BAD; + ++less; + } else { + if (!comparePivot(k, pivot2X, pivot2Y, data)) { + while (true) { + if (!comparePivot(great, pivot2X, pivot2Y, data)) { + if (--great < k) { break; } - len = state.lens[state.have - 1]; - copy = 3 + (hold & 0x03);//BITS(2); - //--- DROPBITS(2) ---// - hold >>>= 2; - bits -= 2; - //---// - } - else if (here_val === 17) { - //=== NEEDBITS(here.bits + 3); - n = here_bits + 3; - while (bits < n) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; + continue; + } else { + if (comparePivot(great, pivot1X, pivot1Y, data)) { + rotate(k, less, great, data) + ++less; + --great; + } else { + swap(k, great, data) + --great; } - //===// - //--- DROPBITS(here.bits) ---// - hold >>>= here_bits; - bits -= here_bits; - //---// - len = 0; - copy = 3 + (hold & 0x07);//BITS(3); - //--- DROPBITS(3) ---// - hold >>>= 3; - bits -= 3; - //---// - } - else { - //=== NEEDBITS(here.bits + 7); - n = here_bits + 7; - while (bits < n) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - //--- DROPBITS(here.bits) ---// - hold >>>= here_bits; - bits -= here_bits; - //---// - len = 0; - copy = 11 + (hold & 0x7f);//BITS(7); - //--- DROPBITS(7) ---// - hold >>>= 7; - bits -= 7; - //---// - } - if (state.have + copy > state.nlen + state.ndist) { - strm.msg = 'invalid bit length repeat'; - state.mode = BAD; break; } - while (copy--) { - state.lens[state.have++] = len; - } } } - - /* handle error breaks in while */ - if (state.mode === BAD) { break; } - - /* check for end-of-block code (better have one) */ - if (state.lens[256] === 0) { - strm.msg = 'invalid code -- missing end-of-block'; - state.mode = BAD; - break; - } - - /* build code tables -- note: do not change the lenbits or distbits - values here (9 and 6) without reading the comments in inftrees.h - concerning the ENOUGH constants, which depend on those values */ - state.lenbits = 9; - - opts = { bits: state.lenbits }; - ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); - // We have separate tables & no pointers. 2 commented lines below not needed. - // state.next_index = opts.table_index; - state.lenbits = opts.bits; - // state.lencode = state.next; - - if (ret) { - strm.msg = 'invalid literal/lengths set'; - state.mode = BAD; - break; - } - - state.distbits = 6; - //state.distcode.copy(state.codes); - // Switch to use dynamic table - state.distcode = state.distdyn; - opts = { bits: state.distbits }; - ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); - // We have separate tables & no pointers. 2 commented lines below not needed. - // state.next_index = opts.table_index; - state.distbits = opts.bits; - // state.distcode = state.next; - - if (ret) { - strm.msg = 'invalid distances set'; - state.mode = BAD; - break; - } - //Tracev((stderr, 'inflate: codes ok\n')); - state.mode = LEN_; - if (flush === Z_TREES) { break inf_leave; } - /* falls through */ - case LEN_: - state.mode = LEN; - /* falls through */ - case LEN: - if (have >= 6 && left >= 258) { - //--- RESTORE() --- - strm.next_out = put; - strm.avail_out = left; - strm.next_in = next; - strm.avail_in = have; - state.hold = hold; - state.bits = bits; - //--- - inflate_fast(strm, _out); - //--- LOAD() --- - put = strm.next_out; - output = strm.output; - left = strm.avail_out; - next = strm.next_in; - input = strm.input; - have = strm.avail_in; - hold = state.hold; - bits = state.bits; - //--- - - if (state.mode === TYPE) { - state.back = -1; - } - break; - } - state.back = 0; - for (;;) { - here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/ - here_bits = here >>> 24; - here_op = (here >>> 16) & 0xff; - here_val = here & 0xffff; - - if (here_bits <= bits) { break; } - //--- PULLBYTE() ---// - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - //---// - } - if (here_op && (here_op & 0xf0) === 0) { - last_bits = here_bits; - last_op = here_op; - last_val = here_val; - for (;;) { - here = state.lencode[last_val + - ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; - here_bits = here >>> 24; - here_op = (here >>> 16) & 0xff; - here_val = here & 0xffff; - - if ((last_bits + here_bits) <= bits) { break; } - //--- PULLBYTE() ---// - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - //---// - } - //--- DROPBITS(last.bits) ---// - hold >>>= last_bits; - bits -= last_bits; - //---// - state.back += last_bits; - } - //--- DROPBITS(here.bits) ---// - hold >>>= here_bits; - bits -= here_bits; - //---// - state.back += here_bits; - state.length = here_val; - if (here_op === 0) { - //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? - // "inflate: literal '%c'\n" : - // "inflate: literal 0x%02x\n", here.val)); - state.mode = LIT; - break; - } - if (here_op & 32) { - //Tracevv((stderr, "inflate: end of block\n")); - state.back = -1; - state.mode = TYPE; - break; - } - if (here_op & 64) { - strm.msg = 'invalid literal/length code'; - state.mode = BAD; - break; - } - state.extra = here_op & 15; - state.mode = LENEXT; - /* falls through */ - case LENEXT: - if (state.extra) { - //=== NEEDBITS(state.extra); - n = state.extra; - while (bits < n) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; - //--- DROPBITS(state.extra) ---// - hold >>>= state.extra; - bits -= state.extra; - //---// - state.back += state.extra; - } - //Tracevv((stderr, "inflate: length %u\n", state.length)); - state.was = state.length; - state.mode = DIST; - /* falls through */ - case DIST: - for (;;) { - here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/ - here_bits = here >>> 24; - here_op = (here >>> 16) & 0xff; - here_val = here & 0xffff; - - if ((here_bits) <= bits) { break; } - //--- PULLBYTE() ---// - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - //---// - } - if ((here_op & 0xf0) === 0) { - last_bits = here_bits; - last_op = here_op; - last_val = here_val; - for (;;) { - here = state.distcode[last_val + - ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; - here_bits = here >>> 24; - here_op = (here >>> 16) & 0xff; - here_val = here & 0xffff; - - if ((last_bits + here_bits) <= bits) { break; } - //--- PULLBYTE() ---// - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - //---// - } - //--- DROPBITS(last.bits) ---// - hold >>>= last_bits; - bits -= last_bits; - //---// - state.back += last_bits; - } - //--- DROPBITS(here.bits) ---// - hold >>>= here_bits; - bits -= here_bits; - //---// - state.back += here_bits; - if (here_op & 64) { - strm.msg = 'invalid distance code'; - state.mode = BAD; - break; - } - state.offset = here_val; - state.extra = (here_op) & 15; - state.mode = DISTEXT; - /* falls through */ - case DISTEXT: - if (state.extra) { - //=== NEEDBITS(state.extra); - n = state.extra; - while (bits < n) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; - //--- DROPBITS(state.extra) ---// - hold >>>= state.extra; - bits -= state.extra; - //---// - state.back += state.extra; - } -//#ifdef INFLATE_STRICT - if (state.offset > state.dmax) { - strm.msg = 'invalid distance too far back'; - state.mode = BAD; - break; - } -//#endif - //Tracevv((stderr, "inflate: distance %u\n", state.offset)); - state.mode = MATCH; - /* falls through */ - case MATCH: - if (left === 0) { break inf_leave; } - copy = _out - left; - if (state.offset > copy) { /* copy from window */ - copy = state.offset - copy; - if (copy > state.whave) { - if (state.sane) { - strm.msg = 'invalid distance too far back'; - state.mode = BAD; - break; - } -// (!) This block is disabled in zlib defailts, -// don't enable it for binary compatibility -//#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR -// Trace((stderr, "inflate.c too far\n")); -// copy -= state.whave; -// if (copy > state.length) { copy = state.length; } -// if (copy > left) { copy = left; } -// left -= copy; -// state.length -= copy; -// do { -// output[put++] = 0; -// } while (--copy); -// if (state.length === 0) { state.mode = LEN; } -// break; -//#endif - } - if (copy > state.wnext) { - copy -= state.wnext; - from = state.wsize - copy; - } - else { - from = state.wnext - copy; - } - if (copy > state.length) { copy = state.length; } - from_source = state.window; - } - else { /* copy from output */ - from_source = output; - from = put - state.offset; - copy = state.length; - } - if (copy > left) { copy = left; } - left -= copy; - state.length -= copy; - do { - output[put++] = from_source[from++]; - } while (--copy); - if (state.length === 0) { state.mode = LEN; } - break; - case LIT: - if (left === 0) { break inf_leave; } - output[put++] = state.length; - left--; - state.mode = LEN; - break; - case CHECK: - if (state.wrap) { - //=== NEEDBITS(32); - while (bits < 32) { - if (have === 0) { break inf_leave; } - have--; - // Use '|' insdead of '+' to make sure that result is signed - hold |= input[next++] << bits; - bits += 8; - } - //===// - _out -= left; - strm.total_out += _out; - state.total += _out; - if (_out) { - strm.adler = state.check = - /*UPDATE(state.check, put - _out, _out);*/ - (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out)); - - } - _out = left; - // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too - if ((state.flags ? hold : zswap32(hold)) !== state.check) { - strm.msg = 'incorrect data check'; - state.mode = BAD; - break; - } - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - //Tracev((stderr, "inflate: check matches trailer\n")); - } - state.mode = LENGTH; - /* falls through */ - case LENGTH: - if (state.wrap && state.flags) { - //=== NEEDBITS(32); - while (bits < 32) { - if (have === 0) { break inf_leave; } - have--; - hold += input[next++] << bits; - bits += 8; - } - //===// - if (hold !== (state.total & 0xffffffff)) { - strm.msg = 'incorrect length check'; - state.mode = BAD; - break; - } - //=== INITBITS(); - hold = 0; - bits = 0; - //===// - //Tracev((stderr, "inflate: length matches trailer\n")); - } - state.mode = DONE; - /* falls through */ - case DONE: - ret = Z_STREAM_END; - break inf_leave; - case BAD: - ret = Z_DATA_ERROR; - break inf_leave; - case MEM: - return Z_MEM_ERROR; - case SYNC: - /* falls through */ - default: - return Z_STREAM_ERROR; } } - - // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave" - - /* - Return from inflate(), updating the total counts and the check value. - If there was no progress during the inflate() call, return a buffer - error. Call updatewindow() to create and/or update the window state. - Note: a memory error from inflate() is non-recoverable. - */ - - //--- RESTORE() --- - strm.next_out = put; - strm.avail_out = left; - strm.next_in = next; - strm.avail_in = have; - state.hold = hold; - state.bits = bits; - //--- - - if (state.wsize || (_out !== strm.avail_out && state.mode < BAD && - (state.mode < CHECK || flush !== Z_FINISH))) { - if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) { - state.mode = MEM; - return Z_MEM_ERROR; - } + shufflePivot(left, less-1, pivot1X, pivot1Y, data) + shufflePivot(right, great+1, pivot2X, pivot2Y, data) + if (less - 2 - left <= INSERT_SORT_CUTOFF) { + insertionSort(left, less - 2, data); + } else { + quickSort(left, less - 2, data); } - _in -= strm.avail_in; - _out -= strm.avail_out; - strm.total_in += _in; - strm.total_out += _out; - state.total += _out; - if (state.wrap && _out) { - strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/ - (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out)); + if (right - (great + 2) <= INSERT_SORT_CUTOFF) { + insertionSort(great + 2, right, data); + } else { + quickSort(great + 2, right, data); } - strm.data_type = state.bits + (state.last ? 64 : 0) + - (state.mode === TYPE ? 128 : 0) + - (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); - if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) { - ret = Z_BUF_ERROR; + if (great - less <= INSERT_SORT_CUTOFF) { + insertionSort(less, great, data); + } else { + quickSort(less, great, data); } - return ret; } - -function inflateEnd(strm) { - - if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) { - return Z_STREAM_ERROR; - } - - var state = strm.state; - if (state.window) { - state.window = null; - } - strm.state = null; - return Z_OK; -} - -function inflateGetHeader(strm, head) { - var state; - - /* check state */ - if (!strm || !strm.state) { return Z_STREAM_ERROR; } - state = strm.state; - if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; } - - /* save header structure */ - state.head = head; - head.done = false; - return Z_OK; -} - -function inflateSetDictionary(strm, dictionary) { - var dictLength = dictionary.length; - - var state; - var dictid; - var ret; - - /* check state */ - if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; } - state = strm.state; - - if (state.wrap !== 0 && state.mode !== DICT) { - return Z_STREAM_ERROR; - } - - /* check for correct dictionary identifier */ - if (state.mode === DICT) { - dictid = 1; /* adler32(0, null, 0)*/ - /* dictid = adler32(dictid, dictionary, dictLength); */ - dictid = adler32(dictid, dictionary, dictLength, 0); - if (dictid !== state.check) { - return Z_DATA_ERROR; - } - } - /* copy dictionary to window using updatewindow(), which will amend the - existing dictionary if appropriate */ - ret = updatewindow(strm, dictionary, dictLength, dictLength); - if (ret) { - state.mode = MEM; - return Z_MEM_ERROR; - } - state.havedict = 1; - // Tracev((stderr, "inflate: dictionary set\n")); - return Z_OK; -} - -exports.inflateReset = inflateReset; -exports.inflateReset2 = inflateReset2; -exports.inflateResetKeep = inflateResetKeep; -exports.inflateInit = inflateInit; -exports.inflateInit2 = inflateInit2; -exports.inflate = inflate; -exports.inflateEnd = inflateEnd; -exports.inflateGetHeader = inflateGetHeader; -exports.inflateSetDictionary = inflateSetDictionary; -exports.inflateInfo = 'pako inflate (from Nodeca project)'; - -/* Not implemented -exports.inflateCopy = inflateCopy; -exports.inflateGetDictionary = inflateGetDictionary; -exports.inflateMark = inflateMark; -exports.inflatePrime = inflatePrime; -exports.inflateSync = inflateSync; -exports.inflateSyncPoint = inflateSyncPoint; -exports.inflateUndermine = inflateUndermine; -*/ - -},{"../utils/common":20,"./adler32":21,"./crc32":23,"./inffast":25,"./inftrees":27}],27:[function(require,module,exports){ -'use strict'; - - -var utils = require('../utils/common'); - -var MAXBITS = 15; -var ENOUGH_LENS = 852; -var ENOUGH_DISTS = 592; -//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); - -var CODES = 0; -var LENS = 1; -var DISTS = 2; - -var lbase = [ /* Length codes 257..285 base */ - 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, - 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 -]; - -var lext = [ /* Length codes 257..285 extra */ - 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, - 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78 -]; - -var dbase = [ /* Distance codes 0..29 base */ - 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, - 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, - 8193, 12289, 16385, 24577, 0, 0 -]; - -var dext = [ /* Distance codes 0..29 extra */ - 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, - 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, - 28, 28, 29, 29, 64, 64 -]; - -module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts) -{ - var bits = opts.bits; - //here = opts.here; /* table entry for duplication */ - - var len = 0; /* a code's length in bits */ - var sym = 0; /* index of code symbols */ - var min = 0, max = 0; /* minimum and maximum code lengths */ - var root = 0; /* number of index bits for root table */ - var curr = 0; /* number of index bits for current table */ - var drop = 0; /* code bits to drop for sub-table */ - var left = 0; /* number of prefix codes available */ - var used = 0; /* code entries in table used */ - var huff = 0; /* Huffman code */ - var incr; /* for incrementing code, index */ - var fill; /* index for replicating entries */ - var low; /* low bits for current root entry */ - var mask; /* mask for low root bits */ - var next; /* next available space in table */ - var base = null; /* base value table to use */ - var base_index = 0; -// var shoextra; /* extra bits table to use */ - var end; /* use base and extra for symbol > end */ - var count = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */ - var offs = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */ - var extra = null; - var extra_index = 0; - - var here_bits, here_op, here_val; - - /* - Process a set of code lengths to create a canonical Huffman code. The - code lengths are lens[0..codes-1]. Each length corresponds to the - symbols 0..codes-1. The Huffman code is generated by first sorting the - symbols by length from short to long, and retaining the symbol order - for codes with equal lengths. Then the code starts with all zero bits - for the first code of the shortest length, and the codes are integer - increments for the same length, and zeros are appended as the length - increases. For the deflate format, these bits are stored backwards - from their more natural integer increment ordering, and so when the - decoding tables are built in the large loop below, the integer codes - are incremented backwards. - - This routine assumes, but does not check, that all of the entries in - lens[] are in the range 0..MAXBITS. The caller must assure this. - 1..MAXBITS is interpreted as that code length. zero means that that - symbol does not occur in this code. - - The codes are sorted by computing a count of codes for each length, - creating from that a table of starting indices for each length in the - sorted table, and then entering the symbols in order in the sorted - table. The sorted table is work[], with that space being provided by - the caller. - - The length counts are used for other purposes as well, i.e. finding - the minimum and maximum length codes, determining if there are any - codes at all, checking for a valid set of lengths, and looking ahead - at length counts to determine sub-table sizes when building the - decoding tables. - */ - - /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ - for (len = 0; len <= MAXBITS; len++) { - count[len] = 0; - } - for (sym = 0; sym < codes; sym++) { - count[lens[lens_index + sym]]++; - } - - /* bound code lengths, force root to be within code lengths */ - root = bits; - for (max = MAXBITS; max >= 1; max--) { - if (count[max] !== 0) { break; } - } - if (root > max) { - root = max; - } - if (max === 0) { /* no symbols to code at all */ - //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */ - //table.bits[opts.table_index] = 1; //here.bits = (var char)1; - //table.val[opts.table_index++] = 0; //here.val = (var short)0; - table[table_index++] = (1 << 24) | (64 << 16) | 0; - - - //table.op[opts.table_index] = 64; - //table.bits[opts.table_index] = 1; - //table.val[opts.table_index++] = 0; - table[table_index++] = (1 << 24) | (64 << 16) | 0; - - opts.bits = 1; - return 0; /* no symbols, but wait for decoding to report error */ - } - for (min = 1; min < max; min++) { - if (count[min] !== 0) { break; } - } - if (root < min) { - root = min; - } - - /* check for an over-subscribed or incomplete set of lengths */ - left = 1; - for (len = 1; len <= MAXBITS; len++) { - left <<= 1; - left -= count[len]; - if (left < 0) { - return -1; - } /* over-subscribed */ - } - if (left > 0 && (type === CODES || max !== 1)) { - return -1; /* incomplete set */ - } - - /* generate offsets into symbol table for each length for sorting */ - offs[1] = 0; - for (len = 1; len < MAXBITS; len++) { - offs[len + 1] = offs[len] + count[len]; - } - - /* sort symbols by length, by symbol order within each length */ - for (sym = 0; sym < codes; sym++) { - if (lens[lens_index + sym] !== 0) { - work[offs[lens[lens_index + sym]]++] = sym; - } - } - - /* - Create and fill in decoding tables. In this loop, the table being - filled is at next and has curr index bits. The code being used is huff - with length len. That code is converted to an index by dropping drop - bits off of the bottom. For codes where len is less than drop + curr, - those top drop + curr - len bits are incremented through all values to - fill the table with replicated entries. - - root is the number of index bits for the root table. When len exceeds - root, sub-tables are created pointed to by the root entry with an index - of the low root bits of huff. This is saved in low to check for when a - new sub-table should be started. drop is zero when the root table is - being filled, and drop is root when sub-tables are being filled. - - When a new sub-table is needed, it is necessary to look ahead in the - code lengths to determine what size sub-table is needed. The length - counts are used for this, and so count[] is decremented as codes are - entered in the tables. - - used keeps track of how many table entries have been allocated from the - provided *table space. It is checked for LENS and DIST tables against - the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in - the initial root table size constants. See the comments in inftrees.h - for more information. - - sym increments through all symbols, and the loop terminates when - all codes of length max, i.e. all codes, have been processed. This - routine permits incomplete codes, so another loop after this one fills - in the rest of the decoding tables with invalid code markers. - */ - - /* set up for code type */ - // poor man optimization - use if-else instead of switch, - // to avoid deopts in old v8 - if (type === CODES) { - base = extra = work; /* dummy value--not used */ - end = 19; - - } else if (type === LENS) { - base = lbase; - base_index -= 257; - extra = lext; - extra_index -= 257; - end = 256; - - } else { /* DISTS */ - base = dbase; - extra = dext; - end = -1; - } - - /* initialize opts for loop */ - huff = 0; /* starting code */ - sym = 0; /* starting code symbol */ - len = min; /* starting code length */ - next = table_index; /* current table to fill in */ - curr = root; /* current table index bits */ - drop = 0; /* current bits to drop from code for index */ - low = -1; /* trigger new sub-table when len > root */ - used = 1 << root; /* use root table entries */ - mask = used - 1; /* mask for comparing low */ - - /* check available table space */ - if ((type === LENS && used > ENOUGH_LENS) || - (type === DISTS && used > ENOUGH_DISTS)) { - return 1; - } - - var i = 0; - /* process all codes and make table entries */ - for (;;) { - i++; - /* create table entry */ - here_bits = len - drop; - if (work[sym] < end) { - here_op = 0; - here_val = work[sym]; - } - else if (work[sym] > end) { - here_op = extra[extra_index + work[sym]]; - here_val = base[base_index + work[sym]]; - } - else { - here_op = 32 + 64; /* end of block */ - here_val = 0; - } - - /* replicate for those indices with low len bits equal to huff */ - incr = 1 << (len - drop); - fill = 1 << curr; - min = fill; /* save offset to next table */ - do { - fill -= incr; - table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0; - } while (fill !== 0); - - /* backwards increment the len-bit code huff */ - incr = 1 << (len - 1); - while (huff & incr) { - incr >>= 1; - } - if (incr !== 0) { - huff &= incr - 1; - huff += incr; - } else { - huff = 0; - } - - /* go to next symbol, update count, len */ - sym++; - if (--count[len] === 0) { - if (len === max) { break; } - len = lens[lens_index + work[sym]]; - } - - /* create new sub-table if needed */ - if (len > root && (huff & mask) !== low) { - /* if first time, transition to sub-tables */ - if (drop === 0) { - drop = root; - } - - /* increment past last table */ - next += min; /* here min is 1 << curr */ - - /* determine length of next table */ - curr = len - drop; - left = 1 << curr; - while (curr + drop < max) { - left -= count[curr + drop]; - if (left <= 0) { break; } - curr++; - left <<= 1; - } - - /* check for enough space */ - used += 1 << curr; - if ((type === LENS && used > ENOUGH_LENS) || - (type === DISTS && used > ENOUGH_DISTS)) { - return 1; - } - - /* point entry in root table to sub-table */ - low = huff & mask; - /*table.op[low] = curr; - table.bits[low] = root; - table.val[low] = next - opts.table_index;*/ - table[low] = (root << 24) | (curr << 16) | (next - table_index) |0; - } - } - - /* fill in remaining table entry if code is incomplete (guaranteed to have - at most one remaining entry, since if the code is incomplete, the - maximum code length that was allowed to get this far is one bit) */ - if (huff !== 0) { - //table.op[next + huff] = 64; /* invalid code marker */ - //table.bits[next + huff] = len - drop; - //table.val[next + huff] = 0; - table[next + huff] = ((len - drop) << 24) | (64 << 16) |0; - } - - /* set return parameters */ - //opts.table_index += used; - opts.bits = root; - return 0; -}; - -},{"../utils/common":20}],28:[function(require,module,exports){ -'use strict'; +},{}],42:[function(require,module,exports){ +'use strict' module.exports = { - 2: 'need dictionary', /* Z_NEED_DICT 2 */ - 1: 'stream end', /* Z_STREAM_END 1 */ - 0: '', /* Z_OK 0 */ - '-1': 'file error', /* Z_ERRNO (-1) */ - '-2': 'stream error', /* Z_STREAM_ERROR (-2) */ - '-3': 'data error', /* Z_DATA_ERROR (-3) */ - '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */ - '-5': 'buffer error', /* Z_BUF_ERROR (-5) */ - '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */ -}; - -},{}],29:[function(require,module,exports){ -'use strict'; - - -var utils = require('../utils/common'); - -/* Public constants ==========================================================*/ -/* ===========================================================================*/ - - -//var Z_FILTERED = 1; -//var Z_HUFFMAN_ONLY = 2; -//var Z_RLE = 3; -var Z_FIXED = 4; -//var Z_DEFAULT_STRATEGY = 0; - -/* Possible values of the data_type field (though see inflate()) */ -var Z_BINARY = 0; -var Z_TEXT = 1; -//var Z_ASCII = 1; // = Z_TEXT -var Z_UNKNOWN = 2; - -/*============================================================================*/ - - -function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } - -// From zutil.h - -var STORED_BLOCK = 0; -var STATIC_TREES = 1; -var DYN_TREES = 2; -/* The three kinds of block type */ - -var MIN_MATCH = 3; -var MAX_MATCH = 258; -/* The minimum and maximum match lengths */ - -// From deflate.h -/* =========================================================================== - * Internal compression state. - */ - -var LENGTH_CODES = 29; -/* number of length codes, not counting the special END_BLOCK code */ - -var LITERALS = 256; -/* number of literal bytes 0..255 */ - -var L_CODES = LITERALS + 1 + LENGTH_CODES; -/* number of Literal or Length codes, including the END_BLOCK code */ - -var D_CODES = 30; -/* number of distance codes */ - -var BL_CODES = 19; -/* number of codes used to transfer the bit lengths */ - -var HEAP_SIZE = 2 * L_CODES + 1; -/* maximum heap size */ - -var MAX_BITS = 15; -/* All codes must not exceed MAX_BITS bits */ - -var Buf_size = 16; -/* size of bit buffer in bi_buf */ - - -/* =========================================================================== - * Constants - */ - -var MAX_BL_BITS = 7; -/* Bit length codes must not exceed MAX_BL_BITS bits */ - -var END_BLOCK = 256; -/* end of block literal code */ - -var REP_3_6 = 16; -/* repeat previous bit length 3-6 times (2 bits of repeat count) */ - -var REPZ_3_10 = 17; -/* repeat a zero length 3-10 times (3 bits of repeat count) */ - -var REPZ_11_138 = 18; -/* repeat a zero length 11-138 times (7 bits of repeat count) */ - -/* eslint-disable comma-spacing,array-bracket-spacing */ -var extra_lbits = /* extra bits for each length code */ - [0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0]; - -var extra_dbits = /* extra bits for each distance code */ - [0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13]; - -var extra_blbits = /* extra bits for each bit length code */ - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]; - -var bl_order = - [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]; -/* eslint-enable comma-spacing,array-bracket-spacing */ - -/* The lengths of the bit length codes are sent in order of decreasing - * probability, to avoid transmitting the lengths for unused bit length codes. - */ - -/* =========================================================================== - * Local data. These are initialized only once. - */ - -// We pre-fill arrays with 0 to avoid uninitialized gaps - -var DIST_CODE_LEN = 512; /* see definition of array dist_code below */ - -// !!!! Use flat array insdead of structure, Freq = i*2, Len = i*2+1 -var static_ltree = new Array((L_CODES + 2) * 2); -zero(static_ltree); -/* The static literal tree. Since the bit lengths are imposed, there is no - * need for the L_CODES extra codes used during heap construction. However - * The codes 286 and 287 are needed to build a canonical tree (see _tr_init - * below). - */ - -var static_dtree = new Array(D_CODES * 2); -zero(static_dtree); -/* The static distance tree. (Actually a trivial tree since all codes use - * 5 bits.) - */ - -var _dist_code = new Array(DIST_CODE_LEN); -zero(_dist_code); -/* Distance codes. The first 256 values correspond to the distances - * 3 .. 258, the last 256 values correspond to the top 8 bits of - * the 15 bit distances. - */ - -var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1); -zero(_length_code); -/* length code for each normalized match length (0 == MIN_MATCH) */ - -var base_length = new Array(LENGTH_CODES); -zero(base_length); -/* First normalized length for each code (0 = MIN_MATCH) */ - -var base_dist = new Array(D_CODES); -zero(base_dist); -/* First normalized distance for each code (0 = distance of 1) */ - - -function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) { - - this.static_tree = static_tree; /* static tree or NULL */ - this.extra_bits = extra_bits; /* extra bits for each code or NULL */ - this.extra_base = extra_base; /* base index for extra_bits */ - this.elems = elems; /* max number of elements in the tree */ - this.max_length = max_length; /* max bit length for the codes */ - - // show if `static_tree` has data or dummy - needed for monomorphic objects - this.has_stree = static_tree && static_tree.length; + init: sqInit, + sweepBipartite: sweepBipartite, + sweepComplete: sweepComplete, + scanBipartite: scanBipartite, + scanComplete: scanComplete } +var pool = require('typedarray-pool') +var bits = require('bit-twiddle') +var isort = require('./sort') -var static_l_desc; -var static_d_desc; -var static_bl_desc; +//Flag for blue +var BLUE_FLAG = (1<<28) +//1D sweep event queue stuff (use pool to save space) +var INIT_CAPACITY = 1024 +var RED_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) +var RED_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) +var BLUE_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) +var BLUE_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) +var COMMON_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) +var COMMON_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) +var SWEEP_EVENTS = pool.mallocDouble(INIT_CAPACITY * 8) -function TreeDesc(dyn_tree, stat_desc) { - this.dyn_tree = dyn_tree; /* the dynamic tree */ - this.max_code = 0; /* largest code with non zero frequency */ - this.stat_desc = stat_desc; /* the corresponding static tree */ -} - - - -function d_code(dist) { - return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)]; -} - - -/* =========================================================================== - * Output a short LSB first on the stream. - * IN assertion: there is enough room in pendingBuf. - */ -function put_short(s, w) { -// put_byte(s, (uch)((w) & 0xff)); -// put_byte(s, (uch)((ush)(w) >> 8)); - s.pending_buf[s.pending++] = (w) & 0xff; - s.pending_buf[s.pending++] = (w >>> 8) & 0xff; -} - - -/* =========================================================================== - * Send a value on a given number of bits. - * IN assertion: length <= 16 and value fits in length bits. - */ -function send_bits(s, value, length) { - if (s.bi_valid > (Buf_size - length)) { - s.bi_buf |= (value << s.bi_valid) & 0xffff; - put_short(s, s.bi_buf); - s.bi_buf = value >> (Buf_size - s.bi_valid); - s.bi_valid += length - Buf_size; - } else { - s.bi_buf |= (value << s.bi_valid) & 0xffff; - s.bi_valid += length; +//Reserves memory for the 1D sweep data structures +function sqInit(count) { + var rcount = bits.nextPow2(count) + if(RED_SWEEP_QUEUE.length < rcount) { + pool.free(RED_SWEEP_QUEUE) + RED_SWEEP_QUEUE = pool.mallocInt32(rcount) + } + if(RED_SWEEP_INDEX.length < rcount) { + pool.free(RED_SWEEP_INDEX) + RED_SWEEP_INDEX = pool.mallocInt32(rcount) + } + if(BLUE_SWEEP_QUEUE.length < rcount) { + pool.free(BLUE_SWEEP_QUEUE) + BLUE_SWEEP_QUEUE = pool.mallocInt32(rcount) + } + if(BLUE_SWEEP_INDEX.length < rcount) { + pool.free(BLUE_SWEEP_INDEX) + BLUE_SWEEP_INDEX = pool.mallocInt32(rcount) + } + if(COMMON_SWEEP_QUEUE.length < rcount) { + pool.free(COMMON_SWEEP_QUEUE) + COMMON_SWEEP_QUEUE = pool.mallocInt32(rcount) + } + if(COMMON_SWEEP_INDEX.length < rcount) { + pool.free(COMMON_SWEEP_INDEX) + COMMON_SWEEP_INDEX = pool.mallocInt32(rcount) + } + var eventLength = 8 * rcount + if(SWEEP_EVENTS.length < eventLength) { + pool.free(SWEEP_EVENTS) + SWEEP_EVENTS = pool.mallocDouble(eventLength) } } - -function send_code(s, c, tree) { - send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/); +//Remove an item from the active queue in O(1) +function sqPop(queue, index, count, item) { + var idx = index[item] + var top = queue[count-1] + queue[idx] = top + index[top] = idx } - -/* =========================================================================== - * Reverse the first len bits of a code, using straightforward code (a faster - * method would use a table) - * IN assertion: 1 <= len <= 15 - */ -function bi_reverse(code, len) { - var res = 0; - do { - res |= code & 1; - code >>>= 1; - res <<= 1; - } while (--len > 0); - return res >>> 1; +//Insert an item into the active queue in O(1) +function sqPush(queue, index, count, item) { + queue[count] = item + index[item] = count } +//Recursion base case: use 1D sweep algorithm +function sweepBipartite( + d, visit, + redStart, redEnd, red, redIndex, + blueStart, blueEnd, blue, blueIndex) { -/* =========================================================================== - * Flush the bit buffer, keeping at most 7 bits in it. - */ -function bi_flush(s) { - if (s.bi_valid === 16) { - put_short(s, s.bi_buf); - s.bi_buf = 0; - s.bi_valid = 0; + //store events as pairs [coordinate, idx] + // + // red create: -(idx+1) + // red destroy: idx + // blue create: -(idx+BLUE_FLAG) + // blue destroy: idx+BLUE_FLAG + // + var ptr = 0 + var elemSize = 2*d + var istart = d-1 + var iend = elemSize-1 - } else if (s.bi_valid >= 8) { - s.pending_buf[s.pending++] = s.bi_buf & 0xff; - s.bi_buf >>= 8; - s.bi_valid -= 8; - } -} - - -/* =========================================================================== - * Compute the optimal bit lengths for a tree and update the total bit length - * for the current block. - * IN assertion: the fields freq and dad are set, heap[heap_max] and - * above are the tree nodes sorted by increasing frequency. - * OUT assertions: the field len is set to the optimal bit length, the - * array bl_count contains the frequencies for each bit length. - * The length opt_len is updated; static_len is also updated if stree is - * not null. - */ -function gen_bitlen(s, desc) -// deflate_state *s; -// tree_desc *desc; /* the tree descriptor */ -{ - var tree = desc.dyn_tree; - var max_code = desc.max_code; - var stree = desc.stat_desc.static_tree; - var has_stree = desc.stat_desc.has_stree; - var extra = desc.stat_desc.extra_bits; - var base = desc.stat_desc.extra_base; - var max_length = desc.stat_desc.max_length; - var h; /* heap index */ - var n, m; /* iterate over the tree elements */ - var bits; /* bit length */ - var xbits; /* extra bits */ - var f; /* frequency */ - var overflow = 0; /* number of elements with bit length too large */ - - for (bits = 0; bits <= MAX_BITS; bits++) { - s.bl_count[bits] = 0; + for(var i=redStart; i max_length) { - bits = max_length; - overflow++; - } - tree[n * 2 + 1]/*.Len*/ = bits; - /* We overwrite tree[n].Dad which is no longer needed */ - - if (n > max_code) { continue; } /* not a leaf node */ - - s.bl_count[bits]++; - xbits = 0; - if (n >= base) { - xbits = extra[n - base]; - } - f = tree[n * 2]/*.Freq*/; - s.opt_len += f * (bits + xbits); - if (has_stree) { - s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits); - } + for(var i=blueStart; i 0); - - /* Now recompute all bit lengths, scanning in increasing frequency. - * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all - * lengths instead of fixing only the wrong ones. This idea is taken - * from 'ar' written by Haruhiko Okumura.) - */ - for (bits = max_length; bits !== 0; bits--) { - n = s.bl_count[bits]; - while (n !== 0) { - m = s.heap[--h]; - if (m > max_code) { continue; } - if (tree[m * 2 + 1]/*.Len*/ !== bits) { - // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); - s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/; - tree[m * 2 + 1]/*.Len*/ = bits; + //process events from left->right + var n = ptr >>> 1 + isort(SWEEP_EVENTS, n) + + var redActive = 0 + var blueActive = 0 + for(var i=0; i= BLUE_FLAG) { + //blue destroy event + e = (e-BLUE_FLAG)|0 + sqPop(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive--, e) + } else if(e >= 0) { + //red destroy event + sqPop(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive--, e) + } else if(e <= -BLUE_FLAG) { + //blue create event + e = (-e-BLUE_FLAG)|0 + for(var j=0; jright + var n = ptr >>> 1 + isort(SWEEP_EVENTS, n) + + var redActive = 0 + var blueActive = 0 + var commonActive = 0 + for(var i=0; i>1) === (SWEEP_EVENTS[2*i+3]>>1)) { + color = 2 + i += 1 + } + + if(e < 0) { + //Create event + var id = -(e>>1) - 1 -/* =========================================================================== - * Initialize the various 'constant' tables. - */ -function tr_static_init() { - var n; /* iterates over tree elements */ - var bits; /* bit counter */ - var length; /* length value */ - var code; /* code value */ - var dist; /* distance index */ - var bl_count = new Array(MAX_BITS + 1); - /* number of codes at each bit length for an optimal tree */ + //Intersect with common + for(var j=0; j length code (0..28) */ - length = 0; - for (code = 0; code < LENGTH_CODES - 1; code++) { - base_length[code] = length; - for (n = 0; n < (1 << extra_lbits[code]); n++) { - _length_code[length++] = code; + if(color === 0) { + //Red + sqPush(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive++, id) + } else if(color === 1) { + //Blue + sqPush(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive++, id) + } else if(color === 2) { + //Both + sqPush(COMMON_SWEEP_QUEUE, COMMON_SWEEP_INDEX, commonActive++, id) + } + } else { + //Destroy event + var id = (e>>1) - 1 + if(color === 0) { + //Red + sqPop(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive--, id) + } else if(color === 1) { + //Blue + sqPop(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive--, id) + } else if(color === 2) { + //Both + sqPop(COMMON_SWEEP_QUEUE, COMMON_SWEEP_INDEX, commonActive--, id) + } } } - //Assert (length == 256, "tr_static_init: length != 256"); - /* Note that the length 255 (match length 258) can be represented - * in two different ways: code 284 + 5 bits or code 285, so we - * overwrite length_code[255] to use the best encoding: - */ - _length_code[length - 1] = code; - - /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ - dist = 0; - for (code = 0; code < 16; code++) { - base_dist[code] = dist; - for (n = 0; n < (1 << extra_dbits[code]); n++) { - _dist_code[dist++] = code; - } - } - //Assert (dist == 256, "tr_static_init: dist != 256"); - dist >>= 7; /* from now on, all distances are divided by 128 */ - for (; code < D_CODES; code++) { - base_dist[code] = dist << 7; - for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { - _dist_code[256 + dist++] = code; - } - } - //Assert (dist == 256, "tr_static_init: 256+dist != 512"); - - /* Construct the codes of the static literal tree */ - for (bits = 0; bits <= MAX_BITS; bits++) { - bl_count[bits] = 0; - } - - n = 0; - while (n <= 143) { - static_ltree[n * 2 + 1]/*.Len*/ = 8; - n++; - bl_count[8]++; - } - while (n <= 255) { - static_ltree[n * 2 + 1]/*.Len*/ = 9; - n++; - bl_count[9]++; - } - while (n <= 279) { - static_ltree[n * 2 + 1]/*.Len*/ = 7; - n++; - bl_count[7]++; - } - while (n <= 287) { - static_ltree[n * 2 + 1]/*.Len*/ = 8; - n++; - bl_count[8]++; - } - /* Codes 286 and 287 do not exist, but we must include them in the - * tree construction to get a canonical Huffman tree (longest code - * all ones) - */ - gen_codes(static_ltree, L_CODES + 1, bl_count); - - /* The static distance tree is trivial: */ - for (n = 0; n < D_CODES; n++) { - static_dtree[n * 2 + 1]/*.Len*/ = 5; - static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5); - } - - // Now data ready and we can init static trees - static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS); - static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS); - static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS); - - //static_init_done = true; } +//Sweep and prune/scanline algorithm: +// Scan along axis, detect intersections +// Brute force all boxes along axis +function scanBipartite( + d, axis, visit, flip, + redStart, redEnd, red, redIndex, + blueStart, blueEnd, blue, blueIndex) { + + var ptr = 0 + var elemSize = 2*d + var istart = axis + var iend = axis+d -/* =========================================================================== - * Initialize a new block. - */ -function init_block(s) { - var n; /* iterates over tree elements */ - - /* Initialize the trees. */ - for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; } - for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; } - for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; } - - s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1; - s.opt_len = s.static_len = 0; - s.last_lit = s.matches = 0; -} - - -/* =========================================================================== - * Flush the bit buffer and align the output on a byte boundary - */ -function bi_windup(s) -{ - if (s.bi_valid > 8) { - put_short(s, s.bi_buf); - } else if (s.bi_valid > 0) { - //put_byte(s, (Byte)s->bi_buf); - s.pending_buf[s.pending++] = s.bi_buf; + var redShift = 1 + var blueShift = 1 + if(flip) { + blueShift = BLUE_FLAG + } else { + redShift = BLUE_FLAG } - s.bi_buf = 0; - s.bi_valid = 0; -} -/* =========================================================================== - * Copy a stored block, storing first the length and its - * one's complement if requested. - */ -function copy_block(s, buf, len, header) -//DeflateState *s; -//charf *buf; /* the input data */ -//unsigned len; /* its length */ -//int header; /* true if block header must be written */ -{ - bi_windup(s); /* align on byte boundary */ - - if (header) { - put_short(s, len); - put_short(s, ~len); + for(var i=redStart; iright + var n = ptr >>> 1 + isort(SWEEP_EVENTS, n) + + var redActive = 0 + for(var i=0; i= BLUE_FLAG) { + isRed = !flip + idx -= BLUE_FLAG } else { - /* Here, lc is the match length - MIN_MATCH */ - code = _length_code[lc]; - send_code(s, code + LITERALS + 1, ltree); /* send the length code */ - extra = extra_lbits[code]; - if (extra !== 0) { - lc -= base_length[code]; - send_bits(s, lc, extra); /* send the extra length bits */ - } - dist--; /* dist is now the match distance - 1 */ - code = d_code(dist); - //Assert (code < D_CODES, "bad d_code"); - - send_code(s, code, dtree); /* send the distance code */ - extra = extra_dbits[code]; - if (extra !== 0) { - dist -= base_dist[code]; - send_bits(s, dist, extra); /* send the extra distance bits */ - } - } /* literal or match pair ? */ - - /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ - //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, - // "pendingBuf overflow"); - - } while (lx < s.last_lit); - } - - send_code(s, END_BLOCK, ltree); -} - - -/* =========================================================================== - * Construct one Huffman tree and assigns the code bit strings and lengths. - * Update the total bit length for the current block. - * IN assertion: the field freq is set for all tree elements. - * OUT assertions: the fields len and code are set to the optimal bit length - * and corresponding code. The length opt_len is updated; static_len is - * also updated if stree is not null. The field max_code is set. - */ -function build_tree(s, desc) -// deflate_state *s; -// tree_desc *desc; /* the tree descriptor */ -{ - var tree = desc.dyn_tree; - var stree = desc.stat_desc.static_tree; - var has_stree = desc.stat_desc.has_stree; - var elems = desc.stat_desc.elems; - var n, m; /* iterate over heap elements */ - var max_code = -1; /* largest code with non zero frequency */ - var node; /* new node being created */ - - /* Construct the initial heap, with least frequent element in - * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. - * heap[0] is not used. - */ - s.heap_len = 0; - s.heap_max = HEAP_SIZE; - - for (n = 0; n < elems; n++) { - if (tree[n * 2]/*.Freq*/ !== 0) { - s.heap[++s.heap_len] = max_code = n; - s.depth[n] = 0; - - } else { - tree[n * 2 + 1]/*.Len*/ = 0; - } - } - - /* The pkzip format requires that at least one distance code exists, - * and that at least one bit should be sent even if there is only one - * possible code. So to avoid special checks later on we force at least - * two codes of non zero frequency. - */ - while (s.heap_len < 2) { - node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0); - tree[node * 2]/*.Freq*/ = 1; - s.depth[node] = 0; - s.opt_len--; - - if (has_stree) { - s.static_len -= stree[node * 2 + 1]/*.Len*/; - } - /* node is 0 or 1 so it does not have extra bits */ - } - desc.max_code = max_code; - - /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, - * establish sub-heaps of increasing lengths: - */ - for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); } - - /* Construct the Huffman tree by repeatedly combining the least two - * frequent nodes. - */ - node = elems; /* next internal node of the tree */ - do { - //pqremove(s, tree, n); /* n = node of least frequency */ - /*** pqremove ***/ - n = s.heap[1/*SMALLEST*/]; - s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--]; - pqdownheap(s, tree, 1/*SMALLEST*/); - /***/ - - m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */ - - s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */ - s.heap[--s.heap_max] = m; - - /* Create a new node father of n and m */ - tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/; - s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1; - tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node; - - /* and insert the new node in the heap */ - s.heap[1/*SMALLEST*/] = node++; - pqdownheap(s, tree, 1/*SMALLEST*/); - - } while (s.heap_len >= 2); - - s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/]; - - /* At this point, the fields freq and dad are set. We can now - * generate the bit lengths. - */ - gen_bitlen(s, desc); - - /* The field len is now set, we can generate the bit codes */ - gen_codes(tree, max_code, s.bl_count); -} - - -/* =========================================================================== - * Scan a literal or distance tree to determine the frequencies of the codes - * in the bit length tree. - */ -function scan_tree(s, tree, max_code) -// deflate_state *s; -// ct_data *tree; /* the tree to be scanned */ -// int max_code; /* and its largest code of non zero frequency */ -{ - var n; /* iterates over all tree elements */ - var prevlen = -1; /* last emitted length */ - var curlen; /* length of current code */ - - var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */ - - var count = 0; /* repeat count of the current code */ - var max_count = 7; /* max repeat count */ - var min_count = 4; /* min repeat count */ - - if (nextlen === 0) { - max_count = 138; - min_count = 3; - } - tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */ - - for (n = 0; n <= max_code; n++) { - curlen = nextlen; - nextlen = tree[(n + 1) * 2 + 1]/*.Len*/; - - if (++count < max_count && curlen === nextlen) { - continue; - - } else if (count < min_count) { - s.bl_tree[curlen * 2]/*.Freq*/ += count; - - } else if (curlen !== 0) { - - if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; } - s.bl_tree[REP_3_6 * 2]/*.Freq*/++; - - } else if (count <= 10) { - s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++; - - } else { - s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++; - } - - count = 0; - prevlen = curlen; - - if (nextlen === 0) { - max_count = 138; - min_count = 3; - - } else if (curlen === nextlen) { - max_count = 6; - min_count = 3; - - } else { - max_count = 7; - min_count = 4; - } - } -} - - -/* =========================================================================== - * Send a literal or distance tree in compressed form, using the codes in - * bl_tree. - */ -function send_tree(s, tree, max_code) -// deflate_state *s; -// ct_data *tree; /* the tree to be scanned */ -// int max_code; /* and its largest code of non zero frequency */ -{ - var n; /* iterates over all tree elements */ - var prevlen = -1; /* last emitted length */ - var curlen; /* length of current code */ - - var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */ - - var count = 0; /* repeat count of the current code */ - var max_count = 7; /* max repeat count */ - var min_count = 4; /* min repeat count */ - - /* tree[max_code+1].Len = -1; */ /* guard already set */ - if (nextlen === 0) { - max_count = 138; - min_count = 3; - } - - for (n = 0; n <= max_code; n++) { - curlen = nextlen; - nextlen = tree[(n + 1) * 2 + 1]/*.Len*/; - - if (++count < max_count && curlen === nextlen) { - continue; - - } else if (count < min_count) { - do { send_code(s, curlen, s.bl_tree); } while (--count !== 0); - - } else if (curlen !== 0) { - if (curlen !== prevlen) { - send_code(s, curlen, s.bl_tree); - count--; + isRed = !!flip + idx -= 1 } - //Assert(count >= 3 && count <= 6, " 3_6?"); - send_code(s, REP_3_6, s.bl_tree); - send_bits(s, count - 3, 2); + if(isRed) { + sqPush(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive++, idx) + } else { + var blueId = blueIndex[idx] + var bluePtr = elemSize * idx + + var b0 = blue[bluePtr+axis+1] + var b1 = blue[bluePtr+axis+1+d] - } else if (count <= 10) { - send_code(s, REPZ_3_10, s.bl_tree); - send_bits(s, count - 3, 3); +red_loop: + for(var j=0; jright + var n = ptr >>> 1 + isort(SWEEP_EVENTS, n) + + var redActive = 0 + for(var i=0; i= BLUE_FLAG) { + RED_SWEEP_QUEUE[redActive++] = idx - BLUE_FLAG + } else { + idx -= 1 + var blueId = blueIndex[idx] + var bluePtr = elemSize * idx + + var b0 = blue[bluePtr+axis+1] + var b1 = blue[bluePtr+axis+1+d] + +red_loop: + for(var j=0; j=0; --j) { + if(RED_SWEEP_QUEUE[j] === idx) { + for(var k=j+1; k= 3; max_blindex--) { - if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) { - break; - } - } - /* Update opt_len to include the bit length tree and counts */ - s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; - //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", - // s->opt_len, s->static_len)); - - return max_blindex; -} - - -/* =========================================================================== - * Send the header for a block using dynamic Huffman trees: the counts, the - * lengths of the bit length codes, the literal tree and the distance tree. - * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. - */ -function send_all_trees(s, lcodes, dcodes, blcodes) -// deflate_state *s; -// int lcodes, dcodes, blcodes; /* number of codes for each tree */ -{ - var rank; /* index in bl_order */ - - //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); - //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, - // "too many codes"); - //Tracev((stderr, "\nbl counts: ")); - send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ - send_bits(s, dcodes - 1, 5); - send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */ - for (rank = 0; rank < blcodes; rank++) { - //Tracev((stderr, "\nbl code %2d ", bl_order[rank])); - send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3); - } - //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); - - send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */ - //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); - - send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */ - //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); -} - - -/* =========================================================================== - * Check if the data type is TEXT or BINARY, using the following algorithm: - * - TEXT if the two conditions below are satisfied: - * a) There are no non-portable control characters belonging to the - * "black list" (0..6, 14..25, 28..31). - * b) There is at least one printable character belonging to the - * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). - * - BINARY otherwise. - * - The following partially-portable control characters form a - * "gray list" that is ignored in this detection algorithm: - * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). - * IN assertion: the fields Freq of dyn_ltree are set. - */ -function detect_data_type(s) { - /* black_mask is the bit mask of black-listed bytes - * set bits 0..6, 14..25, and 28..31 - * 0xf3ffc07f = binary 11110011111111111100000001111111 - */ - var black_mask = 0xf3ffc07f; - var n; - - /* Check for non-textual ("black-listed") bytes. */ - for (n = 0; n <= 31; n++, black_mask >>>= 1) { - if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) { - return Z_BINARY; - } - } - - /* Check for textual ("white-listed") bytes. */ - if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 || - s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) { - return Z_TEXT; - } - for (n = 32; n < LITERALS; n++) { - if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) { - return Z_TEXT; - } - } - - /* There are no "black-listed" or "white-listed" bytes: - * this stream either is empty or has tolerated ("gray-listed") bytes only. - */ - return Z_BINARY; -} - - -var static_init_done = false; - -/* =========================================================================== - * Initialize the tree data structures for a new zlib stream. - */ -function _tr_init(s) -{ - - if (!static_init_done) { - tr_static_init(); - static_init_done = true; - } - - s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc); - s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc); - s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc); - - s.bi_buf = 0; - s.bi_valid = 0; - - /* Initialize the first block of the first file: */ - init_block(s); -} - - -/* =========================================================================== - * Send a stored block - */ -function _tr_stored_block(s, buf, stored_len, last) -//DeflateState *s; -//charf *buf; /* input block */ -//ulg stored_len; /* length of input block */ -//int last; /* one if this is the last block for a file */ -{ - send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */ - copy_block(s, buf, stored_len, true); /* with header */ -} - - -/* =========================================================================== - * Send one empty static block to give enough lookahead for inflate. - * This takes 10 bits, of which 7 may remain in the bit buffer. - */ -function _tr_align(s) { - send_bits(s, STATIC_TREES << 1, 3); - send_code(s, END_BLOCK, static_ltree); - bi_flush(s); -} - - -/* =========================================================================== - * Determine the best encoding for the current block: dynamic trees, static - * trees or store, and output the encoded block to the zip file. - */ -function _tr_flush_block(s, buf, stored_len, last) -//DeflateState *s; -//charf *buf; /* input block, or NULL if too old */ -//ulg stored_len; /* length of input block */ -//int last; /* one if this is the last block for a file */ -{ - var opt_lenb, static_lenb; /* opt_len and static_len in bytes */ - var max_blindex = 0; /* index of last bit length code of non zero freq */ - - /* Build the Huffman trees unless a stored block is forced */ - if (s.level > 0) { - - /* Check if the file is binary or text */ - if (s.strm.data_type === Z_UNKNOWN) { - s.strm.data_type = detect_data_type(s); - } - - /* Construct the literal and distance trees */ - build_tree(s, s.l_desc); - // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, - // s->static_len)); - - build_tree(s, s.d_desc); - // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, - // s->static_len)); - /* At this point, opt_len and static_len are the total bit lengths of - * the compressed block data, excluding the tree representations. - */ - - /* Build the bit length tree for the above two trees, and get the index - * in bl_order of the last bit length code to send. - */ - max_blindex = build_bl_tree(s); - - /* Determine the best encoding. Compute the block lengths in bytes. */ - opt_lenb = (s.opt_len + 3 + 7) >>> 3; - static_lenb = (s.static_len + 3 + 7) >>> 3; - - // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", - // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, - // s->last_lit)); - - if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; } - - } else { - // Assert(buf != (char*)0, "lost buf"); - opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ - } - - if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) { - /* 4: two words for the lengths */ - - /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. - * Otherwise we can't have processed more than WSIZE input bytes since - * the last block flush, because compression would have been - * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to - * transform a block into a stored block. - */ - _tr_stored_block(s, buf, stored_len, last); - - } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) { - - send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3); - compress_block(s, static_ltree, static_dtree); - - } else { - send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3); - send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1); - compress_block(s, s.dyn_ltree, s.dyn_dtree); - } - // Assert (s->compressed_len == s->bits_sent, "bad compressed size"); - /* The above check is made mod 2^32, for files larger than 512 MB - * and uLong implemented on 32 bits. - */ - init_block(s); - - if (last) { - bi_windup(s); - } - // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, - // s->compressed_len-7*last)); -} - -/* =========================================================================== - * Save the match info and tally the frequency counts. Return true if - * the current block must be flushed. - */ -function _tr_tally(s, dist, lc) -// deflate_state *s; -// unsigned dist; /* distance of matched string */ -// unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ -{ - //var out_length, in_length, dcode; - - s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff; - s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff; - - s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff; - s.last_lit++; - - if (dist === 0) { - /* lc is the unmatched char */ - s.dyn_ltree[lc * 2]/*.Freq*/++; - } else { - s.matches++; - /* Here, lc is the match length - MIN_MATCH */ - dist--; /* dist = match distance - 1 */ - //Assert((ush)dist < (ush)MAX_DIST(s) && - // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && - // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); - - s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++; - s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++; - } - -// (!) This block is disabled in zlib defailts, -// don't enable it for binary compatibility - -//#ifdef TRUNCATE_BLOCK -// /* Try to guess if it is profitable to stop the current block here */ -// if ((s.last_lit & 0x1fff) === 0 && s.level > 2) { -// /* Compute an upper bound for the compressed length */ -// out_length = s.last_lit*8; -// in_length = s.strstart - s.block_start; -// -// for (dcode = 0; dcode < D_CODES; dcode++) { -// out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]); -// } -// out_length >>>= 3; -// //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", -// // s->last_lit, in_length, out_length, -// // 100L - out_length*100L/in_length)); -// if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) { -// return true; -// } -// } -//#endif - - return (s.last_lit === s.lit_bufsize - 1); - /* We avoid equality with lit_bufsize because of wraparound at 64K - * on 16 bit machines and because stored blocks are restricted to - * 64K-1 bytes. - */ -} - -exports._tr_init = _tr_init; -exports._tr_stored_block = _tr_stored_block; -exports._tr_flush_block = _tr_flush_block; -exports._tr_tally = _tr_tally; -exports._tr_align = _tr_align; - -},{"../utils/common":20}],30:[function(require,module,exports){ -'use strict'; - - -function ZStream() { - /* next input byte */ - this.input = null; // JS specific, because we have no pointers - this.next_in = 0; - /* number of bytes available at input */ - this.avail_in = 0; - /* total number of input bytes read so far */ - this.total_in = 0; - /* next output byte should be put there */ - this.output = null; // JS specific, because we have no pointers - this.next_out = 0; - /* remaining free space at output */ - this.avail_out = 0; - /* total number of bytes output so far */ - this.total_out = 0; - /* last error message, NULL if no error */ - this.msg = ''/*Z_NULL*/; - /* not visible by applications */ - this.state = null; - /* best guess about the data type: binary or text */ - this.data_type = 2/*Z_UNKNOWN*/; - /* adler32 value of the uncompressed data */ - this.adler = 0; -} - -module.exports = ZStream; - -},{}],31:[function(require,module,exports){ +},{}],44:[function(require,module,exports){ (function (process,Buffer){ var msg = require('pako/lib/zlib/messages'); var zstream = require('pako/lib/zlib/zstream'); @@ -8784,7 +7267,7 @@ Zlib.prototype._error = function(status) { exports.Zlib = Zlib; }).call(this,require('_process'),require("buffer").Buffer) -},{"_process":41,"buffer":33,"pako/lib/zlib/constants":22,"pako/lib/zlib/deflate.js":24,"pako/lib/zlib/inflate.js":26,"pako/lib/zlib/messages":28,"pako/lib/zlib/zstream":30}],32:[function(require,module,exports){ +},{"_process":923,"buffer":46,"pako/lib/zlib/constants":453,"pako/lib/zlib/deflate.js":455,"pako/lib/zlib/inflate.js":457,"pako/lib/zlib/messages":459,"pako/lib/zlib/zstream":461}],45:[function(require,module,exports){ (function (process,Buffer){ // Copyright Joyent, Inc. and other Node contributors. // @@ -9398,7 +7881,7 @@ util.inherits(InflateRaw, Zlib); util.inherits(Unzip, Zlib); }).call(this,require('_process'),require("buffer").Buffer) -},{"./binding":31,"_process":41,"_stream_transform":60,"assert":18,"buffer":33,"util":68}],33:[function(require,module,exports){ +},{"./binding":44,"_process":923,"_stream_transform":943,"assert":9,"buffer":46,"util":1001}],46:[function(require,module,exports){ (function (global){ /*! * The buffer module from node.js, for the browser. @@ -11191,540 +9674,1991 @@ function isnan (val) { } }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{"base64-js":34,"ieee754":35,"isarray":36}],34:[function(require,module,exports){ +},{"base64-js":12,"ieee754":260,"isarray":47}],47:[function(require,module,exports){ +var toString = {}.toString; + +module.exports = Array.isArray || function (arr) { + return toString.call(arr) == '[object Array]'; +}; + +},{}],48:[function(require,module,exports){ +(function (global){ +'use strict'; + +var buffer = require('buffer'); +var Buffer = buffer.Buffer; +var SlowBuffer = buffer.SlowBuffer; +var MAX_LEN = buffer.kMaxLength || 2147483647; +exports.alloc = function alloc(size, fill, encoding) { + if (typeof Buffer.alloc === 'function') { + return Buffer.alloc(size, fill, encoding); + } + if (typeof encoding === 'number') { + throw new TypeError('encoding must not be number'); + } + if (typeof size !== 'number') { + throw new TypeError('size must be a number'); + } + if (size > MAX_LEN) { + throw new RangeError('size is too large'); + } + var enc = encoding; + var _fill = fill; + if (_fill === undefined) { + enc = undefined; + _fill = 0; + } + var buf = new Buffer(size); + if (typeof _fill === 'string') { + var fillBuf = new Buffer(_fill, enc); + var flen = fillBuf.length; + var i = -1; + while (++i < size) { + buf[i] = fillBuf[i % flen]; + } + } else { + buf.fill(_fill); + } + return buf; +} +exports.allocUnsafe = function allocUnsafe(size) { + if (typeof Buffer.allocUnsafe === 'function') { + return Buffer.allocUnsafe(size); + } + if (typeof size !== 'number') { + throw new TypeError('size must be a number'); + } + if (size > MAX_LEN) { + throw new RangeError('size is too large'); + } + return new Buffer(size); +} +exports.from = function from(value, encodingOrOffset, length) { + if (typeof Buffer.from === 'function' && (!global.Uint8Array || Uint8Array.from !== Buffer.from)) { + return Buffer.from(value, encodingOrOffset, length); + } + if (typeof value === 'number') { + throw new TypeError('"value" argument must not be a number'); + } + if (typeof value === 'string') { + return new Buffer(value, encodingOrOffset); + } + if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) { + var offset = encodingOrOffset; + if (arguments.length === 1) { + return new Buffer(value); + } + if (typeof offset === 'undefined') { + offset = 0; + } + var len = length; + if (typeof len === 'undefined') { + len = value.byteLength - offset; + } + if (offset >= value.byteLength) { + throw new RangeError('\'offset\' is out of bounds'); + } + if (len > value.byteLength - offset) { + throw new RangeError('\'length\' is out of bounds'); + } + return new Buffer(value.slice(offset, offset + len)); + } + if (Buffer.isBuffer(value)) { + var out = new Buffer(value.length); + value.copy(out, 0, 0, value.length); + return out; + } + if (value) { + if (Array.isArray(value) || (typeof ArrayBuffer !== 'undefined' && value.buffer instanceof ArrayBuffer) || 'length' in value) { + return new Buffer(value); + } + if (value.type === 'Buffer' && Array.isArray(value.data)) { + return new Buffer(value.data); + } + } + + throw new TypeError('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.'); +} +exports.allocUnsafeSlow = function allocUnsafeSlow(size) { + if (typeof Buffer.allocUnsafeSlow === 'function') { + return Buffer.allocUnsafeSlow(size); + } + if (typeof size !== 'number') { + throw new TypeError('size must be a number'); + } + if (size >= MAX_LEN) { + throw new RangeError('size is too large'); + } + return new SlowBuffer(size); +} + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"buffer":46}],49:[function(require,module,exports){ 'use strict' -exports.byteLength = byteLength -exports.toByteArray = toByteArray -exports.fromByteArray = fromByteArray +var monotoneTriangulate = require('./lib/monotone') +var makeIndex = require('./lib/triangulation') +var delaunayFlip = require('./lib/delaunay') +var filterTriangulation = require('./lib/filter') -var lookup = [] -var revLookup = [] -var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array +module.exports = cdt2d -var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -for (var i = 0, len = code.length; i < len; ++i) { - lookup[i] = code[i] - revLookup[code.charCodeAt(i)] = i +function canonicalizeEdge(e) { + return [Math.min(e[0], e[1]), Math.max(e[0], e[1])] } -revLookup['-'.charCodeAt(0)] = 62 -revLookup['_'.charCodeAt(0)] = 63 +function compareEdge(a, b) { + return a[0]-b[0] || a[1]-b[1] +} -function placeHoldersCount (b64) { - var len = b64.length - if (len % 4 > 0) { - throw new Error('Invalid string. Length must be a multiple of 4') +function canonicalizeEdges(edges) { + return edges.map(canonicalizeEdge).sort(compareEdge) +} + +function getDefault(options, property, dflt) { + if(property in options) { + return options[property] } - - // the number of equal signs (place holders) - // if there are two placeholders, than the two characters before it - // represent one byte - // if there is only one, then the three characters before it represent 2 bytes - // this is just a cheap hack to not do indexOf twice - return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 + return dflt } -function byteLength (b64) { - // base64 is 4/3 + up to two characters of the original data - return b64.length * 3 / 4 - placeHoldersCount(b64) -} +function cdt2d(points, edges, options) { -function toByteArray (b64) { - var i, j, l, tmp, placeHolders, arr - var len = b64.length - placeHolders = placeHoldersCount(b64) - - arr = new Arr(len * 3 / 4 - placeHolders) - - // if there are placeholders, only get up to the last complete 4 chars - l = placeHolders > 0 ? len - 4 : len - - var L = 0 - - for (i = 0, j = 0; i < l; i += 4, j += 3) { - tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] - arr[L++] = (tmp >> 16) & 0xFF - arr[L++] = (tmp >> 8) & 0xFF - arr[L++] = tmp & 0xFF - } - - if (placeHolders === 2) { - tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) - arr[L++] = tmp & 0xFF - } else if (placeHolders === 1) { - tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) - arr[L++] = (tmp >> 8) & 0xFF - arr[L++] = tmp & 0xFF - } - - return arr -} - -function tripletToBase64 (num) { - return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] -} - -function encodeChunk (uint8, start, end) { - var tmp - var output = [] - for (var i = start; i < end; i += 3) { - tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) - output.push(tripletToBase64(tmp)) - } - return output.join('') -} - -function fromByteArray (uint8) { - var tmp - var len = uint8.length - var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes - var output = '' - var parts = [] - var maxChunkLength = 16383 // must be multiple of 3 - - // go through the array every three bytes, we'll deal with trailing stuff later - for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { - parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) - } - - // pad the end with zeros, but make sure to not forget the extra bytes - if (extraBytes === 1) { - tmp = uint8[len - 1] - output += lookup[tmp >> 2] - output += lookup[(tmp << 4) & 0x3F] - output += '==' - } else if (extraBytes === 2) { - tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) - output += lookup[tmp >> 10] - output += lookup[(tmp >> 4) & 0x3F] - output += lookup[(tmp << 2) & 0x3F] - output += '=' - } - - parts.push(output) - - return parts.join('') -} - -},{}],35:[function(require,module,exports){ -exports.read = function (buffer, offset, isLE, mLen, nBytes) { - var e, m - var eLen = nBytes * 8 - mLen - 1 - var eMax = (1 << eLen) - 1 - var eBias = eMax >> 1 - var nBits = -7 - var i = isLE ? (nBytes - 1) : 0 - var d = isLE ? -1 : 1 - var s = buffer[offset + i] - - i += d - - e = s & ((1 << (-nBits)) - 1) - s >>= (-nBits) - nBits += eLen - for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} - - m = e & ((1 << (-nBits)) - 1) - e >>= (-nBits) - nBits += mLen - for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} - - if (e === 0) { - e = 1 - eBias - } else if (e === eMax) { - return m ? NaN : ((s ? -1 : 1) * Infinity) + if(!Array.isArray(edges)) { + options = edges || {} + edges = [] } else { - m = m + Math.pow(2, mLen) - e = e - eBias + options = options || {} + edges = edges || [] } - return (s ? -1 : 1) * m * Math.pow(2, e - mLen) -} -exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { - var e, m, c - var eLen = nBytes * 8 - mLen - 1 - var eMax = (1 << eLen) - 1 - var eBias = eMax >> 1 - var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) - var i = isLE ? 0 : (nBytes - 1) - var d = isLE ? 1 : -1 - var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 + //Parse out options + var delaunay = !!getDefault(options, 'delaunay', true) + var interior = !!getDefault(options, 'interior', true) + var exterior = !!getDefault(options, 'exterior', true) + var infinity = !!getDefault(options, 'infinity', false) - value = Math.abs(value) + //Handle trivial case + if((!interior && !exterior) || points.length === 0) { + return [] + } - if (isNaN(value) || value === Infinity) { - m = isNaN(value) ? 1 : 0 - e = eMax + //Construct initial triangulation + var cells = monotoneTriangulate(points, edges) + + //If delaunay refinement needed, then improve quality by edge flipping + if(delaunay || interior !== exterior || infinity) { + + //Index all of the cells to support fast neighborhood queries + var triangulation = makeIndex(points.length, canonicalizeEdges(edges)) + for(var i=0; i= 1) { - value += rt / c - } else { - value += rt * Math.pow(2, 1 - eBias) - } - if (value * c >= 2) { - e++ - c /= 2 - } + return cells + } +} - if (e + eBias >= eMax) { - m = 0 - e = eMax - } else if (e + eBias >= 1) { - m = (value * c - 1) * Math.pow(2, mLen) - e = e + eBias - } else { - m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) - e = 0 +},{"./lib/delaunay":50,"./lib/filter":51,"./lib/monotone":52,"./lib/triangulation":53}],50:[function(require,module,exports){ +'use strict' + +var inCircle = require('robust-in-sphere')[4] +var bsearch = require('binary-search-bounds') + +module.exports = delaunayRefine + +function testFlip(points, triangulation, stack, a, b, x) { + var y = triangulation.opposite(a, b) + + //Test boundary edge + if(y < 0) { + return + } + + //Swap edge if order flipped + if(b < a) { + var tmp = a + a = b + b = tmp + tmp = x + x = y + y = tmp + } + + //Test if edge is constrained + if(triangulation.isConstraint(a, b)) { + return + } + + //Test if edge is delaunay + if(inCircle(points[a], points[b], points[x], points[y]) < 0) { + stack.push(a, b) + } +} + +//Assume edges are sorted lexicographically +function delaunayRefine(points, triangulation) { + var stack = [] + + var numPoints = points.length + var stars = triangulation.stars + for(var a=0; a= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} + while(stack.length > 0) { + var b = stack.pop() + var a = stack.pop() - e = (e << mLen) | m - eLen += mLen - for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} + //Find opposite pairs + var x = -1, y = -1 + var star = stars[a] + for(var i=1; i= 0) { + continue + } + + //Flip the edge + triangulation.flip(a, b) + + //Test flipping neighboring edges + testFlip(points, triangulation, stack, x, a, y) + testFlip(points, triangulation, stack, a, y, x) + testFlip(points, triangulation, stack, y, b, x) + testFlip(points, triangulation, stack, b, x, y) + } } -},{}],36:[function(require,module,exports){ -arguments[4][13][0].apply(exports,arguments) -},{"dup":13}],37:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. +},{"binary-search-bounds":54,"robust-in-sphere":952}],51:[function(require,module,exports){ +'use strict' -function EventEmitter() { - this._events = this._events || {}; - this._maxListeners = this._maxListeners || undefined; +var bsearch = require('binary-search-bounds') + +module.exports = classifyFaces + +function FaceIndex(cells, neighbor, constraint, flags, active, next, boundary) { + this.cells = cells + this.neighbor = neighbor + this.flags = flags + this.constraint = constraint + this.active = active + this.next = next + this.boundary = boundary } -module.exports = EventEmitter; -// Backwards-compat with node 0.10.x -EventEmitter.EventEmitter = EventEmitter; +var proto = FaceIndex.prototype -EventEmitter.prototype._events = undefined; -EventEmitter.prototype._maxListeners = undefined; +function compareCell(a, b) { + return a[0] - b[0] || + a[1] - b[1] || + a[2] - b[2] +} -// By default EventEmitters will print a warning if more than 10 listeners are -// added to it. This is a useful default which helps finding memory leaks. -EventEmitter.defaultMaxListeners = 10; +proto.locate = (function() { + var key = [0,0,0] + return function(a, b, c) { + var x = a, y = b, z = c + if(b < c) { + if(b < a) { + x = b + y = c + z = a + } + } else if(c < a) { + x = c + y = a + z = b + } + if(x < 0) { + return -1 + } + key[0] = x + key[1] = y + key[2] = z + return bsearch.eq(this.cells, key, compareCell) + } +})() -// Obviously not all Emitters should be limited to 10. This function allows -// that to be increased. Set to zero for unlimited. -EventEmitter.prototype.setMaxListeners = function(n) { - if (!isNumber(n) || n < 0 || isNaN(n)) - throw TypeError('n must be a positive number'); - this._maxListeners = n; - return this; -}; +function indexCells(triangulation, infinity) { + //First get cells and canonicalize + var cells = triangulation.cells() + var nc = cells.length + for(var i=0; i 0 || next.length > 0) { + while(active.length > 0) { + var t = active.pop() + if(flags[t] === -side) { + continue + } + flags[t] = side + var c = cells[t] + for(var j=0; j<3; ++j) { + var f = neighbor[3*t+j] + if(f >= 0 && flags[f] === 0) { + if(constraint[3*t+j]) { + next.push(f) + } else { + active.push(f) + flags[f] = side + } + } + } + } + + //Swap arrays and loop + var tmp = next + next = active + active = tmp + next.length = 0 + side = -side + } + + var result = filterCells(cells, flags, target) + if(infinity) { + return result.concat(index.boundary) + } + return result +} + +},{"binary-search-bounds":54}],52:[function(require,module,exports){ +'use strict' + +var bsearch = require('binary-search-bounds') +var orient = require('robust-orientation')[3] + +var EVENT_POINT = 0 +var EVENT_END = 1 +var EVENT_START = 2 + +module.exports = monotoneTriangulate + +//A partial convex hull fragment, made of two unimonotone polygons +function PartialHull(a, b, idx, lowerIds, upperIds) { + this.a = a + this.b = b + this.idx = idx + this.lowerIds = lowerIds + this.upperIds = upperIds +} + +//An event in the sweep line procedure +function Event(a, b, type, idx) { + this.a = a + this.b = b + this.type = type + this.idx = idx +} + +//This is used to compare events for the sweep line procedure +// Points are: +// 1. sorted lexicographically +// 2. sorted by type (point < end < start) +// 3. segments sorted by winding order +// 4. sorted by index +function compareEvent(a, b) { + var d = + (a.a[0] - b.a[0]) || + (a.a[1] - b.a[1]) || + (a.type - b.type) + if(d) { return d } + if(a.type !== EVENT_POINT) { + d = orient(a.a, a.b, b.b) + if(d) { return d } + } + return a.idx - b.idx +} + +function testPoint(hull, p) { + return orient(hull.a, hull.b, p) +} + +function addPoint(cells, hulls, points, p, idx) { + var lo = bsearch.lt(hulls, p, testPoint) + var hi = bsearch.gt(hulls, p, testPoint) + for(var i=lo; i 1 && orient( + points[lowerIds[m-2]], + points[lowerIds[m-1]], + p) > 0) { + cells.push( + [lowerIds[m-1], + lowerIds[m-2], + idx]) + m -= 1 + } + lowerIds.length = m + lowerIds.push(idx) + + //Insert p into upper hull + var upperIds = hull.upperIds + var m = upperIds.length + while(m > 1 && orient( + points[upperIds[m-2]], + points[upperIds[m-1]], + p) < 0) { + cells.push( + [upperIds[m-2], + upperIds[m-1], + idx]) + m -= 1 + } + upperIds.length = m + upperIds.push(idx) + } +} + +function findSplit(hull, edge) { + var d + if(hull.a[0] < edge.a[0]) { + d = orient(hull.a, hull.b, edge.a) + } else { + d = orient(edge.b, edge.a, hull.a) + } + if(d) { return d } + if(edge.b[0] < hull.b[0]) { + d = orient(hull.a, hull.b, edge.b) + } else { + d = orient(edge.b, edge.a, hull.b) + } + return d || hull.idx - edge.idx +} + +function splitHulls(hulls, points, event) { + var splitIdx = bsearch.le(hulls, event, findSplit) + var hull = hulls[splitIdx] + var upperIds = hull.upperIds + var x = upperIds[upperIds.length-1] + hull.upperIds = [x] + hulls.splice(splitIdx+1, 0, + new PartialHull(event.a, event.b, event.idx, [x], upperIds)) +} + + +function mergeHulls(hulls, points, event) { + //Swap pointers for merge search + var tmp = event.a + event.a = event.b + event.b = tmp + var mergeIdx = bsearch.eq(hulls, event, findSplit) + var upper = hulls[mergeIdx] + var lower = hulls[mergeIdx-1] + lower.upperIds = upper.upperIds + hulls.splice(mergeIdx, 1) +} + + +function monotoneTriangulate(points, edges) { + + var numPoints = points.length + var numEdges = edges.length + + var events = [] + + //Create point events + for(var i=0; i b[0]) { + events.push( + new Event(b, a, EVENT_START, i), + new Event(a, b, EVENT_END, i)) + } + } + + //Sort events + events.sort(compareEvent) + + //Initialize hull + var minX = events[0].a[0] - (1 + Math.abs(events[0].a[0])) * Math.pow(2, -52) + var hull = [ new PartialHull([minX, 1], [minX, 0], -1, [], [], [], []) ] + + //Process events in order + var cells = [] + for(var i=0, numEvents=events.length; i= 0 + } +})() + +proto.removeTriangle = function(i, j, k) { + var stars = this.stars + removePair(stars[i], j, k) + removePair(stars[j], k, i) + removePair(stars[k], i, j) +} + +proto.addTriangle = function(i, j, k) { + var stars = this.stars + stars[i].push(j, k) + stars[j].push(k, i) + stars[k].push(i, j) +} + +proto.opposite = function(j, i) { + var list = this.stars[i] + for(var k=1, n=list.length; k>>1,x=a[m]"] + if(earlyOut) { + if(predicate.indexOf("c") < 0) { + code.push(";if(x===y){return m}else if(x<=y){") + } else { + code.push(";var p=c(x,y);if(p===0){return m}else if(p<=0){") + } + } else { + code.push(";if(", predicate, "){i=m;") + } + if(reversed) { + code.push("l=m+1}else{h=m-1}") + } else { + code.push("h=m-1}else{l=m+1}") + } + code.push("}") + if(earlyOut) { + code.push("return -1};") + } else { + code.push("return i};") + } + return code.join("") +} + +function compileBoundsSearch(predicate, reversed, suffix, earlyOut) { + var result = new Function([ + compileSearch("A", "x" + predicate + "y", reversed, ["y"], earlyOut), + compileSearch("P", "c(x,y)" + predicate + "0", reversed, ["y", "c"], earlyOut), +"function dispatchBsearch", suffix, "(a,y,c,l,h){\ +if(typeof(c)==='function'){\ +return P(a,(l===void 0)?0:l|0,(h===void 0)?a.length-1:h|0,y,c)\ +}else{\ +return A(a,(c===void 0)?0:c|0,(l===void 0)?a.length-1:l|0,y)\ +}}\ +return dispatchBsearch", suffix].join("")) + return result() +} + +module.exports = { + ge: compileBoundsSearch(">=", false, "GE"), + gt: compileBoundsSearch(">", false, "GT"), + lt: compileBoundsSearch("<", true, "LT"), + le: compileBoundsSearch("<=", true, "LE"), + eq: compileBoundsSearch("-", true, "EQ", true) +} + +},{}],55:[function(require,module,exports){ +'use strict' + +module.exports = orientation + +function orientation(s) { + var p = 1 + for(var i=1; i= 0; --i) { + var junction = junctions[i] + e = junction[0] + + var edge = edges[e] + var s = edge[0] + var t = edge[1] + + // Check if edge is not lexicographically sorted + var a = floatPoints[s] + var b = floatPoints[t] + if (((a[0] - b[0]) || (a[1] - b[1])) < 0) { + var tmp = s + s = t + t = tmp + } + + // Split leading edge + edge[0] = s + var last = edge[1] = junction[1] + + // If we are grouping edges by color, remember to track data + var color + if (useColor) { + color = edge[2] + } + + // Split other edges + while (i > 0 && junctions[i - 1][0] === e) { + var junction = junctions[--i] + var next = junction[1] + if (useColor) { + edges.push([last, next, color]) } else { - // At least give some kind of context to the user - var err = new Error('Uncaught, unspecified "error" event. (' + er + ')'); - err.context = er; - throw err; + edges.push([last, next]) } + last = next } - } - handler = this._events[type]; - - if (isUndefined(handler)) - return false; - - if (isFunction(handler)) { - switch (arguments.length) { - // fast cases - case 1: - handler.call(this); - break; - case 2: - handler.call(this, arguments[1]); - break; - case 3: - handler.call(this, arguments[1], arguments[2]); - break; - // slower - default: - args = Array.prototype.slice.call(arguments, 1); - handler.apply(this, args); - } - } else if (isObject(handler)) { - args = Array.prototype.slice.call(arguments, 1); - listeners = handler.slice(); - len = listeners.length; - for (i = 0; i < len; i++) - listeners[i].apply(this, args); - } - - return true; -}; - -EventEmitter.prototype.addListener = function(type, listener) { - var m; - - if (!isFunction(listener)) - throw TypeError('listener must be a function'); - - if (!this._events) - this._events = {}; - - // To avoid recursion in the case that type === "newListener"! Before - // adding it to the listeners, first emit "newListener". - if (this._events.newListener) - this.emit('newListener', type, - isFunction(listener.listener) ? - listener.listener : listener); - - if (!this._events[type]) - // Optimize the case of one listener. Don't need the extra array object. - this._events[type] = listener; - else if (isObject(this._events[type])) - // If we've already got an array, just append. - this._events[type].push(listener); - else - // Adding the second element, need to change to array. - this._events[type] = [this._events[type], listener]; - - // Check for listener leak - if (isObject(this._events[type]) && !this._events[type].warned) { - if (!isUndefined(this._maxListeners)) { - m = this._maxListeners; + // Add final edge + if (useColor) { + edges.push([last, t, color]) } else { - m = EventEmitter.defaultMaxListeners; - } - - if (m && m > 0 && this._events[type].length > m) { - this._events[type].warned = true; - console.error('(node) warning: possible EventEmitter memory ' + - 'leak detected. %d listeners added. ' + - 'Use emitter.setMaxListeners() to increase limit.', - this._events[type].length); - if (typeof console.trace === 'function') { - // not supported in IE 10 - console.trace(); - } + edges.push([last, t]) } } - return this; -}; + // Return constructed rational points + return ratPoints +} -EventEmitter.prototype.on = EventEmitter.prototype.addListener; +// Merge overlapping points +function dedupPoints (floatPoints, ratPoints, floatBounds) { + var numPoints = ratPoints.length + var uf = new UnionFind(numPoints) -EventEmitter.prototype.once = function(type, listener) { - if (!isFunction(listener)) - throw TypeError('listener must be a function'); + // Compute rational bounds + var bounds = [] + for (var i = 0; i < ratPoints.length; ++i) { + var p = ratPoints[i] + var xb = boundRat(p[0]) + var yb = boundRat(p[1]) + bounds.push([ + nextafter(xb[0], -Infinity), + nextafter(yb[0], -Infinity), + nextafter(xb[1], Infinity), + nextafter(yb[1], Infinity) + ]) + } - var fired = false; + // Link all points with over lapping boxes + boxIntersect(bounds, function (i, j) { + uf.link(i, j) + }) - function g() { - this.removeListener(type, g); - - if (!fired) { - fired = true; - listener.apply(this, arguments); + // Do 1 pass over points to combine points in label sets + var noDupes = true + var labels = new Array(numPoints) + for (var i = 0; i < numPoints; ++i) { + var j = uf.find(i) + if (j !== i) { + // Clear no-dupes flag, zero out label + noDupes = false + // Make each point the top-left point from its cell + floatPoints[j] = [ + Math.min(floatPoints[i][0], floatPoints[j][0]), + Math.min(floatPoints[i][1], floatPoints[j][1]) + ] } } - g.listener = listener; - this.on(type, g); + // If no duplicates, return null to signal termination + if (noDupes) { + return null + } - return this; -}; - -// emits a 'removeListener' event iff the listener was removed -EventEmitter.prototype.removeListener = function(type, listener) { - var list, position, length, i; - - if (!isFunction(listener)) - throw TypeError('listener must be a function'); - - if (!this._events || !this._events[type]) - return this; - - list = this._events[type]; - length = list.length; - position = -1; - - if (list === listener || - (isFunction(list.listener) && list.listener === listener)) { - delete this._events[type]; - if (this._events.removeListener) - this.emit('removeListener', type, listener); - - } else if (isObject(list)) { - for (i = length; i-- > 0;) { - if (list[i] === listener || - (list[i].listener && list[i].listener === listener)) { - position = i; - break; - } - } - - if (position < 0) - return this; - - if (list.length === 1) { - list.length = 0; - delete this._events[type]; + var ptr = 0 + for (var i = 0; i < numPoints; ++i) { + var j = uf.find(i) + if (j === i) { + labels[i] = ptr + floatPoints[ptr++] = floatPoints[i] } else { - list.splice(position, 1); + labels[i] = -1 } - - if (this._events.removeListener) - this.emit('removeListener', type, listener); } - return this; -}; + floatPoints.length = ptr -EventEmitter.prototype.removeAllListeners = function(type) { - var key, listeners; - - if (!this._events) - return this; - - // not listening for removeListener, no need to emit - if (!this._events.removeListener) { - if (arguments.length === 0) - this._events = {}; - else if (this._events[type]) - delete this._events[type]; - return this; - } - - // emit removeListener for all listeners on all events - if (arguments.length === 0) { - for (key in this._events) { - if (key === 'removeListener') continue; - this.removeAllListeners(key); + // Do a second pass to fix up missing labels + for (var i = 0; i < numPoints; ++i) { + if (labels[i] < 0) { + labels[i] = labels[uf.find(i)] } - this.removeAllListeners('removeListener'); - this._events = {}; - return this; } - listeners = this._events[type]; + // Return resulting union-find data structure + return labels +} - if (isFunction(listeners)) { - this.removeListener(type, listeners); - } else if (listeners) { - // LIFO order - while (listeners.length) - this.removeListener(type, listeners[listeners.length - 1]); +function compareLex2 (a, b) { return (a[0] - b[0]) || (a[1] - b[1]) } +function compareLex3 (a, b) { + var d = (a[0] - b[0]) || (a[1] - b[1]) + if (d) { + return d } - delete this._events[type]; - - return this; -}; - -EventEmitter.prototype.listeners = function(type) { - var ret; - if (!this._events || !this._events[type]) - ret = []; - else if (isFunction(this._events[type])) - ret = [this._events[type]]; - else - ret = this._events[type].slice(); - return ret; -}; - -EventEmitter.prototype.listenerCount = function(type) { - if (this._events) { - var evlistener = this._events[type]; - - if (isFunction(evlistener)) - return 1; - else if (evlistener) - return evlistener.length; + if (a[2] < b[2]) { + return -1 + } else if (a[2] > b[2]) { + return 1 } - return 0; -}; - -EventEmitter.listenerCount = function(emitter, type) { - return emitter.listenerCount(type); -}; - -function isFunction(arg) { - return typeof arg === 'function'; + return 0 } -function isNumber(arg) { - return typeof arg === 'number'; +// Remove duplicate edge labels +function dedupEdges (edges, labels, useColor) { + if (edges.length === 0) { + return + } + if (labels) { + for (var i = 0; i < edges.length; ++i) { + var e = edges[i] + var a = labels[e[0]] + var b = labels[e[1]] + e[0] = Math.min(a, b) + e[1] = Math.max(a, b) + } + } else { + for (var i = 0; i < edges.length; ++i) { + var e = edges[i] + var a = e[0] + var b = e[1] + e[0] = Math.min(a, b) + e[1] = Math.max(a, b) + } + } + if (useColor) { + edges.sort(compareLex3) + } else { + edges.sort(compareLex2) + } + var ptr = 1 + for (var i = 1; i < edges.length; ++i) { + var prev = edges[i - 1] + var next = edges[i] + if (next[0] === prev[0] && next[1] === prev[1] && + (!useColor || next[2] === prev[2])) { + continue + } + edges[ptr++] = next + } + edges.length = ptr } -function isObject(arg) { - return typeof arg === 'object' && arg !== null; +function preRound (points, edges, useColor) { + var labels = dedupPoints(points, [], boundPoints(points)) + dedupEdges(edges, labels, useColor) + return !!labels } -function isUndefined(arg) { - return arg === void 0; +// Repeat until convergence +function snapRound (points, edges, useColor) { + // 1. find edge crossings + var edgeBounds = boundEdges(points, edges) + var crossings = getCrossings(points, edges, edgeBounds) + + // 2. find t-junctions + var vertBounds = boundPoints(points) + var tjunctions = getTJunctions(points, edges, edgeBounds, vertBounds) + + // 3. cut edges, construct rational points + var ratPoints = cutEdges(points, edges, crossings, tjunctions, useColor) + + // 4. dedupe verts + var labels = dedupPoints(points, ratPoints, vertBounds) + + // 5. dedupe edges + dedupEdges(edges, labels, useColor) + + // 6. check termination + if (!labels) { + return (crossings.length > 0 || tjunctions.length > 0) + } + + // More iterations necessary + return true } -},{}],38:[function(require,module,exports){ -arguments[4][12][0].apply(exports,arguments) -},{"dup":12}],39:[function(require,module,exports){ -/*! - * Determine if an object is a Buffer +// Main loop, runs PSLG clean up until completion +function cleanPSLG (points, edges, colors) { + // If using colors, augment edges with color data + var prevEdges + if (colors) { + prevEdges = edges + var augEdges = new Array(edges.length) + for (var i = 0; i < edges.length; ++i) { + var e = edges[i] + augEdges[i] = [e[0], e[1], colors[i]] + } + edges = augEdges + } + + // First round: remove duplicate edges and points + var modified = preRound(points, edges, !!colors) + + // Run snap rounding until convergence + while (snapRound(points, edges, !!colors)) { + modified = true + } + + // Strip color tags + if (!!colors && modified) { + prevEdges.length = 0 + colors.length = 0 + for (var i = 0; i < edges.length; ++i) { + var e = edges[i] + prevEdges.push([e[0], e[1]]) + colors.push(e[2]) + } + } + + return modified +} + +},{"./lib/rat-seg-intersect":59,"big-rat":19,"big-rat/cmp":17,"big-rat/to-float":31,"box-intersect":36,"nextafter":444,"rat-vec":931,"robust-segment-intersect":957,"union-find":993}],59:[function(require,module,exports){ +'use strict' + +module.exports = solveIntersection + +var ratMul = require('big-rat/mul') +var ratDiv = require('big-rat/div') +var ratSub = require('big-rat/sub') +var ratSign = require('big-rat/sign') +var rvSub = require('rat-vec/sub') +var rvAdd = require('rat-vec/add') +var rvMuls = require('rat-vec/muls') + +function ratPerp (a, b) { + return ratSub(ratMul(a[0], b[1]), ratMul(a[1], b[0])) +} + +// Solve for intersection +// x = a + t (b-a) +// (x - c) ^ (d-c) = 0 +// (t * (b-a) + (a-c) ) ^ (d-c) = 0 +// t * (b-a)^(d-c) = (d-c)^(a-c) +// t = (d-c)^(a-c) / (b-a)^(d-c) + +function solveIntersection (a, b, c, d) { + var ba = rvSub(b, a) + var dc = rvSub(d, c) + + var baXdc = ratPerp(ba, dc) + + if (ratSign(baXdc) === 0) { + return null + } + + var ac = rvSub(a, c) + var dcXac = ratPerp(dc, ac) + + var t = ratDiv(dcXac, baXdc) + var s = rvMuls(ba, t) + var r = rvAdd(a, s) + + return r +} + +},{"big-rat/div":18,"big-rat/mul":28,"big-rat/sign":29,"big-rat/sub":30,"rat-vec/add":930,"rat-vec/muls":932,"rat-vec/sub":933}],60:[function(require,module,exports){ +(function (Buffer){ +var clone = (function() { +'use strict'; + +/** + * Clones (copies) an Object using deep copying. * - * @author Feross Aboukhadijeh - * @license MIT + * This function supports circular references by default, but if you are certain + * there are no circular references in your object, you can save some CPU time + * by calling clone(obj, false). + * + * Caution: if `circular` is false and `parent` contains circular references, + * your program may enter an infinite loop and crash. + * + * @param `parent` - the object to be cloned + * @param `circular` - set to true if the object to be cloned may contain + * circular references. (optional - true by default) + * @param `depth` - set to a number if the object is only to be cloned to + * a particular depth. (optional - defaults to Infinity) + * @param `prototype` - sets the prototype to be used when cloning an object. + * (optional - defaults to parent prototype). +*/ +function clone(parent, circular, depth, prototype) { + var filter; + if (typeof circular === 'object') { + depth = circular.depth; + prototype = circular.prototype; + filter = circular.filter; + circular = circular.circular + } + // maintain two arrays for circular references, where corresponding parents + // and children have the same index + var allParents = []; + var allChildren = []; + + var useBuffer = typeof Buffer != 'undefined'; + + if (typeof circular == 'undefined') + circular = true; + + if (typeof depth == 'undefined') + depth = Infinity; + + // recurse this function so we don't reset allParents and allChildren + function _clone(parent, depth) { + // cloning null always returns null + if (parent === null) + return null; + + if (depth == 0) + return parent; + + var child; + var proto; + if (typeof parent != 'object') { + return parent; + } + + if (clone.__isArray(parent)) { + child = []; + } else if (clone.__isRegExp(parent)) { + child = new RegExp(parent.source, __getRegExpFlags(parent)); + if (parent.lastIndex) child.lastIndex = parent.lastIndex; + } else if (clone.__isDate(parent)) { + child = new Date(parent.getTime()); + } else if (useBuffer && Buffer.isBuffer(parent)) { + child = new Buffer(parent.length); + parent.copy(child); + return child; + } else { + if (typeof prototype == 'undefined') { + proto = Object.getPrototypeOf(parent); + child = Object.create(proto); + } + else { + child = Object.create(prototype); + proto = prototype; + } + } + + if (circular) { + var index = allParents.indexOf(parent); + + if (index != -1) { + return allChildren[index]; + } + allParents.push(parent); + allChildren.push(child); + } + + for (var i in parent) { + var attrs; + if (proto) { + attrs = Object.getOwnPropertyDescriptor(proto, i); + } + + if (attrs && attrs.set == null) { + continue; + } + child[i] = _clone(parent[i], depth - 1); + } + + return child; + } + + return _clone(parent, depth); +} + +/** + * Simple flat clone using prototype, accepts only objects, usefull for property + * override on FLAT configuration object (no nested props). + * + * USE WITH CAUTION! This may not behave as you wish if you do not know how this + * works. + */ +clone.clonePrototype = function clonePrototype(parent) { + if (parent === null) + return null; + + var c = function () {}; + c.prototype = parent; + return new c(); +}; + +// private utility functions + +function __objToStr(o) { + return Object.prototype.toString.call(o); +}; +clone.__objToStr = __objToStr; + +function __isDate(o) { + return typeof o === 'object' && __objToStr(o) === '[object Date]'; +}; +clone.__isDate = __isDate; + +function __isArray(o) { + return typeof o === 'object' && __objToStr(o) === '[object Array]'; +}; +clone.__isArray = __isArray; + +function __isRegExp(o) { + return typeof o === 'object' && __objToStr(o) === '[object RegExp]'; +}; +clone.__isRegExp = __isRegExp; + +function __getRegExpFlags(re) { + var flags = ''; + if (re.global) flags += 'g'; + if (re.ignoreCase) flags += 'i'; + if (re.multiline) flags += 'm'; + return flags; +}; +clone.__getRegExpFlags = __getRegExpFlags; + +return clone; +})(); + +if (typeof module === 'object' && module.exports) { + module.exports = clone; +} + +}).call(this,require("buffer").Buffer) +},{"buffer":46}],61:[function(require,module,exports){ +module.exports={ + "jet":[{"index":0,"rgb":[0,0,131]},{"index":0.125,"rgb":[0,60,170]},{"index":0.375,"rgb":[5,255,255]},{"index":0.625,"rgb":[255,255,0]},{"index":0.875,"rgb":[250,0,0]},{"index":1,"rgb":[128,0,0]}], + + "hsv":[{"index":0,"rgb":[255,0,0]},{"index":0.169,"rgb":[253,255,2]},{"index":0.173,"rgb":[247,255,2]},{"index":0.337,"rgb":[0,252,4]},{"index":0.341,"rgb":[0,252,10]},{"index":0.506,"rgb":[1,249,255]},{"index":0.671,"rgb":[2,0,253]},{"index":0.675,"rgb":[8,0,253]},{"index":0.839,"rgb":[255,0,251]},{"index":0.843,"rgb":[255,0,245]},{"index":1,"rgb":[255,0,6]}], + + "hot":[{"index":0,"rgb":[0,0,0]},{"index":0.3,"rgb":[230,0,0]},{"index":0.6,"rgb":[255,210,0]},{"index":1,"rgb":[255,255,255]}], + + "cool":[{"index":0,"rgb":[0,255,255]},{"index":1,"rgb":[255,0,255]}], + + "spring":[{"index":0,"rgb":[255,0,255]},{"index":1,"rgb":[255,255,0]}], + + "summer":[{"index":0,"rgb":[0,128,102]},{"index":1,"rgb":[255,255,102]}], + + "autumn":[{"index":0,"rgb":[255,0,0]},{"index":1,"rgb":[255,255,0]}], + + "winter":[{"index":0,"rgb":[0,0,255]},{"index":1,"rgb":[0,255,128]}], + + "bone":[{"index":0,"rgb":[0,0,0]},{"index":0.376,"rgb":[84,84,116]},{"index":0.753,"rgb":[169,200,200]},{"index":1,"rgb":[255,255,255]}], + + "copper":[{"index":0,"rgb":[0,0,0]},{"index":0.804,"rgb":[255,160,102]},{"index":1,"rgb":[255,199,127]}], + + "greys":[{"index":0,"rgb":[0,0,0]},{"index":1,"rgb":[255,255,255]}], + + "yignbu":[{"index":0,"rgb":[8,29,88]},{"index":0.125,"rgb":[37,52,148]},{"index":0.25,"rgb":[34,94,168]},{"index":0.375,"rgb":[29,145,192]},{"index":0.5,"rgb":[65,182,196]},{"index":0.625,"rgb":[127,205,187]},{"index":0.75,"rgb":[199,233,180]},{"index":0.875,"rgb":[237,248,217]},{"index":1,"rgb":[255,255,217]}], + + "greens":[{"index":0,"rgb":[0,68,27]},{"index":0.125,"rgb":[0,109,44]},{"index":0.25,"rgb":[35,139,69]},{"index":0.375,"rgb":[65,171,93]},{"index":0.5,"rgb":[116,196,118]},{"index":0.625,"rgb":[161,217,155]},{"index":0.75,"rgb":[199,233,192]},{"index":0.875,"rgb":[229,245,224]},{"index":1,"rgb":[247,252,245]}], + + "yiorrd":[{"index":0,"rgb":[128,0,38]},{"index":0.125,"rgb":[189,0,38]},{"index":0.25,"rgb":[227,26,28]},{"index":0.375,"rgb":[252,78,42]},{"index":0.5,"rgb":[253,141,60]},{"index":0.625,"rgb":[254,178,76]},{"index":0.75,"rgb":[254,217,118]},{"index":0.875,"rgb":[255,237,160]},{"index":1,"rgb":[255,255,204]}], + + "bluered":[{"index":0,"rgb":[0,0,255]},{"index":1,"rgb":[255,0,0]}], + + "rdbu":[{"index":0,"rgb":[5,10,172]},{"index":0.35,"rgb":[106,137,247]},{"index":0.5,"rgb":[190,190,190]},{"index":0.6,"rgb":[220,170,132]},{"index":0.7,"rgb":[230,145,90]},{"index":1,"rgb":[178,10,28]}], + + "picnic":[{"index":0,"rgb":[0,0,255]},{"index":0.1,"rgb":[51,153,255]},{"index":0.2,"rgb":[102,204,255]},{"index":0.3,"rgb":[153,204,255]},{"index":0.4,"rgb":[204,204,255]},{"index":0.5,"rgb":[255,255,255]},{"index":0.6,"rgb":[255,204,255]},{"index":0.7,"rgb":[255,153,255]},{"index":0.8,"rgb":[255,102,204]},{"index":0.9,"rgb":[255,102,102]},{"index":1,"rgb":[255,0,0]}], + + "rainbow":[{"index":0,"rgb":[150,0,90]},{"index":0.125,"rgb":[0,0,200]},{"index":0.25,"rgb":[0,25,255]},{"index":0.375,"rgb":[0,152,255]},{"index":0.5,"rgb":[44,255,150]},{"index":0.625,"rgb":[151,255,0]},{"index":0.75,"rgb":[255,234,0]},{"index":0.875,"rgb":[255,111,0]},{"index":1,"rgb":[255,0,0]}], + + "portland":[{"index":0,"rgb":[12,51,131]},{"index":0.25,"rgb":[10,136,186]},{"index":0.5,"rgb":[242,211,56]},{"index":0.75,"rgb":[242,143,56]},{"index":1,"rgb":[217,30,30]}], + + "blackbody":[{"index":0,"rgb":[0,0,0]},{"index":0.2,"rgb":[230,0,0]},{"index":0.4,"rgb":[230,210,0]},{"index":0.7,"rgb":[255,255,255]},{"index":1,"rgb":[160,200,255]}], + + "earth":[{"index":0,"rgb":[0,0,130]},{"index":0.1,"rgb":[0,180,180]},{"index":0.2,"rgb":[40,210,40]},{"index":0.4,"rgb":[230,230,50]},{"index":0.6,"rgb":[120,70,20]},{"index":1,"rgb":[255,255,255]}], + + "electric":[{"index":0,"rgb":[0,0,0]},{"index":0.15,"rgb":[30,0,100]},{"index":0.4,"rgb":[120,0,100]},{"index":0.6,"rgb":[160,90,0]},{"index":0.8,"rgb":[230,200,0]},{"index":1,"rgb":[255,250,220]}], + + "alpha": [{"index":0, "rgb": [255,255,255,0]},{"index":0, "rgb": [255,255,255,1]}], + + "viridis": [{"index":0,"rgb":[68,1,84]},{"index":0.13,"rgb":[71,44,122]},{"index":0.25,"rgb":[59,81,139]},{"index":0.38,"rgb":[44,113,142]},{"index":0.5,"rgb":[33,144,141]},{"index":0.63,"rgb":[39,173,129]},{"index":0.75,"rgb":[92,200,99]},{"index":0.88,"rgb":[170,220,50]},{"index":1,"rgb":[253,231,37]}], + + "inferno": [{"index":0,"rgb":[0,0,4]},{"index":0.13,"rgb":[31,12,72]},{"index":0.25,"rgb":[85,15,109]},{"index":0.38,"rgb":[136,34,106]},{"index":0.5,"rgb":[186,54,85]},{"index":0.63,"rgb":[227,89,51]},{"index":0.75,"rgb":[249,140,10]},{"index":0.88,"rgb":[249,201,50]},{"index":1,"rgb":[252,255,164]}], + + "magma": [{"index":0,"rgb":[0,0,4]},{"index":0.13,"rgb":[28,16,68]},{"index":0.25,"rgb":[79,18,123]},{"index":0.38,"rgb":[129,37,129]},{"index":0.5,"rgb":[181,54,122]},{"index":0.63,"rgb":[229,80,100]},{"index":0.75,"rgb":[251,135,97]},{"index":0.88,"rgb":[254,194,135]},{"index":1,"rgb":[252,253,191]}], + + "plasma": [{"index":0,"rgb":[13,8,135]},{"index":0.13,"rgb":[75,3,161]},{"index":0.25,"rgb":[125,3,168]},{"index":0.38,"rgb":[168,34,150]},{"index":0.5,"rgb":[203,70,121]},{"index":0.63,"rgb":[229,107,93]},{"index":0.75,"rgb":[248,148,65]},{"index":0.88,"rgb":[253,195,40]},{"index":1,"rgb":[240,249,33]}], + + "warm": [{"index":0,"rgb":[125,0,179]},{"index":0.13,"rgb":[172,0,187]},{"index":0.25,"rgb":[219,0,170]},{"index":0.38,"rgb":[255,0,130]},{"index":0.5,"rgb":[255,63,74]},{"index":0.63,"rgb":[255,123,0]},{"index":0.75,"rgb":[234,176,0]},{"index":0.88,"rgb":[190,228,0]},{"index":1,"rgb":[147,255,0]}], + + "cool": [{"index":0,"rgb":[125,0,179]},{"index":0.13,"rgb":[116,0,218]},{"index":0.25,"rgb":[98,74,237]},{"index":0.38,"rgb":[68,146,231]},{"index":0.5,"rgb":[0,204,197]},{"index":0.63,"rgb":[0,247,146]},{"index":0.75,"rgb":[0,255,88]},{"index":0.88,"rgb":[40,255,8]},{"index":1,"rgb":[147,255,0]}], + + "rainbow-soft": [{"index":0,"rgb":[125,0,179]},{"index":0.1,"rgb":[199,0,180]},{"index":0.2,"rgb":[255,0,121]},{"index":0.3,"rgb":[255,108,0]},{"index":0.4,"rgb":[222,194,0]},{"index":0.5,"rgb":[150,255,0]},{"index":0.6,"rgb":[0,255,55]},{"index":0.7,"rgb":[0,246,150]},{"index":0.8,"rgb":[50,167,222]},{"index":0.9,"rgb":[103,51,235]},{"index":1,"rgb":[124,0,186]}], + + "bathymetry": [{"index":0,"rgb":[40,26,44]},{"index":0.13,"rgb":[59,49,90]},{"index":0.25,"rgb":[64,76,139]},{"index":0.38,"rgb":[63,110,151]},{"index":0.5,"rgb":[72,142,158]},{"index":0.63,"rgb":[85,174,163]},{"index":0.75,"rgb":[120,206,163]},{"index":0.88,"rgb":[187,230,172]},{"index":1,"rgb":[253,254,204]}], + + "cdom": [{"index":0,"rgb":[47,15,62]},{"index":0.13,"rgb":[87,23,86]},{"index":0.25,"rgb":[130,28,99]},{"index":0.38,"rgb":[171,41,96]},{"index":0.5,"rgb":[206,67,86]},{"index":0.63,"rgb":[230,106,84]},{"index":0.75,"rgb":[242,149,103]},{"index":0.88,"rgb":[249,193,135]},{"index":1,"rgb":[254,237,176]}], + + "chlorophyll": [{"index":0,"rgb":[18,36,20]},{"index":0.13,"rgb":[25,63,41]},{"index":0.25,"rgb":[24,91,59]},{"index":0.38,"rgb":[13,119,72]},{"index":0.5,"rgb":[18,148,80]},{"index":0.63,"rgb":[80,173,89]},{"index":0.75,"rgb":[132,196,122]},{"index":0.88,"rgb":[175,221,162]},{"index":1,"rgb":[215,249,208]}], + + "density": [{"index":0,"rgb":[54,14,36]},{"index":0.13,"rgb":[89,23,80]},{"index":0.25,"rgb":[110,45,132]},{"index":0.38,"rgb":[120,77,178]},{"index":0.5,"rgb":[120,113,213]},{"index":0.63,"rgb":[115,151,228]},{"index":0.75,"rgb":[134,185,227]},{"index":0.88,"rgb":[177,214,227]},{"index":1,"rgb":[230,241,241]}], + + "freesurface-blue": [{"index":0,"rgb":[30,4,110]},{"index":0.13,"rgb":[47,14,176]},{"index":0.25,"rgb":[41,45,236]},{"index":0.38,"rgb":[25,99,212]},{"index":0.5,"rgb":[68,131,200]},{"index":0.63,"rgb":[114,156,197]},{"index":0.75,"rgb":[157,181,203]},{"index":0.88,"rgb":[200,208,216]},{"index":1,"rgb":[241,237,236]}], + + "freesurface-red": [{"index":0,"rgb":[60,9,18]},{"index":0.13,"rgb":[100,17,27]},{"index":0.25,"rgb":[142,20,29]},{"index":0.38,"rgb":[177,43,27]},{"index":0.5,"rgb":[192,87,63]},{"index":0.63,"rgb":[205,125,105]},{"index":0.75,"rgb":[216,162,148]},{"index":0.88,"rgb":[227,199,193]},{"index":1,"rgb":[241,237,236]}], + + "oxygen": [{"index":0,"rgb":[64,5,5]},{"index":0.13,"rgb":[106,6,15]},{"index":0.25,"rgb":[144,26,7]},{"index":0.38,"rgb":[168,64,3]},{"index":0.5,"rgb":[188,100,4]},{"index":0.63,"rgb":[206,136,11]},{"index":0.75,"rgb":[220,174,25]},{"index":0.88,"rgb":[231,215,44]},{"index":1,"rgb":[248,254,105]}], + + "par": [{"index":0,"rgb":[51,20,24]},{"index":0.13,"rgb":[90,32,35]},{"index":0.25,"rgb":[129,44,34]},{"index":0.38,"rgb":[159,68,25]},{"index":0.5,"rgb":[182,99,19]},{"index":0.63,"rgb":[199,134,22]},{"index":0.75,"rgb":[212,171,35]},{"index":0.88,"rgb":[221,210,54]},{"index":1,"rgb":[225,253,75]}], + + "phase": [{"index":0,"rgb":[145,105,18]},{"index":0.13,"rgb":[184,71,38]},{"index":0.25,"rgb":[186,58,115]},{"index":0.38,"rgb":[160,71,185]},{"index":0.5,"rgb":[110,97,218]},{"index":0.63,"rgb":[50,123,164]},{"index":0.75,"rgb":[31,131,110]},{"index":0.88,"rgb":[77,129,34]},{"index":1,"rgb":[145,105,18]}], + + "salinity": [{"index":0,"rgb":[42,24,108]},{"index":0.13,"rgb":[33,50,162]},{"index":0.25,"rgb":[15,90,145]},{"index":0.38,"rgb":[40,118,137]},{"index":0.5,"rgb":[59,146,135]},{"index":0.63,"rgb":[79,175,126]},{"index":0.75,"rgb":[120,203,104]},{"index":0.88,"rgb":[193,221,100]},{"index":1,"rgb":[253,239,154]}], + + "temperature": [{"index":0,"rgb":[4,35,51]},{"index":0.13,"rgb":[23,51,122]},{"index":0.25,"rgb":[85,59,157]},{"index":0.38,"rgb":[129,79,143]},{"index":0.5,"rgb":[175,95,130]},{"index":0.63,"rgb":[222,112,101]},{"index":0.75,"rgb":[249,146,66]},{"index":0.88,"rgb":[249,196,65]},{"index":1,"rgb":[232,250,91]}], + + "turbidity": [{"index":0,"rgb":[34,31,27]},{"index":0.13,"rgb":[65,50,41]},{"index":0.25,"rgb":[98,69,52]},{"index":0.38,"rgb":[131,89,57]},{"index":0.5,"rgb":[161,112,59]},{"index":0.63,"rgb":[185,140,66]},{"index":0.75,"rgb":[202,174,88]},{"index":0.88,"rgb":[216,209,126]},{"index":1,"rgb":[233,246,171]}], + + "velocity-blue": [{"index":0,"rgb":[17,32,64]},{"index":0.13,"rgb":[35,52,116]},{"index":0.25,"rgb":[29,81,156]},{"index":0.38,"rgb":[31,113,162]},{"index":0.5,"rgb":[50,144,169]},{"index":0.63,"rgb":[87,173,176]},{"index":0.75,"rgb":[149,196,189]},{"index":0.88,"rgb":[203,221,211]},{"index":1,"rgb":[254,251,230]}], + + "velocity-green": [{"index":0,"rgb":[23,35,19]},{"index":0.13,"rgb":[24,64,38]},{"index":0.25,"rgb":[11,95,45]},{"index":0.38,"rgb":[39,123,35]},{"index":0.5,"rgb":[95,146,12]},{"index":0.63,"rgb":[152,165,18]},{"index":0.75,"rgb":[201,186,69]},{"index":0.88,"rgb":[233,216,137]},{"index":1,"rgb":[255,253,205]}], + + "cubehelix": [{"index":0,"rgb":[0,0,0]},{"index":0.07,"rgb":[22,5,59]},{"index":0.13,"rgb":[60,4,105]},{"index":0.2,"rgb":[109,1,135]},{"index":0.27,"rgb":[161,0,147]},{"index":0.33,"rgb":[210,2,142]},{"index":0.4,"rgb":[251,11,123]},{"index":0.47,"rgb":[255,29,97]},{"index":0.53,"rgb":[255,54,69]},{"index":0.6,"rgb":[255,85,46]},{"index":0.67,"rgb":[255,120,34]},{"index":0.73,"rgb":[255,157,37]},{"index":0.8,"rgb":[241,191,57]},{"index":0.87,"rgb":[224,220,93]},{"index":0.93,"rgb":[218,241,142]},{"index":1,"rgb":[227,253,198]}] +}; + +},{}],62:[function(require,module,exports){ +/* + * Ben Postlethwaite + * January 2013 + * License MIT + */ +'use strict'; + +var at = require('arraytools'); +var clone = require('clone'); +var colorScale = require('./colorScales'); + +module.exports = createColormap; + +function createColormap (spec) { + /* + * Default Options + */ + var indicies, rgba, fromrgba, torgba, + nsteps, cmap, colormap, format, + nshades, colors, alpha, index, i, + r = [], + g = [], + b = [], + a = []; + + if ( !at.isPlainObject(spec) ) spec = {}; + + nshades = spec.nshades || 72; + format = spec.format || 'hex'; + + colormap = spec.colormap; + if (!colormap) colormap = 'jet'; + + if (typeof colormap === 'string') { + colormap = colormap.toLowerCase(); + + if (!colorScale[colormap]) { + throw Error(colormap + ' not a supported colorscale'); + } + + cmap = clone(colorScale[colormap]); + + } else if (Array.isArray(colormap)) { + cmap = clone(colormap); + + } else { + throw Error('unsupported colormap option', colormap); + } + + if (cmap.length > nshades) { + throw new Error( + colormap+' map requires nshades to be at least size '+cmap.length + ); + } + + if (!Array.isArray(spec.alpha)) { + + if (typeof spec.alpha === 'number') { + alpha = [spec.alpha, spec.alpha]; + + } else { + alpha = [1, 1]; + } + + } else if (spec.alpha.length !== 2) { + alpha = [1, 1]; + + } else { + alpha = clone(spec.alpha); + } + + /* + * map index points from 0->1 to 0 -> n-1 + */ + indicies = cmap.map(function(c) { + return Math.round(c.index * nshades); + }); + + /* + * Add alpha channel to the map + */ + if (alpha[0] < 0) alpha[0] = 0; + if (alpha[1] < 0) alpha[0] = 0; + if (alpha[0] > 1) alpha[0] = 1; + if (alpha[1] > 1) alpha[0] = 1; + + for (i = 0; i < indicies.length; ++i) { + index = cmap[i].index; + rgba = cmap[i].rgb; + + // if user supplies their own map use it + if (rgba.length === 4 && rgba[3] >= 0 && rgba[3] <= 1) continue; + rgba[3] = alpha[0] + (alpha[1] - alpha[0])*index; + } + + /* + * map increasing linear values between indicies to + * linear steps in colorvalues + */ + for (i = 0; i < indicies.length-1; ++i) { + nsteps = indicies[i+1] - indicies[i]; + fromrgba = cmap[i].rgb; + torgba = cmap[i+1].rgb; + r = r.concat(at.linspace(fromrgba[0], torgba[0], nsteps ) ); + g = g.concat(at.linspace(fromrgba[1], torgba[1], nsteps ) ); + b = b.concat(at.linspace(fromrgba[2], torgba[2], nsteps ) ); + a = a.concat(at.linspace(fromrgba[3], torgba[3], nsteps ) ); + } + + r = r.map( Math.round ); + g = g.map( Math.round ); + b = b.map( Math.round ); + + colors = at.zip(r, g, b, a); + + if (format === 'hex') colors = colors.map( rgb2hex ); + if (format === 'rgbaString') colors = colors.map( rgbaStr ); + + return colors; +}; + + +function rgb2hex (rgba) { + var dig, hex = '#'; + for (var i = 0; i < 3; ++i) { + dig = rgba[i]; + dig = dig.toString(16); + hex += ('00' + dig).substr( dig.length ); + } + return hex; +} + +function rgbaStr (rgba) { + return 'rgba(' + rgba.join(',') + ')'; +} + +},{"./colorScales":61,"arraytools":8,"clone":60}],63:[function(require,module,exports){ +"use strict" + +module.exports = compareAngle + +var orient = require("robust-orientation") +var sgn = require("signum") +var twoSum = require("two-sum") +var robustProduct = require("robust-product") +var robustSum = require("robust-sum") + +function testInterior(a, b, c) { + var x0 = twoSum(a[0], -b[0]) + var y0 = twoSum(a[1], -b[1]) + var x1 = twoSum(c[0], -b[0]) + var y1 = twoSum(c[1], -b[1]) + + var d = robustSum( + robustProduct(x0, x1), + robustProduct(y0, y1)) + + return d[d.length-1] >= 0 +} + +function compareAngle(a, b, c, d) { + var bcd = orient(b, c, d) + if(bcd === 0) { + //Handle degenerate cases + var sabc = sgn(orient(a, b, c)) + var sabd = sgn(orient(a, b, d)) + if(sabc === sabd) { + if(sabc === 0) { + var ic = testInterior(a, b, c) + var id = testInterior(a, b, d) + if(ic === id) { + return 0 + } else if(ic) { + return 1 + } else { + return -1 + } + } + return 0 + } else if(sabd === 0) { + if(sabc > 0) { + return -1 + } else if(testInterior(a, b, d)) { + return -1 + } else { + return 1 + } + } else if(sabc === 0) { + if(sabd > 0) { + return 1 + } else if(testInterior(a, b, c)) { + return 1 + } else { + return -1 + } + } + return sgn(sabd - sabc) + } + var abc = orient(a, b, c) + if(abc > 0) { + if(bcd > 0 && orient(a, b, d) > 0) { + return 1 + } + return -1 + } else if(abc < 0) { + if(bcd > 0 || orient(a, b, d) > 0) { + return 1 + } + return -1 + } else { + var abd = orient(a, b, d) + if(abd > 0) { + return 1 + } else { + if(testInterior(a, b, c)) { + return 1 + } else { + return -1 + } + } + } +} +},{"robust-orientation":954,"robust-product":955,"robust-sum":959,"signum":965,"two-sum":991}],64:[function(require,module,exports){ +module.exports = compareCells + +var min = Math.min + +function compareInt(a, b) { + return a - b +} + +function compareCells(a, b) { + var n = a.length + , t = a.length - b.length + if(t) { + return t + } + switch(n) { + case 0: + return 0 + case 1: + return a[0] - b[0] + case 2: + return (a[0]+a[1]-b[0]-b[1]) || + min(a[0],a[1]) - min(b[0],b[1]) + case 3: + var l1 = a[0]+a[1] + , m1 = b[0]+b[1] + t = l1+a[2] - (m1+b[2]) + if(t) { + return t + } + var l0 = min(a[0], a[1]) + , m0 = min(b[0], b[1]) + return min(l0, a[2]) - min(m0, b[2]) || + min(l0+a[2], l1) - min(m0+b[2], m1) + case 4: + var aw=a[0], ax=a[1], ay=a[2], az=a[3] + , bw=b[0], bx=b[1], by=b[2], bz=b[3] + return (aw+ax+ay+az)-(bw+bx+by+bz) || + min(aw,ax,ay,az)-min(bw,bx,by,bz,bw) || + min(aw+ax,aw+ay,aw+az,ax+ay,ax+az,ay+az) - + min(bw+bx,bw+by,bw+bz,bx+by,bx+bz,by+bz) || + min(aw+ax+ay,aw+ax+az,aw+ay+az,ax+ay+az) - + min(bw+bx+by,bw+bx+bz,bw+by+bz,bx+by+bz) + default: + var as = a.slice().sort(compareInt) + var bs = b.slice().sort(compareInt) + for(var i=0; i (http://fengmk2.github.com) */ -// The _isBuffer check is for Safari 5-7 support, because it's missing -// Object.prototype.constructor. Remove this eventually -module.exports = function (obj) { - return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) +'use strict'; + +/** + * Module dependencies. + */ + +var Readable = require('readable-stream').Readable; +var util = require('util'); + +module.exports = ContentStream; + +function ContentStream(obj, options) { + if (!(this instanceof ContentStream)) { + return new ContentStream(obj, options); + } + Readable.call(this, options); + if (obj === null || obj === undefined) { + obj = String(obj); + } + this._obj = obj; } -function isBuffer (obj) { - return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) -} +util.inherits(ContentStream, Readable); -// For Node v0.10 support. Remove this eventually. -function isSlowBuffer (obj) { - return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) -} +ContentStream.prototype._read = function (n) { + var obj = this._obj; + if (typeof obj === 'string') { + this.push(new Buffer(obj)); + } else if (Buffer.isBuffer(obj)) { + this.push(obj); + } else { + this.push(new Buffer(JSON.stringify(obj))); + } + this.push(null); +}; -},{}],40:[function(require,module,exports){ +}).call(this,require("buffer").Buffer) +},{"buffer":46,"readable-stream":73,"util":1001}],67:[function(require,module,exports){ +module.exports = Array.isArray || function (arr) { + return Object.prototype.toString.call(arr) == '[object Array]'; +}; + +},{}],68:[function(require,module,exports){ (function (process){ // Copyright Joyent, Inc. and other Node contributors. // @@ -11747,1127 +11681,1862 @@ function isSlowBuffer (obj) { // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -// resolves . and .. elements in a path array with directory names there -// must be no slashes, empty elements, or device names (c:\) in the array -// (so also no leading and trailing slashes - it does not distinguish -// relative and absolute paths) -function normalizeArray(parts, allowAboveRoot) { - // if the path tries to go above the root, `up` ends up > 0 - var up = 0; - for (var i = parts.length - 1; i >= 0; i--) { - var last = parts[i]; - if (last === '.') { - parts.splice(i, 1); - } else if (last === '..') { - parts.splice(i, 1); - up++; - } else if (up) { - parts.splice(i, 1); - up--; - } - } +// a duplex stream is just a stream that is both readable and writable. +// Since JS doesn't have multiple prototypal inheritance, this class +// prototypally inherits from Readable, and then parasitically from +// Writable. - // if the path is allowed to go above the root, restore leading ..s - if (allowAboveRoot) { - for (; up--; up) { - parts.unshift('..'); - } - } +module.exports = Duplex; - return parts; +/**/ +var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) keys.push(key); + return keys; +} +/**/ + + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +var Readable = require('./_stream_readable'); +var Writable = require('./_stream_writable'); + +util.inherits(Duplex, Readable); + +forEach(objectKeys(Writable.prototype), function(method) { + if (!Duplex.prototype[method]) + Duplex.prototype[method] = Writable.prototype[method]; +}); + +function Duplex(options) { + if (!(this instanceof Duplex)) + return new Duplex(options); + + Readable.call(this, options); + Writable.call(this, options); + + if (options && options.readable === false) + this.readable = false; + + if (options && options.writable === false) + this.writable = false; + + this.allowHalfOpen = true; + if (options && options.allowHalfOpen === false) + this.allowHalfOpen = false; + + this.once('end', onend); } -// Split a filename into [root, dir, basename, ext], unix version -// 'root' is just a slash, or nothing. -var splitPathRe = - /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; -var splitPath = function(filename) { - return splitPathRe.exec(filename).slice(1); +// the no-half-open enforcer +function onend() { + // if we allow half-open state, or if the writable side ended, + // then we're ok. + if (this.allowHalfOpen || this._writableState.ended) + return; + + // no more data can be written. + // But allow more writes to happen in this tick. + process.nextTick(this.end.bind(this)); +} + +function forEach (xs, f) { + for (var i = 0, l = xs.length; i < l; i++) { + f(xs[i], i); + } +} + +}).call(this,require('_process')) +},{"./_stream_readable":70,"./_stream_writable":72,"_process":923,"core-util-is":78,"inherits":266}],69:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// a passthrough stream. +// basically just the most minimal sort of Transform stream. +// Every written chunk gets output as-is. + +module.exports = PassThrough; + +var Transform = require('./_stream_transform'); + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +util.inherits(PassThrough, Transform); + +function PassThrough(options) { + if (!(this instanceof PassThrough)) + return new PassThrough(options); + + Transform.call(this, options); +} + +PassThrough.prototype._transform = function(chunk, encoding, cb) { + cb(null, chunk); }; -// path.resolve([from ...], to) -// posix version -exports.resolve = function() { - var resolvedPath = '', - resolvedAbsolute = false; +},{"./_stream_transform":71,"core-util-is":78,"inherits":266}],70:[function(require,module,exports){ +(function (process){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. - for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { - var path = (i >= 0) ? arguments[i] : process.cwd(); +module.exports = Readable; - // Skip empty and invalid entries - if (typeof path !== 'string') { - throw new TypeError('Arguments to path.resolve must be strings'); - } else if (!path) { - continue; +/**/ +var isArray = require('isarray'); +/**/ + + +/**/ +var Buffer = require('buffer').Buffer; +/**/ + +Readable.ReadableState = ReadableState; + +var EE = require('events').EventEmitter; + +/**/ +if (!EE.listenerCount) EE.listenerCount = function(emitter, type) { + return emitter.listeners(type).length; +}; +/**/ + +var Stream = require('stream'); + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +var StringDecoder; + +util.inherits(Readable, Stream); + +function ReadableState(options, stream) { + options = options || {}; + + // the point at which it stops calling _read() to fill the buffer + // Note: 0 is a valid value, means "don't call _read preemptively ever" + var hwm = options.highWaterMark; + this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; + + // cast to ints. + this.highWaterMark = ~~this.highWaterMark; + + this.buffer = []; + this.length = 0; + this.pipes = null; + this.pipesCount = 0; + this.flowing = false; + this.ended = false; + this.endEmitted = false; + this.reading = false; + + // In streams that never have any data, and do push(null) right away, + // the consumer can miss the 'end' event if they do some I/O before + // consuming the stream. So, we don't emit('end') until some reading + // happens. + this.calledRead = false; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, becuase any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + + // whenever we return null, then we set a flag to say + // that we're awaiting a 'readable' event emission. + this.needReadable = false; + this.emittedReadable = false; + this.readableListening = false; + + + // object stream flag. Used to make read(n) ignore n and to + // make all the buffer merging and length checks go away + this.objectMode = !!options.objectMode; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // when piping, we only care about 'readable' events that happen + // after read()ing all the bytes and not getting any pushback. + this.ranOut = false; + + // the number of writers that are awaiting a drain event in .pipe()s + this.awaitDrain = 0; + + // if true, a maybeReadMore has been scheduled + this.readingMore = false; + + this.decoder = null; + this.encoding = null; + if (options.encoding) { + if (!StringDecoder) + StringDecoder = require('string_decoder/').StringDecoder; + this.decoder = new StringDecoder(options.encoding); + this.encoding = options.encoding; + } +} + +function Readable(options) { + if (!(this instanceof Readable)) + return new Readable(options); + + this._readableState = new ReadableState(options, this); + + // legacy + this.readable = true; + + Stream.call(this); +} + +// Manually shove something into the read() buffer. +// This returns true if the highWaterMark has not been hit yet, +// similar to how Writable.write() returns true if you should +// write() some more. +Readable.prototype.push = function(chunk, encoding) { + var state = this._readableState; + + if (typeof chunk === 'string' && !state.objectMode) { + encoding = encoding || state.defaultEncoding; + if (encoding !== state.encoding) { + chunk = new Buffer(chunk, encoding); + encoding = ''; } - - resolvedPath = path + '/' + resolvedPath; - resolvedAbsolute = path.charAt(0) === '/'; } - // At this point the path should be resolved to a full absolute path, but - // handle relative paths to be safe (might happen when process.cwd() fails) - - // Normalize the path - resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) { - return !!p; - }), !resolvedAbsolute).join('/'); - - return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; + return readableAddChunk(this, state, chunk, encoding, false); }; -// path.normalize(path) -// posix version -exports.normalize = function(path) { - var isAbsolute = exports.isAbsolute(path), - trailingSlash = substr(path, -1) === '/'; +// Unshift should *always* be something directly out of read() +Readable.prototype.unshift = function(chunk) { + var state = this._readableState; + return readableAddChunk(this, state, chunk, '', true); +}; - // Normalize the path - path = normalizeArray(filter(path.split('/'), function(p) { - return !!p; - }), !isAbsolute).join('/'); +function readableAddChunk(stream, state, chunk, encoding, addToFront) { + var er = chunkInvalid(state, chunk); + if (er) { + stream.emit('error', er); + } else if (chunk === null || chunk === undefined) { + state.reading = false; + if (!state.ended) + onEofChunk(stream, state); + } else if (state.objectMode || chunk && chunk.length > 0) { + if (state.ended && !addToFront) { + var e = new Error('stream.push() after EOF'); + stream.emit('error', e); + } else if (state.endEmitted && addToFront) { + var e = new Error('stream.unshift() after end event'); + stream.emit('error', e); + } else { + if (state.decoder && !addToFront && !encoding) + chunk = state.decoder.write(chunk); - if (!path && !isAbsolute) { - path = '.'; - } - if (path && trailingSlash) { - path += '/'; + // update the buffer info. + state.length += state.objectMode ? 1 : chunk.length; + if (addToFront) { + state.buffer.unshift(chunk); + } else { + state.reading = false; + state.buffer.push(chunk); + } + + if (state.needReadable) + emitReadable(stream); + + maybeReadMore(stream, state); + } + } else if (!addToFront) { + state.reading = false; } - return (isAbsolute ? '/' : '') + path; + return needMoreData(state); +} + + + +// if it's past the high water mark, we can push in some more. +// Also, if we have no data yet, we can stand some +// more bytes. This is to work around cases where hwm=0, +// such as the repl. Also, if the push() triggered a +// readable event, and the user called read(largeNumber) such that +// needReadable was set, then we ought to push more, so that another +// 'readable' event will be triggered. +function needMoreData(state) { + return !state.ended && + (state.needReadable || + state.length < state.highWaterMark || + state.length === 0); +} + +// backwards compatibility. +Readable.prototype.setEncoding = function(enc) { + if (!StringDecoder) + StringDecoder = require('string_decoder/').StringDecoder; + this._readableState.decoder = new StringDecoder(enc); + this._readableState.encoding = enc; }; -// posix version -exports.isAbsolute = function(path) { - return path.charAt(0) === '/'; -}; +// Don't raise the hwm > 128MB +var MAX_HWM = 0x800000; +function roundUpToNextPowerOf2(n) { + if (n >= MAX_HWM) { + n = MAX_HWM; + } else { + // Get the next highest power of 2 + n--; + for (var p = 1; p < 32; p <<= 1) n |= n >> p; + n++; + } + return n; +} -// posix version -exports.join = function() { - var paths = Array.prototype.slice.call(arguments, 0); - return exports.normalize(filter(paths, function(p, index) { - if (typeof p !== 'string') { - throw new TypeError('Arguments to path.join must be strings'); - } - return p; - }).join('/')); -}; +function howMuchToRead(n, state) { + if (state.length === 0 && state.ended) + return 0; + if (state.objectMode) + return n === 0 ? 0 : 1; -// path.relative(from, to) -// posix version -exports.relative = function(from, to) { - from = exports.resolve(from).substr(1); - to = exports.resolve(to).substr(1); - - function trim(arr) { - var start = 0; - for (; start < arr.length; start++) { - if (arr[start] !== '') break; - } - - var end = arr.length - 1; - for (; end >= 0; end--) { - if (arr[end] !== '') break; - } - - if (start > end) return []; - return arr.slice(start, end - start + 1); + if (n === null || isNaN(n)) { + // only flow one buffer at a time + if (state.flowing && state.buffer.length) + return state.buffer[0].length; + else + return state.length; } - var fromParts = trim(from.split('/')); - var toParts = trim(to.split('/')); + if (n <= 0) + return 0; - var length = Math.min(fromParts.length, toParts.length); - var samePartsLength = length; - for (var i = 0; i < length; i++) { - if (fromParts[i] !== toParts[i]) { - samePartsLength = i; + // If we're asking for more than the target buffer level, + // then raise the water mark. Bump up to the next highest + // power of 2, to prevent increasing it excessively in tiny + // amounts. + if (n > state.highWaterMark) + state.highWaterMark = roundUpToNextPowerOf2(n); + + // don't have that much. return null, unless we've ended. + if (n > state.length) { + if (!state.ended) { + state.needReadable = true; + return 0; + } else + return state.length; + } + + return n; +} + +// you can override either this method, or the async _read(n) below. +Readable.prototype.read = function(n) { + var state = this._readableState; + state.calledRead = true; + var nOrig = n; + var ret; + + if (typeof n !== 'number' || n > 0) + state.emittedReadable = false; + + // if we're doing read(0) to trigger a readable event, but we + // already have a bunch of data in the buffer, then just trigger + // the 'readable' event and move on. + if (n === 0 && + state.needReadable && + (state.length >= state.highWaterMark || state.ended)) { + emitReadable(this); + return null; + } + + n = howMuchToRead(n, state); + + // if we've ended, and we're now clear, then finish it up. + if (n === 0 && state.ended) { + ret = null; + + // In cases where the decoder did not receive enough data + // to produce a full chunk, then immediately received an + // EOF, state.buffer will contain [, ]. + // howMuchToRead will see this and coerce the amount to + // read to zero (because it's looking at the length of the + // first in state.buffer), and we'll end up here. + // + // This can only happen via state.decoder -- no other venue + // exists for pushing a zero-length chunk into state.buffer + // and triggering this behavior. In this case, we return our + // remaining data and end the stream, if appropriate. + if (state.length > 0 && state.decoder) { + ret = fromList(n, state); + state.length -= ret.length; + } + + if (state.length === 0) + endReadable(this); + + return ret; + } + + // All the actual chunk generation logic needs to be + // *below* the call to _read. The reason is that in certain + // synthetic stream cases, such as passthrough streams, _read + // may be a completely synchronous operation which may change + // the state of the read buffer, providing enough data when + // before there was *not* enough. + // + // So, the steps are: + // 1. Figure out what the state of things will be after we do + // a read from the buffer. + // + // 2. If that resulting state will trigger a _read, then call _read. + // Note that this may be asynchronous, or synchronous. Yes, it is + // deeply ugly to write APIs this way, but that still doesn't mean + // that the Readable class should behave improperly, as streams are + // designed to be sync/async agnostic. + // Take note if the _read call is sync or async (ie, if the read call + // has returned yet), so that we know whether or not it's safe to emit + // 'readable' etc. + // + // 3. Actually pull the requested chunks out of the buffer and return. + + // if we need a readable event, then we need to do some reading. + var doRead = state.needReadable; + + // if we currently have less than the highWaterMark, then also read some + if (state.length - n <= state.highWaterMark) + doRead = true; + + // however, if we've ended, then there's no point, and if we're already + // reading, then it's unnecessary. + if (state.ended || state.reading) + doRead = false; + + if (doRead) { + state.reading = true; + state.sync = true; + // if the length is currently zero, then we *need* a readable event. + if (state.length === 0) + state.needReadable = true; + // call internal read method + this._read(state.highWaterMark); + state.sync = false; + } + + // If _read called its callback synchronously, then `reading` + // will be false, and we need to re-evaluate how much data we + // can return to the user. + if (doRead && !state.reading) + n = howMuchToRead(nOrig, state); + + if (n > 0) + ret = fromList(n, state); + else + ret = null; + + if (ret === null) { + state.needReadable = true; + n = 0; + } + + state.length -= n; + + // If we have nothing in the buffer, then we want to know + // as soon as we *do* get something into the buffer. + if (state.length === 0 && !state.ended) + state.needReadable = true; + + // If we happened to read() exactly the remaining amount in the + // buffer, and the EOF has been seen at this point, then make sure + // that we emit 'end' on the very next tick. + if (state.ended && !state.endEmitted && state.length === 0) + endReadable(this); + + return ret; +}; + +function chunkInvalid(state, chunk) { + var er = null; + if (!Buffer.isBuffer(chunk) && + 'string' !== typeof chunk && + chunk !== null && + chunk !== undefined && + !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + return er; +} + + +function onEofChunk(stream, state) { + if (state.decoder && !state.ended) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) { + state.buffer.push(chunk); + state.length += state.objectMode ? 1 : chunk.length; + } + } + state.ended = true; + + // if we've ended and we have some data left, then emit + // 'readable' now to make sure it gets picked up. + if (state.length > 0) + emitReadable(stream); + else + endReadable(stream); +} + +// Don't emit readable right away in sync mode, because this can trigger +// another read() call => stack overflow. This way, it might trigger +// a nextTick recursion warning, but that's not so bad. +function emitReadable(stream) { + var state = stream._readableState; + state.needReadable = false; + if (state.emittedReadable) + return; + + state.emittedReadable = true; + if (state.sync) + process.nextTick(function() { + emitReadable_(stream); + }); + else + emitReadable_(stream); +} + +function emitReadable_(stream) { + stream.emit('readable'); +} + + +// at this point, the user has presumably seen the 'readable' event, +// and called read() to consume some data. that may have triggered +// in turn another _read(n) call, in which case reading = true if +// it's in progress. +// However, if we're not ended, or reading, and the length < hwm, +// then go ahead and try to read some more preemptively. +function maybeReadMore(stream, state) { + if (!state.readingMore) { + state.readingMore = true; + process.nextTick(function() { + maybeReadMore_(stream, state); + }); + } +} + +function maybeReadMore_(stream, state) { + var len = state.length; + while (!state.reading && !state.flowing && !state.ended && + state.length < state.highWaterMark) { + stream.read(0); + if (len === state.length) + // didn't get any data, stop spinning. + break; + else + len = state.length; + } + state.readingMore = false; +} + +// abstract method. to be overridden in specific implementation classes. +// call cb(er, data) where data is <= n in length. +// for virtual (non-string, non-buffer) streams, "length" is somewhat +// arbitrary, and perhaps not very meaningful. +Readable.prototype._read = function(n) { + this.emit('error', new Error('not implemented')); +}; + +Readable.prototype.pipe = function(dest, pipeOpts) { + var src = this; + var state = this._readableState; + + switch (state.pipesCount) { + case 0: + state.pipes = dest; + break; + case 1: + state.pipes = [state.pipes, dest]; + break; + default: + state.pipes.push(dest); + break; + } + state.pipesCount += 1; + + var doEnd = (!pipeOpts || pipeOpts.end !== false) && + dest !== process.stdout && + dest !== process.stderr; + + var endFn = doEnd ? onend : cleanup; + if (state.endEmitted) + process.nextTick(endFn); + else + src.once('end', endFn); + + dest.on('unpipe', onunpipe); + function onunpipe(readable) { + if (readable !== src) return; + cleanup(); + } + + function onend() { + dest.end(); + } + + // when the dest drains, it reduces the awaitDrain counter + // on the source. This would be more elegant with a .once() + // handler in flow(), but adding and removing repeatedly is + // too slow. + var ondrain = pipeOnDrain(src); + dest.on('drain', ondrain); + + function cleanup() { + // cleanup event handlers once the pipe is broken + dest.removeListener('close', onclose); + dest.removeListener('finish', onfinish); + dest.removeListener('drain', ondrain); + dest.removeListener('error', onerror); + dest.removeListener('unpipe', onunpipe); + src.removeListener('end', onend); + src.removeListener('end', cleanup); + + // if the reader is waiting for a drain event from this + // specific writer, then it would cause it to never start + // flowing again. + // So, if this is awaiting a drain, then we just call it now. + // If we don't know, then assume that we are waiting for one. + if (!dest._writableState || dest._writableState.needDrain) + ondrain(); + } + + // if the dest has an error, then stop piping into it. + // however, don't suppress the throwing behavior for this. + function onerror(er) { + unpipe(); + dest.removeListener('error', onerror); + if (EE.listenerCount(dest, 'error') === 0) + dest.emit('error', er); + } + // This is a brutally ugly hack to make sure that our error handler + // is attached before any userland ones. NEVER DO THIS. + if (!dest._events || !dest._events.error) + dest.on('error', onerror); + else if (isArray(dest._events.error)) + dest._events.error.unshift(onerror); + else + dest._events.error = [onerror, dest._events.error]; + + + + // Both close and finish should trigger unpipe, but only once. + function onclose() { + dest.removeListener('finish', onfinish); + unpipe(); + } + dest.once('close', onclose); + function onfinish() { + dest.removeListener('close', onclose); + unpipe(); + } + dest.once('finish', onfinish); + + function unpipe() { + src.unpipe(dest); + } + + // tell the dest that it's being piped to + dest.emit('pipe', src); + + // start the flow if it hasn't been started already. + if (!state.flowing) { + // the handler that waits for readable events after all + // the data gets sucked out in flow. + // This would be easier to follow with a .once() handler + // in flow(), but that is too slow. + this.on('readable', pipeOnReadable); + + state.flowing = true; + process.nextTick(function() { + flow(src); + }); + } + + return dest; +}; + +function pipeOnDrain(src) { + return function() { + var dest = this; + var state = src._readableState; + state.awaitDrain--; + if (state.awaitDrain === 0) + flow(src); + }; +} + +function flow(src) { + var state = src._readableState; + var chunk; + state.awaitDrain = 0; + + function write(dest, i, list) { + var written = dest.write(chunk); + if (false === written) { + state.awaitDrain++; + } + } + + while (state.pipesCount && null !== (chunk = src.read())) { + + if (state.pipesCount === 1) + write(state.pipes, 0, null); + else + forEach(state.pipes, write); + + src.emit('data', chunk); + + // if anyone needs a drain, then we have to wait for that. + if (state.awaitDrain > 0) + return; + } + + // if every destination was unpiped, either before entering this + // function, or in the while loop, then stop flowing. + // + // NB: This is a pretty rare edge case. + if (state.pipesCount === 0) { + state.flowing = false; + + // if there were data event listeners added, then switch to old mode. + if (EE.listenerCount(src, 'data') > 0) + emitDataEvents(src); + return; + } + + // at this point, no one needed a drain, so we just ran out of data + // on the next readable event, start it over again. + state.ranOut = true; +} + +function pipeOnReadable() { + if (this._readableState.ranOut) { + this._readableState.ranOut = false; + flow(this); + } +} + + +Readable.prototype.unpipe = function(dest) { + var state = this._readableState; + + // if we're not piping anywhere, then do nothing. + if (state.pipesCount === 0) + return this; + + // just one destination. most common case. + if (state.pipesCount === 1) { + // passed in one, but it's not the right one. + if (dest && dest !== state.pipes) + return this; + + if (!dest) + dest = state.pipes; + + // got a match. + state.pipes = null; + state.pipesCount = 0; + this.removeListener('readable', pipeOnReadable); + state.flowing = false; + if (dest) + dest.emit('unpipe', this); + return this; + } + + // slow case. multiple pipe destinations. + + if (!dest) { + // remove all. + var dests = state.pipes; + var len = state.pipesCount; + state.pipes = null; + state.pipesCount = 0; + this.removeListener('readable', pipeOnReadable); + state.flowing = false; + + for (var i = 0; i < len; i++) + dests[i].emit('unpipe', this); + return this; + } + + // try to find the right one. + var i = indexOf(state.pipes, dest); + if (i === -1) + return this; + + state.pipes.splice(i, 1); + state.pipesCount -= 1; + if (state.pipesCount === 1) + state.pipes = state.pipes[0]; + + dest.emit('unpipe', this); + + return this; +}; + +// set up data events if they are asked for +// Ensure readable listeners eventually get something +Readable.prototype.on = function(ev, fn) { + var res = Stream.prototype.on.call(this, ev, fn); + + if (ev === 'data' && !this._readableState.flowing) + emitDataEvents(this); + + if (ev === 'readable' && this.readable) { + var state = this._readableState; + if (!state.readableListening) { + state.readableListening = true; + state.emittedReadable = false; + state.needReadable = true; + if (!state.reading) { + this.read(0); + } else if (state.length) { + emitReadable(this, state); + } + } + } + + return res; +}; +Readable.prototype.addListener = Readable.prototype.on; + +// pause() and resume() are remnants of the legacy readable stream API +// If the user uses them, then switch into old mode. +Readable.prototype.resume = function() { + emitDataEvents(this); + this.read(0); + this.emit('resume'); +}; + +Readable.prototype.pause = function() { + emitDataEvents(this, true); + this.emit('pause'); +}; + +function emitDataEvents(stream, startPaused) { + var state = stream._readableState; + + if (state.flowing) { + // https://github.com/isaacs/readable-stream/issues/16 + throw new Error('Cannot switch to old mode now.'); + } + + var paused = startPaused || false; + var readable = false; + + // convert to an old-style stream. + stream.readable = true; + stream.pipe = Stream.prototype.pipe; + stream.on = stream.addListener = Stream.prototype.on; + + stream.on('readable', function() { + readable = true; + + var c; + while (!paused && (null !== (c = stream.read()))) + stream.emit('data', c); + + if (c === null) { + readable = false; + stream._readableState.needReadable = true; + } + }); + + stream.pause = function() { + paused = true; + this.emit('pause'); + }; + + stream.resume = function() { + paused = false; + if (readable) + process.nextTick(function() { + stream.emit('readable'); + }); + else + this.read(0); + this.emit('resume'); + }; + + // now make it start, just in case it hadn't already. + stream.emit('readable'); +} + +// wrap an old-style stream as the async data source. +// This is *not* part of the readable stream interface. +// It is an ugly unfortunate mess of history. +Readable.prototype.wrap = function(stream) { + var state = this._readableState; + var paused = false; + + var self = this; + stream.on('end', function() { + if (state.decoder && !state.ended) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) + self.push(chunk); + } + + self.push(null); + }); + + stream.on('data', function(chunk) { + if (state.decoder) + chunk = state.decoder.write(chunk); + + // don't skip over falsy values in objectMode + //if (state.objectMode && util.isNullOrUndefined(chunk)) + if (state.objectMode && (chunk === null || chunk === undefined)) + return; + else if (!state.objectMode && (!chunk || !chunk.length)) + return; + + var ret = self.push(chunk); + if (!ret) { + paused = true; + stream.pause(); + } + }); + + // proxy all the other methods. + // important when wrapping filters and duplexes. + for (var i in stream) { + if (typeof stream[i] === 'function' && + typeof this[i] === 'undefined') { + this[i] = function(method) { return function() { + return stream[method].apply(stream, arguments); + }}(i); + } + } + + // proxy certain important events. + var events = ['error', 'close', 'destroy', 'pause', 'resume']; + forEach(events, function(ev) { + stream.on(ev, self.emit.bind(self, ev)); + }); + + // when we try to consume some more bytes, simply unpause the + // underlying stream. + self._read = function(n) { + if (paused) { + paused = false; + stream.resume(); + } + }; + + return self; +}; + + + +// exposed for testing purposes only. +Readable._fromList = fromList; + +// Pluck off n bytes from an array of buffers. +// Length is the combined lengths of all the buffers in the list. +function fromList(n, state) { + var list = state.buffer; + var length = state.length; + var stringMode = !!state.decoder; + var objectMode = !!state.objectMode; + var ret; + + // nothing in the list, definitely empty. + if (list.length === 0) + return null; + + if (length === 0) + ret = null; + else if (objectMode) + ret = list.shift(); + else if (!n || n >= length) { + // read it all, truncate the array. + if (stringMode) + ret = list.join(''); + else + ret = Buffer.concat(list, length); + list.length = 0; + } else { + // read just some of it. + if (n < list[0].length) { + // just take a part of the first list item. + // slice is the same for buffers and strings. + var buf = list[0]; + ret = buf.slice(0, n); + list[0] = buf.slice(n); + } else if (n === list[0].length) { + // first list is a perfect match + ret = list.shift(); + } else { + // complex case. + // we have enough to cover it, but it spans past the first buffer. + if (stringMode) + ret = ''; + else + ret = new Buffer(n); + + var c = 0; + for (var i = 0, l = list.length; i < l && c < n; i++) { + var buf = list[0]; + var cpy = Math.min(n - c, buf.length); + + if (stringMode) + ret += buf.slice(0, cpy); + else + buf.copy(ret, c, 0, cpy); + + if (cpy < buf.length) + list[0] = buf.slice(cpy); + else + list.shift(); + + c += cpy; + } + } + } + + return ret; +} + +function endReadable(stream) { + var state = stream._readableState; + + // If we get here before consuming all the bytes, then that is a + // bug in node. Should never happen. + if (state.length > 0) + throw new Error('endReadable called on non-empty stream'); + + if (!state.endEmitted && state.calledRead) { + state.ended = true; + process.nextTick(function() { + // Check that we didn't get one last unshift. + if (!state.endEmitted && state.length === 0) { + state.endEmitted = true; + stream.readable = false; + stream.emit('end'); + } + }); + } +} + +function forEach (xs, f) { + for (var i = 0, l = xs.length; i < l; i++) { + f(xs[i], i); + } +} + +function indexOf (xs, x) { + for (var i = 0, l = xs.length; i < l; i++) { + if (xs[i] === x) return i; + } + return -1; +} + +}).call(this,require('_process')) +},{"_process":923,"buffer":46,"core-util-is":78,"events":95,"inherits":266,"isarray":67,"stream":978,"string_decoder/":979}],71:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + +// a transform stream is a readable/writable stream where you do +// something with the data. Sometimes it's called a "filter", +// but that's not a great name for it, since that implies a thing where +// some bits pass through, and others are simply ignored. (That would +// be a valid example of a transform, of course.) +// +// While the output is causally related to the input, it's not a +// necessarily symmetric or synchronous transformation. For example, +// a zlib stream might take multiple plain-text writes(), and then +// emit a single compressed chunk some time in the future. +// +// Here's how this works: +// +// The Transform stream has all the aspects of the readable and writable +// stream classes. When you write(chunk), that calls _write(chunk,cb) +// internally, and returns false if there's a lot of pending writes +// buffered up. When you call read(), that calls _read(n) until +// there's enough pending readable data buffered up. +// +// In a transform stream, the written data is placed in a buffer. When +// _read(n) is called, it transforms the queued up data, calling the +// buffered _write cb's as it consumes chunks. If consuming a single +// written chunk would result in multiple output chunks, then the first +// outputted bit calls the readcb, and subsequent chunks just go into +// the read buffer, and will cause it to emit 'readable' if necessary. +// +// This way, back-pressure is actually determined by the reading side, +// since _read has to be called to start processing a new chunk. However, +// a pathological inflate type of transform can cause excessive buffering +// here. For example, imagine a stream where every byte of input is +// interpreted as an integer from 0-255, and then results in that many +// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in +// 1kb of data being output. In this case, you could write a very small +// amount of input, and end up with a very large amount of output. In +// such a pathological inflating mechanism, there'd be no way to tell +// the system to stop doing the transform. A single 4MB write could +// cause the system to run out of memory. +// +// However, even in such a pathological case, only a single written chunk +// would be consumed, and then the rest would wait (un-transformed) until +// the results of the previous transformed chunk were consumed. + +module.exports = Transform; + +var Duplex = require('./_stream_duplex'); + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +util.inherits(Transform, Duplex); + + +function TransformState(options, stream) { + this.afterTransform = function(er, data) { + return afterTransform(stream, er, data); + }; + + this.needTransform = false; + this.transforming = false; + this.writecb = null; + this.writechunk = null; +} + +function afterTransform(stream, er, data) { + var ts = stream._transformState; + ts.transforming = false; + + var cb = ts.writecb; + + if (!cb) + return stream.emit('error', new Error('no writecb in Transform class')); + + ts.writechunk = null; + ts.writecb = null; + + if (data !== null && data !== undefined) + stream.push(data); + + if (cb) + cb(er); + + var rs = stream._readableState; + rs.reading = false; + if (rs.needReadable || rs.length < rs.highWaterMark) { + stream._read(rs.highWaterMark); + } +} + + +function Transform(options) { + if (!(this instanceof Transform)) + return new Transform(options); + + Duplex.call(this, options); + + var ts = this._transformState = new TransformState(options, this); + + // when the writable side finishes, then flush out anything remaining. + var stream = this; + + // start out asking for a readable event once data is transformed. + this._readableState.needReadable = true; + + // we have implemented the _read method, and done the other things + // that Readable wants before the first _read call, so unset the + // sync guard flag. + this._readableState.sync = false; + + this.once('finish', function() { + if ('function' === typeof this._flush) + this._flush(function(er) { + done(stream, er); + }); + else + done(stream); + }); +} + +Transform.prototype.push = function(chunk, encoding) { + this._transformState.needTransform = false; + return Duplex.prototype.push.call(this, chunk, encoding); +}; + +// This is the part where you do stuff! +// override this function in implementation classes. +// 'chunk' is an input chunk. +// +// Call `push(newChunk)` to pass along transformed output +// to the readable side. You may call 'push' zero or more times. +// +// Call `cb(err)` when you are done with this chunk. If you pass +// an error, then that'll put the hurt on the whole operation. If you +// never call cb(), then you'll never get another chunk. +Transform.prototype._transform = function(chunk, encoding, cb) { + throw new Error('not implemented'); +}; + +Transform.prototype._write = function(chunk, encoding, cb) { + var ts = this._transformState; + ts.writecb = cb; + ts.writechunk = chunk; + ts.writeencoding = encoding; + if (!ts.transforming) { + var rs = this._readableState; + if (ts.needTransform || + rs.needReadable || + rs.length < rs.highWaterMark) + this._read(rs.highWaterMark); + } +}; + +// Doesn't matter what the args are here. +// _transform does all the work. +// That we got here means that the readable side wants more data. +Transform.prototype._read = function(n) { + var ts = this._transformState; + + if (ts.writechunk !== null && ts.writecb && !ts.transforming) { + ts.transforming = true; + this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); + } else { + // mark that we need a transform, so that any data that comes in + // will get processed, now that we've asked for it. + ts.needTransform = true; + } +}; + + +function done(stream, er) { + if (er) + return stream.emit('error', er); + + // if there's nothing in the write buffer, then that means + // that nothing more will ever be provided + var ws = stream._writableState; + var rs = stream._readableState; + var ts = stream._transformState; + + if (ws.length) + throw new Error('calling transform done when ws.length != 0'); + + if (ts.transforming) + throw new Error('calling transform done when still transforming'); + + return stream.push(null); +} + +},{"./_stream_duplex":68,"core-util-is":78,"inherits":266}],72:[function(require,module,exports){ +(function (process){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// A bit simpler than readable streams. +// Implement an async ._write(chunk, cb), and it'll handle all +// the drain event emission and buffering. + +module.exports = Writable; + +/**/ +var Buffer = require('buffer').Buffer; +/**/ + +Writable.WritableState = WritableState; + + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +var Stream = require('stream'); + +util.inherits(Writable, Stream); + +function WriteReq(chunk, encoding, cb) { + this.chunk = chunk; + this.encoding = encoding; + this.callback = cb; +} + +function WritableState(options, stream) { + options = options || {}; + + // the point at which write() starts returning false + // Note: 0 is a valid value, means that we always return false if + // the entire buffer is not flushed immediately on write() + var hwm = options.highWaterMark; + this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; + + // object stream flag to indicate whether or not this stream + // contains buffers or objects. + this.objectMode = !!options.objectMode; + + // cast to ints. + this.highWaterMark = ~~this.highWaterMark; + + this.needDrain = false; + // at the start of calling end() + this.ending = false; + // when end() has been called, and returned + this.ended = false; + // when 'finish' is emitted + this.finished = false; + + // should we decode strings into buffers before passing to _write? + // this is here so that some node-core streams can optimize string + // handling at a lower level. + var noDecode = options.decodeStrings === false; + this.decodeStrings = !noDecode; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // not an actual buffer we keep track of, but a measurement + // of how much we're waiting to get pushed to some underlying + // socket or file. + this.length = 0; + + // a flag to see when we're in the middle of a write. + this.writing = false; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, becuase any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + + // a flag to know if we're processing previously buffered items, which + // may call the _write() callback in the same tick, so that we don't + // end up in an overlapped onwrite situation. + this.bufferProcessing = false; + + // the callback that's passed to _write(chunk,cb) + this.onwrite = function(er) { + onwrite(stream, er); + }; + + // the callback that the user supplies to write(chunk,encoding,cb) + this.writecb = null; + + // the amount that is being written when _write is called. + this.writelen = 0; + + this.buffer = []; + + // True if the error was already emitted and should not be thrown again + this.errorEmitted = false; +} + +function Writable(options) { + var Duplex = require('./_stream_duplex'); + + // Writable ctor is applied to Duplexes, though they're not + // instanceof Writable, they're instanceof Readable. + if (!(this instanceof Writable) && !(this instanceof Duplex)) + return new Writable(options); + + this._writableState = new WritableState(options, this); + + // legacy. + this.writable = true; + + Stream.call(this); +} + +// Otherwise people can pipe Writable streams, which is just wrong. +Writable.prototype.pipe = function() { + this.emit('error', new Error('Cannot pipe. Not readable.')); +}; + + +function writeAfterEnd(stream, state, cb) { + var er = new Error('write after end'); + // TODO: defer error events consistently everywhere, not just the cb + stream.emit('error', er); + process.nextTick(function() { + cb(er); + }); +} + +// If we get something that is not a buffer, string, null, or undefined, +// and we're not in objectMode, then that's an error. +// Otherwise stream chunks are all considered to be of length=1, and the +// watermarks determine how many objects to keep in the buffer, rather than +// how many bytes or characters. +function validChunk(stream, state, chunk, cb) { + var valid = true; + if (!Buffer.isBuffer(chunk) && + 'string' !== typeof chunk && + chunk !== null && + chunk !== undefined && + !state.objectMode) { + var er = new TypeError('Invalid non-string/buffer chunk'); + stream.emit('error', er); + process.nextTick(function() { + cb(er); + }); + valid = false; + } + return valid; +} + +Writable.prototype.write = function(chunk, encoding, cb) { + var state = this._writableState; + var ret = false; + + if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (Buffer.isBuffer(chunk)) + encoding = 'buffer'; + else if (!encoding) + encoding = state.defaultEncoding; + + if (typeof cb !== 'function') + cb = function() {}; + + if (state.ended) + writeAfterEnd(this, state, cb); + else if (validChunk(this, state, chunk, cb)) + ret = writeOrBuffer(this, state, chunk, encoding, cb); + + return ret; +}; + +function decodeChunk(state, chunk, encoding) { + if (!state.objectMode && + state.decodeStrings !== false && + typeof chunk === 'string') { + chunk = new Buffer(chunk, encoding); + } + return chunk; +} + +// if we're already writing something, then just put this +// in the queue, and wait our turn. Otherwise, call _write +// If we return false, then we need a drain event, so set that flag. +function writeOrBuffer(stream, state, chunk, encoding, cb) { + chunk = decodeChunk(state, chunk, encoding); + if (Buffer.isBuffer(chunk)) + encoding = 'buffer'; + var len = state.objectMode ? 1 : chunk.length; + + state.length += len; + + var ret = state.length < state.highWaterMark; + // we must ensure that previous needDrain will not be reset to false. + if (!ret) + state.needDrain = true; + + if (state.writing) + state.buffer.push(new WriteReq(chunk, encoding, cb)); + else + doWrite(stream, state, len, chunk, encoding, cb); + + return ret; +} + +function doWrite(stream, state, len, chunk, encoding, cb) { + state.writelen = len; + state.writecb = cb; + state.writing = true; + state.sync = true; + stream._write(chunk, encoding, state.onwrite); + state.sync = false; +} + +function onwriteError(stream, state, sync, er, cb) { + if (sync) + process.nextTick(function() { + cb(er); + }); + else + cb(er); + + stream._writableState.errorEmitted = true; + stream.emit('error', er); +} + +function onwriteStateUpdate(state) { + state.writing = false; + state.writecb = null; + state.length -= state.writelen; + state.writelen = 0; +} + +function onwrite(stream, er) { + var state = stream._writableState; + var sync = state.sync; + var cb = state.writecb; + + onwriteStateUpdate(state); + + if (er) + onwriteError(stream, state, sync, er, cb); + else { + // Check if we're actually ready to finish, but don't emit yet + var finished = needFinish(stream, state); + + if (!finished && !state.bufferProcessing && state.buffer.length) + clearBuffer(stream, state); + + if (sync) { + process.nextTick(function() { + afterWrite(stream, state, finished, cb); + }); + } else { + afterWrite(stream, state, finished, cb); + } + } +} + +function afterWrite(stream, state, finished, cb) { + if (!finished) + onwriteDrain(stream, state); + cb(); + if (finished) + finishMaybe(stream, state); +} + +// Must force callback to be called on nextTick, so that we don't +// emit 'drain' before the write() consumer gets the 'false' return +// value, and has a chance to attach a 'drain' listener. +function onwriteDrain(stream, state) { + if (state.length === 0 && state.needDrain) { + state.needDrain = false; + stream.emit('drain'); + } +} + + +// if there's something in the buffer waiting, then process it +function clearBuffer(stream, state) { + state.bufferProcessing = true; + + for (var c = 0; c < state.buffer.length; c++) { + var entry = state.buffer[c]; + var chunk = entry.chunk; + var encoding = entry.encoding; + var cb = entry.callback; + var len = state.objectMode ? 1 : chunk.length; + + doWrite(stream, state, len, chunk, encoding, cb); + + // if we didn't call the onwrite immediately, then + // it means that we need to wait until it does. + // also, that means that the chunk and cb are currently + // being processed, so move the buffer counter past them. + if (state.writing) { + c++; break; } } - var outputParts = []; - for (var i = samePartsLength; i < fromParts.length; i++) { - outputParts.push('..'); - } - - outputParts = outputParts.concat(toParts.slice(samePartsLength)); - - return outputParts.join('/'); -}; - -exports.sep = '/'; -exports.delimiter = ':'; - -exports.dirname = function(path) { - var result = splitPath(path), - root = result[0], - dir = result[1]; - - if (!root && !dir) { - // No dirname whatsoever - return '.'; - } - - if (dir) { - // It has a dirname, strip trailing slash - dir = dir.substr(0, dir.length - 1); - } - - return root + dir; -}; - - -exports.basename = function(path, ext) { - var f = splitPath(path)[2]; - // TODO: make this comparison case-insensitive on windows? - if (ext && f.substr(-1 * ext.length) === ext) { - f = f.substr(0, f.length - ext.length); - } - return f; -}; - - -exports.extname = function(path) { - return splitPath(path)[3]; -}; - -function filter (xs, f) { - if (xs.filter) return xs.filter(f); - var res = []; - for (var i = 0; i < xs.length; i++) { - if (f(xs[i], i, xs)) res.push(xs[i]); - } - return res; + state.bufferProcessing = false; + if (c < state.buffer.length) + state.buffer = state.buffer.slice(c); + else + state.buffer.length = 0; } -// String.prototype.substr - negative index don't work in IE8 -var substr = 'ab'.substr(-1) === 'b' - ? function (str, start, len) { return str.substr(start, len) } - : function (str, start, len) { - if (start < 0) start = str.length + start; - return str.substr(start, len); - } -; +Writable.prototype._write = function(chunk, encoding, cb) { + cb(new Error('not implemented')); +}; + +Writable.prototype.end = function(chunk, encoding, cb) { + var state = this._writableState; + + if (typeof chunk === 'function') { + cb = chunk; + chunk = null; + encoding = null; + } else if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (typeof chunk !== 'undefined' && chunk !== null) + this.write(chunk, encoding); + + // ignore unnecessary end() calls. + if (!state.ending && !state.finished) + endWritable(this, state, cb); +}; + + +function needFinish(stream, state) { + return (state.ending && + state.length === 0 && + !state.finished && + !state.writing); +} + +function finishMaybe(stream, state) { + var need = needFinish(stream, state); + if (need) { + state.finished = true; + stream.emit('finish'); + } + return need; +} + +function endWritable(stream, state, cb) { + state.ending = true; + finishMaybe(stream, state); + if (cb) { + if (state.finished) + process.nextTick(cb); + else + stream.once('finish', cb); + } + state.ended = true; +} }).call(this,require('_process')) -},{"_process":41}],41:[function(require,module,exports){ -// shim for using process in browser -var process = module.exports = {}; - -// cached from whatever global is present so that test runners that stub it -// don't break things. But we need to wrap it in a try catch in case it is -// wrapped in strict mode code which doesn't define any globals. It's inside a -// function because try/catches deoptimize in certain engines. - -var cachedSetTimeout; -var cachedClearTimeout; - -function defaultSetTimout() { - throw new Error('setTimeout has not been defined'); -} -function defaultClearTimeout () { - throw new Error('clearTimeout has not been defined'); -} -(function () { - try { - if (typeof setTimeout === 'function') { - cachedSetTimeout = setTimeout; - } else { - cachedSetTimeout = defaultSetTimout; - } - } catch (e) { - cachedSetTimeout = defaultSetTimout; - } - try { - if (typeof clearTimeout === 'function') { - cachedClearTimeout = clearTimeout; - } else { - cachedClearTimeout = defaultClearTimeout; - } - } catch (e) { - cachedClearTimeout = defaultClearTimeout; - } -} ()) -function runTimeout(fun) { - if (cachedSetTimeout === setTimeout) { - //normal enviroments in sane situations - return setTimeout(fun, 0); - } - // if setTimeout wasn't available but was latter defined - if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { - cachedSetTimeout = setTimeout; - return setTimeout(fun, 0); - } - try { - // when when somebody has screwed with setTimeout but no I.E. maddness - return cachedSetTimeout(fun, 0); - } catch(e){ - try { - // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally - return cachedSetTimeout.call(null, fun, 0); - } catch(e){ - // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error - return cachedSetTimeout.call(this, fun, 0); - } - } - - -} -function runClearTimeout(marker) { - if (cachedClearTimeout === clearTimeout) { - //normal enviroments in sane situations - return clearTimeout(marker); - } - // if clearTimeout wasn't available but was latter defined - if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { - cachedClearTimeout = clearTimeout; - return clearTimeout(marker); - } - try { - // when when somebody has screwed with setTimeout but no I.E. maddness - return cachedClearTimeout(marker); - } catch (e){ - try { - // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally - return cachedClearTimeout.call(null, marker); - } catch (e){ - // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. - // Some versions of I.E. have different rules for clearTimeout vs setTimeout - return cachedClearTimeout.call(this, marker); - } - } - - - -} -var queue = []; -var draining = false; -var currentQueue; -var queueIndex = -1; - -function cleanUpNextTick() { - if (!draining || !currentQueue) { - return; - } - draining = false; - if (currentQueue.length) { - queue = currentQueue.concat(queue); - } else { - queueIndex = -1; - } - if (queue.length) { - drainQueue(); - } +},{"./_stream_duplex":68,"_process":923,"buffer":46,"core-util-is":78,"inherits":266,"stream":978}],73:[function(require,module,exports){ +(function (process){ +var Stream = require('stream'); // hack to fix a circular dependency issue when used with browserify +exports = module.exports = require('./lib/_stream_readable.js'); +exports.Stream = Stream; +exports.Readable = exports; +exports.Writable = require('./lib/_stream_writable.js'); +exports.Duplex = require('./lib/_stream_duplex.js'); +exports.Transform = require('./lib/_stream_transform.js'); +exports.PassThrough = require('./lib/_stream_passthrough.js'); +if (!process.browser && process.env.READABLE_STREAM === 'disable') { + module.exports = require('stream'); } -function drainQueue() { - if (draining) { - return; - } - var timeout = runTimeout(cleanUpNextTick); - draining = true; - - var len = queue.length; - while(len) { - currentQueue = queue; - queue = []; - while (++queueIndex < len) { - if (currentQueue) { - currentQueue[queueIndex].run(); - } - } - queueIndex = -1; - len = queue.length; - } - currentQueue = null; - draining = false; - runClearTimeout(timeout); -} - -process.nextTick = function (fun) { - var args = new Array(arguments.length - 1); - if (arguments.length > 1) { - for (var i = 1; i < arguments.length; i++) { - args[i - 1] = arguments[i]; - } - } - queue.push(new Item(fun, args)); - if (queue.length === 1 && !draining) { - runTimeout(drainQueue); - } -}; - -// v8 likes predictible objects -function Item(fun, array) { - this.fun = fun; - this.array = array; -} -Item.prototype.run = function () { - this.fun.apply(null, this.array); -}; -process.title = 'browser'; -process.browser = true; -process.env = {}; -process.argv = []; -process.version = ''; // empty string to avoid regexp issues -process.versions = {}; - -function noop() {} - -process.on = noop; -process.addListener = noop; -process.once = noop; -process.off = noop; -process.removeListener = noop; -process.removeAllListeners = noop; -process.emit = noop; - -process.binding = function (name) { - throw new Error('process.binding is not supported'); -}; - -process.cwd = function () { return '/' }; -process.chdir = function (dir) { - throw new Error('process.chdir is not supported'); -}; -process.umask = function() { return 0; }; - -},{}],42:[function(require,module,exports){ -(function (global){ -/*! https://mths.be/punycode v1.4.1 by @mathias */ -;(function(root) { - - /** Detect free variables */ - var freeExports = typeof exports == 'object' && exports && - !exports.nodeType && exports; - var freeModule = typeof module == 'object' && module && - !module.nodeType && module; - var freeGlobal = typeof global == 'object' && global; - if ( - freeGlobal.global === freeGlobal || - freeGlobal.window === freeGlobal || - freeGlobal.self === freeGlobal - ) { - root = freeGlobal; - } - - /** - * The `punycode` object. - * @name punycode - * @type Object - */ - var punycode, - - /** Highest positive signed 32-bit float value */ - maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1 - - /** Bootstring parameters */ - base = 36, - tMin = 1, - tMax = 26, - skew = 38, - damp = 700, - initialBias = 72, - initialN = 128, // 0x80 - delimiter = '-', // '\x2D' - - /** Regular expressions */ - regexPunycode = /^xn--/, - regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars - regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators - - /** Error messages */ - errors = { - 'overflow': 'Overflow: input needs wider integers to process', - 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', - 'invalid-input': 'Invalid input' - }, - - /** Convenience shortcuts */ - baseMinusTMin = base - tMin, - floor = Math.floor, - stringFromCharCode = String.fromCharCode, - - /** Temporary variable */ - key; - - /*--------------------------------------------------------------------------*/ - - /** - * A generic error utility function. - * @private - * @param {String} type The error type. - * @returns {Error} Throws a `RangeError` with the applicable error message. - */ - function error(type) { - throw new RangeError(errors[type]); - } - - /** - * A generic `Array#map` utility function. - * @private - * @param {Array} array The array to iterate over. - * @param {Function} callback The function that gets called for every array - * item. - * @returns {Array} A new array of values returned by the callback function. - */ - function map(array, fn) { - var length = array.length; - var result = []; - while (length--) { - result[length] = fn(array[length]); - } - return result; - } - - /** - * A simple `Array#map`-like wrapper to work with domain name strings or email - * addresses. - * @private - * @param {String} domain The domain name or email address. - * @param {Function} callback The function that gets called for every - * character. - * @returns {Array} A new string of characters returned by the callback - * function. - */ - function mapDomain(string, fn) { - var parts = string.split('@'); - var result = ''; - if (parts.length > 1) { - // In email addresses, only the domain name should be punycoded. Leave - // the local part (i.e. everything up to `@`) intact. - result = parts[0] + '@'; - string = parts[1]; - } - // Avoid `split(regex)` for IE8 compatibility. See #17. - string = string.replace(regexSeparators, '\x2E'); - var labels = string.split('.'); - var encoded = map(labels, fn).join('.'); - return result + encoded; - } - - /** - * Creates an array containing the numeric code points of each Unicode - * character in the string. While JavaScript uses UCS-2 internally, - * this function will convert a pair of surrogate halves (each of which - * UCS-2 exposes as separate characters) into a single code point, - * matching UTF-16. - * @see `punycode.ucs2.encode` - * @see - * @memberOf punycode.ucs2 - * @name decode - * @param {String} string The Unicode input string (UCS-2). - * @returns {Array} The new array of code points. - */ - function ucs2decode(string) { - var output = [], - counter = 0, - length = string.length, - value, - extra; - while (counter < length) { - value = string.charCodeAt(counter++); - if (value >= 0xD800 && value <= 0xDBFF && counter < length) { - // high surrogate, and there is a next character - extra = string.charCodeAt(counter++); - if ((extra & 0xFC00) == 0xDC00) { // low surrogate - output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); - } else { - // unmatched surrogate; only append this code unit, in case the next - // code unit is the high surrogate of a surrogate pair - output.push(value); - counter--; - } - } else { - output.push(value); - } - } - return output; - } - - /** - * Creates a string based on an array of numeric code points. - * @see `punycode.ucs2.decode` - * @memberOf punycode.ucs2 - * @name encode - * @param {Array} codePoints The array of numeric code points. - * @returns {String} The new Unicode string (UCS-2). - */ - function ucs2encode(array) { - return map(array, function(value) { - var output = ''; - if (value > 0xFFFF) { - value -= 0x10000; - output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); - value = 0xDC00 | value & 0x3FF; - } - output += stringFromCharCode(value); - return output; - }).join(''); - } - - /** - * Converts a basic code point into a digit/integer. - * @see `digitToBasic()` - * @private - * @param {Number} codePoint The basic numeric code point value. - * @returns {Number} The numeric value of a basic code point (for use in - * representing integers) in the range `0` to `base - 1`, or `base` if - * the code point does not represent a value. - */ - function basicToDigit(codePoint) { - if (codePoint - 48 < 10) { - return codePoint - 22; - } - if (codePoint - 65 < 26) { - return codePoint - 65; - } - if (codePoint - 97 < 26) { - return codePoint - 97; - } - return base; - } - - /** - * Converts a digit/integer into a basic code point. - * @see `basicToDigit()` - * @private - * @param {Number} digit The numeric value of a basic code point. - * @returns {Number} The basic code point whose value (when used for - * representing integers) is `digit`, which needs to be in the range - * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is - * used; else, the lowercase form is used. The behavior is undefined - * if `flag` is non-zero and `digit` has no uppercase form. - */ - function digitToBasic(digit, flag) { - // 0..25 map to ASCII a..z or A..Z - // 26..35 map to ASCII 0..9 - return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5); - } - - /** - * Bias adaptation function as per section 3.4 of RFC 3492. - * https://tools.ietf.org/html/rfc3492#section-3.4 - * @private - */ - function adapt(delta, numPoints, firstTime) { - var k = 0; - delta = firstTime ? floor(delta / damp) : delta >> 1; - delta += floor(delta / numPoints); - for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) { - delta = floor(delta / baseMinusTMin); - } - return floor(k + (baseMinusTMin + 1) * delta / (delta + skew)); - } - - /** - * Converts a Punycode string of ASCII-only symbols to a string of Unicode - * symbols. - * @memberOf punycode - * @param {String} input The Punycode string of ASCII-only symbols. - * @returns {String} The resulting string of Unicode symbols. - */ - function decode(input) { - // Don't use UCS-2 - var output = [], - inputLength = input.length, - out, - i = 0, - n = initialN, - bias = initialBias, - basic, - j, - index, - oldi, - w, - k, - digit, - t, - /** Cached calculation results */ - baseMinusT; - - // Handle the basic code points: let `basic` be the number of input code - // points before the last delimiter, or `0` if there is none, then copy - // the first basic code points to the output. - - basic = input.lastIndexOf(delimiter); - if (basic < 0) { - basic = 0; - } - - for (j = 0; j < basic; ++j) { - // if it's not a basic code point - if (input.charCodeAt(j) >= 0x80) { - error('not-basic'); - } - output.push(input.charCodeAt(j)); - } - - // Main decoding loop: start just after the last delimiter if any basic code - // points were copied; start at the beginning otherwise. - - for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) { - - // `index` is the index of the next character to be consumed. - // Decode a generalized variable-length integer into `delta`, - // which gets added to `i`. The overflow checking is easier - // if we increase `i` as we go, then subtract off its starting - // value at the end to obtain `delta`. - for (oldi = i, w = 1, k = base; /* no condition */; k += base) { - - if (index >= inputLength) { - error('invalid-input'); - } - - digit = basicToDigit(input.charCodeAt(index++)); - - if (digit >= base || digit > floor((maxInt - i) / w)) { - error('overflow'); - } - - i += digit * w; - t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); - - if (digit < t) { - break; - } - - baseMinusT = base - t; - if (w > floor(maxInt / baseMinusT)) { - error('overflow'); - } - - w *= baseMinusT; - - } - - out = output.length + 1; - bias = adapt(i - oldi, out, oldi == 0); - - // `i` was supposed to wrap around from `out` to `0`, - // incrementing `n` each time, so we'll fix that now: - if (floor(i / out) > maxInt - n) { - error('overflow'); - } - - n += floor(i / out); - i %= out; - - // Insert `n` at position `i` of the output - output.splice(i++, 0, n); - - } - - return ucs2encode(output); - } - - /** - * Converts a string of Unicode symbols (e.g. a domain name label) to a - * Punycode string of ASCII-only symbols. - * @memberOf punycode - * @param {String} input The string of Unicode symbols. - * @returns {String} The resulting Punycode string of ASCII-only symbols. - */ - function encode(input) { - var n, - delta, - handledCPCount, - basicLength, - bias, - j, - m, - q, - k, - t, - currentValue, - output = [], - /** `inputLength` will hold the number of code points in `input`. */ - inputLength, - /** Cached calculation results */ - handledCPCountPlusOne, - baseMinusT, - qMinusT; - - // Convert the input in UCS-2 to Unicode - input = ucs2decode(input); - - // Cache the length - inputLength = input.length; - - // Initialize the state - n = initialN; - delta = 0; - bias = initialBias; - - // Handle the basic code points - for (j = 0; j < inputLength; ++j) { - currentValue = input[j]; - if (currentValue < 0x80) { - output.push(stringFromCharCode(currentValue)); - } - } - - handledCPCount = basicLength = output.length; - - // `handledCPCount` is the number of code points that have been handled; - // `basicLength` is the number of basic code points. - - // Finish the basic string - if it is not empty - with a delimiter - if (basicLength) { - output.push(delimiter); - } - - // Main encoding loop: - while (handledCPCount < inputLength) { - - // All non-basic code points < n have been handled already. Find the next - // larger one: - for (m = maxInt, j = 0; j < inputLength; ++j) { - currentValue = input[j]; - if (currentValue >= n && currentValue < m) { - m = currentValue; - } - } - - // Increase `delta` enough to advance the decoder's state to , - // but guard against overflow - handledCPCountPlusOne = handledCPCount + 1; - if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { - error('overflow'); - } - - delta += (m - n) * handledCPCountPlusOne; - n = m; - - for (j = 0; j < inputLength; ++j) { - currentValue = input[j]; - - if (currentValue < n && ++delta > maxInt) { - error('overflow'); - } - - if (currentValue == n) { - // Represent delta as a generalized variable-length integer - for (q = delta, k = base; /* no condition */; k += base) { - t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); - if (q < t) { - break; - } - qMinusT = q - t; - baseMinusT = base - t; - output.push( - stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)) - ); - q = floor(qMinusT / baseMinusT); - } - - output.push(stringFromCharCode(digitToBasic(q, 0))); - bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength); - delta = 0; - ++handledCPCount; - } - } - - ++delta; - ++n; - - } - return output.join(''); - } - - /** - * Converts a Punycode string representing a domain name or an email address - * to Unicode. Only the Punycoded parts of the input will be converted, i.e. - * it doesn't matter if you call it on a string that has already been - * converted to Unicode. - * @memberOf punycode - * @param {String} input The Punycoded domain name or email address to - * convert to Unicode. - * @returns {String} The Unicode representation of the given Punycode - * string. - */ - function toUnicode(input) { - return mapDomain(input, function(string) { - return regexPunycode.test(string) - ? decode(string.slice(4).toLowerCase()) - : string; - }); - } - - /** - * Converts a Unicode string representing a domain name or an email address to - * Punycode. Only the non-ASCII parts of the domain name will be converted, - * i.e. it doesn't matter if you call it with a domain that's already in - * ASCII. - * @memberOf punycode - * @param {String} input The domain name or email address to convert, as a - * Unicode string. - * @returns {String} The Punycode representation of the given domain name or - * email address. - */ - function toASCII(input) { - return mapDomain(input, function(string) { - return regexNonASCII.test(string) - ? 'xn--' + encode(string) - : string; - }); - } - - /*--------------------------------------------------------------------------*/ - - /** Define the public API */ - punycode = { - /** - * A string representing the current Punycode.js version number. - * @memberOf punycode - * @type String - */ - 'version': '1.4.1', - /** - * An object of methods to convert from JavaScript's internal character - * representation (UCS-2) to Unicode code points, and back. - * @see - * @memberOf punycode - * @type Object - */ - 'ucs2': { - 'decode': ucs2decode, - 'encode': ucs2encode - }, - 'decode': decode, - 'encode': encode, - 'toASCII': toASCII, - 'toUnicode': toUnicode - }; - - /** Expose `punycode` */ - // Some AMD build optimizers, like r.js, check for specific condition patterns - // like the following: - if ( - typeof define == 'function' && - typeof define.amd == 'object' && - define.amd - ) { - define('punycode', function() { - return punycode; - }); - } else if (freeExports && freeModule) { - if (module.exports == freeExports) { - // in Node.js, io.js, or RingoJS v0.8.0+ - freeModule.exports = punycode; - } else { - // in Narwhal or RingoJS v0.7.0- - for (key in punycode) { - punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); - } - } - } else { - // in Rhino or a web browser - root.punycode = punycode; - } - -}(this)); - -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{}],43:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -'use strict'; - -// If obj.hasOwnProperty has been overridden, then calling -// obj.hasOwnProperty(prop) will break. -// See: https://github.com/joyent/node/issues/1707 -function hasOwnProperty(obj, prop) { - return Object.prototype.hasOwnProperty.call(obj, prop); -} - -module.exports = function(qs, sep, eq, options) { - sep = sep || '&'; - eq = eq || '='; - var obj = {}; - - if (typeof qs !== 'string' || qs.length === 0) { - return obj; +}).call(this,require('_process')) +},{"./lib/_stream_duplex.js":68,"./lib/_stream_passthrough.js":69,"./lib/_stream_readable.js":70,"./lib/_stream_transform.js":71,"./lib/_stream_writable.js":72,"_process":923,"stream":978}],74:[function(require,module,exports){ +"use strict" + +var convexHull1d = require('./lib/ch1d') +var convexHull2d = require('./lib/ch2d') +var convexHullnd = require('./lib/chnd') + +module.exports = convexHull + +function convexHull(points) { + var n = points.length + if(n === 0) { + return [] + } else if(n === 1) { + return [[0]] } - - var regexp = /\+/g; - qs = qs.split(sep); - - var maxKeys = 1000; - if (options && typeof options.maxKeys === 'number') { - maxKeys = options.maxKeys; + var d = points[0].length + if(d === 0) { + return [] + } else if(d === 1) { + return convexHull1d(points) + } else if(d === 2) { + return convexHull2d(points) } + return convexHullnd(points, d) +} +},{"./lib/ch1d":75,"./lib/ch2d":76,"./lib/chnd":77}],75:[function(require,module,exports){ +"use strict" - var len = qs.length; - // maxKeys <= 0 means that we should not limit keys count - if (maxKeys > 0 && len > maxKeys) { - len = maxKeys; - } +module.exports = convexHull1d - for (var i = 0; i < len; ++i) { - var x = qs[i].replace(regexp, '%20'), - idx = x.indexOf(eq), - kstr, vstr, k, v; - - if (idx >= 0) { - kstr = x.substr(0, idx); - vstr = x.substr(idx + 1); - } else { - kstr = x; - vstr = ''; +function convexHull1d(points) { + var lo = 0 + var hi = 0 + for(var i=1; i points[hi][0]) { + hi = i } } - - return obj; -}; - -var isArray = Array.isArray || function (xs) { - return Object.prototype.toString.call(xs) === '[object Array]'; -}; - -},{}],44:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -'use strict'; - -var stringifyPrimitive = function(v) { - switch (typeof v) { - case 'string': - return v; - - case 'boolean': - return v ? 'true' : 'false'; - - case 'number': - return isFinite(v) ? v : ''; - - default: - return ''; + if(lo < hi) { + return [[lo], [hi]] + } else if(lo > hi) { + return [[hi], [lo]] + } else { + return [[lo]] } -}; +} +},{}],76:[function(require,module,exports){ +'use strict' -module.exports = function(obj, sep, eq, name) { - sep = sep || '&'; - eq = eq || '='; - if (obj === null) { - obj = undefined; +module.exports = convexHull2D + +var monotoneHull = require('monotone-convex-hull-2d') + +function convexHull2D(points) { + var hull = monotoneHull(points) + var h = hull.length + if(h <= 2) { + return [] } + var edges = new Array(h) + var a = hull[h-1] + for(var i=0; i= front[k]) { + x += 1 + } + } + c[j] = x } - }).join(sep); - + } } - - if (!name) return ''; - return encodeURIComponent(stringifyPrimitive(name)) + eq + - encodeURIComponent(stringifyPrimitive(obj)); -}; - -var isArray = Array.isArray || function (xs) { - return Object.prototype.toString.call(xs) === '[object Array]'; -}; - -function map (xs, f) { - if (xs.map) return xs.map(f); - var res = []; - for (var i = 0; i < xs.length; i++) { - res.push(f(xs[i], i)); - } - return res; + return cells } -var objectKeys = Object.keys || function (obj) { - var res = []; - for (var key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key); +function convexHullnD(points, d) { + try { + return ich(points, true) + } catch(e) { + //If point set is degenerate, try to find a basis and rerun it + var ah = aff(points) + if(ah.length <= d) { + //No basis, no try + return [] + } + var npoints = permute(points, ah) + var nhull = ich(npoints, true) + return invPermute(nhull, ah) } - return res; -}; - -},{}],45:[function(require,module,exports){ -'use strict'; - -exports.decode = exports.parse = require('./decode'); -exports.encode = exports.stringify = require('./encode'); - -},{"./decode":43,"./encode":44}],46:[function(require,module,exports){ -module.exports = require("./lib/_stream_duplex.js") - -},{"./lib/_stream_duplex.js":47}],47:[function(require,module,exports){ -arguments[4][4][0].apply(exports,arguments) -},{"./_stream_readable":49,"./_stream_writable":51,"core-util-is":54,"dup":4,"inherits":38,"process-nextick-args":56}],48:[function(require,module,exports){ -arguments[4][5][0].apply(exports,arguments) -},{"./_stream_transform":50,"core-util-is":54,"dup":5,"inherits":38}],49:[function(require,module,exports){ -arguments[4][6][0].apply(exports,arguments) -},{"./_stream_duplex":47,"./internal/streams/BufferList":52,"_process":41,"buffer":33,"buffer-shims":53,"core-util-is":54,"dup":6,"events":37,"inherits":38,"isarray":55,"process-nextick-args":56,"string_decoder/":63,"util":19}],50:[function(require,module,exports){ -arguments[4][7][0].apply(exports,arguments) -},{"./_stream_duplex":47,"core-util-is":54,"dup":7,"inherits":38}],51:[function(require,module,exports){ -arguments[4][8][0].apply(exports,arguments) -},{"./_stream_duplex":47,"_process":41,"buffer":33,"buffer-shims":53,"core-util-is":54,"dup":8,"events":37,"inherits":38,"process-nextick-args":56,"util-deprecate":57}],52:[function(require,module,exports){ -arguments[4][9][0].apply(exports,arguments) -},{"buffer":33,"buffer-shims":53,"dup":9}],53:[function(require,module,exports){ -arguments[4][10][0].apply(exports,arguments) -},{"buffer":33,"dup":10}],54:[function(require,module,exports){ +} +},{"affine-hull":5,"incremental-convex-hull":265}],78:[function(require,module,exports){ (function (Buffer){ // Copyright Joyent, Inc. and other Node contributors. // @@ -12977,1729 +13646,510 @@ function objectToString(o) { return Object.prototype.toString.call(o); } -}).call(this,{"isBuffer":require("../../../../insert-module-globals/node_modules/is-buffer/index.js")}) -},{"../../../../insert-module-globals/node_modules/is-buffer/index.js":39}],55:[function(require,module,exports){ -arguments[4][13][0].apply(exports,arguments) -},{"dup":13}],56:[function(require,module,exports){ -arguments[4][14][0].apply(exports,arguments) -},{"_process":41,"dup":14}],57:[function(require,module,exports){ -arguments[4][16][0].apply(exports,arguments) -},{"dup":16}],58:[function(require,module,exports){ -module.exports = require("./lib/_stream_passthrough.js") - -},{"./lib/_stream_passthrough.js":48}],59:[function(require,module,exports){ -arguments[4][17][0].apply(exports,arguments) -},{"./lib/_stream_duplex.js":47,"./lib/_stream_passthrough.js":48,"./lib/_stream_readable.js":49,"./lib/_stream_transform.js":50,"./lib/_stream_writable.js":51,"_process":41,"dup":17}],60:[function(require,module,exports){ -module.exports = require("./lib/_stream_transform.js") - -},{"./lib/_stream_transform.js":50}],61:[function(require,module,exports){ -module.exports = require("./lib/_stream_writable.js") - -},{"./lib/_stream_writable.js":51}],62:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -module.exports = Stream; - -var EE = require('events').EventEmitter; -var inherits = require('inherits'); - -inherits(Stream, EE); -Stream.Readable = require('readable-stream/readable.js'); -Stream.Writable = require('readable-stream/writable.js'); -Stream.Duplex = require('readable-stream/duplex.js'); -Stream.Transform = require('readable-stream/transform.js'); -Stream.PassThrough = require('readable-stream/passthrough.js'); - -// Backwards-compat with node 0.4.x -Stream.Stream = Stream; - - - -// old-style streams. Note that the pipe method (the only relevant -// part of this class) is overridden in the Readable class. - -function Stream() { - EE.call(this); -} - -Stream.prototype.pipe = function(dest, options) { - var source = this; - - function ondata(chunk) { - if (dest.writable) { - if (false === dest.write(chunk) && source.pause) { - source.pause(); - } - } - } - - source.on('data', ondata); - - function ondrain() { - if (source.readable && source.resume) { - source.resume(); - } - } - - dest.on('drain', ondrain); - - // If the 'end' option is not supplied, dest.end() will be called when - // source gets the 'end' or 'close' events. Only dest.end() once. - if (!dest._isStdio && (!options || options.end !== false)) { - source.on('end', onend); - source.on('close', onclose); - } - - var didOnEnd = false; - function onend() { - if (didOnEnd) return; - didOnEnd = true; - - dest.end(); - } - - - function onclose() { - if (didOnEnd) return; - didOnEnd = true; - - if (typeof dest.destroy === 'function') dest.destroy(); - } - - // don't leave dangling pipes when there are errors. - function onerror(er) { - cleanup(); - if (EE.listenerCount(this, 'error') === 0) { - throw er; // Unhandled stream error in pipe. - } - } - - source.on('error', onerror); - dest.on('error', onerror); - - // remove all the event listeners that were added. - function cleanup() { - source.removeListener('data', ondata); - dest.removeListener('drain', ondrain); - - source.removeListener('end', onend); - source.removeListener('close', onclose); - - source.removeListener('error', onerror); - dest.removeListener('error', onerror); - - source.removeListener('end', cleanup); - source.removeListener('close', cleanup); - - dest.removeListener('close', cleanup); - } - - source.on('end', cleanup); - source.on('close', cleanup); - - dest.on('close', cleanup); - - dest.emit('pipe', source); - - // Allow for unix-like usage: A.pipe(B).pipe(C) - return dest; -}; - -},{"events":37,"inherits":38,"readable-stream/duplex.js":46,"readable-stream/passthrough.js":58,"readable-stream/readable.js":59,"readable-stream/transform.js":60,"readable-stream/writable.js":61}],63:[function(require,module,exports){ -arguments[4][15][0].apply(exports,arguments) -},{"buffer":33,"dup":15}],64:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -'use strict'; - -var punycode = require('punycode'); -var util = require('./util'); - -exports.parse = urlParse; -exports.resolve = urlResolve; -exports.resolveObject = urlResolveObject; -exports.format = urlFormat; - -exports.Url = Url; - -function Url() { - this.protocol = null; - this.slashes = null; - this.auth = null; - this.host = null; - this.port = null; - this.hostname = null; - this.hash = null; - this.search = null; - this.query = null; - this.pathname = null; - this.path = null; - this.href = null; -} - -// Reference: RFC 3986, RFC 1808, RFC 2396 - -// define these here so at least they only have to be -// compiled once on the first module load. -var protocolPattern = /^([a-z0-9.+-]+:)/i, - portPattern = /:[0-9]*$/, - - // Special case for a simple path URL - simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/, - - // RFC 2396: characters reserved for delimiting URLs. - // We actually just auto-escape these. - delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'], - - // RFC 2396: characters not allowed for various reasons. - unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims), - - // Allowed by RFCs, but cause of XSS attacks. Always escape these. - autoEscape = ['\''].concat(unwise), - // Characters that are never ever allowed in a hostname. - // Note that any invalid chars are also handled, but these - // are the ones that are *expected* to be seen, so we fast-path - // them. - nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape), - hostEndingChars = ['/', '?', '#'], - hostnameMaxLen = 255, - hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/, - hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/, - // protocols that can allow "unsafe" and "unwise" chars. - unsafeProtocol = { - 'javascript': true, - 'javascript:': true - }, - // protocols that never have a hostname. - hostlessProtocol = { - 'javascript': true, - 'javascript:': true - }, - // protocols that always contain a // bit. - slashedProtocol = { - 'http': true, - 'https': true, - 'ftp': true, - 'gopher': true, - 'file': true, - 'http:': true, - 'https:': true, - 'ftp:': true, - 'gopher:': true, - 'file:': true - }, - querystring = require('querystring'); - -function urlParse(url, parseQueryString, slashesDenoteHost) { - if (url && util.isObject(url) && url instanceof Url) return url; - - var u = new Url; - u.parse(url, parseQueryString, slashesDenoteHost); - return u; -} - -Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { - if (!util.isString(url)) { - throw new TypeError("Parameter 'url' must be a string, not " + typeof url); - } - - // Copy chrome, IE, opera backslash-handling behavior. - // Back slashes before the query string get converted to forward slashes - // See: https://code.google.com/p/chromium/issues/detail?id=25916 - var queryIndex = url.indexOf('?'), - splitter = - (queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#', - uSplit = url.split(splitter), - slashRegex = /\\/g; - uSplit[0] = uSplit[0].replace(slashRegex, '/'); - url = uSplit.join(splitter); - - var rest = url; - - // trim before proceeding. - // This is to support parse stuff like " http://foo.com \n" - rest = rest.trim(); - - if (!slashesDenoteHost && url.split('#').length === 1) { - // Try fast path regexp - var simplePath = simplePathPattern.exec(rest); - if (simplePath) { - this.path = rest; - this.href = rest; - this.pathname = simplePath[1]; - if (simplePath[2]) { - this.search = simplePath[2]; - if (parseQueryString) { - this.query = querystring.parse(this.search.substr(1)); - } else { - this.query = this.search.substr(1); - } - } else if (parseQueryString) { - this.search = ''; - this.query = {}; - } - return this; - } - } - - var proto = protocolPattern.exec(rest); - if (proto) { - proto = proto[0]; - var lowerProto = proto.toLowerCase(); - this.protocol = lowerProto; - rest = rest.substr(proto.length); - } - - // figure out if it's got a host - // user@server is *always* interpreted as a hostname, and url - // resolution will treat //foo/bar as host=foo,path=bar because that's - // how the browser resolves relative URLs. - if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) { - var slashes = rest.substr(0, 2) === '//'; - if (slashes && !(proto && hostlessProtocol[proto])) { - rest = rest.substr(2); - this.slashes = true; - } - } - - if (!hostlessProtocol[proto] && - (slashes || (proto && !slashedProtocol[proto]))) { - - // there's a hostname. - // the first instance of /, ?, ;, or # ends the host. - // - // If there is an @ in the hostname, then non-host chars *are* allowed - // to the left of the last @ sign, unless some host-ending character - // comes *before* the @-sign. - // URLs are obnoxious. - // - // ex: - // http://a@b@c/ => user:a@b host:c - // http://a@b?@c => user:a host:c path:/?@c - - // v0.12 TODO(isaacs): This is not quite how Chrome does things. - // Review our test case against browsers more comprehensively. - - // find the first instance of any hostEndingChars - var hostEnd = -1; - for (var i = 0; i < hostEndingChars.length; i++) { - var hec = rest.indexOf(hostEndingChars[i]); - if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) - hostEnd = hec; - } - - // at this point, either we have an explicit point where the - // auth portion cannot go past, or the last @ char is the decider. - var auth, atSign; - if (hostEnd === -1) { - // atSign can be anywhere. - atSign = rest.lastIndexOf('@'); - } else { - // atSign must be in auth portion. - // http://a@b/c@d => host:b auth:a path:/c@d - atSign = rest.lastIndexOf('@', hostEnd); - } - - // Now we have a portion which is definitely the auth. - // Pull that off. - if (atSign !== -1) { - auth = rest.slice(0, atSign); - rest = rest.slice(atSign + 1); - this.auth = decodeURIComponent(auth); - } - - // the host is the remaining to the left of the first non-host char - hostEnd = -1; - for (var i = 0; i < nonHostChars.length; i++) { - var hec = rest.indexOf(nonHostChars[i]); - if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) - hostEnd = hec; - } - // if we still have not hit it, then the entire thing is a host. - if (hostEnd === -1) - hostEnd = rest.length; - - this.host = rest.slice(0, hostEnd); - rest = rest.slice(hostEnd); - - // pull out port. - this.parseHost(); - - // we've indicated that there is a hostname, - // so even if it's empty, it has to be present. - this.hostname = this.hostname || ''; - - // if hostname begins with [ and ends with ] - // assume that it's an IPv6 address. - var ipv6Hostname = this.hostname[0] === '[' && - this.hostname[this.hostname.length - 1] === ']'; - - // validate a little. - if (!ipv6Hostname) { - var hostparts = this.hostname.split(/\./); - for (var i = 0, l = hostparts.length; i < l; i++) { - var part = hostparts[i]; - if (!part) continue; - if (!part.match(hostnamePartPattern)) { - var newpart = ''; - for (var j = 0, k = part.length; j < k; j++) { - if (part.charCodeAt(j) > 127) { - // we replace non-ASCII char with a temporary placeholder - // we need this to make sure size of hostname is not - // broken by replacing non-ASCII by nothing - newpart += 'x'; - } else { - newpart += part[j]; - } - } - // we test again with ASCII char only - if (!newpart.match(hostnamePartPattern)) { - var validParts = hostparts.slice(0, i); - var notHost = hostparts.slice(i + 1); - var bit = part.match(hostnamePartStart); - if (bit) { - validParts.push(bit[1]); - notHost.unshift(bit[2]); - } - if (notHost.length) { - rest = '/' + notHost.join('.') + rest; - } - this.hostname = validParts.join('.'); - break; - } - } - } - } - - if (this.hostname.length > hostnameMaxLen) { - this.hostname = ''; - } else { - // hostnames are always lower case. - this.hostname = this.hostname.toLowerCase(); - } - - if (!ipv6Hostname) { - // IDNA Support: Returns a punycoded representation of "domain". - // It only converts parts of the domain name that - // have non-ASCII characters, i.e. it doesn't matter if - // you call it with a domain that already is ASCII-only. - this.hostname = punycode.toASCII(this.hostname); - } - - var p = this.port ? ':' + this.port : ''; - var h = this.hostname || ''; - this.host = h + p; - this.href += this.host; - - // strip [ and ] from the hostname - // the host field still retains them, though - if (ipv6Hostname) { - this.hostname = this.hostname.substr(1, this.hostname.length - 2); - if (rest[0] !== '/') { - rest = '/' + rest; - } - } - } - - // now rest is set to the post-host stuff. - // chop off any delim chars. - if (!unsafeProtocol[lowerProto]) { - - // First, make 100% sure that any "autoEscape" chars get - // escaped, even if encodeURIComponent doesn't think they - // need to be. - for (var i = 0, l = autoEscape.length; i < l; i++) { - var ae = autoEscape[i]; - if (rest.indexOf(ae) === -1) - continue; - var esc = encodeURIComponent(ae); - if (esc === ae) { - esc = escape(ae); - } - rest = rest.split(ae).join(esc); - } - } - - - // chop off from the tail first. - var hash = rest.indexOf('#'); - if (hash !== -1) { - // got a fragment string. - this.hash = rest.substr(hash); - rest = rest.slice(0, hash); - } - var qm = rest.indexOf('?'); - if (qm !== -1) { - this.search = rest.substr(qm); - this.query = rest.substr(qm + 1); - if (parseQueryString) { - this.query = querystring.parse(this.query); - } - rest = rest.slice(0, qm); - } else if (parseQueryString) { - // no query string, but parseQueryString still requested - this.search = ''; - this.query = {}; - } - if (rest) this.pathname = rest; - if (slashedProtocol[lowerProto] && - this.hostname && !this.pathname) { - this.pathname = '/'; - } - - //to support http.request - if (this.pathname || this.search) { - var p = this.pathname || ''; - var s = this.search || ''; - this.path = p + s; - } - - // finally, reconstruct the href based on what has been validated. - this.href = this.format(); - return this; -}; - -// format a parsed object into a url string -function urlFormat(obj) { - // ensure it's an object, and not a string url. - // If it's an obj, this is a no-op. - // this way, you can call url_format() on strings - // to clean up potentially wonky urls. - if (util.isString(obj)) obj = urlParse(obj); - if (!(obj instanceof Url)) return Url.prototype.format.call(obj); - return obj.format(); -} - -Url.prototype.format = function() { - var auth = this.auth || ''; - if (auth) { - auth = encodeURIComponent(auth); - auth = auth.replace(/%3A/i, ':'); - auth += '@'; - } - - var protocol = this.protocol || '', - pathname = this.pathname || '', - hash = this.hash || '', - host = false, - query = ''; - - if (this.host) { - host = auth + this.host; - } else if (this.hostname) { - host = auth + (this.hostname.indexOf(':') === -1 ? - this.hostname : - '[' + this.hostname + ']'); - if (this.port) { - host += ':' + this.port; - } - } - - if (this.query && - util.isObject(this.query) && - Object.keys(this.query).length) { - query = querystring.stringify(this.query); - } - - var search = this.search || (query && ('?' + query)) || ''; - - if (protocol && protocol.substr(-1) !== ':') protocol += ':'; - - // only the slashedProtocols get the //. Not mailto:, xmpp:, etc. - // unless they had them to begin with. - if (this.slashes || - (!protocol || slashedProtocol[protocol]) && host !== false) { - host = '//' + (host || ''); - if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname; - } else if (!host) { - host = ''; - } - - if (hash && hash.charAt(0) !== '#') hash = '#' + hash; - if (search && search.charAt(0) !== '?') search = '?' + search; - - pathname = pathname.replace(/[?#]/g, function(match) { - return encodeURIComponent(match); - }); - search = search.replace('#', '%23'); - - return protocol + host + pathname + search + hash; -}; - -function urlResolve(source, relative) { - return urlParse(source, false, true).resolve(relative); -} - -Url.prototype.resolve = function(relative) { - return this.resolveObject(urlParse(relative, false, true)).format(); -}; - -function urlResolveObject(source, relative) { - if (!source) return relative; - return urlParse(source, false, true).resolveObject(relative); -} - -Url.prototype.resolveObject = function(relative) { - if (util.isString(relative)) { - var rel = new Url(); - rel.parse(relative, false, true); - relative = rel; - } - - var result = new Url(); - var tkeys = Object.keys(this); - for (var tk = 0; tk < tkeys.length; tk++) { - var tkey = tkeys[tk]; - result[tkey] = this[tkey]; - } - - // hash is always overridden, no matter what. - // even href="" will remove it. - result.hash = relative.hash; - - // if the relative url is empty, then there's nothing left to do here. - if (relative.href === '') { - result.href = result.format(); - return result; - } - - // hrefs like //foo/bar always cut to the protocol. - if (relative.slashes && !relative.protocol) { - // take everything except the protocol from relative - var rkeys = Object.keys(relative); - for (var rk = 0; rk < rkeys.length; rk++) { - var rkey = rkeys[rk]; - if (rkey !== 'protocol') - result[rkey] = relative[rkey]; - } - - //urlParse appends trailing / to urls like http://www.example.com - if (slashedProtocol[result.protocol] && - result.hostname && !result.pathname) { - result.path = result.pathname = '/'; - } - - result.href = result.format(); - return result; - } - - if (relative.protocol && relative.protocol !== result.protocol) { - // if it's a known url protocol, then changing - // the protocol does weird things - // first, if it's not file:, then we MUST have a host, - // and if there was a path - // to begin with, then we MUST have a path. - // if it is file:, then the host is dropped, - // because that's known to be hostless. - // anything else is assumed to be absolute. - if (!slashedProtocol[relative.protocol]) { - var keys = Object.keys(relative); - for (var v = 0; v < keys.length; v++) { - var k = keys[v]; - result[k] = relative[k]; - } - result.href = result.format(); - return result; - } - - result.protocol = relative.protocol; - if (!relative.host && !hostlessProtocol[relative.protocol]) { - var relPath = (relative.pathname || '').split('/'); - while (relPath.length && !(relative.host = relPath.shift())); - if (!relative.host) relative.host = ''; - if (!relative.hostname) relative.hostname = ''; - if (relPath[0] !== '') relPath.unshift(''); - if (relPath.length < 2) relPath.unshift(''); - result.pathname = relPath.join('/'); - } else { - result.pathname = relative.pathname; - } - result.search = relative.search; - result.query = relative.query; - result.host = relative.host || ''; - result.auth = relative.auth; - result.hostname = relative.hostname || relative.host; - result.port = relative.port; - // to support http.request - if (result.pathname || result.search) { - var p = result.pathname || ''; - var s = result.search || ''; - result.path = p + s; - } - result.slashes = result.slashes || relative.slashes; - result.href = result.format(); - return result; - } - - var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'), - isRelAbs = ( - relative.host || - relative.pathname && relative.pathname.charAt(0) === '/' - ), - mustEndAbs = (isRelAbs || isSourceAbs || - (result.host && relative.pathname)), - removeAllDots = mustEndAbs, - srcPath = result.pathname && result.pathname.split('/') || [], - relPath = relative.pathname && relative.pathname.split('/') || [], - psychotic = result.protocol && !slashedProtocol[result.protocol]; - - // if the url is a non-slashed url, then relative - // links like ../.. should be able - // to crawl up to the hostname, as well. This is strange. - // result.protocol has already been set by now. - // Later on, put the first path part into the host field. - if (psychotic) { - result.hostname = ''; - result.port = null; - if (result.host) { - if (srcPath[0] === '') srcPath[0] = result.host; - else srcPath.unshift(result.host); - } - result.host = ''; - if (relative.protocol) { - relative.hostname = null; - relative.port = null; - if (relative.host) { - if (relPath[0] === '') relPath[0] = relative.host; - else relPath.unshift(relative.host); - } - relative.host = null; - } - mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === ''); - } - - if (isRelAbs) { - // it's absolute. - result.host = (relative.host || relative.host === '') ? - relative.host : result.host; - result.hostname = (relative.hostname || relative.hostname === '') ? - relative.hostname : result.hostname; - result.search = relative.search; - result.query = relative.query; - srcPath = relPath; - // fall through to the dot-handling below. - } else if (relPath.length) { - // it's relative - // throw away the existing file, and take the new path instead. - if (!srcPath) srcPath = []; - srcPath.pop(); - srcPath = srcPath.concat(relPath); - result.search = relative.search; - result.query = relative.query; - } else if (!util.isNullOrUndefined(relative.search)) { - // just pull out the search. - // like href='?foo'. - // Put this after the other two cases because it simplifies the booleans - if (psychotic) { - result.hostname = result.host = srcPath.shift(); - //occationaly the auth can get stuck only in host - //this especially happens in cases like - //url.resolveObject('mailto:local1@domain1', 'local2@domain2') - var authInHost = result.host && result.host.indexOf('@') > 0 ? - result.host.split('@') : false; - if (authInHost) { - result.auth = authInHost.shift(); - result.host = result.hostname = authInHost.shift(); - } - } - result.search = relative.search; - result.query = relative.query; - //to support http.request - if (!util.isNull(result.pathname) || !util.isNull(result.search)) { - result.path = (result.pathname ? result.pathname : '') + - (result.search ? result.search : ''); - } - result.href = result.format(); - return result; - } - - if (!srcPath.length) { - // no path at all. easy. - // we've already handled the other stuff above. - result.pathname = null; - //to support http.request - if (result.search) { - result.path = '/' + result.search; - } else { - result.path = null; - } - result.href = result.format(); - return result; - } - - // if a url ENDs in . or .., then it must get a trailing slash. - // however, if it ends in anything else non-slashy, - // then it must NOT get a trailing slash. - var last = srcPath.slice(-1)[0]; - var hasTrailingSlash = ( - (result.host || relative.host || srcPath.length > 1) && - (last === '.' || last === '..') || last === ''); - - // strip single dots, resolve double dots to parent dir - // if the path tries to go above the root, `up` ends up > 0 - var up = 0; - for (var i = srcPath.length; i >= 0; i--) { - last = srcPath[i]; - if (last === '.') { - srcPath.splice(i, 1); - } else if (last === '..') { - srcPath.splice(i, 1); - up++; - } else if (up) { - srcPath.splice(i, 1); - up--; - } - } - - // if the path is allowed to go above the root, restore leading ..s - if (!mustEndAbs && !removeAllDots) { - for (; up--; up) { - srcPath.unshift('..'); - } - } - - if (mustEndAbs && srcPath[0] !== '' && - (!srcPath[0] || srcPath[0].charAt(0) !== '/')) { - srcPath.unshift(''); - } - - if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) { - srcPath.push(''); - } - - var isAbsolute = srcPath[0] === '' || - (srcPath[0] && srcPath[0].charAt(0) === '/'); - - // put the host back - if (psychotic) { - result.hostname = result.host = isAbsolute ? '' : - srcPath.length ? srcPath.shift() : ''; - //occationaly the auth can get stuck only in host - //this especially happens in cases like - //url.resolveObject('mailto:local1@domain1', 'local2@domain2') - var authInHost = result.host && result.host.indexOf('@') > 0 ? - result.host.split('@') : false; - if (authInHost) { - result.auth = authInHost.shift(); - result.host = result.hostname = authInHost.shift(); - } - } - - mustEndAbs = mustEndAbs || (result.host && srcPath.length); - - if (mustEndAbs && !isAbsolute) { - srcPath.unshift(''); - } - - if (!srcPath.length) { - result.pathname = null; - result.path = null; - } else { - result.pathname = srcPath.join('/'); - } - - //to support request.http - if (!util.isNull(result.pathname) || !util.isNull(result.search)) { - result.path = (result.pathname ? result.pathname : '') + - (result.search ? result.search : ''); - } - result.auth = relative.auth || result.auth; - result.slashes = result.slashes || relative.slashes; - result.href = result.format(); - return result; -}; - -Url.prototype.parseHost = function() { - var host = this.host; - var port = portPattern.exec(host); - if (port) { - port = port[0]; - if (port !== ':') { - this.port = port.substr(1); - } - host = host.substr(0, host.length - port.length); - } - if (host) this.hostname = host; -}; - -},{"./util":65,"punycode":42,"querystring":45}],65:[function(require,module,exports){ -'use strict'; - +}).call(this,{"isBuffer":require("../../is-buffer/index.js")}) +},{"../../is-buffer/index.js":270}],79:[function(require,module,exports){ module.exports = { - isString: function(arg) { - return typeof(arg) === 'string'; - }, - isObject: function(arg) { - return typeof(arg) === 'object' && arg !== null; - }, - isNull: function(arg) { - return arg === null; - }, - isNullOrUndefined: function(arg) { - return arg == null; - } -}; - -},{}],66:[function(require,module,exports){ -arguments[4][12][0].apply(exports,arguments) -},{"dup":12}],67:[function(require,module,exports){ -module.exports = function isBuffer(arg) { - return arg && typeof arg === 'object' - && typeof arg.copy === 'function' - && typeof arg.fill === 'function' - && typeof arg.readUInt8 === 'function'; + AFG: 'afghan', + ALA: '\\b\\wland', + ALB: 'albania', + DZA: 'algeria', + ASM: '^(?=.*americ).*samoa', + AND: 'andorra', + AGO: 'angola', + AIA: 'anguill?a', + ATA: 'antarctica', + ATG: 'antigua', + ARG: 'argentin', + ARM: 'armenia', + ABW: '^(?!.*bonaire).*\\baruba', + AUS: 'australia', + AUT: '^(?!.*hungary).*austria|\\baustri.*\\bemp', + AZE: 'azerbaijan', + BHS: 'bahamas', + BHR: 'bahrain', + BGD: 'bangladesh|^(?=.*east).*paki?stan', + BRB: 'barbados', + BLR: 'belarus|byelo', + BEL: '^(?!.*luxem).*belgium', + BLZ: 'belize|^(?=.*british).*honduras', + BEN: 'benin|dahome', + BMU: 'bermuda', + BTN: 'bhutan', + BOL: 'bolivia', + BES: '^(?=.*bonaire).*eustatius|^(?=.*carib).*netherlands|\\bbes.?islands', + BIH: 'herzegovina|bosnia', + BWA: 'botswana|bechuana', + BVT: 'bouvet', + BRA: 'brazil', + IOT: 'british.?indian.?ocean', + BRN: 'brunei', + BGR: 'bulgaria', + BFA: 'burkina|\\bfaso|upper.?volta', + BDI: 'burundi', + CPV: 'verde', + KHM: 'cambodia|kampuchea|khmer', + CMR: 'cameroon', + CAN: 'canada', + CYM: 'cayman', + CAF: '\\bcentral.african.republic', + TCD: '\\bchad', + CHL: '\\bchile', + CHN: '^(?!.*\\bmac)(?!.*\\bhong)(?!.*\\btai)(?!.*\\brep).*china|^(?=.*peo)(?=.*rep).*china', + CXR: 'christmas', + CCK: '\\bcocos|keeling', + COL: 'colombia', + COM: 'comoro', + COG: '^(?!.*\\bdem)(?!.*\\bd[\\.]?r)(?!.*kinshasa)(?!.*zaire)(?!.*belg)(?!.*l.opoldville)(?!.*free).*\\bcongo', + COK: '\\bcook', + CRI: 'costa.?rica', + CIV: 'ivoire|ivory', + HRV: 'croatia', + CUB: '\\bcuba', + CUW: '^(?!.*bonaire).*\\bcura(c|ç)ao', + CYP: 'cyprus', + CSK: 'czechoslovakia', + CZE: '^(?=.*rep).*czech|czechia|bohemia', + COD: '\\bdem.*congo|congo.*\\bdem|congo.*\\bd[\\.]?r|\\bd[\\.]?r.*congo|belgian.?congo|congo.?free.?state|kinshasa|zaire|l.opoldville|drc|droc|rdc', + DNK: 'denmark', + DJI: 'djibouti', + DMA: 'dominica(?!n)', + DOM: 'dominican.rep', + ECU: 'ecuador', + EGY: 'egypt', + SLV: 'el.?salvador', + GNQ: 'guine.*eq|eq.*guine|^(?=.*span).*guinea', + ERI: 'eritrea', + EST: 'estonia', + ETH: 'ethiopia|abyssinia', + FLK: 'falkland|malvinas', + FRO: 'faroe|faeroe', + FJI: 'fiji', + FIN: 'finland', + FRA: '^(?!.*\\bdep)(?!.*martinique).*france|french.?republic|\\bgaul', + GUF: '^(?=.*french).*guiana', + PYF: 'french.?polynesia|tahiti', + ATF: 'french.?southern', + GAB: 'gabon', + GMB: 'gambia', + GEO: '^(?!.*south).*georgia', + DDR: 'german.?democratic.?republic|democratic.?republic.*germany|east.germany', + DEU: '^(?!.*east).*germany|^(?=.*\\bfed.*\\brep).*german', + GHA: 'ghana|gold.?coast', + GIB: 'gibraltar', + GRC: 'greece|hellenic|hellas', + GRL: 'greenland', + GRD: 'grenada', + GLP: 'guadeloupe', + GUM: '\\bguam', + GTM: 'guatemala', + GGY: 'guernsey', + GIN: '^(?!.*eq)(?!.*span)(?!.*bissau)(?!.*portu)(?!.*new).*guinea', + GNB: 'bissau|^(?=.*portu).*guinea', + GUY: 'guyana|british.?guiana', + HTI: 'haiti', + HMD: 'heard.*mcdonald', + VAT: 'holy.?see|vatican|papal.?st', + HND: '^(?!.*brit).*honduras', + HKG: 'hong.?kong', + HUN: '^(?!.*austr).*hungary', + ISL: 'iceland', + IND: 'india(?!.*ocea)', + IDN: 'indonesia', + IRN: '\\biran|persia', + IRQ: '\\biraq|mesopotamia', + IRL: '(^ireland)|(^republic.*ireland)', + IMN: '^(?=.*isle).*\\bman', + ISR: 'israel', + ITA: 'italy', + JAM: 'jamaica', + JPN: 'japan', + JEY: 'jersey', + JOR: 'jordan', + KAZ: 'kazak', + KEN: 'kenya|british.?east.?africa|east.?africa.?prot', + KIR: 'kiribati', + PRK: '^(?=.*democrat|people|north|d.*p.*.r).*\\bkorea|dprk|korea.*(d.*p.*r)', + KWT: 'kuwait', + KGZ: 'kyrgyz|kirghiz', + LAO: '\\blaos?\\b', + LVA: 'latvia', + LBN: 'lebanon', + LSO: 'lesotho|basuto', + LBR: 'liberia', + LBY: 'libya', + LIE: 'liechtenstein', + LTU: 'lithuania', + LUX: '^(?!.*belg).*luxem', + MAC: 'maca(o|u)', + MDG: 'madagascar|malagasy', + MWI: 'malawi|nyasa', + MYS: 'malaysia', + MDV: 'maldive', + MLI: '\\bmali\\b', + MLT: '\\bmalta', + MHL: 'marshall', + MTQ: 'martinique', + MRT: 'mauritania', + MUS: 'mauritius', + MYT: '\\bmayotte', + MEX: '\\bmexic', + FSM: 'fed.*micronesia|micronesia.*fed', + MCO: 'monaco', + MNG: 'mongolia', + MNE: '^(?!.*serbia).*montenegro', + MSR: 'montserrat', + MAR: 'morocco|\\bmaroc', + MOZ: 'mozambique', + MMR: 'myanmar|burma', + NAM: 'namibia', + NRU: 'nauru', + NPL: 'nepal', + NLD: '^(?!.*\\bant)(?!.*\\bcarib).*netherlands', + ANT: '^(?=.*\\bant).*(nether|dutch)', + NCL: 'new.?caledonia', + NZL: 'new.?zealand', + NIC: 'nicaragua', + NER: '\\bniger(?!ia)', + NGA: 'nigeria', + NIU: 'niue', + NFK: 'norfolk', + MNP: 'mariana', + NOR: 'norway', + OMN: '\\boman|trucial', + PAK: '^(?!.*east).*paki?stan', + PLW: 'palau', + PSE: 'palestin|\\bgaza|west.?bank', + PAN: 'panama', + PNG: 'papua|new.?guinea', + PRY: 'paraguay', + PER: 'peru', + PHL: 'philippines', + PCN: 'pitcairn', + POL: 'poland', + PRT: 'portugal', + PRI: 'puerto.?rico', + QAT: 'qatar', + KOR: '^(?!.*d.*p.*r)(?!.*democrat)(?!.*people)(?!.*north).*\\bkorea(?!.*d.*p.*r)', + MDA: 'moldov|b(a|e)ssarabia', + REU: 'r(e|é)union', + ROU: 'r(o|u|ou)mania', + RUS: '\\brussia|soviet.?union|u\\.?s\\.?s\\.?r|socialist.?republics', + RWA: 'rwanda', + BLM: 'barth(e|é)lemy', + SHN: 'helena', + KNA: 'kitts|\\bnevis', + LCA: '\\blucia', + MAF: '^(?=.*collectivity).*martin|^(?=.*france).*martin(?!ique)|^(?=.*french).*martin(?!ique)', + SPM: 'miquelon', + VCT: 'vincent', + WSM: '^(?!.*amer).*samoa', + SMR: 'san.?marino', + STP: '\\bs(a|ã)o.?tom(e|é)', + SAU: '\\bsa\\w*.?arabia', + SEN: 'senegal', + SRB: '^(?!.*monte).*serbia', + SYC: 'seychell', + SLE: 'sierra', + SGP: 'singapore', + SXM: '^(?!.*martin)(?!.*saba).*maarten', + SVK: '^(?!.*cze).*slovak', + SVN: 'slovenia', + SLB: 'solomon', + SOM: 'somali', + ZAF: 'south.africa|s\\\\..?africa', + SGS: 'south.?georgia|sandwich', + SSD: '\\bs\\w*.?sudan', + ESP: 'spain', + LKA: 'sri.?lanka|ceylon', + SDN: '^(?!.*\\bs(?!u)).*sudan', + SUR: 'surinam|dutch.?guiana', + SJM: 'svalbard', + SWZ: 'swaziland', + SWE: 'sweden', + CHE: 'switz|swiss', + SYR: 'syria', + TWN: 'taiwan|taipei|formosa|^(?!.*peo)(?=.*rep).*china', + TJK: 'tajik', + THA: 'thailand|\\bsiam', + MKD: 'macedonia|fyrom', + TLS: '^(?=.*leste).*timor|^(?=.*east).*timor', + TGO: 'togo', + TKL: 'tokelau', + TON: 'tonga', + TTO: 'trinidad|tobago', + TUN: 'tunisia', + TUR: 'turkey', + TKM: 'turkmen', + TCA: 'turks', + TUV: 'tuvalu', + UGA: 'uganda', + UKR: 'ukrain', + ARE: 'emirates|^u\\.?a\\.?e\\.?$|united.?arab.?em', + GBR: 'united.?kingdom|britain|^u\\.?k\\.?$', + TZA: 'tanzania', + USA: 'united.?states\\b(?!.*islands)|\\bu\\.?s\\.?a\\.?\\b|^\\s*u\\.?s\\.?\\b(?!.*islands)', + UMI: 'minor.?outlying.?is', + URY: 'uruguay', + UZB: 'uzbek', + VUT: 'vanuatu|new.?hebrides', + VEN: 'venezuela', + VNM: '^(?!.*republic).*viet.?nam|^(?=.*socialist).*viet.?nam', + VGB: '^(?=.*\\bu\\.?\\s?k).*virgin|^(?=.*brit).*virgin|^(?=.*kingdom).*virgin', + VIR: '^(?=.*\\bu\\.?\\s?s).*virgin|^(?=.*states).*virgin', + WLF: 'futuna|wallis', + ESH: 'western.sahara', + YEM: '^(?!.*arab)(?!.*north)(?!.*sana)(?!.*peo)(?!.*dem)(?!.*south)(?!.*aden)(?!.*\\bp\\.?d\\.?r).*yemen', + YMD: '^(?=.*peo).*yemen|^(?!.*rep)(?=.*dem).*yemen|^(?=.*south).*yemen|^(?=.*aden).*yemen|^(?=.*\\bp\\.?d\\.?r).*yemen', + YUG: 'yugoslavia', + ZMB: 'zambia|northern.?rhodesia', + EAZ: 'zanzibar', + ZWE: 'zimbabwe|^(?!.*northern).*rhodesia' } -},{}],68:[function(require,module,exports){ -(function (process,global){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. -var formatRegExp = /%[sdj%]/g; -exports.format = function(f) { - if (!isString(f)) { - var objects = []; - for (var i = 0; i < arguments.length; i++) { - objects.push(inspect(arguments[i])); +},{}],80:[function(require,module,exports){ +// (c) Dean McNamee , 2012. +// +// https://github.com/deanm/css-color-parser-js +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +// http://www.w3.org/TR/css3-color/ +var kCSSColorTable = { + "transparent": [0,0,0,0], "aliceblue": [240,248,255,1], + "antiquewhite": [250,235,215,1], "aqua": [0,255,255,1], + "aquamarine": [127,255,212,1], "azure": [240,255,255,1], + "beige": [245,245,220,1], "bisque": [255,228,196,1], + "black": [0,0,0,1], "blanchedalmond": [255,235,205,1], + "blue": [0,0,255,1], "blueviolet": [138,43,226,1], + "brown": [165,42,42,1], "burlywood": [222,184,135,1], + "cadetblue": [95,158,160,1], "chartreuse": [127,255,0,1], + "chocolate": [210,105,30,1], "coral": [255,127,80,1], + "cornflowerblue": [100,149,237,1], "cornsilk": [255,248,220,1], + "crimson": [220,20,60,1], "cyan": [0,255,255,1], + "darkblue": [0,0,139,1], "darkcyan": [0,139,139,1], + "darkgoldenrod": [184,134,11,1], "darkgray": [169,169,169,1], + "darkgreen": [0,100,0,1], "darkgrey": [169,169,169,1], + "darkkhaki": [189,183,107,1], "darkmagenta": [139,0,139,1], + "darkolivegreen": [85,107,47,1], "darkorange": [255,140,0,1], + "darkorchid": [153,50,204,1], "darkred": [139,0,0,1], + "darksalmon": [233,150,122,1], "darkseagreen": [143,188,143,1], + "darkslateblue": [72,61,139,1], "darkslategray": [47,79,79,1], + "darkslategrey": [47,79,79,1], "darkturquoise": [0,206,209,1], + "darkviolet": [148,0,211,1], "deeppink": [255,20,147,1], + "deepskyblue": [0,191,255,1], "dimgray": [105,105,105,1], + "dimgrey": [105,105,105,1], "dodgerblue": [30,144,255,1], + "firebrick": [178,34,34,1], "floralwhite": [255,250,240,1], + "forestgreen": [34,139,34,1], "fuchsia": [255,0,255,1], + "gainsboro": [220,220,220,1], "ghostwhite": [248,248,255,1], + "gold": [255,215,0,1], "goldenrod": [218,165,32,1], + "gray": [128,128,128,1], "green": [0,128,0,1], + "greenyellow": [173,255,47,1], "grey": [128,128,128,1], + "honeydew": [240,255,240,1], "hotpink": [255,105,180,1], + "indianred": [205,92,92,1], "indigo": [75,0,130,1], + "ivory": [255,255,240,1], "khaki": [240,230,140,1], + "lavender": [230,230,250,1], "lavenderblush": [255,240,245,1], + "lawngreen": [124,252,0,1], "lemonchiffon": [255,250,205,1], + "lightblue": [173,216,230,1], "lightcoral": [240,128,128,1], + "lightcyan": [224,255,255,1], "lightgoldenrodyellow": [250,250,210,1], + "lightgray": [211,211,211,1], "lightgreen": [144,238,144,1], + "lightgrey": [211,211,211,1], "lightpink": [255,182,193,1], + "lightsalmon": [255,160,122,1], "lightseagreen": [32,178,170,1], + "lightskyblue": [135,206,250,1], "lightslategray": [119,136,153,1], + "lightslategrey": [119,136,153,1], "lightsteelblue": [176,196,222,1], + "lightyellow": [255,255,224,1], "lime": [0,255,0,1], + "limegreen": [50,205,50,1], "linen": [250,240,230,1], + "magenta": [255,0,255,1], "maroon": [128,0,0,1], + "mediumaquamarine": [102,205,170,1], "mediumblue": [0,0,205,1], + "mediumorchid": [186,85,211,1], "mediumpurple": [147,112,219,1], + "mediumseagreen": [60,179,113,1], "mediumslateblue": [123,104,238,1], + "mediumspringgreen": [0,250,154,1], "mediumturquoise": [72,209,204,1], + "mediumvioletred": [199,21,133,1], "midnightblue": [25,25,112,1], + "mintcream": [245,255,250,1], "mistyrose": [255,228,225,1], + "moccasin": [255,228,181,1], "navajowhite": [255,222,173,1], + "navy": [0,0,128,1], "oldlace": [253,245,230,1], + "olive": [128,128,0,1], "olivedrab": [107,142,35,1], + "orange": [255,165,0,1], "orangered": [255,69,0,1], + "orchid": [218,112,214,1], "palegoldenrod": [238,232,170,1], + "palegreen": [152,251,152,1], "paleturquoise": [175,238,238,1], + "palevioletred": [219,112,147,1], "papayawhip": [255,239,213,1], + "peachpuff": [255,218,185,1], "peru": [205,133,63,1], + "pink": [255,192,203,1], "plum": [221,160,221,1], + "powderblue": [176,224,230,1], "purple": [128,0,128,1], + "rebeccapurple": [102,51,153,1], + "red": [255,0,0,1], "rosybrown": [188,143,143,1], + "royalblue": [65,105,225,1], "saddlebrown": [139,69,19,1], + "salmon": [250,128,114,1], "sandybrown": [244,164,96,1], + "seagreen": [46,139,87,1], "seashell": [255,245,238,1], + "sienna": [160,82,45,1], "silver": [192,192,192,1], + "skyblue": [135,206,235,1], "slateblue": [106,90,205,1], + "slategray": [112,128,144,1], "slategrey": [112,128,144,1], + "snow": [255,250,250,1], "springgreen": [0,255,127,1], + "steelblue": [70,130,180,1], "tan": [210,180,140,1], + "teal": [0,128,128,1], "thistle": [216,191,216,1], + "tomato": [255,99,71,1], "turquoise": [64,224,208,1], + "violet": [238,130,238,1], "wheat": [245,222,179,1], + "white": [255,255,255,1], "whitesmoke": [245,245,245,1], + "yellow": [255,255,0,1], "yellowgreen": [154,205,50,1]} + +function clamp_css_byte(i) { // Clamp to integer 0 .. 255. + i = Math.round(i); // Seems to be what Chrome does (vs truncation). + return i < 0 ? 0 : i > 255 ? 255 : i; +} + +function clamp_css_float(f) { // Clamp to float 0.0 .. 1.0. + return f < 0 ? 0 : f > 1 ? 1 : f; +} + +function parse_css_int(str) { // int or percentage. + if (str[str.length - 1] === '%') + return clamp_css_byte(parseFloat(str) / 100 * 255); + return clamp_css_byte(parseInt(str)); +} + +function parse_css_float(str) { // float or percentage. + if (str[str.length - 1] === '%') + return clamp_css_float(parseFloat(str) / 100); + return clamp_css_float(parseFloat(str)); +} + +function css_hue_to_rgb(m1, m2, h) { + if (h < 0) h += 1; + else if (h > 1) h -= 1; + + if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; + if (h * 2 < 1) return m2; + if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6; + return m1; +} + +function parseCSSColor(css_str) { + // Remove all whitespace, not compliant, but should just be more accepting. + var str = css_str.replace(/ /g, '').toLowerCase(); + + // Color keywords (and transparent) lookup. + if (str in kCSSColorTable) return kCSSColorTable[str].slice(); // dup. + + // #abc and #abc123 syntax. + if (str[0] === '#') { + if (str.length === 4) { + var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. + if (!(iv >= 0 && iv <= 0xfff)) return null; // Covers NaN. + return [((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8), + (iv & 0xf0) | ((iv & 0xf0) >> 4), + (iv & 0xf) | ((iv & 0xf) << 4), + 1]; + } else if (str.length === 7) { + var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. + if (!(iv >= 0 && iv <= 0xffffff)) return null; // Covers NaN. + return [(iv & 0xff0000) >> 16, + (iv & 0xff00) >> 8, + iv & 0xff, + 1]; } - return objects.join(' '); + + return null; } - var i = 1; - var args = arguments; - var len = args.length; - var str = String(f).replace(formatRegExp, function(x) { - if (x === '%%') return '%'; - if (i >= len) return x; - switch (x) { - case '%s': return String(args[i++]); - case '%d': return Number(args[i++]); - case '%j': - try { - return JSON.stringify(args[i++]); - } catch (_) { - return '[Circular]'; - } + var op = str.indexOf('('), ep = str.indexOf(')'); + if (op !== -1 && ep + 1 === str.length) { + var fname = str.substr(0, op); + var params = str.substr(op+1, ep-(op+1)).split(','); + var alpha = 1; // To allow case fallthrough. + switch (fname) { + case 'rgba': + if (params.length !== 4) return null; + alpha = parse_css_float(params.pop()); + // Fall through. + case 'rgb': + if (params.length !== 3) return null; + return [parse_css_int(params[0]), + parse_css_int(params[1]), + parse_css_int(params[2]), + alpha]; + case 'hsla': + if (params.length !== 4) return null; + alpha = parse_css_float(params.pop()); + // Fall through. + case 'hsl': + if (params.length !== 3) return null; + var h = (((parseFloat(params[0]) % 360) + 360) % 360) / 360; // 0 .. 1 + // NOTE(deanm): According to the CSS spec s/l should only be + // percentages, but we don't bother and let float or percentage. + var s = parse_css_float(params[1]); + var l = parse_css_float(params[2]); + var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s; + var m1 = l * 2 - m2; + return [clamp_css_byte(css_hue_to_rgb(m1, m2, h+1/3) * 255), + clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255), + clamp_css_byte(css_hue_to_rgb(m1, m2, h-1/3) * 255), + alpha]; default: - return x; - } - }); - for (var x = args[i]; i < len; x = args[++i]) { - if (isNull(x) || !isObject(x)) { - str += ' ' + x; - } else { - str += ' ' + inspect(x); - } - } - return str; -}; - - -// Mark that a method should not be used. -// Returns a modified function which warns once by default. -// If --no-deprecation is set, then it is a no-op. -exports.deprecate = function(fn, msg) { - // Allow for deprecating things in the process of starting up. - if (isUndefined(global.process)) { - return function() { - return exports.deprecate(fn, msg).apply(this, arguments); - }; - } - - if (process.noDeprecation === true) { - return fn; - } - - var warned = false; - function deprecated() { - if (!warned) { - if (process.throwDeprecation) { - throw new Error(msg); - } else if (process.traceDeprecation) { - console.trace(msg); - } else { - console.error(msg); - } - warned = true; - } - return fn.apply(this, arguments); - } - - return deprecated; -}; - - -var debugs = {}; -var debugEnviron; -exports.debuglog = function(set) { - if (isUndefined(debugEnviron)) - debugEnviron = process.env.NODE_DEBUG || ''; - set = set.toUpperCase(); - if (!debugs[set]) { - if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { - var pid = process.pid; - debugs[set] = function() { - var msg = exports.format.apply(exports, arguments); - console.error('%s %d: %s', set, pid, msg); - }; - } else { - debugs[set] = function() {}; - } - } - return debugs[set]; -}; - - -/** - * Echos the value of a value. Trys to print the value out - * in the best way possible given the different types. - * - * @param {Object} obj The object to print out. - * @param {Object} opts Optional options object that alters the output. - */ -/* legacy: obj, showHidden, depth, colors*/ -function inspect(obj, opts) { - // default options - var ctx = { - seen: [], - stylize: stylizeNoColor - }; - // legacy... - if (arguments.length >= 3) ctx.depth = arguments[2]; - if (arguments.length >= 4) ctx.colors = arguments[3]; - if (isBoolean(opts)) { - // legacy... - ctx.showHidden = opts; - } else if (opts) { - // got an "options" object - exports._extend(ctx, opts); - } - // set default options - if (isUndefined(ctx.showHidden)) ctx.showHidden = false; - if (isUndefined(ctx.depth)) ctx.depth = 2; - if (isUndefined(ctx.colors)) ctx.colors = false; - if (isUndefined(ctx.customInspect)) ctx.customInspect = true; - if (ctx.colors) ctx.stylize = stylizeWithColor; - return formatValue(ctx, obj, ctx.depth); -} -exports.inspect = inspect; - - -// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics -inspect.colors = { - 'bold' : [1, 22], - 'italic' : [3, 23], - 'underline' : [4, 24], - 'inverse' : [7, 27], - 'white' : [37, 39], - 'grey' : [90, 39], - 'black' : [30, 39], - 'blue' : [34, 39], - 'cyan' : [36, 39], - 'green' : [32, 39], - 'magenta' : [35, 39], - 'red' : [31, 39], - 'yellow' : [33, 39] -}; - -// Don't use 'blue' not visible on cmd.exe -inspect.styles = { - 'special': 'cyan', - 'number': 'yellow', - 'boolean': 'yellow', - 'undefined': 'grey', - 'null': 'bold', - 'string': 'green', - 'date': 'magenta', - // "name": intentionally not styling - 'regexp': 'red' -}; - - -function stylizeWithColor(str, styleType) { - var style = inspect.styles[styleType]; - - if (style) { - return '\u001b[' + inspect.colors[style][0] + 'm' + str + - '\u001b[' + inspect.colors[style][1] + 'm'; - } else { - return str; - } -} - - -function stylizeNoColor(str, styleType) { - return str; -} - - -function arrayToHash(array) { - var hash = {}; - - array.forEach(function(val, idx) { - hash[val] = true; - }); - - return hash; -} - - -function formatValue(ctx, value, recurseTimes) { - // Provide a hook for user-specified inspect functions. - // Check that value is an object with an inspect function on it - if (ctx.customInspect && - value && - isFunction(value.inspect) && - // Filter out the util module, it's inspect function is special - value.inspect !== exports.inspect && - // Also filter out any prototype objects using the circular check. - !(value.constructor && value.constructor.prototype === value)) { - var ret = value.inspect(recurseTimes, ctx); - if (!isString(ret)) { - ret = formatValue(ctx, ret, recurseTimes); - } - return ret; - } - - // Primitive types cannot have properties - var primitive = formatPrimitive(ctx, value); - if (primitive) { - return primitive; - } - - // Look up the keys of the object. - var keys = Object.keys(value); - var visibleKeys = arrayToHash(keys); - - if (ctx.showHidden) { - keys = Object.getOwnPropertyNames(value); - } - - // IE doesn't make error fields non-enumerable - // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx - if (isError(value) - && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { - return formatError(value); - } - - // Some type of object without properties can be shortcutted. - if (keys.length === 0) { - if (isFunction(value)) { - var name = value.name ? ': ' + value.name : ''; - return ctx.stylize('[Function' + name + ']', 'special'); - } - if (isRegExp(value)) { - return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); - } - if (isDate(value)) { - return ctx.stylize(Date.prototype.toString.call(value), 'date'); - } - if (isError(value)) { - return formatError(value); + return null; } } - var base = '', array = false, braces = ['{', '}']; - - // Make Array say that they are Array - if (isArray(value)) { - array = true; - braces = ['[', ']']; - } - - // Make functions say that they are functions - if (isFunction(value)) { - var n = value.name ? ': ' + value.name : ''; - base = ' [Function' + n + ']'; - } - - // Make RegExps say that they are RegExps - if (isRegExp(value)) { - base = ' ' + RegExp.prototype.toString.call(value); - } - - // Make dates with properties first say the date - if (isDate(value)) { - base = ' ' + Date.prototype.toUTCString.call(value); - } - - // Make error with message first say the error - if (isError(value)) { - base = ' ' + formatError(value); - } - - if (keys.length === 0 && (!array || value.length == 0)) { - return braces[0] + base + braces[1]; - } - - if (recurseTimes < 0) { - if (isRegExp(value)) { - return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); - } else { - return ctx.stylize('[Object]', 'special'); - } - } - - ctx.seen.push(value); - - var output; - if (array) { - output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); - } else { - output = keys.map(function(key) { - return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); - }); - } - - ctx.seen.pop(); - - return reduceToSingleString(output, base, braces); + return null; } +try { exports.parseCSSColor = parseCSSColor } catch(e) { } -function formatPrimitive(ctx, value) { - if (isUndefined(value)) - return ctx.stylize('undefined', 'undefined'); - if (isString(value)) { - var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') - .replace(/'/g, "\\'") - .replace(/\\"/g, '"') + '\''; - return ctx.stylize(simple, 'string'); - } - if (isNumber(value)) - return ctx.stylize('' + value, 'number'); - if (isBoolean(value)) - return ctx.stylize('' + value, 'boolean'); - // For some reason typeof null is "object", so special case here. - if (isNull(value)) - return ctx.stylize('null', 'null'); -} - - -function formatError(value) { - return '[' + Error.prototype.toString.call(value) + ']'; -} - - -function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { - var output = []; - for (var i = 0, l = value.length; i < l; ++i) { - if (hasOwnProperty(value, String(i))) { - output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, - String(i), true)); - } else { - output.push(''); - } - } - keys.forEach(function(key) { - if (!key.match(/^\d+$/)) { - output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, - key, true)); - } - }); - return output; -} - - -function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { - var name, str, desc; - desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; - if (desc.get) { - if (desc.set) { - str = ctx.stylize('[Getter/Setter]', 'special'); - } else { - str = ctx.stylize('[Getter]', 'special'); - } - } else { - if (desc.set) { - str = ctx.stylize('[Setter]', 'special'); - } - } - if (!hasOwnProperty(visibleKeys, key)) { - name = '[' + key + ']'; - } - if (!str) { - if (ctx.seen.indexOf(desc.value) < 0) { - if (isNull(recurseTimes)) { - str = formatValue(ctx, desc.value, null); - } else { - str = formatValue(ctx, desc.value, recurseTimes - 1); - } - if (str.indexOf('\n') > -1) { - if (array) { - str = str.split('\n').map(function(line) { - return ' ' + line; - }).join('\n').substr(2); - } else { - str = '\n' + str.split('\n').map(function(line) { - return ' ' + line; - }).join('\n'); - } - } - } else { - str = ctx.stylize('[Circular]', 'special'); - } - } - if (isUndefined(name)) { - if (array && key.match(/^\d+$/)) { - return str; - } - name = JSON.stringify('' + key); - if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { - name = name.substr(1, name.length - 2); - name = ctx.stylize(name, 'name'); - } else { - name = name.replace(/'/g, "\\'") - .replace(/\\"/g, '"') - .replace(/(^"|"$)/g, "'"); - name = ctx.stylize(name, 'string'); - } - } - - return name + ': ' + str; -} - - -function reduceToSingleString(output, base, braces) { - var numLinesEst = 0; - var length = output.reduce(function(prev, cur) { - numLinesEst++; - if (cur.indexOf('\n') >= 0) numLinesEst++; - return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; - }, 0); - - if (length > 60) { - return braces[0] + - (base === '' ? '' : base + '\n ') + - ' ' + - output.join(',\n ') + - ' ' + - braces[1]; - } - - return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; -} - - -// NOTE: These type checking functions intentionally don't use `instanceof` -// because it is fragile and can be easily faked with `Object.create()`. -function isArray(ar) { - return Array.isArray(ar); -} -exports.isArray = isArray; - -function isBoolean(arg) { - return typeof arg === 'boolean'; -} -exports.isBoolean = isBoolean; - -function isNull(arg) { - return arg === null; -} -exports.isNull = isNull; - -function isNullOrUndefined(arg) { - return arg == null; -} -exports.isNullOrUndefined = isNullOrUndefined; - -function isNumber(arg) { - return typeof arg === 'number'; -} -exports.isNumber = isNumber; - -function isString(arg) { - return typeof arg === 'string'; -} -exports.isString = isString; - -function isSymbol(arg) { - return typeof arg === 'symbol'; -} -exports.isSymbol = isSymbol; - -function isUndefined(arg) { - return arg === void 0; -} -exports.isUndefined = isUndefined; - -function isRegExp(re) { - return isObject(re) && objectToString(re) === '[object RegExp]'; -} -exports.isRegExp = isRegExp; - -function isObject(arg) { - return typeof arg === 'object' && arg !== null; -} -exports.isObject = isObject; - -function isDate(d) { - return isObject(d) && objectToString(d) === '[object Date]'; -} -exports.isDate = isDate; - -function isError(e) { - return isObject(e) && - (objectToString(e) === '[object Error]' || e instanceof Error); -} -exports.isError = isError; - -function isFunction(arg) { - return typeof arg === 'function'; -} -exports.isFunction = isFunction; - -function isPrimitive(arg) { - return arg === null || - typeof arg === 'boolean' || - typeof arg === 'number' || - typeof arg === 'string' || - typeof arg === 'symbol' || // ES6 symbol - typeof arg === 'undefined'; -} -exports.isPrimitive = isPrimitive; - -exports.isBuffer = require('./support/isBuffer'); - -function objectToString(o) { - return Object.prototype.toString.call(o); -} - - -function pad(n) { - return n < 10 ? '0' + n.toString(10) : n.toString(10); -} - - -var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', - 'Oct', 'Nov', 'Dec']; - -// 26 Feb 16:19:34 -function timestamp() { - var d = new Date(); - var time = [pad(d.getHours()), - pad(d.getMinutes()), - pad(d.getSeconds())].join(':'); - return [d.getDate(), months[d.getMonth()], time].join(' '); -} - - -// log is just a thin wrapper to console.log that prepends a timestamp -exports.log = function() { - console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); -}; - - -/** - * Inherit the prototype methods from one constructor into another. - * - * The Function.prototype.inherits from lang.js rewritten as a standalone - * function (not on Function.prototype). NOTE: If this file is to be loaded - * during bootstrapping this function needs to be rewritten using some native - * functions as prototype setup using normal JavaScript does not work as - * expected during bootstrapping (see mirror.js in r114903). - * - * @param {function} ctor Constructor function which needs to inherit the - * prototype. - * @param {function} superCtor Constructor function to inherit prototype from. - */ -exports.inherits = require('inherits'); - -exports._extend = function(origin, add) { - // Don't do anything if add isn't an object - if (!add || !isObject(add)) return origin; - - var keys = Object.keys(add); - var i = keys.length; - while (i--) { - origin[keys[i]] = add[keys[i]]; - } - return origin; -}; - -function hasOwnProperty(obj, prop) { - return Object.prototype.hasOwnProperty.call(obj, prop); -} - -}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{"./support/isBuffer":67,"_process":41,"inherits":66}],69:[function(require,module,exports){ -(function (Buffer,process){ -'use strict' - -var path = require('path') -var ndarray = require('ndarray') -var GifReader = require('omggif').GifReader -var pack = require('ndarray-pack') -var through = require('through') -var parseDataURI = require('data-uri-to-buffer') - -function defaultImage(url, cb) { - var img = new Image() - img.crossOrigin = "Anonymous" - img.onload = function() { - var canvas = document.createElement('canvas') - canvas.width = img.width - canvas.height = img.height - var context = canvas.getContext('2d') - context.drawImage(img, 0, 0) - var pixels = context.getImageData(0, 0, img.width, img.height) - cb(null, ndarray(new Uint8Array(pixels.data), [img.width, img.height, 4], [4, 4*img.width, 1], 0)) - } - img.onerror = function(err) { - cb(err) - } - img.src = url -} - -//Animated gif loading -function handleGif(data, cb) { - var reader - try { - reader = new GifReader(data) - } catch(err) { - cb(err) - return - } - if(reader.numFrames() > 0) { - var nshape = [reader.numFrames(), reader.height, reader.width, 4] - var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3]) - var result = ndarray(ndata, nshape) - try { - for(var i=0; i=0; --i) { + f[i] = dh00*p0[i] + dh10*v0[i] + dh01*p1[i] + dh11*v1[i] + } + return f } - if(shape.length === 0) { - return ndarray() - } - if(!result) { - result = ndarray(new Float64Array(sz), shape) - } - do_convert(result, arr) - return result + return dh00*p0 + dh10*v0 + dh01*p1[i] + dh11*v1 } -},{"./doConvert.js":72,"ndarray":77}],72:[function(require,module,exports){ -module.exports=require('cwise-compiler')({"args":["array","scalar","index"],"pre":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"body":{"body":"{\nvar _inline_1_v=_inline_1_arg1_,_inline_1_i\nfor(_inline_1_i=0;_inline_1_i<_inline_1_arg2_.length-1;++_inline_1_i) {\n_inline_1_v=_inline_1_v[_inline_1_arg2_[_inline_1_i]]\n}\n_inline_1_arg0_=_inline_1_v[_inline_1_arg2_[_inline_1_arg2_.length-1]]\n}","args":[{"name":"_inline_1_arg0_","lvalue":true,"rvalue":false,"count":1},{"name":"_inline_1_arg1_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_1_arg2_","lvalue":false,"rvalue":true,"count":4}],"thisVars":[],"localVars":["_inline_1_i","_inline_1_v"]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"funcName":"convert","blockSize":64}) +function cubicHermite(p0, v0, p1, v1, t, f) { + var ti = (t-1), t2 = t*t, ti2 = ti*ti, + h00 = (1+2*t)*ti2, + h10 = t*ti2, + h01 = t2*(3-2*t), + h11 = t2*ti + if(p0.length) { + if(!f) { + f = new Array(p0.length) + } + for(var i=p0.length-1; i>=0; --i) { + f[i] = h00*p0[i] + h10*v0[i] + h01*p1[i] + h11*v1[i] + } + return f + } + return h00*p0 + h10*v0 + h01*p1 + h11*v1 +} -},{"cwise-compiler":73}],73:[function(require,module,exports){ +module.exports = cubicHermite +module.exports.derivative = dcubicHermite +},{}],82:[function(require,module,exports){ "use strict" var createThunk = require("./lib/thunk.js") @@ -14810,7 +14260,7 @@ function compileCwise(user_args) { module.exports = compileCwise -},{"./lib/thunk.js":75}],74:[function(require,module,exports){ +},{"./lib/thunk.js":84}],83:[function(require,module,exports){ "use strict" var uniq = require("uniq") @@ -15166,7 +14616,7 @@ function generateCWiseOp(proc, typesig) { } module.exports = generateCWiseOp -},{"uniq":76}],75:[function(require,module,exports){ +},{"uniq":994}],84:[function(require,module,exports){ "use strict" // The function below is called when constructing a cwise function object, and does the following: @@ -15254,1327 +14704,10622 @@ function createThunk(proc) { module.exports = createThunk -},{"./compile.js":74}],76:[function(require,module,exports){ -"use strict" - -function unique_pred(list, compare) { - var ptr = 1 - , len = list.length - , a=list[0], b=list[0] - for(var i=1; i b ? 1 : a >= b ? 0 : NaN; + } + d3.descending = function(a, b) { + return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN; + }; + d3.min = function(array, f) { + var i = -1, n = array.length, a, b; + if (arguments.length === 1) { + while (++i < n) if ((b = array[i]) != null && b >= b) { + a = b; + break; } - list[ptr++] = a - } - } - list.length = ptr - return list -} - -function unique_eq(list) { - var ptr = 1 - , len = list.length - , a=list[0], b = list[0] - for(var i=1; i b) a = b; + } else { + while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { + a = b; + break; } - list[ptr++] = a + while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b; } - } - list.length = ptr - return list -} - -function unique(list, compare, sorted) { - if(list.length === 0) { - return list - } - if(compare) { - if(!sorted) { - list.sort(compare) + return a; + }; + d3.max = function(array, f) { + var i = -1, n = array.length, a, b; + if (arguments.length === 1) { + while (++i < n) if ((b = array[i]) != null && b >= b) { + a = b; + break; + } + while (++i < n) if ((b = array[i]) != null && b > a) a = b; + } else { + while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { + a = b; + break; + } + while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; } - return unique_pred(list, compare) - } - if(!sorted) { - list.sort() - } - return unique_eq(list) -} - -module.exports = unique - -},{}],77:[function(require,module,exports){ -var iota = require("iota-array") -var isBuffer = require("is-buffer") - -var hasTypedArrays = ((typeof Float64Array) !== "undefined") - -function compare1st(a, b) { - return a[0] - b[0] -} - -function order() { - var stride = this.stride - var terms = new Array(stride.length) - var i - for(i=0; iMath.abs(this.stride[1]))?[1,0]:[0,1]}})") - } else if(dimension === 3) { - code.push( -"var s0=Math.abs(this.stride[0]),s1=Math.abs(this.stride[1]),s2=Math.abs(this.stride[2]);\ -if(s0>s1){\ -if(s1>s2){\ -return [2,1,0];\ -}else if(s0>s2){\ -return [1,2,0];\ -}else{\ -return [1,0,2];\ -}\ -}else if(s0>s2){\ -return [2,0,1];\ -}else if(s2>s1){\ -return [0,1,2];\ -}else{\ -return [0,2,1];\ -}}})") + return a; + }; + d3.extent = function(array, f) { + var i = -1, n = array.length, a, b, c; + if (arguments.length === 1) { + while (++i < n) if ((b = array[i]) != null && b >= b) { + a = c = b; + break; + } + while (++i < n) if ((b = array[i]) != null) { + if (a > b) a = b; + if (c < b) c = b; } } else { - code.push("ORDER})") - } - } - - //view.set(i0, ..., v): - code.push( -"proto.set=function "+className+"_set("+args.join(",")+",v){") - if(useGetters) { - code.push("return this.data.set("+index_str+",v)}") - } else { - code.push("return this.data["+index_str+"]=v}") - } - - //view.get(i0, ...): - code.push("proto.get=function "+className+"_get("+args.join(",")+"){") - if(useGetters) { - code.push("return this.data.get("+index_str+")}") - } else { - code.push("return this.data["+index_str+"]}") - } - - //view.index: - code.push( - "proto.index=function "+className+"_index(", args.join(), "){return "+index_str+"}") - - //view.hi(): - code.push("proto.hi=function "+className+"_hi("+args.join(",")+"){return new "+className+"(this.data,"+ - indices.map(function(i) { - return ["(typeof i",i,"!=='number'||i",i,"<0)?this.shape[", i, "]:i", i,"|0"].join("") - }).join(",")+","+ - indices.map(function(i) { - return "this.stride["+i + "]" - }).join(",")+",this.offset)}") - - //view.lo(): - var a_vars = indices.map(function(i) { return "a"+i+"=this.shape["+i+"]" }) - var c_vars = indices.map(function(i) { return "c"+i+"=this.stride["+i+"]" }) - code.push("proto.lo=function "+className+"_lo("+args.join(",")+"){var b=this.offset,d=0,"+a_vars.join(",")+","+c_vars.join(",")) - for(var i=0; i=0){\ -d=i"+i+"|0;\ -b+=c"+i+"*d;\ -a"+i+"-=d}") - } - code.push("return new "+className+"(this.data,"+ - indices.map(function(i) { - return "a"+i - }).join(",")+","+ - indices.map(function(i) { - return "c"+i - }).join(",")+",b)}") - - //view.step(): - code.push("proto.step=function "+className+"_step("+args.join(",")+"){var "+ - indices.map(function(i) { - return "a"+i+"=this.shape["+i+"]" - }).join(",")+","+ - indices.map(function(i) { - return "b"+i+"=this.stride["+i+"]" - }).join(",")+",c=this.offset,d=0,ceil=Math.ceil") - for(var i=0; i=0){c=(c+this.stride["+i+"]*i"+i+")|0}else{a.push(this.shape["+i+"]);b.push(this.stride["+i+"])}") - } - code.push("var ctor=CTOR_LIST[a.length+1];return ctor(this.data,a,b,c)}") - - //Add return statement - code.push("return function construct_"+className+"(data,shape,stride,offset){return new "+className+"(data,"+ - indices.map(function(i) { - return "shape["+i+"]" - }).join(",")+","+ - indices.map(function(i) { - return "stride["+i+"]" - }).join(",")+",offset)}") - - //Compile procedure - var procedure = new Function("CTOR_LIST", "ORDER", code.join("\n")) - return procedure(CACHED_CONSTRUCTORS[dtype], order) -} - -function arrayDType(data) { - if(isBuffer(data)) { - return "buffer" - } - if(hasTypedArrays) { - switch(Object.prototype.toString.call(data)) { - case "[object Float64Array]": - return "float64" - case "[object Float32Array]": - return "float32" - case "[object Int8Array]": - return "int8" - case "[object Int16Array]": - return "int16" - case "[object Int32Array]": - return "int32" - case "[object Uint8Array]": - return "uint8" - case "[object Uint16Array]": - return "uint16" - case "[object Uint32Array]": - return "uint32" - case "[object Uint8ClampedArray]": - return "uint8_clamped" - } - } - if(Array.isArray(data)) { - return "array" - } - return "generic" -} - -var CACHED_CONSTRUCTORS = { - "float32":[], - "float64":[], - "int8":[], - "int16":[], - "int32":[], - "uint8":[], - "uint16":[], - "uint32":[], - "array":[], - "uint8_clamped":[], - "buffer":[], - "generic":[] -} - -;(function() { - for(var id in CACHED_CONSTRUCTORS) { - CACHED_CONSTRUCTORS[id].push(compileConstructor(id, -1)) - } -}); - -function wrappedNDArrayCtor(data, shape, stride, offset) { - if(data === undefined) { - var ctor = CACHED_CONSTRUCTORS.array[0] - return ctor([]) - } else if(typeof data === "number") { - data = [data] - } - if(shape === undefined) { - shape = [ data.length ] - } - var d = shape.length - if(stride === undefined) { - stride = new Array(d) - for(var i=d-1, sz=1; i>=0; --i) { - stride[i] = sz - sz *= shape[i] - } - } - if(offset === undefined) { - offset = 0 - for(var i=0; i= b) { + a = c = b; + break; + } + while (++i < n) if ((b = f.call(array, array[i], i)) != null) { + if (a > b) a = b; + if (c < b) c = b; } } - } - var dtype = arrayDType(data) - var ctor_list = CACHED_CONSTRUCTORS[dtype] - while(ctor_list.length <= d+1) { - ctor_list.push(compileConstructor(dtype, ctor_list.length-1)) - } - var ctor = ctor_list[d+1] - return ctor(data, shape, stride, offset) -} - -module.exports = wrappedNDArrayCtor - -},{"iota-array":78,"is-buffer":79}],78:[function(require,module,exports){ -"use strict" - -function iota(n) { - var result = new Array(n) - for(var i=0; i, 2013. -// -// https://github.com/deanm/omggif -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. -// -// omggif is a JavaScript implementation of a GIF 89a encoder and decoder, -// including animation and compression. It does not rely on any specific -// underlying system, so should run in the browser, Node, or Plask. - -function GifWriter(buf, width, height, gopts) { - var p = 0; - - var gopts = gopts === undefined ? { } : gopts; - var loop_count = gopts.loop === undefined ? null : gopts.loop; - var global_palette = gopts.palette === undefined ? null : gopts.palette; - - if (width <= 0 || height <= 0 || width > 65535 || height > 65535) - throw "Width/Height invalid." - - function check_palette_and_num_colors(palette) { - var num_colors = palette.length; - if (num_colors < 2 || num_colors > 256 || num_colors & (num_colors-1)) - throw "Invalid code/color length, must be power of 2 and 2 .. 256."; - return num_colors; - } - - // - Header. - buf[p++] = 0x47; buf[p++] = 0x49; buf[p++] = 0x46; // GIF - buf[p++] = 0x38; buf[p++] = 0x39; buf[p++] = 0x61; // 89a - - // Handling of Global Color Table (palette) and background index. - var gp_num_colors_pow2 = 0; - var background = 0; - if (global_palette !== null) { - var gp_num_colors = check_palette_and_num_colors(global_palette); - while (gp_num_colors >>= 1) ++gp_num_colors_pow2; - gp_num_colors = 1 << gp_num_colors_pow2; - --gp_num_colors_pow2; - if (gopts.background !== undefined) { - background = gopts.background; - if (background >= gp_num_colors) throw "Background index out of range."; - // The GIF spec states that a background index of 0 should be ignored, so - // this is probably a mistake and you really want to set it to another - // slot in the palette. But actually in the end most browsers, etc end - // up ignoring this almost completely (including for dispose background). - if (background === 0) - throw "Background index explicitly passed as 0."; - } - } - - // - Logical Screen Descriptor. - // NOTE(deanm): w/h apparently ignored by implementations, but set anyway. - buf[p++] = width & 0xff; buf[p++] = width >> 8 & 0xff; - buf[p++] = height & 0xff; buf[p++] = height >> 8 & 0xff; - // NOTE: Indicates 0-bpp original color resolution (unused?). - buf[p++] = (global_palette !== null ? 0x80 : 0) | // Global Color Table Flag. - gp_num_colors_pow2; // NOTE: No sort flag (unused?). - buf[p++] = background; // Background Color Index. - buf[p++] = 0; // Pixel aspect ratio (unused?). - - // - Global Color Table - if (global_palette !== null) { - for (var i = 0, il = global_palette.length; i < il; ++i) { - var rgb = global_palette[i]; - buf[p++] = rgb >> 16 & 0xff; - buf[p++] = rgb >> 8 & 0xff; - buf[p++] = rgb & 0xff; - } - } - - if (loop_count !== null) { // Netscape block for looping. - if (loop_count < 0 || loop_count > 65535) - throw "Loop count invalid." - // Extension code, label, and length. - buf[p++] = 0x21; buf[p++] = 0xff; buf[p++] = 0x0b; - // NETSCAPE2.0 - buf[p++] = 0x4e; buf[p++] = 0x45; buf[p++] = 0x54; buf[p++] = 0x53; - buf[p++] = 0x43; buf[p++] = 0x41; buf[p++] = 0x50; buf[p++] = 0x45; - buf[p++] = 0x32; buf[p++] = 0x2e; buf[p++] = 0x30; - // Sub-block - buf[p++] = 0x03; buf[p++] = 0x01; - buf[p++] = loop_count & 0xff; buf[p++] = loop_count >> 8 & 0xff; - buf[p++] = 0x00; // Terminator. - } - - - var ended = false; - - this.addFrame = function(x, y, w, h, indexed_pixels, opts) { - if (ended === true) { --p; ended = false; } // Un-end. - - opts = opts === undefined ? { } : opts; - - // TODO(deanm): Bounds check x, y. Do they need to be within the virtual - // canvas width/height, I imagine? - if (x < 0 || y < 0 || x > 65535 || y > 65535) - throw "x/y invalid." - - if (w <= 0 || h <= 0 || w > 65535 || h > 65535) - throw "Width/Height invalid." - - if (indexed_pixels.length < w * h) - throw "Not enough pixels for the frame size."; - - var using_local_palette = true; - var palette = opts.palette; - if (palette === undefined || palette === null) { - using_local_palette = false; - palette = global_palette; - } - - if (palette === undefined || palette === null) - throw "Must supply either a local or global palette."; - - var num_colors = check_palette_and_num_colors(palette); - - // Compute the min_code_size (power of 2), destroying num_colors. - var min_code_size = 0; - while (num_colors >>= 1) ++min_code_size; - num_colors = 1 << min_code_size; // Now we can easily get it back. - - var delay = opts.delay === undefined ? 0 : opts.delay; - - // From the spec: - // 0 - No disposal specified. The decoder is - // not required to take any action. - // 1 - Do not dispose. The graphic is to be left - // in place. - // 2 - Restore to background color. The area used by the - // graphic must be restored to the background color. - // 3 - Restore to previous. The decoder is required to - // restore the area overwritten by the graphic with - // what was there prior to rendering the graphic. - // 4-7 - To be defined. - // NOTE(deanm): Dispose background doesn't really work, apparently most - // browsers ignore the background palette index and clear to transparency. - var disposal = opts.disposal === undefined ? 0 : opts.disposal; - if (disposal < 0 || disposal > 3) // 4-7 is reserved. - throw "Disposal out of range."; - - var use_transparency = false; - var transparent_index = 0; - if (opts.transparent !== undefined && opts.transparent !== null) { - use_transparency = true; - transparent_index = opts.transparent; - if (transparent_index < 0 || transparent_index >= num_colors) - throw "Transparent color index."; - } - - if (disposal !== 0 || use_transparency || delay !== 0) { - // - Graphics Control Extension - buf[p++] = 0x21; buf[p++] = 0xf9; // Extension / Label. - buf[p++] = 4; // Byte size. - - buf[p++] = disposal << 2 | (use_transparency === true ? 1 : 0); - buf[p++] = delay & 0xff; buf[p++] = delay >> 8 & 0xff; - buf[p++] = transparent_index; // Transparent color index. - buf[p++] = 0; // Block Terminator. - } - - // - Image Descriptor - buf[p++] = 0x2c; // Image Seperator. - buf[p++] = x & 0xff; buf[p++] = x >> 8 & 0xff; // Left. - buf[p++] = y & 0xff; buf[p++] = y >> 8 & 0xff; // Top. - buf[p++] = w & 0xff; buf[p++] = w >> 8 & 0xff; - buf[p++] = h & 0xff; buf[p++] = h >> 8 & 0xff; - // NOTE: No sort flag (unused?). - // TODO(deanm): Support interlace. - buf[p++] = using_local_palette === true ? (0x80 | (min_code_size-1)) : 0; - - // - Local Color Table - if (using_local_palette === true) { - for (var i = 0, il = palette.length; i < il; ++i) { - var rgb = palette[i]; - buf[p++] = rgb >> 16 & 0xff; - buf[p++] = rgb >> 8 & 0xff; - buf[p++] = rgb & 0xff; - } - } - - p = GifWriterOutputLZWCodeStream( - buf, p, min_code_size < 2 ? 2 : min_code_size, indexed_pixels); + return [ a, c ]; }; - - this.end = function() { - if (ended === false) { - buf[p++] = 0x3b; // Trailer. - ended = true; + function d3_number(x) { + return x === null ? NaN : +x; + } + function d3_numeric(x) { + return !isNaN(x); + } + d3.sum = function(array, f) { + var s = 0, n = array.length, a, i = -1; + if (arguments.length === 1) { + while (++i < n) if (d3_numeric(a = +array[i])) s += a; + } else { + while (++i < n) if (d3_numeric(a = +f.call(array, array[i], i))) s += a; } - return p; + return s; }; -} - -// Main compression routine, palette indexes -> LZW code stream. -// |index_stream| must have at least one entry. -function GifWriterOutputLZWCodeStream(buf, p, min_code_size, index_stream) { - buf[p++] = min_code_size; - var cur_subblock = p++; // Pointing at the length field. - - var clear_code = 1 << min_code_size; - var code_mask = clear_code - 1; - var eoi_code = clear_code + 1; - var next_code = eoi_code + 1; - - var cur_code_size = min_code_size + 1; // Number of bits per code. - var cur_shift = 0; - // We have at most 12-bit codes, so we should have to hold a max of 19 - // bits here (and then we would write out). - var cur = 0; - - function emit_bytes_to_buffer(bit_block_size) { - while (cur_shift >= bit_block_size) { - buf[p++] = cur & 0xff; - cur >>= 8; cur_shift -= 8; - if (p === cur_subblock + 256) { // Finished a subblock. - buf[cur_subblock] = 255; - cur_subblock = p++; - } + d3.mean = function(array, f) { + var s = 0, n = array.length, a, i = -1, j = n; + if (arguments.length === 1) { + while (++i < n) if (d3_numeric(a = d3_number(array[i]))) s += a; else --j; + } else { + while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) s += a; else --j; } - } - - function emit_code(c) { - cur |= c << cur_shift; - cur_shift += cur_code_size; - emit_bytes_to_buffer(8); - } - - // I am not an expert on the topic, and I don't want to write a thesis. - // However, it is good to outline here the basic algorithm and the few data - // structures and optimizations here that make this implementation fast. - // The basic idea behind LZW is to build a table of previously seen runs - // addressed by a short id (herein called output code). All data is - // referenced by a code, which represents one or more values from the - // original input stream. All input bytes can be referenced as the same - // value as an output code. So if you didn't want any compression, you - // could more or less just output the original bytes as codes (there are - // some details to this, but it is the idea). In order to achieve - // compression, values greater then the input range (codes can be up to - // 12-bit while input only 8-bit) represent a sequence of previously seen - // inputs. The decompressor is able to build the same mapping while - // decoding, so there is always a shared common knowledge between the - // encoding and decoder, which is also important for "timing" aspects like - // how to handle variable bit width code encoding. - // - // One obvious but very important consequence of the table system is there - // is always a unique id (at most 12-bits) to map the runs. 'A' might be - // 4, then 'AA' might be 10, 'AAA' 11, 'AAAA' 12, etc. This relationship - // can be used for an effecient lookup strategy for the code mapping. We - // need to know if a run has been seen before, and be able to map that run - // to the output code. Since we start with known unique ids (input bytes), - // and then from those build more unique ids (table entries), we can - // continue this chain (almost like a linked list) to always have small - // integer values that represent the current byte chains in the encoder. - // This means instead of tracking the input bytes (AAAABCD) to know our - // current state, we can track the table entry for AAAABC (it is guaranteed - // to exist by the nature of the algorithm) and the next character D. - // Therefor the tuple of (table_entry, byte) is guaranteed to also be - // unique. This allows us to create a simple lookup key for mapping input - // sequences to codes (table indices) without having to store or search - // any of the code sequences. So if 'AAAA' has a table entry of 12, the - // tuple of ('AAAA', K) for any input byte K will be unique, and can be our - // key. This leads to a integer value at most 20-bits, which can always - // fit in an SMI value and be used as a fast sparse array / object key. - - // Output code for the current contents of the index buffer. - var ib_code = index_stream[0] & code_mask; // Load first input index. - var code_table = { }; // Key'd on our 20-bit "tuple". - - emit_code(clear_code); // Spec says first code should be a clear code. - - // First index already loaded, process the rest of the stream. - for (var i = 1, il = index_stream.length; i < il; ++i) { - var k = index_stream[i] & code_mask; - var cur_key = ib_code << 8 | k; // (prev, k) unique tuple. - var cur_code = code_table[cur_key]; // buffer + k. - - // Check if we have to create a new code table entry. - if (cur_code === undefined) { // We don't have buffer + k. - // Emit index buffer (without k). - // This is an inline version of emit_code, because this is the core - // writing routine of the compressor (and V8 cannot inline emit_code - // because it is a closure here in a different context). Additionally - // we can call emit_byte_to_buffer less often, because we can have - // 30-bits (from our 31-bit signed SMI), and we know our codes will only - // be 12-bits, so can safely have 18-bits there without overflow. - // emit_code(ib_code); - cur |= ib_code << cur_shift; - cur_shift += cur_code_size; - while (cur_shift >= 8) { - buf[p++] = cur & 0xff; - cur >>= 8; cur_shift -= 8; - if (p === cur_subblock + 256) { // Finished a subblock. - buf[cur_subblock] = 255; - cur_subblock = p++; + if (j) return s / j; + }; + d3.quantile = function(values, p) { + var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h; + return e ? v + e * (values[h] - v) : v; + }; + d3.median = function(array, f) { + var numbers = [], n = array.length, a, i = -1; + if (arguments.length === 1) { + while (++i < n) if (d3_numeric(a = d3_number(array[i]))) numbers.push(a); + } else { + while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) numbers.push(a); + } + if (numbers.length) return d3.quantile(numbers.sort(d3_ascending), .5); + }; + d3.variance = function(array, f) { + var n = array.length, m = 0, a, d, s = 0, i = -1, j = 0; + if (arguments.length === 1) { + while (++i < n) { + if (d3_numeric(a = d3_number(array[i]))) { + d = a - m; + m += d / ++j; + s += d * (a - m); } } - - if (next_code === 4096) { // Table full, need a clear. - emit_code(clear_code); - next_code = eoi_code + 1; - cur_code_size = min_code_size + 1; - code_table = { }; - } else { // Table not full, insert a new entry. - // Increase our variable bit code sizes if necessary. This is a bit - // tricky as it is based on "timing" between the encoding and - // decoder. From the encoders perspective this should happen after - // we've already emitted the index buffer and are about to create the - // first table entry that would overflow our current code bit size. - if (next_code >= (1 << cur_code_size)) ++cur_code_size; - code_table[cur_key] = next_code++; // Insert into code table. - } - - ib_code = k; // Index buffer to single input k. } else { - ib_code = cur_code; // Index buffer to sequence in code table. + while (++i < n) { + if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) { + d = a - m; + m += d / ++j; + s += d * (a - m); + } + } + } + if (j > 1) return s / (j - 1); + }; + d3.deviation = function() { + var v = d3.variance.apply(this, arguments); + return v ? Math.sqrt(v) : v; + }; + function d3_bisector(compare) { + return { + left: function(a, x, lo, hi) { + if (arguments.length < 3) lo = 0; + if (arguments.length < 4) hi = a.length; + while (lo < hi) { + var mid = lo + hi >>> 1; + if (compare(a[mid], x) < 0) lo = mid + 1; else hi = mid; + } + return lo; + }, + right: function(a, x, lo, hi) { + if (arguments.length < 3) lo = 0; + if (arguments.length < 4) hi = a.length; + while (lo < hi) { + var mid = lo + hi >>> 1; + if (compare(a[mid], x) > 0) hi = mid; else lo = mid + 1; + } + return lo; + } + }; + } + var d3_bisect = d3_bisector(d3_ascending); + d3.bisectLeft = d3_bisect.left; + d3.bisect = d3.bisectRight = d3_bisect.right; + d3.bisector = function(f) { + return d3_bisector(f.length === 1 ? function(d, x) { + return d3_ascending(f(d), x); + } : f); + }; + d3.shuffle = function(array, i0, i1) { + if ((m = arguments.length) < 3) { + i1 = array.length; + if (m < 2) i0 = 0; + } + var m = i1 - i0, t, i; + while (m) { + i = Math.random() * m-- | 0; + t = array[m + i0], array[m + i0] = array[i + i0], array[i + i0] = t; + } + return array; + }; + d3.permute = function(array, indexes) { + var i = indexes.length, permutes = new Array(i); + while (i--) permutes[i] = array[indexes[i]]; + return permutes; + }; + d3.pairs = function(array) { + var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n); + while (i < n) pairs[i] = [ p0 = p1, p1 = array[++i] ]; + return pairs; + }; + d3.transpose = function(matrix) { + if (!(n = matrix.length)) return []; + for (var i = -1, m = d3.min(matrix, d3_transposeLength), transpose = new Array(m); ++i < m; ) { + for (var j = -1, n, row = transpose[i] = new Array(n); ++j < n; ) { + row[j] = matrix[j][i]; + } + } + return transpose; + }; + function d3_transposeLength(d) { + return d.length; + } + d3.zip = function() { + return d3.transpose(arguments); + }; + d3.keys = function(map) { + var keys = []; + for (var key in map) keys.push(key); + return keys; + }; + d3.values = function(map) { + var values = []; + for (var key in map) values.push(map[key]); + return values; + }; + d3.entries = function(map) { + var entries = []; + for (var key in map) entries.push({ + key: key, + value: map[key] + }); + return entries; + }; + d3.merge = function(arrays) { + var n = arrays.length, m, i = -1, j = 0, merged, array; + while (++i < n) j += arrays[i].length; + merged = new Array(j); + while (--n >= 0) { + array = arrays[n]; + m = array.length; + while (--m >= 0) { + merged[--j] = array[m]; + } + } + return merged; + }; + var abs = Math.abs; + d3.range = function(start, stop, step) { + if (arguments.length < 3) { + step = 1; + if (arguments.length < 2) { + stop = start; + start = 0; + } + } + if ((stop - start) / step === Infinity) throw new Error("infinite range"); + var range = [], k = d3_range_integerScale(abs(step)), i = -1, j; + start *= k, stop *= k, step *= k; + if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k); else while ((j = start + step * ++i) < stop) range.push(j / k); + return range; + }; + function d3_range_integerScale(x) { + var k = 1; + while (x * k % 1) k *= 10; + return k; + } + function d3_class(ctor, properties) { + for (var key in properties) { + Object.defineProperty(ctor.prototype, key, { + value: properties[key], + enumerable: false + }); } } - - emit_code(ib_code); // There will still be something in the index buffer. - emit_code(eoi_code); // End Of Information. - - // Flush / finalize the sub-blocks stream to the buffer. - emit_bytes_to_buffer(1); - - // Finish the sub-blocks, writing out any unfinished lengths and - // terminating with a sub-block of length 0. If we have already started - // but not yet used a sub-block it can just become the terminator. - if (cur_subblock + 1 === p) { // Started but unused. - buf[cur_subblock] = 0; - } else { // Started and used, write length and additional terminator block. - buf[cur_subblock] = p - cur_subblock - 1; - buf[p++] = 0; + d3.map = function(object, f) { + var map = new d3_Map(); + if (object instanceof d3_Map) { + object.forEach(function(key, value) { + map.set(key, value); + }); + } else if (Array.isArray(object)) { + var i = -1, n = object.length, o; + if (arguments.length === 1) while (++i < n) map.set(i, object[i]); else while (++i < n) map.set(f.call(object, o = object[i], i), o); + } else { + for (var key in object) map.set(key, object[key]); + } + return map; + }; + function d3_Map() { + this._ = Object.create(null); } - return p; -} - -function GifReader(buf) { - var p = 0; - - // - Header (GIF87a or GIF89a). - if (buf[p++] !== 0x47 || buf[p++] !== 0x49 || buf[p++] !== 0x46 || - buf[p++] !== 0x38 || (buf[p++]+1 & 0xfd) !== 0x38 || buf[p++] !== 0x61) { - throw "Invalid GIF 87a/89a header."; + var d3_map_proto = "__proto__", d3_map_zero = "\x00"; + d3_class(d3_Map, { + has: d3_map_has, + get: function(key) { + return this._[d3_map_escape(key)]; + }, + set: function(key, value) { + return this._[d3_map_escape(key)] = value; + }, + remove: d3_map_remove, + keys: d3_map_keys, + values: function() { + var values = []; + for (var key in this._) values.push(this._[key]); + return values; + }, + entries: function() { + var entries = []; + for (var key in this._) entries.push({ + key: d3_map_unescape(key), + value: this._[key] + }); + return entries; + }, + size: d3_map_size, + empty: d3_map_empty, + forEach: function(f) { + for (var key in this._) f.call(this, d3_map_unescape(key), this._[key]); + } + }); + function d3_map_escape(key) { + return (key += "") === d3_map_proto || key[0] === d3_map_zero ? d3_map_zero + key : key; } - - // - Logical Screen Descriptor. - var width = buf[p++] | buf[p++] << 8; - var height = buf[p++] | buf[p++] << 8; - var pf0 = buf[p++]; // . - var global_palette_flag = pf0 >> 7; - var num_global_colors_pow2 = pf0 & 0x7; - var num_global_colors = 1 << (num_global_colors_pow2 + 1); - var background = buf[p++]; - buf[p++]; // Pixel aspect ratio (unused?). - - var global_palette_offset = null; - - if (global_palette_flag) { - global_palette_offset = p; - p += num_global_colors * 3; // Seek past palette. + function d3_map_unescape(key) { + return (key += "")[0] === d3_map_zero ? key.slice(1) : key; } + function d3_map_has(key) { + return d3_map_escape(key) in this._; + } + function d3_map_remove(key) { + return (key = d3_map_escape(key)) in this._ && delete this._[key]; + } + function d3_map_keys() { + var keys = []; + for (var key in this._) keys.push(d3_map_unescape(key)); + return keys; + } + function d3_map_size() { + var size = 0; + for (var key in this._) ++size; + return size; + } + function d3_map_empty() { + for (var key in this._) return false; + return true; + } + d3.nest = function() { + var nest = {}, keys = [], sortKeys = [], sortValues, rollup; + function map(mapType, array, depth) { + if (depth >= keys.length) return rollup ? rollup.call(nest, array) : sortValues ? array.sort(sortValues) : array; + var i = -1, n = array.length, key = keys[depth++], keyValue, object, setter, valuesByKey = new d3_Map(), values; + while (++i < n) { + if (values = valuesByKey.get(keyValue = key(object = array[i]))) { + values.push(object); + } else { + valuesByKey.set(keyValue, [ object ]); + } + } + if (mapType) { + object = mapType(); + setter = function(keyValue, values) { + object.set(keyValue, map(mapType, values, depth)); + }; + } else { + object = {}; + setter = function(keyValue, values) { + object[keyValue] = map(mapType, values, depth); + }; + } + valuesByKey.forEach(setter); + return object; + } + function entries(map, depth) { + if (depth >= keys.length) return map; + var array = [], sortKey = sortKeys[depth++]; + map.forEach(function(key, keyMap) { + array.push({ + key: key, + values: entries(keyMap, depth) + }); + }); + return sortKey ? array.sort(function(a, b) { + return sortKey(a.key, b.key); + }) : array; + } + nest.map = function(array, mapType) { + return map(mapType, array, 0); + }; + nest.entries = function(array) { + return entries(map(d3.map, array, 0), 0); + }; + nest.key = function(d) { + keys.push(d); + return nest; + }; + nest.sortKeys = function(order) { + sortKeys[keys.length - 1] = order; + return nest; + }; + nest.sortValues = function(order) { + sortValues = order; + return nest; + }; + nest.rollup = function(f) { + rollup = f; + return nest; + }; + return nest; + }; + d3.set = function(array) { + var set = new d3_Set(); + if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]); + return set; + }; + function d3_Set() { + this._ = Object.create(null); + } + d3_class(d3_Set, { + has: d3_map_has, + add: function(key) { + this._[d3_map_escape(key += "")] = true; + return key; + }, + remove: d3_map_remove, + values: d3_map_keys, + size: d3_map_size, + empty: d3_map_empty, + forEach: function(f) { + for (var key in this._) f.call(this, d3_map_unescape(key)); + } + }); + d3.behavior = {}; + function d3_identity(d) { + return d; + } + d3.rebind = function(target, source) { + var i = 1, n = arguments.length, method; + while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]); + return target; + }; + function d3_rebind(target, source, method) { + return function() { + var value = method.apply(source, arguments); + return value === source ? target : value; + }; + } + function d3_vendorSymbol(object, name) { + if (name in object) return name; + name = name.charAt(0).toUpperCase() + name.slice(1); + for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) { + var prefixName = d3_vendorPrefixes[i] + name; + if (prefixName in object) return prefixName; + } + } + var d3_vendorPrefixes = [ "webkit", "ms", "moz", "Moz", "o", "O" ]; + function d3_noop() {} + d3.dispatch = function() { + var dispatch = new d3_dispatch(), i = -1, n = arguments.length; + while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); + return dispatch; + }; + function d3_dispatch() {} + d3_dispatch.prototype.on = function(type, listener) { + var i = type.indexOf("."), name = ""; + if (i >= 0) { + name = type.slice(i + 1); + type = type.slice(0, i); + } + if (type) return arguments.length < 2 ? this[type].on(name) : this[type].on(name, listener); + if (arguments.length === 2) { + if (listener == null) for (type in this) { + if (this.hasOwnProperty(type)) this[type].on(name, null); + } + return this; + } + }; + function d3_dispatch_event(dispatch) { + var listeners = [], listenerByName = new d3_Map(); + function event() { + var z = listeners, i = -1, n = z.length, l; + while (++i < n) if (l = z[i].on) l.apply(this, arguments); + return dispatch; + } + event.on = function(name, listener) { + var l = listenerByName.get(name), i; + if (arguments.length < 2) return l && l.on; + if (l) { + l.on = null; + listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1)); + listenerByName.remove(name); + } + if (listener) listeners.push(listenerByName.set(name, { + on: listener + })); + return dispatch; + }; + return event; + } + d3.event = null; + function d3_eventPreventDefault() { + d3.event.preventDefault(); + } + function d3_eventSource() { + var e = d3.event, s; + while (s = e.sourceEvent) e = s; + return e; + } + function d3_eventDispatch(target) { + var dispatch = new d3_dispatch(), i = 0, n = arguments.length; + while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); + dispatch.of = function(thiz, argumentz) { + return function(e1) { + try { + var e0 = e1.sourceEvent = d3.event; + e1.target = target; + d3.event = e1; + dispatch[e1.type].apply(thiz, argumentz); + } finally { + d3.event = e0; + } + }; + }; + return dispatch; + } + d3.requote = function(s) { + return s.replace(d3_requote_re, "\\$&"); + }; + var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g; + var d3_subclass = {}.__proto__ ? function(object, prototype) { + object.__proto__ = prototype; + } : function(object, prototype) { + for (var property in prototype) object[property] = prototype[property]; + }; + function d3_selection(groups) { + d3_subclass(groups, d3_selectionPrototype); + return groups; + } + var d3_select = function(s, n) { + return n.querySelector(s); + }, d3_selectAll = function(s, n) { + return n.querySelectorAll(s); + }, d3_selectMatches = function(n, s) { + var d3_selectMatcher = n.matches || n[d3_vendorSymbol(n, "matchesSelector")]; + d3_selectMatches = function(n, s) { + return d3_selectMatcher.call(n, s); + }; + return d3_selectMatches(n, s); + }; + if (typeof Sizzle === "function") { + d3_select = function(s, n) { + return Sizzle(s, n)[0] || null; + }; + d3_selectAll = Sizzle; + d3_selectMatches = Sizzle.matchesSelector; + } + d3.selection = function() { + return d3.select(d3_document.documentElement); + }; + var d3_selectionPrototype = d3.selection.prototype = []; + d3_selectionPrototype.select = function(selector) { + var subgroups = [], subgroup, subnode, group, node; + selector = d3_selection_selector(selector); + for (var j = -1, m = this.length; ++j < m; ) { + subgroups.push(subgroup = []); + subgroup.parentNode = (group = this[j]).parentNode; + for (var i = -1, n = group.length; ++i < n; ) { + if (node = group[i]) { + subgroup.push(subnode = selector.call(node, node.__data__, i, j)); + if (subnode && "__data__" in node) subnode.__data__ = node.__data__; + } else { + subgroup.push(null); + } + } + } + return d3_selection(subgroups); + }; + function d3_selection_selector(selector) { + return typeof selector === "function" ? selector : function() { + return d3_select(selector, this); + }; + } + d3_selectionPrototype.selectAll = function(selector) { + var subgroups = [], subgroup, node; + selector = d3_selection_selectorAll(selector); + for (var j = -1, m = this.length; ++j < m; ) { + for (var group = this[j], i = -1, n = group.length; ++i < n; ) { + if (node = group[i]) { + subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j))); + subgroup.parentNode = node; + } + } + } + return d3_selection(subgroups); + }; + function d3_selection_selectorAll(selector) { + return typeof selector === "function" ? selector : function() { + return d3_selectAll(selector, this); + }; + } + var d3_nsXhtml = "http://www.w3.org/1999/xhtml"; + var d3_nsPrefix = { + svg: "http://www.w3.org/2000/svg", + xhtml: d3_nsXhtml, + xlink: "http://www.w3.org/1999/xlink", + xml: "http://www.w3.org/XML/1998/namespace", + xmlns: "http://www.w3.org/2000/xmlns/" + }; + d3.ns = { + prefix: d3_nsPrefix, + qualify: function(name) { + var i = name.indexOf(":"), prefix = name; + if (i >= 0 && (prefix = name.slice(0, i)) !== "xmlns") name = name.slice(i + 1); + return d3_nsPrefix.hasOwnProperty(prefix) ? { + space: d3_nsPrefix[prefix], + local: name + } : name; + } + }; + d3_selectionPrototype.attr = function(name, value) { + if (arguments.length < 2) { + if (typeof name === "string") { + var node = this.node(); + name = d3.ns.qualify(name); + return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name); + } + for (value in name) this.each(d3_selection_attr(value, name[value])); + return this; + } + return this.each(d3_selection_attr(name, value)); + }; + function d3_selection_attr(name, value) { + name = d3.ns.qualify(name); + function attrNull() { + this.removeAttribute(name); + } + function attrNullNS() { + this.removeAttributeNS(name.space, name.local); + } + function attrConstant() { + this.setAttribute(name, value); + } + function attrConstantNS() { + this.setAttributeNS(name.space, name.local, value); + } + function attrFunction() { + var x = value.apply(this, arguments); + if (x == null) this.removeAttribute(name); else this.setAttribute(name, x); + } + function attrFunctionNS() { + var x = value.apply(this, arguments); + if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x); + } + return value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant; + } + function d3_collapse(s) { + return s.trim().replace(/\s+/g, " "); + } + d3_selectionPrototype.classed = function(name, value) { + if (arguments.length < 2) { + if (typeof name === "string") { + var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1; + if (value = node.classList) { + while (++i < n) if (!value.contains(name[i])) return false; + } else { + value = node.getAttribute("class"); + while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false; + } + return true; + } + for (value in name) this.each(d3_selection_classed(value, name[value])); + return this; + } + return this.each(d3_selection_classed(name, value)); + }; + function d3_selection_classedRe(name) { + return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g"); + } + function d3_selection_classes(name) { + return (name + "").trim().split(/^|\s+/); + } + function d3_selection_classed(name, value) { + name = d3_selection_classes(name).map(d3_selection_classedName); + var n = name.length; + function classedConstant() { + var i = -1; + while (++i < n) name[i](this, value); + } + function classedFunction() { + var i = -1, x = value.apply(this, arguments); + while (++i < n) name[i](this, x); + } + return typeof value === "function" ? classedFunction : classedConstant; + } + function d3_selection_classedName(name) { + var re = d3_selection_classedRe(name); + return function(node, value) { + if (c = node.classList) return value ? c.add(name) : c.remove(name); + var c = node.getAttribute("class") || ""; + if (value) { + re.lastIndex = 0; + if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name)); + } else { + node.setAttribute("class", d3_collapse(c.replace(re, " "))); + } + }; + } + d3_selectionPrototype.style = function(name, value, priority) { + var n = arguments.length; + if (n < 3) { + if (typeof name !== "string") { + if (n < 2) value = ""; + for (priority in name) this.each(d3_selection_style(priority, name[priority], value)); + return this; + } + if (n < 2) { + var node = this.node(); + return d3_window(node).getComputedStyle(node, null).getPropertyValue(name); + } + priority = ""; + } + return this.each(d3_selection_style(name, value, priority)); + }; + function d3_selection_style(name, value, priority) { + function styleNull() { + this.style.removeProperty(name); + } + function styleConstant() { + this.style.setProperty(name, value, priority); + } + function styleFunction() { + var x = value.apply(this, arguments); + if (x == null) this.style.removeProperty(name); else this.style.setProperty(name, x, priority); + } + return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant; + } + d3_selectionPrototype.property = function(name, value) { + if (arguments.length < 2) { + if (typeof name === "string") return this.node()[name]; + for (value in name) this.each(d3_selection_property(value, name[value])); + return this; + } + return this.each(d3_selection_property(name, value)); + }; + function d3_selection_property(name, value) { + function propertyNull() { + delete this[name]; + } + function propertyConstant() { + this[name] = value; + } + function propertyFunction() { + var x = value.apply(this, arguments); + if (x == null) delete this[name]; else this[name] = x; + } + return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant; + } + d3_selectionPrototype.text = function(value) { + return arguments.length ? this.each(typeof value === "function" ? function() { + var v = value.apply(this, arguments); + this.textContent = v == null ? "" : v; + } : value == null ? function() { + this.textContent = ""; + } : function() { + this.textContent = value; + }) : this.node().textContent; + }; + d3_selectionPrototype.html = function(value) { + return arguments.length ? this.each(typeof value === "function" ? function() { + var v = value.apply(this, arguments); + this.innerHTML = v == null ? "" : v; + } : value == null ? function() { + this.innerHTML = ""; + } : function() { + this.innerHTML = value; + }) : this.node().innerHTML; + }; + d3_selectionPrototype.append = function(name) { + name = d3_selection_creator(name); + return this.select(function() { + return this.appendChild(name.apply(this, arguments)); + }); + }; + function d3_selection_creator(name) { + function create() { + var document = this.ownerDocument, namespace = this.namespaceURI; + return namespace === d3_nsXhtml && document.documentElement.namespaceURI === d3_nsXhtml ? document.createElement(name) : document.createElementNS(namespace, name); + } + function createNS() { + return this.ownerDocument.createElementNS(name.space, name.local); + } + return typeof name === "function" ? name : (name = d3.ns.qualify(name)).local ? createNS : create; + } + d3_selectionPrototype.insert = function(name, before) { + name = d3_selection_creator(name); + before = d3_selection_selector(before); + return this.select(function() { + return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null); + }); + }; + d3_selectionPrototype.remove = function() { + return this.each(d3_selectionRemove); + }; + function d3_selectionRemove() { + var parent = this.parentNode; + if (parent) parent.removeChild(this); + } + d3_selectionPrototype.data = function(value, key) { + var i = -1, n = this.length, group, node; + if (!arguments.length) { + value = new Array(n = (group = this[0]).length); + while (++i < n) { + if (node = group[i]) { + value[i] = node.__data__; + } + } + return value; + } + function bind(group, groupData) { + var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData; + if (key) { + var nodeByKeyValue = new d3_Map(), keyValues = new Array(n), keyValue; + for (i = -1; ++i < n; ) { + if (node = group[i]) { + if (nodeByKeyValue.has(keyValue = key.call(node, node.__data__, i))) { + exitNodes[i] = node; + } else { + nodeByKeyValue.set(keyValue, node); + } + keyValues[i] = keyValue; + } + } + for (i = -1; ++i < m; ) { + if (!(node = nodeByKeyValue.get(keyValue = key.call(groupData, nodeData = groupData[i], i)))) { + enterNodes[i] = d3_selection_dataNode(nodeData); + } else if (node !== true) { + updateNodes[i] = node; + node.__data__ = nodeData; + } + nodeByKeyValue.set(keyValue, true); + } + for (i = -1; ++i < n; ) { + if (i in keyValues && nodeByKeyValue.get(keyValues[i]) !== true) { + exitNodes[i] = group[i]; + } + } + } else { + for (i = -1; ++i < n0; ) { + node = group[i]; + nodeData = groupData[i]; + if (node) { + node.__data__ = nodeData; + updateNodes[i] = node; + } else { + enterNodes[i] = d3_selection_dataNode(nodeData); + } + } + for (;i < m; ++i) { + enterNodes[i] = d3_selection_dataNode(groupData[i]); + } + for (;i < n; ++i) { + exitNodes[i] = group[i]; + } + } + enterNodes.update = updateNodes; + enterNodes.parentNode = updateNodes.parentNode = exitNodes.parentNode = group.parentNode; + enter.push(enterNodes); + update.push(updateNodes); + exit.push(exitNodes); + } + var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]); + if (typeof value === "function") { + while (++i < n) { + bind(group = this[i], value.call(group, group.parentNode.__data__, i)); + } + } else { + while (++i < n) { + bind(group = this[i], value); + } + } + update.enter = function() { + return enter; + }; + update.exit = function() { + return exit; + }; + return update; + }; + function d3_selection_dataNode(data) { + return { + __data__: data + }; + } + d3_selectionPrototype.datum = function(value) { + return arguments.length ? this.property("__data__", value) : this.property("__data__"); + }; + d3_selectionPrototype.filter = function(filter) { + var subgroups = [], subgroup, group, node; + if (typeof filter !== "function") filter = d3_selection_filter(filter); + for (var j = 0, m = this.length; j < m; j++) { + subgroups.push(subgroup = []); + subgroup.parentNode = (group = this[j]).parentNode; + for (var i = 0, n = group.length; i < n; i++) { + if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { + subgroup.push(node); + } + } + } + return d3_selection(subgroups); + }; + function d3_selection_filter(selector) { + return function() { + return d3_selectMatches(this, selector); + }; + } + d3_selectionPrototype.order = function() { + for (var j = -1, m = this.length; ++j < m; ) { + for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0; ) { + if (node = group[i]) { + if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next); + next = node; + } + } + } + return this; + }; + d3_selectionPrototype.sort = function(comparator) { + comparator = d3_selection_sortComparator.apply(this, arguments); + for (var j = -1, m = this.length; ++j < m; ) this[j].sort(comparator); + return this.order(); + }; + function d3_selection_sortComparator(comparator) { + if (!arguments.length) comparator = d3_ascending; + return function(a, b) { + return a && b ? comparator(a.__data__, b.__data__) : !a - !b; + }; + } + d3_selectionPrototype.each = function(callback) { + return d3_selection_each(this, function(node, i, j) { + callback.call(node, node.__data__, i, j); + }); + }; + function d3_selection_each(groups, callback) { + for (var j = 0, m = groups.length; j < m; j++) { + for (var group = groups[j], i = 0, n = group.length, node; i < n; i++) { + if (node = group[i]) callback(node, i, j); + } + } + return groups; + } + d3_selectionPrototype.call = function(callback) { + var args = d3_array(arguments); + callback.apply(args[0] = this, args); + return this; + }; + d3_selectionPrototype.empty = function() { + return !this.node(); + }; + d3_selectionPrototype.node = function() { + for (var j = 0, m = this.length; j < m; j++) { + for (var group = this[j], i = 0, n = group.length; i < n; i++) { + var node = group[i]; + if (node) return node; + } + } + return null; + }; + d3_selectionPrototype.size = function() { + var n = 0; + d3_selection_each(this, function() { + ++n; + }); + return n; + }; + function d3_selection_enter(selection) { + d3_subclass(selection, d3_selection_enterPrototype); + return selection; + } + var d3_selection_enterPrototype = []; + d3.selection.enter = d3_selection_enter; + d3.selection.enter.prototype = d3_selection_enterPrototype; + d3_selection_enterPrototype.append = d3_selectionPrototype.append; + d3_selection_enterPrototype.empty = d3_selectionPrototype.empty; + d3_selection_enterPrototype.node = d3_selectionPrototype.node; + d3_selection_enterPrototype.call = d3_selectionPrototype.call; + d3_selection_enterPrototype.size = d3_selectionPrototype.size; + d3_selection_enterPrototype.select = function(selector) { + var subgroups = [], subgroup, subnode, upgroup, group, node; + for (var j = -1, m = this.length; ++j < m; ) { + upgroup = (group = this[j]).update; + subgroups.push(subgroup = []); + subgroup.parentNode = group.parentNode; + for (var i = -1, n = group.length; ++i < n; ) { + if (node = group[i]) { + subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i, j)); + subnode.__data__ = node.__data__; + } else { + subgroup.push(null); + } + } + } + return d3_selection(subgroups); + }; + d3_selection_enterPrototype.insert = function(name, before) { + if (arguments.length < 2) before = d3_selection_enterInsertBefore(this); + return d3_selectionPrototype.insert.call(this, name, before); + }; + function d3_selection_enterInsertBefore(enter) { + var i0, j0; + return function(d, i, j) { + var group = enter[j].update, n = group.length, node; + if (j != j0) j0 = j, i0 = 0; + if (i >= i0) i0 = i + 1; + while (!(node = group[i0]) && ++i0 < n) ; + return node; + }; + } + d3.select = function(node) { + var group; + if (typeof node === "string") { + group = [ d3_select(node, d3_document) ]; + group.parentNode = d3_document.documentElement; + } else { + group = [ node ]; + group.parentNode = d3_documentElement(node); + } + return d3_selection([ group ]); + }; + d3.selectAll = function(nodes) { + var group; + if (typeof nodes === "string") { + group = d3_array(d3_selectAll(nodes, d3_document)); + group.parentNode = d3_document.documentElement; + } else { + group = d3_array(nodes); + group.parentNode = null; + } + return d3_selection([ group ]); + }; + d3_selectionPrototype.on = function(type, listener, capture) { + var n = arguments.length; + if (n < 3) { + if (typeof type !== "string") { + if (n < 2) listener = false; + for (capture in type) this.each(d3_selection_on(capture, type[capture], listener)); + return this; + } + if (n < 2) return (n = this.node()["__on" + type]) && n._; + capture = false; + } + return this.each(d3_selection_on(type, listener, capture)); + }; + function d3_selection_on(type, listener, capture) { + var name = "__on" + type, i = type.indexOf("."), wrap = d3_selection_onListener; + if (i > 0) type = type.slice(0, i); + var filter = d3_selection_onFilters.get(type); + if (filter) type = filter, wrap = d3_selection_onFilter; + function onRemove() { + var l = this[name]; + if (l) { + this.removeEventListener(type, l, l.$); + delete this[name]; + } + } + function onAdd() { + var l = wrap(listener, d3_array(arguments)); + onRemove.call(this); + this.addEventListener(type, this[name] = l, l.$ = capture); + l._ = listener; + } + function removeAll() { + var re = new RegExp("^__on([^.]+)" + d3.requote(type) + "$"), match; + for (var name in this) { + if (match = name.match(re)) { + var l = this[name]; + this.removeEventListener(match[1], l, l.$); + delete this[name]; + } + } + } + return i ? listener ? onAdd : onRemove : listener ? d3_noop : removeAll; + } + var d3_selection_onFilters = d3.map({ + mouseenter: "mouseover", + mouseleave: "mouseout" + }); + if (d3_document) { + d3_selection_onFilters.forEach(function(k) { + if ("on" + k in d3_document) d3_selection_onFilters.remove(k); + }); + } + function d3_selection_onListener(listener, argumentz) { + return function(e) { + var o = d3.event; + d3.event = e; + argumentz[0] = this.__data__; + try { + listener.apply(this, argumentz); + } finally { + d3.event = o; + } + }; + } + function d3_selection_onFilter(listener, argumentz) { + var l = d3_selection_onListener(listener, argumentz); + return function(e) { + var target = this, related = e.relatedTarget; + if (!related || related !== target && !(related.compareDocumentPosition(target) & 8)) { + l.call(target, e); + } + }; + } + var d3_event_dragSelect, d3_event_dragId = 0; + function d3_event_dragSuppress(node) { + var name = ".dragsuppress-" + ++d3_event_dragId, click = "click" + name, w = d3.select(d3_window(node)).on("touchmove" + name, d3_eventPreventDefault).on("dragstart" + name, d3_eventPreventDefault).on("selectstart" + name, d3_eventPreventDefault); + if (d3_event_dragSelect == null) { + d3_event_dragSelect = "onselectstart" in node ? false : d3_vendorSymbol(node.style, "userSelect"); + } + if (d3_event_dragSelect) { + var style = d3_documentElement(node).style, select = style[d3_event_dragSelect]; + style[d3_event_dragSelect] = "none"; + } + return function(suppressClick) { + w.on(name, null); + if (d3_event_dragSelect) style[d3_event_dragSelect] = select; + if (suppressClick) { + var off = function() { + w.on(click, null); + }; + w.on(click, function() { + d3_eventPreventDefault(); + off(); + }, true); + setTimeout(off, 0); + } + }; + } + d3.mouse = function(container) { + return d3_mousePoint(container, d3_eventSource()); + }; + var d3_mouse_bug44083 = this.navigator && /WebKit/.test(this.navigator.userAgent) ? -1 : 0; + function d3_mousePoint(container, e) { + if (e.changedTouches) e = e.changedTouches[0]; + var svg = container.ownerSVGElement || container; + if (svg.createSVGPoint) { + var point = svg.createSVGPoint(); + if (d3_mouse_bug44083 < 0) { + var window = d3_window(container); + if (window.scrollX || window.scrollY) { + svg = d3.select("body").append("svg").style({ + position: "absolute", + top: 0, + left: 0, + margin: 0, + padding: 0, + border: "none" + }, "important"); + var ctm = svg[0][0].getScreenCTM(); + d3_mouse_bug44083 = !(ctm.f || ctm.e); + svg.remove(); + } + } + if (d3_mouse_bug44083) point.x = e.pageX, point.y = e.pageY; else point.x = e.clientX, + point.y = e.clientY; + point = point.matrixTransform(container.getScreenCTM().inverse()); + return [ point.x, point.y ]; + } + var rect = container.getBoundingClientRect(); + return [ e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop ]; + } + d3.touch = function(container, touches, identifier) { + if (arguments.length < 3) identifier = touches, touches = d3_eventSource().changedTouches; + if (touches) for (var i = 0, n = touches.length, touch; i < n; ++i) { + if ((touch = touches[i]).identifier === identifier) { + return d3_mousePoint(container, touch); + } + } + }; + d3.behavior.drag = function() { + var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"), origin = null, mousedown = dragstart(d3_noop, d3.mouse, d3_window, "mousemove", "mouseup"), touchstart = dragstart(d3_behavior_dragTouchId, d3.touch, d3_identity, "touchmove", "touchend"); + function drag() { + this.on("mousedown.drag", mousedown).on("touchstart.drag", touchstart); + } + function dragstart(id, position, subject, move, end) { + return function() { + var that = this, target = d3.event.target.correspondingElement || d3.event.target, parent = that.parentNode, dispatch = event.of(that, arguments), dragged = 0, dragId = id(), dragName = ".drag" + (dragId == null ? "" : "-" + dragId), dragOffset, dragSubject = d3.select(subject(target)).on(move + dragName, moved).on(end + dragName, ended), dragRestore = d3_event_dragSuppress(target), position0 = position(parent, dragId); + if (origin) { + dragOffset = origin.apply(that, arguments); + dragOffset = [ dragOffset.x - position0[0], dragOffset.y - position0[1] ]; + } else { + dragOffset = [ 0, 0 ]; + } + dispatch({ + type: "dragstart" + }); + function moved() { + var position1 = position(parent, dragId), dx, dy; + if (!position1) return; + dx = position1[0] - position0[0]; + dy = position1[1] - position0[1]; + dragged |= dx | dy; + position0 = position1; + dispatch({ + type: "drag", + x: position1[0] + dragOffset[0], + y: position1[1] + dragOffset[1], + dx: dx, + dy: dy + }); + } + function ended() { + if (!position(parent, dragId)) return; + dragSubject.on(move + dragName, null).on(end + dragName, null); + dragRestore(dragged); + dispatch({ + type: "dragend" + }); + } + }; + } + drag.origin = function(x) { + if (!arguments.length) return origin; + origin = x; + return drag; + }; + return d3.rebind(drag, event, "on"); + }; + function d3_behavior_dragTouchId() { + return d3.event.changedTouches[0].identifier; + } + d3.touches = function(container, touches) { + if (arguments.length < 2) touches = d3_eventSource().touches; + return touches ? d3_array(touches).map(function(touch) { + var point = d3_mousePoint(container, touch); + point.identifier = touch.identifier; + return point; + }) : []; + }; + var ε = 1e-6, ε2 = ε * ε, π = Math.PI, τ = 2 * π, τε = τ - ε, halfπ = π / 2, d3_radians = π / 180, d3_degrees = 180 / π; + function d3_sgn(x) { + return x > 0 ? 1 : x < 0 ? -1 : 0; + } + function d3_cross2d(a, b, c) { + return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); + } + function d3_acos(x) { + return x > 1 ? 0 : x < -1 ? π : Math.acos(x); + } + function d3_asin(x) { + return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x); + } + function d3_sinh(x) { + return ((x = Math.exp(x)) - 1 / x) / 2; + } + function d3_cosh(x) { + return ((x = Math.exp(x)) + 1 / x) / 2; + } + function d3_tanh(x) { + return ((x = Math.exp(2 * x)) - 1) / (x + 1); + } + function d3_haversin(x) { + return (x = Math.sin(x / 2)) * x; + } + var ρ = Math.SQRT2, ρ2 = 2, ρ4 = 4; + d3.interpolateZoom = function(p0, p1) { + var ux0 = p0[0], uy0 = p0[1], w0 = p0[2], ux1 = p1[0], uy1 = p1[1], w1 = p1[2], dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, i, S; + if (d2 < ε2) { + S = Math.log(w1 / w0) / ρ; + i = function(t) { + return [ ux0 + t * dx, uy0 + t * dy, w0 * Math.exp(ρ * t * S) ]; + }; + } else { + var d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1), b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0), r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1); + S = (r1 - r0) / ρ; + i = function(t) { + var s = t * S, coshr0 = d3_cosh(r0), u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0)); + return [ ux0 + u * dx, uy0 + u * dy, w0 * coshr0 / d3_cosh(ρ * s + r0) ]; + }; + } + i.duration = S * 1e3; + return i; + }; + d3.behavior.zoom = function() { + var view = { + x: 0, + y: 0, + k: 1 + }, translate0, center0, center, size = [ 960, 500 ], scaleExtent = d3_behavior_zoomInfinity, duration = 250, zooming = 0, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", mousewheelTimer, touchstart = "touchstart.zoom", touchtime, event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"), x0, x1, y0, y1; + if (!d3_behavior_zoomWheel) { + d3_behavior_zoomWheel = "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { + return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); + }, "wheel") : "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { + return d3.event.wheelDelta; + }, "mousewheel") : (d3_behavior_zoomDelta = function() { + return -d3.event.detail; + }, "MozMousePixelScroll"); + } + function zoom(g) { + g.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on("dblclick.zoom", dblclicked).on(touchstart, touchstarted); + } + zoom.event = function(g) { + g.each(function() { + var dispatch = event.of(this, arguments), view1 = view; + if (d3_transitionInheritId) { + d3.select(this).transition().each("start.zoom", function() { + view = this.__chart__ || { + x: 0, + y: 0, + k: 1 + }; + zoomstarted(dispatch); + }).tween("zoom:zoom", function() { + var dx = size[0], dy = size[1], cx = center0 ? center0[0] : dx / 2, cy = center0 ? center0[1] : dy / 2, i = d3.interpolateZoom([ (cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k ], [ (cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k ]); + return function(t) { + var l = i(t), k = dx / l[2]; + this.__chart__ = view = { + x: cx - l[0] * k, + y: cy - l[1] * k, + k: k + }; + zoomed(dispatch); + }; + }).each("interrupt.zoom", function() { + zoomended(dispatch); + }).each("end.zoom", function() { + zoomended(dispatch); + }); + } else { + this.__chart__ = view; + zoomstarted(dispatch); + zoomed(dispatch); + zoomended(dispatch); + } + }); + }; + zoom.translate = function(_) { + if (!arguments.length) return [ view.x, view.y ]; + view = { + x: +_[0], + y: +_[1], + k: view.k + }; + rescale(); + return zoom; + }; + zoom.scale = function(_) { + if (!arguments.length) return view.k; + view = { + x: view.x, + y: view.y, + k: null + }; + scaleTo(+_); + rescale(); + return zoom; + }; + zoom.scaleExtent = function(_) { + if (!arguments.length) return scaleExtent; + scaleExtent = _ == null ? d3_behavior_zoomInfinity : [ +_[0], +_[1] ]; + return zoom; + }; + zoom.center = function(_) { + if (!arguments.length) return center; + center = _ && [ +_[0], +_[1] ]; + return zoom; + }; + zoom.size = function(_) { + if (!arguments.length) return size; + size = _ && [ +_[0], +_[1] ]; + return zoom; + }; + zoom.duration = function(_) { + if (!arguments.length) return duration; + duration = +_; + return zoom; + }; + zoom.x = function(z) { + if (!arguments.length) return x1; + x1 = z; + x0 = z.copy(); + view = { + x: 0, + y: 0, + k: 1 + }; + return zoom; + }; + zoom.y = function(z) { + if (!arguments.length) return y1; + y1 = z; + y0 = z.copy(); + view = { + x: 0, + y: 0, + k: 1 + }; + return zoom; + }; + function location(p) { + return [ (p[0] - view.x) / view.k, (p[1] - view.y) / view.k ]; + } + function point(l) { + return [ l[0] * view.k + view.x, l[1] * view.k + view.y ]; + } + function scaleTo(s) { + view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s)); + } + function translateTo(p, l) { + l = point(l); + view.x += p[0] - l[0]; + view.y += p[1] - l[1]; + } + function zoomTo(that, p, l, k) { + that.__chart__ = { + x: view.x, + y: view.y, + k: view.k + }; + scaleTo(Math.pow(2, k)); + translateTo(center0 = p, l); + that = d3.select(that); + if (duration > 0) that = that.transition().duration(duration); + that.call(zoom.event); + } + function rescale() { + if (x1) x1.domain(x0.range().map(function(x) { + return (x - view.x) / view.k; + }).map(x0.invert)); + if (y1) y1.domain(y0.range().map(function(y) { + return (y - view.y) / view.k; + }).map(y0.invert)); + } + function zoomstarted(dispatch) { + if (!zooming++) dispatch({ + type: "zoomstart" + }); + } + function zoomed(dispatch) { + rescale(); + dispatch({ + type: "zoom", + scale: view.k, + translate: [ view.x, view.y ] + }); + } + function zoomended(dispatch) { + if (!--zooming) dispatch({ + type: "zoomend" + }), center0 = null; + } + function mousedowned() { + var that = this, dispatch = event.of(that, arguments), dragged = 0, subject = d3.select(d3_window(that)).on(mousemove, moved).on(mouseup, ended), location0 = location(d3.mouse(that)), dragRestore = d3_event_dragSuppress(that); + d3_selection_interrupt.call(that); + zoomstarted(dispatch); + function moved() { + dragged = 1; + translateTo(d3.mouse(that), location0); + zoomed(dispatch); + } + function ended() { + subject.on(mousemove, null).on(mouseup, null); + dragRestore(dragged); + zoomended(dispatch); + } + } + function touchstarted() { + var that = this, dispatch = event.of(that, arguments), locations0 = {}, distance0 = 0, scale0, zoomName = ".zoom-" + d3.event.changedTouches[0].identifier, touchmove = "touchmove" + zoomName, touchend = "touchend" + zoomName, targets = [], subject = d3.select(that), dragRestore = d3_event_dragSuppress(that); + started(); + zoomstarted(dispatch); + subject.on(mousedown, null).on(touchstart, started); + function relocate() { + var touches = d3.touches(that); + scale0 = view.k; + touches.forEach(function(t) { + if (t.identifier in locations0) locations0[t.identifier] = location(t); + }); + return touches; + } + function started() { + var target = d3.event.target; + d3.select(target).on(touchmove, moved).on(touchend, ended); + targets.push(target); + var changed = d3.event.changedTouches; + for (var i = 0, n = changed.length; i < n; ++i) { + locations0[changed[i].identifier] = null; + } + var touches = relocate(), now = Date.now(); + if (touches.length === 1) { + if (now - touchtime < 500) { + var p = touches[0]; + zoomTo(that, p, locations0[p.identifier], Math.floor(Math.log(view.k) / Math.LN2) + 1); + d3_eventPreventDefault(); + } + touchtime = now; + } else if (touches.length > 1) { + var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1]; + distance0 = dx * dx + dy * dy; + } + } + function moved() { + var touches = d3.touches(that), p0, l0, p1, l1; + d3_selection_interrupt.call(that); + for (var i = 0, n = touches.length; i < n; ++i, l1 = null) { + p1 = touches[i]; + if (l1 = locations0[p1.identifier]) { + if (l0) break; + p0 = p1, l0 = l1; + } + } + if (l1) { + var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1, scale1 = distance0 && Math.sqrt(distance1 / distance0); + p0 = [ (p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2 ]; + l0 = [ (l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2 ]; + scaleTo(scale1 * scale0); + } + touchtime = null; + translateTo(p0, l0); + zoomed(dispatch); + } + function ended() { + if (d3.event.touches.length) { + var changed = d3.event.changedTouches; + for (var i = 0, n = changed.length; i < n; ++i) { + delete locations0[changed[i].identifier]; + } + for (var identifier in locations0) { + return void relocate(); + } + } + d3.selectAll(targets).on(zoomName, null); + subject.on(mousedown, mousedowned).on(touchstart, touchstarted); + dragRestore(); + zoomended(dispatch); + } + } + function mousewheeled() { + var dispatch = event.of(this, arguments); + if (mousewheelTimer) clearTimeout(mousewheelTimer); else d3_selection_interrupt.call(this), + translate0 = location(center0 = center || d3.mouse(this)), zoomstarted(dispatch); + mousewheelTimer = setTimeout(function() { + mousewheelTimer = null; + zoomended(dispatch); + }, 50); + d3_eventPreventDefault(); + scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k); + translateTo(center0, translate0); + zoomed(dispatch); + } + function dblclicked() { + var p = d3.mouse(this), k = Math.log(view.k) / Math.LN2; + zoomTo(this, p, location(p), d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1); + } + return d3.rebind(zoom, event, "on"); + }; + var d3_behavior_zoomInfinity = [ 0, Infinity ], d3_behavior_zoomDelta, d3_behavior_zoomWheel; + d3.color = d3_color; + function d3_color() {} + d3_color.prototype.toString = function() { + return this.rgb() + ""; + }; + d3.hsl = d3_hsl; + function d3_hsl(h, s, l) { + return this instanceof d3_hsl ? void (this.h = +h, this.s = +s, this.l = +l) : arguments.length < 2 ? h instanceof d3_hsl ? new d3_hsl(h.h, h.s, h.l) : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl) : new d3_hsl(h, s, l); + } + var d3_hslPrototype = d3_hsl.prototype = new d3_color(); + d3_hslPrototype.brighter = function(k) { + k = Math.pow(.7, arguments.length ? k : 1); + return new d3_hsl(this.h, this.s, this.l / k); + }; + d3_hslPrototype.darker = function(k) { + k = Math.pow(.7, arguments.length ? k : 1); + return new d3_hsl(this.h, this.s, k * this.l); + }; + d3_hslPrototype.rgb = function() { + return d3_hsl_rgb(this.h, this.s, this.l); + }; + function d3_hsl_rgb(h, s, l) { + var m1, m2; + h = isNaN(h) ? 0 : (h %= 360) < 0 ? h + 360 : h; + s = isNaN(s) ? 0 : s < 0 ? 0 : s > 1 ? 1 : s; + l = l < 0 ? 0 : l > 1 ? 1 : l; + m2 = l <= .5 ? l * (1 + s) : l + s - l * s; + m1 = 2 * l - m2; + function v(h) { + if (h > 360) h -= 360; else if (h < 0) h += 360; + if (h < 60) return m1 + (m2 - m1) * h / 60; + if (h < 180) return m2; + if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60; + return m1; + } + function vv(h) { + return Math.round(v(h) * 255); + } + return new d3_rgb(vv(h + 120), vv(h), vv(h - 120)); + } + d3.hcl = d3_hcl; + function d3_hcl(h, c, l) { + return this instanceof d3_hcl ? void (this.h = +h, this.c = +c, this.l = +l) : arguments.length < 2 ? h instanceof d3_hcl ? new d3_hcl(h.h, h.c, h.l) : h instanceof d3_lab ? d3_lab_hcl(h.l, h.a, h.b) : d3_lab_hcl((h = d3_rgb_lab((h = d3.rgb(h)).r, h.g, h.b)).l, h.a, h.b) : new d3_hcl(h, c, l); + } + var d3_hclPrototype = d3_hcl.prototype = new d3_color(); + d3_hclPrototype.brighter = function(k) { + return new d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1))); + }; + d3_hclPrototype.darker = function(k) { + return new d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1))); + }; + d3_hclPrototype.rgb = function() { + return d3_hcl_lab(this.h, this.c, this.l).rgb(); + }; + function d3_hcl_lab(h, c, l) { + if (isNaN(h)) h = 0; + if (isNaN(c)) c = 0; + return new d3_lab(l, Math.cos(h *= d3_radians) * c, Math.sin(h) * c); + } + d3.lab = d3_lab; + function d3_lab(l, a, b) { + return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b) : arguments.length < 2 ? l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b) : l instanceof d3_hcl ? d3_hcl_lab(l.h, l.c, l.l) : d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b) : new d3_lab(l, a, b); + } + var d3_lab_K = 18; + var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883; + var d3_labPrototype = d3_lab.prototype = new d3_color(); + d3_labPrototype.brighter = function(k) { + return new d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); + }; + d3_labPrototype.darker = function(k) { + return new d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); + }; + d3_labPrototype.rgb = function() { + return d3_lab_rgb(this.l, this.a, this.b); + }; + function d3_lab_rgb(l, a, b) { + var y = (l + 16) / 116, x = y + a / 500, z = y - b / 200; + x = d3_lab_xyz(x) * d3_lab_X; + y = d3_lab_xyz(y) * d3_lab_Y; + z = d3_lab_xyz(z) * d3_lab_Z; + return new d3_rgb(d3_xyz_rgb(3.2404542 * x - 1.5371385 * y - .4985314 * z), d3_xyz_rgb(-.969266 * x + 1.8760108 * y + .041556 * z), d3_xyz_rgb(.0556434 * x - .2040259 * y + 1.0572252 * z)); + } + function d3_lab_hcl(l, a, b) { + return l > 0 ? new d3_hcl(Math.atan2(b, a) * d3_degrees, Math.sqrt(a * a + b * b), l) : new d3_hcl(NaN, NaN, l); + } + function d3_lab_xyz(x) { + return x > .206893034 ? x * x * x : (x - 4 / 29) / 7.787037; + } + function d3_xyz_lab(x) { + return x > .008856 ? Math.pow(x, 1 / 3) : 7.787037 * x + 4 / 29; + } + function d3_xyz_rgb(r) { + return Math.round(255 * (r <= .00304 ? 12.92 * r : 1.055 * Math.pow(r, 1 / 2.4) - .055)); + } + d3.rgb = d3_rgb; + function d3_rgb(r, g, b) { + return this instanceof d3_rgb ? void (this.r = ~~r, this.g = ~~g, this.b = ~~b) : arguments.length < 2 ? r instanceof d3_rgb ? new d3_rgb(r.r, r.g, r.b) : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb) : new d3_rgb(r, g, b); + } + function d3_rgbNumber(value) { + return new d3_rgb(value >> 16, value >> 8 & 255, value & 255); + } + function d3_rgbString(value) { + return d3_rgbNumber(value) + ""; + } + var d3_rgbPrototype = d3_rgb.prototype = new d3_color(); + d3_rgbPrototype.brighter = function(k) { + k = Math.pow(.7, arguments.length ? k : 1); + var r = this.r, g = this.g, b = this.b, i = 30; + if (!r && !g && !b) return new d3_rgb(i, i, i); + if (r && r < i) r = i; + if (g && g < i) g = i; + if (b && b < i) b = i; + return new d3_rgb(Math.min(255, r / k), Math.min(255, g / k), Math.min(255, b / k)); + }; + d3_rgbPrototype.darker = function(k) { + k = Math.pow(.7, arguments.length ? k : 1); + return new d3_rgb(k * this.r, k * this.g, k * this.b); + }; + d3_rgbPrototype.hsl = function() { + return d3_rgb_hsl(this.r, this.g, this.b); + }; + d3_rgbPrototype.toString = function() { + return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b); + }; + function d3_rgb_hex(v) { + return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16); + } + function d3_rgb_parse(format, rgb, hsl) { + var r = 0, g = 0, b = 0, m1, m2, color; + m1 = /([a-z]+)\((.*)\)/.exec(format = format.toLowerCase()); + if (m1) { + m2 = m1[2].split(","); + switch (m1[1]) { + case "hsl": + { + return hsl(parseFloat(m2[0]), parseFloat(m2[1]) / 100, parseFloat(m2[2]) / 100); + } - var no_eof = true; + case "rgb": + { + return rgb(d3_rgb_parseNumber(m2[0]), d3_rgb_parseNumber(m2[1]), d3_rgb_parseNumber(m2[2])); + } + } + } + if (color = d3_rgb_names.get(format)) { + return rgb(color.r, color.g, color.b); + } + if (format != null && format.charAt(0) === "#" && !isNaN(color = parseInt(format.slice(1), 16))) { + if (format.length === 4) { + r = (color & 3840) >> 4; + r = r >> 4 | r; + g = color & 240; + g = g >> 4 | g; + b = color & 15; + b = b << 4 | b; + } else if (format.length === 7) { + r = (color & 16711680) >> 16; + g = (color & 65280) >> 8; + b = color & 255; + } + } + return rgb(r, g, b); + } + function d3_rgb_hsl(r, g, b) { + var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h, s, l = (max + min) / 2; + if (d) { + s = l < .5 ? d / (max + min) : d / (2 - max - min); + if (r == max) h = (g - b) / d + (g < b ? 6 : 0); else if (g == max) h = (b - r) / d + 2; else h = (r - g) / d + 4; + h *= 60; + } else { + h = NaN; + s = l > 0 && l < 1 ? 0 : h; + } + return new d3_hsl(h, s, l); + } + function d3_rgb_lab(r, g, b) { + r = d3_rgb_xyz(r); + g = d3_rgb_xyz(g); + b = d3_rgb_xyz(b); + var x = d3_xyz_lab((.4124564 * r + .3575761 * g + .1804375 * b) / d3_lab_X), y = d3_xyz_lab((.2126729 * r + .7151522 * g + .072175 * b) / d3_lab_Y), z = d3_xyz_lab((.0193339 * r + .119192 * g + .9503041 * b) / d3_lab_Z); + return d3_lab(116 * y - 16, 500 * (x - y), 200 * (y - z)); + } + function d3_rgb_xyz(r) { + return (r /= 255) <= .04045 ? r / 12.92 : Math.pow((r + .055) / 1.055, 2.4); + } + function d3_rgb_parseNumber(c) { + var f = parseFloat(c); + return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f; + } + var d3_rgb_names = d3.map({ + aliceblue: 15792383, + antiquewhite: 16444375, + aqua: 65535, + aquamarine: 8388564, + azure: 15794175, + beige: 16119260, + bisque: 16770244, + black: 0, + blanchedalmond: 16772045, + blue: 255, + blueviolet: 9055202, + brown: 10824234, + burlywood: 14596231, + cadetblue: 6266528, + chartreuse: 8388352, + chocolate: 13789470, + coral: 16744272, + cornflowerblue: 6591981, + cornsilk: 16775388, + crimson: 14423100, + cyan: 65535, + darkblue: 139, + darkcyan: 35723, + darkgoldenrod: 12092939, + darkgray: 11119017, + darkgreen: 25600, + darkgrey: 11119017, + darkkhaki: 12433259, + darkmagenta: 9109643, + darkolivegreen: 5597999, + darkorange: 16747520, + darkorchid: 10040012, + darkred: 9109504, + darksalmon: 15308410, + darkseagreen: 9419919, + darkslateblue: 4734347, + darkslategray: 3100495, + darkslategrey: 3100495, + darkturquoise: 52945, + darkviolet: 9699539, + deeppink: 16716947, + deepskyblue: 49151, + dimgray: 6908265, + dimgrey: 6908265, + dodgerblue: 2003199, + firebrick: 11674146, + floralwhite: 16775920, + forestgreen: 2263842, + fuchsia: 16711935, + gainsboro: 14474460, + ghostwhite: 16316671, + gold: 16766720, + goldenrod: 14329120, + gray: 8421504, + green: 32768, + greenyellow: 11403055, + grey: 8421504, + honeydew: 15794160, + hotpink: 16738740, + indianred: 13458524, + indigo: 4915330, + ivory: 16777200, + khaki: 15787660, + lavender: 15132410, + lavenderblush: 16773365, + lawngreen: 8190976, + lemonchiffon: 16775885, + lightblue: 11393254, + lightcoral: 15761536, + lightcyan: 14745599, + lightgoldenrodyellow: 16448210, + lightgray: 13882323, + lightgreen: 9498256, + lightgrey: 13882323, + lightpink: 16758465, + lightsalmon: 16752762, + lightseagreen: 2142890, + lightskyblue: 8900346, + lightslategray: 7833753, + lightslategrey: 7833753, + lightsteelblue: 11584734, + lightyellow: 16777184, + lime: 65280, + limegreen: 3329330, + linen: 16445670, + magenta: 16711935, + maroon: 8388608, + mediumaquamarine: 6737322, + mediumblue: 205, + mediumorchid: 12211667, + mediumpurple: 9662683, + mediumseagreen: 3978097, + mediumslateblue: 8087790, + mediumspringgreen: 64154, + mediumturquoise: 4772300, + mediumvioletred: 13047173, + midnightblue: 1644912, + mintcream: 16121850, + mistyrose: 16770273, + moccasin: 16770229, + navajowhite: 16768685, + navy: 128, + oldlace: 16643558, + olive: 8421376, + olivedrab: 7048739, + orange: 16753920, + orangered: 16729344, + orchid: 14315734, + palegoldenrod: 15657130, + palegreen: 10025880, + paleturquoise: 11529966, + palevioletred: 14381203, + papayawhip: 16773077, + peachpuff: 16767673, + peru: 13468991, + pink: 16761035, + plum: 14524637, + powderblue: 11591910, + purple: 8388736, + rebeccapurple: 6697881, + red: 16711680, + rosybrown: 12357519, + royalblue: 4286945, + saddlebrown: 9127187, + salmon: 16416882, + sandybrown: 16032864, + seagreen: 3050327, + seashell: 16774638, + sienna: 10506797, + silver: 12632256, + skyblue: 8900331, + slateblue: 6970061, + slategray: 7372944, + slategrey: 7372944, + snow: 16775930, + springgreen: 65407, + steelblue: 4620980, + tan: 13808780, + teal: 32896, + thistle: 14204888, + tomato: 16737095, + turquoise: 4251856, + violet: 15631086, + wheat: 16113331, + white: 16777215, + whitesmoke: 16119285, + yellow: 16776960, + yellowgreen: 10145074 + }); + d3_rgb_names.forEach(function(key, value) { + d3_rgb_names.set(key, d3_rgbNumber(value)); + }); + function d3_functor(v) { + return typeof v === "function" ? v : function() { + return v; + }; + } + d3.functor = d3_functor; + d3.xhr = d3_xhrType(d3_identity); + function d3_xhrType(response) { + return function(url, mimeType, callback) { + if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType, + mimeType = null; + return d3_xhr(url, mimeType, response, callback); + }; + } + function d3_xhr(url, mimeType, response, callback) { + var xhr = {}, dispatch = d3.dispatch("beforesend", "progress", "load", "error"), headers = {}, request = new XMLHttpRequest(), responseType = null; + if (this.XDomainRequest && !("withCredentials" in request) && /^(http(s)?:)?\/\//.test(url)) request = new XDomainRequest(); + "onload" in request ? request.onload = request.onerror = respond : request.onreadystatechange = function() { + request.readyState > 3 && respond(); + }; + function respond() { + var status = request.status, result; + if (!status && d3_xhrHasResponse(request) || status >= 200 && status < 300 || status === 304) { + try { + result = response.call(xhr, request); + } catch (e) { + dispatch.error.call(xhr, e); + return; + } + dispatch.load.call(xhr, result); + } else { + dispatch.error.call(xhr, request); + } + } + request.onprogress = function(event) { + var o = d3.event; + d3.event = event; + try { + dispatch.progress.call(xhr, request); + } finally { + d3.event = o; + } + }; + xhr.header = function(name, value) { + name = (name + "").toLowerCase(); + if (arguments.length < 2) return headers[name]; + if (value == null) delete headers[name]; else headers[name] = value + ""; + return xhr; + }; + xhr.mimeType = function(value) { + if (!arguments.length) return mimeType; + mimeType = value == null ? null : value + ""; + return xhr; + }; + xhr.responseType = function(value) { + if (!arguments.length) return responseType; + responseType = value; + return xhr; + }; + xhr.response = function(value) { + response = value; + return xhr; + }; + [ "get", "post" ].forEach(function(method) { + xhr[method] = function() { + return xhr.send.apply(xhr, [ method ].concat(d3_array(arguments))); + }; + }); + xhr.send = function(method, data, callback) { + if (arguments.length === 2 && typeof data === "function") callback = data, data = null; + request.open(method, url, true); + if (mimeType != null && !("accept" in headers)) headers["accept"] = mimeType + ",*/*"; + if (request.setRequestHeader) for (var name in headers) request.setRequestHeader(name, headers[name]); + if (mimeType != null && request.overrideMimeType) request.overrideMimeType(mimeType); + if (responseType != null) request.responseType = responseType; + if (callback != null) xhr.on("error", callback).on("load", function(request) { + callback(null, request); + }); + dispatch.beforesend.call(xhr, request); + request.send(data == null ? null : data); + return xhr; + }; + xhr.abort = function() { + request.abort(); + return xhr; + }; + d3.rebind(xhr, dispatch, "on"); + return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback)); + } + function d3_xhr_fixCallback(callback) { + return callback.length === 1 ? function(error, request) { + callback(error == null ? request : null); + } : callback; + } + function d3_xhrHasResponse(request) { + var type = request.responseType; + return type && type !== "text" ? request.response : request.responseText; + } + d3.dsv = function(delimiter, mimeType) { + var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0); + function dsv(url, row, callback) { + if (arguments.length < 3) callback = row, row = null; + var xhr = d3_xhr(url, mimeType, row == null ? response : typedResponse(row), callback); + xhr.row = function(_) { + return arguments.length ? xhr.response((row = _) == null ? response : typedResponse(_)) : row; + }; + return xhr; + } + function response(request) { + return dsv.parse(request.responseText); + } + function typedResponse(f) { + return function(request) { + return dsv.parse(request.responseText, f); + }; + } + dsv.parse = function(text, f) { + var o; + return dsv.parseRows(text, function(row, i) { + if (o) return o(row, i - 1); + var a = new Function("d", "return {" + row.map(function(name, i) { + return JSON.stringify(name) + ": d[" + i + "]"; + }).join(",") + "}"); + o = f ? function(row, i) { + return f(a(row), i); + } : a; + }); + }; + dsv.parseRows = function(text, f) { + var EOL = {}, EOF = {}, rows = [], N = text.length, I = 0, n = 0, t, eol; + function token() { + if (I >= N) return EOF; + if (eol) return eol = false, EOL; + var j = I; + if (text.charCodeAt(j) === 34) { + var i = j; + while (i++ < N) { + if (text.charCodeAt(i) === 34) { + if (text.charCodeAt(i + 1) !== 34) break; + ++i; + } + } + I = i + 2; + var c = text.charCodeAt(i + 1); + if (c === 13) { + eol = true; + if (text.charCodeAt(i + 2) === 10) ++I; + } else if (c === 10) { + eol = true; + } + return text.slice(j + 1, i).replace(/""/g, '"'); + } + while (I < N) { + var c = text.charCodeAt(I++), k = 1; + if (c === 10) eol = true; else if (c === 13) { + eol = true; + if (text.charCodeAt(I) === 10) ++I, ++k; + } else if (c !== delimiterCode) continue; + return text.slice(j, I - k); + } + return text.slice(j); + } + while ((t = token()) !== EOF) { + var a = []; + while (t !== EOL && t !== EOF) { + a.push(t); + t = token(); + } + if (f && (a = f(a, n++)) == null) continue; + rows.push(a); + } + return rows; + }; + dsv.format = function(rows) { + if (Array.isArray(rows[0])) return dsv.formatRows(rows); + var fieldSet = new d3_Set(), fields = []; + rows.forEach(function(row) { + for (var field in row) { + if (!fieldSet.has(field)) { + fields.push(fieldSet.add(field)); + } + } + }); + return [ fields.map(formatValue).join(delimiter) ].concat(rows.map(function(row) { + return fields.map(function(field) { + return formatValue(row[field]); + }).join(delimiter); + })).join("\n"); + }; + dsv.formatRows = function(rows) { + return rows.map(formatRow).join("\n"); + }; + function formatRow(row) { + return row.map(formatValue).join(delimiter); + } + function formatValue(text) { + return reFormat.test(text) ? '"' + text.replace(/\"/g, '""') + '"' : text; + } + return dsv; + }; + d3.csv = d3.dsv(",", "text/csv"); + d3.tsv = d3.dsv(" ", "text/tab-separated-values"); + var d3_timer_queueHead, d3_timer_queueTail, d3_timer_interval, d3_timer_timeout, d3_timer_frame = this[d3_vendorSymbol(this, "requestAnimationFrame")] || function(callback) { + setTimeout(callback, 17); + }; + d3.timer = function() { + d3_timer.apply(this, arguments); + }; + function d3_timer(callback, delay, then) { + var n = arguments.length; + if (n < 2) delay = 0; + if (n < 3) then = Date.now(); + var time = then + delay, timer = { + c: callback, + t: time, + n: null + }; + if (d3_timer_queueTail) d3_timer_queueTail.n = timer; else d3_timer_queueHead = timer; + d3_timer_queueTail = timer; + if (!d3_timer_interval) { + d3_timer_timeout = clearTimeout(d3_timer_timeout); + d3_timer_interval = 1; + d3_timer_frame(d3_timer_step); + } + return timer; + } + function d3_timer_step() { + var now = d3_timer_mark(), delay = d3_timer_sweep() - now; + if (delay > 24) { + if (isFinite(delay)) { + clearTimeout(d3_timer_timeout); + d3_timer_timeout = setTimeout(d3_timer_step, delay); + } + d3_timer_interval = 0; + } else { + d3_timer_interval = 1; + d3_timer_frame(d3_timer_step); + } + } + d3.timer.flush = function() { + d3_timer_mark(); + d3_timer_sweep(); + }; + function d3_timer_mark() { + var now = Date.now(), timer = d3_timer_queueHead; + while (timer) { + if (now >= timer.t && timer.c(now - timer.t)) timer.c = null; + timer = timer.n; + } + return now; + } + function d3_timer_sweep() { + var t0, t1 = d3_timer_queueHead, time = Infinity; + while (t1) { + if (t1.c) { + if (t1.t < time) time = t1.t; + t1 = (t0 = t1).n; + } else { + t1 = t0 ? t0.n = t1.n : d3_timer_queueHead = t1.n; + } + } + d3_timer_queueTail = t0; + return time; + } + function d3_format_precision(x, p) { + return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1); + } + d3.round = function(x, n) { + return n ? Math.round(x * (n = Math.pow(10, n))) / n : Math.round(x); + }; + var d3_formatPrefixes = [ "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" ].map(d3_formatPrefix); + d3.formatPrefix = function(value, precision) { + var i = 0; + if (value = +value) { + if (value < 0) value *= -1; + if (precision) value = d3.round(value, d3_format_precision(value, precision)); + i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10); + i = Math.max(-24, Math.min(24, Math.floor((i - 1) / 3) * 3)); + } + return d3_formatPrefixes[8 + i / 3]; + }; + function d3_formatPrefix(d, i) { + var k = Math.pow(10, abs(8 - i) * 3); + return { + scale: i > 8 ? function(d) { + return d / k; + } : function(d) { + return d * k; + }, + symbol: d + }; + } + function d3_locale_numberFormat(locale) { + var locale_decimal = locale.decimal, locale_thousands = locale.thousands, locale_grouping = locale.grouping, locale_currency = locale.currency, formatGroup = locale_grouping && locale_thousands ? function(value, width) { + var i = value.length, t = [], j = 0, g = locale_grouping[0], length = 0; + while (i > 0 && g > 0) { + if (length + g + 1 > width) g = Math.max(1, width - length); + t.push(value.substring(i -= g, i + g)); + if ((length += g + 1) > width) break; + g = locale_grouping[j = (j + 1) % locale_grouping.length]; + } + return t.reverse().join(locale_thousands); + } : d3_identity; + return function(specifier) { + var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "-", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false, exponent = true; + if (precision) precision = +precision.substring(1); + if (zfill || fill === "0" && align === "=") { + zfill = fill = "0"; + align = "="; + } + switch (type) { + case "n": + comma = true; + type = "g"; + break; - var frames = [ ]; + case "%": + scale = 100; + suffix = "%"; + type = "f"; + break; - var delay = 0; - var transparent_index = null; - var disposal = 0; // 0 - No disposal specified. - var loop_count = null; + case "p": + scale = 100; + suffix = "%"; + type = "r"; + break; - this.width = width; - this.height = height; + case "b": + case "o": + case "x": + case "X": + if (symbol === "#") prefix = "0" + type.toLowerCase(); - while (no_eof && p < buf.length) { - switch (buf[p++]) { - case 0x21: // Graphics Control Extension Block - switch (buf[p++]) { - case 0xff: // Application specific block - // Try if it's a Netscape block (with animation loop counter). - if (buf[p ] !== 0x0b || // 21 FF already read, check block size. - // NETSCAPE2.0 - buf[p+1 ] == 0x4e && buf[p+2 ] == 0x45 && buf[p+3 ] == 0x54 && - buf[p+4 ] == 0x53 && buf[p+5 ] == 0x43 && buf[p+6 ] == 0x41 && - buf[p+7 ] == 0x50 && buf[p+8 ] == 0x45 && buf[p+9 ] == 0x32 && - buf[p+10] == 0x2e && buf[p+11] == 0x30 && - // Sub-block - buf[p+12] == 0x03 && buf[p+13] == 0x01 && buf[p+16] == 0) { - p += 14; - loop_count = buf[p++] | buf[p++] << 8; - p++; // Skip terminator. - } else { // We don't know what it is, just try to get past it. - p += 12; - while (true) { // Seek through subblocks. - var block_size = buf[p++]; - if (block_size === 0) break; - p += block_size; + case "c": + exponent = false; + + case "d": + integer = true; + precision = 0; + break; + + case "s": + scale = -1; + type = "r"; + break; + } + if (symbol === "$") prefix = locale_currency[0], suffix = locale_currency[1]; + if (type == "r" && !precision) type = "g"; + if (precision != null) { + if (type == "g") precision = Math.max(1, Math.min(21, precision)); else if (type == "e" || type == "f") precision = Math.max(0, Math.min(20, precision)); + } + type = d3_format_types.get(type) || d3_format_typeDefault; + var zcomma = zfill && comma; + return function(value) { + var fullSuffix = suffix; + if (integer && value % 1) return ""; + var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign === "-" ? "" : sign; + if (scale < 0) { + var unit = d3.formatPrefix(value, precision); + value = unit.scale(value); + fullSuffix = unit.symbol + suffix; + } else { + value *= scale; + } + value = type(value, precision); + var i = value.lastIndexOf("."), before, after; + if (i < 0) { + var j = exponent ? value.lastIndexOf("e") : -1; + if (j < 0) before = value, after = ""; else before = value.substring(0, j), after = value.substring(j); + } else { + before = value.substring(0, i); + after = locale_decimal + value.substring(i + 1); + } + if (!zfill && comma) before = formatGroup(before, Infinity); + var length = prefix.length + before.length + after.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : ""; + if (zcomma) before = formatGroup(padding + before, padding.length ? width - after.length : Infinity); + negative += prefix; + value = before + after; + return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + fullSuffix; + }; + }; + } + var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i; + var d3_format_types = d3.map({ + b: function(x) { + return x.toString(2); + }, + c: function(x) { + return String.fromCharCode(x); + }, + o: function(x) { + return x.toString(8); + }, + x: function(x) { + return x.toString(16); + }, + X: function(x) { + return x.toString(16).toUpperCase(); + }, + g: function(x, p) { + return x.toPrecision(p); + }, + e: function(x, p) { + return x.toExponential(p); + }, + f: function(x, p) { + return x.toFixed(p); + }, + r: function(x, p) { + return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + 1e-15), p)))); + } + }); + function d3_format_typeDefault(x) { + return x + ""; + } + var d3_time = d3.time = {}, d3_date = Date; + function d3_date_utc() { + this._ = new Date(arguments.length > 1 ? Date.UTC.apply(this, arguments) : arguments[0]); + } + d3_date_utc.prototype = { + getDate: function() { + return this._.getUTCDate(); + }, + getDay: function() { + return this._.getUTCDay(); + }, + getFullYear: function() { + return this._.getUTCFullYear(); + }, + getHours: function() { + return this._.getUTCHours(); + }, + getMilliseconds: function() { + return this._.getUTCMilliseconds(); + }, + getMinutes: function() { + return this._.getUTCMinutes(); + }, + getMonth: function() { + return this._.getUTCMonth(); + }, + getSeconds: function() { + return this._.getUTCSeconds(); + }, + getTime: function() { + return this._.getTime(); + }, + getTimezoneOffset: function() { + return 0; + }, + valueOf: function() { + return this._.valueOf(); + }, + setDate: function() { + d3_time_prototype.setUTCDate.apply(this._, arguments); + }, + setDay: function() { + d3_time_prototype.setUTCDay.apply(this._, arguments); + }, + setFullYear: function() { + d3_time_prototype.setUTCFullYear.apply(this._, arguments); + }, + setHours: function() { + d3_time_prototype.setUTCHours.apply(this._, arguments); + }, + setMilliseconds: function() { + d3_time_prototype.setUTCMilliseconds.apply(this._, arguments); + }, + setMinutes: function() { + d3_time_prototype.setUTCMinutes.apply(this._, arguments); + }, + setMonth: function() { + d3_time_prototype.setUTCMonth.apply(this._, arguments); + }, + setSeconds: function() { + d3_time_prototype.setUTCSeconds.apply(this._, arguments); + }, + setTime: function() { + d3_time_prototype.setTime.apply(this._, arguments); + } + }; + var d3_time_prototype = Date.prototype; + function d3_time_interval(local, step, number) { + function round(date) { + var d0 = local(date), d1 = offset(d0, 1); + return date - d0 < d1 - date ? d0 : d1; + } + function ceil(date) { + step(date = local(new d3_date(date - 1)), 1); + return date; + } + function offset(date, k) { + step(date = new d3_date(+date), k); + return date; + } + function range(t0, t1, dt) { + var time = ceil(t0), times = []; + if (dt > 1) { + while (time < t1) { + if (!(number(time) % dt)) times.push(new Date(+time)); + step(time, 1); + } + } else { + while (time < t1) times.push(new Date(+time)), step(time, 1); + } + return times; + } + function range_utc(t0, t1, dt) { + try { + d3_date = d3_date_utc; + var utc = new d3_date_utc(); + utc._ = t0; + return range(utc, t1, dt); + } finally { + d3_date = Date; + } + } + local.floor = local; + local.round = round; + local.ceil = ceil; + local.offset = offset; + local.range = range; + var utc = local.utc = d3_time_interval_utc(local); + utc.floor = utc; + utc.round = d3_time_interval_utc(round); + utc.ceil = d3_time_interval_utc(ceil); + utc.offset = d3_time_interval_utc(offset); + utc.range = range_utc; + return local; + } + function d3_time_interval_utc(method) { + return function(date, k) { + try { + d3_date = d3_date_utc; + var utc = new d3_date_utc(); + utc._ = date; + return method(utc, k)._; + } finally { + d3_date = Date; + } + }; + } + d3_time.year = d3_time_interval(function(date) { + date = d3_time.day(date); + date.setMonth(0, 1); + return date; + }, function(date, offset) { + date.setFullYear(date.getFullYear() + offset); + }, function(date) { + return date.getFullYear(); + }); + d3_time.years = d3_time.year.range; + d3_time.years.utc = d3_time.year.utc.range; + d3_time.day = d3_time_interval(function(date) { + var day = new d3_date(2e3, 0); + day.setFullYear(date.getFullYear(), date.getMonth(), date.getDate()); + return day; + }, function(date, offset) { + date.setDate(date.getDate() + offset); + }, function(date) { + return date.getDate() - 1; + }); + d3_time.days = d3_time.day.range; + d3_time.days.utc = d3_time.day.utc.range; + d3_time.dayOfYear = function(date) { + var year = d3_time.year(date); + return Math.floor((date - year - (date.getTimezoneOffset() - year.getTimezoneOffset()) * 6e4) / 864e5); + }; + [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ].forEach(function(day, i) { + i = 7 - i; + var interval = d3_time[day] = d3_time_interval(function(date) { + (date = d3_time.day(date)).setDate(date.getDate() - (date.getDay() + i) % 7); + return date; + }, function(date, offset) { + date.setDate(date.getDate() + Math.floor(offset) * 7); + }, function(date) { + var day = d3_time.year(date).getDay(); + return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7) - (day !== i); + }); + d3_time[day + "s"] = interval.range; + d3_time[day + "s"].utc = interval.utc.range; + d3_time[day + "OfYear"] = function(date) { + var day = d3_time.year(date).getDay(); + return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7); + }; + }); + d3_time.week = d3_time.sunday; + d3_time.weeks = d3_time.sunday.range; + d3_time.weeks.utc = d3_time.sunday.utc.range; + d3_time.weekOfYear = d3_time.sundayOfYear; + function d3_locale_timeFormat(locale) { + var locale_dateTime = locale.dateTime, locale_date = locale.date, locale_time = locale.time, locale_periods = locale.periods, locale_days = locale.days, locale_shortDays = locale.shortDays, locale_months = locale.months, locale_shortMonths = locale.shortMonths; + function d3_time_format(template) { + var n = template.length; + function format(date) { + var string = [], i = -1, j = 0, c, p, f; + while (++i < n) { + if (template.charCodeAt(i) === 37) { + string.push(template.slice(j, i)); + if ((p = d3_time_formatPads[c = template.charAt(++i)]) != null) c = template.charAt(++i); + if (f = d3_time_formats[c]) c = f(date, p == null ? c === "e" ? " " : "0" : p); + string.push(c); + j = i + 1; + } + } + string.push(template.slice(j, i)); + return string.join(""); + } + format.parse = function(string) { + var d = { + y: 1900, + m: 0, + d: 1, + H: 0, + M: 0, + S: 0, + L: 0, + Z: null + }, i = d3_time_parse(d, template, string, 0); + if (i != string.length) return null; + if ("p" in d) d.H = d.H % 12 + d.p * 12; + var localZ = d.Z != null && d3_date !== d3_date_utc, date = new (localZ ? d3_date_utc : d3_date)(); + if ("j" in d) date.setFullYear(d.y, 0, d.j); else if ("W" in d || "U" in d) { + if (!("w" in d)) d.w = "W" in d ? 1 : 0; + date.setFullYear(d.y, 0, 1); + date.setFullYear(d.y, 0, "W" in d ? (d.w + 6) % 7 + d.W * 7 - (date.getDay() + 5) % 7 : d.w + d.U * 7 - (date.getDay() + 6) % 7); + } else date.setFullYear(d.y, d.m, d.d); + date.setHours(d.H + (d.Z / 100 | 0), d.M + d.Z % 100, d.S, d.L); + return localZ ? date._ : date; + }; + format.toString = function() { + return template; + }; + return format; + } + function d3_time_parse(date, template, string, j) { + var c, p, t, i = 0, n = template.length, m = string.length; + while (i < n) { + if (j >= m) return -1; + c = template.charCodeAt(i++); + if (c === 37) { + t = template.charAt(i++); + p = d3_time_parsers[t in d3_time_formatPads ? template.charAt(i++) : t]; + if (!p || (j = p(date, string, j)) < 0) return -1; + } else if (c != string.charCodeAt(j++)) { + return -1; + } + } + return j; + } + d3_time_format.utc = function(template) { + var local = d3_time_format(template); + function format(date) { + try { + d3_date = d3_date_utc; + var utc = new d3_date(); + utc._ = date; + return local(utc); + } finally { + d3_date = Date; + } + } + format.parse = function(string) { + try { + d3_date = d3_date_utc; + var date = local.parse(string); + return date && date._; + } finally { + d3_date = Date; + } + }; + format.toString = local.toString; + return format; + }; + d3_time_format.multi = d3_time_format.utc.multi = d3_time_formatMulti; + var d3_time_periodLookup = d3.map(), d3_time_dayRe = d3_time_formatRe(locale_days), d3_time_dayLookup = d3_time_formatLookup(locale_days), d3_time_dayAbbrevRe = d3_time_formatRe(locale_shortDays), d3_time_dayAbbrevLookup = d3_time_formatLookup(locale_shortDays), d3_time_monthRe = d3_time_formatRe(locale_months), d3_time_monthLookup = d3_time_formatLookup(locale_months), d3_time_monthAbbrevRe = d3_time_formatRe(locale_shortMonths), d3_time_monthAbbrevLookup = d3_time_formatLookup(locale_shortMonths); + locale_periods.forEach(function(p, i) { + d3_time_periodLookup.set(p.toLowerCase(), i); + }); + var d3_time_formats = { + a: function(d) { + return locale_shortDays[d.getDay()]; + }, + A: function(d) { + return locale_days[d.getDay()]; + }, + b: function(d) { + return locale_shortMonths[d.getMonth()]; + }, + B: function(d) { + return locale_months[d.getMonth()]; + }, + c: d3_time_format(locale_dateTime), + d: function(d, p) { + return d3_time_formatPad(d.getDate(), p, 2); + }, + e: function(d, p) { + return d3_time_formatPad(d.getDate(), p, 2); + }, + H: function(d, p) { + return d3_time_formatPad(d.getHours(), p, 2); + }, + I: function(d, p) { + return d3_time_formatPad(d.getHours() % 12 || 12, p, 2); + }, + j: function(d, p) { + return d3_time_formatPad(1 + d3_time.dayOfYear(d), p, 3); + }, + L: function(d, p) { + return d3_time_formatPad(d.getMilliseconds(), p, 3); + }, + m: function(d, p) { + return d3_time_formatPad(d.getMonth() + 1, p, 2); + }, + M: function(d, p) { + return d3_time_formatPad(d.getMinutes(), p, 2); + }, + p: function(d) { + return locale_periods[+(d.getHours() >= 12)]; + }, + S: function(d, p) { + return d3_time_formatPad(d.getSeconds(), p, 2); + }, + U: function(d, p) { + return d3_time_formatPad(d3_time.sundayOfYear(d), p, 2); + }, + w: function(d) { + return d.getDay(); + }, + W: function(d, p) { + return d3_time_formatPad(d3_time.mondayOfYear(d), p, 2); + }, + x: d3_time_format(locale_date), + X: d3_time_format(locale_time), + y: function(d, p) { + return d3_time_formatPad(d.getFullYear() % 100, p, 2); + }, + Y: function(d, p) { + return d3_time_formatPad(d.getFullYear() % 1e4, p, 4); + }, + Z: d3_time_zone, + "%": function() { + return "%"; + } + }; + var d3_time_parsers = { + a: d3_time_parseWeekdayAbbrev, + A: d3_time_parseWeekday, + b: d3_time_parseMonthAbbrev, + B: d3_time_parseMonth, + c: d3_time_parseLocaleFull, + d: d3_time_parseDay, + e: d3_time_parseDay, + H: d3_time_parseHour24, + I: d3_time_parseHour24, + j: d3_time_parseDayOfYear, + L: d3_time_parseMilliseconds, + m: d3_time_parseMonthNumber, + M: d3_time_parseMinutes, + p: d3_time_parseAmPm, + S: d3_time_parseSeconds, + U: d3_time_parseWeekNumberSunday, + w: d3_time_parseWeekdayNumber, + W: d3_time_parseWeekNumberMonday, + x: d3_time_parseLocaleDate, + X: d3_time_parseLocaleTime, + y: d3_time_parseYear, + Y: d3_time_parseFullYear, + Z: d3_time_parseZone, + "%": d3_time_parseLiteralPercent + }; + function d3_time_parseWeekdayAbbrev(date, string, i) { + d3_time_dayAbbrevRe.lastIndex = 0; + var n = d3_time_dayAbbrevRe.exec(string.slice(i)); + return n ? (date.w = d3_time_dayAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; + } + function d3_time_parseWeekday(date, string, i) { + d3_time_dayRe.lastIndex = 0; + var n = d3_time_dayRe.exec(string.slice(i)); + return n ? (date.w = d3_time_dayLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; + } + function d3_time_parseMonthAbbrev(date, string, i) { + d3_time_monthAbbrevRe.lastIndex = 0; + var n = d3_time_monthAbbrevRe.exec(string.slice(i)); + return n ? (date.m = d3_time_monthAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; + } + function d3_time_parseMonth(date, string, i) { + d3_time_monthRe.lastIndex = 0; + var n = d3_time_monthRe.exec(string.slice(i)); + return n ? (date.m = d3_time_monthLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; + } + function d3_time_parseLocaleFull(date, string, i) { + return d3_time_parse(date, d3_time_formats.c.toString(), string, i); + } + function d3_time_parseLocaleDate(date, string, i) { + return d3_time_parse(date, d3_time_formats.x.toString(), string, i); + } + function d3_time_parseLocaleTime(date, string, i) { + return d3_time_parse(date, d3_time_formats.X.toString(), string, i); + } + function d3_time_parseAmPm(date, string, i) { + var n = d3_time_periodLookup.get(string.slice(i, i += 2).toLowerCase()); + return n == null ? -1 : (date.p = n, i); + } + return d3_time_format; + } + var d3_time_formatPads = { + "-": "", + _: " ", + "0": "0" + }, d3_time_numberRe = /^\s*\d+/, d3_time_percentRe = /^%/; + function d3_time_formatPad(value, fill, width) { + var sign = value < 0 ? "-" : "", string = (sign ? -value : value) + "", length = string.length; + return sign + (length < width ? new Array(width - length + 1).join(fill) + string : string); + } + function d3_time_formatRe(names) { + return new RegExp("^(?:" + names.map(d3.requote).join("|") + ")", "i"); + } + function d3_time_formatLookup(names) { + var map = new d3_Map(), i = -1, n = names.length; + while (++i < n) map.set(names[i].toLowerCase(), i); + return map; + } + function d3_time_parseWeekdayNumber(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 1)); + return n ? (date.w = +n[0], i + n[0].length) : -1; + } + function d3_time_parseWeekNumberSunday(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i)); + return n ? (date.U = +n[0], i + n[0].length) : -1; + } + function d3_time_parseWeekNumberMonday(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i)); + return n ? (date.W = +n[0], i + n[0].length) : -1; + } + function d3_time_parseFullYear(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 4)); + return n ? (date.y = +n[0], i + n[0].length) : -1; + } + function d3_time_parseYear(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 2)); + return n ? (date.y = d3_time_expandYear(+n[0]), i + n[0].length) : -1; + } + function d3_time_parseZone(date, string, i) { + return /^[+-]\d{4}$/.test(string = string.slice(i, i + 5)) ? (date.Z = -string, + i + 5) : -1; + } + function d3_time_expandYear(d) { + return d + (d > 68 ? 1900 : 2e3); + } + function d3_time_parseMonthNumber(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 2)); + return n ? (date.m = n[0] - 1, i + n[0].length) : -1; + } + function d3_time_parseDay(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 2)); + return n ? (date.d = +n[0], i + n[0].length) : -1; + } + function d3_time_parseDayOfYear(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 3)); + return n ? (date.j = +n[0], i + n[0].length) : -1; + } + function d3_time_parseHour24(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 2)); + return n ? (date.H = +n[0], i + n[0].length) : -1; + } + function d3_time_parseMinutes(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 2)); + return n ? (date.M = +n[0], i + n[0].length) : -1; + } + function d3_time_parseSeconds(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 2)); + return n ? (date.S = +n[0], i + n[0].length) : -1; + } + function d3_time_parseMilliseconds(date, string, i) { + d3_time_numberRe.lastIndex = 0; + var n = d3_time_numberRe.exec(string.slice(i, i + 3)); + return n ? (date.L = +n[0], i + n[0].length) : -1; + } + function d3_time_zone(d) { + var z = d.getTimezoneOffset(), zs = z > 0 ? "-" : "+", zh = abs(z) / 60 | 0, zm = abs(z) % 60; + return zs + d3_time_formatPad(zh, "0", 2) + d3_time_formatPad(zm, "0", 2); + } + function d3_time_parseLiteralPercent(date, string, i) { + d3_time_percentRe.lastIndex = 0; + var n = d3_time_percentRe.exec(string.slice(i, i + 1)); + return n ? i + n[0].length : -1; + } + function d3_time_formatMulti(formats) { + var n = formats.length, i = -1; + while (++i < n) formats[i][0] = this(formats[i][0]); + return function(date) { + var i = 0, f = formats[i]; + while (!f[1](date)) f = formats[++i]; + return f[0](date); + }; + } + d3.locale = function(locale) { + return { + numberFormat: d3_locale_numberFormat(locale), + timeFormat: d3_locale_timeFormat(locale) + }; + }; + var d3_locale_enUS = d3.locale({ + decimal: ".", + thousands: ",", + grouping: [ 3 ], + currency: [ "$", "" ], + dateTime: "%a %b %e %X %Y", + date: "%m/%d/%Y", + time: "%H:%M:%S", + periods: [ "AM", "PM" ], + days: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], + shortDays: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], + months: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], + shortMonths: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] + }); + d3.format = d3_locale_enUS.numberFormat; + d3.geo = {}; + function d3_adder() {} + d3_adder.prototype = { + s: 0, + t: 0, + add: function(y) { + d3_adderSum(y, this.t, d3_adderTemp); + d3_adderSum(d3_adderTemp.s, this.s, this); + if (this.s) this.t += d3_adderTemp.t; else this.s = d3_adderTemp.t; + }, + reset: function() { + this.s = this.t = 0; + }, + valueOf: function() { + return this.s; + } + }; + var d3_adderTemp = new d3_adder(); + function d3_adderSum(a, b, o) { + var x = o.s = a + b, bv = x - a, av = x - bv; + o.t = a - av + (b - bv); + } + d3.geo.stream = function(object, listener) { + if (object && d3_geo_streamObjectType.hasOwnProperty(object.type)) { + d3_geo_streamObjectType[object.type](object, listener); + } else { + d3_geo_streamGeometry(object, listener); + } + }; + function d3_geo_streamGeometry(geometry, listener) { + if (geometry && d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) { + d3_geo_streamGeometryType[geometry.type](geometry, listener); + } + } + var d3_geo_streamObjectType = { + Feature: function(feature, listener) { + d3_geo_streamGeometry(feature.geometry, listener); + }, + FeatureCollection: function(object, listener) { + var features = object.features, i = -1, n = features.length; + while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener); + } + }; + var d3_geo_streamGeometryType = { + Sphere: function(object, listener) { + listener.sphere(); + }, + Point: function(object, listener) { + object = object.coordinates; + listener.point(object[0], object[1], object[2]); + }, + MultiPoint: function(object, listener) { + var coordinates = object.coordinates, i = -1, n = coordinates.length; + while (++i < n) object = coordinates[i], listener.point(object[0], object[1], object[2]); + }, + LineString: function(object, listener) { + d3_geo_streamLine(object.coordinates, listener, 0); + }, + MultiLineString: function(object, listener) { + var coordinates = object.coordinates, i = -1, n = coordinates.length; + while (++i < n) d3_geo_streamLine(coordinates[i], listener, 0); + }, + Polygon: function(object, listener) { + d3_geo_streamPolygon(object.coordinates, listener); + }, + MultiPolygon: function(object, listener) { + var coordinates = object.coordinates, i = -1, n = coordinates.length; + while (++i < n) d3_geo_streamPolygon(coordinates[i], listener); + }, + GeometryCollection: function(object, listener) { + var geometries = object.geometries, i = -1, n = geometries.length; + while (++i < n) d3_geo_streamGeometry(geometries[i], listener); + } + }; + function d3_geo_streamLine(coordinates, listener, closed) { + var i = -1, n = coordinates.length - closed, coordinate; + listener.lineStart(); + while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1], coordinate[2]); + listener.lineEnd(); + } + function d3_geo_streamPolygon(coordinates, listener) { + var i = -1, n = coordinates.length; + listener.polygonStart(); + while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1); + listener.polygonEnd(); + } + d3.geo.area = function(object) { + d3_geo_areaSum = 0; + d3.geo.stream(object, d3_geo_area); + return d3_geo_areaSum; + }; + var d3_geo_areaSum, d3_geo_areaRingSum = new d3_adder(); + var d3_geo_area = { + sphere: function() { + d3_geo_areaSum += 4 * π; + }, + point: d3_noop, + lineStart: d3_noop, + lineEnd: d3_noop, + polygonStart: function() { + d3_geo_areaRingSum.reset(); + d3_geo_area.lineStart = d3_geo_areaRingStart; + }, + polygonEnd: function() { + var area = 2 * d3_geo_areaRingSum; + d3_geo_areaSum += area < 0 ? 4 * π + area : area; + d3_geo_area.lineStart = d3_geo_area.lineEnd = d3_geo_area.point = d3_noop; + } + }; + function d3_geo_areaRingStart() { + var λ00, φ00, λ0, cosφ0, sinφ0; + d3_geo_area.point = function(λ, φ) { + d3_geo_area.point = nextPoint; + λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4), + sinφ0 = Math.sin(φ); + }; + function nextPoint(λ, φ) { + λ *= d3_radians; + φ = φ * d3_radians / 2 + π / 4; + var dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, cosφ = Math.cos(φ), sinφ = Math.sin(φ), k = sinφ0 * sinφ, u = cosφ0 * cosφ + k * Math.cos(adλ), v = k * sdλ * Math.sin(adλ); + d3_geo_areaRingSum.add(Math.atan2(v, u)); + λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ; + } + d3_geo_area.lineEnd = function() { + nextPoint(λ00, φ00); + }; + } + function d3_geo_cartesian(spherical) { + var λ = spherical[0], φ = spherical[1], cosφ = Math.cos(φ); + return [ cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ) ]; + } + function d3_geo_cartesianDot(a, b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; + } + function d3_geo_cartesianCross(a, b) { + return [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ]; + } + function d3_geo_cartesianAdd(a, b) { + a[0] += b[0]; + a[1] += b[1]; + a[2] += b[2]; + } + function d3_geo_cartesianScale(vector, k) { + return [ vector[0] * k, vector[1] * k, vector[2] * k ]; + } + function d3_geo_cartesianNormalize(d) { + var l = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); + d[0] /= l; + d[1] /= l; + d[2] /= l; + } + function d3_geo_spherical(cartesian) { + return [ Math.atan2(cartesian[1], cartesian[0]), d3_asin(cartesian[2]) ]; + } + function d3_geo_sphericalEqual(a, b) { + return abs(a[0] - b[0]) < ε && abs(a[1] - b[1]) < ε; + } + d3.geo.bounds = function() { + var λ0, φ0, λ1, φ1, λ_, λ__, φ__, p0, dλSum, ranges, range; + var bound = { + point: point, + lineStart: lineStart, + lineEnd: lineEnd, + polygonStart: function() { + bound.point = ringPoint; + bound.lineStart = ringStart; + bound.lineEnd = ringEnd; + dλSum = 0; + d3_geo_area.polygonStart(); + }, + polygonEnd: function() { + d3_geo_area.polygonEnd(); + bound.point = point; + bound.lineStart = lineStart; + bound.lineEnd = lineEnd; + if (d3_geo_areaRingSum < 0) λ0 = -(λ1 = 180), φ0 = -(φ1 = 90); else if (dλSum > ε) φ1 = 90; else if (dλSum < -ε) φ0 = -90; + range[0] = λ0, range[1] = λ1; + } + }; + function point(λ, φ) { + ranges.push(range = [ λ0 = λ, λ1 = λ ]); + if (φ < φ0) φ0 = φ; + if (φ > φ1) φ1 = φ; + } + function linePoint(λ, φ) { + var p = d3_geo_cartesian([ λ * d3_radians, φ * d3_radians ]); + if (p0) { + var normal = d3_geo_cartesianCross(p0, p), equatorial = [ normal[1], -normal[0], 0 ], inflection = d3_geo_cartesianCross(equatorial, normal); + d3_geo_cartesianNormalize(inflection); + inflection = d3_geo_spherical(inflection); + var dλ = λ - λ_, s = dλ > 0 ? 1 : -1, λi = inflection[0] * d3_degrees * s, antimeridian = abs(dλ) > 180; + if (antimeridian ^ (s * λ_ < λi && λi < s * λ)) { + var φi = inflection[1] * d3_degrees; + if (φi > φ1) φ1 = φi; + } else if (λi = (λi + 360) % 360 - 180, antimeridian ^ (s * λ_ < λi && λi < s * λ)) { + var φi = -inflection[1] * d3_degrees; + if (φi < φ0) φ0 = φi; + } else { + if (φ < φ0) φ0 = φ; + if (φ > φ1) φ1 = φ; + } + if (antimeridian) { + if (λ < λ_) { + if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; + } else { + if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; + } + } else { + if (λ1 >= λ0) { + if (λ < λ0) λ0 = λ; + if (λ > λ1) λ1 = λ; + } else { + if (λ > λ_) { + if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; + } else { + if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; + } + } + } + } else { + point(λ, φ); + } + p0 = p, λ_ = λ; + } + function lineStart() { + bound.point = linePoint; + } + function lineEnd() { + range[0] = λ0, range[1] = λ1; + bound.point = point; + p0 = null; + } + function ringPoint(λ, φ) { + if (p0) { + var dλ = λ - λ_; + dλSum += abs(dλ) > 180 ? dλ + (dλ > 0 ? 360 : -360) : dλ; + } else λ__ = λ, φ__ = φ; + d3_geo_area.point(λ, φ); + linePoint(λ, φ); + } + function ringStart() { + d3_geo_area.lineStart(); + } + function ringEnd() { + ringPoint(λ__, φ__); + d3_geo_area.lineEnd(); + if (abs(dλSum) > ε) λ0 = -(λ1 = 180); + range[0] = λ0, range[1] = λ1; + p0 = null; + } + function angle(λ0, λ1) { + return (λ1 -= λ0) < 0 ? λ1 + 360 : λ1; + } + function compareRanges(a, b) { + return a[0] - b[0]; + } + function withinRange(x, range) { + return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x; + } + return function(feature) { + φ1 = λ1 = -(λ0 = φ0 = Infinity); + ranges = []; + d3.geo.stream(feature, bound); + var n = ranges.length; + if (n) { + ranges.sort(compareRanges); + for (var i = 1, a = ranges[0], b, merged = [ a ]; i < n; ++i) { + b = ranges[i]; + if (withinRange(b[0], a) || withinRange(b[1], a)) { + if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1]; + if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0]; + } else { + merged.push(a = b); + } + } + var best = -Infinity, dλ; + for (var n = merged.length - 1, i = 0, a = merged[n], b; i <= n; a = b, ++i) { + b = merged[i]; + if ((dλ = angle(a[1], b[0])) > best) best = dλ, λ0 = b[0], λ1 = a[1]; + } + } + ranges = range = null; + return λ0 === Infinity || φ0 === Infinity ? [ [ NaN, NaN ], [ NaN, NaN ] ] : [ [ λ0, φ0 ], [ λ1, φ1 ] ]; + }; + }(); + d3.geo.centroid = function(object) { + d3_geo_centroidW0 = d3_geo_centroidW1 = d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; + d3.geo.stream(object, d3_geo_centroid); + var x = d3_geo_centroidX2, y = d3_geo_centroidY2, z = d3_geo_centroidZ2, m = x * x + y * y + z * z; + if (m < ε2) { + x = d3_geo_centroidX1, y = d3_geo_centroidY1, z = d3_geo_centroidZ1; + if (d3_geo_centroidW1 < ε) x = d3_geo_centroidX0, y = d3_geo_centroidY0, z = d3_geo_centroidZ0; + m = x * x + y * y + z * z; + if (m < ε2) return [ NaN, NaN ]; + } + return [ Math.atan2(y, x) * d3_degrees, d3_asin(z / Math.sqrt(m)) * d3_degrees ]; + }; + var d3_geo_centroidW0, d3_geo_centroidW1, d3_geo_centroidX0, d3_geo_centroidY0, d3_geo_centroidZ0, d3_geo_centroidX1, d3_geo_centroidY1, d3_geo_centroidZ1, d3_geo_centroidX2, d3_geo_centroidY2, d3_geo_centroidZ2; + var d3_geo_centroid = { + sphere: d3_noop, + point: d3_geo_centroidPoint, + lineStart: d3_geo_centroidLineStart, + lineEnd: d3_geo_centroidLineEnd, + polygonStart: function() { + d3_geo_centroid.lineStart = d3_geo_centroidRingStart; + }, + polygonEnd: function() { + d3_geo_centroid.lineStart = d3_geo_centroidLineStart; + } + }; + function d3_geo_centroidPoint(λ, φ) { + λ *= d3_radians; + var cosφ = Math.cos(φ *= d3_radians); + d3_geo_centroidPointXYZ(cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ)); + } + function d3_geo_centroidPointXYZ(x, y, z) { + ++d3_geo_centroidW0; + d3_geo_centroidX0 += (x - d3_geo_centroidX0) / d3_geo_centroidW0; + d3_geo_centroidY0 += (y - d3_geo_centroidY0) / d3_geo_centroidW0; + d3_geo_centroidZ0 += (z - d3_geo_centroidZ0) / d3_geo_centroidW0; + } + function d3_geo_centroidLineStart() { + var x0, y0, z0; + d3_geo_centroid.point = function(λ, φ) { + λ *= d3_radians; + var cosφ = Math.cos(φ *= d3_radians); + x0 = cosφ * Math.cos(λ); + y0 = cosφ * Math.sin(λ); + z0 = Math.sin(φ); + d3_geo_centroid.point = nextPoint; + d3_geo_centroidPointXYZ(x0, y0, z0); + }; + function nextPoint(λ, φ) { + λ *= d3_radians; + var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z); + d3_geo_centroidW1 += w; + d3_geo_centroidX1 += w * (x0 + (x0 = x)); + d3_geo_centroidY1 += w * (y0 + (y0 = y)); + d3_geo_centroidZ1 += w * (z0 + (z0 = z)); + d3_geo_centroidPointXYZ(x0, y0, z0); + } + } + function d3_geo_centroidLineEnd() { + d3_geo_centroid.point = d3_geo_centroidPoint; + } + function d3_geo_centroidRingStart() { + var λ00, φ00, x0, y0, z0; + d3_geo_centroid.point = function(λ, φ) { + λ00 = λ, φ00 = φ; + d3_geo_centroid.point = nextPoint; + λ *= d3_radians; + var cosφ = Math.cos(φ *= d3_radians); + x0 = cosφ * Math.cos(λ); + y0 = cosφ * Math.sin(λ); + z0 = Math.sin(φ); + d3_geo_centroidPointXYZ(x0, y0, z0); + }; + d3_geo_centroid.lineEnd = function() { + nextPoint(λ00, φ00); + d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd; + d3_geo_centroid.point = d3_geo_centroidPoint; + }; + function nextPoint(λ, φ) { + λ *= d3_radians; + var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), cx = y0 * z - z0 * y, cy = z0 * x - x0 * z, cz = x0 * y - y0 * x, m = Math.sqrt(cx * cx + cy * cy + cz * cz), u = x0 * x + y0 * y + z0 * z, v = m && -d3_acos(u) / m, w = Math.atan2(m, u); + d3_geo_centroidX2 += v * cx; + d3_geo_centroidY2 += v * cy; + d3_geo_centroidZ2 += v * cz; + d3_geo_centroidW1 += w; + d3_geo_centroidX1 += w * (x0 + (x0 = x)); + d3_geo_centroidY1 += w * (y0 + (y0 = y)); + d3_geo_centroidZ1 += w * (z0 + (z0 = z)); + d3_geo_centroidPointXYZ(x0, y0, z0); + } + } + function d3_geo_compose(a, b) { + function compose(x, y) { + return x = a(x, y), b(x[0], x[1]); + } + if (a.invert && b.invert) compose.invert = function(x, y) { + return x = b.invert(x, y), x && a.invert(x[0], x[1]); + }; + return compose; + } + function d3_true() { + return true; + } + function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) { + var subject = [], clip = []; + segments.forEach(function(segment) { + if ((n = segment.length - 1) <= 0) return; + var n, p0 = segment[0], p1 = segment[n]; + if (d3_geo_sphericalEqual(p0, p1)) { + listener.lineStart(); + for (var i = 0; i < n; ++i) listener.point((p0 = segment[i])[0], p0[1]); + listener.lineEnd(); + return; + } + var a = new d3_geo_clipPolygonIntersection(p0, segment, null, true), b = new d3_geo_clipPolygonIntersection(p0, null, a, false); + a.o = b; + subject.push(a); + clip.push(b); + a = new d3_geo_clipPolygonIntersection(p1, segment, null, false); + b = new d3_geo_clipPolygonIntersection(p1, null, a, true); + a.o = b; + subject.push(a); + clip.push(b); + }); + clip.sort(compare); + d3_geo_clipPolygonLinkCircular(subject); + d3_geo_clipPolygonLinkCircular(clip); + if (!subject.length) return; + for (var i = 0, entry = clipStartInside, n = clip.length; i < n; ++i) { + clip[i].e = entry = !entry; + } + var start = subject[0], points, point; + while (1) { + var current = start, isSubject = true; + while (current.v) if ((current = current.n) === start) return; + points = current.z; + listener.lineStart(); + do { + current.v = current.o.v = true; + if (current.e) { + if (isSubject) { + for (var i = 0, n = points.length; i < n; ++i) listener.point((point = points[i])[0], point[1]); + } else { + interpolate(current.x, current.n.x, 1, listener); + } + current = current.n; + } else { + if (isSubject) { + points = current.p.z; + for (var i = points.length - 1; i >= 0; --i) listener.point((point = points[i])[0], point[1]); + } else { + interpolate(current.x, current.p.x, -1, listener); + } + current = current.p; + } + current = current.o; + points = current.z; + isSubject = !isSubject; + } while (!current.v); + listener.lineEnd(); + } + } + function d3_geo_clipPolygonLinkCircular(array) { + if (!(n = array.length)) return; + var n, i = 0, a = array[0], b; + while (++i < n) { + a.n = b = array[i]; + b.p = a; + a = b; + } + a.n = b = array[0]; + b.p = a; + } + function d3_geo_clipPolygonIntersection(point, points, other, entry) { + this.x = point; + this.z = points; + this.o = other; + this.e = entry; + this.v = false; + this.n = this.p = null; + } + function d3_geo_clip(pointVisible, clipLine, interpolate, clipStart) { + return function(rotate, listener) { + var line = clipLine(listener), rotatedClipStart = rotate.invert(clipStart[0], clipStart[1]); + var clip = { + point: point, + lineStart: lineStart, + lineEnd: lineEnd, + polygonStart: function() { + clip.point = pointRing; + clip.lineStart = ringStart; + clip.lineEnd = ringEnd; + segments = []; + polygon = []; + }, + polygonEnd: function() { + clip.point = point; + clip.lineStart = lineStart; + clip.lineEnd = lineEnd; + segments = d3.merge(segments); + var clipStartInside = d3_geo_pointInPolygon(rotatedClipStart, polygon); + if (segments.length) { + if (!polygonStarted) listener.polygonStart(), polygonStarted = true; + d3_geo_clipPolygon(segments, d3_geo_clipSort, clipStartInside, interpolate, listener); + } else if (clipStartInside) { + if (!polygonStarted) listener.polygonStart(), polygonStarted = true; + listener.lineStart(); + interpolate(null, null, 1, listener); + listener.lineEnd(); + } + if (polygonStarted) listener.polygonEnd(), polygonStarted = false; + segments = polygon = null; + }, + sphere: function() { + listener.polygonStart(); + listener.lineStart(); + interpolate(null, null, 1, listener); + listener.lineEnd(); + listener.polygonEnd(); + } + }; + function point(λ, φ) { + var point = rotate(λ, φ); + if (pointVisible(λ = point[0], φ = point[1])) listener.point(λ, φ); + } + function pointLine(λ, φ) { + var point = rotate(λ, φ); + line.point(point[0], point[1]); + } + function lineStart() { + clip.point = pointLine; + line.lineStart(); + } + function lineEnd() { + clip.point = point; + line.lineEnd(); + } + var segments; + var buffer = d3_geo_clipBufferListener(), ringListener = clipLine(buffer), polygonStarted = false, polygon, ring; + function pointRing(λ, φ) { + ring.push([ λ, φ ]); + var point = rotate(λ, φ); + ringListener.point(point[0], point[1]); + } + function ringStart() { + ringListener.lineStart(); + ring = []; + } + function ringEnd() { + pointRing(ring[0][0], ring[0][1]); + ringListener.lineEnd(); + var clean = ringListener.clean(), ringSegments = buffer.buffer(), segment, n = ringSegments.length; + ring.pop(); + polygon.push(ring); + ring = null; + if (!n) return; + if (clean & 1) { + segment = ringSegments[0]; + var n = segment.length - 1, i = -1, point; + if (n > 0) { + if (!polygonStarted) listener.polygonStart(), polygonStarted = true; + listener.lineStart(); + while (++i < n) listener.point((point = segment[i])[0], point[1]); + listener.lineEnd(); + } + return; + } + if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift())); + segments.push(ringSegments.filter(d3_geo_clipSegmentLength1)); + } + return clip; + }; + } + function d3_geo_clipSegmentLength1(segment) { + return segment.length > 1; + } + function d3_geo_clipBufferListener() { + var lines = [], line; + return { + lineStart: function() { + lines.push(line = []); + }, + point: function(λ, φ) { + line.push([ λ, φ ]); + }, + lineEnd: d3_noop, + buffer: function() { + var buffer = lines; + lines = []; + line = null; + return buffer; + }, + rejoin: function() { + if (lines.length > 1) lines.push(lines.pop().concat(lines.shift())); + } + }; + } + function d3_geo_clipSort(a, b) { + return ((a = a.x)[0] < 0 ? a[1] - halfπ - ε : halfπ - a[1]) - ((b = b.x)[0] < 0 ? b[1] - halfπ - ε : halfπ - b[1]); + } + var d3_geo_clipAntimeridian = d3_geo_clip(d3_true, d3_geo_clipAntimeridianLine, d3_geo_clipAntimeridianInterpolate, [ -π, -π / 2 ]); + function d3_geo_clipAntimeridianLine(listener) { + var λ0 = NaN, φ0 = NaN, sλ0 = NaN, clean; + return { + lineStart: function() { + listener.lineStart(); + clean = 1; + }, + point: function(λ1, φ1) { + var sλ1 = λ1 > 0 ? π : -π, dλ = abs(λ1 - λ0); + if (abs(dλ - π) < ε) { + listener.point(λ0, φ0 = (φ0 + φ1) / 2 > 0 ? halfπ : -halfπ); + listener.point(sλ0, φ0); + listener.lineEnd(); + listener.lineStart(); + listener.point(sλ1, φ0); + listener.point(λ1, φ0); + clean = 0; + } else if (sλ0 !== sλ1 && dλ >= π) { + if (abs(λ0 - sλ0) < ε) λ0 -= sλ0 * ε; + if (abs(λ1 - sλ1) < ε) λ1 -= sλ1 * ε; + φ0 = d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1); + listener.point(sλ0, φ0); + listener.lineEnd(); + listener.lineStart(); + listener.point(sλ1, φ0); + clean = 0; + } + listener.point(λ0 = λ1, φ0 = φ1); + sλ0 = sλ1; + }, + lineEnd: function() { + listener.lineEnd(); + λ0 = φ0 = NaN; + }, + clean: function() { + return 2 - clean; + } + }; + } + function d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1) { + var cosφ0, cosφ1, sinλ0_λ1 = Math.sin(λ0 - λ1); + return abs(sinλ0_λ1) > ε ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1) - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0)) / (cosφ0 * cosφ1 * sinλ0_λ1)) : (φ0 + φ1) / 2; + } + function d3_geo_clipAntimeridianInterpolate(from, to, direction, listener) { + var φ; + if (from == null) { + φ = direction * halfπ; + listener.point(-π, φ); + listener.point(0, φ); + listener.point(π, φ); + listener.point(π, 0); + listener.point(π, -φ); + listener.point(0, -φ); + listener.point(-π, -φ); + listener.point(-π, 0); + listener.point(-π, φ); + } else if (abs(from[0] - to[0]) > ε) { + var s = from[0] < to[0] ? π : -π; + φ = direction * s / 2; + listener.point(-s, φ); + listener.point(0, φ); + listener.point(s, φ); + } else { + listener.point(to[0], to[1]); + } + } + function d3_geo_pointInPolygon(point, polygon) { + var meridian = point[0], parallel = point[1], meridianNormal = [ Math.sin(meridian), -Math.cos(meridian), 0 ], polarAngle = 0, winding = 0; + d3_geo_areaRingSum.reset(); + for (var i = 0, n = polygon.length; i < n; ++i) { + var ring = polygon[i], m = ring.length; + if (!m) continue; + var point0 = ring[0], λ0 = point0[0], φ0 = point0[1] / 2 + π / 4, sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), j = 1; + while (true) { + if (j === m) j = 0; + point = ring[j]; + var λ = point[0], φ = point[1] / 2 + π / 4, sinφ = Math.sin(φ), cosφ = Math.cos(φ), dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, antimeridian = adλ > π, k = sinφ0 * sinφ; + d3_geo_areaRingSum.add(Math.atan2(k * sdλ * Math.sin(adλ), cosφ0 * cosφ + k * Math.cos(adλ))); + polarAngle += antimeridian ? dλ + sdλ * τ : dλ; + if (antimeridian ^ λ0 >= meridian ^ λ >= meridian) { + var arc = d3_geo_cartesianCross(d3_geo_cartesian(point0), d3_geo_cartesian(point)); + d3_geo_cartesianNormalize(arc); + var intersection = d3_geo_cartesianCross(meridianNormal, arc); + d3_geo_cartesianNormalize(intersection); + var φarc = (antimeridian ^ dλ >= 0 ? -1 : 1) * d3_asin(intersection[2]); + if (parallel > φarc || parallel === φarc && (arc[0] || arc[1])) { + winding += antimeridian ^ dλ >= 0 ? 1 : -1; + } + } + if (!j++) break; + λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ, point0 = point; + } + } + return (polarAngle < -ε || polarAngle < ε && d3_geo_areaRingSum < -ε) ^ winding & 1; + } + function d3_geo_clipCircle(radius) { + var cr = Math.cos(radius), smallRadius = cr > 0, notHemisphere = abs(cr) > ε, interpolate = d3_geo_circleInterpolate(radius, 6 * d3_radians); + return d3_geo_clip(visible, clipLine, interpolate, smallRadius ? [ 0, -radius ] : [ -π, radius - π ]); + function visible(λ, φ) { + return Math.cos(λ) * Math.cos(φ) > cr; + } + function clipLine(listener) { + var point0, c0, v0, v00, clean; + return { + lineStart: function() { + v00 = v0 = false; + clean = 1; + }, + point: function(λ, φ) { + var point1 = [ λ, φ ], point2, v = visible(λ, φ), c = smallRadius ? v ? 0 : code(λ, φ) : v ? code(λ + (λ < 0 ? π : -π), φ) : 0; + if (!point0 && (v00 = v0 = v)) listener.lineStart(); + if (v !== v0) { + point2 = intersect(point0, point1); + if (d3_geo_sphericalEqual(point0, point2) || d3_geo_sphericalEqual(point1, point2)) { + point1[0] += ε; + point1[1] += ε; + v = visible(point1[0], point1[1]); + } + } + if (v !== v0) { + clean = 0; + if (v) { + listener.lineStart(); + point2 = intersect(point1, point0); + listener.point(point2[0], point2[1]); + } else { + point2 = intersect(point0, point1); + listener.point(point2[0], point2[1]); + listener.lineEnd(); + } + point0 = point2; + } else if (notHemisphere && point0 && smallRadius ^ v) { + var t; + if (!(c & c0) && (t = intersect(point1, point0, true))) { + clean = 0; + if (smallRadius) { + listener.lineStart(); + listener.point(t[0][0], t[0][1]); + listener.point(t[1][0], t[1][1]); + listener.lineEnd(); + } else { + listener.point(t[1][0], t[1][1]); + listener.lineEnd(); + listener.lineStart(); + listener.point(t[0][0], t[0][1]); } } - break; - - case 0xf9: // Graphics Control Extension - if (buf[p++] !== 0x4 || buf[p+4] !== 0) - throw "Invalid graphics extension block."; - var pf1 = buf[p++]; - delay = buf[p++] | buf[p++] << 8; - transparent_index = buf[p++]; - if ((pf1 & 1) === 0) transparent_index = null; - disposal = pf1 >> 2 & 0x7; - p++; // Skip terminator. - break; - - case 0xfe: // Comment Extension. - while (true) { // Seek through subblocks. - var block_size = buf[p++]; - if (block_size === 0) break; - // console.log(buf.slice(p, p+block_size).toString('ascii')); - p += block_size; + } + if (v && (!point0 || !d3_geo_sphericalEqual(point0, point1))) { + listener.point(point1[0], point1[1]); + } + point0 = point1, v0 = v, c0 = c; + }, + lineEnd: function() { + if (v0) listener.lineEnd(); + point0 = null; + }, + clean: function() { + return clean | (v00 && v0) << 1; + } + }; + } + function intersect(a, b, two) { + var pa = d3_geo_cartesian(a), pb = d3_geo_cartesian(b); + var n1 = [ 1, 0, 0 ], n2 = d3_geo_cartesianCross(pa, pb), n2n2 = d3_geo_cartesianDot(n2, n2), n1n2 = n2[0], determinant = n2n2 - n1n2 * n1n2; + if (!determinant) return !two && a; + var c1 = cr * n2n2 / determinant, c2 = -cr * n1n2 / determinant, n1xn2 = d3_geo_cartesianCross(n1, n2), A = d3_geo_cartesianScale(n1, c1), B = d3_geo_cartesianScale(n2, c2); + d3_geo_cartesianAdd(A, B); + var u = n1xn2, w = d3_geo_cartesianDot(A, u), uu = d3_geo_cartesianDot(u, u), t2 = w * w - uu * (d3_geo_cartesianDot(A, A) - 1); + if (t2 < 0) return; + var t = Math.sqrt(t2), q = d3_geo_cartesianScale(u, (-w - t) / uu); + d3_geo_cartesianAdd(q, A); + q = d3_geo_spherical(q); + if (!two) return q; + var λ0 = a[0], λ1 = b[0], φ0 = a[1], φ1 = b[1], z; + if (λ1 < λ0) z = λ0, λ0 = λ1, λ1 = z; + var δλ = λ1 - λ0, polar = abs(δλ - π) < ε, meridian = polar || δλ < ε; + if (!polar && φ1 < φ0) z = φ0, φ0 = φ1, φ1 = z; + if (meridian ? polar ? φ0 + φ1 > 0 ^ q[1] < (abs(q[0] - λ0) < ε ? φ0 : φ1) : φ0 <= q[1] && q[1] <= φ1 : δλ > π ^ (λ0 <= q[0] && q[0] <= λ1)) { + var q1 = d3_geo_cartesianScale(u, (-w + t) / uu); + d3_geo_cartesianAdd(q1, A); + return [ q, d3_geo_spherical(q1) ]; + } + } + function code(λ, φ) { + var r = smallRadius ? radius : π - radius, code = 0; + if (λ < -r) code |= 1; else if (λ > r) code |= 2; + if (φ < -r) code |= 4; else if (φ > r) code |= 8; + return code; + } + } + function d3_geom_clipLine(x0, y0, x1, y1) { + return function(line) { + var a = line.a, b = line.b, ax = a.x, ay = a.y, bx = b.x, by = b.y, t0 = 0, t1 = 1, dx = bx - ax, dy = by - ay, r; + r = x0 - ax; + if (!dx && r > 0) return; + r /= dx; + if (dx < 0) { + if (r < t0) return; + if (r < t1) t1 = r; + } else if (dx > 0) { + if (r > t1) return; + if (r > t0) t0 = r; + } + r = x1 - ax; + if (!dx && r < 0) return; + r /= dx; + if (dx < 0) { + if (r > t1) return; + if (r > t0) t0 = r; + } else if (dx > 0) { + if (r < t0) return; + if (r < t1) t1 = r; + } + r = y0 - ay; + if (!dy && r > 0) return; + r /= dy; + if (dy < 0) { + if (r < t0) return; + if (r < t1) t1 = r; + } else if (dy > 0) { + if (r > t1) return; + if (r > t0) t0 = r; + } + r = y1 - ay; + if (!dy && r < 0) return; + r /= dy; + if (dy < 0) { + if (r > t1) return; + if (r > t0) t0 = r; + } else if (dy > 0) { + if (r < t0) return; + if (r < t1) t1 = r; + } + if (t0 > 0) line.a = { + x: ax + t0 * dx, + y: ay + t0 * dy + }; + if (t1 < 1) line.b = { + x: ax + t1 * dx, + y: ay + t1 * dy + }; + return line; + }; + } + var d3_geo_clipExtentMAX = 1e9; + d3.geo.clipExtent = function() { + var x0, y0, x1, y1, stream, clip, clipExtent = { + stream: function(output) { + if (stream) stream.valid = false; + stream = clip(output); + stream.valid = true; + return stream; + }, + extent: function(_) { + if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; + clip = d3_geo_clipExtent(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]); + if (stream) stream.valid = false, stream = null; + return clipExtent; + } + }; + return clipExtent.extent([ [ 0, 0 ], [ 960, 500 ] ]); + }; + function d3_geo_clipExtent(x0, y0, x1, y1) { + return function(listener) { + var listener_ = listener, bufferListener = d3_geo_clipBufferListener(), clipLine = d3_geom_clipLine(x0, y0, x1, y1), segments, polygon, ring; + var clip = { + point: point, + lineStart: lineStart, + lineEnd: lineEnd, + polygonStart: function() { + listener = bufferListener; + segments = []; + polygon = []; + clean = true; + }, + polygonEnd: function() { + listener = listener_; + segments = d3.merge(segments); + var clipStartInside = insidePolygon([ x0, y1 ]), inside = clean && clipStartInside, visible = segments.length; + if (inside || visible) { + listener.polygonStart(); + if (inside) { + listener.lineStart(); + interpolate(null, null, 1, listener); + listener.lineEnd(); } + if (visible) { + d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener); + } + listener.polygonEnd(); + } + segments = polygon = ring = null; + } + }; + function insidePolygon(p) { + var wn = 0, n = polygon.length, y = p[1]; + for (var i = 0; i < n; ++i) { + for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) { + b = v[j]; + if (a[1] <= y) { + if (b[1] > y && d3_cross2d(a, b, p) > 0) ++wn; + } else { + if (b[1] <= y && d3_cross2d(a, b, p) < 0) --wn; + } + a = b; + } + } + return wn !== 0; + } + function interpolate(from, to, direction, listener) { + var a = 0, a1 = 0; + if (from == null || (a = corner(from, direction)) !== (a1 = corner(to, direction)) || comparePoints(from, to) < 0 ^ direction > 0) { + do { + listener.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0); + } while ((a = (a + direction + 4) % 4) !== a1); + } else { + listener.point(to[0], to[1]); + } + } + function pointVisible(x, y) { + return x0 <= x && x <= x1 && y0 <= y && y <= y1; + } + function point(x, y) { + if (pointVisible(x, y)) listener.point(x, y); + } + var x__, y__, v__, x_, y_, v_, first, clean; + function lineStart() { + clip.point = linePoint; + if (polygon) polygon.push(ring = []); + first = true; + v_ = false; + x_ = y_ = NaN; + } + function lineEnd() { + if (segments) { + linePoint(x__, y__); + if (v__ && v_) bufferListener.rejoin(); + segments.push(bufferListener.buffer()); + } + clip.point = point; + if (v_) listener.lineEnd(); + } + function linePoint(x, y) { + x = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, x)); + y = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, y)); + var v = pointVisible(x, y); + if (polygon) ring.push([ x, y ]); + if (first) { + x__ = x, y__ = y, v__ = v; + first = false; + if (v) { + listener.lineStart(); + listener.point(x, y); + } + } else { + if (v && v_) listener.point(x, y); else { + var l = { + a: { + x: x_, + y: y_ + }, + b: { + x: x, + y: y + } + }; + if (clipLine(l)) { + if (!v_) { + listener.lineStart(); + listener.point(l.a.x, l.a.y); + } + listener.point(l.b.x, l.b.y); + if (!v) listener.lineEnd(); + clean = false; + } else if (v) { + listener.lineStart(); + listener.point(x, y); + clean = false; + } + } + } + x_ = x, y_ = y, v_ = v; + } + return clip; + }; + function corner(p, direction) { + return abs(p[0] - x0) < ε ? direction > 0 ? 0 : 3 : abs(p[0] - x1) < ε ? direction > 0 ? 2 : 1 : abs(p[1] - y0) < ε ? direction > 0 ? 1 : 0 : direction > 0 ? 3 : 2; + } + function compare(a, b) { + return comparePoints(a.x, b.x); + } + function comparePoints(a, b) { + var ca = corner(a, 1), cb = corner(b, 1); + return ca !== cb ? ca - cb : ca === 0 ? b[1] - a[1] : ca === 1 ? a[0] - b[0] : ca === 2 ? a[1] - b[1] : b[0] - a[0]; + } + } + function d3_geo_conic(projectAt) { + var φ0 = 0, φ1 = π / 3, m = d3_geo_projectionMutator(projectAt), p = m(φ0, φ1); + p.parallels = function(_) { + if (!arguments.length) return [ φ0 / π * 180, φ1 / π * 180 ]; + return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180); + }; + return p; + } + function d3_geo_conicEqualArea(φ0, φ1) { + var sinφ0 = Math.sin(φ0), n = (sinφ0 + Math.sin(φ1)) / 2, C = 1 + sinφ0 * (2 * n - sinφ0), ρ0 = Math.sqrt(C) / n; + function forward(λ, φ) { + var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n; + return [ ρ * Math.sin(λ *= n), ρ0 - ρ * Math.cos(λ) ]; + } + forward.invert = function(x, y) { + var ρ0_y = ρ0 - y; + return [ Math.atan2(x, ρ0_y) / n, d3_asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n)) ]; + }; + return forward; + } + (d3.geo.conicEqualArea = function() { + return d3_geo_conic(d3_geo_conicEqualArea); + }).raw = d3_geo_conicEqualArea; + d3.geo.albers = function() { + return d3.geo.conicEqualArea().rotate([ 96, 0 ]).center([ -.6, 38.7 ]).parallels([ 29.5, 45.5 ]).scale(1070); + }; + d3.geo.albersUsa = function() { + var lower48 = d3.geo.albers(); + var alaska = d3.geo.conicEqualArea().rotate([ 154, 0 ]).center([ -2, 58.5 ]).parallels([ 55, 65 ]); + var hawaii = d3.geo.conicEqualArea().rotate([ 157, 0 ]).center([ -3, 19.9 ]).parallels([ 8, 18 ]); + var point, pointStream = { + point: function(x, y) { + point = [ x, y ]; + } + }, lower48Point, alaskaPoint, hawaiiPoint; + function albersUsa(coordinates) { + var x = coordinates[0], y = coordinates[1]; + point = null; + (lower48Point(x, y), point) || (alaskaPoint(x, y), point) || hawaiiPoint(x, y); + return point; + } + albersUsa.invert = function(coordinates) { + var k = lower48.scale(), t = lower48.translate(), x = (coordinates[0] - t[0]) / k, y = (coordinates[1] - t[1]) / k; + return (y >= .12 && y < .234 && x >= -.425 && x < -.214 ? alaska : y >= .166 && y < .234 && x >= -.214 && x < -.115 ? hawaii : lower48).invert(coordinates); + }; + albersUsa.stream = function(stream) { + var lower48Stream = lower48.stream(stream), alaskaStream = alaska.stream(stream), hawaiiStream = hawaii.stream(stream); + return { + point: function(x, y) { + lower48Stream.point(x, y); + alaskaStream.point(x, y); + hawaiiStream.point(x, y); + }, + sphere: function() { + lower48Stream.sphere(); + alaskaStream.sphere(); + hawaiiStream.sphere(); + }, + lineStart: function() { + lower48Stream.lineStart(); + alaskaStream.lineStart(); + hawaiiStream.lineStart(); + }, + lineEnd: function() { + lower48Stream.lineEnd(); + alaskaStream.lineEnd(); + hawaiiStream.lineEnd(); + }, + polygonStart: function() { + lower48Stream.polygonStart(); + alaskaStream.polygonStart(); + hawaiiStream.polygonStart(); + }, + polygonEnd: function() { + lower48Stream.polygonEnd(); + alaskaStream.polygonEnd(); + hawaiiStream.polygonEnd(); + } + }; + }; + albersUsa.precision = function(_) { + if (!arguments.length) return lower48.precision(); + lower48.precision(_); + alaska.precision(_); + hawaii.precision(_); + return albersUsa; + }; + albersUsa.scale = function(_) { + if (!arguments.length) return lower48.scale(); + lower48.scale(_); + alaska.scale(_ * .35); + hawaii.scale(_); + return albersUsa.translate(lower48.translate()); + }; + albersUsa.translate = function(_) { + if (!arguments.length) return lower48.translate(); + var k = lower48.scale(), x = +_[0], y = +_[1]; + lower48Point = lower48.translate(_).clipExtent([ [ x - .455 * k, y - .238 * k ], [ x + .455 * k, y + .238 * k ] ]).stream(pointStream).point; + alaskaPoint = alaska.translate([ x - .307 * k, y + .201 * k ]).clipExtent([ [ x - .425 * k + ε, y + .12 * k + ε ], [ x - .214 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; + hawaiiPoint = hawaii.translate([ x - .205 * k, y + .212 * k ]).clipExtent([ [ x - .214 * k + ε, y + .166 * k + ε ], [ x - .115 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; + return albersUsa; + }; + return albersUsa.scale(1070); + }; + var d3_geo_pathAreaSum, d3_geo_pathAreaPolygon, d3_geo_pathArea = { + point: d3_noop, + lineStart: d3_noop, + lineEnd: d3_noop, + polygonStart: function() { + d3_geo_pathAreaPolygon = 0; + d3_geo_pathArea.lineStart = d3_geo_pathAreaRingStart; + }, + polygonEnd: function() { + d3_geo_pathArea.lineStart = d3_geo_pathArea.lineEnd = d3_geo_pathArea.point = d3_noop; + d3_geo_pathAreaSum += abs(d3_geo_pathAreaPolygon / 2); + } + }; + function d3_geo_pathAreaRingStart() { + var x00, y00, x0, y0; + d3_geo_pathArea.point = function(x, y) { + d3_geo_pathArea.point = nextPoint; + x00 = x0 = x, y00 = y0 = y; + }; + function nextPoint(x, y) { + d3_geo_pathAreaPolygon += y0 * x - x0 * y; + x0 = x, y0 = y; + } + d3_geo_pathArea.lineEnd = function() { + nextPoint(x00, y00); + }; + } + var d3_geo_pathBoundsX0, d3_geo_pathBoundsY0, d3_geo_pathBoundsX1, d3_geo_pathBoundsY1; + var d3_geo_pathBounds = { + point: d3_geo_pathBoundsPoint, + lineStart: d3_noop, + lineEnd: d3_noop, + polygonStart: d3_noop, + polygonEnd: d3_noop + }; + function d3_geo_pathBoundsPoint(x, y) { + if (x < d3_geo_pathBoundsX0) d3_geo_pathBoundsX0 = x; + if (x > d3_geo_pathBoundsX1) d3_geo_pathBoundsX1 = x; + if (y < d3_geo_pathBoundsY0) d3_geo_pathBoundsY0 = y; + if (y > d3_geo_pathBoundsY1) d3_geo_pathBoundsY1 = y; + } + function d3_geo_pathBuffer() { + var pointCircle = d3_geo_pathBufferCircle(4.5), buffer = []; + var stream = { + point: point, + lineStart: function() { + stream.point = pointLineStart; + }, + lineEnd: lineEnd, + polygonStart: function() { + stream.lineEnd = lineEndPolygon; + }, + polygonEnd: function() { + stream.lineEnd = lineEnd; + stream.point = point; + }, + pointRadius: function(_) { + pointCircle = d3_geo_pathBufferCircle(_); + return stream; + }, + result: function() { + if (buffer.length) { + var result = buffer.join(""); + buffer = []; + return result; + } + } + }; + function point(x, y) { + buffer.push("M", x, ",", y, pointCircle); + } + function pointLineStart(x, y) { + buffer.push("M", x, ",", y); + stream.point = pointLine; + } + function pointLine(x, y) { + buffer.push("L", x, ",", y); + } + function lineEnd() { + stream.point = point; + } + function lineEndPolygon() { + buffer.push("Z"); + } + return stream; + } + function d3_geo_pathBufferCircle(radius) { + return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius + "z"; + } + var d3_geo_pathCentroid = { + point: d3_geo_pathCentroidPoint, + lineStart: d3_geo_pathCentroidLineStart, + lineEnd: d3_geo_pathCentroidLineEnd, + polygonStart: function() { + d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidRingStart; + }, + polygonEnd: function() { + d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; + d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidLineStart; + d3_geo_pathCentroid.lineEnd = d3_geo_pathCentroidLineEnd; + } + }; + function d3_geo_pathCentroidPoint(x, y) { + d3_geo_centroidX0 += x; + d3_geo_centroidY0 += y; + ++d3_geo_centroidZ0; + } + function d3_geo_pathCentroidLineStart() { + var x0, y0; + d3_geo_pathCentroid.point = function(x, y) { + d3_geo_pathCentroid.point = nextPoint; + d3_geo_pathCentroidPoint(x0 = x, y0 = y); + }; + function nextPoint(x, y) { + var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); + d3_geo_centroidX1 += z * (x0 + x) / 2; + d3_geo_centroidY1 += z * (y0 + y) / 2; + d3_geo_centroidZ1 += z; + d3_geo_pathCentroidPoint(x0 = x, y0 = y); + } + } + function d3_geo_pathCentroidLineEnd() { + d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; + } + function d3_geo_pathCentroidRingStart() { + var x00, y00, x0, y0; + d3_geo_pathCentroid.point = function(x, y) { + d3_geo_pathCentroid.point = nextPoint; + d3_geo_pathCentroidPoint(x00 = x0 = x, y00 = y0 = y); + }; + function nextPoint(x, y) { + var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); + d3_geo_centroidX1 += z * (x0 + x) / 2; + d3_geo_centroidY1 += z * (y0 + y) / 2; + d3_geo_centroidZ1 += z; + z = y0 * x - x0 * y; + d3_geo_centroidX2 += z * (x0 + x); + d3_geo_centroidY2 += z * (y0 + y); + d3_geo_centroidZ2 += z * 3; + d3_geo_pathCentroidPoint(x0 = x, y0 = y); + } + d3_geo_pathCentroid.lineEnd = function() { + nextPoint(x00, y00); + }; + } + function d3_geo_pathContext(context) { + var pointRadius = 4.5; + var stream = { + point: point, + lineStart: function() { + stream.point = pointLineStart; + }, + lineEnd: lineEnd, + polygonStart: function() { + stream.lineEnd = lineEndPolygon; + }, + polygonEnd: function() { + stream.lineEnd = lineEnd; + stream.point = point; + }, + pointRadius: function(_) { + pointRadius = _; + return stream; + }, + result: d3_noop + }; + function point(x, y) { + context.moveTo(x + pointRadius, y); + context.arc(x, y, pointRadius, 0, τ); + } + function pointLineStart(x, y) { + context.moveTo(x, y); + stream.point = pointLine; + } + function pointLine(x, y) { + context.lineTo(x, y); + } + function lineEnd() { + stream.point = point; + } + function lineEndPolygon() { + context.closePath(); + } + return stream; + } + function d3_geo_resample(project) { + var δ2 = .5, cosMinDistance = Math.cos(30 * d3_radians), maxDepth = 16; + function resample(stream) { + return (maxDepth ? resampleRecursive : resampleNone)(stream); + } + function resampleNone(stream) { + return d3_geo_transformPoint(stream, function(x, y) { + x = project(x, y); + stream.point(x[0], x[1]); + }); + } + function resampleRecursive(stream) { + var λ00, φ00, x00, y00, a00, b00, c00, λ0, x0, y0, a0, b0, c0; + var resample = { + point: point, + lineStart: lineStart, + lineEnd: lineEnd, + polygonStart: function() { + stream.polygonStart(); + resample.lineStart = ringStart; + }, + polygonEnd: function() { + stream.polygonEnd(); + resample.lineStart = lineStart; + } + }; + function point(x, y) { + x = project(x, y); + stream.point(x[0], x[1]); + } + function lineStart() { + x0 = NaN; + resample.point = linePoint; + stream.lineStart(); + } + function linePoint(λ, φ) { + var c = d3_geo_cartesian([ λ, φ ]), p = project(λ, φ); + resampleLineTo(x0, y0, λ0, a0, b0, c0, x0 = p[0], y0 = p[1], λ0 = λ, a0 = c[0], b0 = c[1], c0 = c[2], maxDepth, stream); + stream.point(x0, y0); + } + function lineEnd() { + resample.point = point; + stream.lineEnd(); + } + function ringStart() { + lineStart(); + resample.point = ringPoint; + resample.lineEnd = ringEnd; + } + function ringPoint(λ, φ) { + linePoint(λ00 = λ, φ00 = φ), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0; + resample.point = linePoint; + } + function ringEnd() { + resampleLineTo(x0, y0, λ0, a0, b0, c0, x00, y00, λ00, a00, b00, c00, maxDepth, stream); + resample.lineEnd = lineEnd; + lineEnd(); + } + return resample; + } + function resampleLineTo(x0, y0, λ0, a0, b0, c0, x1, y1, λ1, a1, b1, c1, depth, stream) { + var dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy; + if (d2 > 4 * δ2 && depth--) { + var a = a0 + a1, b = b0 + b1, c = c0 + c1, m = Math.sqrt(a * a + b * b + c * c), φ2 = Math.asin(c /= m), λ2 = abs(abs(c) - 1) < ε || abs(λ0 - λ1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a), p = project(λ2, φ2), x2 = p[0], y2 = p[1], dx2 = x2 - x0, dy2 = y2 - y0, dz = dy * dx2 - dx * dy2; + if (dz * dz / d2 > δ2 || abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) { + resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream); + stream.point(x2, y2); + resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream); + } + } + } + resample.precision = function(_) { + if (!arguments.length) return Math.sqrt(δ2); + maxDepth = (δ2 = _ * _) > 0 && 16; + return resample; + }; + return resample; + } + d3.geo.path = function() { + var pointRadius = 4.5, projection, context, projectStream, contextStream, cacheStream; + function path(object) { + if (object) { + if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments)); + if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream); + d3.geo.stream(object, cacheStream); + } + return contextStream.result(); + } + path.area = function(object) { + d3_geo_pathAreaSum = 0; + d3.geo.stream(object, projectStream(d3_geo_pathArea)); + return d3_geo_pathAreaSum; + }; + path.centroid = function(object) { + d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; + d3.geo.stream(object, projectStream(d3_geo_pathCentroid)); + return d3_geo_centroidZ2 ? [ d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2 ] : d3_geo_centroidZ1 ? [ d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1 ] : d3_geo_centroidZ0 ? [ d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0 ] : [ NaN, NaN ]; + }; + path.bounds = function(object) { + d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity); + d3.geo.stream(object, projectStream(d3_geo_pathBounds)); + return [ [ d3_geo_pathBoundsX0, d3_geo_pathBoundsY0 ], [ d3_geo_pathBoundsX1, d3_geo_pathBoundsY1 ] ]; + }; + path.projection = function(_) { + if (!arguments.length) return projection; + projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity; + return reset(); + }; + path.context = function(_) { + if (!arguments.length) return context; + contextStream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_); + if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius); + return reset(); + }; + path.pointRadius = function(_) { + if (!arguments.length) return pointRadius; + pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_); + return path; + }; + function reset() { + cacheStream = null; + return path; + } + return path.projection(d3.geo.albersUsa()).context(null); + }; + function d3_geo_pathProjectStream(project) { + var resample = d3_geo_resample(function(x, y) { + return project([ x * d3_degrees, y * d3_degrees ]); + }); + return function(stream) { + return d3_geo_projectionRadians(resample(stream)); + }; + } + d3.geo.transform = function(methods) { + return { + stream: function(stream) { + var transform = new d3_geo_transform(stream); + for (var k in methods) transform[k] = methods[k]; + return transform; + } + }; + }; + function d3_geo_transform(stream) { + this.stream = stream; + } + d3_geo_transform.prototype = { + point: function(x, y) { + this.stream.point(x, y); + }, + sphere: function() { + this.stream.sphere(); + }, + lineStart: function() { + this.stream.lineStart(); + }, + lineEnd: function() { + this.stream.lineEnd(); + }, + polygonStart: function() { + this.stream.polygonStart(); + }, + polygonEnd: function() { + this.stream.polygonEnd(); + } + }; + function d3_geo_transformPoint(stream, point) { + return { + point: point, + sphere: function() { + stream.sphere(); + }, + lineStart: function() { + stream.lineStart(); + }, + lineEnd: function() { + stream.lineEnd(); + }, + polygonStart: function() { + stream.polygonStart(); + }, + polygonEnd: function() { + stream.polygonEnd(); + } + }; + } + d3.geo.projection = d3_geo_projection; + d3.geo.projectionMutator = d3_geo_projectionMutator; + function d3_geo_projection(project) { + return d3_geo_projectionMutator(function() { + return project; + })(); + } + function d3_geo_projectionMutator(projectAt) { + var project, rotate, projectRotate, projectResample = d3_geo_resample(function(x, y) { + x = project(x, y); + return [ x[0] * k + δx, δy - x[1] * k ]; + }), k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx, δy, preclip = d3_geo_clipAntimeridian, postclip = d3_identity, clipAngle = null, clipExtent = null, stream; + function projection(point) { + point = projectRotate(point[0] * d3_radians, point[1] * d3_radians); + return [ point[0] * k + δx, δy - point[1] * k ]; + } + function invert(point) { + point = projectRotate.invert((point[0] - δx) / k, (δy - point[1]) / k); + return point && [ point[0] * d3_degrees, point[1] * d3_degrees ]; + } + projection.stream = function(output) { + if (stream) stream.valid = false; + stream = d3_geo_projectionRadians(preclip(rotate, projectResample(postclip(output)))); + stream.valid = true; + return stream; + }; + projection.clipAngle = function(_) { + if (!arguments.length) return clipAngle; + preclip = _ == null ? (clipAngle = _, d3_geo_clipAntimeridian) : d3_geo_clipCircle((clipAngle = +_) * d3_radians); + return invalidate(); + }; + projection.clipExtent = function(_) { + if (!arguments.length) return clipExtent; + clipExtent = _; + postclip = _ ? d3_geo_clipExtent(_[0][0], _[0][1], _[1][0], _[1][1]) : d3_identity; + return invalidate(); + }; + projection.scale = function(_) { + if (!arguments.length) return k; + k = +_; + return reset(); + }; + projection.translate = function(_) { + if (!arguments.length) return [ x, y ]; + x = +_[0]; + y = +_[1]; + return reset(); + }; + projection.center = function(_) { + if (!arguments.length) return [ λ * d3_degrees, φ * d3_degrees ]; + λ = _[0] % 360 * d3_radians; + φ = _[1] % 360 * d3_radians; + return reset(); + }; + projection.rotate = function(_) { + if (!arguments.length) return [ δλ * d3_degrees, δφ * d3_degrees, δγ * d3_degrees ]; + δλ = _[0] % 360 * d3_radians; + δφ = _[1] % 360 * d3_radians; + δγ = _.length > 2 ? _[2] % 360 * d3_radians : 0; + return reset(); + }; + d3.rebind(projection, projectResample, "precision"); + function reset() { + projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project); + var center = project(λ, φ); + δx = x - center[0] * k; + δy = y + center[1] * k; + return invalidate(); + } + function invalidate() { + if (stream) stream.valid = false, stream = null; + return projection; + } + return function() { + project = projectAt.apply(this, arguments); + projection.invert = project.invert && invert; + return reset(); + }; + } + function d3_geo_projectionRadians(stream) { + return d3_geo_transformPoint(stream, function(x, y) { + stream.point(x * d3_radians, y * d3_radians); + }); + } + function d3_geo_equirectangular(λ, φ) { + return [ λ, φ ]; + } + (d3.geo.equirectangular = function() { + return d3_geo_projection(d3_geo_equirectangular); + }).raw = d3_geo_equirectangular.invert = d3_geo_equirectangular; + d3.geo.rotation = function(rotate) { + rotate = d3_geo_rotation(rotate[0] % 360 * d3_radians, rotate[1] * d3_radians, rotate.length > 2 ? rotate[2] * d3_radians : 0); + function forward(coordinates) { + coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians); + return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; + } + forward.invert = function(coordinates) { + coordinates = rotate.invert(coordinates[0] * d3_radians, coordinates[1] * d3_radians); + return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; + }; + return forward; + }; + function d3_geo_identityRotation(λ, φ) { + return [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; + } + d3_geo_identityRotation.invert = d3_geo_equirectangular; + function d3_geo_rotation(δλ, δφ, δγ) { + return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation; + } + function d3_geo_forwardRotationλ(δλ) { + return function(λ, φ) { + return λ += δλ, [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; + }; + } + function d3_geo_rotationλ(δλ) { + var rotation = d3_geo_forwardRotationλ(δλ); + rotation.invert = d3_geo_forwardRotationλ(-δλ); + return rotation; + } + function d3_geo_rotationφγ(δφ, δγ) { + var cosδφ = Math.cos(δφ), sinδφ = Math.sin(δφ), cosδγ = Math.cos(δγ), sinδγ = Math.sin(δγ); + function rotation(λ, φ) { + var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδφ + x * sinδφ; + return [ Math.atan2(y * cosδγ - k * sinδγ, x * cosδφ - z * sinδφ), d3_asin(k * cosδγ + y * sinδγ) ]; + } + rotation.invert = function(λ, φ) { + var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδγ - y * sinδγ; + return [ Math.atan2(y * cosδγ + z * sinδγ, x * cosδφ + k * sinδφ), d3_asin(k * cosδφ - x * sinδφ) ]; + }; + return rotation; + } + d3.geo.circle = function() { + var origin = [ 0, 0 ], angle, precision = 6, interpolate; + function circle() { + var center = typeof origin === "function" ? origin.apply(this, arguments) : origin, rotate = d3_geo_rotation(-center[0] * d3_radians, -center[1] * d3_radians, 0).invert, ring = []; + interpolate(null, null, 1, { + point: function(x, y) { + ring.push(x = rotate(x, y)); + x[0] *= d3_degrees, x[1] *= d3_degrees; + } + }); + return { + type: "Polygon", + coordinates: [ ring ] + }; + } + circle.origin = function(x) { + if (!arguments.length) return origin; + origin = x; + return circle; + }; + circle.angle = function(x) { + if (!arguments.length) return angle; + interpolate = d3_geo_circleInterpolate((angle = +x) * d3_radians, precision * d3_radians); + return circle; + }; + circle.precision = function(_) { + if (!arguments.length) return precision; + interpolate = d3_geo_circleInterpolate(angle * d3_radians, (precision = +_) * d3_radians); + return circle; + }; + return circle.angle(90); + }; + function d3_geo_circleInterpolate(radius, precision) { + var cr = Math.cos(radius), sr = Math.sin(radius); + return function(from, to, direction, listener) { + var step = direction * precision; + if (from != null) { + from = d3_geo_circleAngle(cr, from); + to = d3_geo_circleAngle(cr, to); + if (direction > 0 ? from < to : from > to) from += direction * τ; + } else { + from = radius + direction * τ; + to = radius - .5 * step; + } + for (var point, t = from; direction > 0 ? t > to : t < to; t -= step) { + listener.point((point = d3_geo_spherical([ cr, -sr * Math.cos(t), -sr * Math.sin(t) ]))[0], point[1]); + } + }; + } + function d3_geo_circleAngle(cr, point) { + var a = d3_geo_cartesian(point); + a[0] -= cr; + d3_geo_cartesianNormalize(a); + var angle = d3_acos(-a[1]); + return ((-a[2] < 0 ? -angle : angle) + 2 * Math.PI - ε) % (2 * Math.PI); + } + d3.geo.distance = function(a, b) { + var Δλ = (b[0] - a[0]) * d3_radians, φ0 = a[1] * d3_radians, φ1 = b[1] * d3_radians, sinΔλ = Math.sin(Δλ), cosΔλ = Math.cos(Δλ), sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), sinφ1 = Math.sin(φ1), cosφ1 = Math.cos(φ1), t; + return Math.atan2(Math.sqrt((t = cosφ1 * sinΔλ) * t + (t = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * t), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ); + }; + d3.geo.graticule = function() { + var x1, x0, X1, X0, y1, y0, Y1, Y0, dx = 10, dy = dx, DX = 90, DY = 360, x, y, X, Y, precision = 2.5; + function graticule() { + return { + type: "MultiLineString", + coordinates: lines() + }; + } + function lines() { + return d3.range(Math.ceil(X0 / DX) * DX, X1, DX).map(X).concat(d3.range(Math.ceil(Y0 / DY) * DY, Y1, DY).map(Y)).concat(d3.range(Math.ceil(x0 / dx) * dx, x1, dx).filter(function(x) { + return abs(x % DX) > ε; + }).map(x)).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).filter(function(y) { + return abs(y % DY) > ε; + }).map(y)); + } + graticule.lines = function() { + return lines().map(function(coordinates) { + return { + type: "LineString", + coordinates: coordinates + }; + }); + }; + graticule.outline = function() { + return { + type: "Polygon", + coordinates: [ X(X0).concat(Y(Y1).slice(1), X(X1).reverse().slice(1), Y(Y0).reverse().slice(1)) ] + }; + }; + graticule.extent = function(_) { + if (!arguments.length) return graticule.minorExtent(); + return graticule.majorExtent(_).minorExtent(_); + }; + graticule.majorExtent = function(_) { + if (!arguments.length) return [ [ X0, Y0 ], [ X1, Y1 ] ]; + X0 = +_[0][0], X1 = +_[1][0]; + Y0 = +_[0][1], Y1 = +_[1][1]; + if (X0 > X1) _ = X0, X0 = X1, X1 = _; + if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _; + return graticule.precision(precision); + }; + graticule.minorExtent = function(_) { + if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; + x0 = +_[0][0], x1 = +_[1][0]; + y0 = +_[0][1], y1 = +_[1][1]; + if (x0 > x1) _ = x0, x0 = x1, x1 = _; + if (y0 > y1) _ = y0, y0 = y1, y1 = _; + return graticule.precision(precision); + }; + graticule.step = function(_) { + if (!arguments.length) return graticule.minorStep(); + return graticule.majorStep(_).minorStep(_); + }; + graticule.majorStep = function(_) { + if (!arguments.length) return [ DX, DY ]; + DX = +_[0], DY = +_[1]; + return graticule; + }; + graticule.minorStep = function(_) { + if (!arguments.length) return [ dx, dy ]; + dx = +_[0], dy = +_[1]; + return graticule; + }; + graticule.precision = function(_) { + if (!arguments.length) return precision; + precision = +_; + x = d3_geo_graticuleX(y0, y1, 90); + y = d3_geo_graticuleY(x0, x1, precision); + X = d3_geo_graticuleX(Y0, Y1, 90); + Y = d3_geo_graticuleY(X0, X1, precision); + return graticule; + }; + return graticule.majorExtent([ [ -180, -90 + ε ], [ 180, 90 - ε ] ]).minorExtent([ [ -180, -80 - ε ], [ 180, 80 + ε ] ]); + }; + function d3_geo_graticuleX(y0, y1, dy) { + var y = d3.range(y0, y1 - ε, dy).concat(y1); + return function(x) { + return y.map(function(y) { + return [ x, y ]; + }); + }; + } + function d3_geo_graticuleY(x0, x1, dx) { + var x = d3.range(x0, x1 - ε, dx).concat(x1); + return function(y) { + return x.map(function(x) { + return [ x, y ]; + }); + }; + } + function d3_source(d) { + return d.source; + } + function d3_target(d) { + return d.target; + } + d3.geo.greatArc = function() { + var source = d3_source, source_, target = d3_target, target_; + function greatArc() { + return { + type: "LineString", + coordinates: [ source_ || source.apply(this, arguments), target_ || target.apply(this, arguments) ] + }; + } + greatArc.distance = function() { + return d3.geo.distance(source_ || source.apply(this, arguments), target_ || target.apply(this, arguments)); + }; + greatArc.source = function(_) { + if (!arguments.length) return source; + source = _, source_ = typeof _ === "function" ? null : _; + return greatArc; + }; + greatArc.target = function(_) { + if (!arguments.length) return target; + target = _, target_ = typeof _ === "function" ? null : _; + return greatArc; + }; + greatArc.precision = function() { + return arguments.length ? greatArc : 0; + }; + return greatArc; + }; + d3.geo.interpolate = function(source, target) { + return d3_geo_interpolate(source[0] * d3_radians, source[1] * d3_radians, target[0] * d3_radians, target[1] * d3_radians); + }; + function d3_geo_interpolate(x0, y0, x1, y1) { + var cy0 = Math.cos(y0), sy0 = Math.sin(y0), cy1 = Math.cos(y1), sy1 = Math.sin(y1), kx0 = cy0 * Math.cos(x0), ky0 = cy0 * Math.sin(x0), kx1 = cy1 * Math.cos(x1), ky1 = cy1 * Math.sin(x1), d = 2 * Math.asin(Math.sqrt(d3_haversin(y1 - y0) + cy0 * cy1 * d3_haversin(x1 - x0))), k = 1 / Math.sin(d); + var interpolate = d ? function(t) { + var B = Math.sin(t *= d) * k, A = Math.sin(d - t) * k, x = A * kx0 + B * kx1, y = A * ky0 + B * ky1, z = A * sy0 + B * sy1; + return [ Math.atan2(y, x) * d3_degrees, Math.atan2(z, Math.sqrt(x * x + y * y)) * d3_degrees ]; + } : function() { + return [ x0 * d3_degrees, y0 * d3_degrees ]; + }; + interpolate.distance = d; + return interpolate; + } + d3.geo.length = function(object) { + d3_geo_lengthSum = 0; + d3.geo.stream(object, d3_geo_length); + return d3_geo_lengthSum; + }; + var d3_geo_lengthSum; + var d3_geo_length = { + sphere: d3_noop, + point: d3_noop, + lineStart: d3_geo_lengthLineStart, + lineEnd: d3_noop, + polygonStart: d3_noop, + polygonEnd: d3_noop + }; + function d3_geo_lengthLineStart() { + var λ0, sinφ0, cosφ0; + d3_geo_length.point = function(λ, φ) { + λ0 = λ * d3_radians, sinφ0 = Math.sin(φ *= d3_radians), cosφ0 = Math.cos(φ); + d3_geo_length.point = nextPoint; + }; + d3_geo_length.lineEnd = function() { + d3_geo_length.point = d3_geo_length.lineEnd = d3_noop; + }; + function nextPoint(λ, φ) { + var sinφ = Math.sin(φ *= d3_radians), cosφ = Math.cos(φ), t = abs((λ *= d3_radians) - λ0), cosΔλ = Math.cos(t); + d3_geo_lengthSum += Math.atan2(Math.sqrt((t = cosφ * Math.sin(t)) * t + (t = cosφ0 * sinφ - sinφ0 * cosφ * cosΔλ) * t), sinφ0 * sinφ + cosφ0 * cosφ * cosΔλ); + λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ; + } + } + function d3_geo_azimuthal(scale, angle) { + function azimuthal(λ, φ) { + var cosλ = Math.cos(λ), cosφ = Math.cos(φ), k = scale(cosλ * cosφ); + return [ k * cosφ * Math.sin(λ), k * Math.sin(φ) ]; + } + azimuthal.invert = function(x, y) { + var ρ = Math.sqrt(x * x + y * y), c = angle(ρ), sinc = Math.sin(c), cosc = Math.cos(c); + return [ Math.atan2(x * sinc, ρ * cosc), Math.asin(ρ && y * sinc / ρ) ]; + }; + return azimuthal; + } + var d3_geo_azimuthalEqualArea = d3_geo_azimuthal(function(cosλcosφ) { + return Math.sqrt(2 / (1 + cosλcosφ)); + }, function(ρ) { + return 2 * Math.asin(ρ / 2); + }); + (d3.geo.azimuthalEqualArea = function() { + return d3_geo_projection(d3_geo_azimuthalEqualArea); + }).raw = d3_geo_azimuthalEqualArea; + var d3_geo_azimuthalEquidistant = d3_geo_azimuthal(function(cosλcosφ) { + var c = Math.acos(cosλcosφ); + return c && c / Math.sin(c); + }, d3_identity); + (d3.geo.azimuthalEquidistant = function() { + return d3_geo_projection(d3_geo_azimuthalEquidistant); + }).raw = d3_geo_azimuthalEquidistant; + function d3_geo_conicConformal(φ0, φ1) { + var cosφ0 = Math.cos(φ0), t = function(φ) { + return Math.tan(π / 4 + φ / 2); + }, n = φ0 === φ1 ? Math.sin(φ0) : Math.log(cosφ0 / Math.cos(φ1)) / Math.log(t(φ1) / t(φ0)), F = cosφ0 * Math.pow(t(φ0), n) / n; + if (!n) return d3_geo_mercator; + function forward(λ, φ) { + if (F > 0) { + if (φ < -halfπ + ε) φ = -halfπ + ε; + } else { + if (φ > halfπ - ε) φ = halfπ - ε; + } + var ρ = F / Math.pow(t(φ), n); + return [ ρ * Math.sin(n * λ), F - ρ * Math.cos(n * λ) ]; + } + forward.invert = function(x, y) { + var ρ0_y = F - y, ρ = d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y); + return [ Math.atan2(x, ρ0_y) / n, 2 * Math.atan(Math.pow(F / ρ, 1 / n)) - halfπ ]; + }; + return forward; + } + (d3.geo.conicConformal = function() { + return d3_geo_conic(d3_geo_conicConformal); + }).raw = d3_geo_conicConformal; + function d3_geo_conicEquidistant(φ0, φ1) { + var cosφ0 = Math.cos(φ0), n = φ0 === φ1 ? Math.sin(φ0) : (cosφ0 - Math.cos(φ1)) / (φ1 - φ0), G = cosφ0 / n + φ0; + if (abs(n) < ε) return d3_geo_equirectangular; + function forward(λ, φ) { + var ρ = G - φ; + return [ ρ * Math.sin(n * λ), G - ρ * Math.cos(n * λ) ]; + } + forward.invert = function(x, y) { + var ρ0_y = G - y; + return [ Math.atan2(x, ρ0_y) / n, G - d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y) ]; + }; + return forward; + } + (d3.geo.conicEquidistant = function() { + return d3_geo_conic(d3_geo_conicEquidistant); + }).raw = d3_geo_conicEquidistant; + var d3_geo_gnomonic = d3_geo_azimuthal(function(cosλcosφ) { + return 1 / cosλcosφ; + }, Math.atan); + (d3.geo.gnomonic = function() { + return d3_geo_projection(d3_geo_gnomonic); + }).raw = d3_geo_gnomonic; + function d3_geo_mercator(λ, φ) { + return [ λ, Math.log(Math.tan(π / 4 + φ / 2)) ]; + } + d3_geo_mercator.invert = function(x, y) { + return [ x, 2 * Math.atan(Math.exp(y)) - halfπ ]; + }; + function d3_geo_mercatorProjection(project) { + var m = d3_geo_projection(project), scale = m.scale, translate = m.translate, clipExtent = m.clipExtent, clipAuto; + m.scale = function() { + var v = scale.apply(m, arguments); + return v === m ? clipAuto ? m.clipExtent(null) : m : v; + }; + m.translate = function() { + var v = translate.apply(m, arguments); + return v === m ? clipAuto ? m.clipExtent(null) : m : v; + }; + m.clipExtent = function(_) { + var v = clipExtent.apply(m, arguments); + if (v === m) { + if (clipAuto = _ == null) { + var k = π * scale(), t = translate(); + clipExtent([ [ t[0] - k, t[1] - k ], [ t[0] + k, t[1] + k ] ]); + } + } else if (clipAuto) { + v = null; + } + return v; + }; + return m.clipExtent(null); + } + (d3.geo.mercator = function() { + return d3_geo_mercatorProjection(d3_geo_mercator); + }).raw = d3_geo_mercator; + var d3_geo_orthographic = d3_geo_azimuthal(function() { + return 1; + }, Math.asin); + (d3.geo.orthographic = function() { + return d3_geo_projection(d3_geo_orthographic); + }).raw = d3_geo_orthographic; + var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) { + return 1 / (1 + cosλcosφ); + }, function(ρ) { + return 2 * Math.atan(ρ); + }); + (d3.geo.stereographic = function() { + return d3_geo_projection(d3_geo_stereographic); + }).raw = d3_geo_stereographic; + function d3_geo_transverseMercator(λ, φ) { + return [ Math.log(Math.tan(π / 4 + φ / 2)), -λ ]; + } + d3_geo_transverseMercator.invert = function(x, y) { + return [ -y, 2 * Math.atan(Math.exp(x)) - halfπ ]; + }; + (d3.geo.transverseMercator = function() { + var projection = d3_geo_mercatorProjection(d3_geo_transverseMercator), center = projection.center, rotate = projection.rotate; + projection.center = function(_) { + return _ ? center([ -_[1], _[0] ]) : (_ = center(), [ _[1], -_[0] ]); + }; + projection.rotate = function(_) { + return _ ? rotate([ _[0], _[1], _.length > 2 ? _[2] + 90 : 90 ]) : (_ = rotate(), + [ _[0], _[1], _[2] - 90 ]); + }; + return rotate([ 0, 0, 90 ]); + }).raw = d3_geo_transverseMercator; + d3.geom = {}; + function d3_geom_pointX(d) { + return d[0]; + } + function d3_geom_pointY(d) { + return d[1]; + } + d3.geom.hull = function(vertices) { + var x = d3_geom_pointX, y = d3_geom_pointY; + if (arguments.length) return hull(vertices); + function hull(data) { + if (data.length < 3) return []; + var fx = d3_functor(x), fy = d3_functor(y), i, n = data.length, points = [], flippedPoints = []; + for (i = 0; i < n; i++) { + points.push([ +fx.call(this, data[i], i), +fy.call(this, data[i], i), i ]); + } + points.sort(d3_geom_hullOrder); + for (i = 0; i < n; i++) flippedPoints.push([ points[i][0], -points[i][1] ]); + var upper = d3_geom_hullUpper(points), lower = d3_geom_hullUpper(flippedPoints); + var skipLeft = lower[0] === upper[0], skipRight = lower[lower.length - 1] === upper[upper.length - 1], polygon = []; + for (i = upper.length - 1; i >= 0; --i) polygon.push(data[points[upper[i]][2]]); + for (i = +skipLeft; i < lower.length - skipRight; ++i) polygon.push(data[points[lower[i]][2]]); + return polygon; + } + hull.x = function(_) { + return arguments.length ? (x = _, hull) : x; + }; + hull.y = function(_) { + return arguments.length ? (y = _, hull) : y; + }; + return hull; + }; + function d3_geom_hullUpper(points) { + var n = points.length, hull = [ 0, 1 ], hs = 2; + for (var i = 2; i < n; i++) { + while (hs > 1 && d3_cross2d(points[hull[hs - 2]], points[hull[hs - 1]], points[i]) <= 0) --hs; + hull[hs++] = i; + } + return hull.slice(0, hs); + } + function d3_geom_hullOrder(a, b) { + return a[0] - b[0] || a[1] - b[1]; + } + d3.geom.polygon = function(coordinates) { + d3_subclass(coordinates, d3_geom_polygonPrototype); + return coordinates; + }; + var d3_geom_polygonPrototype = d3.geom.polygon.prototype = []; + d3_geom_polygonPrototype.area = function() { + var i = -1, n = this.length, a, b = this[n - 1], area = 0; + while (++i < n) { + a = b; + b = this[i]; + area += a[1] * b[0] - a[0] * b[1]; + } + return area * .5; + }; + d3_geom_polygonPrototype.centroid = function(k) { + var i = -1, n = this.length, x = 0, y = 0, a, b = this[n - 1], c; + if (!arguments.length) k = -1 / (6 * this.area()); + while (++i < n) { + a = b; + b = this[i]; + c = a[0] * b[1] - b[0] * a[1]; + x += (a[0] + b[0]) * c; + y += (a[1] + b[1]) * c; + } + return [ x * k, y * k ]; + }; + d3_geom_polygonPrototype.clip = function(subject) { + var input, closed = d3_geom_polygonClosed(subject), i = -1, n = this.length - d3_geom_polygonClosed(this), j, m, a = this[n - 1], b, c, d; + while (++i < n) { + input = subject.slice(); + subject.length = 0; + b = this[i]; + c = input[(m = input.length - closed) - 1]; + j = -1; + while (++j < m) { + d = input[j]; + if (d3_geom_polygonInside(d, a, b)) { + if (!d3_geom_polygonInside(c, a, b)) { + subject.push(d3_geom_polygonIntersect(c, d, a, b)); + } + subject.push(d); + } else if (d3_geom_polygonInside(c, a, b)) { + subject.push(d3_geom_polygonIntersect(c, d, a, b)); + } + c = d; + } + if (closed) subject.push(subject[0]); + a = b; + } + return subject; + }; + function d3_geom_polygonInside(p, a, b) { + return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]); + } + function d3_geom_polygonIntersect(c, d, a, b) { + var x1 = c[0], x3 = a[0], x21 = d[0] - x1, x43 = b[0] - x3, y1 = c[1], y3 = a[1], y21 = d[1] - y1, y43 = b[1] - y3, ua = (x43 * (y1 - y3) - y43 * (x1 - x3)) / (y43 * x21 - x43 * y21); + return [ x1 + ua * x21, y1 + ua * y21 ]; + } + function d3_geom_polygonClosed(coordinates) { + var a = coordinates[0], b = coordinates[coordinates.length - 1]; + return !(a[0] - b[0] || a[1] - b[1]); + } + var d3_geom_voronoiEdges, d3_geom_voronoiCells, d3_geom_voronoiBeaches, d3_geom_voronoiBeachPool = [], d3_geom_voronoiFirstCircle, d3_geom_voronoiCircles, d3_geom_voronoiCirclePool = []; + function d3_geom_voronoiBeach() { + d3_geom_voronoiRedBlackNode(this); + this.edge = this.site = this.circle = null; + } + function d3_geom_voronoiCreateBeach(site) { + var beach = d3_geom_voronoiBeachPool.pop() || new d3_geom_voronoiBeach(); + beach.site = site; + return beach; + } + function d3_geom_voronoiDetachBeach(beach) { + d3_geom_voronoiDetachCircle(beach); + d3_geom_voronoiBeaches.remove(beach); + d3_geom_voronoiBeachPool.push(beach); + d3_geom_voronoiRedBlackNode(beach); + } + function d3_geom_voronoiRemoveBeach(beach) { + var circle = beach.circle, x = circle.x, y = circle.cy, vertex = { + x: x, + y: y + }, previous = beach.P, next = beach.N, disappearing = [ beach ]; + d3_geom_voronoiDetachBeach(beach); + var lArc = previous; + while (lArc.circle && abs(x - lArc.circle.x) < ε && abs(y - lArc.circle.cy) < ε) { + previous = lArc.P; + disappearing.unshift(lArc); + d3_geom_voronoiDetachBeach(lArc); + lArc = previous; + } + disappearing.unshift(lArc); + d3_geom_voronoiDetachCircle(lArc); + var rArc = next; + while (rArc.circle && abs(x - rArc.circle.x) < ε && abs(y - rArc.circle.cy) < ε) { + next = rArc.N; + disappearing.push(rArc); + d3_geom_voronoiDetachBeach(rArc); + rArc = next; + } + disappearing.push(rArc); + d3_geom_voronoiDetachCircle(rArc); + var nArcs = disappearing.length, iArc; + for (iArc = 1; iArc < nArcs; ++iArc) { + rArc = disappearing[iArc]; + lArc = disappearing[iArc - 1]; + d3_geom_voronoiSetEdgeEnd(rArc.edge, lArc.site, rArc.site, vertex); + } + lArc = disappearing[0]; + rArc = disappearing[nArcs - 1]; + rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, rArc.site, null, vertex); + d3_geom_voronoiAttachCircle(lArc); + d3_geom_voronoiAttachCircle(rArc); + } + function d3_geom_voronoiAddBeach(site) { + var x = site.x, directrix = site.y, lArc, rArc, dxl, dxr, node = d3_geom_voronoiBeaches._; + while (node) { + dxl = d3_geom_voronoiLeftBreakPoint(node, directrix) - x; + if (dxl > ε) node = node.L; else { + dxr = x - d3_geom_voronoiRightBreakPoint(node, directrix); + if (dxr > ε) { + if (!node.R) { + lArc = node; break; - - default: - throw "Unknown graphic control label: 0x" + buf[p-1].toString(16); - } - break; - - case 0x2c: // Image Descriptor. - var x = buf[p++] | buf[p++] << 8; - var y = buf[p++] | buf[p++] << 8; - var w = buf[p++] | buf[p++] << 8; - var h = buf[p++] | buf[p++] << 8; - var pf2 = buf[p++]; - var local_palette_flag = pf2 >> 7; - var interlace_flag = pf2 >> 6 & 1; - var num_local_colors_pow2 = pf2 & 0x7; - var num_local_colors = 1 << (num_local_colors_pow2 + 1); - var palette_offset = global_palette_offset; - var has_local_palette = false; - if (local_palette_flag) { - var has_local_palette = true; - palette_offset = p; // Override with local palette. - p += num_local_colors * 3; // Seek past palette. - } - - var data_offset = p; - - p++; // codesize - while (true) { - var block_size = buf[p++]; - if (block_size === 0) break; - p += block_size; - } - - frames.push({x: x, y: y, width: w, height: h, - has_local_palette: has_local_palette, - palette_offset: palette_offset, - data_offset: data_offset, - data_length: p - data_offset, - transparent_index: transparent_index, - interlaced: !!interlace_flag, - delay: delay, - disposal: disposal}); - break; - - case 0x3b: // Trailer Marker (end of file). - no_eof = false; - break; - - default: - throw "Unknown gif block: 0x" + buf[p-1].toString(16); - break; - } - } - - this.numFrames = function() { - return frames.length; - }; - - this.loopCount = function() { - return loop_count; - }; - - this.frameInfo = function(frame_num) { - if (frame_num < 0 || frame_num >= frames.length) - throw "Frame index out of range."; - return frames[frame_num]; - } - - this.decodeAndBlitFrameBGRA = function(frame_num, pixels) { - var frame = this.frameInfo(frame_num); - var num_pixels = frame.width * frame.height; - var index_stream = new Uint8Array(num_pixels); // At most 8-bit indices. - GifReaderLZWOutputIndexStream( - buf, frame.data_offset, index_stream, num_pixels); - var palette_offset = frame.palette_offset; - - // NOTE(deanm): It seems to be much faster to compare index to 256 than - // to === null. Not sure why, but CompareStub_EQ_STRICT shows up high in - // the profile, not sure if it's related to using a Uint8Array. - var trans = frame.transparent_index; - if (trans === null) trans = 256; - - // We are possibly just blitting to a portion of the entire frame. - // That is a subrect within the framerect, so the additional pixels - // must be skipped over after we finished a scanline. - var framewidth = frame.width; - var framestride = width - framewidth; - var xleft = framewidth; // Number of subrect pixels left in scanline. - - // Output indicies of the top left and bottom right corners of the subrect. - var opbeg = ((frame.y * width) + frame.x) * 4; - var opend = ((frame.y + frame.height) * width + frame.x) * 4; - var op = opbeg; - - var scanstride = framestride * 4; - - // Use scanstride to skip past the rows when interlacing. This is skipping - // 7 rows for the first two passes, then 3 then 1. - if (frame.interlaced === true) { - scanstride += width * 4 * 7; // Pass 1. - } - - var interlaceskip = 8; // Tracking the row interval in the current pass. - - for (var i = 0, il = index_stream.length; i < il; ++i) { - var index = index_stream[i]; - - if (xleft === 0) { // Beginning of new scan line - op += scanstride; - xleft = framewidth; - if (op >= opend) { // Catch the wrap to switch passes when interlacing. - scanstride = framestride * 4 + width * 4 * (interlaceskip-1); - // interlaceskip / 2 * 4 is interlaceskip << 1. - op = opbeg + (framewidth + framestride) * (interlaceskip << 1); - interlaceskip >>= 1; + } + node = node.R; + } else { + if (dxl > -ε) { + lArc = node.P; + rArc = node; + } else if (dxr > -ε) { + lArc = node; + rArc = node.N; + } else { + lArc = rArc = node; + } + break; } } - - if (index === trans) { - op += 4; - } else { - var r = buf[palette_offset + index * 3]; - var g = buf[palette_offset + index * 3 + 1]; - var b = buf[palette_offset + index * 3 + 2]; - pixels[op++] = b; - pixels[op++] = g; - pixels[op++] = r; - pixels[op++] = 255; - } - --xleft; } - }; - - // I will go to copy and paste hell one day... - this.decodeAndBlitFrameRGBA = function(frame_num, pixels) { - var frame = this.frameInfo(frame_num); - var num_pixels = frame.width * frame.height; - var index_stream = new Uint8Array(num_pixels); // At most 8-bit indices. - GifReaderLZWOutputIndexStream( - buf, frame.data_offset, index_stream, num_pixels); - var palette_offset = frame.palette_offset; - - // NOTE(deanm): It seems to be much faster to compare index to 256 than - // to === null. Not sure why, but CompareStub_EQ_STRICT shows up high in - // the profile, not sure if it's related to using a Uint8Array. - var trans = frame.transparent_index; - if (trans === null) trans = 256; - - // We are possibly just blitting to a portion of the entire frame. - // That is a subrect within the framerect, so the additional pixels - // must be skipped over after we finished a scanline. - var framewidth = frame.width; - var framestride = width - framewidth; - var xleft = framewidth; // Number of subrect pixels left in scanline. - - // Output indicies of the top left and bottom right corners of the subrect. - var opbeg = ((frame.y * width) + frame.x) * 4; - var opend = ((frame.y + frame.height) * width + frame.x) * 4; - var op = opbeg; - - var scanstride = framestride * 4; - - // Use scanstride to skip past the rows when interlacing. This is skipping - // 7 rows for the first two passes, then 3 then 1. - if (frame.interlaced === true) { - scanstride += width * 4 * 7; // Pass 1. - } - - var interlaceskip = 8; // Tracking the row interval in the current pass. - - for (var i = 0, il = index_stream.length; i < il; ++i) { - var index = index_stream[i]; - - if (xleft === 0) { // Beginning of new scan line - op += scanstride; - xleft = framewidth; - if (op >= opend) { // Catch the wrap to switch passes when interlacing. - scanstride = framestride * 4 + width * 4 * (interlaceskip-1); - // interlaceskip / 2 * 4 is interlaceskip << 1. - op = opbeg + (framewidth + framestride) * (interlaceskip << 1); - interlaceskip >>= 1; - } - } - - if (index === trans) { - op += 4; - } else { - var r = buf[palette_offset + index * 3]; - var g = buf[palette_offset + index * 3 + 1]; - var b = buf[palette_offset + index * 3 + 2]; - pixels[op++] = r; - pixels[op++] = g; - pixels[op++] = b; - pixels[op++] = 255; - } - --xleft; - } - }; -} - -function GifReaderLZWOutputIndexStream(code_stream, p, output, output_length) { - var min_code_size = code_stream[p++]; - - var clear_code = 1 << min_code_size; - var eoi_code = clear_code + 1; - var next_code = eoi_code + 1; - - var cur_code_size = min_code_size + 1; // Number of bits per code. - // NOTE: This shares the same name as the encoder, but has a different - // meaning here. Here this masks each code coming from the code stream. - var code_mask = (1 << cur_code_size) - 1; - var cur_shift = 0; - var cur = 0; - - var op = 0; // Output pointer. - - var subblock_size = code_stream[p++]; - - // TODO(deanm): Would using a TypedArray be any faster? At least it would - // solve the fast mode / backing store uncertainty. - // var code_table = Array(4096); - var code_table = new Int32Array(4096); // Can be signed, we only use 20 bits. - - var prev_code = null; // Track code-1. - - while (true) { - // Read up to two bytes, making sure we always 12-bits for max sized code. - while (cur_shift < 16) { - if (subblock_size === 0) break; // No more data to be read. - - cur |= code_stream[p++] << cur_shift; - cur_shift += 8; - - if (subblock_size === 1) { // Never let it get to 0 to hold logic above. - subblock_size = code_stream[p++]; // Next subblock. - } else { - --subblock_size; - } - } - - // TODO(deanm): We should never really get here, we should have received - // and EOI. - if (cur_shift < cur_code_size) - break; - - var code = cur & code_mask; - cur >>= cur_code_size; - cur_shift -= cur_code_size; - - // TODO(deanm): Maybe should check that the first code was a clear code, - // at least this is what you're supposed to do. But actually our encoder - // now doesn't emit a clear code first anyway. - if (code === clear_code) { - // We don't actually have to clear the table. This could be a good idea - // for greater error checking, but we don't really do any anyway. We - // will just track it with next_code and overwrite old entries. - - next_code = eoi_code + 1; - cur_code_size = min_code_size + 1; - code_mask = (1 << cur_code_size) - 1; - - // Don't update prev_code ? - prev_code = null; - continue; - } else if (code === eoi_code) { - break; - } - - // We have a similar situation as the decoder, where we want to store - // variable length entries (code table entries), but we want to do in a - // faster manner than an array of arrays. The code below stores sort of a - // linked list within the code table, and then "chases" through it to - // construct the dictionary entries. When a new entry is created, just the - // last byte is stored, and the rest (prefix) of the entry is only - // referenced by its table entry. Then the code chases through the - // prefixes until it reaches a single byte code. We have to chase twice, - // first to compute the length, and then to actually copy the data to the - // output (backwards, since we know the length). The alternative would be - // storing something in an intermediate stack, but that doesn't make any - // more sense. I implemented an approach where it also stored the length - // in the code table, although it's a bit tricky because you run out of - // bits (12 + 12 + 8), but I didn't measure much improvements (the table - // entries are generally not the long). Even when I created benchmarks for - // very long table entries the complexity did not seem worth it. - // The code table stores the prefix entry in 12 bits and then the suffix - // byte in 8 bits, so each entry is 20 bits. - - var chase_code = code < next_code ? code : prev_code; - - // Chase what we will output, either {CODE} or {CODE-1}. - var chase_length = 0; - var chase = chase_code; - while (chase > clear_code) { - chase = code_table[chase] >> 8; - ++chase_length; - } - - var k = chase; - - var op_end = op + chase_length + (chase_code !== code ? 1 : 0); - if (op_end > output_length) { - console.log("Warning, gif stream longer than expected."); + var newArc = d3_geom_voronoiCreateBeach(site); + d3_geom_voronoiBeaches.insert(lArc, newArc); + if (!lArc && !rArc) return; + if (lArc === rArc) { + d3_geom_voronoiDetachCircle(lArc); + rArc = d3_geom_voronoiCreateBeach(lArc.site); + d3_geom_voronoiBeaches.insert(newArc, rArc); + newArc.edge = rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); + d3_geom_voronoiAttachCircle(lArc); + d3_geom_voronoiAttachCircle(rArc); return; } - - // Already have the first byte from the chase, might as well write it fast. - output[op++] = k; - - op += chase_length; - var b = op; // Track pointer, writing backwards. - - if (chase_code !== code) // The case of emitting {CODE-1} + k. - output[op++] = k; - - chase = chase_code; - while (chase_length--) { - chase = code_table[chase]; - output[--b] = chase & 0xff; // Write backwards. - chase >>= 8; // Pull down to the prefix code. + if (!rArc) { + newArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); + return; } - - if (prev_code !== null && next_code < 4096) { - code_table[next_code++] = prev_code << 8 | k; - // TODO(deanm): Figure out this clearing vs code growth logic better. I - // have an feeling that it should just happen somewhere else, for now it - // is awkward between when we grow past the max and then hit a clear code. - // For now just check if we hit the max 12-bits (then a clear code should - // follow, also of course encoded in 12-bits). - if (next_code >= code_mask+1 && cur_code_size < 12) { - ++cur_code_size; - code_mask = code_mask << 1 | 1; + d3_geom_voronoiDetachCircle(lArc); + d3_geom_voronoiDetachCircle(rArc); + var lSite = lArc.site, ax = lSite.x, ay = lSite.y, bx = site.x - ax, by = site.y - ay, rSite = rArc.site, cx = rSite.x - ax, cy = rSite.y - ay, d = 2 * (bx * cy - by * cx), hb = bx * bx + by * by, hc = cx * cx + cy * cy, vertex = { + x: (cy * hb - by * hc) / d + ax, + y: (bx * hc - cx * hb) / d + ay + }; + d3_geom_voronoiSetEdgeEnd(rArc.edge, lSite, rSite, vertex); + newArc.edge = d3_geom_voronoiCreateEdge(lSite, site, null, vertex); + rArc.edge = d3_geom_voronoiCreateEdge(site, rSite, null, vertex); + d3_geom_voronoiAttachCircle(lArc); + d3_geom_voronoiAttachCircle(rArc); + } + function d3_geom_voronoiLeftBreakPoint(arc, directrix) { + var site = arc.site, rfocx = site.x, rfocy = site.y, pby2 = rfocy - directrix; + if (!pby2) return rfocx; + var lArc = arc.P; + if (!lArc) return -Infinity; + site = lArc.site; + var lfocx = site.x, lfocy = site.y, plby2 = lfocy - directrix; + if (!plby2) return lfocx; + var hl = lfocx - rfocx, aby2 = 1 / pby2 - 1 / plby2, b = hl / plby2; + if (aby2) return (-b + Math.sqrt(b * b - 2 * aby2 * (hl * hl / (-2 * plby2) - lfocy + plby2 / 2 + rfocy - pby2 / 2))) / aby2 + rfocx; + return (rfocx + lfocx) / 2; + } + function d3_geom_voronoiRightBreakPoint(arc, directrix) { + var rArc = arc.N; + if (rArc) return d3_geom_voronoiLeftBreakPoint(rArc, directrix); + var site = arc.site; + return site.y === directrix ? site.x : Infinity; + } + function d3_geom_voronoiCell(site) { + this.site = site; + this.edges = []; + } + d3_geom_voronoiCell.prototype.prepare = function() { + var halfEdges = this.edges, iHalfEdge = halfEdges.length, edge; + while (iHalfEdge--) { + edge = halfEdges[iHalfEdge].edge; + if (!edge.b || !edge.a) halfEdges.splice(iHalfEdge, 1); + } + halfEdges.sort(d3_geom_voronoiHalfEdgeOrder); + return halfEdges.length; + }; + function d3_geom_voronoiCloseCells(extent) { + var x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], x2, y2, x3, y3, cells = d3_geom_voronoiCells, iCell = cells.length, cell, iHalfEdge, halfEdges, nHalfEdges, start, end; + while (iCell--) { + cell = cells[iCell]; + if (!cell || !cell.prepare()) continue; + halfEdges = cell.edges; + nHalfEdges = halfEdges.length; + iHalfEdge = 0; + while (iHalfEdge < nHalfEdges) { + end = halfEdges[iHalfEdge].end(), x3 = end.x, y3 = end.y; + start = halfEdges[++iHalfEdge % nHalfEdges].start(), x2 = start.x, y2 = start.y; + if (abs(x3 - x2) > ε || abs(y3 - y2) > ε) { + halfEdges.splice(iHalfEdge, 0, new d3_geom_voronoiHalfEdge(d3_geom_voronoiCreateBorderEdge(cell.site, end, abs(x3 - x0) < ε && y1 - y3 > ε ? { + x: x0, + y: abs(x2 - x0) < ε ? y2 : y1 + } : abs(y3 - y1) < ε && x1 - x3 > ε ? { + x: abs(y2 - y1) < ε ? x2 : x1, + y: y1 + } : abs(x3 - x1) < ε && y3 - y0 > ε ? { + x: x1, + y: abs(x2 - x1) < ε ? y2 : y0 + } : abs(y3 - y0) < ε && x3 - x0 > ε ? { + x: abs(y2 - y0) < ε ? x2 : x0, + y: y0 + } : null), cell.site, null)); + ++nHalfEdges; + } } } + } + function d3_geom_voronoiHalfEdgeOrder(a, b) { + return b.angle - a.angle; + } + function d3_geom_voronoiCircle() { + d3_geom_voronoiRedBlackNode(this); + this.x = this.y = this.arc = this.site = this.cy = null; + } + function d3_geom_voronoiAttachCircle(arc) { + var lArc = arc.P, rArc = arc.N; + if (!lArc || !rArc) return; + var lSite = lArc.site, cSite = arc.site, rSite = rArc.site; + if (lSite === rSite) return; + var bx = cSite.x, by = cSite.y, ax = lSite.x - bx, ay = lSite.y - by, cx = rSite.x - bx, cy = rSite.y - by; + var d = 2 * (ax * cy - ay * cx); + if (d >= -ε2) return; + var ha = ax * ax + ay * ay, hc = cx * cx + cy * cy, x = (cy * ha - ay * hc) / d, y = (ax * hc - cx * ha) / d, cy = y + by; + var circle = d3_geom_voronoiCirclePool.pop() || new d3_geom_voronoiCircle(); + circle.arc = arc; + circle.site = cSite; + circle.x = x + bx; + circle.y = cy + Math.sqrt(x * x + y * y); + circle.cy = cy; + arc.circle = circle; + var before = null, node = d3_geom_voronoiCircles._; + while (node) { + if (circle.y < node.y || circle.y === node.y && circle.x <= node.x) { + if (node.L) node = node.L; else { + before = node.P; + break; + } + } else { + if (node.R) node = node.R; else { + before = node; + break; + } + } + } + d3_geom_voronoiCircles.insert(before, circle); + if (!before) d3_geom_voronoiFirstCircle = circle; + } + function d3_geom_voronoiDetachCircle(arc) { + var circle = arc.circle; + if (circle) { + if (!circle.P) d3_geom_voronoiFirstCircle = circle.N; + d3_geom_voronoiCircles.remove(circle); + d3_geom_voronoiCirclePool.push(circle); + d3_geom_voronoiRedBlackNode(circle); + arc.circle = null; + } + } + function d3_geom_voronoiClipEdges(extent) { + var edges = d3_geom_voronoiEdges, clip = d3_geom_clipLine(extent[0][0], extent[0][1], extent[1][0], extent[1][1]), i = edges.length, e; + while (i--) { + e = edges[i]; + if (!d3_geom_voronoiConnectEdge(e, extent) || !clip(e) || abs(e.a.x - e.b.x) < ε && abs(e.a.y - e.b.y) < ε) { + e.a = e.b = null; + edges.splice(i, 1); + } + } + } + function d3_geom_voronoiConnectEdge(edge, extent) { + var vb = edge.b; + if (vb) return true; + var va = edge.a, x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], lSite = edge.l, rSite = edge.r, lx = lSite.x, ly = lSite.y, rx = rSite.x, ry = rSite.y, fx = (lx + rx) / 2, fy = (ly + ry) / 2, fm, fb; + if (ry === ly) { + if (fx < x0 || fx >= x1) return; + if (lx > rx) { + if (!va) va = { + x: fx, + y: y0 + }; else if (va.y >= y1) return; + vb = { + x: fx, + y: y1 + }; + } else { + if (!va) va = { + x: fx, + y: y1 + }; else if (va.y < y0) return; + vb = { + x: fx, + y: y0 + }; + } + } else { + fm = (lx - rx) / (ry - ly); + fb = fy - fm * fx; + if (fm < -1 || fm > 1) { + if (lx > rx) { + if (!va) va = { + x: (y0 - fb) / fm, + y: y0 + }; else if (va.y >= y1) return; + vb = { + x: (y1 - fb) / fm, + y: y1 + }; + } else { + if (!va) va = { + x: (y1 - fb) / fm, + y: y1 + }; else if (va.y < y0) return; + vb = { + x: (y0 - fb) / fm, + y: y0 + }; + } + } else { + if (ly < ry) { + if (!va) va = { + x: x0, + y: fm * x0 + fb + }; else if (va.x >= x1) return; + vb = { + x: x1, + y: fm * x1 + fb + }; + } else { + if (!va) va = { + x: x1, + y: fm * x1 + fb + }; else if (va.x < x0) return; + vb = { + x: x0, + y: fm * x0 + fb + }; + } + } + } + edge.a = va; + edge.b = vb; + return true; + } + function d3_geom_voronoiEdge(lSite, rSite) { + this.l = lSite; + this.r = rSite; + this.a = this.b = null; + } + function d3_geom_voronoiCreateEdge(lSite, rSite, va, vb) { + var edge = new d3_geom_voronoiEdge(lSite, rSite); + d3_geom_voronoiEdges.push(edge); + if (va) d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, va); + if (vb) d3_geom_voronoiSetEdgeEnd(edge, rSite, lSite, vb); + d3_geom_voronoiCells[lSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, lSite, rSite)); + d3_geom_voronoiCells[rSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, rSite, lSite)); + return edge; + } + function d3_geom_voronoiCreateBorderEdge(lSite, va, vb) { + var edge = new d3_geom_voronoiEdge(lSite, null); + edge.a = va; + edge.b = vb; + d3_geom_voronoiEdges.push(edge); + return edge; + } + function d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, vertex) { + if (!edge.a && !edge.b) { + edge.a = vertex; + edge.l = lSite; + edge.r = rSite; + } else if (edge.l === rSite) { + edge.b = vertex; + } else { + edge.a = vertex; + } + } + function d3_geom_voronoiHalfEdge(edge, lSite, rSite) { + var va = edge.a, vb = edge.b; + this.edge = edge; + this.site = lSite; + this.angle = rSite ? Math.atan2(rSite.y - lSite.y, rSite.x - lSite.x) : edge.l === lSite ? Math.atan2(vb.x - va.x, va.y - vb.y) : Math.atan2(va.x - vb.x, vb.y - va.y); + } + d3_geom_voronoiHalfEdge.prototype = { + start: function() { + return this.edge.l === this.site ? this.edge.a : this.edge.b; + }, + end: function() { + return this.edge.l === this.site ? this.edge.b : this.edge.a; + } + }; + function d3_geom_voronoiRedBlackTree() { + this._ = null; + } + function d3_geom_voronoiRedBlackNode(node) { + node.U = node.C = node.L = node.R = node.P = node.N = null; + } + d3_geom_voronoiRedBlackTree.prototype = { + insert: function(after, node) { + var parent, grandpa, uncle; + if (after) { + node.P = after; + node.N = after.N; + if (after.N) after.N.P = node; + after.N = node; + if (after.R) { + after = after.R; + while (after.L) after = after.L; + after.L = node; + } else { + after.R = node; + } + parent = after; + } else if (this._) { + after = d3_geom_voronoiRedBlackFirst(this._); + node.P = null; + node.N = after; + after.P = after.L = node; + parent = after; + } else { + node.P = node.N = null; + this._ = node; + parent = null; + } + node.L = node.R = null; + node.U = parent; + node.C = true; + after = node; + while (parent && parent.C) { + grandpa = parent.U; + if (parent === grandpa.L) { + uncle = grandpa.R; + if (uncle && uncle.C) { + parent.C = uncle.C = false; + grandpa.C = true; + after = grandpa; + } else { + if (after === parent.R) { + d3_geom_voronoiRedBlackRotateLeft(this, parent); + after = parent; + parent = after.U; + } + parent.C = false; + grandpa.C = true; + d3_geom_voronoiRedBlackRotateRight(this, grandpa); + } + } else { + uncle = grandpa.L; + if (uncle && uncle.C) { + parent.C = uncle.C = false; + grandpa.C = true; + after = grandpa; + } else { + if (after === parent.L) { + d3_geom_voronoiRedBlackRotateRight(this, parent); + after = parent; + parent = after.U; + } + parent.C = false; + grandpa.C = true; + d3_geom_voronoiRedBlackRotateLeft(this, grandpa); + } + } + parent = after.U; + } + this._.C = false; + }, + remove: function(node) { + if (node.N) node.N.P = node.P; + if (node.P) node.P.N = node.N; + node.N = node.P = null; + var parent = node.U, sibling, left = node.L, right = node.R, next, red; + if (!left) next = right; else if (!right) next = left; else next = d3_geom_voronoiRedBlackFirst(right); + if (parent) { + if (parent.L === node) parent.L = next; else parent.R = next; + } else { + this._ = next; + } + if (left && right) { + red = next.C; + next.C = node.C; + next.L = left; + left.U = next; + if (next !== right) { + parent = next.U; + next.U = node.U; + node = next.R; + parent.L = node; + next.R = right; + right.U = next; + } else { + next.U = parent; + parent = next; + node = next.R; + } + } else { + red = node.C; + node = next; + } + if (node) node.U = parent; + if (red) return; + if (node && node.C) { + node.C = false; + return; + } + do { + if (node === this._) break; + if (node === parent.L) { + sibling = parent.R; + if (sibling.C) { + sibling.C = false; + parent.C = true; + d3_geom_voronoiRedBlackRotateLeft(this, parent); + sibling = parent.R; + } + if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { + if (!sibling.R || !sibling.R.C) { + sibling.L.C = false; + sibling.C = true; + d3_geom_voronoiRedBlackRotateRight(this, sibling); + sibling = parent.R; + } + sibling.C = parent.C; + parent.C = sibling.R.C = false; + d3_geom_voronoiRedBlackRotateLeft(this, parent); + node = this._; + break; + } + } else { + sibling = parent.L; + if (sibling.C) { + sibling.C = false; + parent.C = true; + d3_geom_voronoiRedBlackRotateRight(this, parent); + sibling = parent.L; + } + if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { + if (!sibling.L || !sibling.L.C) { + sibling.R.C = false; + sibling.C = true; + d3_geom_voronoiRedBlackRotateLeft(this, sibling); + sibling = parent.L; + } + sibling.C = parent.C; + parent.C = sibling.L.C = false; + d3_geom_voronoiRedBlackRotateRight(this, parent); + node = this._; + break; + } + } + sibling.C = true; + node = parent; + parent = parent.U; + } while (!node.C); + if (node) node.C = false; + } + }; + function d3_geom_voronoiRedBlackRotateLeft(tree, node) { + var p = node, q = node.R, parent = p.U; + if (parent) { + if (parent.L === p) parent.L = q; else parent.R = q; + } else { + tree._ = q; + } + q.U = parent; + p.U = q; + p.R = q.L; + if (p.R) p.R.U = p; + q.L = p; + } + function d3_geom_voronoiRedBlackRotateRight(tree, node) { + var p = node, q = node.L, parent = p.U; + if (parent) { + if (parent.L === p) parent.L = q; else parent.R = q; + } else { + tree._ = q; + } + q.U = parent; + p.U = q; + p.L = q.R; + if (p.L) p.L.U = p; + q.R = p; + } + function d3_geom_voronoiRedBlackFirst(node) { + while (node.L) node = node.L; + return node; + } + function d3_geom_voronoi(sites, bbox) { + var site = sites.sort(d3_geom_voronoiVertexOrder).pop(), x0, y0, circle; + d3_geom_voronoiEdges = []; + d3_geom_voronoiCells = new Array(sites.length); + d3_geom_voronoiBeaches = new d3_geom_voronoiRedBlackTree(); + d3_geom_voronoiCircles = new d3_geom_voronoiRedBlackTree(); + while (true) { + circle = d3_geom_voronoiFirstCircle; + if (site && (!circle || site.y < circle.y || site.y === circle.y && site.x < circle.x)) { + if (site.x !== x0 || site.y !== y0) { + d3_geom_voronoiCells[site.i] = new d3_geom_voronoiCell(site); + d3_geom_voronoiAddBeach(site); + x0 = site.x, y0 = site.y; + } + site = sites.pop(); + } else if (circle) { + d3_geom_voronoiRemoveBeach(circle.arc); + } else { + break; + } + } + if (bbox) d3_geom_voronoiClipEdges(bbox), d3_geom_voronoiCloseCells(bbox); + var diagram = { + cells: d3_geom_voronoiCells, + edges: d3_geom_voronoiEdges + }; + d3_geom_voronoiBeaches = d3_geom_voronoiCircles = d3_geom_voronoiEdges = d3_geom_voronoiCells = null; + return diagram; + } + function d3_geom_voronoiVertexOrder(a, b) { + return b.y - a.y || b.x - a.x; + } + d3.geom.voronoi = function(points) { + var x = d3_geom_pointX, y = d3_geom_pointY, fx = x, fy = y, clipExtent = d3_geom_voronoiClipExtent; + if (points) return voronoi(points); + function voronoi(data) { + var polygons = new Array(data.length), x0 = clipExtent[0][0], y0 = clipExtent[0][1], x1 = clipExtent[1][0], y1 = clipExtent[1][1]; + d3_geom_voronoi(sites(data), clipExtent).cells.forEach(function(cell, i) { + var edges = cell.edges, site = cell.site, polygon = polygons[i] = edges.length ? edges.map(function(e) { + var s = e.start(); + return [ s.x, s.y ]; + }) : site.x >= x0 && site.x <= x1 && site.y >= y0 && site.y <= y1 ? [ [ x0, y1 ], [ x1, y1 ], [ x1, y0 ], [ x0, y0 ] ] : []; + polygon.point = data[i]; + }); + return polygons; + } + function sites(data) { + return data.map(function(d, i) { + return { + x: Math.round(fx(d, i) / ε) * ε, + y: Math.round(fy(d, i) / ε) * ε, + i: i + }; + }); + } + voronoi.links = function(data) { + return d3_geom_voronoi(sites(data)).edges.filter(function(edge) { + return edge.l && edge.r; + }).map(function(edge) { + return { + source: data[edge.l.i], + target: data[edge.r.i] + }; + }); + }; + voronoi.triangles = function(data) { + var triangles = []; + d3_geom_voronoi(sites(data)).cells.forEach(function(cell, i) { + var site = cell.site, edges = cell.edges.sort(d3_geom_voronoiHalfEdgeOrder), j = -1, m = edges.length, e0, s0, e1 = edges[m - 1].edge, s1 = e1.l === site ? e1.r : e1.l; + while (++j < m) { + e0 = e1; + s0 = s1; + e1 = edges[j].edge; + s1 = e1.l === site ? e1.r : e1.l; + if (i < s0.i && i < s1.i && d3_geom_voronoiTriangleArea(site, s0, s1) < 0) { + triangles.push([ data[i], data[s0.i], data[s1.i] ]); + } + } + }); + return triangles; + }; + voronoi.x = function(_) { + return arguments.length ? (fx = d3_functor(x = _), voronoi) : x; + }; + voronoi.y = function(_) { + return arguments.length ? (fy = d3_functor(y = _), voronoi) : y; + }; + voronoi.clipExtent = function(_) { + if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent; + clipExtent = _ == null ? d3_geom_voronoiClipExtent : _; + return voronoi; + }; + voronoi.size = function(_) { + if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent && clipExtent[1]; + return voronoi.clipExtent(_ && [ [ 0, 0 ], _ ]); + }; + return voronoi; + }; + var d3_geom_voronoiClipExtent = [ [ -1e6, -1e6 ], [ 1e6, 1e6 ] ]; + function d3_geom_voronoiTriangleArea(a, b, c) { + return (a.x - c.x) * (b.y - a.y) - (a.x - b.x) * (c.y - a.y); + } + d3.geom.delaunay = function(vertices) { + return d3.geom.voronoi().triangles(vertices); + }; + d3.geom.quadtree = function(points, x1, y1, x2, y2) { + var x = d3_geom_pointX, y = d3_geom_pointY, compat; + if (compat = arguments.length) { + x = d3_geom_quadtreeCompatX; + y = d3_geom_quadtreeCompatY; + if (compat === 3) { + y2 = y1; + x2 = x1; + y1 = x1 = 0; + } + return quadtree(points); + } + function quadtree(data) { + var d, fx = d3_functor(x), fy = d3_functor(y), xs, ys, i, n, x1_, y1_, x2_, y2_; + if (x1 != null) { + x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2; + } else { + x2_ = y2_ = -(x1_ = y1_ = Infinity); + xs = [], ys = []; + n = data.length; + if (compat) for (i = 0; i < n; ++i) { + d = data[i]; + if (d.x < x1_) x1_ = d.x; + if (d.y < y1_) y1_ = d.y; + if (d.x > x2_) x2_ = d.x; + if (d.y > y2_) y2_ = d.y; + xs.push(d.x); + ys.push(d.y); + } else for (i = 0; i < n; ++i) { + var x_ = +fx(d = data[i], i), y_ = +fy(d, i); + if (x_ < x1_) x1_ = x_; + if (y_ < y1_) y1_ = y_; + if (x_ > x2_) x2_ = x_; + if (y_ > y2_) y2_ = y_; + xs.push(x_); + ys.push(y_); + } + } + var dx = x2_ - x1_, dy = y2_ - y1_; + if (dx > dy) y2_ = y1_ + dx; else x2_ = x1_ + dy; + function insert(n, d, x, y, x1, y1, x2, y2) { + if (isNaN(x) || isNaN(y)) return; + if (n.leaf) { + var nx = n.x, ny = n.y; + if (nx != null) { + if (abs(nx - x) + abs(ny - y) < .01) { + insertChild(n, d, x, y, x1, y1, x2, y2); + } else { + var nPoint = n.point; + n.x = n.y = n.point = null; + insertChild(n, nPoint, nx, ny, x1, y1, x2, y2); + insertChild(n, d, x, y, x1, y1, x2, y2); + } + } else { + n.x = x, n.y = y, n.point = d; + } + } else { + insertChild(n, d, x, y, x1, y1, x2, y2); + } + } + function insertChild(n, d, x, y, x1, y1, x2, y2) { + var xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym, i = below << 1 | right; + n.leaf = false; + n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode()); + if (right) x1 = xm; else x2 = xm; + if (below) y1 = ym; else y2 = ym; + insert(n, d, x, y, x1, y1, x2, y2); + } + var root = d3_geom_quadtreeNode(); + root.add = function(d) { + insert(root, d, +fx(d, ++i), +fy(d, i), x1_, y1_, x2_, y2_); + }; + root.visit = function(f) { + d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_); + }; + root.find = function(point) { + return d3_geom_quadtreeFind(root, point[0], point[1], x1_, y1_, x2_, y2_); + }; + i = -1; + if (x1 == null) { + while (++i < n) { + insert(root, data[i], xs[i], ys[i], x1_, y1_, x2_, y2_); + } + --i; + } else data.forEach(root.add); + xs = ys = data = d = null; + return root; + } + quadtree.x = function(_) { + return arguments.length ? (x = _, quadtree) : x; + }; + quadtree.y = function(_) { + return arguments.length ? (y = _, quadtree) : y; + }; + quadtree.extent = function(_) { + if (!arguments.length) return x1 == null ? null : [ [ x1, y1 ], [ x2, y2 ] ]; + if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0], + y2 = +_[1][1]; + return quadtree; + }; + quadtree.size = function(_) { + if (!arguments.length) return x1 == null ? null : [ x2 - x1, y2 - y1 ]; + if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = y1 = 0, x2 = +_[0], y2 = +_[1]; + return quadtree; + }; + return quadtree; + }; + function d3_geom_quadtreeCompatX(d) { + return d.x; + } + function d3_geom_quadtreeCompatY(d) { + return d.y; + } + function d3_geom_quadtreeNode() { + return { + leaf: true, + nodes: [], + point: null, + x: null, + y: null + }; + } + function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) { + if (!f(node, x1, y1, x2, y2)) { + var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, children = node.nodes; + if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy); + if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy); + if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2); + if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2); + } + } + function d3_geom_quadtreeFind(root, x, y, x0, y0, x3, y3) { + var minDistance2 = Infinity, closestPoint; + (function find(node, x1, y1, x2, y2) { + if (x1 > x3 || y1 > y3 || x2 < x0 || y2 < y0) return; + if (point = node.point) { + var point, dx = x - node.x, dy = y - node.y, distance2 = dx * dx + dy * dy; + if (distance2 < minDistance2) { + var distance = Math.sqrt(minDistance2 = distance2); + x0 = x - distance, y0 = y - distance; + x3 = x + distance, y3 = y + distance; + closestPoint = point; + } + } + var children = node.nodes, xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym; + for (var i = below << 1 | right, j = i + 4; i < j; ++i) { + if (node = children[i & 3]) switch (i & 3) { + case 0: + find(node, x1, y1, xm, ym); + break; - prev_code = code; + case 1: + find(node, xm, y1, x2, ym); + break; + + case 2: + find(node, x1, ym, xm, y2); + break; + + case 3: + find(node, xm, ym, x2, y2); + break; + } + } + })(root, x0, y0, x3, y3); + return closestPoint; + } + d3.interpolateRgb = d3_interpolateRgb; + function d3_interpolateRgb(a, b) { + a = d3.rgb(a); + b = d3.rgb(b); + var ar = a.r, ag = a.g, ab = a.b, br = b.r - ar, bg = b.g - ag, bb = b.b - ab; + return function(t) { + return "#" + d3_rgb_hex(Math.round(ar + br * t)) + d3_rgb_hex(Math.round(ag + bg * t)) + d3_rgb_hex(Math.round(ab + bb * t)); + }; + } + d3.interpolateObject = d3_interpolateObject; + function d3_interpolateObject(a, b) { + var i = {}, c = {}, k; + for (k in a) { + if (k in b) { + i[k] = d3_interpolate(a[k], b[k]); + } else { + c[k] = a[k]; + } + } + for (k in b) { + if (!(k in a)) { + c[k] = b[k]; + } + } + return function(t) { + for (k in i) c[k] = i[k](t); + return c; + }; + } + d3.interpolateNumber = d3_interpolateNumber; + function d3_interpolateNumber(a, b) { + a = +a, b = +b; + return function(t) { + return a * (1 - t) + b * t; + }; + } + d3.interpolateString = d3_interpolateString; + function d3_interpolateString(a, b) { + var bi = d3_interpolate_numberA.lastIndex = d3_interpolate_numberB.lastIndex = 0, am, bm, bs, i = -1, s = [], q = []; + a = a + "", b = b + ""; + while ((am = d3_interpolate_numberA.exec(a)) && (bm = d3_interpolate_numberB.exec(b))) { + if ((bs = bm.index) > bi) { + bs = b.slice(bi, bs); + if (s[i]) s[i] += bs; else s[++i] = bs; + } + if ((am = am[0]) === (bm = bm[0])) { + if (s[i]) s[i] += bm; else s[++i] = bm; + } else { + s[++i] = null; + q.push({ + i: i, + x: d3_interpolateNumber(am, bm) + }); + } + bi = d3_interpolate_numberB.lastIndex; + } + if (bi < b.length) { + bs = b.slice(bi); + if (s[i]) s[i] += bs; else s[++i] = bs; + } + return s.length < 2 ? q[0] ? (b = q[0].x, function(t) { + return b(t) + ""; + }) : function() { + return b; + } : (b = q.length, function(t) { + for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t); + return s.join(""); + }); + } + var d3_interpolate_numberA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g, d3_interpolate_numberB = new RegExp(d3_interpolate_numberA.source, "g"); + d3.interpolate = d3_interpolate; + function d3_interpolate(a, b) { + var i = d3.interpolators.length, f; + while (--i >= 0 && !(f = d3.interpolators[i](a, b))) ; + return f; + } + d3.interpolators = [ function(a, b) { + var t = typeof b; + return (t === "string" ? d3_rgb_names.has(b.toLowerCase()) || /^(#|rgb\(|hsl\()/i.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_color ? d3_interpolateRgb : Array.isArray(b) ? d3_interpolateArray : t === "object" && isNaN(b) ? d3_interpolateObject : d3_interpolateNumber)(a, b); + } ]; + d3.interpolateArray = d3_interpolateArray; + function d3_interpolateArray(a, b) { + var x = [], c = [], na = a.length, nb = b.length, n0 = Math.min(a.length, b.length), i; + for (i = 0; i < n0; ++i) x.push(d3_interpolate(a[i], b[i])); + for (;i < na; ++i) c[i] = a[i]; + for (;i < nb; ++i) c[i] = b[i]; + return function(t) { + for (i = 0; i < n0; ++i) c[i] = x[i](t); + return c; + }; + } + var d3_ease_default = function() { + return d3_identity; + }; + var d3_ease = d3.map({ + linear: d3_ease_default, + poly: d3_ease_poly, + quad: function() { + return d3_ease_quad; + }, + cubic: function() { + return d3_ease_cubic; + }, + sin: function() { + return d3_ease_sin; + }, + exp: function() { + return d3_ease_exp; + }, + circle: function() { + return d3_ease_circle; + }, + elastic: d3_ease_elastic, + back: d3_ease_back, + bounce: function() { + return d3_ease_bounce; + } + }); + var d3_ease_mode = d3.map({ + "in": d3_identity, + out: d3_ease_reverse, + "in-out": d3_ease_reflect, + "out-in": function(f) { + return d3_ease_reflect(d3_ease_reverse(f)); + } + }); + d3.ease = function(name) { + var i = name.indexOf("-"), t = i >= 0 ? name.slice(0, i) : name, m = i >= 0 ? name.slice(i + 1) : "in"; + t = d3_ease.get(t) || d3_ease_default; + m = d3_ease_mode.get(m) || d3_identity; + return d3_ease_clamp(m(t.apply(null, d3_arraySlice.call(arguments, 1)))); + }; + function d3_ease_clamp(f) { + return function(t) { + return t <= 0 ? 0 : t >= 1 ? 1 : f(t); + }; + } + function d3_ease_reverse(f) { + return function(t) { + return 1 - f(1 - t); + }; + } + function d3_ease_reflect(f) { + return function(t) { + return .5 * (t < .5 ? f(2 * t) : 2 - f(2 - 2 * t)); + }; + } + function d3_ease_quad(t) { + return t * t; + } + function d3_ease_cubic(t) { + return t * t * t; + } + function d3_ease_cubicInOut(t) { + if (t <= 0) return 0; + if (t >= 1) return 1; + var t2 = t * t, t3 = t2 * t; + return 4 * (t < .5 ? t3 : 3 * (t - t2) + t3 - .75); + } + function d3_ease_poly(e) { + return function(t) { + return Math.pow(t, e); + }; + } + function d3_ease_sin(t) { + return 1 - Math.cos(t * halfπ); + } + function d3_ease_exp(t) { + return Math.pow(2, 10 * (t - 1)); + } + function d3_ease_circle(t) { + return 1 - Math.sqrt(1 - t * t); + } + function d3_ease_elastic(a, p) { + var s; + if (arguments.length < 2) p = .45; + if (arguments.length) s = p / τ * Math.asin(1 / a); else a = 1, s = p / 4; + return function(t) { + return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * τ / p); + }; + } + function d3_ease_back(s) { + if (!s) s = 1.70158; + return function(t) { + return t * t * ((s + 1) * t - s); + }; + } + function d3_ease_bounce(t) { + return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; + } + d3.interpolateHcl = d3_interpolateHcl; + function d3_interpolateHcl(a, b) { + a = d3.hcl(a); + b = d3.hcl(b); + var ah = a.h, ac = a.c, al = a.l, bh = b.h - ah, bc = b.c - ac, bl = b.l - al; + if (isNaN(bc)) bc = 0, ac = isNaN(ac) ? b.c : ac; + if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; + return function(t) { + return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + ""; + }; + } + d3.interpolateHsl = d3_interpolateHsl; + function d3_interpolateHsl(a, b) { + a = d3.hsl(a); + b = d3.hsl(b); + var ah = a.h, as = a.s, al = a.l, bh = b.h - ah, bs = b.s - as, bl = b.l - al; + if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as; + if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; + return function(t) { + return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + ""; + }; + } + d3.interpolateLab = d3_interpolateLab; + function d3_interpolateLab(a, b) { + a = d3.lab(a); + b = d3.lab(b); + var al = a.l, aa = a.a, ab = a.b, bl = b.l - al, ba = b.a - aa, bb = b.b - ab; + return function(t) { + return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + ""; + }; + } + d3.interpolateRound = d3_interpolateRound; + function d3_interpolateRound(a, b) { + b -= a; + return function(t) { + return Math.round(a + b * t); + }; + } + d3.transform = function(string) { + var g = d3_document.createElementNS(d3.ns.prefix.svg, "g"); + return (d3.transform = function(string) { + if (string != null) { + g.setAttribute("transform", string); + var t = g.transform.baseVal.consolidate(); + } + return new d3_transform(t ? t.matrix : d3_transformIdentity); + })(string); + }; + function d3_transform(m) { + var r0 = [ m.a, m.b ], r1 = [ m.c, m.d ], kx = d3_transformNormalize(r0), kz = d3_transformDot(r0, r1), ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0; + if (r0[0] * r1[1] < r1[0] * r0[1]) { + r0[0] *= -1; + r0[1] *= -1; + kx *= -1; + kz *= -1; + } + this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_degrees; + this.translate = [ m.e, m.f ]; + this.scale = [ kx, ky ]; + this.skew = ky ? Math.atan2(kz, ky) * d3_degrees : 0; + } + d3_transform.prototype.toString = function() { + return "translate(" + this.translate + ")rotate(" + this.rotate + ")skewX(" + this.skew + ")scale(" + this.scale + ")"; + }; + function d3_transformDot(a, b) { + return a[0] * b[0] + a[1] * b[1]; + } + function d3_transformNormalize(a) { + var k = Math.sqrt(d3_transformDot(a, a)); + if (k) { + a[0] /= k; + a[1] /= k; + } + return k; + } + function d3_transformCombine(a, b, k) { + a[0] += k * b[0]; + a[1] += k * b[1]; + return a; + } + var d3_transformIdentity = { + a: 1, + b: 0, + c: 0, + d: 1, + e: 0, + f: 0 + }; + d3.interpolateTransform = d3_interpolateTransform; + function d3_interpolateTransformPop(s) { + return s.length ? s.pop() + "," : ""; + } + function d3_interpolateTranslate(ta, tb, s, q) { + if (ta[0] !== tb[0] || ta[1] !== tb[1]) { + var i = s.push("translate(", null, ",", null, ")"); + q.push({ + i: i - 4, + x: d3_interpolateNumber(ta[0], tb[0]) + }, { + i: i - 2, + x: d3_interpolateNumber(ta[1], tb[1]) + }); + } else if (tb[0] || tb[1]) { + s.push("translate(" + tb + ")"); + } + } + function d3_interpolateRotate(ra, rb, s, q) { + if (ra !== rb) { + if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; + q.push({ + i: s.push(d3_interpolateTransformPop(s) + "rotate(", null, ")") - 2, + x: d3_interpolateNumber(ra, rb) + }); + } else if (rb) { + s.push(d3_interpolateTransformPop(s) + "rotate(" + rb + ")"); + } + } + function d3_interpolateSkew(wa, wb, s, q) { + if (wa !== wb) { + q.push({ + i: s.push(d3_interpolateTransformPop(s) + "skewX(", null, ")") - 2, + x: d3_interpolateNumber(wa, wb) + }); + } else if (wb) { + s.push(d3_interpolateTransformPop(s) + "skewX(" + wb + ")"); + } + } + function d3_interpolateScale(ka, kb, s, q) { + if (ka[0] !== kb[0] || ka[1] !== kb[1]) { + var i = s.push(d3_interpolateTransformPop(s) + "scale(", null, ",", null, ")"); + q.push({ + i: i - 4, + x: d3_interpolateNumber(ka[0], kb[0]) + }, { + i: i - 2, + x: d3_interpolateNumber(ka[1], kb[1]) + }); + } else if (kb[0] !== 1 || kb[1] !== 1) { + s.push(d3_interpolateTransformPop(s) + "scale(" + kb + ")"); + } + } + function d3_interpolateTransform(a, b) { + var s = [], q = []; + a = d3.transform(a), b = d3.transform(b); + d3_interpolateTranslate(a.translate, b.translate, s, q); + d3_interpolateRotate(a.rotate, b.rotate, s, q); + d3_interpolateSkew(a.skew, b.skew, s, q); + d3_interpolateScale(a.scale, b.scale, s, q); + a = b = null; + return function(t) { + var i = -1, n = q.length, o; + while (++i < n) s[(o = q[i]).i] = o.x(t); + return s.join(""); + }; + } + function d3_uninterpolateNumber(a, b) { + b = (b -= a = +a) || 1 / b; + return function(x) { + return (x - a) / b; + }; + } + function d3_uninterpolateClamp(a, b) { + b = (b -= a = +a) || 1 / b; + return function(x) { + return Math.max(0, Math.min(1, (x - a) / b)); + }; + } + d3.layout = {}; + d3.layout.bundle = function() { + return function(links) { + var paths = [], i = -1, n = links.length; + while (++i < n) paths.push(d3_layout_bundlePath(links[i])); + return paths; + }; + }; + function d3_layout_bundlePath(link) { + var start = link.source, end = link.target, lca = d3_layout_bundleLeastCommonAncestor(start, end), points = [ start ]; + while (start !== lca) { + start = start.parent; + points.push(start); + } + var k = points.length; + while (end !== lca) { + points.splice(k, 0, end); + end = end.parent; + } + return points; + } + function d3_layout_bundleAncestors(node) { + var ancestors = [], parent = node.parent; + while (parent != null) { + ancestors.push(node); + node = parent; + parent = parent.parent; + } + ancestors.push(node); + return ancestors; + } + function d3_layout_bundleLeastCommonAncestor(a, b) { + if (a === b) return a; + var aNodes = d3_layout_bundleAncestors(a), bNodes = d3_layout_bundleAncestors(b), aNode = aNodes.pop(), bNode = bNodes.pop(), sharedNode = null; + while (aNode === bNode) { + sharedNode = aNode; + aNode = aNodes.pop(); + bNode = bNodes.pop(); + } + return sharedNode; + } + d3.layout.chord = function() { + var chord = {}, chords, groups, matrix, n, padding = 0, sortGroups, sortSubgroups, sortChords; + function relayout() { + var subgroups = {}, groupSums = [], groupIndex = d3.range(n), subgroupIndex = [], k, x, x0, i, j; + chords = []; + groups = []; + k = 0, i = -1; + while (++i < n) { + x = 0, j = -1; + while (++j < n) { + x += matrix[i][j]; + } + groupSums.push(x); + subgroupIndex.push(d3.range(n)); + k += x; + } + if (sortGroups) { + groupIndex.sort(function(a, b) { + return sortGroups(groupSums[a], groupSums[b]); + }); + } + if (sortSubgroups) { + subgroupIndex.forEach(function(d, i) { + d.sort(function(a, b) { + return sortSubgroups(matrix[i][a], matrix[i][b]); + }); + }); + } + k = (τ - padding * n) / k; + x = 0, i = -1; + while (++i < n) { + x0 = x, j = -1; + while (++j < n) { + var di = groupIndex[i], dj = subgroupIndex[di][j], v = matrix[di][dj], a0 = x, a1 = x += v * k; + subgroups[di + "-" + dj] = { + index: di, + subindex: dj, + startAngle: a0, + endAngle: a1, + value: v + }; + } + groups[di] = { + index: di, + startAngle: x0, + endAngle: x, + value: groupSums[di] + }; + x += padding; + } + i = -1; + while (++i < n) { + j = i - 1; + while (++j < n) { + var source = subgroups[i + "-" + j], target = subgroups[j + "-" + i]; + if (source.value || target.value) { + chords.push(source.value < target.value ? { + source: target, + target: source + } : { + source: source, + target: target + }); + } + } + } + if (sortChords) resort(); + } + function resort() { + chords.sort(function(a, b) { + return sortChords((a.source.value + a.target.value) / 2, (b.source.value + b.target.value) / 2); + }); + } + chord.matrix = function(x) { + if (!arguments.length) return matrix; + n = (matrix = x) && matrix.length; + chords = groups = null; + return chord; + }; + chord.padding = function(x) { + if (!arguments.length) return padding; + padding = x; + chords = groups = null; + return chord; + }; + chord.sortGroups = function(x) { + if (!arguments.length) return sortGroups; + sortGroups = x; + chords = groups = null; + return chord; + }; + chord.sortSubgroups = function(x) { + if (!arguments.length) return sortSubgroups; + sortSubgroups = x; + chords = null; + return chord; + }; + chord.sortChords = function(x) { + if (!arguments.length) return sortChords; + sortChords = x; + if (chords) resort(); + return chord; + }; + chord.chords = function() { + if (!chords) relayout(); + return chords; + }; + chord.groups = function() { + if (!groups) relayout(); + return groups; + }; + return chord; + }; + d3.layout.force = function() { + var force = {}, event = d3.dispatch("start", "tick", "end"), timer, size = [ 1, 1 ], drag, alpha, friction = .9, linkDistance = d3_layout_forceLinkDistance, linkStrength = d3_layout_forceLinkStrength, charge = -30, chargeDistance2 = d3_layout_forceChargeDistance2, gravity = .1, theta2 = .64, nodes = [], links = [], distances, strengths, charges; + function repulse(node) { + return function(quad, x1, _, x2) { + if (quad.point !== node) { + var dx = quad.cx - node.x, dy = quad.cy - node.y, dw = x2 - x1, dn = dx * dx + dy * dy; + if (dw * dw / theta2 < dn) { + if (dn < chargeDistance2) { + var k = quad.charge / dn; + node.px -= dx * k; + node.py -= dy * k; + } + return true; + } + if (quad.point && dn && dn < chargeDistance2) { + var k = quad.pointCharge / dn; + node.px -= dx * k; + node.py -= dy * k; + } + } + return !quad.charge; + }; + } + force.tick = function() { + if ((alpha *= .99) < .005) { + timer = null; + event.end({ + type: "end", + alpha: alpha = 0 + }); + return true; + } + var n = nodes.length, m = links.length, q, i, o, s, t, l, k, x, y; + for (i = 0; i < m; ++i) { + o = links[i]; + s = o.source; + t = o.target; + x = t.x - s.x; + y = t.y - s.y; + if (l = x * x + y * y) { + l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l; + x *= l; + y *= l; + t.x -= x * (k = s.weight + t.weight ? s.weight / (s.weight + t.weight) : .5); + t.y -= y * k; + s.x += x * (k = 1 - k); + s.y += y * k; + } + } + if (k = alpha * gravity) { + x = size[0] / 2; + y = size[1] / 2; + i = -1; + if (k) while (++i < n) { + o = nodes[i]; + o.x += (x - o.x) * k; + o.y += (y - o.y) * k; + } + } + if (charge) { + d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges); + i = -1; + while (++i < n) { + if (!(o = nodes[i]).fixed) { + q.visit(repulse(o)); + } + } + } + i = -1; + while (++i < n) { + o = nodes[i]; + if (o.fixed) { + o.x = o.px; + o.y = o.py; + } else { + o.x -= (o.px - (o.px = o.x)) * friction; + o.y -= (o.py - (o.py = o.y)) * friction; + } + } + event.tick({ + type: "tick", + alpha: alpha + }); + }; + force.nodes = function(x) { + if (!arguments.length) return nodes; + nodes = x; + return force; + }; + force.links = function(x) { + if (!arguments.length) return links; + links = x; + return force; + }; + force.size = function(x) { + if (!arguments.length) return size; + size = x; + return force; + }; + force.linkDistance = function(x) { + if (!arguments.length) return linkDistance; + linkDistance = typeof x === "function" ? x : +x; + return force; + }; + force.distance = force.linkDistance; + force.linkStrength = function(x) { + if (!arguments.length) return linkStrength; + linkStrength = typeof x === "function" ? x : +x; + return force; + }; + force.friction = function(x) { + if (!arguments.length) return friction; + friction = +x; + return force; + }; + force.charge = function(x) { + if (!arguments.length) return charge; + charge = typeof x === "function" ? x : +x; + return force; + }; + force.chargeDistance = function(x) { + if (!arguments.length) return Math.sqrt(chargeDistance2); + chargeDistance2 = x * x; + return force; + }; + force.gravity = function(x) { + if (!arguments.length) return gravity; + gravity = +x; + return force; + }; + force.theta = function(x) { + if (!arguments.length) return Math.sqrt(theta2); + theta2 = x * x; + return force; + }; + force.alpha = function(x) { + if (!arguments.length) return alpha; + x = +x; + if (alpha) { + if (x > 0) { + alpha = x; + } else { + timer.c = null, timer.t = NaN, timer = null; + event.end({ + type: "end", + alpha: alpha = 0 + }); + } + } else if (x > 0) { + event.start({ + type: "start", + alpha: alpha = x + }); + timer = d3_timer(force.tick); + } + return force; + }; + force.start = function() { + var i, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o; + for (i = 0; i < n; ++i) { + (o = nodes[i]).index = i; + o.weight = 0; + } + for (i = 0; i < m; ++i) { + o = links[i]; + if (typeof o.source == "number") o.source = nodes[o.source]; + if (typeof o.target == "number") o.target = nodes[o.target]; + ++o.source.weight; + ++o.target.weight; + } + for (i = 0; i < n; ++i) { + o = nodes[i]; + if (isNaN(o.x)) o.x = position("x", w); + if (isNaN(o.y)) o.y = position("y", h); + if (isNaN(o.px)) o.px = o.x; + if (isNaN(o.py)) o.py = o.y; + } + distances = []; + if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i); else for (i = 0; i < m; ++i) distances[i] = linkDistance; + strengths = []; + if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i); else for (i = 0; i < m; ++i) strengths[i] = linkStrength; + charges = []; + if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge; + function position(dimension, size) { + if (!neighbors) { + neighbors = new Array(n); + for (j = 0; j < n; ++j) { + neighbors[j] = []; + } + for (j = 0; j < m; ++j) { + var o = links[j]; + neighbors[o.source.index].push(o.target); + neighbors[o.target.index].push(o.source); + } + } + var candidates = neighbors[i], j = -1, l = candidates.length, x; + while (++j < l) if (!isNaN(x = candidates[j][dimension])) return x; + return Math.random() * size; + } + return force.resume(); + }; + force.resume = function() { + return force.alpha(.1); + }; + force.stop = function() { + return force.alpha(0); + }; + force.drag = function() { + if (!drag) drag = d3.behavior.drag().origin(d3_identity).on("dragstart.force", d3_layout_forceDragstart).on("drag.force", dragmove).on("dragend.force", d3_layout_forceDragend); + if (!arguments.length) return drag; + this.on("mouseover.force", d3_layout_forceMouseover).on("mouseout.force", d3_layout_forceMouseout).call(drag); + }; + function dragmove(d) { + d.px = d3.event.x, d.py = d3.event.y; + force.resume(); + } + return d3.rebind(force, event, "on"); + }; + function d3_layout_forceDragstart(d) { + d.fixed |= 2; + } + function d3_layout_forceDragend(d) { + d.fixed &= ~6; + } + function d3_layout_forceMouseover(d) { + d.fixed |= 4; + d.px = d.x, d.py = d.y; + } + function d3_layout_forceMouseout(d) { + d.fixed &= ~4; + } + function d3_layout_forceAccumulate(quad, alpha, charges) { + var cx = 0, cy = 0; + quad.charge = 0; + if (!quad.leaf) { + var nodes = quad.nodes, n = nodes.length, i = -1, c; + while (++i < n) { + c = nodes[i]; + if (c == null) continue; + d3_layout_forceAccumulate(c, alpha, charges); + quad.charge += c.charge; + cx += c.charge * c.cx; + cy += c.charge * c.cy; + } + } + if (quad.point) { + if (!quad.leaf) { + quad.point.x += Math.random() - .5; + quad.point.y += Math.random() - .5; + } + var k = alpha * charges[quad.point.index]; + quad.charge += quad.pointCharge = k; + cx += k * quad.point.x; + cy += k * quad.point.y; + } + quad.cx = cx / quad.charge; + quad.cy = cy / quad.charge; + } + var d3_layout_forceLinkDistance = 20, d3_layout_forceLinkStrength = 1, d3_layout_forceChargeDistance2 = Infinity; + d3.layout.hierarchy = function() { + var sort = d3_layout_hierarchySort, children = d3_layout_hierarchyChildren, value = d3_layout_hierarchyValue; + function hierarchy(root) { + var stack = [ root ], nodes = [], node; + root.depth = 0; + while ((node = stack.pop()) != null) { + nodes.push(node); + if ((childs = children.call(hierarchy, node, node.depth)) && (n = childs.length)) { + var n, childs, child; + while (--n >= 0) { + stack.push(child = childs[n]); + child.parent = node; + child.depth = node.depth + 1; + } + if (value) node.value = 0; + node.children = childs; + } else { + if (value) node.value = +value.call(hierarchy, node, node.depth) || 0; + delete node.children; + } + } + d3_layout_hierarchyVisitAfter(root, function(node) { + var childs, parent; + if (sort && (childs = node.children)) childs.sort(sort); + if (value && (parent = node.parent)) parent.value += node.value; + }); + return nodes; + } + hierarchy.sort = function(x) { + if (!arguments.length) return sort; + sort = x; + return hierarchy; + }; + hierarchy.children = function(x) { + if (!arguments.length) return children; + children = x; + return hierarchy; + }; + hierarchy.value = function(x) { + if (!arguments.length) return value; + value = x; + return hierarchy; + }; + hierarchy.revalue = function(root) { + if (value) { + d3_layout_hierarchyVisitBefore(root, function(node) { + if (node.children) node.value = 0; + }); + d3_layout_hierarchyVisitAfter(root, function(node) { + var parent; + if (!node.children) node.value = +value.call(hierarchy, node, node.depth) || 0; + if (parent = node.parent) parent.value += node.value; + }); + } + return root; + }; + return hierarchy; + }; + function d3_layout_hierarchyRebind(object, hierarchy) { + d3.rebind(object, hierarchy, "sort", "children", "value"); + object.nodes = object; + object.links = d3_layout_hierarchyLinks; + return object; + } + function d3_layout_hierarchyVisitBefore(node, callback) { + var nodes = [ node ]; + while ((node = nodes.pop()) != null) { + callback(node); + if ((children = node.children) && (n = children.length)) { + var n, children; + while (--n >= 0) nodes.push(children[n]); + } + } + } + function d3_layout_hierarchyVisitAfter(node, callback) { + var nodes = [ node ], nodes2 = []; + while ((node = nodes.pop()) != null) { + nodes2.push(node); + if ((children = node.children) && (n = children.length)) { + var i = -1, n, children; + while (++i < n) nodes.push(children[i]); + } + } + while ((node = nodes2.pop()) != null) { + callback(node); + } + } + function d3_layout_hierarchyChildren(d) { + return d.children; + } + function d3_layout_hierarchyValue(d) { + return d.value; + } + function d3_layout_hierarchySort(a, b) { + return b.value - a.value; + } + function d3_layout_hierarchyLinks(nodes) { + return d3.merge(nodes.map(function(parent) { + return (parent.children || []).map(function(child) { + return { + source: parent, + target: child + }; + }); + })); + } + d3.layout.partition = function() { + var hierarchy = d3.layout.hierarchy(), size = [ 1, 1 ]; + function position(node, x, dx, dy) { + var children = node.children; + node.x = x; + node.y = node.depth * dy; + node.dx = dx; + node.dy = dy; + if (children && (n = children.length)) { + var i = -1, n, c, d; + dx = node.value ? dx / node.value : 0; + while (++i < n) { + position(c = children[i], x, d = c.value * dx, dy); + x += d; + } + } + } + function depth(node) { + var children = node.children, d = 0; + if (children && (n = children.length)) { + var i = -1, n; + while (++i < n) d = Math.max(d, depth(children[i])); + } + return 1 + d; + } + function partition(d, i) { + var nodes = hierarchy.call(this, d, i); + position(nodes[0], 0, size[0], size[1] / depth(nodes[0])); + return nodes; + } + partition.size = function(x) { + if (!arguments.length) return size; + size = x; + return partition; + }; + return d3_layout_hierarchyRebind(partition, hierarchy); + }; + d3.layout.pie = function() { + var value = Number, sort = d3_layout_pieSortByValue, startAngle = 0, endAngle = τ, padAngle = 0; + function pie(data) { + var n = data.length, values = data.map(function(d, i) { + return +value.call(pie, d, i); + }), a = +(typeof startAngle === "function" ? startAngle.apply(this, arguments) : startAngle), da = (typeof endAngle === "function" ? endAngle.apply(this, arguments) : endAngle) - a, p = Math.min(Math.abs(da) / n, +(typeof padAngle === "function" ? padAngle.apply(this, arguments) : padAngle)), pa = p * (da < 0 ? -1 : 1), sum = d3.sum(values), k = sum ? (da - n * pa) / sum : 0, index = d3.range(n), arcs = [], v; + if (sort != null) index.sort(sort === d3_layout_pieSortByValue ? function(i, j) { + return values[j] - values[i]; + } : function(i, j) { + return sort(data[i], data[j]); + }); + index.forEach(function(i) { + arcs[i] = { + data: data[i], + value: v = values[i], + startAngle: a, + endAngle: a += v * k + pa, + padAngle: p + }; + }); + return arcs; + } + pie.value = function(_) { + if (!arguments.length) return value; + value = _; + return pie; + }; + pie.sort = function(_) { + if (!arguments.length) return sort; + sort = _; + return pie; + }; + pie.startAngle = function(_) { + if (!arguments.length) return startAngle; + startAngle = _; + return pie; + }; + pie.endAngle = function(_) { + if (!arguments.length) return endAngle; + endAngle = _; + return pie; + }; + pie.padAngle = function(_) { + if (!arguments.length) return padAngle; + padAngle = _; + return pie; + }; + return pie; + }; + var d3_layout_pieSortByValue = {}; + d3.layout.stack = function() { + var values = d3_identity, order = d3_layout_stackOrderDefault, offset = d3_layout_stackOffsetZero, out = d3_layout_stackOut, x = d3_layout_stackX, y = d3_layout_stackY; + function stack(data, index) { + if (!(n = data.length)) return data; + var series = data.map(function(d, i) { + return values.call(stack, d, i); + }); + var points = series.map(function(d) { + return d.map(function(v, i) { + return [ x.call(stack, v, i), y.call(stack, v, i) ]; + }); + }); + var orders = order.call(stack, points, index); + series = d3.permute(series, orders); + points = d3.permute(points, orders); + var offsets = offset.call(stack, points, index); + var m = series[0].length, n, i, j, o; + for (j = 0; j < m; ++j) { + out.call(stack, series[0][j], o = offsets[j], points[0][j][1]); + for (i = 1; i < n; ++i) { + out.call(stack, series[i][j], o += points[i - 1][j][1], points[i][j][1]); + } + } + return data; + } + stack.values = function(x) { + if (!arguments.length) return values; + values = x; + return stack; + }; + stack.order = function(x) { + if (!arguments.length) return order; + order = typeof x === "function" ? x : d3_layout_stackOrders.get(x) || d3_layout_stackOrderDefault; + return stack; + }; + stack.offset = function(x) { + if (!arguments.length) return offset; + offset = typeof x === "function" ? x : d3_layout_stackOffsets.get(x) || d3_layout_stackOffsetZero; + return stack; + }; + stack.x = function(z) { + if (!arguments.length) return x; + x = z; + return stack; + }; + stack.y = function(z) { + if (!arguments.length) return y; + y = z; + return stack; + }; + stack.out = function(z) { + if (!arguments.length) return out; + out = z; + return stack; + }; + return stack; + }; + function d3_layout_stackX(d) { + return d.x; + } + function d3_layout_stackY(d) { + return d.y; + } + function d3_layout_stackOut(d, y0, y) { + d.y0 = y0; + d.y = y; + } + var d3_layout_stackOrders = d3.map({ + "inside-out": function(data) { + var n = data.length, i, j, max = data.map(d3_layout_stackMaxIndex), sums = data.map(d3_layout_stackReduceSum), index = d3.range(n).sort(function(a, b) { + return max[a] - max[b]; + }), top = 0, bottom = 0, tops = [], bottoms = []; + for (i = 0; i < n; ++i) { + j = index[i]; + if (top < bottom) { + top += sums[j]; + tops.push(j); + } else { + bottom += sums[j]; + bottoms.push(j); + } + } + return bottoms.reverse().concat(tops); + }, + reverse: function(data) { + return d3.range(data.length).reverse(); + }, + "default": d3_layout_stackOrderDefault + }); + var d3_layout_stackOffsets = d3.map({ + silhouette: function(data) { + var n = data.length, m = data[0].length, sums = [], max = 0, i, j, o, y0 = []; + for (j = 0; j < m; ++j) { + for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; + if (o > max) max = o; + sums.push(o); + } + for (j = 0; j < m; ++j) { + y0[j] = (max - sums[j]) / 2; + } + return y0; + }, + wiggle: function(data) { + var n = data.length, x = data[0], m = x.length, i, j, k, s1, s2, s3, dx, o, o0, y0 = []; + y0[0] = o = o0 = 0; + for (j = 1; j < m; ++j) { + for (i = 0, s1 = 0; i < n; ++i) s1 += data[i][j][1]; + for (i = 0, s2 = 0, dx = x[j][0] - x[j - 1][0]; i < n; ++i) { + for (k = 0, s3 = (data[i][j][1] - data[i][j - 1][1]) / (2 * dx); k < i; ++k) { + s3 += (data[k][j][1] - data[k][j - 1][1]) / dx; + } + s2 += s3 * data[i][j][1]; + } + y0[j] = o -= s1 ? s2 / s1 * dx : 0; + if (o < o0) o0 = o; + } + for (j = 0; j < m; ++j) y0[j] -= o0; + return y0; + }, + expand: function(data) { + var n = data.length, m = data[0].length, k = 1 / n, i, j, o, y0 = []; + for (j = 0; j < m; ++j) { + for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; + if (o) for (i = 0; i < n; i++) data[i][j][1] /= o; else for (i = 0; i < n; i++) data[i][j][1] = k; + } + for (j = 0; j < m; ++j) y0[j] = 0; + return y0; + }, + zero: d3_layout_stackOffsetZero + }); + function d3_layout_stackOrderDefault(data) { + return d3.range(data.length); + } + function d3_layout_stackOffsetZero(data) { + var j = -1, m = data[0].length, y0 = []; + while (++j < m) y0[j] = 0; + return y0; + } + function d3_layout_stackMaxIndex(array) { + var i = 1, j = 0, v = array[0][1], k, n = array.length; + for (;i < n; ++i) { + if ((k = array[i][1]) > v) { + j = i; + v = k; + } + } + return j; + } + function d3_layout_stackReduceSum(d) { + return d.reduce(d3_layout_stackSum, 0); + } + function d3_layout_stackSum(p, d) { + return p + d[1]; + } + d3.layout.histogram = function() { + var frequency = true, valuer = Number, ranger = d3_layout_histogramRange, binner = d3_layout_histogramBinSturges; + function histogram(data, i) { + var bins = [], values = data.map(valuer, this), range = ranger.call(this, values, i), thresholds = binner.call(this, range, values, i), bin, i = -1, n = values.length, m = thresholds.length - 1, k = frequency ? 1 : 1 / n, x; + while (++i < m) { + bin = bins[i] = []; + bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]); + bin.y = 0; + } + if (m > 0) { + i = -1; + while (++i < n) { + x = values[i]; + if (x >= range[0] && x <= range[1]) { + bin = bins[d3.bisect(thresholds, x, 1, m) - 1]; + bin.y += k; + bin.push(data[i]); + } + } + } + return bins; + } + histogram.value = function(x) { + if (!arguments.length) return valuer; + valuer = x; + return histogram; + }; + histogram.range = function(x) { + if (!arguments.length) return ranger; + ranger = d3_functor(x); + return histogram; + }; + histogram.bins = function(x) { + if (!arguments.length) return binner; + binner = typeof x === "number" ? function(range) { + return d3_layout_histogramBinFixed(range, x); + } : d3_functor(x); + return histogram; + }; + histogram.frequency = function(x) { + if (!arguments.length) return frequency; + frequency = !!x; + return histogram; + }; + return histogram; + }; + function d3_layout_histogramBinSturges(range, values) { + return d3_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1)); + } + function d3_layout_histogramBinFixed(range, n) { + var x = -1, b = +range[0], m = (range[1] - b) / n, f = []; + while (++x <= n) f[x] = m * x + b; + return f; + } + function d3_layout_histogramRange(values) { + return [ d3.min(values), d3.max(values) ]; + } + d3.layout.pack = function() { + var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius; + function pack(d, i) { + var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() { + return radius; + }; + root.x = root.y = 0; + d3_layout_hierarchyVisitAfter(root, function(d) { + d.r = +r(d.value); + }); + d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings); + if (padding) { + var dr = padding * (radius ? 1 : Math.max(2 * root.r / w, 2 * root.r / h)) / 2; + d3_layout_hierarchyVisitAfter(root, function(d) { + d.r += dr; + }); + d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings); + d3_layout_hierarchyVisitAfter(root, function(d) { + d.r -= dr; + }); + } + d3_layout_packTransform(root, w / 2, h / 2, radius ? 1 : 1 / Math.max(2 * root.r / w, 2 * root.r / h)); + return nodes; + } + pack.size = function(_) { + if (!arguments.length) return size; + size = _; + return pack; + }; + pack.radius = function(_) { + if (!arguments.length) return radius; + radius = _ == null || typeof _ === "function" ? _ : +_; + return pack; + }; + pack.padding = function(_) { + if (!arguments.length) return padding; + padding = +_; + return pack; + }; + return d3_layout_hierarchyRebind(pack, hierarchy); + }; + function d3_layout_packSort(a, b) { + return a.value - b.value; + } + function d3_layout_packInsert(a, b) { + var c = a._pack_next; + a._pack_next = b; + b._pack_prev = a; + b._pack_next = c; + c._pack_prev = b; + } + function d3_layout_packSplice(a, b) { + a._pack_next = b; + b._pack_prev = a; + } + function d3_layout_packIntersects(a, b) { + var dx = b.x - a.x, dy = b.y - a.y, dr = a.r + b.r; + return .999 * dr * dr > dx * dx + dy * dy; + } + function d3_layout_packSiblings(node) { + if (!(nodes = node.children) || !(n = nodes.length)) return; + var nodes, xMin = Infinity, xMax = -Infinity, yMin = Infinity, yMax = -Infinity, a, b, c, i, j, k, n; + function bound(node) { + xMin = Math.min(node.x - node.r, xMin); + xMax = Math.max(node.x + node.r, xMax); + yMin = Math.min(node.y - node.r, yMin); + yMax = Math.max(node.y + node.r, yMax); + } + nodes.forEach(d3_layout_packLink); + a = nodes[0]; + a.x = -a.r; + a.y = 0; + bound(a); + if (n > 1) { + b = nodes[1]; + b.x = b.r; + b.y = 0; + bound(b); + if (n > 2) { + c = nodes[2]; + d3_layout_packPlace(a, b, c); + bound(c); + d3_layout_packInsert(a, c); + a._pack_prev = c; + d3_layout_packInsert(c, b); + b = a._pack_next; + for (i = 3; i < n; i++) { + d3_layout_packPlace(a, b, c = nodes[i]); + var isect = 0, s1 = 1, s2 = 1; + for (j = b._pack_next; j !== b; j = j._pack_next, s1++) { + if (d3_layout_packIntersects(j, c)) { + isect = 1; + break; + } + } + if (isect == 1) { + for (k = a._pack_prev; k !== j._pack_prev; k = k._pack_prev, s2++) { + if (d3_layout_packIntersects(k, c)) { + break; + } + } + } + if (isect) { + if (s1 < s2 || s1 == s2 && b.r < a.r) d3_layout_packSplice(a, b = j); else d3_layout_packSplice(a = k, b); + i--; + } else { + d3_layout_packInsert(a, c); + b = c; + bound(c); + } + } + } + } + var cx = (xMin + xMax) / 2, cy = (yMin + yMax) / 2, cr = 0; + for (i = 0; i < n; i++) { + c = nodes[i]; + c.x -= cx; + c.y -= cy; + cr = Math.max(cr, c.r + Math.sqrt(c.x * c.x + c.y * c.y)); + } + node.r = cr; + nodes.forEach(d3_layout_packUnlink); + } + function d3_layout_packLink(node) { + node._pack_next = node._pack_prev = node; + } + function d3_layout_packUnlink(node) { + delete node._pack_next; + delete node._pack_prev; + } + function d3_layout_packTransform(node, x, y, k) { + var children = node.children; + node.x = x += k * node.x; + node.y = y += k * node.y; + node.r *= k; + if (children) { + var i = -1, n = children.length; + while (++i < n) d3_layout_packTransform(children[i], x, y, k); + } + } + function d3_layout_packPlace(a, b, c) { + var db = a.r + c.r, dx = b.x - a.x, dy = b.y - a.y; + if (db && (dx || dy)) { + var da = b.r + c.r, dc = dx * dx + dy * dy; + da *= da; + db *= db; + var x = .5 + (db - da) / (2 * dc), y = Math.sqrt(Math.max(0, 2 * da * (db + dc) - (db -= dc) * db - da * da)) / (2 * dc); + c.x = a.x + x * dx + y * dy; + c.y = a.y + x * dy - y * dx; + } else { + c.x = a.x + db; + c.y = a.y; + } + } + d3.layout.tree = function() { + var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = null; + function tree(d, i) { + var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0); + d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z; + d3_layout_hierarchyVisitBefore(root1, secondWalk); + if (nodeSize) d3_layout_hierarchyVisitBefore(root0, sizeNode); else { + var left = root0, right = root0, bottom = root0; + d3_layout_hierarchyVisitBefore(root0, function(node) { + if (node.x < left.x) left = node; + if (node.x > right.x) right = node; + if (node.depth > bottom.depth) bottom = node; + }); + var tx = separation(left, right) / 2 - left.x, kx = size[0] / (right.x + separation(right, left) / 2 + tx), ky = size[1] / (bottom.depth || 1); + d3_layout_hierarchyVisitBefore(root0, function(node) { + node.x = (node.x + tx) * kx; + node.y = node.depth * ky; + }); + } + return nodes; + } + function wrapTree(root0) { + var root1 = { + A: null, + children: [ root0 ] + }, queue = [ root1 ], node1; + while ((node1 = queue.pop()) != null) { + for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) { + queue.push((children[i] = child = { + _: children[i], + parent: node1, + children: (child = children[i].children) && child.slice() || [], + A: null, + a: null, + z: 0, + m: 0, + c: 0, + s: 0, + t: null, + i: i + }).a = child); + } + } + return root1.children[0]; + } + function firstWalk(v) { + var children = v.children, siblings = v.parent.children, w = v.i ? siblings[v.i - 1] : null; + if (children.length) { + d3_layout_treeShift(v); + var midpoint = (children[0].z + children[children.length - 1].z) / 2; + if (w) { + v.z = w.z + separation(v._, w._); + v.m = v.z - midpoint; + } else { + v.z = midpoint; + } + } else if (w) { + v.z = w.z + separation(v._, w._); + } + v.parent.A = apportion(v, w, v.parent.A || siblings[0]); + } + function secondWalk(v) { + v._.x = v.z + v.parent.m; + v.m += v.parent.m; + } + function apportion(v, w, ancestor) { + if (w) { + var vip = v, vop = v, vim = w, vom = vip.parent.children[0], sip = vip.m, sop = vop.m, sim = vim.m, som = vom.m, shift; + while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) { + vom = d3_layout_treeLeft(vom); + vop = d3_layout_treeRight(vop); + vop.a = v; + shift = vim.z + sim - vip.z - sip + separation(vim._, vip._); + if (shift > 0) { + d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift); + sip += shift; + sop += shift; + } + sim += vim.m; + sip += vip.m; + som += vom.m; + sop += vop.m; + } + if (vim && !d3_layout_treeRight(vop)) { + vop.t = vim; + vop.m += sim - sop; + } + if (vip && !d3_layout_treeLeft(vom)) { + vom.t = vip; + vom.m += sip - som; + ancestor = v; + } + } + return ancestor; + } + function sizeNode(node) { + node.x *= size[0]; + node.y = node.depth * size[1]; + } + tree.separation = function(x) { + if (!arguments.length) return separation; + separation = x; + return tree; + }; + tree.size = function(x) { + if (!arguments.length) return nodeSize ? null : size; + nodeSize = (size = x) == null ? sizeNode : null; + return tree; + }; + tree.nodeSize = function(x) { + if (!arguments.length) return nodeSize ? size : null; + nodeSize = (size = x) == null ? null : sizeNode; + return tree; + }; + return d3_layout_hierarchyRebind(tree, hierarchy); + }; + function d3_layout_treeSeparation(a, b) { + return a.parent == b.parent ? 1 : 2; + } + function d3_layout_treeLeft(v) { + var children = v.children; + return children.length ? children[0] : v.t; + } + function d3_layout_treeRight(v) { + var children = v.children, n; + return (n = children.length) ? children[n - 1] : v.t; + } + function d3_layout_treeMove(wm, wp, shift) { + var change = shift / (wp.i - wm.i); + wp.c -= change; + wp.s += shift; + wm.c += change; + wp.z += shift; + wp.m += shift; + } + function d3_layout_treeShift(v) { + var shift = 0, change = 0, children = v.children, i = children.length, w; + while (--i >= 0) { + w = children[i]; + w.z += shift; + w.m += shift; + shift += w.s + (change += w.c); + } + } + function d3_layout_treeAncestor(vim, v, ancestor) { + return vim.a.parent === v.parent ? vim.a : ancestor; + } + d3.layout.cluster = function() { + var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; + function cluster(d, i) { + var nodes = hierarchy.call(this, d, i), root = nodes[0], previousNode, x = 0; + d3_layout_hierarchyVisitAfter(root, function(node) { + var children = node.children; + if (children && children.length) { + node.x = d3_layout_clusterX(children); + node.y = d3_layout_clusterY(children); + } else { + node.x = previousNode ? x += separation(node, previousNode) : 0; + node.y = 0; + previousNode = node; + } + }); + var left = d3_layout_clusterLeft(root), right = d3_layout_clusterRight(root), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2; + d3_layout_hierarchyVisitAfter(root, nodeSize ? function(node) { + node.x = (node.x - root.x) * size[0]; + node.y = (root.y - node.y) * size[1]; + } : function(node) { + node.x = (node.x - x0) / (x1 - x0) * size[0]; + node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1]; + }); + return nodes; + } + cluster.separation = function(x) { + if (!arguments.length) return separation; + separation = x; + return cluster; + }; + cluster.size = function(x) { + if (!arguments.length) return nodeSize ? null : size; + nodeSize = (size = x) == null; + return cluster; + }; + cluster.nodeSize = function(x) { + if (!arguments.length) return nodeSize ? size : null; + nodeSize = (size = x) != null; + return cluster; + }; + return d3_layout_hierarchyRebind(cluster, hierarchy); + }; + function d3_layout_clusterY(children) { + return 1 + d3.max(children, function(child) { + return child.y; + }); + } + function d3_layout_clusterX(children) { + return children.reduce(function(x, child) { + return x + child.x; + }, 0) / children.length; + } + function d3_layout_clusterLeft(node) { + var children = node.children; + return children && children.length ? d3_layout_clusterLeft(children[0]) : node; + } + function d3_layout_clusterRight(node) { + var children = node.children, n; + return children && (n = children.length) ? d3_layout_clusterRight(children[n - 1]) : node; + } + d3.layout.treemap = function() { + var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, mode = "squarify", ratio = .5 * (1 + Math.sqrt(5)); + function scale(children, k) { + var i = -1, n = children.length, child, area; + while (++i < n) { + area = (child = children[i]).value * (k < 0 ? 0 : k); + child.area = isNaN(area) || area <= 0 ? 0 : area; + } + } + function squarify(node) { + var children = node.children; + if (children && children.length) { + var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" ? rect.dy : mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx : Math.min(rect.dx, rect.dy), n; + scale(remaining, rect.dx * rect.dy / node.value); + row.area = 0; + while ((n = remaining.length) > 0) { + row.push(child = remaining[n - 1]); + row.area += child.area; + if (mode !== "squarify" || (score = worst(row, u)) <= best) { + remaining.pop(); + best = score; + } else { + row.area -= row.pop().area; + position(row, u, rect, false); + u = Math.min(rect.dx, rect.dy); + row.length = row.area = 0; + best = Infinity; + } + } + if (row.length) { + position(row, u, rect, true); + row.length = row.area = 0; + } + children.forEach(squarify); + } + } + function stickify(node) { + var children = node.children; + if (children && children.length) { + var rect = pad(node), remaining = children.slice(), child, row = []; + scale(remaining, rect.dx * rect.dy / node.value); + row.area = 0; + while (child = remaining.pop()) { + row.push(child); + row.area += child.area; + if (child.z != null) { + position(row, child.z ? rect.dx : rect.dy, rect, !remaining.length); + row.length = row.area = 0; + } + } + children.forEach(stickify); + } + } + function worst(row, u) { + var s = row.area, r, rmax = 0, rmin = Infinity, i = -1, n = row.length; + while (++i < n) { + if (!(r = row[i].area)) continue; + if (r < rmin) rmin = r; + if (r > rmax) rmax = r; + } + s *= s; + u *= u; + return s ? Math.max(u * rmax * ratio / s, s / (u * rmin * ratio)) : Infinity; + } + function position(row, u, rect, flush) { + var i = -1, n = row.length, x = rect.x, y = rect.y, v = u ? round(row.area / u) : 0, o; + if (u == rect.dx) { + if (flush || v > rect.dy) v = rect.dy; + while (++i < n) { + o = row[i]; + o.x = x; + o.y = y; + o.dy = v; + x += o.dx = Math.min(rect.x + rect.dx - x, v ? round(o.area / v) : 0); + } + o.z = true; + o.dx += rect.x + rect.dx - x; + rect.y += v; + rect.dy -= v; + } else { + if (flush || v > rect.dx) v = rect.dx; + while (++i < n) { + o = row[i]; + o.x = x; + o.y = y; + o.dx = v; + y += o.dy = Math.min(rect.y + rect.dy - y, v ? round(o.area / v) : 0); + } + o.z = false; + o.dy += rect.y + rect.dy - y; + rect.x += v; + rect.dx -= v; + } + } + function treemap(d) { + var nodes = stickies || hierarchy(d), root = nodes[0]; + root.x = root.y = 0; + if (root.value) root.dx = size[0], root.dy = size[1]; else root.dx = root.dy = 0; + if (stickies) hierarchy.revalue(root); + scale([ root ], root.dx * root.dy / root.value); + (stickies ? stickify : squarify)(root); + if (sticky) stickies = nodes; + return nodes; + } + treemap.size = function(x) { + if (!arguments.length) return size; + size = x; + return treemap; + }; + treemap.padding = function(x) { + if (!arguments.length) return padding; + function padFunction(node) { + var p = x.call(treemap, node, node.depth); + return p == null ? d3_layout_treemapPadNull(node) : d3_layout_treemapPad(node, typeof p === "number" ? [ p, p, p, p ] : p); + } + function padConstant(node) { + return d3_layout_treemapPad(node, x); + } + var type; + pad = (padding = x) == null ? d3_layout_treemapPadNull : (type = typeof x) === "function" ? padFunction : type === "number" ? (x = [ x, x, x, x ], + padConstant) : padConstant; + return treemap; + }; + treemap.round = function(x) { + if (!arguments.length) return round != Number; + round = x ? Math.round : Number; + return treemap; + }; + treemap.sticky = function(x) { + if (!arguments.length) return sticky; + sticky = x; + stickies = null; + return treemap; + }; + treemap.ratio = function(x) { + if (!arguments.length) return ratio; + ratio = x; + return treemap; + }; + treemap.mode = function(x) { + if (!arguments.length) return mode; + mode = x + ""; + return treemap; + }; + return d3_layout_hierarchyRebind(treemap, hierarchy); + }; + function d3_layout_treemapPadNull(node) { + return { + x: node.x, + y: node.y, + dx: node.dx, + dy: node.dy + }; + } + function d3_layout_treemapPad(node, padding) { + var x = node.x + padding[3], y = node.y + padding[0], dx = node.dx - padding[1] - padding[3], dy = node.dy - padding[0] - padding[2]; + if (dx < 0) { + x += dx / 2; + dx = 0; + } + if (dy < 0) { + y += dy / 2; + dy = 0; + } + return { + x: x, + y: y, + dx: dx, + dy: dy + }; + } + d3.random = { + normal: function(µ, σ) { + var n = arguments.length; + if (n < 2) σ = 1; + if (n < 1) µ = 0; + return function() { + var x, y, r; + do { + x = Math.random() * 2 - 1; + y = Math.random() * 2 - 1; + r = x * x + y * y; + } while (!r || r > 1); + return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r); + }; + }, + logNormal: function() { + var random = d3.random.normal.apply(d3, arguments); + return function() { + return Math.exp(random()); + }; + }, + bates: function(m) { + var random = d3.random.irwinHall(m); + return function() { + return random() / m; + }; + }, + irwinHall: function(m) { + return function() { + for (var s = 0, j = 0; j < m; j++) s += Math.random(); + return s; + }; + } + }; + d3.scale = {}; + function d3_scaleExtent(domain) { + var start = domain[0], stop = domain[domain.length - 1]; + return start < stop ? [ start, stop ] : [ stop, start ]; + } + function d3_scaleRange(scale) { + return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range()); + } + function d3_scale_bilinear(domain, range, uninterpolate, interpolate) { + var u = uninterpolate(domain[0], domain[1]), i = interpolate(range[0], range[1]); + return function(x) { + return i(u(x)); + }; + } + function d3_scale_nice(domain, nice) { + var i0 = 0, i1 = domain.length - 1, x0 = domain[i0], x1 = domain[i1], dx; + if (x1 < x0) { + dx = i0, i0 = i1, i1 = dx; + dx = x0, x0 = x1, x1 = dx; + } + domain[i0] = nice.floor(x0); + domain[i1] = nice.ceil(x1); + return domain; + } + function d3_scale_niceStep(step) { + return step ? { + floor: function(x) { + return Math.floor(x / step) * step; + }, + ceil: function(x) { + return Math.ceil(x / step) * step; + } + } : d3_scale_niceIdentity; + } + var d3_scale_niceIdentity = { + floor: d3_identity, + ceil: d3_identity + }; + function d3_scale_polylinear(domain, range, uninterpolate, interpolate) { + var u = [], i = [], j = 0, k = Math.min(domain.length, range.length) - 1; + if (domain[k] < domain[0]) { + domain = domain.slice().reverse(); + range = range.slice().reverse(); + } + while (++j <= k) { + u.push(uninterpolate(domain[j - 1], domain[j])); + i.push(interpolate(range[j - 1], range[j])); + } + return function(x) { + var j = d3.bisect(domain, x, 1, k) - 1; + return i[j](u[j](x)); + }; + } + d3.scale.linear = function() { + return d3_scale_linear([ 0, 1 ], [ 0, 1 ], d3_interpolate, false); + }; + function d3_scale_linear(domain, range, interpolate, clamp) { + var output, input; + function rescale() { + var linear = Math.min(domain.length, range.length) > 2 ? d3_scale_polylinear : d3_scale_bilinear, uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber; + output = linear(domain, range, uninterpolate, interpolate); + input = linear(range, domain, uninterpolate, d3_interpolate); + return scale; + } + function scale(x) { + return output(x); + } + scale.invert = function(y) { + return input(y); + }; + scale.domain = function(x) { + if (!arguments.length) return domain; + domain = x.map(Number); + return rescale(); + }; + scale.range = function(x) { + if (!arguments.length) return range; + range = x; + return rescale(); + }; + scale.rangeRound = function(x) { + return scale.range(x).interpolate(d3_interpolateRound); + }; + scale.clamp = function(x) { + if (!arguments.length) return clamp; + clamp = x; + return rescale(); + }; + scale.interpolate = function(x) { + if (!arguments.length) return interpolate; + interpolate = x; + return rescale(); + }; + scale.ticks = function(m) { + return d3_scale_linearTicks(domain, m); + }; + scale.tickFormat = function(m, format) { + return d3_scale_linearTickFormat(domain, m, format); + }; + scale.nice = function(m) { + d3_scale_linearNice(domain, m); + return rescale(); + }; + scale.copy = function() { + return d3_scale_linear(domain, range, interpolate, clamp); + }; + return rescale(); + } + function d3_scale_linearRebind(scale, linear) { + return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp"); + } + function d3_scale_linearNice(domain, m) { + d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); + d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); + return domain; + } + function d3_scale_linearTickRange(domain, m) { + if (m == null) m = 10; + var extent = d3_scaleExtent(domain), span = extent[1] - extent[0], step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), err = m / span * step; + if (err <= .15) step *= 10; else if (err <= .35) step *= 5; else if (err <= .75) step *= 2; + extent[0] = Math.ceil(extent[0] / step) * step; + extent[1] = Math.floor(extent[1] / step) * step + step * .5; + extent[2] = step; + return extent; + } + function d3_scale_linearTicks(domain, m) { + return d3.range.apply(d3, d3_scale_linearTickRange(domain, m)); + } + function d3_scale_linearTickFormat(domain, m, format) { + var range = d3_scale_linearTickRange(domain, m); + if (format) { + var match = d3_format_re.exec(format); + match.shift(); + if (match[8] === "s") { + var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1]))); + if (!match[7]) match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2])); + match[8] = "f"; + format = d3.format(match.join("")); + return function(d) { + return format(prefix.scale(d)) + prefix.symbol; + }; + } + if (!match[7]) match[7] = "." + d3_scale_linearFormatPrecision(match[8], range); + format = match.join(""); + } else { + format = ",." + d3_scale_linearPrecision(range[2]) + "f"; + } + return d3.format(format); + } + var d3_scale_linearFormatSignificant = { + s: 1, + g: 1, + p: 1, + r: 1, + e: 1 + }; + function d3_scale_linearPrecision(value) { + return -Math.floor(Math.log(value) / Math.LN10 + .01); + } + function d3_scale_linearFormatPrecision(type, range) { + var p = d3_scale_linearPrecision(range[2]); + return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(abs(range[0]), abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2; + } + d3.scale.log = function() { + return d3_scale_log(d3.scale.linear().domain([ 0, 1 ]), 10, true, [ 1, 10 ]); + }; + function d3_scale_log(linear, base, positive, domain) { + function log(x) { + return (positive ? Math.log(x < 0 ? 0 : x) : -Math.log(x > 0 ? 0 : -x)) / Math.log(base); + } + function pow(x) { + return positive ? Math.pow(base, x) : -Math.pow(base, -x); + } + function scale(x) { + return linear(log(x)); + } + scale.invert = function(x) { + return pow(linear.invert(x)); + }; + scale.domain = function(x) { + if (!arguments.length) return domain; + positive = x[0] >= 0; + linear.domain((domain = x.map(Number)).map(log)); + return scale; + }; + scale.base = function(_) { + if (!arguments.length) return base; + base = +_; + linear.domain(domain.map(log)); + return scale; + }; + scale.nice = function() { + var niced = d3_scale_nice(domain.map(log), positive ? Math : d3_scale_logNiceNegative); + linear.domain(niced); + domain = niced.map(pow); + return scale; + }; + scale.ticks = function() { + var extent = d3_scaleExtent(domain), ticks = [], u = extent[0], v = extent[1], i = Math.floor(log(u)), j = Math.ceil(log(v)), n = base % 1 ? 2 : base; + if (isFinite(j - i)) { + if (positive) { + for (;i < j; i++) for (var k = 1; k < n; k++) ticks.push(pow(i) * k); + ticks.push(pow(i)); + } else { + ticks.push(pow(i)); + for (;i++ < j; ) for (var k = n - 1; k > 0; k--) ticks.push(pow(i) * k); + } + for (i = 0; ticks[i] < u; i++) {} + for (j = ticks.length; ticks[j - 1] > v; j--) {} + ticks = ticks.slice(i, j); + } + return ticks; + }; + scale.tickFormat = function(n, format) { + if (!arguments.length) return d3_scale_logFormat; + if (arguments.length < 2) format = d3_scale_logFormat; else if (typeof format !== "function") format = d3.format(format); + var k = Math.max(1, base * n / scale.ticks().length); + return function(d) { + var i = d / pow(Math.round(log(d))); + if (i * base < base - .5) i *= base; + return i <= k ? format(d) : ""; + }; + }; + scale.copy = function() { + return d3_scale_log(linear.copy(), base, positive, domain); + }; + return d3_scale_linearRebind(scale, linear); + } + var d3_scale_logFormat = d3.format(".0e"), d3_scale_logNiceNegative = { + floor: function(x) { + return -Math.ceil(-x); + }, + ceil: function(x) { + return -Math.floor(-x); + } + }; + d3.scale.pow = function() { + return d3_scale_pow(d3.scale.linear(), 1, [ 0, 1 ]); + }; + function d3_scale_pow(linear, exponent, domain) { + var powp = d3_scale_powPow(exponent), powb = d3_scale_powPow(1 / exponent); + function scale(x) { + return linear(powp(x)); + } + scale.invert = function(x) { + return powb(linear.invert(x)); + }; + scale.domain = function(x) { + if (!arguments.length) return domain; + linear.domain((domain = x.map(Number)).map(powp)); + return scale; + }; + scale.ticks = function(m) { + return d3_scale_linearTicks(domain, m); + }; + scale.tickFormat = function(m, format) { + return d3_scale_linearTickFormat(domain, m, format); + }; + scale.nice = function(m) { + return scale.domain(d3_scale_linearNice(domain, m)); + }; + scale.exponent = function(x) { + if (!arguments.length) return exponent; + powp = d3_scale_powPow(exponent = x); + powb = d3_scale_powPow(1 / exponent); + linear.domain(domain.map(powp)); + return scale; + }; + scale.copy = function() { + return d3_scale_pow(linear.copy(), exponent, domain); + }; + return d3_scale_linearRebind(scale, linear); + } + function d3_scale_powPow(e) { + return function(x) { + return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e); + }; + } + d3.scale.sqrt = function() { + return d3.scale.pow().exponent(.5); + }; + d3.scale.ordinal = function() { + return d3_scale_ordinal([], { + t: "range", + a: [ [] ] + }); + }; + function d3_scale_ordinal(domain, ranger) { + var index, range, rangeBand; + function scale(x) { + return range[((index.get(x) || (ranger.t === "range" ? index.set(x, domain.push(x)) : NaN)) - 1) % range.length]; + } + function steps(start, step) { + return d3.range(domain.length).map(function(i) { + return start + step * i; + }); + } + scale.domain = function(x) { + if (!arguments.length) return domain; + domain = []; + index = new d3_Map(); + var i = -1, n = x.length, xi; + while (++i < n) if (!index.has(xi = x[i])) index.set(xi, domain.push(xi)); + return scale[ranger.t].apply(scale, ranger.a); + }; + scale.range = function(x) { + if (!arguments.length) return range; + range = x; + rangeBand = 0; + ranger = { + t: "range", + a: arguments + }; + return scale; + }; + scale.rangePoints = function(x, padding) { + if (arguments.length < 2) padding = 0; + var start = x[0], stop = x[1], step = domain.length < 2 ? (start = (start + stop) / 2, + 0) : (stop - start) / (domain.length - 1 + padding); + range = steps(start + step * padding / 2, step); + rangeBand = 0; + ranger = { + t: "rangePoints", + a: arguments + }; + return scale; + }; + scale.rangeRoundPoints = function(x, padding) { + if (arguments.length < 2) padding = 0; + var start = x[0], stop = x[1], step = domain.length < 2 ? (start = stop = Math.round((start + stop) / 2), + 0) : (stop - start) / (domain.length - 1 + padding) | 0; + range = steps(start + Math.round(step * padding / 2 + (stop - start - (domain.length - 1 + padding) * step) / 2), step); + rangeBand = 0; + ranger = { + t: "rangeRoundPoints", + a: arguments + }; + return scale; + }; + scale.rangeBands = function(x, padding, outerPadding) { + if (arguments.length < 2) padding = 0; + if (arguments.length < 3) outerPadding = padding; + var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = (stop - start) / (domain.length - padding + 2 * outerPadding); + range = steps(start + step * outerPadding, step); + if (reverse) range.reverse(); + rangeBand = step * (1 - padding); + ranger = { + t: "rangeBands", + a: arguments + }; + return scale; + }; + scale.rangeRoundBands = function(x, padding, outerPadding) { + if (arguments.length < 2) padding = 0; + if (arguments.length < 3) outerPadding = padding; + var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = Math.floor((stop - start) / (domain.length - padding + 2 * outerPadding)); + range = steps(start + Math.round((stop - start - (domain.length - padding) * step) / 2), step); + if (reverse) range.reverse(); + rangeBand = Math.round(step * (1 - padding)); + ranger = { + t: "rangeRoundBands", + a: arguments + }; + return scale; + }; + scale.rangeBand = function() { + return rangeBand; + }; + scale.rangeExtent = function() { + return d3_scaleExtent(ranger.a[0]); + }; + scale.copy = function() { + return d3_scale_ordinal(domain, ranger); + }; + return scale.domain(domain); + } + d3.scale.category10 = function() { + return d3.scale.ordinal().range(d3_category10); + }; + d3.scale.category20 = function() { + return d3.scale.ordinal().range(d3_category20); + }; + d3.scale.category20b = function() { + return d3.scale.ordinal().range(d3_category20b); + }; + d3.scale.category20c = function() { + return d3.scale.ordinal().range(d3_category20c); + }; + var d3_category10 = [ 2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175 ].map(d3_rgbString); + var d3_category20 = [ 2062260, 11454440, 16744206, 16759672, 2924588, 10018698, 14034728, 16750742, 9725885, 12955861, 9197131, 12885140, 14907330, 16234194, 8355711, 13092807, 12369186, 14408589, 1556175, 10410725 ].map(d3_rgbString); + var d3_category20b = [ 3750777, 5395619, 7040719, 10264286, 6519097, 9216594, 11915115, 13556636, 9202993, 12426809, 15186514, 15190932, 8666169, 11356490, 14049643, 15177372, 8077683, 10834324, 13528509, 14589654 ].map(d3_rgbString); + var d3_category20c = [ 3244733, 7057110, 10406625, 13032431, 15095053, 16616764, 16625259, 16634018, 3253076, 7652470, 10607003, 13101504, 7695281, 10394312, 12369372, 14342891, 6513507, 9868950, 12434877, 14277081 ].map(d3_rgbString); + d3.scale.quantile = function() { + return d3_scale_quantile([], []); + }; + function d3_scale_quantile(domain, range) { + var thresholds; + function rescale() { + var k = 0, q = range.length; + thresholds = []; + while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q); + return scale; + } + function scale(x) { + if (!isNaN(x = +x)) return range[d3.bisect(thresholds, x)]; + } + scale.domain = function(x) { + if (!arguments.length) return domain; + domain = x.map(d3_number).filter(d3_numeric).sort(d3_ascending); + return rescale(); + }; + scale.range = function(x) { + if (!arguments.length) return range; + range = x; + return rescale(); + }; + scale.quantiles = function() { + return thresholds; + }; + scale.invertExtent = function(y) { + y = range.indexOf(y); + return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ]; + }; + scale.copy = function() { + return d3_scale_quantile(domain, range); + }; + return rescale(); + } + d3.scale.quantize = function() { + return d3_scale_quantize(0, 1, [ 0, 1 ]); + }; + function d3_scale_quantize(x0, x1, range) { + var kx, i; + function scale(x) { + return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))]; + } + function rescale() { + kx = range.length / (x1 - x0); + i = range.length - 1; + return scale; + } + scale.domain = function(x) { + if (!arguments.length) return [ x0, x1 ]; + x0 = +x[0]; + x1 = +x[x.length - 1]; + return rescale(); + }; + scale.range = function(x) { + if (!arguments.length) return range; + range = x; + return rescale(); + }; + scale.invertExtent = function(y) { + y = range.indexOf(y); + y = y < 0 ? NaN : y / kx + x0; + return [ y, y + 1 / kx ]; + }; + scale.copy = function() { + return d3_scale_quantize(x0, x1, range); + }; + return rescale(); + } + d3.scale.threshold = function() { + return d3_scale_threshold([ .5 ], [ 0, 1 ]); + }; + function d3_scale_threshold(domain, range) { + function scale(x) { + if (x <= x) return range[d3.bisect(domain, x)]; + } + scale.domain = function(_) { + if (!arguments.length) return domain; + domain = _; + return scale; + }; + scale.range = function(_) { + if (!arguments.length) return range; + range = _; + return scale; + }; + scale.invertExtent = function(y) { + y = range.indexOf(y); + return [ domain[y - 1], domain[y] ]; + }; + scale.copy = function() { + return d3_scale_threshold(domain, range); + }; + return scale; + } + d3.scale.identity = function() { + return d3_scale_identity([ 0, 1 ]); + }; + function d3_scale_identity(domain) { + function identity(x) { + return +x; + } + identity.invert = identity; + identity.domain = identity.range = function(x) { + if (!arguments.length) return domain; + domain = x.map(identity); + return identity; + }; + identity.ticks = function(m) { + return d3_scale_linearTicks(domain, m); + }; + identity.tickFormat = function(m, format) { + return d3_scale_linearTickFormat(domain, m, format); + }; + identity.copy = function() { + return d3_scale_identity(domain); + }; + return identity; + } + d3.svg = {}; + function d3_zero() { + return 0; + } + d3.svg.arc = function() { + var innerRadius = d3_svg_arcInnerRadius, outerRadius = d3_svg_arcOuterRadius, cornerRadius = d3_zero, padRadius = d3_svg_arcAuto, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle, padAngle = d3_svg_arcPadAngle; + function arc() { + var r0 = Math.max(0, +innerRadius.apply(this, arguments)), r1 = Math.max(0, +outerRadius.apply(this, arguments)), a0 = startAngle.apply(this, arguments) - halfπ, a1 = endAngle.apply(this, arguments) - halfπ, da = Math.abs(a1 - a0), cw = a0 > a1 ? 0 : 1; + if (r1 < r0) rc = r1, r1 = r0, r0 = rc; + if (da >= τε) return circleSegment(r1, cw) + (r0 ? circleSegment(r0, 1 - cw) : "") + "Z"; + var rc, cr, rp, ap, p0 = 0, p1 = 0, x0, y0, x1, y1, x2, y2, x3, y3, path = []; + if (ap = (+padAngle.apply(this, arguments) || 0) / 2) { + rp = padRadius === d3_svg_arcAuto ? Math.sqrt(r0 * r0 + r1 * r1) : +padRadius.apply(this, arguments); + if (!cw) p1 *= -1; + if (r1) p1 = d3_asin(rp / r1 * Math.sin(ap)); + if (r0) p0 = d3_asin(rp / r0 * Math.sin(ap)); + } + if (r1) { + x0 = r1 * Math.cos(a0 + p1); + y0 = r1 * Math.sin(a0 + p1); + x1 = r1 * Math.cos(a1 - p1); + y1 = r1 * Math.sin(a1 - p1); + var l1 = Math.abs(a1 - a0 - 2 * p1) <= π ? 0 : 1; + if (p1 && d3_svg_arcSweep(x0, y0, x1, y1) === cw ^ l1) { + var h1 = (a0 + a1) / 2; + x0 = r1 * Math.cos(h1); + y0 = r1 * Math.sin(h1); + x1 = y1 = null; + } + } else { + x0 = y0 = 0; + } + if (r0) { + x2 = r0 * Math.cos(a1 - p0); + y2 = r0 * Math.sin(a1 - p0); + x3 = r0 * Math.cos(a0 + p0); + y3 = r0 * Math.sin(a0 + p0); + var l0 = Math.abs(a0 - a1 + 2 * p0) <= π ? 0 : 1; + if (p0 && d3_svg_arcSweep(x2, y2, x3, y3) === 1 - cw ^ l0) { + var h0 = (a0 + a1) / 2; + x2 = r0 * Math.cos(h0); + y2 = r0 * Math.sin(h0); + x3 = y3 = null; + } + } else { + x2 = y2 = 0; + } + if (da > ε && (rc = Math.min(Math.abs(r1 - r0) / 2, +cornerRadius.apply(this, arguments))) > .001) { + cr = r0 < r1 ^ cw ? 0 : 1; + var rc1 = rc, rc0 = rc; + if (da < π) { + var oc = x3 == null ? [ x2, y2 ] : x1 == null ? [ x0, y0 ] : d3_geom_polygonIntersect([ x0, y0 ], [ x3, y3 ], [ x1, y1 ], [ x2, y2 ]), ax = x0 - oc[0], ay = y0 - oc[1], bx = x1 - oc[0], by = y1 - oc[1], kc = 1 / Math.sin(Math.acos((ax * bx + ay * by) / (Math.sqrt(ax * ax + ay * ay) * Math.sqrt(bx * bx + by * by))) / 2), lc = Math.sqrt(oc[0] * oc[0] + oc[1] * oc[1]); + rc0 = Math.min(rc, (r0 - lc) / (kc - 1)); + rc1 = Math.min(rc, (r1 - lc) / (kc + 1)); + } + if (x1 != null) { + var t30 = d3_svg_arcCornerTangents(x3 == null ? [ x2, y2 ] : [ x3, y3 ], [ x0, y0 ], r1, rc1, cw), t12 = d3_svg_arcCornerTangents([ x1, y1 ], [ x2, y2 ], r1, rc1, cw); + if (rc === rc1) { + path.push("M", t30[0], "A", rc1, ",", rc1, " 0 0,", cr, " ", t30[1], "A", r1, ",", r1, " 0 ", 1 - cw ^ d3_svg_arcSweep(t30[1][0], t30[1][1], t12[1][0], t12[1][1]), ",", cw, " ", t12[1], "A", rc1, ",", rc1, " 0 0,", cr, " ", t12[0]); + } else { + path.push("M", t30[0], "A", rc1, ",", rc1, " 0 1,", cr, " ", t12[0]); + } + } else { + path.push("M", x0, ",", y0); + } + if (x3 != null) { + var t03 = d3_svg_arcCornerTangents([ x0, y0 ], [ x3, y3 ], r0, -rc0, cw), t21 = d3_svg_arcCornerTangents([ x2, y2 ], x1 == null ? [ x0, y0 ] : [ x1, y1 ], r0, -rc0, cw); + if (rc === rc0) { + path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t21[1], "A", r0, ",", r0, " 0 ", cw ^ d3_svg_arcSweep(t21[1][0], t21[1][1], t03[1][0], t03[1][1]), ",", 1 - cw, " ", t03[1], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]); + } else { + path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]); + } + } else { + path.push("L", x2, ",", y2); + } + } else { + path.push("M", x0, ",", y0); + if (x1 != null) path.push("A", r1, ",", r1, " 0 ", l1, ",", cw, " ", x1, ",", y1); + path.push("L", x2, ",", y2); + if (x3 != null) path.push("A", r0, ",", r0, " 0 ", l0, ",", 1 - cw, " ", x3, ",", y3); + } + path.push("Z"); + return path.join(""); + } + function circleSegment(r1, cw) { + return "M0," + r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + -r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + r1; + } + arc.innerRadius = function(v) { + if (!arguments.length) return innerRadius; + innerRadius = d3_functor(v); + return arc; + }; + arc.outerRadius = function(v) { + if (!arguments.length) return outerRadius; + outerRadius = d3_functor(v); + return arc; + }; + arc.cornerRadius = function(v) { + if (!arguments.length) return cornerRadius; + cornerRadius = d3_functor(v); + return arc; + }; + arc.padRadius = function(v) { + if (!arguments.length) return padRadius; + padRadius = v == d3_svg_arcAuto ? d3_svg_arcAuto : d3_functor(v); + return arc; + }; + arc.startAngle = function(v) { + if (!arguments.length) return startAngle; + startAngle = d3_functor(v); + return arc; + }; + arc.endAngle = function(v) { + if (!arguments.length) return endAngle; + endAngle = d3_functor(v); + return arc; + }; + arc.padAngle = function(v) { + if (!arguments.length) return padAngle; + padAngle = d3_functor(v); + return arc; + }; + arc.centroid = function() { + var r = (+innerRadius.apply(this, arguments) + +outerRadius.apply(this, arguments)) / 2, a = (+startAngle.apply(this, arguments) + +endAngle.apply(this, arguments)) / 2 - halfπ; + return [ Math.cos(a) * r, Math.sin(a) * r ]; + }; + return arc; + }; + var d3_svg_arcAuto = "auto"; + function d3_svg_arcInnerRadius(d) { + return d.innerRadius; + } + function d3_svg_arcOuterRadius(d) { + return d.outerRadius; + } + function d3_svg_arcStartAngle(d) { + return d.startAngle; + } + function d3_svg_arcEndAngle(d) { + return d.endAngle; + } + function d3_svg_arcPadAngle(d) { + return d && d.padAngle; + } + function d3_svg_arcSweep(x0, y0, x1, y1) { + return (x0 - x1) * y0 - (y0 - y1) * x0 > 0 ? 0 : 1; + } + function d3_svg_arcCornerTangents(p0, p1, r1, rc, cw) { + var x01 = p0[0] - p1[0], y01 = p0[1] - p1[1], lo = (cw ? rc : -rc) / Math.sqrt(x01 * x01 + y01 * y01), ox = lo * y01, oy = -lo * x01, x1 = p0[0] + ox, y1 = p0[1] + oy, x2 = p1[0] + ox, y2 = p1[1] + oy, x3 = (x1 + x2) / 2, y3 = (y1 + y2) / 2, dx = x2 - x1, dy = y2 - y1, d2 = dx * dx + dy * dy, r = r1 - rc, D = x1 * y2 - x2 * y1, d = (dy < 0 ? -1 : 1) * Math.sqrt(Math.max(0, r * r * d2 - D * D)), cx0 = (D * dy - dx * d) / d2, cy0 = (-D * dx - dy * d) / d2, cx1 = (D * dy + dx * d) / d2, cy1 = (-D * dx + dy * d) / d2, dx0 = cx0 - x3, dy0 = cy0 - y3, dx1 = cx1 - x3, dy1 = cy1 - y3; + if (dx0 * dx0 + dy0 * dy0 > dx1 * dx1 + dy1 * dy1) cx0 = cx1, cy0 = cy1; + return [ [ cx0 - ox, cy0 - oy ], [ cx0 * r1 / r, cy0 * r1 / r ] ]; + } + function d3_svg_line(projection) { + var x = d3_geom_pointX, y = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, tension = .7; + function line(data) { + var segments = [], points = [], i = -1, n = data.length, d, fx = d3_functor(x), fy = d3_functor(y); + function segment() { + segments.push("M", interpolate(projection(points), tension)); + } + while (++i < n) { + if (defined.call(this, d = data[i], i)) { + points.push([ +fx.call(this, d, i), +fy.call(this, d, i) ]); + } else if (points.length) { + segment(); + points = []; + } + } + if (points.length) segment(); + return segments.length ? segments.join("") : null; + } + line.x = function(_) { + if (!arguments.length) return x; + x = _; + return line; + }; + line.y = function(_) { + if (!arguments.length) return y; + y = _; + return line; + }; + line.defined = function(_) { + if (!arguments.length) return defined; + defined = _; + return line; + }; + line.interpolate = function(_) { + if (!arguments.length) return interpolateKey; + if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; + return line; + }; + line.tension = function(_) { + if (!arguments.length) return tension; + tension = _; + return line; + }; + return line; + } + d3.svg.line = function() { + return d3_svg_line(d3_identity); + }; + var d3_svg_lineInterpolators = d3.map({ + linear: d3_svg_lineLinear, + "linear-closed": d3_svg_lineLinearClosed, + step: d3_svg_lineStep, + "step-before": d3_svg_lineStepBefore, + "step-after": d3_svg_lineStepAfter, + basis: d3_svg_lineBasis, + "basis-open": d3_svg_lineBasisOpen, + "basis-closed": d3_svg_lineBasisClosed, + bundle: d3_svg_lineBundle, + cardinal: d3_svg_lineCardinal, + "cardinal-open": d3_svg_lineCardinalOpen, + "cardinal-closed": d3_svg_lineCardinalClosed, + monotone: d3_svg_lineMonotone + }); + d3_svg_lineInterpolators.forEach(function(key, value) { + value.key = key; + value.closed = /-closed$/.test(key); + }); + function d3_svg_lineLinear(points) { + return points.length > 1 ? points.join("L") : points + "Z"; + } + function d3_svg_lineLinearClosed(points) { + return points.join("L") + "Z"; + } + function d3_svg_lineStep(points) { + var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; + while (++i < n) path.push("H", (p[0] + (p = points[i])[0]) / 2, "V", p[1]); + if (n > 1) path.push("H", p[0]); + return path.join(""); + } + function d3_svg_lineStepBefore(points) { + var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; + while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]); + return path.join(""); + } + function d3_svg_lineStepAfter(points) { + var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; + while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]); + return path.join(""); + } + function d3_svg_lineCardinalOpen(points, tension) { + return points.length < 4 ? d3_svg_lineLinear(points) : points[1] + d3_svg_lineHermite(points.slice(1, -1), d3_svg_lineCardinalTangents(points, tension)); + } + function d3_svg_lineCardinalClosed(points, tension) { + return points.length < 3 ? d3_svg_lineLinearClosed(points) : points[0] + d3_svg_lineHermite((points.push(points[0]), + points), d3_svg_lineCardinalTangents([ points[points.length - 2] ].concat(points, [ points[1] ]), tension)); + } + function d3_svg_lineCardinal(points, tension) { + return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineCardinalTangents(points, tension)); + } + function d3_svg_lineHermite(points, tangents) { + if (tangents.length < 1 || points.length != tangents.length && points.length != tangents.length + 2) { + return d3_svg_lineLinear(points); + } + var quad = points.length != tangents.length, path = "", p0 = points[0], p = points[1], t0 = tangents[0], t = t0, pi = 1; + if (quad) { + path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3) + "," + p[0] + "," + p[1]; + p0 = points[1]; + pi = 2; + } + if (tangents.length > 1) { + t = tangents[1]; + p = points[pi]; + pi++; + path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1]) + "," + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; + for (var i = 2; i < tangents.length; i++, pi++) { + p = points[pi]; + t = tangents[i]; + path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; + } + } + if (quad) { + var lp = points[pi]; + path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3) + "," + lp[0] + "," + lp[1]; + } + return path; + } + function d3_svg_lineCardinalTangents(points, tension) { + var tangents = [], a = (1 - tension) / 2, p0, p1 = points[0], p2 = points[1], i = 1, n = points.length; + while (++i < n) { + p0 = p1; + p1 = p2; + p2 = points[i]; + tangents.push([ a * (p2[0] - p0[0]), a * (p2[1] - p0[1]) ]); + } + return tangents; + } + function d3_svg_lineBasis(points) { + if (points.length < 3) return d3_svg_lineLinear(points); + var i = 1, n = points.length, pi = points[0], x0 = pi[0], y0 = pi[1], px = [ x0, x0, x0, (pi = points[1])[0] ], py = [ y0, y0, y0, pi[1] ], path = [ x0, ",", y0, "L", d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; + points.push(points[n - 1]); + while (++i <= n) { + pi = points[i]; + px.shift(); + px.push(pi[0]); + py.shift(); + py.push(pi[1]); + d3_svg_lineBasisBezier(path, px, py); + } + points.pop(); + path.push("L", pi); + return path.join(""); + } + function d3_svg_lineBasisOpen(points) { + if (points.length < 4) return d3_svg_lineLinear(points); + var path = [], i = -1, n = points.length, pi, px = [ 0 ], py = [ 0 ]; + while (++i < 3) { + pi = points[i]; + px.push(pi[0]); + py.push(pi[1]); + } + path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px) + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)); + --i; + while (++i < n) { + pi = points[i]; + px.shift(); + px.push(pi[0]); + py.shift(); + py.push(pi[1]); + d3_svg_lineBasisBezier(path, px, py); + } + return path.join(""); + } + function d3_svg_lineBasisClosed(points) { + var path, i = -1, n = points.length, m = n + 4, pi, px = [], py = []; + while (++i < 4) { + pi = points[i % n]; + px.push(pi[0]); + py.push(pi[1]); + } + path = [ d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; + --i; + while (++i < m) { + pi = points[i % n]; + px.shift(); + px.push(pi[0]); + py.shift(); + py.push(pi[1]); + d3_svg_lineBasisBezier(path, px, py); + } + return path.join(""); + } + function d3_svg_lineBundle(points, tension) { + var n = points.length - 1; + if (n) { + var x0 = points[0][0], y0 = points[0][1], dx = points[n][0] - x0, dy = points[n][1] - y0, i = -1, p, t; + while (++i <= n) { + p = points[i]; + t = i / n; + p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx); + p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy); + } + } + return d3_svg_lineBasis(points); + } + function d3_svg_lineDot4(a, b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; + } + var d3_svg_lineBasisBezier1 = [ 0, 2 / 3, 1 / 3, 0 ], d3_svg_lineBasisBezier2 = [ 0, 1 / 3, 2 / 3, 0 ], d3_svg_lineBasisBezier3 = [ 0, 1 / 6, 2 / 3, 1 / 6 ]; + function d3_svg_lineBasisBezier(path, x, y) { + path.push("C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y)); + } + function d3_svg_lineSlope(p0, p1) { + return (p1[1] - p0[1]) / (p1[0] - p0[0]); + } + function d3_svg_lineFiniteDifferences(points) { + var i = 0, j = points.length - 1, m = [], p0 = points[0], p1 = points[1], d = m[0] = d3_svg_lineSlope(p0, p1); + while (++i < j) { + m[i] = (d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]))) / 2; + } + m[i] = d; + return m; + } + function d3_svg_lineMonotoneTangents(points) { + var tangents = [], d, a, b, s, m = d3_svg_lineFiniteDifferences(points), i = -1, j = points.length - 1; + while (++i < j) { + d = d3_svg_lineSlope(points[i], points[i + 1]); + if (abs(d) < ε) { + m[i] = m[i + 1] = 0; + } else { + a = m[i] / d; + b = m[i + 1] / d; + s = a * a + b * b; + if (s > 9) { + s = d * 3 / Math.sqrt(s); + m[i] = s * a; + m[i + 1] = s * b; + } + } + } + i = -1; + while (++i <= j) { + s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0]) / (6 * (1 + m[i] * m[i])); + tangents.push([ s || 0, m[i] * s || 0 ]); + } + return tangents; + } + function d3_svg_lineMonotone(points) { + return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points)); + } + d3.svg.line.radial = function() { + var line = d3_svg_line(d3_svg_lineRadial); + line.radius = line.x, delete line.x; + line.angle = line.y, delete line.y; + return line; + }; + function d3_svg_lineRadial(points) { + var point, i = -1, n = points.length, r, a; + while (++i < n) { + point = points[i]; + r = point[0]; + a = point[1] - halfπ; + point[0] = r * Math.cos(a); + point[1] = r * Math.sin(a); + } + return points; + } + function d3_svg_area(projection) { + var x0 = d3_geom_pointX, x1 = d3_geom_pointX, y0 = 0, y1 = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, interpolateReverse = interpolate, L = "L", tension = .7; + function area(data) { + var segments = [], points0 = [], points1 = [], i = -1, n = data.length, d, fx0 = d3_functor(x0), fy0 = d3_functor(y0), fx1 = x0 === x1 ? function() { + return x; + } : d3_functor(x1), fy1 = y0 === y1 ? function() { + return y; + } : d3_functor(y1), x, y; + function segment() { + segments.push("M", interpolate(projection(points1), tension), L, interpolateReverse(projection(points0.reverse()), tension), "Z"); + } + while (++i < n) { + if (defined.call(this, d = data[i], i)) { + points0.push([ x = +fx0.call(this, d, i), y = +fy0.call(this, d, i) ]); + points1.push([ +fx1.call(this, d, i), +fy1.call(this, d, i) ]); + } else if (points0.length) { + segment(); + points0 = []; + points1 = []; + } + } + if (points0.length) segment(); + return segments.length ? segments.join("") : null; + } + area.x = function(_) { + if (!arguments.length) return x1; + x0 = x1 = _; + return area; + }; + area.x0 = function(_) { + if (!arguments.length) return x0; + x0 = _; + return area; + }; + area.x1 = function(_) { + if (!arguments.length) return x1; + x1 = _; + return area; + }; + area.y = function(_) { + if (!arguments.length) return y1; + y0 = y1 = _; + return area; + }; + area.y0 = function(_) { + if (!arguments.length) return y0; + y0 = _; + return area; + }; + area.y1 = function(_) { + if (!arguments.length) return y1; + y1 = _; + return area; + }; + area.defined = function(_) { + if (!arguments.length) return defined; + defined = _; + return area; + }; + area.interpolate = function(_) { + if (!arguments.length) return interpolateKey; + if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; + interpolateReverse = interpolate.reverse || interpolate; + L = interpolate.closed ? "M" : "L"; + return area; + }; + area.tension = function(_) { + if (!arguments.length) return tension; + tension = _; + return area; + }; + return area; + } + d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter; + d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore; + d3.svg.area = function() { + return d3_svg_area(d3_identity); + }; + d3.svg.area.radial = function() { + var area = d3_svg_area(d3_svg_lineRadial); + area.radius = area.x, delete area.x; + area.innerRadius = area.x0, delete area.x0; + area.outerRadius = area.x1, delete area.x1; + area.angle = area.y, delete area.y; + area.startAngle = area.y0, delete area.y0; + area.endAngle = area.y1, delete area.y1; + return area; + }; + d3.svg.chord = function() { + var source = d3_source, target = d3_target, radius = d3_svg_chordRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle; + function chord(d, i) { + var s = subgroup(this, source, d, i), t = subgroup(this, target, d, i); + return "M" + s.p0 + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t) ? curve(s.r, s.p1, s.r, s.p0) : curve(s.r, s.p1, t.r, t.p0) + arc(t.r, t.p1, t.a1 - t.a0) + curve(t.r, t.p1, s.r, s.p0)) + "Z"; + } + function subgroup(self, f, d, i) { + var subgroup = f.call(self, d, i), r = radius.call(self, subgroup, i), a0 = startAngle.call(self, subgroup, i) - halfπ, a1 = endAngle.call(self, subgroup, i) - halfπ; + return { + r: r, + a0: a0, + a1: a1, + p0: [ r * Math.cos(a0), r * Math.sin(a0) ], + p1: [ r * Math.cos(a1), r * Math.sin(a1) ] + }; + } + function equals(a, b) { + return a.a0 == b.a0 && a.a1 == b.a1; + } + function arc(r, p, a) { + return "A" + r + "," + r + " 0 " + +(a > π) + ",1 " + p; + } + function curve(r0, p0, r1, p1) { + return "Q 0,0 " + p1; + } + chord.radius = function(v) { + if (!arguments.length) return radius; + radius = d3_functor(v); + return chord; + }; + chord.source = function(v) { + if (!arguments.length) return source; + source = d3_functor(v); + return chord; + }; + chord.target = function(v) { + if (!arguments.length) return target; + target = d3_functor(v); + return chord; + }; + chord.startAngle = function(v) { + if (!arguments.length) return startAngle; + startAngle = d3_functor(v); + return chord; + }; + chord.endAngle = function(v) { + if (!arguments.length) return endAngle; + endAngle = d3_functor(v); + return chord; + }; + return chord; + }; + function d3_svg_chordRadius(d) { + return d.radius; + } + d3.svg.diagonal = function() { + var source = d3_source, target = d3_target, projection = d3_svg_diagonalProjection; + function diagonal(d, i) { + var p0 = source.call(this, d, i), p3 = target.call(this, d, i), m = (p0.y + p3.y) / 2, p = [ p0, { + x: p0.x, + y: m + }, { + x: p3.x, + y: m + }, p3 ]; + p = p.map(projection); + return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3]; + } + diagonal.source = function(x) { + if (!arguments.length) return source; + source = d3_functor(x); + return diagonal; + }; + diagonal.target = function(x) { + if (!arguments.length) return target; + target = d3_functor(x); + return diagonal; + }; + diagonal.projection = function(x) { + if (!arguments.length) return projection; + projection = x; + return diagonal; + }; + return diagonal; + }; + function d3_svg_diagonalProjection(d) { + return [ d.x, d.y ]; + } + d3.svg.diagonal.radial = function() { + var diagonal = d3.svg.diagonal(), projection = d3_svg_diagonalProjection, projection_ = diagonal.projection; + diagonal.projection = function(x) { + return arguments.length ? projection_(d3_svg_diagonalRadialProjection(projection = x)) : projection; + }; + return diagonal; + }; + function d3_svg_diagonalRadialProjection(projection) { + return function() { + var d = projection.apply(this, arguments), r = d[0], a = d[1] - halfπ; + return [ r * Math.cos(a), r * Math.sin(a) ]; + }; + } + d3.svg.symbol = function() { + var type = d3_svg_symbolType, size = d3_svg_symbolSize; + function symbol(d, i) { + return (d3_svg_symbols.get(type.call(this, d, i)) || d3_svg_symbolCircle)(size.call(this, d, i)); + } + symbol.type = function(x) { + if (!arguments.length) return type; + type = d3_functor(x); + return symbol; + }; + symbol.size = function(x) { + if (!arguments.length) return size; + size = d3_functor(x); + return symbol; + }; + return symbol; + }; + function d3_svg_symbolSize() { + return 64; + } + function d3_svg_symbolType() { + return "circle"; + } + function d3_svg_symbolCircle(size) { + var r = Math.sqrt(size / π); + return "M0," + r + "A" + r + "," + r + " 0 1,1 0," + -r + "A" + r + "," + r + " 0 1,1 0," + r + "Z"; + } + var d3_svg_symbols = d3.map({ + circle: d3_svg_symbolCircle, + cross: function(size) { + var r = Math.sqrt(size / 5) / 2; + return "M" + -3 * r + "," + -r + "H" + -r + "V" + -3 * r + "H" + r + "V" + -r + "H" + 3 * r + "V" + r + "H" + r + "V" + 3 * r + "H" + -r + "V" + r + "H" + -3 * r + "Z"; + }, + diamond: function(size) { + var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)), rx = ry * d3_svg_symbolTan30; + return "M0," + -ry + "L" + rx + ",0" + " 0," + ry + " " + -rx + ",0" + "Z"; + }, + square: function(size) { + var r = Math.sqrt(size) / 2; + return "M" + -r + "," + -r + "L" + r + "," + -r + " " + r + "," + r + " " + -r + "," + r + "Z"; + }, + "triangle-down": function(size) { + var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; + return "M0," + ry + "L" + rx + "," + -ry + " " + -rx + "," + -ry + "Z"; + }, + "triangle-up": function(size) { + var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; + return "M0," + -ry + "L" + rx + "," + ry + " " + -rx + "," + ry + "Z"; + } + }); + d3.svg.symbolTypes = d3_svg_symbols.keys(); + var d3_svg_symbolSqrt3 = Math.sqrt(3), d3_svg_symbolTan30 = Math.tan(30 * d3_radians); + d3_selectionPrototype.transition = function(name) { + var id = d3_transitionInheritId || ++d3_transitionId, ns = d3_transitionNamespace(name), subgroups = [], subgroup, node, transition = d3_transitionInherit || { + time: Date.now(), + ease: d3_ease_cubicInOut, + delay: 0, + duration: 250 + }; + for (var j = -1, m = this.length; ++j < m; ) { + subgroups.push(subgroup = []); + for (var group = this[j], i = -1, n = group.length; ++i < n; ) { + if (node = group[i]) d3_transitionNode(node, i, ns, id, transition); + subgroup.push(node); + } + } + return d3_transition(subgroups, ns, id); + }; + d3_selectionPrototype.interrupt = function(name) { + return this.each(name == null ? d3_selection_interrupt : d3_selection_interruptNS(d3_transitionNamespace(name))); + }; + var d3_selection_interrupt = d3_selection_interruptNS(d3_transitionNamespace()); + function d3_selection_interruptNS(ns) { + return function() { + var lock, activeId, active; + if ((lock = this[ns]) && (active = lock[activeId = lock.active])) { + active.timer.c = null; + active.timer.t = NaN; + if (--lock.count) delete lock[activeId]; else delete this[ns]; + lock.active += .5; + active.event && active.event.interrupt.call(this, this.__data__, active.index); + } + }; + } + function d3_transition(groups, ns, id) { + d3_subclass(groups, d3_transitionPrototype); + groups.namespace = ns; + groups.id = id; + return groups; + } + var d3_transitionPrototype = [], d3_transitionId = 0, d3_transitionInheritId, d3_transitionInherit; + d3_transitionPrototype.call = d3_selectionPrototype.call; + d3_transitionPrototype.empty = d3_selectionPrototype.empty; + d3_transitionPrototype.node = d3_selectionPrototype.node; + d3_transitionPrototype.size = d3_selectionPrototype.size; + d3.transition = function(selection, name) { + return selection && selection.transition ? d3_transitionInheritId ? selection.transition(name) : selection : d3.selection().transition(selection); + }; + d3.transition.prototype = d3_transitionPrototype; + d3_transitionPrototype.select = function(selector) { + var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnode, node; + selector = d3_selection_selector(selector); + for (var j = -1, m = this.length; ++j < m; ) { + subgroups.push(subgroup = []); + for (var group = this[j], i = -1, n = group.length; ++i < n; ) { + if ((node = group[i]) && (subnode = selector.call(node, node.__data__, i, j))) { + if ("__data__" in node) subnode.__data__ = node.__data__; + d3_transitionNode(subnode, i, ns, id, node[ns][id]); + subgroup.push(subnode); + } else { + subgroup.push(null); + } + } + } + return d3_transition(subgroups, ns, id); + }; + d3_transitionPrototype.selectAll = function(selector) { + var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnodes, node, subnode, transition; + selector = d3_selection_selectorAll(selector); + for (var j = -1, m = this.length; ++j < m; ) { + for (var group = this[j], i = -1, n = group.length; ++i < n; ) { + if (node = group[i]) { + transition = node[ns][id]; + subnodes = selector.call(node, node.__data__, i, j); + subgroups.push(subgroup = []); + for (var k = -1, o = subnodes.length; ++k < o; ) { + if (subnode = subnodes[k]) d3_transitionNode(subnode, k, ns, id, transition); + subgroup.push(subnode); + } + } + } + } + return d3_transition(subgroups, ns, id); + }; + d3_transitionPrototype.filter = function(filter) { + var subgroups = [], subgroup, group, node; + if (typeof filter !== "function") filter = d3_selection_filter(filter); + for (var j = 0, m = this.length; j < m; j++) { + subgroups.push(subgroup = []); + for (var group = this[j], i = 0, n = group.length; i < n; i++) { + if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { + subgroup.push(node); + } + } + } + return d3_transition(subgroups, this.namespace, this.id); + }; + d3_transitionPrototype.tween = function(name, tween) { + var id = this.id, ns = this.namespace; + if (arguments.length < 2) return this.node()[ns][id].tween.get(name); + return d3_selection_each(this, tween == null ? function(node) { + node[ns][id].tween.remove(name); + } : function(node) { + node[ns][id].tween.set(name, tween); + }); + }; + function d3_transition_tween(groups, name, value, tween) { + var id = groups.id, ns = groups.namespace; + return d3_selection_each(groups, typeof value === "function" ? function(node, i, j) { + node[ns][id].tween.set(name, tween(value.call(node, node.__data__, i, j))); + } : (value = tween(value), function(node) { + node[ns][id].tween.set(name, value); + })); + } + d3_transitionPrototype.attr = function(nameNS, value) { + if (arguments.length < 2) { + for (value in nameNS) this.attr(value, nameNS[value]); + return this; + } + var interpolate = nameNS == "transform" ? d3_interpolateTransform : d3_interpolate, name = d3.ns.qualify(nameNS); + function attrNull() { + this.removeAttribute(name); + } + function attrNullNS() { + this.removeAttributeNS(name.space, name.local); + } + function attrTween(b) { + return b == null ? attrNull : (b += "", function() { + var a = this.getAttribute(name), i; + return a !== b && (i = interpolate(a, b), function(t) { + this.setAttribute(name, i(t)); + }); + }); + } + function attrTweenNS(b) { + return b == null ? attrNullNS : (b += "", function() { + var a = this.getAttributeNS(name.space, name.local), i; + return a !== b && (i = interpolate(a, b), function(t) { + this.setAttributeNS(name.space, name.local, i(t)); + }); + }); + } + return d3_transition_tween(this, "attr." + nameNS, value, name.local ? attrTweenNS : attrTween); + }; + d3_transitionPrototype.attrTween = function(nameNS, tween) { + var name = d3.ns.qualify(nameNS); + function attrTween(d, i) { + var f = tween.call(this, d, i, this.getAttribute(name)); + return f && function(t) { + this.setAttribute(name, f(t)); + }; + } + function attrTweenNS(d, i) { + var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local)); + return f && function(t) { + this.setAttributeNS(name.space, name.local, f(t)); + }; + } + return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween); + }; + d3_transitionPrototype.style = function(name, value, priority) { + var n = arguments.length; + if (n < 3) { + if (typeof name !== "string") { + if (n < 2) value = ""; + for (priority in name) this.style(priority, name[priority], value); + return this; + } + priority = ""; + } + function styleNull() { + this.style.removeProperty(name); + } + function styleString(b) { + return b == null ? styleNull : (b += "", function() { + var a = d3_window(this).getComputedStyle(this, null).getPropertyValue(name), i; + return a !== b && (i = d3_interpolate(a, b), function(t) { + this.style.setProperty(name, i(t), priority); + }); + }); + } + return d3_transition_tween(this, "style." + name, value, styleString); + }; + d3_transitionPrototype.styleTween = function(name, tween, priority) { + if (arguments.length < 3) priority = ""; + function styleTween(d, i) { + var f = tween.call(this, d, i, d3_window(this).getComputedStyle(this, null).getPropertyValue(name)); + return f && function(t) { + this.style.setProperty(name, f(t), priority); + }; + } + return this.tween("style." + name, styleTween); + }; + d3_transitionPrototype.text = function(value) { + return d3_transition_tween(this, "text", value, d3_transition_text); + }; + function d3_transition_text(b) { + if (b == null) b = ""; + return function() { + this.textContent = b; + }; + } + d3_transitionPrototype.remove = function() { + var ns = this.namespace; + return this.each("end.transition", function() { + var p; + if (this[ns].count < 2 && (p = this.parentNode)) p.removeChild(this); + }); + }; + d3_transitionPrototype.ease = function(value) { + var id = this.id, ns = this.namespace; + if (arguments.length < 1) return this.node()[ns][id].ease; + if (typeof value !== "function") value = d3.ease.apply(d3, arguments); + return d3_selection_each(this, function(node) { + node[ns][id].ease = value; + }); + }; + d3_transitionPrototype.delay = function(value) { + var id = this.id, ns = this.namespace; + if (arguments.length < 1) return this.node()[ns][id].delay; + return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { + node[ns][id].delay = +value.call(node, node.__data__, i, j); + } : (value = +value, function(node) { + node[ns][id].delay = value; + })); + }; + d3_transitionPrototype.duration = function(value) { + var id = this.id, ns = this.namespace; + if (arguments.length < 1) return this.node()[ns][id].duration; + return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { + node[ns][id].duration = Math.max(1, value.call(node, node.__data__, i, j)); + } : (value = Math.max(1, value), function(node) { + node[ns][id].duration = value; + })); + }; + d3_transitionPrototype.each = function(type, listener) { + var id = this.id, ns = this.namespace; + if (arguments.length < 2) { + var inherit = d3_transitionInherit, inheritId = d3_transitionInheritId; + try { + d3_transitionInheritId = id; + d3_selection_each(this, function(node, i, j) { + d3_transitionInherit = node[ns][id]; + type.call(node, node.__data__, i, j); + }); + } finally { + d3_transitionInherit = inherit; + d3_transitionInheritId = inheritId; + } + } else { + d3_selection_each(this, function(node) { + var transition = node[ns][id]; + (transition.event || (transition.event = d3.dispatch("start", "end", "interrupt"))).on(type, listener); + }); + } + return this; + }; + d3_transitionPrototype.transition = function() { + var id0 = this.id, id1 = ++d3_transitionId, ns = this.namespace, subgroups = [], subgroup, group, node, transition; + for (var j = 0, m = this.length; j < m; j++) { + subgroups.push(subgroup = []); + for (var group = this[j], i = 0, n = group.length; i < n; i++) { + if (node = group[i]) { + transition = node[ns][id0]; + d3_transitionNode(node, i, ns, id1, { + time: transition.time, + ease: transition.ease, + delay: transition.delay + transition.duration, + duration: transition.duration + }); + } + subgroup.push(node); + } + } + return d3_transition(subgroups, ns, id1); + }; + function d3_transitionNamespace(name) { + return name == null ? "__transition__" : "__transition_" + name + "__"; + } + function d3_transitionNode(node, i, ns, id, inherit) { + var lock = node[ns] || (node[ns] = { + active: 0, + count: 0 + }), transition = lock[id], time, timer, duration, ease, tweens; + function schedule(elapsed) { + var delay = transition.delay; + timer.t = delay + time; + if (delay <= elapsed) return start(elapsed - delay); + timer.c = start; + } + function start(elapsed) { + var activeId = lock.active, active = lock[activeId]; + if (active) { + active.timer.c = null; + active.timer.t = NaN; + --lock.count; + delete lock[activeId]; + active.event && active.event.interrupt.call(node, node.__data__, active.index); + } + for (var cancelId in lock) { + if (+cancelId < id) { + var cancel = lock[cancelId]; + cancel.timer.c = null; + cancel.timer.t = NaN; + --lock.count; + delete lock[cancelId]; + } + } + timer.c = tick; + d3_timer(function() { + if (timer.c && tick(elapsed || 1)) { + timer.c = null; + timer.t = NaN; + } + return 1; + }, 0, time); + lock.active = id; + transition.event && transition.event.start.call(node, node.__data__, i); + tweens = []; + transition.tween.forEach(function(key, value) { + if (value = value.call(node, node.__data__, i)) { + tweens.push(value); + } + }); + ease = transition.ease; + duration = transition.duration; + } + function tick(elapsed) { + var t = elapsed / duration, e = ease(t), n = tweens.length; + while (n > 0) { + tweens[--n].call(node, e); + } + if (t >= 1) { + transition.event && transition.event.end.call(node, node.__data__, i); + if (--lock.count) delete lock[id]; else delete node[ns]; + return 1; + } + } + if (!transition) { + time = inherit.time; + timer = d3_timer(schedule, 0, time); + transition = lock[id] = { + tween: new d3_Map(), + time: time, + timer: timer, + delay: inherit.delay, + duration: inherit.duration, + ease: inherit.ease, + index: i + }; + inherit = null; + ++lock.count; + } + } + d3.svg.axis = function() { + var scale = d3.scale.linear(), orient = d3_svg_axisDefaultOrient, innerTickSize = 6, outerTickSize = 6, tickPadding = 3, tickArguments_ = [ 10 ], tickValues = null, tickFormat_; + function axis(g) { + g.each(function() { + var g = d3.select(this); + var scale0 = this.__chart__ || scale, scale1 = this.__chart__ = scale.copy(); + var ticks = tickValues == null ? scale1.ticks ? scale1.ticks.apply(scale1, tickArguments_) : scale1.domain() : tickValues, tickFormat = tickFormat_ == null ? scale1.tickFormat ? scale1.tickFormat.apply(scale1, tickArguments_) : d3_identity : tickFormat_, tick = g.selectAll(".tick").data(ticks, scale1), tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", ε), tickExit = d3.transition(tick.exit()).style("opacity", ε).remove(), tickUpdate = d3.transition(tick.order()).style("opacity", 1), tickSpacing = Math.max(innerTickSize, 0) + tickPadding, tickTransform; + var range = d3_scaleRange(scale1), path = g.selectAll(".domain").data([ 0 ]), pathUpdate = (path.enter().append("path").attr("class", "domain"), + d3.transition(path)); + tickEnter.append("line"); + tickEnter.append("text"); + var lineEnter = tickEnter.select("line"), lineUpdate = tickUpdate.select("line"), text = tick.select("text").text(tickFormat), textEnter = tickEnter.select("text"), textUpdate = tickUpdate.select("text"), sign = orient === "top" || orient === "left" ? -1 : 1, x1, x2, y1, y2; + if (orient === "bottom" || orient === "top") { + tickTransform = d3_svg_axisX, x1 = "x", y1 = "y", x2 = "x2", y2 = "y2"; + text.attr("dy", sign < 0 ? "0em" : ".71em").style("text-anchor", "middle"); + pathUpdate.attr("d", "M" + range[0] + "," + sign * outerTickSize + "V0H" + range[1] + "V" + sign * outerTickSize); + } else { + tickTransform = d3_svg_axisY, x1 = "y", y1 = "x", x2 = "y2", y2 = "x2"; + text.attr("dy", ".32em").style("text-anchor", sign < 0 ? "end" : "start"); + pathUpdate.attr("d", "M" + sign * outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + sign * outerTickSize); + } + lineEnter.attr(y2, sign * innerTickSize); + textEnter.attr(y1, sign * tickSpacing); + lineUpdate.attr(x2, 0).attr(y2, sign * innerTickSize); + textUpdate.attr(x1, 0).attr(y1, sign * tickSpacing); + if (scale1.rangeBand) { + var x = scale1, dx = x.rangeBand() / 2; + scale0 = scale1 = function(d) { + return x(d) + dx; + }; + } else if (scale0.rangeBand) { + scale0 = scale1; + } else { + tickExit.call(tickTransform, scale1, scale0); + } + tickEnter.call(tickTransform, scale0, scale1); + tickUpdate.call(tickTransform, scale1, scale1); + }); + } + axis.scale = function(x) { + if (!arguments.length) return scale; + scale = x; + return axis; + }; + axis.orient = function(x) { + if (!arguments.length) return orient; + orient = x in d3_svg_axisOrients ? x + "" : d3_svg_axisDefaultOrient; + return axis; + }; + axis.ticks = function() { + if (!arguments.length) return tickArguments_; + tickArguments_ = d3_array(arguments); + return axis; + }; + axis.tickValues = function(x) { + if (!arguments.length) return tickValues; + tickValues = x; + return axis; + }; + axis.tickFormat = function(x) { + if (!arguments.length) return tickFormat_; + tickFormat_ = x; + return axis; + }; + axis.tickSize = function(x) { + var n = arguments.length; + if (!n) return innerTickSize; + innerTickSize = +x; + outerTickSize = +arguments[n - 1]; + return axis; + }; + axis.innerTickSize = function(x) { + if (!arguments.length) return innerTickSize; + innerTickSize = +x; + return axis; + }; + axis.outerTickSize = function(x) { + if (!arguments.length) return outerTickSize; + outerTickSize = +x; + return axis; + }; + axis.tickPadding = function(x) { + if (!arguments.length) return tickPadding; + tickPadding = +x; + return axis; + }; + axis.tickSubdivide = function() { + return arguments.length && axis; + }; + return axis; + }; + var d3_svg_axisDefaultOrient = "bottom", d3_svg_axisOrients = { + top: 1, + right: 1, + bottom: 1, + left: 1 + }; + function d3_svg_axisX(selection, x0, x1) { + selection.attr("transform", function(d) { + var v0 = x0(d); + return "translate(" + (isFinite(v0) ? v0 : x1(d)) + ",0)"; + }); + } + function d3_svg_axisY(selection, y0, y1) { + selection.attr("transform", function(d) { + var v0 = y0(d); + return "translate(0," + (isFinite(v0) ? v0 : y1(d)) + ")"; + }); + } + d3.svg.brush = function() { + var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, xExtent = [ 0, 0 ], yExtent = [ 0, 0 ], xExtentDomain, yExtentDomain, xClamp = true, yClamp = true, resizes = d3_svg_brushResizes[0]; + function brush(g) { + g.each(function() { + var g = d3.select(this).style("pointer-events", "all").style("-webkit-tap-highlight-color", "rgba(0,0,0,0)").on("mousedown.brush", brushstart).on("touchstart.brush", brushstart); + var background = g.selectAll(".background").data([ 0 ]); + background.enter().append("rect").attr("class", "background").style("visibility", "hidden").style("cursor", "crosshair"); + g.selectAll(".extent").data([ 0 ]).enter().append("rect").attr("class", "extent").style("cursor", "move"); + var resize = g.selectAll(".resize").data(resizes, d3_identity); + resize.exit().remove(); + resize.enter().append("g").attr("class", function(d) { + return "resize " + d; + }).style("cursor", function(d) { + return d3_svg_brushCursor[d]; + }).append("rect").attr("x", function(d) { + return /[ew]$/.test(d) ? -3 : null; + }).attr("y", function(d) { + return /^[ns]/.test(d) ? -3 : null; + }).attr("width", 6).attr("height", 6).style("visibility", "hidden"); + resize.style("display", brush.empty() ? "none" : null); + var gUpdate = d3.transition(g), backgroundUpdate = d3.transition(background), range; + if (x) { + range = d3_scaleRange(x); + backgroundUpdate.attr("x", range[0]).attr("width", range[1] - range[0]); + redrawX(gUpdate); + } + if (y) { + range = d3_scaleRange(y); + backgroundUpdate.attr("y", range[0]).attr("height", range[1] - range[0]); + redrawY(gUpdate); + } + redraw(gUpdate); + }); + } + brush.event = function(g) { + g.each(function() { + var event_ = event.of(this, arguments), extent1 = { + x: xExtent, + y: yExtent, + i: xExtentDomain, + j: yExtentDomain + }, extent0 = this.__chart__ || extent1; + this.__chart__ = extent1; + if (d3_transitionInheritId) { + d3.select(this).transition().each("start.brush", function() { + xExtentDomain = extent0.i; + yExtentDomain = extent0.j; + xExtent = extent0.x; + yExtent = extent0.y; + event_({ + type: "brushstart" + }); + }).tween("brush:brush", function() { + var xi = d3_interpolateArray(xExtent, extent1.x), yi = d3_interpolateArray(yExtent, extent1.y); + xExtentDomain = yExtentDomain = null; + return function(t) { + xExtent = extent1.x = xi(t); + yExtent = extent1.y = yi(t); + event_({ + type: "brush", + mode: "resize" + }); + }; + }).each("end.brush", function() { + xExtentDomain = extent1.i; + yExtentDomain = extent1.j; + event_({ + type: "brush", + mode: "resize" + }); + event_({ + type: "brushend" + }); + }); + } else { + event_({ + type: "brushstart" + }); + event_({ + type: "brush", + mode: "resize" + }); + event_({ + type: "brushend" + }); + } + }); + }; + function redraw(g) { + g.selectAll(".resize").attr("transform", function(d) { + return "translate(" + xExtent[+/e$/.test(d)] + "," + yExtent[+/^s/.test(d)] + ")"; + }); + } + function redrawX(g) { + g.select(".extent").attr("x", xExtent[0]); + g.selectAll(".extent,.n>rect,.s>rect").attr("width", xExtent[1] - xExtent[0]); + } + function redrawY(g) { + g.select(".extent").attr("y", yExtent[0]); + g.selectAll(".extent,.e>rect,.w>rect").attr("height", yExtent[1] - yExtent[0]); + } + function brushstart() { + var target = this, eventTarget = d3.select(d3.event.target), event_ = event.of(target, arguments), g = d3.select(target), resizing = eventTarget.datum(), resizingX = !/^(n|s)$/.test(resizing) && x, resizingY = !/^(e|w)$/.test(resizing) && y, dragging = eventTarget.classed("extent"), dragRestore = d3_event_dragSuppress(target), center, origin = d3.mouse(target), offset; + var w = d3.select(d3_window(target)).on("keydown.brush", keydown).on("keyup.brush", keyup); + if (d3.event.changedTouches) { + w.on("touchmove.brush", brushmove).on("touchend.brush", brushend); + } else { + w.on("mousemove.brush", brushmove).on("mouseup.brush", brushend); + } + g.interrupt().selectAll("*").interrupt(); + if (dragging) { + origin[0] = xExtent[0] - origin[0]; + origin[1] = yExtent[0] - origin[1]; + } else if (resizing) { + var ex = +/w$/.test(resizing), ey = +/^n/.test(resizing); + offset = [ xExtent[1 - ex] - origin[0], yExtent[1 - ey] - origin[1] ]; + origin[0] = xExtent[ex]; + origin[1] = yExtent[ey]; + } else if (d3.event.altKey) center = origin.slice(); + g.style("pointer-events", "none").selectAll(".resize").style("display", null); + d3.select("body").style("cursor", eventTarget.style("cursor")); + event_({ + type: "brushstart" + }); + brushmove(); + function keydown() { + if (d3.event.keyCode == 32) { + if (!dragging) { + center = null; + origin[0] -= xExtent[1]; + origin[1] -= yExtent[1]; + dragging = 2; + } + d3_eventPreventDefault(); + } + } + function keyup() { + if (d3.event.keyCode == 32 && dragging == 2) { + origin[0] += xExtent[1]; + origin[1] += yExtent[1]; + dragging = 0; + d3_eventPreventDefault(); + } + } + function brushmove() { + var point = d3.mouse(target), moved = false; + if (offset) { + point[0] += offset[0]; + point[1] += offset[1]; + } + if (!dragging) { + if (d3.event.altKey) { + if (!center) center = [ (xExtent[0] + xExtent[1]) / 2, (yExtent[0] + yExtent[1]) / 2 ]; + origin[0] = xExtent[+(point[0] < center[0])]; + origin[1] = yExtent[+(point[1] < center[1])]; + } else center = null; + } + if (resizingX && move1(point, x, 0)) { + redrawX(g); + moved = true; + } + if (resizingY && move1(point, y, 1)) { + redrawY(g); + moved = true; + } + if (moved) { + redraw(g); + event_({ + type: "brush", + mode: dragging ? "move" : "resize" + }); + } + } + function move1(point, scale, i) { + var range = d3_scaleRange(scale), r0 = range[0], r1 = range[1], position = origin[i], extent = i ? yExtent : xExtent, size = extent[1] - extent[0], min, max; + if (dragging) { + r0 -= position; + r1 -= size + position; + } + min = (i ? yClamp : xClamp) ? Math.max(r0, Math.min(r1, point[i])) : point[i]; + if (dragging) { + max = (min += position) + size; + } else { + if (center) position = Math.max(r0, Math.min(r1, 2 * center[i] - min)); + if (position < min) { + max = min; + min = position; + } else { + max = position; + } + } + if (extent[0] != min || extent[1] != max) { + if (i) yExtentDomain = null; else xExtentDomain = null; + extent[0] = min; + extent[1] = max; + return true; + } + } + function brushend() { + brushmove(); + g.style("pointer-events", "all").selectAll(".resize").style("display", brush.empty() ? "none" : null); + d3.select("body").style("cursor", null); + w.on("mousemove.brush", null).on("mouseup.brush", null).on("touchmove.brush", null).on("touchend.brush", null).on("keydown.brush", null).on("keyup.brush", null); + dragRestore(); + event_({ + type: "brushend" + }); + } + } + brush.x = function(z) { + if (!arguments.length) return x; + x = z; + resizes = d3_svg_brushResizes[!x << 1 | !y]; + return brush; + }; + brush.y = function(z) { + if (!arguments.length) return y; + y = z; + resizes = d3_svg_brushResizes[!x << 1 | !y]; + return brush; + }; + brush.clamp = function(z) { + if (!arguments.length) return x && y ? [ xClamp, yClamp ] : x ? xClamp : y ? yClamp : null; + if (x && y) xClamp = !!z[0], yClamp = !!z[1]; else if (x) xClamp = !!z; else if (y) yClamp = !!z; + return brush; + }; + brush.extent = function(z) { + var x0, x1, y0, y1, t; + if (!arguments.length) { + if (x) { + if (xExtentDomain) { + x0 = xExtentDomain[0], x1 = xExtentDomain[1]; + } else { + x0 = xExtent[0], x1 = xExtent[1]; + if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1); + if (x1 < x0) t = x0, x0 = x1, x1 = t; + } + } + if (y) { + if (yExtentDomain) { + y0 = yExtentDomain[0], y1 = yExtentDomain[1]; + } else { + y0 = yExtent[0], y1 = yExtent[1]; + if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1); + if (y1 < y0) t = y0, y0 = y1, y1 = t; + } + } + return x && y ? [ [ x0, y0 ], [ x1, y1 ] ] : x ? [ x0, x1 ] : y && [ y0, y1 ]; + } + if (x) { + x0 = z[0], x1 = z[1]; + if (y) x0 = x0[0], x1 = x1[0]; + xExtentDomain = [ x0, x1 ]; + if (x.invert) x0 = x(x0), x1 = x(x1); + if (x1 < x0) t = x0, x0 = x1, x1 = t; + if (x0 != xExtent[0] || x1 != xExtent[1]) xExtent = [ x0, x1 ]; + } + if (y) { + y0 = z[0], y1 = z[1]; + if (x) y0 = y0[1], y1 = y1[1]; + yExtentDomain = [ y0, y1 ]; + if (y.invert) y0 = y(y0), y1 = y(y1); + if (y1 < y0) t = y0, y0 = y1, y1 = t; + if (y0 != yExtent[0] || y1 != yExtent[1]) yExtent = [ y0, y1 ]; + } + return brush; + }; + brush.clear = function() { + if (!brush.empty()) { + xExtent = [ 0, 0 ], yExtent = [ 0, 0 ]; + xExtentDomain = yExtentDomain = null; + } + return brush; + }; + brush.empty = function() { + return !!x && xExtent[0] == xExtent[1] || !!y && yExtent[0] == yExtent[1]; + }; + return d3.rebind(brush, event, "on"); + }; + var d3_svg_brushCursor = { + n: "ns-resize", + e: "ew-resize", + s: "ns-resize", + w: "ew-resize", + nw: "nwse-resize", + ne: "nesw-resize", + se: "nwse-resize", + sw: "nesw-resize" + }; + var d3_svg_brushResizes = [ [ "n", "e", "s", "w", "nw", "ne", "se", "sw" ], [ "e", "w" ], [ "n", "s" ], [] ]; + var d3_time_format = d3_time.format = d3_locale_enUS.timeFormat; + var d3_time_formatUtc = d3_time_format.utc; + var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ"); + d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z") ? d3_time_formatIsoNative : d3_time_formatIso; + function d3_time_formatIsoNative(date) { + return date.toISOString(); + } + d3_time_formatIsoNative.parse = function(string) { + var date = new Date(string); + return isNaN(date) ? null : date; + }; + d3_time_formatIsoNative.toString = d3_time_formatIso.toString; + d3_time.second = d3_time_interval(function(date) { + return new d3_date(Math.floor(date / 1e3) * 1e3); + }, function(date, offset) { + date.setTime(date.getTime() + Math.floor(offset) * 1e3); + }, function(date) { + return date.getSeconds(); + }); + d3_time.seconds = d3_time.second.range; + d3_time.seconds.utc = d3_time.second.utc.range; + d3_time.minute = d3_time_interval(function(date) { + return new d3_date(Math.floor(date / 6e4) * 6e4); + }, function(date, offset) { + date.setTime(date.getTime() + Math.floor(offset) * 6e4); + }, function(date) { + return date.getMinutes(); + }); + d3_time.minutes = d3_time.minute.range; + d3_time.minutes.utc = d3_time.minute.utc.range; + d3_time.hour = d3_time_interval(function(date) { + var timezone = date.getTimezoneOffset() / 60; + return new d3_date((Math.floor(date / 36e5 - timezone) + timezone) * 36e5); + }, function(date, offset) { + date.setTime(date.getTime() + Math.floor(offset) * 36e5); + }, function(date) { + return date.getHours(); + }); + d3_time.hours = d3_time.hour.range; + d3_time.hours.utc = d3_time.hour.utc.range; + d3_time.month = d3_time_interval(function(date) { + date = d3_time.day(date); + date.setDate(1); + return date; + }, function(date, offset) { + date.setMonth(date.getMonth() + offset); + }, function(date) { + return date.getMonth(); + }); + d3_time.months = d3_time.month.range; + d3_time.months.utc = d3_time.month.utc.range; + function d3_time_scale(linear, methods, format) { + function scale(x) { + return linear(x); + } + scale.invert = function(x) { + return d3_time_scaleDate(linear.invert(x)); + }; + scale.domain = function(x) { + if (!arguments.length) return linear.domain().map(d3_time_scaleDate); + linear.domain(x); + return scale; + }; + function tickMethod(extent, count) { + var span = extent[1] - extent[0], target = span / count, i = d3.bisect(d3_time_scaleSteps, target); + return i == d3_time_scaleSteps.length ? [ methods.year, d3_scale_linearTickRange(extent.map(function(d) { + return d / 31536e6; + }), count)[2] ] : !i ? [ d3_time_scaleMilliseconds, d3_scale_linearTickRange(extent, count)[2] ] : methods[target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target ? i - 1 : i]; + } + scale.nice = function(interval, skip) { + var domain = scale.domain(), extent = d3_scaleExtent(domain), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" && tickMethod(extent, interval); + if (method) interval = method[0], skip = method[1]; + function skipped(date) { + return !isNaN(date) && !interval.range(date, d3_time_scaleDate(+date + 1), skip).length; + } + return scale.domain(d3_scale_nice(domain, skip > 1 ? { + floor: function(date) { + while (skipped(date = interval.floor(date))) date = d3_time_scaleDate(date - 1); + return date; + }, + ceil: function(date) { + while (skipped(date = interval.ceil(date))) date = d3_time_scaleDate(+date + 1); + return date; + } + } : interval)); + }; + scale.ticks = function(interval, skip) { + var extent = d3_scaleExtent(scale.domain()), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" ? tickMethod(extent, interval) : !interval.range && [ { + range: interval + }, skip ]; + if (method) interval = method[0], skip = method[1]; + return interval.range(extent[0], d3_time_scaleDate(+extent[1] + 1), skip < 1 ? 1 : skip); + }; + scale.tickFormat = function() { + return format; + }; + scale.copy = function() { + return d3_time_scale(linear.copy(), methods, format); + }; + return d3_scale_linearRebind(scale, linear); + } + function d3_time_scaleDate(t) { + return new Date(t); + } + var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ]; + var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ]; + var d3_time_scaleLocalFormat = d3_time_format.multi([ [ ".%L", function(d) { + return d.getMilliseconds(); + } ], [ ":%S", function(d) { + return d.getSeconds(); + } ], [ "%I:%M", function(d) { + return d.getMinutes(); + } ], [ "%I %p", function(d) { + return d.getHours(); + } ], [ "%a %d", function(d) { + return d.getDay() && d.getDate() != 1; + } ], [ "%b %d", function(d) { + return d.getDate() != 1; + } ], [ "%B", function(d) { + return d.getMonth(); + } ], [ "%Y", d3_true ] ]); + var d3_time_scaleMilliseconds = { + range: function(start, stop, step) { + return d3.range(Math.ceil(start / step) * step, +stop, step).map(d3_time_scaleDate); + }, + floor: d3_identity, + ceil: d3_identity + }; + d3_time_scaleLocalMethods.year = d3_time.year; + d3_time.scale = function() { + return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat); + }; + var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) { + return [ m[0].utc, m[1] ]; + }); + var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([ [ ".%L", function(d) { + return d.getUTCMilliseconds(); + } ], [ ":%S", function(d) { + return d.getUTCSeconds(); + } ], [ "%I:%M", function(d) { + return d.getUTCMinutes(); + } ], [ "%I %p", function(d) { + return d.getUTCHours(); + } ], [ "%a %d", function(d) { + return d.getUTCDay() && d.getUTCDate() != 1; + } ], [ "%b %d", function(d) { + return d.getUTCDate() != 1; + } ], [ "%B", function(d) { + return d.getUTCMonth(); + } ], [ "%Y", d3_true ] ]); + d3_time_scaleUtcMethods.year = d3_time.year.utc; + d3_time.scale.utc = function() { + return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat); + }; + d3.text = d3_xhrType(function(request) { + return request.responseText; + }); + d3.json = function(url, callback) { + return d3_xhr(url, "application/json", d3_json, callback); + }; + function d3_json(request) { + return JSON.parse(request.responseText); + } + d3.html = function(url, callback) { + return d3_xhr(url, "text/html", d3_html, callback); + }; + function d3_html(request) { + var range = d3_document.createRange(); + range.selectNode(d3_document.body); + return range.createContextualFragment(request.responseText); + } + d3.xml = d3_xhrType(function(request) { + return request.responseXML; + }); + if (typeof define === "function" && define.amd) this.d3 = d3, define(d3); else if (typeof module === "object" && module.exports) module.exports = d3; else this.d3 = d3; +}(); +},{}],87:[function(require,module,exports){ +(function (Buffer){ + +/** + * Module exports. + */ + +module.exports = dataUriToBuffer; + +/** + * Returns a `Buffer` instance from the given data URI `uri`. + * + * @param {String} uri Data URI to turn into a Buffer instance + * @return {Buffer} Buffer instance from Data URI + * @api public + */ + +function dataUriToBuffer (uri) { + if (!/^data\:/i.test(uri)) { + throw new TypeError('`uri` does not appear to be a Data URI (must begin with "data:")'); } - if (op !== output_length) { - console.log("Warning, gif stream shorter than expected."); - } + // strip newlines + uri = uri.replace(/\r?\n/g, ''); - return output; -} + // split the URI up into the "metadata" and the "data" portions + var firstComma = uri.indexOf(','); + if (-1 === firstComma || firstComma <= 4) throw new TypeError('malformed data: URI'); -try { exports.GifWriter = GifWriter; exports.GifReader = GifReader } catch(e) { } // CommonJS. + // remove the "data:" scheme and parse the metadata + var meta = uri.substring(5, firstComma).split(';'); -},{}],81:[function(require,module,exports){ -(function (process){ -var Stream = require('stream') - -// through -// -// a stream that does nothing but re-emit the input. -// useful for aggregating a series of changing but not ending streams into one stream) - -exports = module.exports = through -through.through = through - -//create a readable writable stream. - -function through (write, end, opts) { - write = write || function (data) { this.queue(data) } - end = end || function () { this.queue(null) } - - var ended = false, destroyed = false, buffer = [], _ended = false - var stream = new Stream() - stream.readable = stream.writable = true - stream.paused = false - -// stream.autoPause = !(opts && opts.autoPause === false) - stream.autoDestroy = !(opts && opts.autoDestroy === false) - - stream.write = function (data) { - write.call(this, data) - return !stream.paused - } - - function drain() { - while(buffer.length && !stream.paused) { - var data = buffer.shift() - if(null === data) - return stream.emit('end') - else - stream.emit('data', data) + var base64 = false; + var charset = 'US-ASCII'; + for (var i = 0; i < meta.length; i++) { + if ('base64' == meta[i]) { + base64 = true; + } else if (0 == meta[i].indexOf('charset=')) { + charset = meta[i].substring(8); } } - stream.queue = stream.push = function (data) { -// console.error(ended) - if(_ended) return stream - if(data === null) _ended = true - buffer.push(data) - drain() - return stream + // get the encoded data portion and decode URI-encoded chars + var data = unescape(uri.substring(firstComma + 1)); + + var encoding = base64 ? 'base64' : 'ascii'; + var buffer = new Buffer(data, encoding); + + // set `.type` property to MIME type + buffer.type = meta[0] || 'text/plain'; + + // set the `.charset` property + buffer.charset = charset; + + return buffer; +} + +}).call(this,require("buffer").Buffer) +},{"buffer":46}],88:[function(require,module,exports){ +"use strict" + +var ch = require("incremental-convex-hull") +var uniq = require("uniq") + +module.exports = triangulate + +function LiftedPoint(p, i) { + this.point = p + this.index = i +} + +function compareLifted(a, b) { + var ap = a.point + var bp = b.point + var d = ap.length + for(var i=0; i= 2) { + return false + } + } + cell[j] = v + } + return true + }) + } else { + hull = hull.filter(function(cell) { + for(var i=0; i<=d; ++i) { + var v = dindex[cell[i]] + if(v < 0) { + return false + } + cell[i] = v + } + return true + }) + } + + if(d & 1) { + for(var i=0; i>> 31 +} + +module.exports.exponent = function(n) { + var b = module.exports.hi(n) + return ((b<<1) >>> 21) - 1023 +} + +module.exports.fraction = function(n) { + var lo = module.exports.lo(n) + var hi = module.exports.hi(n) + var b = hi & ((1<<20) - 1) + if(hi & 0x7ff00000) { + b += (1<<20) + } + return [lo, b] +} + +module.exports.denormalized = function(n) { + var hi = module.exports.hi(n) + return !(hi & 0x7ff00000) +} +}).call(this,require("buffer").Buffer) +},{"buffer":46}],90:[function(require,module,exports){ +"use strict" + +function dupe_array(count, value, i) { + var c = count[i]|0 + if(c <= 0) { + return [] + } + var result = new Array(c), j + if(i === count.length-1) { + for(j=0; j 0) { + return dupe_number(count|0, value) + } + break + case "object": + if(typeof (count.length) === "number") { + return dupe_array(count, value, 0) + } + break + } + return [] +} + +module.exports = dupe +},{}],91:[function(require,module,exports){ +'use strict'; + +module.exports = earcut; + +function earcut(data, holeIndices, dim) { + + dim = dim || 2; + + var hasHoles = holeIndices && holeIndices.length, + outerLen = hasHoles ? holeIndices[0] * dim : data.length, + outerNode = linkedList(data, 0, outerLen, dim, true), + triangles = []; + + if (!outerNode) return triangles; + + var minX, minY, maxX, maxY, x, y, size; + + if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim); + + // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox + if (data.length > 80 * dim) { + minX = maxX = data[0]; + minY = maxY = data[1]; + + for (var i = dim; i < outerLen; i += dim) { + x = data[i]; + y = data[i + 1]; + if (x < minX) minX = x; + if (y < minY) minY = y; + if (x > maxX) maxX = x; + if (y > maxY) maxY = y; + } + + // minX, minY and size are later used to transform coords into integers for z-order calculation + size = Math.max(maxX - minX, maxY - minY); + } + + earcutLinked(outerNode, triangles, dim, minX, minY, size); + + return triangles; +} + +// create a circular doubly linked list from polygon points in the specified winding order +function linkedList(data, start, end, dim, clockwise) { + var i, last; + + if (clockwise === (signedArea(data, start, end, dim) > 0)) { + for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last); + } else { + for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last); + } + + if (last && equals(last, last.next)) { + removeNode(last); + last = last.next; + } + + return last; +} + +// eliminate colinear or duplicate points +function filterPoints(start, end) { + if (!start) return start; + if (!end) end = start; + + var p = start, + again; + do { + again = false; + + if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) { + removeNode(p); + p = end = p.prev; + if (p === p.next) return null; + again = true; + + } else { + p = p.next; + } + } while (again || p !== end); + + return end; +} + +// main ear slicing loop which triangulates a polygon (given as a linked list) +function earcutLinked(ear, triangles, dim, minX, minY, size, pass) { + if (!ear) return; + + // interlink polygon nodes in z-order + if (!pass && size) indexCurve(ear, minX, minY, size); + + var stop = ear, + prev, next; + + // iterate through ears, slicing them one by one + while (ear.prev !== ear.next) { + prev = ear.prev; + next = ear.next; + + if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) { + // cut off the triangle + triangles.push(prev.i / dim); + triangles.push(ear.i / dim); + triangles.push(next.i / dim); + + removeNode(ear); + + // skipping the next vertice leads to less sliver triangles + ear = next.next; + stop = next.next; + + continue; + } + + ear = next; + + // if we looped through the whole remaining polygon and can't find any more ears + if (ear === stop) { + // try filtering points and slicing again + if (!pass) { + earcutLinked(filterPoints(ear), triangles, dim, minX, minY, size, 1); + + // if this didn't work, try curing all small self-intersections locally + } else if (pass === 1) { + ear = cureLocalIntersections(ear, triangles, dim); + earcutLinked(ear, triangles, dim, minX, minY, size, 2); + + // as a last resort, try splitting the remaining polygon into two + } else if (pass === 2) { + splitEarcut(ear, triangles, dim, minX, minY, size); + } + + break; + } + } +} + +// check whether a polygon node forms a valid ear with adjacent nodes +function isEar(ear) { + var a = ear.prev, + b = ear, + c = ear.next; + + if (area(a, b, c) >= 0) return false; // reflex, can't be an ear + + // now make sure we don't have other points inside the potential ear + var p = ear.next.next; + + while (p !== ear.prev) { + if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && + area(p.prev, p, p.next) >= 0) return false; + p = p.next; + } + + return true; +} + +function isEarHashed(ear, minX, minY, size) { + var a = ear.prev, + b = ear, + c = ear.next; + + if (area(a, b, c) >= 0) return false; // reflex, can't be an ear + + // triangle bbox; min & max are calculated like this for speed + var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x), + minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y), + maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x), + maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y); + + // z-order range for the current triangle bbox; + var minZ = zOrder(minTX, minTY, minX, minY, size), + maxZ = zOrder(maxTX, maxTY, minX, minY, size); + + // first look for points inside the triangle in increasing z-order + var p = ear.nextZ; + + while (p && p.z <= maxZ) { + if (p !== ear.prev && p !== ear.next && + pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && + area(p.prev, p, p.next) >= 0) return false; + p = p.nextZ; + } + + // then look for points in decreasing z-order + p = ear.prevZ; + + while (p && p.z >= minZ) { + if (p !== ear.prev && p !== ear.next && + pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && + area(p.prev, p, p.next) >= 0) return false; + p = p.prevZ; + } + + return true; +} + +// go through all polygon nodes and cure small local self-intersections +function cureLocalIntersections(start, triangles, dim) { + var p = start; + do { + var a = p.prev, + b = p.next.next; + + if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) { + + triangles.push(a.i / dim); + triangles.push(p.i / dim); + triangles.push(b.i / dim); + + // remove two nodes involved + removeNode(p); + removeNode(p.next); + + p = start = b; + } + p = p.next; + } while (p !== start); + + return p; +} + +// try splitting polygon into two and triangulate them independently +function splitEarcut(start, triangles, dim, minX, minY, size) { + // look for a valid diagonal that divides the polygon into two + var a = start; + do { + var b = a.next.next; + while (b !== a.prev) { + if (a.i !== b.i && isValidDiagonal(a, b)) { + // split the polygon in two by the diagonal + var c = splitPolygon(a, b); + + // filter colinear points around the cuts + a = filterPoints(a, a.next); + c = filterPoints(c, c.next); + + // run earcut on each half + earcutLinked(a, triangles, dim, minX, minY, size); + earcutLinked(c, triangles, dim, minX, minY, size); + return; + } + b = b.next; + } + a = a.next; + } while (a !== start); +} + +// link every hole into the outer loop, producing a single-ring polygon without holes +function eliminateHoles(data, holeIndices, outerNode, dim) { + var queue = [], + i, len, start, end, list; + + for (i = 0, len = holeIndices.length; i < len; i++) { + start = holeIndices[i] * dim; + end = i < len - 1 ? holeIndices[i + 1] * dim : data.length; + list = linkedList(data, start, end, dim, false); + if (list === list.next) list.steiner = true; + queue.push(getLeftmost(list)); + } + + queue.sort(compareX); + + // process holes from left to right + for (i = 0; i < queue.length; i++) { + eliminateHole(queue[i], outerNode); + outerNode = filterPoints(outerNode, outerNode.next); + } + + return outerNode; +} + +function compareX(a, b) { + return a.x - b.x; +} + +// find a bridge between vertices that connects hole with an outer ring and and link it +function eliminateHole(hole, outerNode) { + outerNode = findHoleBridge(hole, outerNode); + if (outerNode) { + var b = splitPolygon(outerNode, hole); + filterPoints(b, b.next); + } +} + +// David Eberly's algorithm for finding a bridge between hole and outer polygon +function findHoleBridge(hole, outerNode) { + var p = outerNode, + hx = hole.x, + hy = hole.y, + qx = -Infinity, + m; + + // find a segment intersected by a ray from the hole's leftmost point to the left; + // segment's endpoint with lesser x will be potential connection point + do { + if (hy <= p.y && hy >= p.next.y) { + var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y); + if (x <= hx && x > qx) { + qx = x; + if (x === hx) { + if (hy === p.y) return p; + if (hy === p.next.y) return p.next; + } + m = p.x < p.next.x ? p : p.next; + } + } + p = p.next; + } while (p !== outerNode); + + if (!m) return null; + + if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint + + // look for points inside the triangle of hole point, segment intersection and endpoint; + // if there are no points found, we have a valid connection; + // otherwise choose the point of the minimum angle with the ray as connection point + + var stop = m, + mx = m.x, + my = m.y, + tanMin = Infinity, + tan; + + p = m.next; + + while (p !== stop) { + if (hx >= p.x && p.x >= mx && + pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) { + + tan = Math.abs(hy - p.y) / (hx - p.x); // tangential + + if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) { + m = p; + tanMin = tan; + } + } + + p = p.next; + } + + return m; +} + +// interlink polygon nodes in z-order +function indexCurve(start, minX, minY, size) { + var p = start; + do { + if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, size); + p.prevZ = p.prev; + p.nextZ = p.next; + p = p.next; + } while (p !== start); + + p.prevZ.nextZ = null; + p.prevZ = null; + + sortLinked(p); +} + +// Simon Tatham's linked list merge sort algorithm +// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html +function sortLinked(list) { + var i, p, q, e, tail, numMerges, pSize, qSize, + inSize = 1; + + do { + p = list; + list = null; + tail = null; + numMerges = 0; + + while (p) { + numMerges++; + q = p; + pSize = 0; + for (i = 0; i < inSize; i++) { + pSize++; + q = q.nextZ; + if (!q) break; + } + + qSize = inSize; + + while (pSize > 0 || (qSize > 0 && q)) { + + if (pSize === 0) { + e = q; + q = q.nextZ; + qSize--; + } else if (qSize === 0 || !q) { + e = p; + p = p.nextZ; + pSize--; + } else if (p.z <= q.z) { + e = p; + p = p.nextZ; + pSize--; + } else { + e = q; + q = q.nextZ; + qSize--; + } + + if (tail) tail.nextZ = e; + else list = e; + + e.prevZ = tail; + tail = e; + } + + p = q; + } + + tail.nextZ = null; + inSize *= 2; + + } while (numMerges > 1); + + return list; +} + +// z-order of a point given coords and size of the data bounding box +function zOrder(x, y, minX, minY, size) { + // coords are transformed into non-negative 15-bit integer range + x = 32767 * (x - minX) / size; + y = 32767 * (y - minY) / size; + + x = (x | (x << 8)) & 0x00FF00FF; + x = (x | (x << 4)) & 0x0F0F0F0F; + x = (x | (x << 2)) & 0x33333333; + x = (x | (x << 1)) & 0x55555555; + + y = (y | (y << 8)) & 0x00FF00FF; + y = (y | (y << 4)) & 0x0F0F0F0F; + y = (y | (y << 2)) & 0x33333333; + y = (y | (y << 1)) & 0x55555555; + + return x | (y << 1); +} + +// find the leftmost node of a polygon ring +function getLeftmost(start) { + var p = start, + leftmost = start; + do { + if (p.x < leftmost.x) leftmost = p; + p = p.next; + } while (p !== start); + + return leftmost; +} + +// check if a point lies within a convex triangle +function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) { + return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 && + (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 && + (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0; +} + +// check if a diagonal between two polygon nodes is valid (lies in polygon interior) +function isValidDiagonal(a, b) { + return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && + locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b); +} + +// signed area of a triangle +function area(p, q, r) { + return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); +} + +// check if two points are equal +function equals(p1, p2) { + return p1.x === p2.x && p1.y === p2.y; +} + +// check if two segments intersect +function intersects(p1, q1, p2, q2) { + if ((equals(p1, q1) && equals(p2, q2)) || + (equals(p1, q2) && equals(p2, q1))) return true; + return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 && + area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0; +} + +// check if a polygon diagonal intersects any polygon segments +function intersectsPolygon(a, b) { + var p = a; + do { + if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && + intersects(p, p.next, a, b)) return true; + p = p.next; + } while (p !== a); + + return false; +} + +// check if a polygon diagonal is locally inside the polygon +function locallyInside(a, b) { + return area(a.prev, a, a.next) < 0 ? + area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 : + area(a, b, a.prev) < 0 || area(a, a.next, b) < 0; +} + +// check if the middle point of a polygon diagonal is inside the polygon +function middleInside(a, b) { + var p = a, + inside = false, + px = (a.x + b.x) / 2, + py = (a.y + b.y) / 2; + do { + if (((p.y > py) !== (p.next.y > py)) && (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x)) + inside = !inside; + p = p.next; + } while (p !== a); + + return inside; +} + +// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two; +// if one belongs to the outer ring and another to a hole, it merges it into a single ring +function splitPolygon(a, b) { + var a2 = new Node(a.i, a.x, a.y), + b2 = new Node(b.i, b.x, b.y), + an = a.next, + bp = b.prev; + + a.next = b; + b.prev = a; + + a2.next = an; + an.prev = a2; + + b2.next = a2; + a2.prev = b2; + + bp.next = b2; + b2.prev = bp; + + return b2; +} + +// create a node and optionally link it with previous one (in a circular doubly linked list) +function insertNode(i, x, y, last) { + var p = new Node(i, x, y); + + if (!last) { + p.prev = p; + p.next = p; + + } else { + p.next = last.next; + p.prev = last; + last.next.prev = p; + last.next = p; + } + return p; +} + +function removeNode(p) { + p.next.prev = p.prev; + p.prev.next = p.next; + + if (p.prevZ) p.prevZ.nextZ = p.nextZ; + if (p.nextZ) p.nextZ.prevZ = p.prevZ; +} + +function Node(i, x, y) { + // vertice index in coordinates array + this.i = i; + + // vertex coordinates + this.x = x; + this.y = y; + + // previous and next vertice nodes in a polygon ring + this.prev = null; + this.next = null; + + // z-order curve value + this.z = null; + + // previous and next nodes in z-order + this.prevZ = null; + this.nextZ = null; + + // indicates whether this is a steiner point + this.steiner = false; +} + +// return a percentage difference between the polygon area and its triangulation area; +// used to verify correctness of triangulation +earcut.deviation = function (data, holeIndices, dim, triangles) { + var hasHoles = holeIndices && holeIndices.length; + var outerLen = hasHoles ? holeIndices[0] * dim : data.length; + + var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim)); + if (hasHoles) { + for (var i = 0, len = holeIndices.length; i < len; i++) { + var start = holeIndices[i] * dim; + var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length; + polygonArea -= Math.abs(signedArea(data, start, end, dim)); + } + } + + var trianglesArea = 0; + for (i = 0; i < triangles.length; i += 3) { + var a = triangles[i] * dim; + var b = triangles[i + 1] * dim; + var c = triangles[i + 2] * dim; + trianglesArea += Math.abs( + (data[a] - data[c]) * (data[b + 1] - data[a + 1]) - + (data[a] - data[b]) * (data[c + 1] - data[a + 1])); + } + + return polygonArea === 0 && trianglesArea === 0 ? 0 : + Math.abs((trianglesArea - polygonArea) / polygonArea); +}; + +function signedArea(data, start, end, dim) { + var sum = 0; + for (var i = start, j = end - dim; i < end; i += dim) { + sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]); + j = i; + } + return sum; +} + +// turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts +earcut.flatten = function (data) { + var dim = data[0][0].length, + result = {vertices: [], holes: [], dimensions: dim}, + holeIndex = 0; + + for (var i = 0; i < data.length; i++) { + for (var j = 0; j < data[i].length; j++) { + for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]); + } + if (i > 0) { + holeIndex += data[i - 1].length; + result.holes.push(holeIndex); + } + } + return result; +}; + +},{}],92:[function(require,module,exports){ +"use strict" + +module.exports = edgeToAdjacency + +var uniq = require("uniq") + +function edgeToAdjacency(edges, numVertices) { + var numEdges = edges.length + if(typeof numVertices !== "number") { + numVertices = 0 + for(var i=0; i 0 && this._events[type].length > m) { + this._events[type].warned = true; + console.error('(node) warning: possible EventEmitter memory ' + + 'leak detected. %d listeners added. ' + + 'Use emitter.setMaxListeners() to increase limit.', + this._events[type].length); + if (typeof console.trace === 'function') { + // not supported in IE 10 + console.trace(); + } + } + } + + return this; +}; + +EventEmitter.prototype.on = EventEmitter.prototype.addListener; + +EventEmitter.prototype.once = function(type, listener) { + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + var fired = false; + + function g() { + this.removeListener(type, g); + + if (!fired) { + fired = true; + listener.apply(this, arguments); + } + } + + g.listener = listener; + this.on(type, g); + + return this; +}; + +// emits a 'removeListener' event iff the listener was removed +EventEmitter.prototype.removeListener = function(type, listener) { + var list, position, length, i; + + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + if (!this._events || !this._events[type]) + return this; + + list = this._events[type]; + length = list.length; + position = -1; + + if (list === listener || + (isFunction(list.listener) && list.listener === listener)) { + delete this._events[type]; + if (this._events.removeListener) + this.emit('removeListener', type, listener); + + } else if (isObject(list)) { + for (i = length; i-- > 0;) { + if (list[i] === listener || + (list[i].listener && list[i].listener === listener)) { + position = i; + break; + } + } + + if (position < 0) + return this; + + if (list.length === 1) { + list.length = 0; + delete this._events[type]; + } else { + list.splice(position, 1); + } + + if (this._events.removeListener) + this.emit('removeListener', type, listener); + } + + return this; +}; + +EventEmitter.prototype.removeAllListeners = function(type) { + var key, listeners; + + if (!this._events) + return this; + + // not listening for removeListener, no need to emit + if (!this._events.removeListener) { + if (arguments.length === 0) + this._events = {}; + else if (this._events[type]) + delete this._events[type]; + return this; + } + + // emit removeListener for all listeners on all events + if (arguments.length === 0) { + for (key in this._events) { + if (key === 'removeListener') continue; + this.removeAllListeners(key); + } + this.removeAllListeners('removeListener'); + this._events = {}; + return this; + } + + listeners = this._events[type]; + + if (isFunction(listeners)) { + this.removeListener(type, listeners); + } else if (listeners) { + // LIFO order + while (listeners.length) + this.removeListener(type, listeners[listeners.length - 1]); + } + delete this._events[type]; + + return this; +}; + +EventEmitter.prototype.listeners = function(type) { + var ret; + if (!this._events || !this._events[type]) + ret = []; + else if (isFunction(this._events[type])) + ret = [this._events[type]]; + else + ret = this._events[type].slice(); + return ret; +}; + +EventEmitter.prototype.listenerCount = function(type) { + if (this._events) { + var evlistener = this._events[type]; + + if (isFunction(evlistener)) + return 1; + else if (evlistener) + return evlistener.length; + } + return 0; +}; + +EventEmitter.listenerCount = function(emitter, type) { + return emitter.listenerCount(type); +}; + +function isFunction(arg) { + return typeof arg === 'function'; +} + +function isNumber(arg) { + return typeof arg === 'number'; +} + +function isObject(arg) { + return typeof arg === 'object' && arg !== null; +} + +function isUndefined(arg) { + return arg === void 0; +} + +},{}],96:[function(require,module,exports){ +"use strict" + +module.exports = extractPlanes + +function extractPlanes(M, zNear, zFar) { + var z = zNear || 0.0 + var zf = zFar || 1.0 + return [ + [ M[12] + M[0], M[13] + M[1], M[14] + M[2], M[15] + M[3] ], + [ M[12] - M[0], M[13] - M[1], M[14] - M[2], M[15] - M[3] ], + [ M[12] + M[4], M[13] + M[5], M[14] + M[6], M[15] + M[7] ], + [ M[12] - M[4], M[13] - M[5], M[14] - M[6], M[15] - M[7] ], + [ z*M[12] + M[8], z*M[13] + M[9], z*M[14] + M[10], z*M[15] + M[11] ], + [ zf*M[12] - M[8], zf*M[13] - M[9], zf*M[14] - M[10], zf*M[15] - M[11] ] + ] +} +},{}],97:[function(require,module,exports){ +/** + * inspired by is-number + * but significantly simplified and sped up by ignoring number and string constructors + * ie these return false: + * new Number(1) + * new String('1') + */ + +'use strict'; + +/** + * Is this string all whitespace? + * This solution kind of makes my brain hurt, but it's significantly faster + * than !str.trim() or any other solution I could find. + * + * whitespace codes from: http://en.wikipedia.org/wiki/Whitespace_character + * and verified with: + * + * for(var i = 0; i < 65536; i++) { + * var s = String.fromCharCode(i); + * if(+s===0 && !s.trim()) console.log(i, s); + * } + * + * which counts a couple of these as *not* whitespace, but finds nothing else + * that *is* whitespace. Note that charCodeAt stops at 16 bits, but it appears + * that there are no whitespace characters above this, and code points above + * this do not map onto white space characters. + */ +function allBlankCharCodes(str){ + var l = str.length, + a; + for(var i = 0; i < l; i++) { + a = str.charCodeAt(i); + if((a < 9 || a > 13) && (a !== 32) && (a !== 133) && (a !== 160) && + (a !== 5760) && (a !== 6158) && (a < 8192 || a > 8205) && + (a !== 8232) && (a !== 8233) && (a !== 8239) && (a !== 8287) && + (a !== 8288) && (a !== 12288) && (a !== 65279)) { + return false; + } + } + return true; +} + +module.exports = function(n) { + var type = typeof n; + if(type === 'string') { + var original = n; + n = +n; + // whitespace strings cast to zero - filter them out + if(n===0 && allBlankCharCodes(original)) return false; + } + else if(type !== 'number') return false; + + return n - n < 1; +}; + +},{}],98:[function(require,module,exports){ +'use strict'; + +module.exports = createFilter; + +var types = ['Unknown', 'Point', 'LineString', 'Polygon']; + +/** + * Given a filter expressed as nested arrays, return a new function + * that evaluates whether a given feature (with a .properties or .tags property) + * passes its test. + * + * @param {Array} filter mapbox gl filter + * @returns {Function} filter-evaluating function + */ +function createFilter(filter) { + return new Function('f', 'var p = (f && f.properties || {}); return ' + compile(filter)); +} + +function compile(filter) { + if (!filter) return 'true'; + var op = filter[0]; + if (filter.length <= 1) return op === 'any' ? 'false' : 'true'; + var str = + op === '==' ? compileComparisonOp(filter[1], filter[2], '===', false) : + op === '!=' ? compileComparisonOp(filter[1], filter[2], '!==', false) : + op === '<' || + op === '>' || + op === '<=' || + op === '>=' ? compileComparisonOp(filter[1], filter[2], op, true) : + op === 'any' ? compileLogicalOp(filter.slice(1), '||') : + op === 'all' ? compileLogicalOp(filter.slice(1), '&&') : + op === 'none' ? compileNegation(compileLogicalOp(filter.slice(1), '||')) : + op === 'in' ? compileInOp(filter[1], filter.slice(2)) : + op === '!in' ? compileNegation(compileInOp(filter[1], filter.slice(2))) : + op === 'has' ? compileHasOp(filter[1]) : + op === '!has' ? compileNegation(compileHasOp([filter[1]])) : + 'true'; + return '(' + str + ')'; +} + +function compilePropertyReference(property) { + return property === '$type' ? 'f.type' : + property === '$id' ? 'f.id' : + 'p[' + JSON.stringify(property) + ']'; +} + +function compileComparisonOp(property, value, op, checkType) { + var left = compilePropertyReference(property); + var right = property === '$type' ? types.indexOf(value) : JSON.stringify(value); + return (checkType ? 'typeof ' + left + '=== typeof ' + right + '&&' : '') + left + op + right; +} + +function compileLogicalOp(expressions, op) { + return expressions.map(compile).join(op); +} + +function compileInOp(property, values) { + if (property === '$type') values = values.map(function(value) { return types.indexOf(value); }); + var left = JSON.stringify(values.sort(compare)); + var right = compilePropertyReference(property); + + if (values.length <= 200) return left + '.indexOf(' + right + ') !== -1'; + + return 'function(v, a, i, j) {' + + 'while (i <= j) { var m = (i + j) >> 1;' + + ' if (a[m] === v) return true; if (a[m] > v) j = m - 1; else i = m + 1;' + + '}' + + 'return false; }(' + right + ', ' + left + ',0,' + (values.length - 1) + ')'; +} + +function compileHasOp(property) { + return JSON.stringify(property) + ' in p'; +} + +function compileNegation(expression) { + return '!(' + expression + ')'; +} + +// Comparison function to sort numbers and strings +function compare(a, b) { + return a < b ? -1 : a > b ? 1 : 0; +} + +},{}],99:[function(require,module,exports){ +'use strict' + +module.exports = createFilteredVector + +var cubicHermite = require('cubic-hermite') +var bsearch = require('binary-search-bounds') + +function clamp(lo, hi, x) { + return Math.min(hi, Math.max(lo, x)) +} + +function FilteredVector(state0, velocity0, t0) { + this.dimension = state0.length + this.bounds = [ new Array(this.dimension), new Array(this.dimension) ] + for(var i=0; i= n-1) { + var ptr = state.length-1 + var tf = t - time[n-1] + for(var i=0; i= n-1) { + var ptr = state.length-1 + var tf = t - time[n-1] + for(var i=0; i=0; --i) { + if(velocity[--ptr]) { + return false + } + } + return true +} + +proto.jump = function(t) { + var t0 = this.lastT() + var d = this.dimension + if(t < t0 || arguments.length !== d+1) { + return + } + var state = this._state + var velocity = this._velocity + var ptr = state.length-this.dimension + var bounds = this.bounds + var lo = bounds[0] + var hi = bounds[1] + this._time.push(t0, t) + for(var j=0; j<2; ++j) { + for(var i=0; i0; --i) { + state.push(clamp(lo[i-1], hi[i-1], arguments[i])) + velocity.push(0) + } +} + +proto.push = function(t) { + var t0 = this.lastT() + var d = this.dimension + if(t < t0 || arguments.length !== d+1) { + return + } + var state = this._state + var velocity = this._velocity + var ptr = state.length-this.dimension + var dt = t - t0 + var bounds = this.bounds + var lo = bounds[0] + var hi = bounds[1] + var sf = (dt > 1e-6) ? 1/dt : 0 + this._time.push(t) + for(var i=d; i>0; --i) { + var xc = clamp(lo[i-1], hi[i-1], arguments[i]) + state.push(xc) + velocity.push((xc - state[ptr++]) * sf) + } +} + +proto.set = function(t) { + var d = this.dimension + if(t < this.lastT() || arguments.length !== d+1) { + return + } + var state = this._state + var velocity = this._velocity + var bounds = this.bounds + var lo = bounds[0] + var hi = bounds[1] + this._time.push(t) + for(var i=d; i>0; --i) { + state.push(clamp(lo[i-1], hi[i-1], arguments[i])) + velocity.push(0) + } +} + +proto.move = function(t) { + var t0 = this.lastT() + var d = this.dimension + if(t <= t0 || arguments.length !== d+1) { + return + } + var state = this._state + var velocity = this._velocity + var statePtr = state.length - this.dimension + var bounds = this.bounds + var lo = bounds[0] + var hi = bounds[1] + var dt = t - t0 + var sf = (dt > 1e-6) ? 1/dt : 0.0 + this._time.push(t) + for(var i=d; i>0; --i) { + var dx = arguments[i] + state.push(clamp(lo[i-1], hi[i-1], state[statePtr++] + dx)) + velocity.push(dx * sf) + } +} + +proto.idle = function(t) { + var t0 = this.lastT() + if(t < t0) { + return + } + var d = this.dimension + var state = this._state + var velocity = this._velocity + var statePtr = state.length-d + var bounds = this.bounds + var lo = bounds[0] + var hi = bounds[1] + var dt = t - t0 + this._time.push(t) + for(var i=d-1; i>=0; --i) { + state.push(clamp(lo[i], hi[i], state[statePtr] + dt * velocity[statePtr])) + velocity.push(0) + statePtr += 1 + } +} + +function getZero(d) { + var result = new Array(d) + for(var i=0; i=0; --s) { + var n = n_stack[s] + if(d_stack[s] <= 0) { + n_stack[s] = new RBNode(n._color, n.key, n.value, n_stack[s+1], n.right, n._count+1) + } else { + n_stack[s] = new RBNode(n._color, n.key, n.value, n.left, n_stack[s+1], n._count+1) + } + } + //Rebalance tree using rotations + //console.log("start insert", key, d_stack) + for(var s=n_stack.length-1; s>1; --s) { + var p = n_stack[s-1] + var n = n_stack[s] + if(p._color === BLACK || n._color === BLACK) { + break + } + var pp = n_stack[s-2] + if(pp.left === p) { + if(p.left === n) { + var y = pp.right + if(y && y._color === RED) { + //console.log("LLr") + p._color = BLACK + pp.right = repaint(BLACK, y) + pp._color = RED + s -= 1 + } else { + //console.log("LLb") + pp._color = RED + pp.left = p.right + p._color = BLACK + p.right = pp + n_stack[s-2] = p + n_stack[s-1] = n + recount(pp) + recount(p) + if(s >= 3) { + var ppp = n_stack[s-3] + if(ppp.left === pp) { + ppp.left = p + } else { + ppp.right = p + } + } + break + } + } else { + var y = pp.right + if(y && y._color === RED) { + //console.log("LRr") + p._color = BLACK + pp.right = repaint(BLACK, y) + pp._color = RED + s -= 1 + } else { + //console.log("LRb") + p.right = n.left + pp._color = RED + pp.left = n.right + n._color = BLACK + n.left = p + n.right = pp + n_stack[s-2] = n + n_stack[s-1] = p + recount(pp) + recount(p) + recount(n) + if(s >= 3) { + var ppp = n_stack[s-3] + if(ppp.left === pp) { + ppp.left = n + } else { + ppp.right = n + } + } + break + } + } + } else { + if(p.right === n) { + var y = pp.left + if(y && y._color === RED) { + //console.log("RRr", y.key) + p._color = BLACK + pp.left = repaint(BLACK, y) + pp._color = RED + s -= 1 + } else { + //console.log("RRb") + pp._color = RED + pp.right = p.left + p._color = BLACK + p.left = pp + n_stack[s-2] = p + n_stack[s-1] = n + recount(pp) + recount(p) + if(s >= 3) { + var ppp = n_stack[s-3] + if(ppp.right === pp) { + ppp.right = p + } else { + ppp.left = p + } + } + break + } + } else { + var y = pp.left + if(y && y._color === RED) { + //console.log("RLr") + p._color = BLACK + pp.left = repaint(BLACK, y) + pp._color = RED + s -= 1 + } else { + //console.log("RLb") + p.left = n.right + pp._color = RED + pp.right = n.left + n._color = BLACK + n.right = p + n.left = pp + n_stack[s-2] = n + n_stack[s-1] = p + recount(pp) + recount(p) + recount(n) + if(s >= 3) { + var ppp = n_stack[s-3] + if(ppp.right === pp) { + ppp.right = n + } else { + ppp.left = n + } + } + break + } + } + } + } + //Return new tree + n_stack[0]._color = BLACK + return new RedBlackTree(cmp, n_stack[0]) +} + + +//Visit all nodes inorder +function doVisitFull(visit, node) { + if(node.left) { + var v = doVisitFull(visit, node.left) + if(v) { return v } + } + var v = visit(node.key, node.value) + if(v) { return v } + if(node.right) { + return doVisitFull(visit, node.right) + } +} + +//Visit half nodes in order +function doVisitHalf(lo, compare, visit, node) { + var l = compare(lo, node.key) + if(l <= 0) { + if(node.left) { + var v = doVisitHalf(lo, compare, visit, node.left) + if(v) { return v } + } + var v = visit(node.key, node.value) + if(v) { return v } + } + if(node.right) { + return doVisitHalf(lo, compare, visit, node.right) + } +} + +//Visit all nodes within a range +function doVisit(lo, hi, compare, visit, node) { + var l = compare(lo, node.key) + var h = compare(hi, node.key) + var v + if(l <= 0) { + if(node.left) { + v = doVisit(lo, hi, compare, visit, node.left) + if(v) { return v } + } + if(h > 0) { + v = visit(node.key, node.value) + if(v) { return v } + } + } + if(h > 0 && node.right) { + return doVisit(lo, hi, compare, visit, node.right) + } +} + + +proto.forEach = function rbTreeForEach(visit, lo, hi) { + if(!this.root) { + return + } + switch(arguments.length) { + case 1: + return doVisitFull(visit, this.root) + break + + case 2: + return doVisitHalf(lo, this._compare, visit, this.root) + break + + case 3: + if(this._compare(lo, hi) >= 0) { + return + } + return doVisit(lo, hi, this._compare, visit, this.root) + break + } +} + +//First item in list +Object.defineProperty(proto, "begin", { + get: function() { + var stack = [] + var n = this.root + while(n) { + stack.push(n) + n = n.left + } + return new RedBlackTreeIterator(this, stack) + } +}) + +//Last item in list +Object.defineProperty(proto, "end", { + get: function() { + var stack = [] + var n = this.root + while(n) { + stack.push(n) + n = n.right + } + return new RedBlackTreeIterator(this, stack) + } +}) + +//Find the ith item in the tree +proto.at = function(idx) { + if(idx < 0) { + return new RedBlackTreeIterator(this, []) + } + var n = this.root + var stack = [] + while(true) { + stack.push(n) + if(n.left) { + if(idx < n.left._count) { + n = n.left + continue + } + idx -= n.left._count + } + if(!idx) { + return new RedBlackTreeIterator(this, stack) + } + idx -= 1 + if(n.right) { + if(idx >= n.right._count) { + break + } + n = n.right + } else { + break + } + } + return new RedBlackTreeIterator(this, []) +} + +proto.ge = function(key) { + var cmp = this._compare + var n = this.root + var stack = [] + var last_ptr = 0 + while(n) { + var d = cmp(key, n.key) + stack.push(n) + if(d <= 0) { + last_ptr = stack.length + } + if(d <= 0) { + n = n.left + } else { + n = n.right + } + } + stack.length = last_ptr + return new RedBlackTreeIterator(this, stack) +} + +proto.gt = function(key) { + var cmp = this._compare + var n = this.root + var stack = [] + var last_ptr = 0 + while(n) { + var d = cmp(key, n.key) + stack.push(n) + if(d < 0) { + last_ptr = stack.length + } + if(d < 0) { + n = n.left + } else { + n = n.right + } + } + stack.length = last_ptr + return new RedBlackTreeIterator(this, stack) +} + +proto.lt = function(key) { + var cmp = this._compare + var n = this.root + var stack = [] + var last_ptr = 0 + while(n) { + var d = cmp(key, n.key) + stack.push(n) + if(d > 0) { + last_ptr = stack.length + } + if(d <= 0) { + n = n.left + } else { + n = n.right + } + } + stack.length = last_ptr + return new RedBlackTreeIterator(this, stack) +} + +proto.le = function(key) { + var cmp = this._compare + var n = this.root + var stack = [] + var last_ptr = 0 + while(n) { + var d = cmp(key, n.key) + stack.push(n) + if(d >= 0) { + last_ptr = stack.length + } + if(d < 0) { + n = n.left + } else { + n = n.right + } + } + stack.length = last_ptr + return new RedBlackTreeIterator(this, stack) +} + +//Finds the item with key if it exists +proto.find = function(key) { + var cmp = this._compare + var n = this.root + var stack = [] + while(n) { + var d = cmp(key, n.key) + stack.push(n) + if(d === 0) { + return new RedBlackTreeIterator(this, stack) + } + if(d <= 0) { + n = n.left + } else { + n = n.right + } + } + return new RedBlackTreeIterator(this, []) +} + +//Removes item with key from tree +proto.remove = function(key) { + var iter = this.find(key) + if(iter) { + return iter.remove() + } + return this +} + +//Returns the item at `key` +proto.get = function(key) { + var cmp = this._compare + var n = this.root + while(n) { + var d = cmp(key, n.key) + if(d === 0) { + return n.value + } + if(d <= 0) { + n = n.left + } else { + n = n.right + } + } + return +} + +//Iterator for red black tree +function RedBlackTreeIterator(tree, stack) { + this.tree = tree + this._stack = stack +} + +var iproto = RedBlackTreeIterator.prototype + +//Test if iterator is valid +Object.defineProperty(iproto, "valid", { + get: function() { + return this._stack.length > 0 + } +}) + +//Node of the iterator +Object.defineProperty(iproto, "node", { + get: function() { + if(this._stack.length > 0) { + return this._stack[this._stack.length-1] + } + return null + }, + enumerable: true +}) + +//Makes a copy of an iterator +iproto.clone = function() { + return new RedBlackTreeIterator(this.tree, this._stack.slice()) +} + +//Swaps two nodes +function swapNode(n, v) { + n.key = v.key + n.value = v.value + n.left = v.left + n.right = v.right + n._color = v._color + n._count = v._count +} + +//Fix up a double black node in a tree +function fixDoubleBlack(stack) { + var n, p, s, z + for(var i=stack.length-1; i>=0; --i) { + n = stack[i] + if(i === 0) { + n._color = BLACK + return + } + //console.log("visit node:", n.key, i, stack[i].key, stack[i-1].key) + p = stack[i-1] + if(p.left === n) { + //console.log("left child") + s = p.right + if(s.right && s.right._color === RED) { + //console.log("case 1: right sibling child red") + s = p.right = cloneNode(s) + z = s.right = cloneNode(s.right) + p.right = s.left + s.left = p + s.right = z + s._color = p._color + n._color = BLACK + p._color = BLACK + z._color = BLACK + recount(p) + recount(s) + if(i > 1) { + var pp = stack[i-2] + if(pp.left === p) { + pp.left = s + } else { + pp.right = s + } + } + stack[i-1] = s + return + } else if(s.left && s.left._color === RED) { + //console.log("case 1: left sibling child red") + s = p.right = cloneNode(s) + z = s.left = cloneNode(s.left) + p.right = z.left + s.left = z.right + z.left = p + z.right = s + z._color = p._color + p._color = BLACK + s._color = BLACK + n._color = BLACK + recount(p) + recount(s) + recount(z) + if(i > 1) { + var pp = stack[i-2] + if(pp.left === p) { + pp.left = z + } else { + pp.right = z + } + } + stack[i-1] = z + return + } + if(s._color === BLACK) { + if(p._color === RED) { + //console.log("case 2: black sibling, red parent", p.right.value) + p._color = BLACK + p.right = repaint(RED, s) + return + } else { + //console.log("case 2: black sibling, black parent", p.right.value) + p.right = repaint(RED, s) + continue + } + } else { + //console.log("case 3: red sibling") + s = cloneNode(s) + p.right = s.left + s.left = p + s._color = p._color + p._color = RED + recount(p) + recount(s) + if(i > 1) { + var pp = stack[i-2] + if(pp.left === p) { + pp.left = s + } else { + pp.right = s + } + } + stack[i-1] = s + stack[i] = p + if(i+1 < stack.length) { + stack[i+1] = n + } else { + stack.push(n) + } + i = i+2 + } + } else { + //console.log("right child") + s = p.left + if(s.left && s.left._color === RED) { + //console.log("case 1: left sibling child red", p.value, p._color) + s = p.left = cloneNode(s) + z = s.left = cloneNode(s.left) + p.left = s.right + s.right = p + s.left = z + s._color = p._color + n._color = BLACK + p._color = BLACK + z._color = BLACK + recount(p) + recount(s) + if(i > 1) { + var pp = stack[i-2] + if(pp.right === p) { + pp.right = s + } else { + pp.left = s + } + } + stack[i-1] = s + return + } else if(s.right && s.right._color === RED) { + //console.log("case 1: right sibling child red") + s = p.left = cloneNode(s) + z = s.right = cloneNode(s.right) + p.left = z.right + s.right = z.left + z.right = p + z.left = s + z._color = p._color + p._color = BLACK + s._color = BLACK + n._color = BLACK + recount(p) + recount(s) + recount(z) + if(i > 1) { + var pp = stack[i-2] + if(pp.right === p) { + pp.right = z + } else { + pp.left = z + } + } + stack[i-1] = z + return + } + if(s._color === BLACK) { + if(p._color === RED) { + //console.log("case 2: black sibling, red parent") + p._color = BLACK + p.left = repaint(RED, s) + return + } else { + //console.log("case 2: black sibling, black parent") + p.left = repaint(RED, s) + continue + } + } else { + //console.log("case 3: red sibling") + s = cloneNode(s) + p.left = s.right + s.right = p + s._color = p._color + p._color = RED + recount(p) + recount(s) + if(i > 1) { + var pp = stack[i-2] + if(pp.right === p) { + pp.right = s + } else { + pp.left = s + } + } + stack[i-1] = s + stack[i] = p + if(i+1 < stack.length) { + stack[i+1] = n + } else { + stack.push(n) + } + i = i+2 + } + } + } +} + +//Removes item at iterator from tree +iproto.remove = function() { + var stack = this._stack + if(stack.length === 0) { + return this.tree + } + //First copy path to node + var cstack = new Array(stack.length) + var n = stack[stack.length-1] + cstack[cstack.length-1] = new RBNode(n._color, n.key, n.value, n.left, n.right, n._count) + for(var i=stack.length-2; i>=0; --i) { + var n = stack[i] + if(n.left === stack[i+1]) { + cstack[i] = new RBNode(n._color, n.key, n.value, cstack[i+1], n.right, n._count) + } else { + cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) + } + } + + //Get node + n = cstack[cstack.length-1] + //console.log("start remove: ", n.value) + + //If not leaf, then swap with previous node + if(n.left && n.right) { + //console.log("moving to leaf") + + //First walk to previous leaf + var split = cstack.length + n = n.left + while(n.right) { + cstack.push(n) + n = n.right + } + //Copy path to leaf + var v = cstack[split-1] + cstack.push(new RBNode(n._color, v.key, v.value, n.left, n.right, n._count)) + cstack[split-1].key = n.key + cstack[split-1].value = n.value + + //Fix up stack + for(var i=cstack.length-2; i>=split; --i) { + n = cstack[i] + cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) + } + cstack[split-1].left = cstack[split] + } + //console.log("stack=", cstack.map(function(v) { return v.value })) + + //Remove leaf node + n = cstack[cstack.length-1] + if(n._color === RED) { + //Easy case: removing red leaf + //console.log("RED leaf") + var p = cstack[cstack.length-2] + if(p.left === n) { + p.left = null + } else if(p.right === n) { + p.right = null + } + cstack.pop() + for(var i=0; i 0) { + return this._stack[this._stack.length-1].key + } + return + }, + enumerable: true +}) + +//Returns value +Object.defineProperty(iproto, "value", { + get: function() { + if(this._stack.length > 0) { + return this._stack[this._stack.length-1].value + } + return + }, + enumerable: true +}) + + +//Returns the position of this iterator in the sorted list +Object.defineProperty(iproto, "index", { + get: function() { + var idx = 0 + var stack = this._stack + if(stack.length === 0) { + var r = this.tree.root + if(r) { + return r._count + } + return 0 + } else if(stack[stack.length-1].left) { + idx = stack[stack.length-1].left._count + } + for(var s=stack.length-2; s>=0; --s) { + if(stack[s+1] === stack[s].right) { + ++idx + if(stack[s].left) { + idx += stack[s].left._count + } + } + } + return idx + }, + enumerable: true +}) + +//Advances iterator to next element in list +iproto.next = function() { + var stack = this._stack + if(stack.length === 0) { + return + } + var n = stack[stack.length-1] + if(n.right) { + n = n.right + while(n) { + stack.push(n) + n = n.left + } + } else { + stack.pop() + while(stack.length > 0 && stack[stack.length-1].right === n) { + n = stack[stack.length-1] + stack.pop() + } + } +} + +//Checks if iterator is at end of tree +Object.defineProperty(iproto, "hasNext", { + get: function() { + var stack = this._stack + if(stack.length === 0) { + return false + } + if(stack[stack.length-1].right) { + return true + } + for(var s=stack.length-1; s>0; --s) { + if(stack[s-1].left === stack[s]) { + return true + } + } + return false + } +}) + +//Update value +iproto.update = function(value) { + var stack = this._stack + if(stack.length === 0) { + throw new Error("Can't update empty node!") + } + var cstack = new Array(stack.length) + var n = stack[stack.length-1] + cstack[cstack.length-1] = new RBNode(n._color, n.key, value, n.left, n.right, n._count) + for(var i=stack.length-2; i>=0; --i) { + n = stack[i] + if(n.left === stack[i+1]) { + cstack[i] = new RBNode(n._color, n.key, n.value, cstack[i+1], n.right, n._count) + } else { + cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) + } + } + return new RedBlackTree(this.tree._compare, cstack[0]) +} + +//Moves iterator backward one element +iproto.prev = function() { + var stack = this._stack + if(stack.length === 0) { + return + } + var n = stack[stack.length-1] + if(n.left) { + n = n.left + while(n) { + stack.push(n) + n = n.right + } + } else { + stack.pop() + while(stack.length > 0 && stack[stack.length-1].left === n) { + n = stack[stack.length-1] + stack.pop() + } + } +} + +//Checks if iterator is at start of tree +Object.defineProperty(iproto, "hasPrev", { + get: function() { + var stack = this._stack + if(stack.length === 0) { + return false + } + if(stack[stack.length-1].left) { + return true + } + for(var s=stack.length-1; s>0; --s) { + if(stack[s-1].right === stack[s]) { + return true + } + } + return false + } +}) + +//Default comparison function +function defaultCompare(a, b) { + if(a < b) { + return -1 + } + if(a > b) { + return 1 + } + return 0 +} + +//Build a tree +function createRBTree(compare) { + return new RedBlackTree(compare || defaultCompare, null) +} +},{}],101:[function(require,module,exports){ +// transliterated from the python snippet here: +// http://en.wikipedia.org/wiki/Lanczos_approximation + +var g = 7; +var p = [ + 0.99999999999980993, + 676.5203681218851, + -1259.1392167224028, + 771.32342877765313, + -176.61502916214059, + 12.507343278686905, + -0.13857109526572012, + 9.9843695780195716e-6, + 1.5056327351493116e-7 +]; + +var g_ln = 607/128; +var p_ln = [ + 0.99999999999999709182, + 57.156235665862923517, + -59.597960355475491248, + 14.136097974741747174, + -0.49191381609762019978, + 0.33994649984811888699e-4, + 0.46523628927048575665e-4, + -0.98374475304879564677e-4, + 0.15808870322491248884e-3, + -0.21026444172410488319e-3, + 0.21743961811521264320e-3, + -0.16431810653676389022e-3, + 0.84418223983852743293e-4, + -0.26190838401581408670e-4, + 0.36899182659531622704e-5 +]; + +// Spouge approximation (suitable for large arguments) +function lngamma(z) { + + if(z < 0) return Number('0/0'); + var x = p_ln[0]; + for(var i = p_ln.length - 1; i > 0; --i) x += p_ln[i] / (z + i); + var t = z + g_ln + 0.5; + return .5*Math.log(2*Math.PI)+(z+.5)*Math.log(t)-t+Math.log(x)-Math.log(z); +} + +module.exports = function gamma (z) { + if (z < 0.5) { + return Math.PI / (Math.sin(Math.PI * z) * gamma(1 - z)); + } + else if(z > 100) return Math.exp(lngamma(z)); + else { + z -= 1; + var x = p[0]; + for (var i = 1; i < g + 2; i++) { + x += p[i] / (z + i); + } + var t = z + g + 0.5; + + return Math.sqrt(2 * Math.PI) + * Math.pow(t, z + 0.5) + * Math.exp(-t) + * x + ; + } +}; + +module.exports.log = lngamma; + +},{}],102:[function(require,module,exports){ +var wgs84 = require('wgs84'); + +module.exports.geometry = geometry; +module.exports.ring = ringArea; + +function geometry(_) { + if (_.type === 'Polygon') return polygonArea(_.coordinates); + else if (_.type === 'MultiPolygon') { + var area = 0; + for (var i = 0; i < _.coordinates.length; i++) { + area += polygonArea(_.coordinates[i]); + } + return area; + } else { + return null; + } +} + +function polygonArea(coords) { + var area = 0; + if (coords && coords.length > 0) { + area += Math.abs(ringArea(coords[0])); + for (var i = 1; i < coords.length; i++) { + area -= Math.abs(ringArea(coords[i])); + } + } + return area; +} + +/** + * Calculate the approximate area of the polygon were it projected onto + * the earth. Note that this area will be positive if ring is oriented + * clockwise, otherwise it will be negative. + * + * Reference: + * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for + * Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion + * Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409 + * + * Returns: + * {float} The approximate signed geodesic area of the polygon in square + * meters. + */ + +function ringArea(coords) { + var area = 0; + + if (coords.length > 2) { + var p1, p2; + for (var i = 0; i < coords.length - 1; i++) { + p1 = coords[i]; + p2 = coords[i + 1]; + area += rad(p2[0] - p1[0]) * (2 + Math.sin(rad(p1[1])) + Math.sin(rad(p2[1]))); + } + + area = area * wgs84.RADIUS * wgs84.RADIUS / 2; + } + + return area; +} + +function rad(_) { + return _ * Math.PI / 180; +} + +},{"wgs84":1017}],103:[function(require,module,exports){ +var geojsonArea = require('geojson-area'); + +module.exports = rewind; + +function rewind(gj, outer) { + switch ((gj && gj.type) || null) { + case 'FeatureCollection': + gj.features = gj.features.map(curryOuter(rewind, outer)); + return gj; + case 'Feature': + gj.geometry = rewind(gj.geometry, outer); + return gj; + case 'Polygon': + case 'MultiPolygon': + return correct(gj, outer); + default: + return gj; + } +} + +function curryOuter(a, b) { + return function(_) { return a(_, b); }; +} + +function correct(_, outer) { + if (_.type === 'Polygon') { + _.coordinates = correctRings(_.coordinates, outer); + } else if (_.type === 'MultiPolygon') { + _.coordinates = _.coordinates.map(curryOuter(correctRings, outer)); + } + return _; +} + +function correctRings(_, outer) { + outer = !!outer; + _[0] = wind(_[0], !outer); + for (var i = 1; i < _.length; i++) { + _[i] = wind(_[i], outer); + } + return _; +} + +function wind(_, dir) { + return cw(_) === dir ? _ : _.reverse(); +} + +function cw(_) { + return geojsonArea.ring(_) >= 0; +} + +},{"geojson-area":102}],104:[function(require,module,exports){ +'use strict'; + +module.exports = clip; + +var createFeature = require('./feature'); + +/* clip features between two axis-parallel lines: + * | | + * ___|___ | / + * / | \____|____/ + * | | + */ + +function clip(features, scale, k1, k2, axis, intersect, minAll, maxAll) { + + k1 /= scale; + k2 /= scale; + + if (minAll >= k1 && maxAll <= k2) return features; // trivial accept + else if (minAll > k2 || maxAll < k1) return null; // trivial reject + + var clipped = []; + + for (var i = 0; i < features.length; i++) { + + var feature = features[i], + geometry = feature.geometry, + type = feature.type, + min, max; + + min = feature.min[axis]; + max = feature.max[axis]; + + if (min >= k1 && max <= k2) { // trivial accept + clipped.push(feature); + continue; + } else if (min > k2 || max < k1) continue; // trivial reject + + var slices = type === 1 ? + clipPoints(geometry, k1, k2, axis) : + clipGeometry(geometry, k1, k2, axis, intersect, type === 3); + + if (slices.length) { + // if a feature got clipped, it will likely get clipped on the next zoom level as well, + // so there's no need to recalculate bboxes + clipped.push(createFeature(feature.tags, type, slices, feature.id)); } } - if (!wkey) { - wkey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16); - var wcache = {}; - for (var i = 0, l = cacheKeys.length; i < l; i++) { - var key = cacheKeys[i]; - wcache[key] = key; - } - sources[wkey] = [ - Function(['require','module','exports'], '(' + fn + ')(self)'), - wcache - ]; + return clipped.length ? clipped : null; +} + +function clipPoints(geometry, k1, k2, axis) { + var slice = []; + + for (var i = 0; i < geometry.length; i++) { + var a = geometry[i], + ak = a[axis]; + + if (ak >= k1 && ak <= k2) slice.push(a); } - var skey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16); + return slice; +} - var scache = {}; scache[wkey] = wkey; - sources[skey] = [ - Function(['require'], ( - // try to call default if defined to also support babel esmodule - // exports - 'var f = require(' + stringify(wkey) + ');' + - '(f.default ? f.default : f)(self);' - )), - scache - ]; +function clipGeometry(geometry, k1, k2, axis, intersect, closed) { - var workerSources = {}; - resolveSources(skey); + var slices = []; - function resolveSources(key) { - workerSources[key] = true; + for (var i = 0; i < geometry.length; i++) { - for (var depPath in sources[key][1]) { - var depKey = sources[key][1][depPath]; - if (!workerSources[depKey]) { - resolveSources(depKey); + var ak = 0, + bk = 0, + b = null, + points = geometry[i], + area = points.area, + dist = points.dist, + outer = points.outer, + len = points.length, + a, j, last; + + var slice = []; + + for (j = 0; j < len - 1; j++) { + a = b || points[j]; + b = points[j + 1]; + ak = bk || a[axis]; + bk = b[axis]; + + if (ak < k1) { + + if ((bk > k2)) { // ---|-----|--> + slice.push(intersect(a, b, k1), intersect(a, b, k2)); + if (!closed) slice = newSlice(slices, slice, area, dist, outer); + + } else if (bk >= k1) slice.push(intersect(a, b, k1)); // ---|--> | + + } else if (ak > k2) { + + if ((bk < k1)) { // <--|-----|--- + slice.push(intersect(a, b, k2), intersect(a, b, k1)); + if (!closed) slice = newSlice(slices, slice, area, dist, outer); + + } else if (bk <= k2) slice.push(intersect(a, b, k2)); // | <--|--- + + } else { + + slice.push(a); + + if (bk < k1) { // <--|--- | + slice.push(intersect(a, b, k1)); + if (!closed) slice = newSlice(slices, slice, area, dist, outer); + + } else if (bk > k2) { // | ---|--> + slice.push(intersect(a, b, k2)); + if (!closed) slice = newSlice(slices, slice, area, dist, outer); + } + // | --> | + } + } + + // add the last point + a = points[len - 1]; + ak = a[axis]; + if (ak >= k1 && ak <= k2) slice.push(a); + + // close the polygon if its endpoints are not the same after clipping + + last = slice[slice.length - 1]; + if (closed && last && (slice[0][0] !== last[0] || slice[0][1] !== last[1])) slice.push(slice[0]); + + // add the final slice + newSlice(slices, slice, area, dist, outer); + } + + return slices; +} + +function newSlice(slices, slice, area, dist, outer) { + if (slice.length) { + // we don't recalculate the area/length of the unclipped geometry because the case where it goes + // below the visibility threshold as a result of clipping is rare, so we avoid doing unnecessary work + slice.area = area; + slice.dist = dist; + if (outer !== undefined) slice.outer = outer; + + slices.push(slice); + } + return []; +} + +},{"./feature":106}],105:[function(require,module,exports){ +'use strict'; + +module.exports = convert; + +var simplify = require('./simplify'); +var createFeature = require('./feature'); + +// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data + +function convert(data, tolerance) { + var features = []; + + if (data.type === 'FeatureCollection') { + for (var i = 0; i < data.features.length; i++) { + convertFeature(features, data.features[i], tolerance); + } + } else if (data.type === 'Feature') { + convertFeature(features, data, tolerance); + + } else { + // single geometry or a geometry collection + convertFeature(features, {geometry: data}, tolerance); + } + return features; +} + +function convertFeature(features, feature, tolerance) { + if (feature.geometry === null) { + // ignore features with null geometry + return; + } + + var geom = feature.geometry, + type = geom.type, + coords = geom.coordinates, + tags = feature.properties, + id = feature.id, + i, j, rings, projectedRing; + + if (type === 'Point') { + features.push(createFeature(tags, 1, [projectPoint(coords)], id)); + + } else if (type === 'MultiPoint') { + features.push(createFeature(tags, 1, project(coords), id)); + + } else if (type === 'LineString') { + features.push(createFeature(tags, 2, [project(coords, tolerance)], id)); + + } else if (type === 'MultiLineString' || type === 'Polygon') { + rings = []; + for (i = 0; i < coords.length; i++) { + projectedRing = project(coords[i], tolerance); + if (type === 'Polygon') projectedRing.outer = (i === 0); + rings.push(projectedRing); + } + features.push(createFeature(tags, type === 'Polygon' ? 3 : 2, rings, id)); + + } else if (type === 'MultiPolygon') { + rings = []; + for (i = 0; i < coords.length; i++) { + for (j = 0; j < coords[i].length; j++) { + projectedRing = project(coords[i][j], tolerance); + projectedRing.outer = (j === 0); + rings.push(projectedRing); + } + } + features.push(createFeature(tags, 3, rings, id)); + + } else if (type === 'GeometryCollection') { + for (i = 0; i < geom.geometries.length; i++) { + convertFeature(features, { + geometry: geom.geometries[i], + properties: tags + }, tolerance); + } + + } else { + throw new Error('Input data is not a valid GeoJSON object.'); + } +} + +function project(lonlats, tolerance) { + var projected = []; + for (var i = 0; i < lonlats.length; i++) { + projected.push(projectPoint(lonlats[i])); + } + if (tolerance) { + simplify(projected, tolerance); + calcSize(projected); + } + return projected; +} + +function projectPoint(p) { + var sin = Math.sin(p[1] * Math.PI / 180), + x = (p[0] / 360 + 0.5), + y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI); + + y = y < 0 ? 0 : + y > 1 ? 1 : y; + + return [x, y, 0]; +} + +// calculate area and length of the poly +function calcSize(points) { + var area = 0, + dist = 0; + + for (var i = 0, a, b; i < points.length - 1; i++) { + a = b || points[i]; + b = points[i + 1]; + + area += a[0] * b[1] - b[0] * a[1]; + + // use Manhattan distance instead of Euclidian one to avoid expensive square root computation + dist += Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1]); + } + points.area = Math.abs(area / 2); + points.dist = dist; +} + +},{"./feature":106,"./simplify":108}],106:[function(require,module,exports){ +'use strict'; + +module.exports = createFeature; + +function createFeature(tags, type, geom, id) { + var feature = { + id: id || null, + type: type, + geometry: geom, + tags: tags || null, + min: [Infinity, Infinity], // initial bbox values + max: [-Infinity, -Infinity] + }; + calcBBox(feature); + return feature; +} + +// calculate the feature bounding box for faster clipping later +function calcBBox(feature) { + var geometry = feature.geometry, + min = feature.min, + max = feature.max; + + if (feature.type === 1) { + calcRingBBox(min, max, geometry); + } else { + for (var i = 0; i < geometry.length; i++) { + calcRingBBox(min, max, geometry[i]); + } + } + + return feature; +} + +function calcRingBBox(min, max, points) { + for (var i = 0, p; i < points.length; i++) { + p = points[i]; + min[0] = Math.min(p[0], min[0]); + max[0] = Math.max(p[0], max[0]); + min[1] = Math.min(p[1], min[1]); + max[1] = Math.max(p[1], max[1]); + } +} + +},{}],107:[function(require,module,exports){ +'use strict'; + +module.exports = geojsonvt; + +var convert = require('./convert'), // GeoJSON conversion and preprocessing + transform = require('./transform'), // coordinate transformation + clip = require('./clip'), // stripe clipping algorithm + wrap = require('./wrap'), // date line processing + createTile = require('./tile'); // final simplified tile generation + + +function geojsonvt(data, options) { + return new GeoJSONVT(data, options); +} + +function GeoJSONVT(data, options) { + options = this.options = extend(Object.create(this.options), options); + + var debug = options.debug; + + if (debug) console.time('preprocess data'); + + var z2 = 1 << options.maxZoom, // 2^z + features = convert(data, options.tolerance / (z2 * options.extent)); + + this.tiles = {}; + this.tileCoords = []; + + if (debug) { + console.timeEnd('preprocess data'); + console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints); + console.time('generate tiles'); + this.stats = {}; + this.total = 0; + } + + features = wrap(features, options.buffer / options.extent, intersectX); + + // start slicing from the top tile down + if (features.length) this.splitTile(features, 0, 0, 0); + + if (debug) { + if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints); + console.timeEnd('generate tiles'); + console.log('tiles generated:', this.total, JSON.stringify(this.stats)); + } +} + +GeoJSONVT.prototype.options = { + maxZoom: 14, // max zoom to preserve detail on + indexMaxZoom: 5, // max zoom in the tile index + indexMaxPoints: 100000, // max number of points per tile in the tile index + solidChildren: false, // whether to tile solid square tiles further + tolerance: 3, // simplification tolerance (higher means simpler) + extent: 4096, // tile extent + buffer: 64, // tile buffer on each side + debug: 0 // logging level (0, 1 or 2) +}; + +GeoJSONVT.prototype.splitTile = function (features, z, x, y, cz, cx, cy) { + + var stack = [features, z, x, y], + options = this.options, + debug = options.debug, + solid = null; + + // avoid recursion by using a processing queue + while (stack.length) { + y = stack.pop(); + x = stack.pop(); + z = stack.pop(); + features = stack.pop(); + + var z2 = 1 << z, + id = toID(z, x, y), + tile = this.tiles[id], + tileTolerance = z === options.maxZoom ? 0 : options.tolerance / (z2 * options.extent); + + if (!tile) { + if (debug > 1) console.time('creation'); + + tile = this.tiles[id] = createTile(features, z2, x, y, tileTolerance, z === options.maxZoom); + this.tileCoords.push({z: z, x: x, y: y}); + + if (debug) { + if (debug > 1) { + console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)', + z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified); + console.timeEnd('creation'); + } + var key = 'z' + z; + this.stats[key] = (this.stats[key] || 0) + 1; + this.total++; + } + } + + // save reference to original geometry in tile so that we can drill down later if we stop now + tile.source = features; + + // if it's the first-pass tiling + if (!cz) { + // stop tiling if we reached max zoom, or if the tile is too simple + if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue; + + // if a drilldown to a specific tile + } else { + // stop tiling if we reached base zoom or our target tile zoom + if (z === options.maxZoom || z === cz) continue; + + // stop tiling if it's not an ancestor of the target tile + var m = 1 << (cz - z); + if (x !== Math.floor(cx / m) || y !== Math.floor(cy / m)) continue; + } + + // stop tiling if the tile is solid clipped square + if (!options.solidChildren && isClippedSquare(tile, options.extent, options.buffer)) { + if (cz) solid = z; // and remember the zoom if we're drilling down + continue; + } + + // if we slice further down, no need to keep source geometry + tile.source = null; + + if (debug > 1) console.time('clipping'); + + // values we'll use for clipping + var k1 = 0.5 * options.buffer / options.extent, + k2 = 0.5 - k1, + k3 = 0.5 + k1, + k4 = 1 + k1, + tl, bl, tr, br, left, right; + + tl = bl = tr = br = null; + + left = clip(features, z2, x - k1, x + k3, 0, intersectX, tile.min[0], tile.max[0]); + right = clip(features, z2, x + k2, x + k4, 0, intersectX, tile.min[0], tile.max[0]); + + if (left) { + tl = clip(left, z2, y - k1, y + k3, 1, intersectY, tile.min[1], tile.max[1]); + bl = clip(left, z2, y + k2, y + k4, 1, intersectY, tile.min[1], tile.max[1]); + } + + if (right) { + tr = clip(right, z2, y - k1, y + k3, 1, intersectY, tile.min[1], tile.max[1]); + br = clip(right, z2, y + k2, y + k4, 1, intersectY, tile.min[1], tile.max[1]); + } + + if (debug > 1) console.timeEnd('clipping'); + + if (features.length) { + stack.push(tl || [], z + 1, x * 2, y * 2); + stack.push(bl || [], z + 1, x * 2, y * 2 + 1); + stack.push(tr || [], z + 1, x * 2 + 1, y * 2); + stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1); + } + } + + return solid; +}; + +GeoJSONVT.prototype.getTile = function (z, x, y) { + var options = this.options, + extent = options.extent, + debug = options.debug; + + var z2 = 1 << z; + x = ((x % z2) + z2) % z2; // wrap tile x coordinate + + var id = toID(z, x, y); + if (this.tiles[id]) return transform.tile(this.tiles[id], extent); + + if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y); + + var z0 = z, + x0 = x, + y0 = y, + parent; + + while (!parent && z0 > 0) { + z0--; + x0 = Math.floor(x0 / 2); + y0 = Math.floor(y0 / 2); + parent = this.tiles[toID(z0, x0, y0)]; + } + + if (!parent || !parent.source) return null; + + // if we found a parent tile containing the original geometry, we can drill down from it + if (debug > 1) console.log('found parent tile z%d-%d-%d', z0, x0, y0); + + // it parent tile is a solid clipped square, return it instead since it's identical + if (isClippedSquare(parent, extent, options.buffer)) return transform.tile(parent, extent); + + if (debug > 1) console.time('drilling down'); + var solid = this.splitTile(parent.source, z0, x0, y0, z, x, y); + if (debug > 1) console.timeEnd('drilling down'); + + // one of the parent tiles was a solid clipped square + if (solid !== null) { + var m = 1 << (z - solid); + id = toID(solid, Math.floor(x / m), Math.floor(y / m)); + } + + return this.tiles[id] ? transform.tile(this.tiles[id], extent) : null; +}; + +function toID(z, x, y) { + return (((1 << z) * y + x) * 32) + z; +} + +function intersectX(a, b, x) { + return [x, (x - a[0]) * (b[1] - a[1]) / (b[0] - a[0]) + a[1], 1]; +} +function intersectY(a, b, y) { + return [(y - a[1]) * (b[0] - a[0]) / (b[1] - a[1]) + a[0], y, 1]; +} + +function extend(dest, src) { + for (var i in src) dest[i] = src[i]; + return dest; +} + +// checks whether a tile is a whole-area fill after clipping; if it is, there's no sense slicing it further +function isClippedSquare(tile, extent, buffer) { + + var features = tile.source; + if (features.length !== 1) return false; + + var feature = features[0]; + if (feature.type !== 3 || feature.geometry.length > 1) return false; + + var len = feature.geometry[0].length; + if (len !== 5) return false; + + for (var i = 0; i < len; i++) { + var p = transform.point(feature.geometry[0][i], extent, tile.z2, tile.x, tile.y); + if ((p[0] !== -buffer && p[0] !== extent + buffer) || + (p[1] !== -buffer && p[1] !== extent + buffer)) return false; + } + + return true; +} + +},{"./clip":104,"./convert":105,"./tile":109,"./transform":110,"./wrap":111}],108:[function(require,module,exports){ +'use strict'; + +module.exports = simplify; + +// calculate simplification data using optimized Douglas-Peucker algorithm + +function simplify(points, tolerance) { + + var sqTolerance = tolerance * tolerance, + len = points.length, + first = 0, + last = len - 1, + stack = [], + i, maxSqDist, sqDist, index; + + // always retain the endpoints (1 is the max value) + points[first][2] = 1; + points[last][2] = 1; + + // avoid recursion by using a stack + while (last) { + + maxSqDist = 0; + + for (i = first + 1; i < last; i++) { + sqDist = getSqSegDist(points[i], points[first], points[last]); + + if (sqDist > maxSqDist) { + index = i; + maxSqDist = sqDist; + } + } + + if (maxSqDist > sqTolerance) { + points[index][2] = maxSqDist; // save the point importance in squared pixels as a z coordinate + stack.push(first); + stack.push(index); + first = index; + + } else { + last = stack.pop(); + first = stack.pop(); + } + } +} + +// square distance from a point to a segment +function getSqSegDist(p, a, b) { + + var x = a[0], y = a[1], + bx = b[0], by = b[1], + px = p[0], py = p[1], + dx = bx - x, + dy = by - y; + + if (dx !== 0 || dy !== 0) { + + var t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy); + + if (t > 1) { + x = bx; + y = by; + + } else if (t > 0) { + x += dx * t; + y += dy * t; + } + } + + dx = px - x; + dy = py - y; + + return dx * dx + dy * dy; +} + +},{}],109:[function(require,module,exports){ +'use strict'; + +module.exports = createTile; + +function createTile(features, z2, tx, ty, tolerance, noSimplify) { + var tile = { + features: [], + numPoints: 0, + numSimplified: 0, + numFeatures: 0, + source: null, + x: tx, + y: ty, + z2: z2, + transformed: false, + min: [2, 1], + max: [-1, 0] + }; + for (var i = 0; i < features.length; i++) { + tile.numFeatures++; + addFeature(tile, features[i], tolerance, noSimplify); + + var min = features[i].min, + max = features[i].max; + + if (min[0] < tile.min[0]) tile.min[0] = min[0]; + if (min[1] < tile.min[1]) tile.min[1] = min[1]; + if (max[0] > tile.max[0]) tile.max[0] = max[0]; + if (max[1] > tile.max[1]) tile.max[1] = max[1]; + } + return tile; +} + +function addFeature(tile, feature, tolerance, noSimplify) { + + var geom = feature.geometry, + type = feature.type, + simplified = [], + sqTolerance = tolerance * tolerance, + i, j, ring, p; + + if (type === 1) { + for (i = 0; i < geom.length; i++) { + simplified.push(geom[i]); + tile.numPoints++; + tile.numSimplified++; + } + + } else { + + // simplify and transform projected coordinates for tile geometry + for (i = 0; i < geom.length; i++) { + ring = geom[i]; + + // filter out tiny polylines & polygons + if (!noSimplify && ((type === 2 && ring.dist < tolerance) || + (type === 3 && ring.area < sqTolerance))) { + tile.numPoints += ring.length; + continue; + } + + var simplifiedRing = []; + + for (j = 0; j < ring.length; j++) { + p = ring[j]; + // keep points with importance > tolerance + if (noSimplify || p[2] > sqTolerance) { + simplifiedRing.push(p); + tile.numSimplified++; + } + tile.numPoints++; + } + + if (type === 3) rewind(simplifiedRing, ring.outer); + + simplified.push(simplifiedRing); + } + } + + if (simplified.length) { + var tileFeature = { + geometry: simplified, + type: type, + tags: feature.tags || null + }; + if (feature.id !== null) { + tileFeature.id = feature.id; + } + tile.features.push(tileFeature); + } +} + +function rewind(ring, clockwise) { + var area = signedArea(ring); + if (area < 0 === clockwise) ring.reverse(); +} + +function signedArea(ring) { + var sum = 0; + for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) { + p1 = ring[i]; + p2 = ring[j]; + sum += (p2[0] - p1[0]) * (p1[1] + p2[1]); + } + return sum; +} + +},{}],110:[function(require,module,exports){ +'use strict'; + +exports.tile = transformTile; +exports.point = transformPoint; + +// Transforms the coordinates of each feature in the given tile from +// mercator-projected space into (extent x extent) tile space. +function transformTile(tile, extent) { + if (tile.transformed) return tile; + + var z2 = tile.z2, + tx = tile.x, + ty = tile.y, + i, j, k; + + for (i = 0; i < tile.features.length; i++) { + var feature = tile.features[i], + geom = feature.geometry, + type = feature.type; + + if (type === 1) { + for (j = 0; j < geom.length; j++) geom[j] = transformPoint(geom[j], extent, z2, tx, ty); + + } else { + for (j = 0; j < geom.length; j++) { + var ring = geom[j]; + for (k = 0; k < ring.length; k++) ring[k] = transformPoint(ring[k], extent, z2, tx, ty); } } } - var src = '(' + bundleFn + ')({' - + Object.keys(workerSources).map(function (key) { - return stringify(key) + ':[' - + sources[key][0] - + ',' + stringify(sources[key][1]) + ']' - ; - }).join(',') - + '},{},[' + stringify(skey) + '])' - ; + tile.transformed = true; - var URL = window.URL || window.webkitURL || window.mozURL || window.msURL; + return tile; +} - var blob = new Blob([src], { type: 'text/javascript' }); - if (options && options.bare) { return blob; } - var workerUrl = URL.createObjectURL(blob); - var worker = new Worker(workerUrl); - worker.objectURL = workerUrl; - return worker; +function transformPoint(p, extent, z2, tx, ty) { + var x = Math.round(extent * (p[0] * z2 - tx)), + y = Math.round(extent * (p[1] * z2 - ty)); + return [x, y]; +} + +},{}],111:[function(require,module,exports){ +'use strict'; + +var clip = require('./clip'); +var createFeature = require('./feature'); + +module.exports = wrap; + +function wrap(features, buffer, intersectX) { + var merged = features, + left = clip(features, 1, -1 - buffer, buffer, 0, intersectX, -1, 2), // left world copy + right = clip(features, 1, 1 - buffer, 2 + buffer, 0, intersectX, -1, 2); // right world copy + + if (left || right) { + merged = clip(features, 1, -buffer, 1 + buffer, 0, intersectX, -1, 2) || []; // center world copy + + if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center + if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center + } + + return merged; +} + +function shiftFeatureCoords(features, offset) { + var newFeatures = []; + + for (var i = 0; i < features.length; i++) { + var feature = features[i], + type = feature.type; + + var newGeometry; + + if (type === 1) { + newGeometry = shiftCoords(feature.geometry, offset); + } else { + newGeometry = []; + for (var j = 0; j < feature.geometry.length; j++) { + newGeometry.push(shiftCoords(feature.geometry[j], offset)); + } + } + + newFeatures.push(createFeature(feature.tags, type, newGeometry, feature.id)); + } + + return newFeatures; +} + +function shiftCoords(points, offset) { + var newPoints = []; + newPoints.area = points.area; + newPoints.dist = points.dist; + + for (var i = 0; i < points.length; i++) { + newPoints.push([points[i][0] + offset, points[i][1], points[i][2]]); + } + return newPoints; +} + +},{"./clip":104,"./feature":106}],112:[function(require,module,exports){ +module.exports = getCanvasContext +function getCanvasContext (type, opts) { + if (typeof type !== 'string') { + throw new TypeError('must specify type string') + } + + opts = opts || {} + + if (typeof document === 'undefined' && !opts.canvas) { + return null // check for Node + } + + var canvas = opts.canvas || document.createElement('canvas') + if (typeof opts.width === 'number') { + canvas.width = opts.width + } + if (typeof opts.height === 'number') { + canvas.height = opts.height + } + + var attribs = opts + var gl + try { + var names = [ type ] + // prefix GL contexts + if (type.indexOf('webgl') === 0) { + names.push('experimental-' + type) + } + + for (var i = 0; i < names.length; i++) { + gl = canvas.getContext(names[i], attribs) + if (gl) return gl + } + } catch (e) { + gl = null + } + return (gl || null) // ensure null on fail +} + +},{}],113:[function(require,module,exports){ +(function (Buffer,process){ +'use strict' + +var path = require('path') +var ndarray = require('ndarray') +var GifReader = require('omggif').GifReader +var pack = require('ndarray-pack') +var through = require('through') +var parseDataURI = require('data-uri-to-buffer') + +function defaultImage(url, cb) { + var img = new Image() + img.crossOrigin = "Anonymous" + img.onload = function() { + var canvas = document.createElement('canvas') + canvas.width = img.width + canvas.height = img.height + var context = canvas.getContext('2d') + context.drawImage(img, 0, 0) + var pixels = context.getImageData(0, 0, img.width, img.height) + cb(null, ndarray(new Uint8Array(pixels.data), [img.width, img.height, 4], [4, 4*img.width, 1], 0)) + } + img.onerror = function(err) { + cb(err) + } + img.src = url +} + +//Animated gif loading +function handleGif(data, cb) { + var reader + try { + reader = new GifReader(data) + } catch(err) { + cb(err) + return + } + if(reader.numFrames() > 0) { + var nshape = [reader.numFrames(), reader.height, reader.width, 4] + var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3]) + var result = ndarray(ndata, nshape) + try { + for(var i=0; i= 0) this.dispose = disposalCode; +}; + +/* + Sets the number of times the set of GIF frames should be played. + + -1 = play once + 0 = repeat indefinitely + + Default is -1 + + Must be invoked before the first image is added +*/ + +GIFEncoder.prototype.setRepeat = function(repeat) { + this.repeat = repeat; +}; + +/* + Sets the transparent color for the last added frame and any subsequent + frames. Since all colors are subject to modification in the quantization + process, the color in the final palette for each frame closest to the given + color becomes the transparent color for that frame. May be set to null to + indicate no transparent color. +*/ +GIFEncoder.prototype.setTransparent = function(color) { + this.transparent = color; +}; + +// Custom methods for performance hacks around streaming GIF data pieces without re-analyzing/loading +GIFEncoder.prototype.analyzeImage = function (imageData) { + // convert to correct format if necessary + this.setImagePixels(this.removeAlphaChannel(imageData)); + this.analyzePixels(); // build color table & map pixels +}; + +GIFEncoder.prototype.writeImageInfo = function () { + if (this.firstFrame) { + this.writeLSD(); // logical screen descriptior + this.writePalette(); // global color table + if (this.repeat >= 0) { + // use NS app extension to indicate reps + this.writeNetscapeExt(); + } + } + + this.writeGraphicCtrlExt(); // write graphic control extension + this.writeImageDesc(); // image descriptor + if (!this.firstFrame) this.writePalette(); // local color table + + // DEV: This was originally after outputImage but it does not affect order it seems + this.firstFrame = false; +}; + +GIFEncoder.prototype.outputImage = function () { + this.writePixels(); // encode and write pixel data +}; + +/* + Adds next GIF frame. The frame is not written immediately, but is + actually deferred until the next frame is received so that timing + data can be inserted. Invoking finish() flushes all frames. +*/ +GIFEncoder.prototype.addFrame = function(imageData) { + this.emit('frame#start'); + + this.analyzeImage(imageData); + this.writeImageInfo(); + this.outputImage(); + + this.emit('frame#stop'); +}; + +/* + Adds final trailer to the GIF stream, if you don't call the finish method + the GIF stream will not be valid. +*/ +GIFEncoder.prototype.finish = function() { + this.emit('finish#start'); + this.writeByte(0x3b); // gif trailer + this.emit('finish#stop'); +}; + +/* + Sets quality of color quantization (conversion of images to the maximum 256 + colors allowed by the GIF specification). Lower values (minimum = 1) + produce better colors, but slow processing significantly. 10 is the + default, and produces good color mapping at reasonable speeds. Values + greater than 20 do not yield significant improvements in speed. +*/ +GIFEncoder.prototype.setQuality = function(quality) { + if (quality < 1) quality = 1; + this.sample = quality; +}; + +/* + Writes GIF file header +*/ +GIFEncoder.prototype.writeHeader = function() { + this.emit('writeHeader#start'); + this.writeUTFBytes("GIF89a"); + this.emit('writeHeader#stop'); +}; + +/* + Analyzes current frame colors and creates color map. +*/ +GIFEncoder.prototype.analyzePixels = function() { + var len = this.pixels.length; + var nPix = len / 3; + + // TODO: Re-use indexedPixels + this.indexedPixels = new Uint8Array(nPix); + + var imgq = new NeuQuant(this.pixels, this.sample); + imgq.buildColormap(); // create reduced palette + this.colorTab = imgq.getColormap(); + + // map image pixels to new palette + var k = 0; + for (var j = 0; j < nPix; j++) { + var index = imgq.lookupRGB( + this.pixels[k++] & 0xff, + this.pixels[k++] & 0xff, + this.pixels[k++] & 0xff + ); + this.usedEntry[index] = true; + this.indexedPixels[j] = index; + } + + this.pixels = null; + this.colorDepth = 8; + this.palSize = 7; + + // get closest match to transparent color if specified + if (this.transparent !== null) { + this.transIndex = this.findClosest(this.transparent); + } +}; + +/* + Returns index of palette color closest to c +*/ +GIFEncoder.prototype.findClosest = function(c) { + if (this.colorTab === null) return -1; + + var r = (c & 0xFF0000) >> 16; + var g = (c & 0x00FF00) >> 8; + var b = (c & 0x0000FF); + var minpos = 0; + var dmin = 256 * 256 * 256; + var len = this.colorTab.length; + + for (var i = 0; i < len;) { + var dr = r - (this.colorTab[i++] & 0xff); + var dg = g - (this.colorTab[i++] & 0xff); + var db = b - (this.colorTab[i] & 0xff); + var d = dr * dr + dg * dg + db * db; + var index = i / 3; + if (this.usedEntry[index] && (d < dmin)) { + dmin = d; + minpos = index; + } + i++; + } + + return minpos; +}; + +/* + Extracts image pixels into byte array pixels + (removes alphachannel from canvas imagedata) +*/ +GIFEncoder.prototype.removeAlphaChannel = function (data) { + var w = this.width; + var h = this.height; + var pixels = new Uint8Array(w * h * 3); + + var count = 0; + + for (var i = 0; i < h; i++) { + for (var j = 0; j < w; j++) { + var b = (i * w * 4) + j * 4; + pixels[count++] = data[b]; + pixels[count++] = data[b+1]; + pixels[count++] = data[b+2]; + } + } + + return pixels; +}; + +GIFEncoder.prototype.setImagePixels = function(pixels) { + this.pixels = pixels; +}; + +/* + Writes Graphic Control Extension +*/ +GIFEncoder.prototype.writeGraphicCtrlExt = function() { + this.writeByte(0x21); // extension introducer + this.writeByte(0xf9); // GCE label + this.writeByte(4); // data block size + + var transp, disp; + if (this.transparent === null) { + transp = 0; + disp = 0; // dispose = no action + } else { + transp = 1; + disp = 2; // force clear if using transparent color + } + + if (this.dispose >= 0) { + disp = dispose & 7; // user override + } + disp <<= 2; + + // packed fields + this.writeByte( + 0 | // 1:3 reserved + disp | // 4:6 disposal + 0 | // 7 user input - 0 = none + transp // 8 transparency flag + ); + + this.writeShort(this.delay); // delay x 1/100 sec + this.writeByte(this.transIndex); // transparent color index + this.writeByte(0); // block terminator +}; + +/* + Writes Image Descriptor +*/ +GIFEncoder.prototype.writeImageDesc = function() { + this.writeByte(0x2c); // image separator + this.writeShort(0); // image position x,y = 0,0 + this.writeShort(0); + this.writeShort(this.width); // image size + this.writeShort(this.height); + + // packed fields + if (this.firstFrame) { + // no LCT - GCT is used for first (or only) frame + this.writeByte(0); + } else { + // specify normal LCT + this.writeByte( + 0x80 | // 1 local color table 1=yes + 0 | // 2 interlace - 0=no + 0 | // 3 sorted - 0=no + 0 | // 4-5 reserved + this.palSize // 6-8 size of color table + ); + } +}; + +/* + Writes Logical Screen Descriptor +*/ +GIFEncoder.prototype.writeLSD = function() { + // logical screen size + this.writeShort(this.width); + this.writeShort(this.height); + + // packed fields + this.writeByte( + 0x80 | // 1 : global color table flag = 1 (gct used) + 0x70 | // 2-4 : color resolution = 7 + 0x00 | // 5 : gct sort flag = 0 + this.palSize // 6-8 : gct size + ); + + this.writeByte(0); // background color index + this.writeByte(0); // pixel aspect ratio - assume 1:1 +}; + +/* + Writes Netscape application extension to define repeat count. +*/ +GIFEncoder.prototype.writeNetscapeExt = function() { + this.writeByte(0x21); // extension introducer + this.writeByte(0xff); // app extension label + this.writeByte(11); // block size + this.writeUTFBytes('NETSCAPE2.0'); // app id + auth code + this.writeByte(3); // sub-block size + this.writeByte(1); // loop sub-block id + this.writeShort(this.repeat); // loop count (extra iterations, 0=repeat forever) + this.writeByte(0); // block terminator +}; + +/* + Writes color table +*/ +GIFEncoder.prototype.writePalette = function() { + this.writeBytes(this.colorTab); + var n = (3 * 256) - this.colorTab.length; + for (var i = 0; i < n; i++) + this.writeByte(0); +}; + +GIFEncoder.prototype.writeShort = function(pValue) { + this.writeByte(pValue & 0xFF); + this.writeByte((pValue >> 8) & 0xFF); +}; + +/* + Encodes and writes pixel data +*/ +GIFEncoder.prototype.writePixels = function() { + var enc = new LZWEncoder(this.width, this.height, this.indexedPixels, this.colorDepth); + enc.encode(this); +}; + +/* + Retrieves the GIF stream +*/ +GIFEncoder.prototype.stream = function() { + return this; +}; + +GIFEncoder.ByteCapacitor = ByteCapacitor; + +module.exports = GIFEncoder; + +}).call(this,require("buffer").Buffer) +},{"./LZWEncoder.js":115,"./TypedNeuQuant.js":116,"assert":9,"buffer":46,"events":95,"readable-stream":123,"util":1001}],115:[function(require,module,exports){ +/* + LZWEncoder.js + + Authors + Kevin Weiner (original Java version - kweiner@fmsware.com) + Thibault Imbert (AS3 version - bytearray.org) + Johan Nordberg (JS version - code@johan-nordberg.com) + + Acknowledgements + GIFCOMPR.C - GIF Image compression routines + Lempel-Ziv compression based on 'compress'. GIF modifications by + David Rowley (mgardi@watdcsu.waterloo.edu) + GIF Image compression - modified 'compress' + Based on: compress.c - File compression ala IEEE Computer, June 1984. + By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) + Jim McKie (decvax!mcvax!jim) + Steve Davies (decvax!vax135!petsd!peora!srd) + Ken Turkowski (decvax!decwrl!turtlevax!ken) + James A. Woods (decvax!ihnp4!ames!jaw) + Joe Orost (decvax!vax135!petsd!joe) +*/ + +var EOF = -1; +var BITS = 12; +var HSIZE = 5003; // 80% occupancy +var masks = [0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, + 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, + 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF]; + +function LZWEncoder(width, height, pixels, colorDepth) { + var initCodeSize = Math.max(2, colorDepth); + + var accum = new Uint8Array(256); + var htab = new Int32Array(HSIZE); + var codetab = new Int32Array(HSIZE); + + var cur_accum, cur_bits = 0; + var a_count; + var free_ent = 0; // first unused entry + var maxcode; + var remaining; + var curPixel; + var n_bits; + + // block compression parameters -- after all codes are used up, + // and compression rate changes, start over. + var clear_flg = false; + + // Algorithm: use open addressing double hashing (no chaining) on the + // prefix code / next character combination. We do a variant of Knuth's + // algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime + // secondary probe. Here, the modular division first probe is gives way + // to a faster exclusive-or manipulation. Also do block compression with + // an adaptive reset, whereby the code table is cleared when the compression + // ratio decreases, but after the table fills. The variable-length output + // codes are re-sized at this point, and a special CLEAR code is generated + // for the decompressor. Late addition: construct the table according to + // file size for noticeable speed improvement on small files. Please direct + // questions about this implementation to ames!jaw. + var g_init_bits, ClearCode, EOFCode; + + // Add a character to the end of the current packet, and if it is 254 + // characters, flush the packet to disk. + function char_out(c, outs) { + accum[a_count++] = c; + if (a_count >= 254) flush_char(outs); + } + + // Clear out the hash table + // table clear for block compress + function cl_block(outs) { + cl_hash(HSIZE); + free_ent = ClearCode + 2; + clear_flg = true; + output(ClearCode, outs); + } + + // Reset code table + function cl_hash(hsize) { + for (var i = 0; i < hsize; ++i) htab[i] = -1; + } + + function compress(init_bits, outs) { + var fcode, c, i, ent, disp, hsize_reg, hshift; + + // Set up the globals: g_init_bits - initial number of bits + g_init_bits = init_bits; + + // Set up the necessary values + clear_flg = false; + n_bits = g_init_bits; + maxcode = MAXCODE(n_bits); + + ClearCode = 1 << (init_bits - 1); + EOFCode = ClearCode + 1; + free_ent = ClearCode + 2; + + a_count = 0; // clear packet + + ent = nextPixel(); + + hshift = 0; + for (fcode = HSIZE; fcode < 65536; fcode *= 2) ++hshift; + hshift = 8 - hshift; // set hash code range bound + hsize_reg = HSIZE; + cl_hash(hsize_reg); // clear hash table + + output(ClearCode, outs); + + outer_loop: while ((c = nextPixel()) != EOF) { + fcode = (c << BITS) + ent; + i = (c << hshift) ^ ent; // xor hashing + if (htab[i] === fcode) { + ent = codetab[i]; + continue; + } else if (htab[i] >= 0) { // non-empty slot + disp = hsize_reg - i; // secondary hash (after G. Knott) + if (i === 0) disp = 1; + do { + if ((i -= disp) < 0) i += hsize_reg; + if (htab[i] === fcode) { + ent = codetab[i]; + continue outer_loop; + } + } while (htab[i] >= 0); + } + output(ent, outs); + ent = c; + if (free_ent < 1 << BITS) { + codetab[i] = free_ent++; // code -> hashtable + htab[i] = fcode; + } else { + cl_block(outs); + } + } + + // Put out the final code. + output(ent, outs); + output(EOFCode, outs); + } + + function encode(outs) { + outs.writeByte(initCodeSize); // write "initial code size" byte + remaining = width * height; // reset navigation variables + curPixel = 0; + compress(initCodeSize + 1, outs); // compress and write the pixel data + outs.writeByte(0); // write block terminator + } + + // Flush the packet to disk, and reset the accumulator + function flush_char(outs) { + if (a_count > 0) { + outs.writeByte(a_count); + outs.writeBytes(accum, 0, a_count); + a_count = 0; + } + } + + function MAXCODE(n_bits) { + return (1 << n_bits) - 1; + } + + // Return the next pixel from the image + function nextPixel() { + if (remaining === 0) return EOF; + --remaining; + var pix = pixels[curPixel++]; + return pix & 0xff; + } + + function output(code, outs) { + cur_accum &= masks[cur_bits]; + + if (cur_bits > 0) cur_accum |= (code << cur_bits); + else cur_accum = code; + + cur_bits += n_bits; + + while (cur_bits >= 8) { + char_out((cur_accum & 0xff), outs); + cur_accum >>= 8; + cur_bits -= 8; + } + + // If the next entry is going to be too big for the code size, + // then increase it, if possible. + if (free_ent > maxcode || clear_flg) { + if (clear_flg) { + maxcode = MAXCODE(n_bits = g_init_bits); + clear_flg = false; + } else { + ++n_bits; + if (n_bits == BITS) maxcode = 1 << BITS; + else maxcode = MAXCODE(n_bits); + } + } + + if (code == EOFCode) { + // At EOF, write the rest of the buffer. + while (cur_bits > 0) { + char_out((cur_accum & 0xff), outs); + cur_accum >>= 8; + cur_bits -= 8; + } + flush_char(outs); + } + } + + this.encode = encode; +} + +module.exports = LZWEncoder; + +},{}],116:[function(require,module,exports){ +/* NeuQuant Neural-Net Quantization Algorithm + * ------------------------------------------ + * + * Copyright (c) 1994 Anthony Dekker + * + * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. + * See "Kohonen neural networks for optimal colour quantization" + * in "Network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367. + * for a discussion of the algorithm. + * See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML + * + * Any party obtaining a copy of these files from the author, directly or + * indirectly, is granted, free of charge, a full and unrestricted irrevocable, + * world-wide, paid up, royalty-free, nonexclusive right and license to deal + * in this software and documentation files (the "Software"), including without + * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons who receive + * copies from any such party to do so, with the only requirement being + * that this copyright notice remain intact. + * + * (JavaScript port 2012 by Johan Nordberg) + */ + +var ncycles = 100; // number of learning cycles +var netsize = 256; // number of colors used +var maxnetpos = netsize - 1; + +// defs for freq and bias +var netbiasshift = 4; // bias for colour values +var intbiasshift = 16; // bias for fractions +var intbias = (1 << intbiasshift); +var gammashift = 10; +var gamma = (1 << gammashift); +var betashift = 10; +var beta = (intbias >> betashift); /* beta = 1/1024 */ +var betagamma = (intbias << (gammashift - betashift)); + +// defs for decreasing radius factor +var initrad = (netsize >> 3); // for 256 cols, radius starts +var radiusbiasshift = 6; // at 32.0 biased by 6 bits +var radiusbias = (1 << radiusbiasshift); +var initradius = (initrad * radiusbias); //and decreases by a +var radiusdec = 30; // factor of 1/30 each cycle + +// defs for decreasing alpha factor +var alphabiasshift = 10; // alpha starts at 1.0 +var initalpha = (1 << alphabiasshift); +var alphadec; // biased by 10 bits + +/* radbias and alpharadbias used for radpower calculation */ +var radbiasshift = 8; +var radbias = (1 << radbiasshift); +var alpharadbshift = (alphabiasshift + radbiasshift); +var alpharadbias = (1 << alpharadbshift); + +// four primes near 500 - assume no image has a length so large that it is +// divisible by all four primes +var prime1 = 499; +var prime2 = 491; +var prime3 = 487; +var prime4 = 503; +var minpicturebytes = (3 * prime4); + +/* + Constructor: NeuQuant + + Arguments: + + pixels - array of pixels in RGB format + samplefac - sampling factor 1 to 30 where lower is better quality + + > + > pixels = [r, g, b, r, g, b, r, g, b, ..] + > +*/ +function NeuQuant(pixels, samplefac) { + var network; // int[netsize][4] + var netindex; // for network lookup - really 256 + + // bias and freq arrays for learning + var bias; + var freq; + var radpower; + + /* + Private Method: init + + sets up arrays + */ + function init() { + network = []; + netindex = new Int32Array(256); + bias = new Int32Array(netsize); + freq = new Int32Array(netsize); + radpower = new Int32Array(netsize >> 3); + + var i, v; + for (i = 0; i < netsize; i++) { + v = (i << (netbiasshift + 8)) / netsize; + network[i] = new Float64Array([v, v, v, 0]); + //network[i] = [v, v, v, 0] + freq[i] = intbias / netsize; + bias[i] = 0; + } + } + + /* + Private Method: unbiasnet + + unbiases network to give byte values 0..255 and record position i to prepare for sort + */ + function unbiasnet() { + for (var i = 0; i < netsize; i++) { + network[i][0] >>= netbiasshift; + network[i][1] >>= netbiasshift; + network[i][2] >>= netbiasshift; + network[i][3] = i; // record color number + } + } + + /* + Private Method: altersingle + + moves neuron *i* towards biased (b,g,r) by factor *alpha* + */ + function altersingle(alpha, i, b, g, r) { + network[i][0] -= (alpha * (network[i][0] - b)) / initalpha; + network[i][1] -= (alpha * (network[i][1] - g)) / initalpha; + network[i][2] -= (alpha * (network[i][2] - r)) / initalpha; + } + + /* + Private Method: alterneigh + + moves neurons in *radius* around index *i* towards biased (b,g,r) by factor *alpha* + */ + function alterneigh(radius, i, b, g, r) { + var lo = Math.abs(i - radius); + var hi = Math.min(i + radius, netsize); + + var j = i + 1; + var k = i - 1; + var m = 1; + + var p, a; + while ((j < hi) || (k > lo)) { + a = radpower[m++]; + + if (j < hi) { + p = network[j++]; + p[0] -= (a * (p[0] - b)) / alpharadbias; + p[1] -= (a * (p[1] - g)) / alpharadbias; + p[2] -= (a * (p[2] - r)) / alpharadbias; + } + + if (k > lo) { + p = network[k--]; + p[0] -= (a * (p[0] - b)) / alpharadbias; + p[1] -= (a * (p[1] - g)) / alpharadbias; + p[2] -= (a * (p[2] - r)) / alpharadbias; + } + } + } + + /* + Private Method: contest + + searches for biased BGR values + */ + function contest(b, g, r) { + /* + finds closest neuron (min dist) and updates freq + finds best neuron (min dist-bias) and returns position + for frequently chosen neurons, freq[i] is high and bias[i] is negative + bias[i] = gamma * ((1 / netsize) - freq[i]) + */ + + var bestd = ~(1 << 31); + var bestbiasd = bestd; + var bestpos = -1; + var bestbiaspos = bestpos; + + var i, n, dist, biasdist, betafreq; + for (i = 0; i < netsize; i++) { + n = network[i]; + + dist = Math.abs(n[0] - b) + Math.abs(n[1] - g) + Math.abs(n[2] - r); + if (dist < bestd) { + bestd = dist; + bestpos = i; + } + + biasdist = dist - ((bias[i]) >> (intbiasshift - netbiasshift)); + if (biasdist < bestbiasd) { + bestbiasd = biasdist; + bestbiaspos = i; + } + + betafreq = (freq[i] >> betashift); + freq[i] -= betafreq; + bias[i] += (betafreq << gammashift); + } + + freq[bestpos] += beta; + bias[bestpos] -= betagamma; + + return bestbiaspos; + } + + /* + Private Method: inxbuild + + sorts network and builds netindex[0..255] + */ + function inxbuild() { + var i, j, p, q, smallpos, smallval, previouscol = 0, startpos = 0; + for (i = 0; i < netsize; i++) { + p = network[i]; + smallpos = i; + smallval = p[1]; // index on g + // find smallest in i..netsize-1 + for (j = i + 1; j < netsize; j++) { + q = network[j]; + if (q[1] < smallval) { // index on g + smallpos = j; + smallval = q[1]; // index on g + } + } + q = network[smallpos]; + // swap p (i) and q (smallpos) entries + if (i != smallpos) { + j = q[0]; q[0] = p[0]; p[0] = j; + j = q[1]; q[1] = p[1]; p[1] = j; + j = q[2]; q[2] = p[2]; p[2] = j; + j = q[3]; q[3] = p[3]; p[3] = j; + } + // smallval entry is now in position i + + if (smallval != previouscol) { + netindex[previouscol] = (startpos + i) >> 1; + for (j = previouscol + 1; j < smallval; j++) + netindex[j] = i; + previouscol = smallval; + startpos = i; + } + } + netindex[previouscol] = (startpos + maxnetpos) >> 1; + for (j = previouscol + 1; j < 256; j++) + netindex[j] = maxnetpos; // really 256 + } + + /* + Private Method: inxsearch + + searches for BGR values 0..255 and returns a color index + */ + function inxsearch(b, g, r) { + var a, p, dist; + + var bestd = 1000; // biggest possible dist is 256*3 + var best = -1; + + var i = netindex[g]; // index on g + var j = i - 1; // start at netindex[g] and work outwards + + while ((i < netsize) || (j >= 0)) { + if (i < netsize) { + p = network[i]; + dist = p[1] - g; // inx key + if (dist >= bestd) i = netsize; // stop iter + else { + i++; + if (dist < 0) dist = -dist; + a = p[0] - b; if (a < 0) a = -a; + dist += a; + if (dist < bestd) { + a = p[2] - r; if (a < 0) a = -a; + dist += a; + if (dist < bestd) { + bestd = dist; + best = p[3]; + } + } + } + } + if (j >= 0) { + p = network[j]; + dist = g - p[1]; // inx key - reverse dif + if (dist >= bestd) j = -1; // stop iter + else { + j--; + if (dist < 0) dist = -dist; + a = p[0] - b; if (a < 0) a = -a; + dist += a; + if (dist < bestd) { + a = p[2] - r; if (a < 0) a = -a; + dist += a; + if (dist < bestd) { + bestd = dist; + best = p[3]; + } + } + } + } + } + + return best; + } + + /* + Private Method: learn + + "Main Learning Loop" + */ + function learn() { + var i; + + var lengthcount = pixels.length; + var alphadec = 30 + ((samplefac - 1) / 3); + var samplepixels = lengthcount / (3 * samplefac); + var delta = ~~(samplepixels / ncycles); + var alpha = initalpha; + var radius = initradius; + + var rad = radius >> radiusbiasshift; + + if (rad <= 1) rad = 0; + for (i = 0; i < rad; i++) + radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad)); + + var step; + if (lengthcount < minpicturebytes) { + samplefac = 1; + step = 3; + } else if ((lengthcount % prime1) !== 0) { + step = 3 * prime1; + } else if ((lengthcount % prime2) !== 0) { + step = 3 * prime2; + } else if ((lengthcount % prime3) !== 0) { + step = 3 * prime3; + } else { + step = 3 * prime4; + } + + var b, g, r, j; + var pix = 0; // current pixel + + i = 0; + while (i < samplepixels) { + b = (pixels[pix] & 0xff) << netbiasshift; + g = (pixels[pix + 1] & 0xff) << netbiasshift; + r = (pixels[pix + 2] & 0xff) << netbiasshift; + + j = contest(b, g, r); + + altersingle(alpha, j, b, g, r); + if (rad !== 0) alterneigh(rad, j, b, g, r); // alter neighbours + + pix += step; + if (pix >= lengthcount) pix -= lengthcount; + + i++; + + if (delta === 0) delta = 1; + if (i % delta === 0) { + alpha -= alpha / alphadec; + radius -= radius / radiusdec; + rad = radius >> radiusbiasshift; + + if (rad <= 1) rad = 0; + for (j = 0; j < rad; j++) + radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad)); + } + } + } + + /* + Method: buildColormap + + 1. initializes network + 2. trains it + 3. removes misconceptions + 4. builds colorindex + */ + function buildColormap() { + init(); + learn(); + unbiasnet(); + inxbuild(); + } + this.buildColormap = buildColormap; + + /* + Method: getColormap + + builds colormap from the index + + returns array in the format: + + > + > [r, g, b, r, g, b, r, g, b, ..] + > + */ + function getColormap() { + var map = []; + var index = []; + + for (var i = 0; i < netsize; i++) + index[network[i][3]] = i; + + var k = 0; + for (var l = 0; l < netsize; l++) { + var j = index[l]; + map[k++] = (network[j][0]); + map[k++] = (network[j][1]); + map[k++] = (network[j][2]); + } + return map; + } + this.getColormap = getColormap; + + /* + Method: lookupRGB + + looks for the closest *r*, *g*, *b* color in the map and + returns its index + */ + this.lookupRGB = inxsearch; +} + +module.exports = NeuQuant; + +},{}],117:[function(require,module,exports){ +arguments[4][67][0].apply(exports,arguments) +},{"dup":67}],118:[function(require,module,exports){ +arguments[4][68][0].apply(exports,arguments) +},{"./_stream_readable":120,"./_stream_writable":122,"_process":923,"core-util-is":78,"dup":68,"inherits":266}],119:[function(require,module,exports){ +arguments[4][69][0].apply(exports,arguments) +},{"./_stream_transform":121,"core-util-is":78,"dup":69,"inherits":266}],120:[function(require,module,exports){ +(function (process){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +module.exports = Readable; + +/**/ +var isArray = require('isarray'); +/**/ + + +/**/ +var Buffer = require('buffer').Buffer; +/**/ + +Readable.ReadableState = ReadableState; + +var EE = require('events').EventEmitter; + +/**/ +if (!EE.listenerCount) EE.listenerCount = function(emitter, type) { + return emitter.listeners(type).length; +}; +/**/ + +var Stream = require('stream'); + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +var StringDecoder; + + +/**/ +var debug = require('util'); +if (debug && debug.debuglog) { + debug = debug.debuglog('stream'); +} else { + debug = function () {}; +} +/**/ + + +util.inherits(Readable, Stream); + +function ReadableState(options, stream) { + var Duplex = require('./_stream_duplex'); + + options = options || {}; + + // the point at which it stops calling _read() to fill the buffer + // Note: 0 is a valid value, means "don't call _read preemptively ever" + var hwm = options.highWaterMark; + var defaultHwm = options.objectMode ? 16 : 16 * 1024; + this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; + + // cast to ints. + this.highWaterMark = ~~this.highWaterMark; + + this.buffer = []; + this.length = 0; + this.pipes = null; + this.pipesCount = 0; + this.flowing = null; + this.ended = false; + this.endEmitted = false; + this.reading = false; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, because any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + + // whenever we return null, then we set a flag to say + // that we're awaiting a 'readable' event emission. + this.needReadable = false; + this.emittedReadable = false; + this.readableListening = false; + + + // object stream flag. Used to make read(n) ignore n and to + // make all the buffer merging and length checks go away + this.objectMode = !!options.objectMode; + + if (stream instanceof Duplex) + this.objectMode = this.objectMode || !!options.readableObjectMode; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // when piping, we only care about 'readable' events that happen + // after read()ing all the bytes and not getting any pushback. + this.ranOut = false; + + // the number of writers that are awaiting a drain event in .pipe()s + this.awaitDrain = 0; + + // if true, a maybeReadMore has been scheduled + this.readingMore = false; + + this.decoder = null; + this.encoding = null; + if (options.encoding) { + if (!StringDecoder) + StringDecoder = require('string_decoder/').StringDecoder; + this.decoder = new StringDecoder(options.encoding); + this.encoding = options.encoding; + } +} + +function Readable(options) { + var Duplex = require('./_stream_duplex'); + + if (!(this instanceof Readable)) + return new Readable(options); + + this._readableState = new ReadableState(options, this); + + // legacy + this.readable = true; + + Stream.call(this); +} + +// Manually shove something into the read() buffer. +// This returns true if the highWaterMark has not been hit yet, +// similar to how Writable.write() returns true if you should +// write() some more. +Readable.prototype.push = function(chunk, encoding) { + var state = this._readableState; + + if (util.isString(chunk) && !state.objectMode) { + encoding = encoding || state.defaultEncoding; + if (encoding !== state.encoding) { + chunk = new Buffer(chunk, encoding); + encoding = ''; + } + } + + return readableAddChunk(this, state, chunk, encoding, false); +}; + +// Unshift should *always* be something directly out of read() +Readable.prototype.unshift = function(chunk) { + var state = this._readableState; + return readableAddChunk(this, state, chunk, '', true); +}; + +function readableAddChunk(stream, state, chunk, encoding, addToFront) { + var er = chunkInvalid(state, chunk); + if (er) { + stream.emit('error', er); + } else if (util.isNullOrUndefined(chunk)) { + state.reading = false; + if (!state.ended) + onEofChunk(stream, state); + } else if (state.objectMode || chunk && chunk.length > 0) { + if (state.ended && !addToFront) { + var e = new Error('stream.push() after EOF'); + stream.emit('error', e); + } else if (state.endEmitted && addToFront) { + var e = new Error('stream.unshift() after end event'); + stream.emit('error', e); + } else { + if (state.decoder && !addToFront && !encoding) + chunk = state.decoder.write(chunk); + + if (!addToFront) + state.reading = false; + + // if we want the data now, just emit it. + if (state.flowing && state.length === 0 && !state.sync) { + stream.emit('data', chunk); + stream.read(0); + } else { + // update the buffer info. + state.length += state.objectMode ? 1 : chunk.length; + if (addToFront) + state.buffer.unshift(chunk); + else + state.buffer.push(chunk); + + if (state.needReadable) + emitReadable(stream); + } + + maybeReadMore(stream, state); + } + } else if (!addToFront) { + state.reading = false; + } + + return needMoreData(state); +} + + + +// if it's past the high water mark, we can push in some more. +// Also, if we have no data yet, we can stand some +// more bytes. This is to work around cases where hwm=0, +// such as the repl. Also, if the push() triggered a +// readable event, and the user called read(largeNumber) such that +// needReadable was set, then we ought to push more, so that another +// 'readable' event will be triggered. +function needMoreData(state) { + return !state.ended && + (state.needReadable || + state.length < state.highWaterMark || + state.length === 0); +} + +// backwards compatibility. +Readable.prototype.setEncoding = function(enc) { + if (!StringDecoder) + StringDecoder = require('string_decoder/').StringDecoder; + this._readableState.decoder = new StringDecoder(enc); + this._readableState.encoding = enc; + return this; +}; + +// Don't raise the hwm > 128MB +var MAX_HWM = 0x800000; +function roundUpToNextPowerOf2(n) { + if (n >= MAX_HWM) { + n = MAX_HWM; + } else { + // Get the next highest power of 2 + n--; + for (var p = 1; p < 32; p <<= 1) n |= n >> p; + n++; + } + return n; +} + +function howMuchToRead(n, state) { + if (state.length === 0 && state.ended) + return 0; + + if (state.objectMode) + return n === 0 ? 0 : 1; + + if (isNaN(n) || util.isNull(n)) { + // only flow one buffer at a time + if (state.flowing && state.buffer.length) + return state.buffer[0].length; + else + return state.length; + } + + if (n <= 0) + return 0; + + // If we're asking for more than the target buffer level, + // then raise the water mark. Bump up to the next highest + // power of 2, to prevent increasing it excessively in tiny + // amounts. + if (n > state.highWaterMark) + state.highWaterMark = roundUpToNextPowerOf2(n); + + // don't have that much. return null, unless we've ended. + if (n > state.length) { + if (!state.ended) { + state.needReadable = true; + return 0; + } else + return state.length; + } + + return n; +} + +// you can override either this method, or the async _read(n) below. +Readable.prototype.read = function(n) { + debug('read', n); + var state = this._readableState; + var nOrig = n; + + if (!util.isNumber(n) || n > 0) + state.emittedReadable = false; + + // if we're doing read(0) to trigger a readable event, but we + // already have a bunch of data in the buffer, then just trigger + // the 'readable' event and move on. + if (n === 0 && + state.needReadable && + (state.length >= state.highWaterMark || state.ended)) { + debug('read: emitReadable', state.length, state.ended); + if (state.length === 0 && state.ended) + endReadable(this); + else + emitReadable(this); + return null; + } + + n = howMuchToRead(n, state); + + // if we've ended, and we're now clear, then finish it up. + if (n === 0 && state.ended) { + if (state.length === 0) + endReadable(this); + return null; + } + + // All the actual chunk generation logic needs to be + // *below* the call to _read. The reason is that in certain + // synthetic stream cases, such as passthrough streams, _read + // may be a completely synchronous operation which may change + // the state of the read buffer, providing enough data when + // before there was *not* enough. + // + // So, the steps are: + // 1. Figure out what the state of things will be after we do + // a read from the buffer. + // + // 2. If that resulting state will trigger a _read, then call _read. + // Note that this may be asynchronous, or synchronous. Yes, it is + // deeply ugly to write APIs this way, but that still doesn't mean + // that the Readable class should behave improperly, as streams are + // designed to be sync/async agnostic. + // Take note if the _read call is sync or async (ie, if the read call + // has returned yet), so that we know whether or not it's safe to emit + // 'readable' etc. + // + // 3. Actually pull the requested chunks out of the buffer and return. + + // if we need a readable event, then we need to do some reading. + var doRead = state.needReadable; + debug('need readable', doRead); + + // if we currently have less than the highWaterMark, then also read some + if (state.length === 0 || state.length - n < state.highWaterMark) { + doRead = true; + debug('length less than watermark', doRead); + } + + // however, if we've ended, then there's no point, and if we're already + // reading, then it's unnecessary. + if (state.ended || state.reading) { + doRead = false; + debug('reading or ended', doRead); + } + + if (doRead) { + debug('do read'); + state.reading = true; + state.sync = true; + // if the length is currently zero, then we *need* a readable event. + if (state.length === 0) + state.needReadable = true; + // call internal read method + this._read(state.highWaterMark); + state.sync = false; + } + + // If _read pushed data synchronously, then `reading` will be false, + // and we need to re-evaluate how much data we can return to the user. + if (doRead && !state.reading) + n = howMuchToRead(nOrig, state); + + var ret; + if (n > 0) + ret = fromList(n, state); + else + ret = null; + + if (util.isNull(ret)) { + state.needReadable = true; + n = 0; + } + + state.length -= n; + + // If we have nothing in the buffer, then we want to know + // as soon as we *do* get something into the buffer. + if (state.length === 0 && !state.ended) + state.needReadable = true; + + // If we tried to read() past the EOF, then emit end on the next tick. + if (nOrig !== n && state.ended && state.length === 0) + endReadable(this); + + if (!util.isNull(ret)) + this.emit('data', ret); + + return ret; +}; + +function chunkInvalid(state, chunk) { + var er = null; + if (!util.isBuffer(chunk) && + !util.isString(chunk) && + !util.isNullOrUndefined(chunk) && + !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + return er; +} + + +function onEofChunk(stream, state) { + if (state.decoder && !state.ended) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) { + state.buffer.push(chunk); + state.length += state.objectMode ? 1 : chunk.length; + } + } + state.ended = true; + + // emit 'readable' now to make sure it gets picked up. + emitReadable(stream); +} + +// Don't emit readable right away in sync mode, because this can trigger +// another read() call => stack overflow. This way, it might trigger +// a nextTick recursion warning, but that's not so bad. +function emitReadable(stream) { + var state = stream._readableState; + state.needReadable = false; + if (!state.emittedReadable) { + debug('emitReadable', state.flowing); + state.emittedReadable = true; + if (state.sync) + process.nextTick(function() { + emitReadable_(stream); + }); + else + emitReadable_(stream); + } +} + +function emitReadable_(stream) { + debug('emit readable'); + stream.emit('readable'); + flow(stream); +} + + +// at this point, the user has presumably seen the 'readable' event, +// and called read() to consume some data. that may have triggered +// in turn another _read(n) call, in which case reading = true if +// it's in progress. +// However, if we're not ended, or reading, and the length < hwm, +// then go ahead and try to read some more preemptively. +function maybeReadMore(stream, state) { + if (!state.readingMore) { + state.readingMore = true; + process.nextTick(function() { + maybeReadMore_(stream, state); + }); + } +} + +function maybeReadMore_(stream, state) { + var len = state.length; + while (!state.reading && !state.flowing && !state.ended && + state.length < state.highWaterMark) { + debug('maybeReadMore read 0'); + stream.read(0); + if (len === state.length) + // didn't get any data, stop spinning. + break; + else + len = state.length; + } + state.readingMore = false; +} + +// abstract method. to be overridden in specific implementation classes. +// call cb(er, data) where data is <= n in length. +// for virtual (non-string, non-buffer) streams, "length" is somewhat +// arbitrary, and perhaps not very meaningful. +Readable.prototype._read = function(n) { + this.emit('error', new Error('not implemented')); +}; + +Readable.prototype.pipe = function(dest, pipeOpts) { + var src = this; + var state = this._readableState; + + switch (state.pipesCount) { + case 0: + state.pipes = dest; + break; + case 1: + state.pipes = [state.pipes, dest]; + break; + default: + state.pipes.push(dest); + break; + } + state.pipesCount += 1; + debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); + + var doEnd = (!pipeOpts || pipeOpts.end !== false) && + dest !== process.stdout && + dest !== process.stderr; + + var endFn = doEnd ? onend : cleanup; + if (state.endEmitted) + process.nextTick(endFn); + else + src.once('end', endFn); + + dest.on('unpipe', onunpipe); + function onunpipe(readable) { + debug('onunpipe'); + if (readable === src) { + cleanup(); + } + } + + function onend() { + debug('onend'); + dest.end(); + } + + // when the dest drains, it reduces the awaitDrain counter + // on the source. This would be more elegant with a .once() + // handler in flow(), but adding and removing repeatedly is + // too slow. + var ondrain = pipeOnDrain(src); + dest.on('drain', ondrain); + + function cleanup() { + debug('cleanup'); + // cleanup event handlers once the pipe is broken + dest.removeListener('close', onclose); + dest.removeListener('finish', onfinish); + dest.removeListener('drain', ondrain); + dest.removeListener('error', onerror); + dest.removeListener('unpipe', onunpipe); + src.removeListener('end', onend); + src.removeListener('end', cleanup); + src.removeListener('data', ondata); + + // if the reader is waiting for a drain event from this + // specific writer, then it would cause it to never start + // flowing again. + // So, if this is awaiting a drain, then we just call it now. + // If we don't know, then assume that we are waiting for one. + if (state.awaitDrain && + (!dest._writableState || dest._writableState.needDrain)) + ondrain(); + } + + src.on('data', ondata); + function ondata(chunk) { + debug('ondata'); + var ret = dest.write(chunk); + if (false === ret) { + debug('false write response, pause', + src._readableState.awaitDrain); + src._readableState.awaitDrain++; + src.pause(); + } + } + + // if the dest has an error, then stop piping into it. + // however, don't suppress the throwing behavior for this. + function onerror(er) { + debug('onerror', er); + unpipe(); + dest.removeListener('error', onerror); + if (EE.listenerCount(dest, 'error') === 0) + dest.emit('error', er); + } + // This is a brutally ugly hack to make sure that our error handler + // is attached before any userland ones. NEVER DO THIS. + if (!dest._events || !dest._events.error) + dest.on('error', onerror); + else if (isArray(dest._events.error)) + dest._events.error.unshift(onerror); + else + dest._events.error = [onerror, dest._events.error]; + + + + // Both close and finish should trigger unpipe, but only once. + function onclose() { + dest.removeListener('finish', onfinish); + unpipe(); + } + dest.once('close', onclose); + function onfinish() { + debug('onfinish'); + dest.removeListener('close', onclose); + unpipe(); + } + dest.once('finish', onfinish); + + function unpipe() { + debug('unpipe'); + src.unpipe(dest); + } + + // tell the dest that it's being piped to + dest.emit('pipe', src); + + // start the flow if it hasn't been started already. + if (!state.flowing) { + debug('pipe resume'); + src.resume(); + } + + return dest; +}; + +function pipeOnDrain(src) { + return function() { + var state = src._readableState; + debug('pipeOnDrain', state.awaitDrain); + if (state.awaitDrain) + state.awaitDrain--; + if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) { + state.flowing = true; + flow(src); + } + }; +} + + +Readable.prototype.unpipe = function(dest) { + var state = this._readableState; + + // if we're not piping anywhere, then do nothing. + if (state.pipesCount === 0) + return this; + + // just one destination. most common case. + if (state.pipesCount === 1) { + // passed in one, but it's not the right one. + if (dest && dest !== state.pipes) + return this; + + if (!dest) + dest = state.pipes; + + // got a match. + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + if (dest) + dest.emit('unpipe', this); + return this; + } + + // slow case. multiple pipe destinations. + + if (!dest) { + // remove all. + var dests = state.pipes; + var len = state.pipesCount; + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + + for (var i = 0; i < len; i++) + dests[i].emit('unpipe', this); + return this; + } + + // try to find the right one. + var i = indexOf(state.pipes, dest); + if (i === -1) + return this; + + state.pipes.splice(i, 1); + state.pipesCount -= 1; + if (state.pipesCount === 1) + state.pipes = state.pipes[0]; + + dest.emit('unpipe', this); + + return this; +}; + +// set up data events if they are asked for +// Ensure readable listeners eventually get something +Readable.prototype.on = function(ev, fn) { + var res = Stream.prototype.on.call(this, ev, fn); + + // If listening to data, and it has not explicitly been paused, + // then call resume to start the flow of data on the next tick. + if (ev === 'data' && false !== this._readableState.flowing) { + this.resume(); + } + + if (ev === 'readable' && this.readable) { + var state = this._readableState; + if (!state.readableListening) { + state.readableListening = true; + state.emittedReadable = false; + state.needReadable = true; + if (!state.reading) { + var self = this; + process.nextTick(function() { + debug('readable nexttick read 0'); + self.read(0); + }); + } else if (state.length) { + emitReadable(this, state); + } + } + } + + return res; +}; +Readable.prototype.addListener = Readable.prototype.on; + +// pause() and resume() are remnants of the legacy readable stream API +// If the user uses them, then switch into old mode. +Readable.prototype.resume = function() { + var state = this._readableState; + if (!state.flowing) { + debug('resume'); + state.flowing = true; + if (!state.reading) { + debug('resume read 0'); + this.read(0); + } + resume(this, state); + } + return this; +}; + +function resume(stream, state) { + if (!state.resumeScheduled) { + state.resumeScheduled = true; + process.nextTick(function() { + resume_(stream, state); + }); + } +} + +function resume_(stream, state) { + state.resumeScheduled = false; + stream.emit('resume'); + flow(stream); + if (state.flowing && !state.reading) + stream.read(0); +} + +Readable.prototype.pause = function() { + debug('call pause flowing=%j', this._readableState.flowing); + if (false !== this._readableState.flowing) { + debug('pause'); + this._readableState.flowing = false; + this.emit('pause'); + } + return this; +}; + +function flow(stream) { + var state = stream._readableState; + debug('flow', state.flowing); + if (state.flowing) { + do { + var chunk = stream.read(); + } while (null !== chunk && state.flowing); + } +} + +// wrap an old-style stream as the async data source. +// This is *not* part of the readable stream interface. +// It is an ugly unfortunate mess of history. +Readable.prototype.wrap = function(stream) { + var state = this._readableState; + var paused = false; + + var self = this; + stream.on('end', function() { + debug('wrapped end'); + if (state.decoder && !state.ended) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) + self.push(chunk); + } + + self.push(null); + }); + + stream.on('data', function(chunk) { + debug('wrapped data'); + if (state.decoder) + chunk = state.decoder.write(chunk); + if (!chunk || !state.objectMode && !chunk.length) + return; + + var ret = self.push(chunk); + if (!ret) { + paused = true; + stream.pause(); + } + }); + + // proxy all the other methods. + // important when wrapping filters and duplexes. + for (var i in stream) { + if (util.isFunction(stream[i]) && util.isUndefined(this[i])) { + this[i] = function(method) { return function() { + return stream[method].apply(stream, arguments); + }}(i); + } + } + + // proxy certain important events. + var events = ['error', 'close', 'destroy', 'pause', 'resume']; + forEach(events, function(ev) { + stream.on(ev, self.emit.bind(self, ev)); + }); + + // when we try to consume some more bytes, simply unpause the + // underlying stream. + self._read = function(n) { + debug('wrapped _read', n); + if (paused) { + paused = false; + stream.resume(); + } + }; + + return self; +}; + + + +// exposed for testing purposes only. +Readable._fromList = fromList; + +// Pluck off n bytes from an array of buffers. +// Length is the combined lengths of all the buffers in the list. +function fromList(n, state) { + var list = state.buffer; + var length = state.length; + var stringMode = !!state.decoder; + var objectMode = !!state.objectMode; + var ret; + + // nothing in the list, definitely empty. + if (list.length === 0) + return null; + + if (length === 0) + ret = null; + else if (objectMode) + ret = list.shift(); + else if (!n || n >= length) { + // read it all, truncate the array. + if (stringMode) + ret = list.join(''); + else + ret = Buffer.concat(list, length); + list.length = 0; + } else { + // read just some of it. + if (n < list[0].length) { + // just take a part of the first list item. + // slice is the same for buffers and strings. + var buf = list[0]; + ret = buf.slice(0, n); + list[0] = buf.slice(n); + } else if (n === list[0].length) { + // first list is a perfect match + ret = list.shift(); + } else { + // complex case. + // we have enough to cover it, but it spans past the first buffer. + if (stringMode) + ret = ''; + else + ret = new Buffer(n); + + var c = 0; + for (var i = 0, l = list.length; i < l && c < n; i++) { + var buf = list[0]; + var cpy = Math.min(n - c, buf.length); + + if (stringMode) + ret += buf.slice(0, cpy); + else + buf.copy(ret, c, 0, cpy); + + if (cpy < buf.length) + list[0] = buf.slice(cpy); + else + list.shift(); + + c += cpy; + } + } + } + + return ret; +} + +function endReadable(stream) { + var state = stream._readableState; + + // If we get here before consuming all the bytes, then that is a + // bug in node. Should never happen. + if (state.length > 0) + throw new Error('endReadable called on non-empty stream'); + + if (!state.endEmitted) { + state.ended = true; + process.nextTick(function() { + // Check that we didn't get one last unshift. + if (!state.endEmitted && state.length === 0) { + state.endEmitted = true; + stream.readable = false; + stream.emit('end'); + } + }); + } +} + +function forEach (xs, f) { + for (var i = 0, l = xs.length; i < l; i++) { + f(xs[i], i); + } +} + +function indexOf (xs, x) { + for (var i = 0, l = xs.length; i < l; i++) { + if (xs[i] === x) return i; + } + return -1; +} + +}).call(this,require('_process')) +},{"./_stream_duplex":118,"_process":923,"buffer":46,"core-util-is":78,"events":95,"inherits":266,"isarray":117,"stream":978,"string_decoder/":979,"util":43}],121:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + +// a transform stream is a readable/writable stream where you do +// something with the data. Sometimes it's called a "filter", +// but that's not a great name for it, since that implies a thing where +// some bits pass through, and others are simply ignored. (That would +// be a valid example of a transform, of course.) +// +// While the output is causally related to the input, it's not a +// necessarily symmetric or synchronous transformation. For example, +// a zlib stream might take multiple plain-text writes(), and then +// emit a single compressed chunk some time in the future. +// +// Here's how this works: +// +// The Transform stream has all the aspects of the readable and writable +// stream classes. When you write(chunk), that calls _write(chunk,cb) +// internally, and returns false if there's a lot of pending writes +// buffered up. When you call read(), that calls _read(n) until +// there's enough pending readable data buffered up. +// +// In a transform stream, the written data is placed in a buffer. When +// _read(n) is called, it transforms the queued up data, calling the +// buffered _write cb's as it consumes chunks. If consuming a single +// written chunk would result in multiple output chunks, then the first +// outputted bit calls the readcb, and subsequent chunks just go into +// the read buffer, and will cause it to emit 'readable' if necessary. +// +// This way, back-pressure is actually determined by the reading side, +// since _read has to be called to start processing a new chunk. However, +// a pathological inflate type of transform can cause excessive buffering +// here. For example, imagine a stream where every byte of input is +// interpreted as an integer from 0-255, and then results in that many +// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in +// 1kb of data being output. In this case, you could write a very small +// amount of input, and end up with a very large amount of output. In +// such a pathological inflating mechanism, there'd be no way to tell +// the system to stop doing the transform. A single 4MB write could +// cause the system to run out of memory. +// +// However, even in such a pathological case, only a single written chunk +// would be consumed, and then the rest would wait (un-transformed) until +// the results of the previous transformed chunk were consumed. + +module.exports = Transform; + +var Duplex = require('./_stream_duplex'); + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +util.inherits(Transform, Duplex); + + +function TransformState(options, stream) { + this.afterTransform = function(er, data) { + return afterTransform(stream, er, data); + }; + + this.needTransform = false; + this.transforming = false; + this.writecb = null; + this.writechunk = null; +} + +function afterTransform(stream, er, data) { + var ts = stream._transformState; + ts.transforming = false; + + var cb = ts.writecb; + + if (!cb) + return stream.emit('error', new Error('no writecb in Transform class')); + + ts.writechunk = null; + ts.writecb = null; + + if (!util.isNullOrUndefined(data)) + stream.push(data); + + if (cb) + cb(er); + + var rs = stream._readableState; + rs.reading = false; + if (rs.needReadable || rs.length < rs.highWaterMark) { + stream._read(rs.highWaterMark); + } +} + + +function Transform(options) { + if (!(this instanceof Transform)) + return new Transform(options); + + Duplex.call(this, options); + + this._transformState = new TransformState(options, this); + + // when the writable side finishes, then flush out anything remaining. + var stream = this; + + // start out asking for a readable event once data is transformed. + this._readableState.needReadable = true; + + // we have implemented the _read method, and done the other things + // that Readable wants before the first _read call, so unset the + // sync guard flag. + this._readableState.sync = false; + + this.once('prefinish', function() { + if (util.isFunction(this._flush)) + this._flush(function(er) { + done(stream, er); + }); + else + done(stream); + }); +} + +Transform.prototype.push = function(chunk, encoding) { + this._transformState.needTransform = false; + return Duplex.prototype.push.call(this, chunk, encoding); +}; + +// This is the part where you do stuff! +// override this function in implementation classes. +// 'chunk' is an input chunk. +// +// Call `push(newChunk)` to pass along transformed output +// to the readable side. You may call 'push' zero or more times. +// +// Call `cb(err)` when you are done with this chunk. If you pass +// an error, then that'll put the hurt on the whole operation. If you +// never call cb(), then you'll never get another chunk. +Transform.prototype._transform = function(chunk, encoding, cb) { + throw new Error('not implemented'); +}; + +Transform.prototype._write = function(chunk, encoding, cb) { + var ts = this._transformState; + ts.writecb = cb; + ts.writechunk = chunk; + ts.writeencoding = encoding; + if (!ts.transforming) { + var rs = this._readableState; + if (ts.needTransform || + rs.needReadable || + rs.length < rs.highWaterMark) + this._read(rs.highWaterMark); + } +}; + +// Doesn't matter what the args are here. +// _transform does all the work. +// That we got here means that the readable side wants more data. +Transform.prototype._read = function(n) { + var ts = this._transformState; + + if (!util.isNull(ts.writechunk) && ts.writecb && !ts.transforming) { + ts.transforming = true; + this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); + } else { + // mark that we need a transform, so that any data that comes in + // will get processed, now that we've asked for it. + ts.needTransform = true; + } +}; + + +function done(stream, er) { + if (er) + return stream.emit('error', er); + + // if there's nothing in the write buffer, then that means + // that nothing more will ever be provided + var ws = stream._writableState; + var ts = stream._transformState; + + if (ws.length) + throw new Error('calling transform done when ws.length != 0'); + + if (ts.transforming) + throw new Error('calling transform done when still transforming'); + + return stream.push(null); +} + +},{"./_stream_duplex":118,"core-util-is":78,"inherits":266}],122:[function(require,module,exports){ +(function (process){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// A bit simpler than readable streams. +// Implement an async ._write(chunk, cb), and it'll handle all +// the drain event emission and buffering. + +module.exports = Writable; + +/**/ +var Buffer = require('buffer').Buffer; +/**/ + +Writable.WritableState = WritableState; + + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +var Stream = require('stream'); + +util.inherits(Writable, Stream); + +function WriteReq(chunk, encoding, cb) { + this.chunk = chunk; + this.encoding = encoding; + this.callback = cb; +} + +function WritableState(options, stream) { + var Duplex = require('./_stream_duplex'); + + options = options || {}; + + // the point at which write() starts returning false + // Note: 0 is a valid value, means that we always return false if + // the entire buffer is not flushed immediately on write() + var hwm = options.highWaterMark; + var defaultHwm = options.objectMode ? 16 : 16 * 1024; + this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; + + // object stream flag to indicate whether or not this stream + // contains buffers or objects. + this.objectMode = !!options.objectMode; + + if (stream instanceof Duplex) + this.objectMode = this.objectMode || !!options.writableObjectMode; + + // cast to ints. + this.highWaterMark = ~~this.highWaterMark; + + this.needDrain = false; + // at the start of calling end() + this.ending = false; + // when end() has been called, and returned + this.ended = false; + // when 'finish' is emitted + this.finished = false; + + // should we decode strings into buffers before passing to _write? + // this is here so that some node-core streams can optimize string + // handling at a lower level. + var noDecode = options.decodeStrings === false; + this.decodeStrings = !noDecode; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // not an actual buffer we keep track of, but a measurement + // of how much we're waiting to get pushed to some underlying + // socket or file. + this.length = 0; + + // a flag to see when we're in the middle of a write. + this.writing = false; + + // when true all writes will be buffered until .uncork() call + this.corked = 0; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, because any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + + // a flag to know if we're processing previously buffered items, which + // may call the _write() callback in the same tick, so that we don't + // end up in an overlapped onwrite situation. + this.bufferProcessing = false; + + // the callback that's passed to _write(chunk,cb) + this.onwrite = function(er) { + onwrite(stream, er); + }; + + // the callback that the user supplies to write(chunk,encoding,cb) + this.writecb = null; + + // the amount that is being written when _write is called. + this.writelen = 0; + + this.buffer = []; + + // number of pending user-supplied write callbacks + // this must be 0 before 'finish' can be emitted + this.pendingcb = 0; + + // emit prefinish if the only thing we're waiting for is _write cbs + // This is relevant for synchronous Transform streams + this.prefinished = false; + + // True if the error was already emitted and should not be thrown again + this.errorEmitted = false; +} + +function Writable(options) { + var Duplex = require('./_stream_duplex'); + + // Writable ctor is applied to Duplexes, though they're not + // instanceof Writable, they're instanceof Readable. + if (!(this instanceof Writable) && !(this instanceof Duplex)) + return new Writable(options); + + this._writableState = new WritableState(options, this); + + // legacy. + this.writable = true; + + Stream.call(this); +} + +// Otherwise people can pipe Writable streams, which is just wrong. +Writable.prototype.pipe = function() { + this.emit('error', new Error('Cannot pipe. Not readable.')); +}; + + +function writeAfterEnd(stream, state, cb) { + var er = new Error('write after end'); + // TODO: defer error events consistently everywhere, not just the cb + stream.emit('error', er); + process.nextTick(function() { + cb(er); + }); +} + +// If we get something that is not a buffer, string, null, or undefined, +// and we're not in objectMode, then that's an error. +// Otherwise stream chunks are all considered to be of length=1, and the +// watermarks determine how many objects to keep in the buffer, rather than +// how many bytes or characters. +function validChunk(stream, state, chunk, cb) { + var valid = true; + if (!util.isBuffer(chunk) && + !util.isString(chunk) && + !util.isNullOrUndefined(chunk) && + !state.objectMode) { + var er = new TypeError('Invalid non-string/buffer chunk'); + stream.emit('error', er); + process.nextTick(function() { + cb(er); + }); + valid = false; + } + return valid; +} + +Writable.prototype.write = function(chunk, encoding, cb) { + var state = this._writableState; + var ret = false; + + if (util.isFunction(encoding)) { + cb = encoding; + encoding = null; + } + + if (util.isBuffer(chunk)) + encoding = 'buffer'; + else if (!encoding) + encoding = state.defaultEncoding; + + if (!util.isFunction(cb)) + cb = function() {}; + + if (state.ended) + writeAfterEnd(this, state, cb); + else if (validChunk(this, state, chunk, cb)) { + state.pendingcb++; + ret = writeOrBuffer(this, state, chunk, encoding, cb); + } + + return ret; +}; + +Writable.prototype.cork = function() { + var state = this._writableState; + + state.corked++; +}; + +Writable.prototype.uncork = function() { + var state = this._writableState; + + if (state.corked) { + state.corked--; + + if (!state.writing && + !state.corked && + !state.finished && + !state.bufferProcessing && + state.buffer.length) + clearBuffer(this, state); + } +}; + +function decodeChunk(state, chunk, encoding) { + if (!state.objectMode && + state.decodeStrings !== false && + util.isString(chunk)) { + chunk = new Buffer(chunk, encoding); + } + return chunk; +} + +// if we're already writing something, then just put this +// in the queue, and wait our turn. Otherwise, call _write +// If we return false, then we need a drain event, so set that flag. +function writeOrBuffer(stream, state, chunk, encoding, cb) { + chunk = decodeChunk(state, chunk, encoding); + if (util.isBuffer(chunk)) + encoding = 'buffer'; + var len = state.objectMode ? 1 : chunk.length; + + state.length += len; + + var ret = state.length < state.highWaterMark; + // we must ensure that previous needDrain will not be reset to false. + if (!ret) + state.needDrain = true; + + if (state.writing || state.corked) + state.buffer.push(new WriteReq(chunk, encoding, cb)); + else + doWrite(stream, state, false, len, chunk, encoding, cb); + + return ret; +} + +function doWrite(stream, state, writev, len, chunk, encoding, cb) { + state.writelen = len; + state.writecb = cb; + state.writing = true; + state.sync = true; + if (writev) + stream._writev(chunk, state.onwrite); + else + stream._write(chunk, encoding, state.onwrite); + state.sync = false; +} + +function onwriteError(stream, state, sync, er, cb) { + if (sync) + process.nextTick(function() { + state.pendingcb--; + cb(er); + }); + else { + state.pendingcb--; + cb(er); + } + + stream._writableState.errorEmitted = true; + stream.emit('error', er); +} + +function onwriteStateUpdate(state) { + state.writing = false; + state.writecb = null; + state.length -= state.writelen; + state.writelen = 0; +} + +function onwrite(stream, er) { + var state = stream._writableState; + var sync = state.sync; + var cb = state.writecb; + + onwriteStateUpdate(state); + + if (er) + onwriteError(stream, state, sync, er, cb); + else { + // Check if we're actually ready to finish, but don't emit yet + var finished = needFinish(stream, state); + + if (!finished && + !state.corked && + !state.bufferProcessing && + state.buffer.length) { + clearBuffer(stream, state); + } + + if (sync) { + process.nextTick(function() { + afterWrite(stream, state, finished, cb); + }); + } else { + afterWrite(stream, state, finished, cb); + } + } +} + +function afterWrite(stream, state, finished, cb) { + if (!finished) + onwriteDrain(stream, state); + state.pendingcb--; + cb(); + finishMaybe(stream, state); +} + +// Must force callback to be called on nextTick, so that we don't +// emit 'drain' before the write() consumer gets the 'false' return +// value, and has a chance to attach a 'drain' listener. +function onwriteDrain(stream, state) { + if (state.length === 0 && state.needDrain) { + state.needDrain = false; + stream.emit('drain'); + } +} + + +// if there's something in the buffer waiting, then process it +function clearBuffer(stream, state) { + state.bufferProcessing = true; + + if (stream._writev && state.buffer.length > 1) { + // Fast case, write everything using _writev() + var cbs = []; + for (var c = 0; c < state.buffer.length; c++) + cbs.push(state.buffer[c].callback); + + // count the one we are adding, as well. + // TODO(isaacs) clean this up + state.pendingcb++; + doWrite(stream, state, true, state.length, state.buffer, '', function(err) { + for (var i = 0; i < cbs.length; i++) { + state.pendingcb--; + cbs[i](err); + } + }); + + // Clear buffer + state.buffer = []; + } else { + // Slow case, write chunks one-by-one + for (var c = 0; c < state.buffer.length; c++) { + var entry = state.buffer[c]; + var chunk = entry.chunk; + var encoding = entry.encoding; + var cb = entry.callback; + var len = state.objectMode ? 1 : chunk.length; + + doWrite(stream, state, false, len, chunk, encoding, cb); + + // if we didn't call the onwrite immediately, then + // it means that we need to wait until it does. + // also, that means that the chunk and cb are currently + // being processed, so move the buffer counter past them. + if (state.writing) { + c++; + break; + } + } + + if (c < state.buffer.length) + state.buffer = state.buffer.slice(c); + else + state.buffer.length = 0; + } + + state.bufferProcessing = false; +} + +Writable.prototype._write = function(chunk, encoding, cb) { + cb(new Error('not implemented')); + +}; + +Writable.prototype._writev = null; + +Writable.prototype.end = function(chunk, encoding, cb) { + var state = this._writableState; + + if (util.isFunction(chunk)) { + cb = chunk; + chunk = null; + encoding = null; + } else if (util.isFunction(encoding)) { + cb = encoding; + encoding = null; + } + + if (!util.isNullOrUndefined(chunk)) + this.write(chunk, encoding); + + // .end() fully uncorks + if (state.corked) { + state.corked = 1; + this.uncork(); + } + + // ignore unnecessary end() calls. + if (!state.ending && !state.finished) + endWritable(this, state, cb); +}; + + +function needFinish(stream, state) { + return (state.ending && + state.length === 0 && + !state.finished && + !state.writing); +} + +function prefinish(stream, state) { + if (!state.prefinished) { + state.prefinished = true; + stream.emit('prefinish'); + } +} + +function finishMaybe(stream, state) { + var need = needFinish(stream, state); + if (need) { + if (state.pendingcb === 0) { + prefinish(stream, state); + state.finished = true; + stream.emit('finish'); + } else + prefinish(stream, state); + } + return need; +} + +function endWritable(stream, state, cb) { + state.ending = true; + finishMaybe(stream, state); + if (cb) { + if (state.finished) + process.nextTick(cb); + else + stream.once('finish', cb); + } + state.ended = true; +} + +}).call(this,require('_process')) +},{"./_stream_duplex":118,"_process":923,"buffer":46,"core-util-is":78,"inherits":266,"stream":978}],123:[function(require,module,exports){ +(function (process){ +exports = module.exports = require('./lib/_stream_readable.js'); +exports.Stream = require('stream'); +exports.Readable = exports; +exports.Writable = require('./lib/_stream_writable.js'); +exports.Duplex = require('./lib/_stream_duplex.js'); +exports.Transform = require('./lib/_stream_transform.js'); +exports.PassThrough = require('./lib/_stream_passthrough.js'); +if (!process.browser && process.env.READABLE_STREAM === 'disable') { + module.exports = require('stream'); +} + +}).call(this,require('_process')) +},{"./lib/_stream_duplex.js":118,"./lib/_stream_passthrough.js":119,"./lib/_stream_readable.js":120,"./lib/_stream_transform.js":121,"./lib/_stream_writable.js":122,"_process":923,"stream":978}],124:[function(require,module,exports){ +'use strict' + +module.exports = createAxes + +var createText = require('./lib/text.js') +var createLines = require('./lib/lines.js') +var createBackground = require('./lib/background.js') +var getCubeProperties = require('./lib/cube.js') +var Ticks = require('./lib/ticks.js') + +var identity = new Float32Array([ + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1]) + +function copyVec3(a, b) { + a[0] = b[0] + a[1] = b[1] + a[2] = b[2] + return a +} + +function Axes(gl) { + this.gl = gl + + this.pixelRatio = 1 + + this.bounds = [ [-10, -10, -10], + [ 10, 10, 10] ] + this.ticks = [ [], [], [] ] + this.autoTicks = true + this.tickSpacing = [ 1, 1, 1 ] + + this.tickEnable = [ true, true, true ] + this.tickFont = [ 'sans-serif', 'sans-serif', 'sans-serif' ] + this.tickSize = [ 12, 12, 12 ] + this.tickAngle = [ 0, 0, 0 ] + this.tickColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] + this.tickPad = [ 10, 10, 10 ] + + this.lastCubeProps = { + cubeEdges: [0,0,0], + axis: [0,0,0] + } + + this.labels = [ 'x', 'y', 'z' ] + this.labelEnable = [ true, true, true ] + this.labelFont = 'sans-serif' + this.labelSize = [ 20, 20, 20 ] + this.labelAngle = [ 0, 0, 0 ] + this.labelColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] + this.labelPad = [ 10, 10, 10 ] + + this.lineEnable = [ true, true, true ] + this.lineMirror = [ false, false, false ] + this.lineWidth = [ 1, 1, 1 ] + this.lineColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] + + this.lineTickEnable = [ true, true, true ] + this.lineTickMirror = [ false, false, false ] + this.lineTickLength = [ 0, 0, 0 ] + this.lineTickWidth = [ 1, 1, 1 ] + this.lineTickColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] + + this.gridEnable = [ true, true, true ] + this.gridWidth = [ 1, 1, 1 ] + this.gridColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] + + this.zeroEnable = [ true, true, true ] + this.zeroLineColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] + this.zeroLineWidth = [ 2, 2, 2 ] + + this.backgroundEnable = [ false, false, false ] + this.backgroundColor = [ [0.8, 0.8, 0.8, 0.5], + [0.8, 0.8, 0.8, 0.5], + [0.8, 0.8, 0.8, 0.5] ] + + this._firstInit = true + this._text = null + this._lines = null + this._background = createBackground(gl) +} + +var proto = Axes.prototype + +proto.update = function(options) { + options = options || {} + + //Option parsing helper functions + function parseOption(nest, cons, name) { + if(name in options) { + var opt = options[name] + var prev = this[name] + var next + if(nest ? (Array.isArray(opt) && Array.isArray(opt[0])) : + Array.isArray(opt) ) { + this[name] = next = [ cons(opt[0]), cons(opt[1]), cons(opt[2]) ] + } else { + this[name] = next = [ cons(opt), cons(opt), cons(opt) ] + } + for(var i=0; i<3; ++i) { + if(next[i] !== prev[i]) { + return true + } + } + } + return false + } + + var NUMBER = parseOption.bind(this, false, Number) + var BOOLEAN = parseOption.bind(this, false, Boolean) + var STRING = parseOption.bind(this, false, String) + var COLOR = parseOption.bind(this, true, function(v) { + if(Array.isArray(v)) { + if(v.length === 3) { + return [ +v[0], +v[1], +v[2], 1.0 ] + } else if(v.length === 4) { + return [ +v[0], +v[1], +v[2], +v[3] ] + } + } + return [ 0, 0, 0, 1 ] + }) + + //Tick marks and bounds + var nextTicks + var ticksUpdate = false + var boundsChanged = false + if('bounds' in options) { + var bounds = options.bounds +i_loop: + for(var i=0; i<2; ++i) { + for(var j=0; j<3; ++j) { + if(bounds[i][j] !== this.bounds[i][j]) { + boundsChanged = true + } + this.bounds[i][j] = bounds[i][j] + } + } + } + if('ticks' in options) { + nextTicks = options.ticks + ticksUpdate = true + this.autoTicks = false + for(var i=0; i<3; ++i) { + this.tickSpacing[i] = 0.0 + } + } else if(NUMBER('tickSpacing')) { + this.autoTicks = true + boundsChanged = true + } + + if(this._firstInit) { + if(!('ticks' in options || 'tickSpacing' in options)) { + this.autoTicks = true + } + + //Force tick recomputation on first update + boundsChanged = true + ticksUpdate = true + this._firstInit = false + } + + if(boundsChanged && this.autoTicks) { + nextTicks = Ticks.create(this.bounds, this.tickSpacing) + ticksUpdate = true + } + + //Compare next ticks to previous ticks, only update if needed + if(ticksUpdate) { + for(var i=0; i<3; ++i) { + nextTicks[i].sort(function(a,b) { + return a.x-b.x + }) + } + if(Ticks.equal(nextTicks, this.ticks)) { + ticksUpdate = false + } else { + this.ticks = nextTicks + } + } + + //Parse tick properties + BOOLEAN('tickEnable') + if(STRING('tickFont')) { + ticksUpdate = true //If font changes, must rebuild vbo + } + NUMBER('tickSize') + NUMBER('tickAngle') + NUMBER('tickPad') + COLOR('tickColor') + + //Axis labels + var labelUpdate = STRING('labels') + if(STRING('labelFont')) { + labelUpdate = true + } + BOOLEAN('labelEnable') + NUMBER('labelSize') + NUMBER('labelPad') + COLOR('labelColor') + + //Axis lines + BOOLEAN('lineEnable') + BOOLEAN('lineMirror') + NUMBER('lineWidth') + COLOR('lineColor') + + //Axis line ticks + BOOLEAN('lineTickEnable') + BOOLEAN('lineTickMirror') + NUMBER('lineTickLength') + NUMBER('lineTickWidth') + COLOR('lineTickColor') + + //Grid lines + BOOLEAN('gridEnable') + NUMBER('gridWidth') + COLOR('gridColor') + + //Zero line + BOOLEAN('zeroEnable') + COLOR('zeroLineColor') + NUMBER('zeroLineWidth') + + //Background + BOOLEAN('backgroundEnable') + COLOR('backgroundColor') + + //Update text if necessary + if(!this._text) { + this._text = createText( + this.gl, + this.bounds, + this.labels, + this.labelFont, + this.ticks, + this.tickFont) + } else if(this._text && (labelUpdate || ticksUpdate)) { + this._text.update( + this.bounds, + this.labels, + this.labelFont, + this.ticks, + this.tickFont) + } + + //Update lines if necessary + if(this._lines && ticksUpdate) { + this._lines.dispose() + this._lines = null + } + if(!this._lines) { + this._lines = createLines(this.gl, this.bounds, this.ticks) + } +} + +function OffsetInfo() { + this.primalOffset = [0,0,0] + this.primalMinor = [0,0,0] + this.mirrorOffset = [0,0,0] + this.mirrorMinor = [0,0,0] +} + +var LINE_OFFSET = [ new OffsetInfo(), new OffsetInfo(), new OffsetInfo() ] + +function computeLineOffset(result, i, bounds, cubeEdges, cubeAxis) { + var primalOffset = result.primalOffset + var primalMinor = result.primalMinor + var dualOffset = result.mirrorOffset + var dualMinor = result.mirrorMinor + var e = cubeEdges[i] + + //Calculate offsets + for(var j=0; j<3; ++j) { + if(i === j) { + continue + } + var a = primalOffset, + b = dualOffset, + c = primalMinor, + d = dualMinor + if(e & (1< 0) { + c[j] = -1 + d[j] = 0 + } else { + c[j] = 0 + d[j] = +1 + } + } +} + +var CUBE_ENABLE = [0,0,0] +var DEFAULT_PARAMS = { + model: identity, + view: identity, + projection: identity +} + +proto.isOpaque = function() { + return true +} + +proto.isTransparent = function() { + return false +} + +proto.drawTransparent = function(params) {} + + +var PRIMAL_MINOR = [0,0,0] +var MIRROR_MINOR = [0,0,0] +var PRIMAL_OFFSET = [0,0,0] + +proto.draw = function(params) { + params = params || DEFAULT_PARAMS + + var gl = this.gl + + //Geometry for camera and axes + var model = params.model || identity + var view = params.view || identity + var projection = params.projection || identity + var bounds = this.bounds + + //Unpack axis info + var cubeParams = getCubeProperties(model, view, projection, bounds) + var cubeEdges = cubeParams.cubeEdges + var cubeAxis = cubeParams.axis + + var cx = view[12] + var cy = view[13] + var cz = view[14] + var cw = view[15] + + var pixelScaleF = this.pixelRatio * (projection[3]*cx + projection[7]*cy + projection[11]*cz + projection[15]*cw) / gl.drawingBufferHeight + + for(var i=0; i<3; ++i) { + this.lastCubeProps.cubeEdges[i] = cubeEdges[i] + this.lastCubeProps.axis[i] = cubeAxis[i] + } + + //Compute axis info + var lineOffset = LINE_OFFSET + for(var i=0; i<3; ++i) { + computeLineOffset( + LINE_OFFSET[i], + i, + this.bounds, + cubeEdges, + cubeAxis) + } + + //Set up state parameters + var gl = this.gl + + //Draw background first + var cubeEnable = CUBE_ENABLE + for(var i=0; i<3; ++i) { + if(this.backgroundEnable[i]) { + cubeEnable[i] = cubeAxis[i] + } else { + cubeEnable[i] = 0 + } + } + + this._background.draw( + model, + view, + projection, + bounds, + cubeEnable, + this.backgroundColor) + + //Draw lines + this._lines.bind( + model, + view, + projection, + this) + + //First draw grid lines and zero lines + for(var i=0; i<3; ++i) { + var x = [0,0,0] + if(cubeAxis[i] > 0) { + x[i] = bounds[1][i] + } else { + x[i] = bounds[0][i] + } + + //Draw grid lines + for(var j=0; j<2; ++j) { + var u = (i + 1 + j) % 3 + var v = (i + 1 + (j^1)) % 3 + if(this.gridEnable[u]) { + this._lines.drawGrid(u, v, this.bounds, x, this.gridColor[u], this.gridWidth[u]*this.pixelRatio) + } + } + + //Draw zero lines (need to do this AFTER all grid lines are drawn) + for(var j=0; j<2; ++j) { + var u = (i + 1 + j) % 3 + var v = (i + 1 + (j^1)) % 3 + if(this.zeroEnable[v]) { + //Check if zero line in bounds + if(bounds[0][v] <= 0 && bounds[1][v] >= 0) { + this._lines.drawZero(u, v, this.bounds, x, this.zeroLineColor[v], this.zeroLineWidth[v]*this.pixelRatio) + } + } + } + } + + //Then draw axis lines and tick marks + for(var i=0; i<3; ++i) { + + //Draw axis lines + if(this.lineEnable[i]) { + this._lines.drawAxisLine(i, this.bounds, lineOffset[i].primalOffset, this.lineColor[i], this.lineWidth[i]*this.pixelRatio) + } + if(this.lineMirror[i]) { + this._lines.drawAxisLine(i, this.bounds, lineOffset[i].mirrorOffset, this.lineColor[i], this.lineWidth[i]*this.pixelRatio) + } + + //Compute minor axes + var primalMinor = copyVec3(PRIMAL_MINOR, lineOffset[i].primalMinor) + var mirrorMinor = copyVec3(MIRROR_MINOR, lineOffset[i].mirrorMinor) + var tickLength = this.lineTickLength + var op = 0 + for(var j=0; j<3; ++j) { + var scaleFactor = pixelScaleF / model[5*j] + primalMinor[j] *= tickLength[j] * scaleFactor + mirrorMinor[j] *= tickLength[j] * scaleFactor + } + + //Draw axis line ticks + if(this.lineTickEnable[i]) { + this._lines.drawAxisTicks(i, lineOffset[i].primalOffset, primalMinor, this.lineTickColor[i], this.lineTickWidth[i]*this.pixelRatio) + } + if(this.lineTickMirror[i]) { + this._lines.drawAxisTicks(i, lineOffset[i].mirrorOffset, mirrorMinor, this.lineTickColor[i], this.lineTickWidth[i]*this.pixelRatio) + } + } + + //Draw text sprites + this._text.bind( + model, + view, + projection, + this.pixelRatio) + + for(var i=0; i<3; ++i) { + + var minor = lineOffset[i].primalMinor + var offset = copyVec3(PRIMAL_OFFSET, lineOffset[i].primalOffset) + + for(var j=0; j<3; ++j) { + if(this.lineTickEnable[i]) { + offset[j] += pixelScaleF * minor[j] * Math.max(this.lineTickLength[j], 0) / model[5*j] + } + } + + //Draw tick text + if(this.tickEnable[i]) { + + //Add tick padding + for(var j=0; j<3; ++j) { + offset[j] += pixelScaleF * minor[j] * this.tickPad[j] / model[5*j] + } + + //Draw axis + this._text.drawTicks( + i, + this.tickSize[i], + this.tickAngle[i], + offset, + this.tickColor[i]) + } + + //Draw labels + if(this.labelEnable[i]) { + + //Add label padding + for(var j=0; j<3; ++j) { + offset[j] += pixelScaleF * minor[j] * this.labelPad[j] / model[5*j] + } + offset[i] += 0.5 * (bounds[0][i] + bounds[1][i]) + + //Draw axis + this._text.drawLabel( + i, + this.labelSize[i], + this.labelAngle[i], + offset, + this.labelColor[i]) + } + } +} + +proto.dispose = function() { + this._text.dispose() + this._lines.dispose() + this._background.dispose() + this._lines = null + this._text = null + this._background = null + this.gl = null +} + +function createAxes(gl, options) { + var axes = new Axes(gl) + axes.update(options) + return axes +} + +},{"./lib/background.js":125,"./lib/cube.js":126,"./lib/lines.js":127,"./lib/text.js":129,"./lib/ticks.js":130}],125:[function(require,module,exports){ +'use strict' + +module.exports = createBackgroundCube + +var createBuffer = require('gl-buffer') +var createVAO = require('gl-vao') +var createShader = require('./shaders').bg + +function BackgroundCube(gl, buffer, vao, shader) { + this.gl = gl + this.buffer = buffer + this.vao = vao + this.shader = shader +} + +var proto = BackgroundCube.prototype + +proto.draw = function(model, view, projection, bounds, enable, colors) { + var needsBG = false + for(var i=0; i<3; ++i) { + needsBG = needsBG || enable[i] + } + if(!needsBG) { + return + } + + var gl = this.gl + + gl.enable(gl.POLYGON_OFFSET_FILL) + gl.polygonOffset(1, 2) + + this.shader.bind() + this.shader.uniforms = { + model: model, + view: view, + projection: projection, + bounds: bounds, + enable: enable, + colors: colors + } + this.vao.bind() + this.vao.draw(this.gl.TRIANGLES, 36) + + gl.disable(gl.POLYGON_OFFSET_FILL) +} + +proto.dispose = function() { + this.vao.dispose() + this.buffer.dispose() + this.shader.dispose() +} + +function createBackgroundCube(gl) { + //Create cube vertices + var vertices = [] + var indices = [] + var ptr = 0 + for(var d=0; d<3; ++d) { + var u = (d+1) % 3 + var v = (d+2) % 3 + var x = [0,0,0] + var c = [0,0,0] + for(var s=-1; s<=1; s+=2) { + indices.push(ptr, ptr+2, ptr+1, + ptr+1, ptr+2, ptr+3) + x[d] = s + c[d] = s + for(var i=-1; i<=1; i+=2) { + x[u] = i + for(var j=-1; j<=1; j+=2) { + x[v] = j + vertices.push(x[0], x[1], x[2], + c[0], c[1], c[2]) + ptr += 1 + } + } + //Swap u and v + var tt = u + u = v + v = tt + } + } + + //Allocate buffer and vertex array + var buffer = createBuffer(gl, new Float32Array(vertices)) + var elements = createBuffer(gl, new Uint16Array(indices), gl.ELEMENT_ARRAY_BUFFER) + var vao = createVAO(gl, [ + { + buffer: buffer, + type: gl.FLOAT, + size: 3, + offset: 0, + stride: 24 + }, + { + buffer: buffer, + type: gl.FLOAT, + size: 3, + offset: 12, + stride: 24 + } + ], elements) + + //Create shader object + var shader = createShader(gl) + shader.attributes.position.location = 0 + shader.attributes.normal.location = 1 + + return new BackgroundCube(gl, buffer, vao, shader) +} + +},{"./shaders":128,"gl-buffer":132,"gl-vao":243}],126:[function(require,module,exports){ +"use strict" + +module.exports = getCubeEdges + +var bits = require('bit-twiddle') +var multiply = require('gl-mat4/multiply') +var invert = require('gl-mat4/invert') +var splitPoly = require('split-polygon') +var orient = require('robust-orientation') + +var mvp = new Array(16) +var imvp = new Array(16) +var pCubeVerts = new Array(8) +var cubeVerts = new Array(8) +var x = new Array(3) +var zero3 = [0,0,0] + +;(function() { + for(var i=0; i<8; ++i) { + pCubeVerts[i] =[1,1,1,1] + cubeVerts[i] = [1,1,1] + } +})() + + +function transformHg(result, x, mat) { + for(var i=0; i<4; ++i) { + result[i] = mat[12+i] + for(var j=0; j<3; ++j) { + result[i] += x[j]*mat[4*j+i] + } + } +} + +var FRUSTUM_PLANES = [ + [ 0, 0, 1, 0, 0], + [ 0, 0,-1, 1, 0], + [ 0,-1, 0, 1, 0], + [ 0, 1, 0, 1, 0], + [-1, 0, 0, 1, 0], + [ 1, 0, 0, 1, 0] +] + +function polygonArea(p) { + for(var i=0; i o0) { + closest |= 1< o0) { + closest |= 1< cubeVerts[i][1]) { + bottom = i + } + } + + //Find left/right neighbors of bottom vertex + var left = -1 + for(var i=0; i<3; ++i) { + var idx = bottom ^ (1< cubeVerts[right][0]) { + right = idx + } + } + + //Determine edge axis coordinates + var cubeEdges = CUBE_EDGES + cubeEdges[0] = cubeEdges[1] = cubeEdges[2] = 0 + cubeEdges[bits.log2(left^bottom)] = bottom&left + cubeEdges[bits.log2(bottom^right)] = bottom&right + var top = right ^ 7 + if(top === closest || top === farthest) { + top = left ^ 7 + cubeEdges[bits.log2(right^top)] = top&right + } else { + cubeEdges[bits.log2(left^top)] = top&left + } + + //Determine visible faces + var axis = CUBE_AXIS + var cutCorner = closest + for(var d=0; d<3; ++d) { + if(cutCorner & (1<=0; --j) { + var p = positions[c[j]] + data.push(scale*p[0], -scale*p[1], t) + } + } + } + + //Generate sprites for all 3 axes, store data in texture atlases + var tickOffset = [0,0,0] + var tickCount = [0,0,0] + var labelOffset = [0,0,0] + var labelCount = [0,0,0] + for(var d=0; d<3; ++d) { + + //Generate label + labelOffset[d] = (data.length/VERTEX_SIZE)|0 + addItem(0.5*(bounds[0][d]+bounds[1][d]), labels[d], labelFont) + labelCount[d] = ((data.length/VERTEX_SIZE)|0) - labelOffset[d] + + //Generate sprites for tick marks + tickOffset[d] = (data.length/VERTEX_SIZE)|0 + for(var i=0; i= 0) { + sigFigs = stepStr.length - u - 1 + } + var shift = Math.pow(10, sigFigs) + var x = Math.round(spacing * i * shift) + var xstr = x + "" + if(xstr.indexOf("e") >= 0) { + return xstr + } + var xi = x / shift, xf = x % shift + if(x < 0) { + xi = -Math.ceil(xi)|0 + xf = (-xf)|0 + } else { + xi = Math.floor(xi)|0 + xf = xf|0 + } + var xis = "" + xi + if(x < 0) { + xis = "-" + xis + } + if(sigFigs) { + var xs = "" + xf + while(xs.length < sigFigs) { + xs = "0" + xs + } + return xis + "." + xs + } else { + return xis + } +} + +function defaultTicks(bounds, tickSpacing) { + var array = [] + for(var d=0; d<3; ++d) { + var ticks = [] + var m = 0.5*(bounds[0][d]+bounds[1][d]) + for(var t=0; t*tickSpacing[d]<=bounds[1][d]; ++t) { + ticks.push({x: t*tickSpacing[d], text: prettyPrint(tickSpacing[d], t)}) + } + for(var t=-1; t*tickSpacing[d]>=bounds[0][d]; --t) { + ticks.push({x: t*tickSpacing[d], text: prettyPrint(tickSpacing[d], t)}) + } + array.push(ticks) + } + return array +} + +function ticksEqual(ticksA, ticksB) { + for(var i=0; i<3; ++i) { + if(ticksA[i].length !== ticksB[i].length) { + return false + } + for(var j=0; j len) { + throw new Error("gl-buffer: If resizing buffer, must not specify offset") + } + gl.bufferSubData(type, offset, data) + return len +} + +function makeScratchTypeArray(array, dtype) { + var res = pool.malloc(array.length, dtype) + var n = array.length + for(var i=0; i=0; --i) { + if(stride[i] !== n) { + return false + } + n *= shape[i] + } + return true +} + +proto.update = function(array, offset) { + if(typeof offset !== "number") { + offset = -1 + } + this.bind() + if(typeof array === "object" && typeof array.shape !== "undefined") { //ndarray + var dtype = array.dtype + if(SUPPORTED_TYPES.indexOf(dtype) < 0) { + dtype = "float32" + } + if(this.type === this.gl.ELEMENT_ARRAY_BUFFER) { + var ext = gl.getExtension('OES_element_index_uint') + if(ext && dtype !== "uint16") { + dtype = "uint32" + } else { + dtype = "uint16" + } + } + if(dtype === array.dtype && isPacked(array.shape, array.stride)) { + if(array.offset === 0 && array.data.length === array.shape[0]) { + this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array.data, offset) + } else { + this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array.data.subarray(array.offset, array.shape[0]), offset) + } + } else { + var tmp = pool.malloc(array.size, dtype) + var ndt = ndarray(tmp, array.shape) + ops.assign(ndt, array) + if(offset < 0) { + this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, tmp, offset) + } else { + this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, tmp.subarray(0, array.size), offset) + } + pool.free(tmp) + } + } else if(Array.isArray(array)) { //Vanilla array + var t + if(this.type === this.gl.ELEMENT_ARRAY_BUFFER) { + t = makeScratchTypeArray(array, "uint16") + } else { + t = makeScratchTypeArray(array, "float32") + } + if(offset < 0) { + this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, t, offset) + } else { + this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, t.subarray(0, array.length), offset) + } + pool.free(t) + } else if(typeof array === "object" && typeof array.length === "number") { //Typed array + this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array, offset) + } else if(typeof array === "number" || array === undefined) { //Number/default + if(offset >= 0) { + throw new Error("gl-buffer: Cannot specify offset when resizing buffer") + } + array = array | 0 + if(array <= 0) { + array = 1 + } + this.gl.bufferData(this.type, array|0, this.usage) + this.length = array + } else { //Error, case should not happen + throw new Error("gl-buffer: Invalid data type") + } +} + +function createBuffer(gl, data, type, usage) { + type = type || gl.ARRAY_BUFFER + usage = usage || gl.DYNAMIC_DRAW + if(type !== gl.ARRAY_BUFFER && type !== gl.ELEMENT_ARRAY_BUFFER) { + throw new Error("gl-buffer: Invalid type for webgl buffer, must be either gl.ARRAY_BUFFER or gl.ELEMENT_ARRAY_BUFFER") + } + if(usage !== gl.DYNAMIC_DRAW && usage !== gl.STATIC_DRAW && usage !== gl.STREAM_DRAW) { + throw new Error("gl-buffer: Invalid usage for buffer, must be either gl.DYNAMIC_DRAW, gl.STATIC_DRAW or gl.STREAM_DRAW") + } + var handle = gl.createBuffer() + var result = new GLBuffer(gl, type, handle, 0, usage) + result.update(data) + return result +} + +module.exports = createBuffer + +},{"ndarray":443,"ndarray-ops":437,"typedarray-pool":992}],133:[function(require,module,exports){ +module.exports = { + 0: 'NONE', + 1: 'ONE', + 2: 'LINE_LOOP', + 3: 'LINE_STRIP', + 4: 'TRIANGLES', + 5: 'TRIANGLE_STRIP', + 6: 'TRIANGLE_FAN', + 256: 'DEPTH_BUFFER_BIT', + 512: 'NEVER', + 513: 'LESS', + 514: 'EQUAL', + 515: 'LEQUAL', + 516: 'GREATER', + 517: 'NOTEQUAL', + 518: 'GEQUAL', + 519: 'ALWAYS', + 768: 'SRC_COLOR', + 769: 'ONE_MINUS_SRC_COLOR', + 770: 'SRC_ALPHA', + 771: 'ONE_MINUS_SRC_ALPHA', + 772: 'DST_ALPHA', + 773: 'ONE_MINUS_DST_ALPHA', + 774: 'DST_COLOR', + 775: 'ONE_MINUS_DST_COLOR', + 776: 'SRC_ALPHA_SATURATE', + 1024: 'STENCIL_BUFFER_BIT', + 1028: 'FRONT', + 1029: 'BACK', + 1032: 'FRONT_AND_BACK', + 1280: 'INVALID_ENUM', + 1281: 'INVALID_VALUE', + 1282: 'INVALID_OPERATION', + 1285: 'OUT_OF_MEMORY', + 1286: 'INVALID_FRAMEBUFFER_OPERATION', + 2304: 'CW', + 2305: 'CCW', + 2849: 'LINE_WIDTH', + 2884: 'CULL_FACE', + 2885: 'CULL_FACE_MODE', + 2886: 'FRONT_FACE', + 2928: 'DEPTH_RANGE', + 2929: 'DEPTH_TEST', + 2930: 'DEPTH_WRITEMASK', + 2931: 'DEPTH_CLEAR_VALUE', + 2932: 'DEPTH_FUNC', + 2960: 'STENCIL_TEST', + 2961: 'STENCIL_CLEAR_VALUE', + 2962: 'STENCIL_FUNC', + 2963: 'STENCIL_VALUE_MASK', + 2964: 'STENCIL_FAIL', + 2965: 'STENCIL_PASS_DEPTH_FAIL', + 2966: 'STENCIL_PASS_DEPTH_PASS', + 2967: 'STENCIL_REF', + 2968: 'STENCIL_WRITEMASK', + 2978: 'VIEWPORT', + 3024: 'DITHER', + 3042: 'BLEND', + 3088: 'SCISSOR_BOX', + 3089: 'SCISSOR_TEST', + 3106: 'COLOR_CLEAR_VALUE', + 3107: 'COLOR_WRITEMASK', + 3317: 'UNPACK_ALIGNMENT', + 3333: 'PACK_ALIGNMENT', + 3379: 'MAX_TEXTURE_SIZE', + 3386: 'MAX_VIEWPORT_DIMS', + 3408: 'SUBPIXEL_BITS', + 3410: 'RED_BITS', + 3411: 'GREEN_BITS', + 3412: 'BLUE_BITS', + 3413: 'ALPHA_BITS', + 3414: 'DEPTH_BITS', + 3415: 'STENCIL_BITS', + 3553: 'TEXTURE_2D', + 4352: 'DONT_CARE', + 4353: 'FASTEST', + 4354: 'NICEST', + 5120: 'BYTE', + 5121: 'UNSIGNED_BYTE', + 5122: 'SHORT', + 5123: 'UNSIGNED_SHORT', + 5124: 'INT', + 5125: 'UNSIGNED_INT', + 5126: 'FLOAT', + 5386: 'INVERT', + 5890: 'TEXTURE', + 6401: 'STENCIL_INDEX', + 6402: 'DEPTH_COMPONENT', + 6406: 'ALPHA', + 6407: 'RGB', + 6408: 'RGBA', + 6409: 'LUMINANCE', + 6410: 'LUMINANCE_ALPHA', + 7680: 'KEEP', + 7681: 'REPLACE', + 7682: 'INCR', + 7683: 'DECR', + 7936: 'VENDOR', + 7937: 'RENDERER', + 7938: 'VERSION', + 9728: 'NEAREST', + 9729: 'LINEAR', + 9984: 'NEAREST_MIPMAP_NEAREST', + 9985: 'LINEAR_MIPMAP_NEAREST', + 9986: 'NEAREST_MIPMAP_LINEAR', + 9987: 'LINEAR_MIPMAP_LINEAR', + 10240: 'TEXTURE_MAG_FILTER', + 10241: 'TEXTURE_MIN_FILTER', + 10242: 'TEXTURE_WRAP_S', + 10243: 'TEXTURE_WRAP_T', + 10497: 'REPEAT', + 10752: 'POLYGON_OFFSET_UNITS', + 16384: 'COLOR_BUFFER_BIT', + 32769: 'CONSTANT_COLOR', + 32770: 'ONE_MINUS_CONSTANT_COLOR', + 32771: 'CONSTANT_ALPHA', + 32772: 'ONE_MINUS_CONSTANT_ALPHA', + 32773: 'BLEND_COLOR', + 32774: 'FUNC_ADD', + 32777: 'BLEND_EQUATION_RGB', + 32778: 'FUNC_SUBTRACT', + 32779: 'FUNC_REVERSE_SUBTRACT', + 32819: 'UNSIGNED_SHORT_4_4_4_4', + 32820: 'UNSIGNED_SHORT_5_5_5_1', + 32823: 'POLYGON_OFFSET_FILL', + 32824: 'POLYGON_OFFSET_FACTOR', + 32854: 'RGBA4', + 32855: 'RGB5_A1', + 32873: 'TEXTURE_BINDING_2D', + 32926: 'SAMPLE_ALPHA_TO_COVERAGE', + 32928: 'SAMPLE_COVERAGE', + 32936: 'SAMPLE_BUFFERS', + 32937: 'SAMPLES', + 32938: 'SAMPLE_COVERAGE_VALUE', + 32939: 'SAMPLE_COVERAGE_INVERT', + 32968: 'BLEND_DST_RGB', + 32969: 'BLEND_SRC_RGB', + 32970: 'BLEND_DST_ALPHA', + 32971: 'BLEND_SRC_ALPHA', + 33071: 'CLAMP_TO_EDGE', + 33170: 'GENERATE_MIPMAP_HINT', + 33189: 'DEPTH_COMPONENT16', + 33306: 'DEPTH_STENCIL_ATTACHMENT', + 33635: 'UNSIGNED_SHORT_5_6_5', + 33648: 'MIRRORED_REPEAT', + 33901: 'ALIASED_POINT_SIZE_RANGE', + 33902: 'ALIASED_LINE_WIDTH_RANGE', + 33984: 'TEXTURE0', + 33985: 'TEXTURE1', + 33986: 'TEXTURE2', + 33987: 'TEXTURE3', + 33988: 'TEXTURE4', + 33989: 'TEXTURE5', + 33990: 'TEXTURE6', + 33991: 'TEXTURE7', + 33992: 'TEXTURE8', + 33993: 'TEXTURE9', + 33994: 'TEXTURE10', + 33995: 'TEXTURE11', + 33996: 'TEXTURE12', + 33997: 'TEXTURE13', + 33998: 'TEXTURE14', + 33999: 'TEXTURE15', + 34000: 'TEXTURE16', + 34001: 'TEXTURE17', + 34002: 'TEXTURE18', + 34003: 'TEXTURE19', + 34004: 'TEXTURE20', + 34005: 'TEXTURE21', + 34006: 'TEXTURE22', + 34007: 'TEXTURE23', + 34008: 'TEXTURE24', + 34009: 'TEXTURE25', + 34010: 'TEXTURE26', + 34011: 'TEXTURE27', + 34012: 'TEXTURE28', + 34013: 'TEXTURE29', + 34014: 'TEXTURE30', + 34015: 'TEXTURE31', + 34016: 'ACTIVE_TEXTURE', + 34024: 'MAX_RENDERBUFFER_SIZE', + 34041: 'DEPTH_STENCIL', + 34055: 'INCR_WRAP', + 34056: 'DECR_WRAP', + 34067: 'TEXTURE_CUBE_MAP', + 34068: 'TEXTURE_BINDING_CUBE_MAP', + 34069: 'TEXTURE_CUBE_MAP_POSITIVE_X', + 34070: 'TEXTURE_CUBE_MAP_NEGATIVE_X', + 34071: 'TEXTURE_CUBE_MAP_POSITIVE_Y', + 34072: 'TEXTURE_CUBE_MAP_NEGATIVE_Y', + 34073: 'TEXTURE_CUBE_MAP_POSITIVE_Z', + 34074: 'TEXTURE_CUBE_MAP_NEGATIVE_Z', + 34076: 'MAX_CUBE_MAP_TEXTURE_SIZE', + 34338: 'VERTEX_ATTRIB_ARRAY_ENABLED', + 34339: 'VERTEX_ATTRIB_ARRAY_SIZE', + 34340: 'VERTEX_ATTRIB_ARRAY_STRIDE', + 34341: 'VERTEX_ATTRIB_ARRAY_TYPE', + 34342: 'CURRENT_VERTEX_ATTRIB', + 34373: 'VERTEX_ATTRIB_ARRAY_POINTER', + 34466: 'NUM_COMPRESSED_TEXTURE_FORMATS', + 34467: 'COMPRESSED_TEXTURE_FORMATS', + 34660: 'BUFFER_SIZE', + 34661: 'BUFFER_USAGE', + 34816: 'STENCIL_BACK_FUNC', + 34817: 'STENCIL_BACK_FAIL', + 34818: 'STENCIL_BACK_PASS_DEPTH_FAIL', + 34819: 'STENCIL_BACK_PASS_DEPTH_PASS', + 34877: 'BLEND_EQUATION_ALPHA', + 34921: 'MAX_VERTEX_ATTRIBS', + 34922: 'VERTEX_ATTRIB_ARRAY_NORMALIZED', + 34930: 'MAX_TEXTURE_IMAGE_UNITS', + 34962: 'ARRAY_BUFFER', + 34963: 'ELEMENT_ARRAY_BUFFER', + 34964: 'ARRAY_BUFFER_BINDING', + 34965: 'ELEMENT_ARRAY_BUFFER_BINDING', + 34975: 'VERTEX_ATTRIB_ARRAY_BUFFER_BINDING', + 35040: 'STREAM_DRAW', + 35044: 'STATIC_DRAW', + 35048: 'DYNAMIC_DRAW', + 35632: 'FRAGMENT_SHADER', + 35633: 'VERTEX_SHADER', + 35660: 'MAX_VERTEX_TEXTURE_IMAGE_UNITS', + 35661: 'MAX_COMBINED_TEXTURE_IMAGE_UNITS', + 35663: 'SHADER_TYPE', + 35664: 'FLOAT_VEC2', + 35665: 'FLOAT_VEC3', + 35666: 'FLOAT_VEC4', + 35667: 'INT_VEC2', + 35668: 'INT_VEC3', + 35669: 'INT_VEC4', + 35670: 'BOOL', + 35671: 'BOOL_VEC2', + 35672: 'BOOL_VEC3', + 35673: 'BOOL_VEC4', + 35674: 'FLOAT_MAT2', + 35675: 'FLOAT_MAT3', + 35676: 'FLOAT_MAT4', + 35678: 'SAMPLER_2D', + 35680: 'SAMPLER_CUBE', + 35712: 'DELETE_STATUS', + 35713: 'COMPILE_STATUS', + 35714: 'LINK_STATUS', + 35715: 'VALIDATE_STATUS', + 35716: 'INFO_LOG_LENGTH', + 35717: 'ATTACHED_SHADERS', + 35718: 'ACTIVE_UNIFORMS', + 35719: 'ACTIVE_UNIFORM_MAX_LENGTH', + 35720: 'SHADER_SOURCE_LENGTH', + 35721: 'ACTIVE_ATTRIBUTES', + 35722: 'ACTIVE_ATTRIBUTE_MAX_LENGTH', + 35724: 'SHADING_LANGUAGE_VERSION', + 35725: 'CURRENT_PROGRAM', + 36003: 'STENCIL_BACK_REF', + 36004: 'STENCIL_BACK_VALUE_MASK', + 36005: 'STENCIL_BACK_WRITEMASK', + 36006: 'FRAMEBUFFER_BINDING', + 36007: 'RENDERBUFFER_BINDING', + 36048: 'FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE', + 36049: 'FRAMEBUFFER_ATTACHMENT_OBJECT_NAME', + 36050: 'FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL', + 36051: 'FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE', + 36053: 'FRAMEBUFFER_COMPLETE', + 36054: 'FRAMEBUFFER_INCOMPLETE_ATTACHMENT', + 36055: 'FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT', + 36057: 'FRAMEBUFFER_INCOMPLETE_DIMENSIONS', + 36061: 'FRAMEBUFFER_UNSUPPORTED', + 36064: 'COLOR_ATTACHMENT0', + 36096: 'DEPTH_ATTACHMENT', + 36128: 'STENCIL_ATTACHMENT', + 36160: 'FRAMEBUFFER', + 36161: 'RENDERBUFFER', + 36162: 'RENDERBUFFER_WIDTH', + 36163: 'RENDERBUFFER_HEIGHT', + 36164: 'RENDERBUFFER_INTERNAL_FORMAT', + 36168: 'STENCIL_INDEX8', + 36176: 'RENDERBUFFER_RED_SIZE', + 36177: 'RENDERBUFFER_GREEN_SIZE', + 36178: 'RENDERBUFFER_BLUE_SIZE', + 36179: 'RENDERBUFFER_ALPHA_SIZE', + 36180: 'RENDERBUFFER_DEPTH_SIZE', + 36181: 'RENDERBUFFER_STENCIL_SIZE', + 36194: 'RGB565', + 36336: 'LOW_FLOAT', + 36337: 'MEDIUM_FLOAT', + 36338: 'HIGH_FLOAT', + 36339: 'LOW_INT', + 36340: 'MEDIUM_INT', + 36341: 'HIGH_INT', + 36346: 'SHADER_COMPILER', + 36347: 'MAX_VERTEX_UNIFORM_VECTORS', + 36348: 'MAX_VARYING_VECTORS', + 36349: 'MAX_FRAGMENT_UNIFORM_VECTORS', + 37440: 'UNPACK_FLIP_Y_WEBGL', + 37441: 'UNPACK_PREMULTIPLY_ALPHA_WEBGL', + 37442: 'CONTEXT_LOST_WEBGL', + 37443: 'UNPACK_COLORSPACE_CONVERSION_WEBGL', + 37444: 'BROWSER_DEFAULT_WEBGL' +} + +},{}],134:[function(require,module,exports){ +var gl10 = require('./1.0/numbers') + +module.exports = function lookupConstant (number) { + return gl10[number] +} + +},{"./1.0/numbers":133}],135:[function(require,module,exports){ +'use strict' + +var createShader = require('gl-shader') +var createBuffer = require('gl-buffer') +var pool = require('typedarray-pool') +var shaders = require('./lib/shaders') + +module.exports = createError2D + +var WEIGHTS = [ + // x-error bar + [1, 0, 0, 1, 0, 0], + [1, 0, 0, -1, 0, 0], + [-1, 0, 0, -1, 0, 0], + + [-1, 0, 0, -1, 0, 0], + [-1, 0, 0, 1, 0, 0], + [1, 0, 0, 1, 0, 0], + + // x-error right cap + [1, 0, -1, 0, 0, 1], + [1, 0, -1, 0, 0, -1], + [1, 0, 1, 0, 0, -1], + + [1, 0, 1, 0, 0, -1], + [1, 0, 1, 0, 0, 1], + [1, 0, -1, 0, 0, 1], + + // x-error left cap + [-1, 0, -1, 0, 0, 1], + [-1, 0, -1, 0, 0, -1], + [-1, 0, 1, 0, 0, -1], + + [-1, 0, 1, 0, 0, -1], + [-1, 0, 1, 0, 0, 1], + [-1, 0, -1, 0, 0, 1], + + // y-error bar + [0, 1, 1, 0, 0, 0], + [0, 1, -1, 0, 0, 0], + [0, -1, -1, 0, 0, 0], + + [0, -1, -1, 0, 0, 0], + [0, 1, 1, 0, 0, 0], + [0, -1, 1, 0, 0, 0], + + // y-error top cap + [0, 1, 0, -1, 1, 0], + [0, 1, 0, -1, -1, 0], + [0, 1, 0, 1, -1, 0], + + [0, 1, 0, 1, 1, 0], + [0, 1, 0, -1, 1, 0], + [0, 1, 0, 1, -1, 0], + + // y-error bottom cap + [0, -1, 0, -1, 1, 0], + [0, -1, 0, -1, -1, 0], + [0, -1, 0, 1, -1, 0], + + [0, -1, 0, 1, 1, 0], + [0, -1, 0, -1, 1, 0], + [0, -1, 0, 1, -1, 0] +] + +function GLError2D (plot, shader, bufferHi, bufferLo) { + this.plot = plot + + this.shader = shader + this.bufferHi = bufferHi + this.bufferLo = bufferLo + + this.bounds = [Infinity, Infinity, -Infinity, -Infinity] + + this.numPoints = 0 + + this.color = [0, 0, 0, 1] +} + +var proto = GLError2D.prototype + +proto.draw = (function () { + var SCALE_HI = new Float32Array([0, 0]) + var SCALE_LO = new Float32Array([0, 0]) + var TRANSLATE_HI = new Float32Array([0, 0]) + var TRANSLATE_LO = new Float32Array([0, 0]) + + var PIXEL_SCALE = [1, 1] + + return function () { + var plot = this.plot + var shader = this.shader + var bounds = this.bounds + var numPoints = this.numPoints + + if (!numPoints) { + return + } + + var gl = plot.gl + var dataBox = plot.dataBox + var viewBox = plot.viewBox + var pixelRatio = plot.pixelRatio + + var boundX = bounds[2] - bounds[0] + var boundY = bounds[3] - bounds[1] + var dataX = dataBox[2] - dataBox[0] + var dataY = dataBox[3] - dataBox[1] + + var scaleX = 2 * boundX / dataX + var scaleY = 2 * boundY / dataY + var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX + var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY + + SCALE_HI[0] = scaleX + SCALE_HI[1] = scaleY + SCALE_LO[0] = scaleX - SCALE_HI[0] + SCALE_LO[1] = scaleY - SCALE_HI[1] + TRANSLATE_HI[0] = translateX + TRANSLATE_HI[1] = translateY + TRANSLATE_LO[0] = translateX - TRANSLATE_HI[0] + TRANSLATE_LO[1] = translateY - TRANSLATE_HI[1] + + var screenX = viewBox[2] - viewBox[0] + var screenY = viewBox[3] - viewBox[1] + + PIXEL_SCALE[0] = 2.0 * pixelRatio / screenX + PIXEL_SCALE[1] = 2.0 * pixelRatio / screenY + + shader.bind() + + shader.uniforms.scaleHi = SCALE_HI + shader.uniforms.scaleLo = SCALE_LO + shader.uniforms.translateHi = TRANSLATE_HI + shader.uniforms.translateLo = TRANSLATE_LO + shader.uniforms.pixelScale = PIXEL_SCALE + shader.uniforms.color = this.color + + this.bufferLo.bind() + shader.attributes.positionLo.pointer(gl.FLOAT, false, 16, 0) + + this.bufferHi.bind() + shader.attributes.positionHi.pointer(gl.FLOAT, false, 16, 0) + + shader.attributes.pixelOffset.pointer(gl.FLOAT, false, 16, 8) + + gl.drawArrays(gl.TRIANGLES, 0, numPoints * WEIGHTS.length) + } +})() + +proto.drawPick = function (offset) { return offset } +proto.pick = function () { + return null +} + +proto.update = function (options) { + options = options || {} + + var i, x, y + + var positions = options.positions || [] + var errors = options.errors || [] + + var lineWidth = 1 + if ('lineWidth' in options) { + lineWidth = +options.lineWidth + } + + var capSize = 5 + if ('capSize' in options) { + capSize = +options.capSize + } + + this.color = (options.color || [0, 0, 0, 1]).slice() + + var bounds = this.bounds = [Infinity, Infinity, -Infinity, -Infinity] + + var numPoints = this.numPoints = positions.length >> 1 + for (i = 0; i < numPoints; ++i) { + x = positions[i * 2] + y = positions[i * 2 + 1] + + bounds[0] = Math.min(x, bounds[0]) + bounds[1] = Math.min(y, bounds[1]) + bounds[2] = Math.max(x, bounds[2]) + bounds[3] = Math.max(y, bounds[3]) + } + if (bounds[2] === bounds[0]) { + bounds[2] += 1 + } + if (bounds[3] === bounds[1]) { + bounds[3] += 1 + } + var sx = 1.0 / (bounds[2] - bounds[0]) + var sy = 1.0 / (bounds[3] - bounds[1]) + var tx = bounds[0] + var ty = bounds[1] + + var bufferData = pool.mallocFloat64(numPoints * WEIGHTS.length * 4) + var bufferDataHi = pool.mallocFloat32(numPoints * WEIGHTS.length * 4) + var bufferDataLo = pool.mallocFloat32(numPoints * WEIGHTS.length * 4) + var ptr = 0 + for (i = 0; i < numPoints; ++i) { + x = positions[2 * i] + y = positions[2 * i + 1] + var ex0 = errors[4 * i] + var ex1 = errors[4 * i + 1] + var ey0 = errors[4 * i + 2] + var ey1 = errors[4 * i + 3] + + for (var j = 0; j < WEIGHTS.length; ++j) { + var w = WEIGHTS[j] + + var dx = w[0] + var dy = w[1] + + if (dx < 0) { + dx *= ex0 + } else if (dx > 0) { + dx *= ex1 + } + + if (dy < 0) { + dy *= ey0 + } else if (dy > 0) { + dy *= ey1 + } + + bufferData[ptr++] = sx * ((x - tx) + dx) + bufferData[ptr++] = sy * ((y - ty) + dy) + bufferData[ptr++] = lineWidth * w[2] + (capSize + lineWidth) * w[4] + bufferData[ptr++] = lineWidth * w[3] + (capSize + lineWidth) * w[5] + } + } + for(i = 0; i < bufferData.length; i++) { + bufferDataHi[i] = bufferData[i] + bufferDataLo[i] = bufferData[i] - bufferDataHi[i] + } + this.bufferHi.update(bufferDataHi) + this.bufferLo.update(bufferDataLo) + pool.free(bufferData) +} + +proto.dispose = function () { + this.plot.removeObject(this) + this.shader.dispose() + this.bufferHi.dispose() + this.bufferLo.dispose() +} + +function createError2D (plot, options) { + var shader = createShader(plot.gl, shaders.vertex, shaders.fragment) + var bufferHi = createBuffer(plot.gl) + var bufferLo = createBuffer(plot.gl) + + var errorBars = new GLError2D(plot, shader, bufferHi, bufferLo) + + errorBars.update(options) + + plot.addObject(errorBars) + + return errorBars +} + +},{"./lib/shaders":136,"gl-buffer":132,"gl-shader":227,"typedarray-pool":992}],136:[function(require,module,exports){ + + +module.exports = { + vertex: "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi;\nattribute vec2 positionLo;\nattribute vec2 pixelOffset;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale;\n\nvec2 project(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\nvoid main() {\n vec3 scrPosition = vec3(\n project(scaleHi, translateHi, scaleLo, translateLo, positionHi, positionLo),\n 1);\n gl_Position = vec4(\n scrPosition.xy + scrPosition.z * pixelScale * pixelOffset,\n 0,\n scrPosition.z);\n}\n", + fragment: "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color;\n\nvoid main() {\n gl_FragColor = vec4(color.rgb * color.a, color.a);\n}\n" +} + +},{}],137:[function(require,module,exports){ +'use strict' + +module.exports = createErrorBars + +var createBuffer = require('gl-buffer') +var createVAO = require('gl-vao') +var createShader = require('./shaders/index') + +var IDENTITY = [1,0,0,0, + 0,1,0,0, + 0,0,1,0, + 0,0,0,1] + +function ErrorBars(gl, buffer, vao, shader) { + this.gl = gl + this.shader = shader + this.buffer = buffer + this.vao = vao + this.pixelRatio = 1 + this.bounds = [[ Infinity, Infinity, Infinity], [-Infinity,-Infinity,-Infinity]] + this.clipBounds = [[-Infinity,-Infinity,-Infinity], [ Infinity, Infinity, Infinity]] + this.lineWidth = [1,1,1] + this.capSize = [10,10,10] + this.lineCount = [0,0,0] + this.lineOffset = [0,0,0] + this.opacity = 1 +} + +var proto = ErrorBars.prototype + +proto.isOpaque = function() { + return this.opacity >= 1 +} + +proto.isTransparent = function() { + return this.opacity < 1 +} + +proto.drawTransparent = proto.draw = function(cameraParams) { + var gl = this.gl + var uniforms = this.shader.uniforms + + this.shader.bind() + var view = uniforms.view = cameraParams.view || IDENTITY + var projection = uniforms.projection = cameraParams.projection || IDENTITY + uniforms.model = cameraParams.model || IDENTITY + uniforms.clipBounds = this.clipBounds + uniforms.opacity = this.opacity + + + var cx = view[12] + var cy = view[13] + var cz = view[14] + var cw = view[15] + var pixelScaleF = this.pixelRatio * (projection[3]*cx + projection[7]*cy + projection[11]*cz + projection[15]*cw) / gl.drawingBufferHeight + + + this.vao.bind() + for(var i=0; i<3; ++i) { + gl.lineWidth(this.lineWidth[i]) + uniforms.capSize = this.capSize[i] * pixelScaleF + gl.drawArrays(gl.LINES, this.lineOffset[i], this.lineCount[i]) + } + this.vao.unbind() +} + +function updateBounds(bounds, point) { + for(var i=0; i<3; ++i) { + bounds[0][i] = Math.min(bounds[0][i], point[i]) + bounds[1][i] = Math.max(bounds[1][i], point[i]) + } +} + +var FACE_TABLE = (function(){ + var table = new Array(3) + for(var d=0; d<3; ++d) { + var row = [] + for(var j=1; j<=2; ++j) { + for(var s=-1; s<=1; s+=2) { + var u = (j+d) % 3 + var y = [0,0,0] + y[u] = s + row.push(y) + } + } + table[d] = row + } + return table +})() + + +function emitFace(verts, x, c, d) { + var offsets = FACE_TABLE[d] + for(var i=0; i 0) { + var x = p.slice() + x[j] += e[1][j] + verts.push(p[0], p[1], p[2], + c[0], c[1], c[2], c[3], + 0, 0, 0, + x[0], x[1], x[2], + c[0], c[1], c[2], c[3], + 0, 0, 0) + updateBounds(this.bounds, x) + vertexCount += 2 + emitFace(verts, x, c, j) + } + } + this.lineCount[j] = vertexCount - this.lineOffset[j] + } + this.buffer.update(verts) + } +} + +proto.dispose = function() { + this.shader.dispose() + this.buffer.dispose() + this.vao.dispose() +} + +function createErrorBars(options) { + var gl = options.gl + var buffer = createBuffer(gl) + var vao = createVAO(gl, [ + { + buffer: buffer, + type: gl.FLOAT, + size: 3, + offset: 0, + stride: 40 + }, + { + buffer: buffer, + type: gl.FLOAT, + size: 4, + offset: 12, + stride: 40 + }, + { + buffer: buffer, + type: gl.FLOAT, + size: 3, + offset: 28, + stride: 40 + } + ]) + + var shader = createShader(gl) + shader.attributes.position.location = 0 + shader.attributes.color.location = 1 + shader.attributes.offset.location = 2 + + var result = new ErrorBars(gl, buffer, vao, shader) + result.update(options) + return result +} + +},{"./shaders/index":138,"gl-buffer":132,"gl-vao":243}],138:[function(require,module,exports){ +'use strict' + + +var createShader = require('gl-shader') + +var vertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position, offset;\nattribute vec4 color;\nuniform mat4 model, view, projection;\nuniform float capSize;\nvarying vec4 fragColor;\nvarying vec3 fragPosition;\n\nvoid main() {\n vec4 worldPosition = model * vec4(position, 1.0);\n worldPosition = (worldPosition / worldPosition.w) + vec4(capSize * offset, 0.0);\n gl_Position = projection * view * worldPosition;\n fragColor = color;\n fragPosition = position;\n}" +var fragSrc = "precision mediump float;\n#define GLSLIFY 1\nuniform vec3 clipBounds[2];\nuniform float opacity;\nvarying vec3 fragPosition;\nvarying vec4 fragColor;\n\nvoid main() {\n if(any(lessThan(fragPosition, clipBounds[0])) || any(greaterThan(fragPosition, clipBounds[1]))) {\n discard;\n }\n gl_FragColor = opacity * fragColor;\n}" + +module.exports = function(gl) { + return createShader(gl, vertSrc, fragSrc, null, [ + {name: 'position', type: 'vec3'}, + {name: 'offset', type: 'vec3'}, + {name: 'color', type: 'vec4'} + ]) +} + +},{"gl-shader":227}],139:[function(require,module,exports){ +'use strict' + +var createTexture = require('gl-texture2d') + +module.exports = createFBO + +var colorAttachmentArrays = null +var FRAMEBUFFER_UNSUPPORTED +var FRAMEBUFFER_INCOMPLETE_ATTACHMENT +var FRAMEBUFFER_INCOMPLETE_DIMENSIONS +var FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT + +function saveFBOState(gl) { + var fbo = gl.getParameter(gl.FRAMEBUFFER_BINDING) + var rbo = gl.getParameter(gl.RENDERBUFFER_BINDING) + var tex = gl.getParameter(gl.TEXTURE_BINDING_2D) + return [fbo, rbo, tex] +} + +function restoreFBOState(gl, data) { + gl.bindFramebuffer(gl.FRAMEBUFFER, data[0]) + gl.bindRenderbuffer(gl.RENDERBUFFER, data[1]) + gl.bindTexture(gl.TEXTURE_2D, data[2]) +} + +function lazyInitColorAttachments(gl, ext) { + var maxColorAttachments = gl.getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL) + colorAttachmentArrays = new Array(maxColorAttachments + 1) + for(var i=0; i<=maxColorAttachments; ++i) { + var x = new Array(maxColorAttachments) + for(var j=0; j 1) { + ext.drawBuffersWEBGL(colorAttachmentArrays[numColors]) + } + + //Allocate depth/stencil buffers + var WEBGL_depth_texture = gl.getExtension('WEBGL_depth_texture') + if(WEBGL_depth_texture) { + if(useStencil) { + fbo.depth = initTexture(gl, width, height, + WEBGL_depth_texture.UNSIGNED_INT_24_8_WEBGL, + gl.DEPTH_STENCIL, + gl.DEPTH_STENCIL_ATTACHMENT) + } else if(useDepth) { + fbo.depth = initTexture(gl, width, height, + gl.UNSIGNED_SHORT, + gl.DEPTH_COMPONENT, + gl.DEPTH_ATTACHMENT) + } + } else { + if(useDepth && useStencil) { + fbo._depth_rb = initRenderBuffer(gl, width, height, gl.DEPTH_STENCIL, gl.DEPTH_STENCIL_ATTACHMENT) + } else if(useDepth) { + fbo._depth_rb = initRenderBuffer(gl, width, height, gl.DEPTH_COMPONENT16, gl.DEPTH_ATTACHMENT) + } else if(useStencil) { + fbo._depth_rb = initRenderBuffer(gl, width, height, gl.STENCIL_INDEX, gl.STENCIL_ATTACHMENT) + } + } + + //Check frame buffer state + var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER) + if(status !== gl.FRAMEBUFFER_COMPLETE) { + + //Release all partially allocated resources + fbo._destroyed = true + + //Release all resources + gl.bindFramebuffer(gl.FRAMEBUFFER, null) + gl.deleteFramebuffer(fbo.handle) + fbo.handle = null + if(fbo.depth) { + fbo.depth.dispose() + fbo.depth = null + } + if(fbo._depth_rb) { + gl.deleteRenderbuffer(fbo._depth_rb) + fbo._depth_rb = null + } + for(var i=0; i maxFBOSize || + h < 0 || h > maxFBOSize) { + throw new Error('gl-fbo: Can\'t resize FBO, invalid dimensions') + } + + //Update shape + fbo._shape[0] = w + fbo._shape[1] = h + + //Save framebuffer state + var state = saveFBOState(gl) + + //Resize framebuffer attachments + for(var i=0; i maxFBOSize || height < 0 || height > maxFBOSize) { + throw new Error('gl-fbo: Parameters are too large for FBO') + } + + //Handle each option type + options = options || {} + + //Figure out number of color buffers to use + var numColors = 1 + if('color' in options) { + numColors = Math.max(options.color|0, 0) + if(numColors < 0) { + throw new Error('gl-fbo: Must specify a nonnegative number of colors') + } + if(numColors > 1) { + //Check if multiple render targets supported + if(!WEBGL_draw_buffers) { + throw new Error('gl-fbo: Multiple draw buffer extension not supported') + } else if(numColors > gl.getParameter(WEBGL_draw_buffers.MAX_COLOR_ATTACHMENTS_WEBGL)) { + throw new Error('gl-fbo: Context does not support ' + numColors + ' draw buffers') + } + } + } + + //Determine whether to use floating point textures + var colorType = gl.UNSIGNED_BYTE + var OES_texture_float = gl.getExtension('OES_texture_float') + if(options.float && numColors > 0) { + if(!OES_texture_float) { + throw new Error('gl-fbo: Context does not support floating point textures') + } + colorType = gl.FLOAT + } else if(options.preferFloat && numColors > 0) { + if(OES_texture_float) { + colorType = gl.FLOAT + } + } + + //Check if we should use depth buffer + var useDepth = true + if('depth' in options) { + useDepth = !!options.depth + } + + //Check if we should use a stencil buffer + var useStencil = false + if('stencil' in options) { + useStencil = !!options.stencil + } + + return new Framebuffer( + gl, + width, + height, + colorType, + numColors, + useDepth, + useStencil, + WEBGL_draw_buffers) +} + +},{"gl-texture2d":239}],140:[function(require,module,exports){ + +var sprintf = require('sprintf-js').sprintf; +var glConstants = require('gl-constants/lookup'); +var shaderName = require('glsl-shader-name'); +var addLineNumbers = require('add-line-numbers'); + +module.exports = formatCompilerError; + +function formatCompilerError(errLog, src, type) { + "use strict"; + + var name = shaderName(src) || 'of unknown name (see npm glsl-shader-name)'; + + var typeName = 'unknown type'; + if (type !== undefined) { + typeName = type === glConstants.FRAGMENT_SHADER ? 'fragment' : 'vertex' + } + + var longForm = sprintf('Error compiling %s shader %s:\n', typeName, name); + var shortForm = sprintf("%s%s", longForm, errLog); + + var errorStrings = errLog.split('\n'); + var errors = {}; + + for (var i = 0; i < errorStrings.length; i++) { + var errorString = errorStrings[i]; + if (errorString === '') continue; + var lineNo = parseInt(errorString.split(':')[2]); + if (isNaN(lineNo)) { + throw new Error(sprintf('Could not parse error: %s', errorString)); + } + errors[lineNo] = errorString; + } + + var lines = addLineNumbers(src).split('\n'); + + for (var i = 0; i < lines.length; i++) { + if (!errors[i+3] && !errors[i+2] && !errors[i+1]) continue; + var line = lines[i]; + longForm += line + '\n'; + if (errors[i+1]) { + var e = errors[i+1]; + e = e.substr(e.split(':', 3).join(':').length + 1).trim(); + longForm += sprintf('^^^ %s\n\n', e); + } + } + + return { + long: longForm.trim(), + short: shortForm.trim() + }; +} + + +},{"add-line-numbers":4,"gl-constants/lookup":134,"glsl-shader-name":251,"sprintf-js":977}],141:[function(require,module,exports){ +'use strict' + +module.exports = createHeatmap2D + +var bsearch = require('binary-search-bounds') +var iota = require('iota-array') +var pool = require('typedarray-pool') +var createShader = require('gl-shader') +var createBuffer = require('gl-buffer') + +var shaders = require('./lib/shaders') + +function GLHeatmap2D ( + plot, + shader, + pickShader, + positionBuffer, + weightBuffer, + colorBuffer, + idBuffer) { + this.plot = plot + this.shader = shader + this.pickShader = pickShader + this.positionBuffer = positionBuffer + this.weightBuffer = weightBuffer + this.colorBuffer = colorBuffer + this.idBuffer = idBuffer + this.xData = [] + this.yData = [] + this.shape = [0, 0] + this.bounds = [Infinity, Infinity, -Infinity, -Infinity] + this.pickOffset = 0 +} + +var proto = GLHeatmap2D.prototype + +var WEIGHTS = [ + 0, 0, + 1, 0, + 0, 1, + 1, 0, + 1, 1, + 0, 1 +] + +proto.draw = (function () { + var MATRIX = [ + 1, 0, 0, + 0, 1, 0, + 0, 0, 1 + ] + + return function () { + var plot = this.plot + var shader = this.shader + var bounds = this.bounds + var numVertices = this.numVertices + + if (numVertices <= 0) { + return + } + + var gl = plot.gl + var dataBox = plot.dataBox + + var boundX = bounds[2] - bounds[0] + var boundY = bounds[3] - bounds[1] + var dataX = dataBox[2] - dataBox[0] + var dataY = dataBox[3] - dataBox[1] + + MATRIX[0] = 2.0 * boundX / dataX + MATRIX[4] = 2.0 * boundY / dataY + MATRIX[6] = 2.0 * (bounds[0] - dataBox[0]) / dataX - 1.0 + MATRIX[7] = 2.0 * (bounds[1] - dataBox[1]) / dataY - 1.0 + + shader.bind() + + var uniforms = shader.uniforms + uniforms.viewTransform = MATRIX + + uniforms.shape = this.shape + + var attributes = shader.attributes + this.positionBuffer.bind() + attributes.position.pointer() + + this.weightBuffer.bind() + attributes.weight.pointer(gl.UNSIGNED_BYTE, false) + + this.colorBuffer.bind() + attributes.color.pointer(gl.UNSIGNED_BYTE, true) + + gl.drawArrays(gl.TRIANGLES, 0, numVertices) + } +})() + +proto.drawPick = (function () { + var MATRIX = [ + 1, 0, 0, + 0, 1, 0, + 0, 0, 1 + ] + + var PICK_VECTOR = [0, 0, 0, 0] + + return function (pickOffset) { + var plot = this.plot + var shader = this.pickShader + var bounds = this.bounds + var numVertices = this.numVertices + + if (numVertices <= 0) { + return + } + + var gl = plot.gl + var dataBox = plot.dataBox + + var boundX = bounds[2] - bounds[0] + var boundY = bounds[3] - bounds[1] + var dataX = dataBox[2] - dataBox[0] + var dataY = dataBox[3] - dataBox[1] + + MATRIX[0] = 2.0 * boundX / dataX + MATRIX[4] = 2.0 * boundY / dataY + MATRIX[6] = 2.0 * (bounds[0] - dataBox[0]) / dataX - 1.0 + MATRIX[7] = 2.0 * (bounds[1] - dataBox[1]) / dataY - 1.0 + + for (var i = 0; i < 4; ++i) { + PICK_VECTOR[i] = (pickOffset >> (i * 8)) & 0xff + } + + this.pickOffset = pickOffset + + shader.bind() + + var uniforms = shader.uniforms + uniforms.viewTransform = MATRIX + uniforms.pickOffset = PICK_VECTOR + uniforms.shape = this.shape + + var attributes = shader.attributes + this.positionBuffer.bind() + attributes.position.pointer() + + this.weightBuffer.bind() + attributes.weight.pointer(gl.UNSIGNED_BYTE, false) + + this.idBuffer.bind() + attributes.pickId.pointer(gl.UNSIGNED_BYTE, false) + + gl.drawArrays(gl.TRIANGLES, 0, numVertices) + + return pickOffset + this.shape[0] * this.shape[1] + } +})() + +proto.pick = function (x, y, value) { + var pickOffset = this.pickOffset + var pointCount = this.shape[0] * this.shape[1] + if (value < pickOffset || value >= pickOffset + pointCount) { + return null + } + var pointId = value - pickOffset + var xData = this.xData + var yData = this.yData + return { + object: this, + pointId: pointId, + dataCoord: [ + xData[pointId % this.shape[0]], + yData[(pointId / this.shape[0]) | 0]] + } +} + +proto.update = function (options) { + options = options || {} + + var shape = options.shape || [0, 0] + + var x = options.x || iota(shape[0]) + var y = options.y || iota(shape[1]) + var z = options.z || new Float32Array(shape[0] * shape[1]) + + this.xData = x + this.yData = y + + var colorLevels = options.colorLevels || [0] + var colorValues = options.colorValues || [0, 0, 0, 1] + var colorCount = colorLevels.length + + var bounds = this.bounds + var lox = bounds[0] = x[0] + var loy = bounds[1] = y[0] + var hix = bounds[2] = x[x.length - 1] + var hiy = bounds[3] = y[y.length - 1] + + var xs = 1.0 / (hix - lox) + var ys = 1.0 / (hiy - loy) + + var numX = shape[0] + var numY = shape[1] + + this.shape = [numX, numY] + + var numVerts = (numX - 1) * (numY - 1) * (WEIGHTS.length >>> 1) + + this.numVertices = numVerts + + var colors = pool.mallocUint8(numVerts * 4) + var positions = pool.mallocFloat32(numVerts * 2) + var weights = pool.mallocUint8 (numVerts * 2) + var ids = pool.mallocUint32(numVerts) + + var ptr = 0 + + for (var j = 0; j < numY - 1; ++j) { + var yc0 = ys * (y[j] - loy) + var yc1 = ys * (y[j + 1] - loy) + for (var i = 0; i < numX - 1; ++i) { + var xc0 = xs * (x[i] - lox) + var xc1 = xs * (x[i + 1] - lox) + + for (var dd = 0; dd < WEIGHTS.length; dd += 2) { + var dx = WEIGHTS[dd] + var dy = WEIGHTS[dd + 1] + var offset = (j + dy) * numX + (i + dx) + var zc = z[offset] + var colorIdx = bsearch.le(colorLevels, zc) + var r, g, b, a + if (colorIdx < 0) { + r = colorValues[0] + g = colorValues[1] + b = colorValues[2] + a = colorValues[3] + } else if (colorIdx === colorCount - 1) { + r = colorValues[4 * colorCount - 4] + g = colorValues[4 * colorCount - 3] + b = colorValues[4 * colorCount - 2] + a = colorValues[4 * colorCount - 1] + } else { + var t = (zc - colorLevels[colorIdx]) / + (colorLevels[colorIdx + 1] - colorLevels[colorIdx]) + var ti = 1.0 - t + var i0 = 4 * colorIdx + var i1 = 4 * (colorIdx + 1) + r = ti * colorValues[i0] + t * colorValues[i1] + g = ti * colorValues[i0 + 1] + t * colorValues[i1 + 1] + b = ti * colorValues[i0 + 2] + t * colorValues[i1 + 2] + a = ti * colorValues[i0 + 3] + t * colorValues[i1 + 3] + } + + colors[4 * ptr] = 255 * r + colors[4 * ptr + 1] = 255 * g + colors[4 * ptr + 2] = 255 * b + colors[4 * ptr + 3] = 255 * a + + positions[2*ptr] = xc0*.5 + xc1*.5; + positions[2*ptr+1] = yc0*.5 + yc1*.5; + + weights[2*ptr] = dx; + weights[2*ptr+1] = dy; + + ids[ptr] = j * numX + i + + ptr += 1 + } + } + } + + this.positionBuffer.update(positions) + this.weightBuffer.update(weights) + this.colorBuffer.update(colors) + this.idBuffer.update(ids) + + pool.free(positions) + pool.free(colors) + pool.free(weights) + pool.free(ids) +} + +proto.dispose = function () { + this.shader.dispose() + this.pickShader.dispose() + this.positionBuffer.dispose() + this.weightBuffer.dispose() + this.colorBuffer.dispose() + this.idBuffer.dispose() + this.plot.removeObject(this) +} + +function createHeatmap2D (plot, options) { + var gl = plot.gl + + var shader = createShader(gl, shaders.vertex, shaders.fragment) + var pickShader = createShader(gl, shaders.pickVertex, shaders.pickFragment) + + var positionBuffer = createBuffer(gl) + var weightBuffer = createBuffer(gl) + var colorBuffer = createBuffer(gl) + var idBuffer = createBuffer(gl) + + var heatmap = new GLHeatmap2D( + plot, + shader, + pickShader, + positionBuffer, + weightBuffer, + colorBuffer, + idBuffer) + + heatmap.update(options) + plot.addObject(heatmap) + + return heatmap +} + +},{"./lib/shaders":142,"binary-search-bounds":143,"gl-buffer":132,"gl-shader":227,"iota-array":269,"typedarray-pool":992}],142:[function(require,module,exports){ +'use strict' + + + +module.exports = { + fragment: "precision lowp float;\n#define GLSLIFY 1\nvarying vec4 fragColor;\nvoid main() {\n gl_FragColor = vec4(fragColor.rgb * fragColor.a, fragColor.a);\n}\n", + vertex: "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec2 position;\nattribute vec4 color;\nattribute vec2 weight;\n\nuniform vec2 shape;\nuniform mat3 viewTransform;\n\nvarying vec4 fragColor;\n\nvoid main() {\n vec3 vPosition = viewTransform * vec3( position + (weight-.5)/(shape-1.) , 1.0);\n fragColor = color;\n gl_Position = vec4(vPosition.xy, 0, vPosition.z);\n}\n", + pickFragment: "precision mediump float;\n#define GLSLIFY 1\n\nvarying vec4 fragId;\nvarying vec2 vWeight;\n\nuniform vec2 shape;\nuniform vec4 pickOffset;\n\nvoid main() {\n vec2 d = step(.5, vWeight);\n vec4 id = fragId + pickOffset;\n id.x += d.x + d.y*shape.x;\n\n id.y += floor(id.x / 256.0);\n id.x -= floor(id.x / 256.0) * 256.0;\n\n id.z += floor(id.y / 256.0);\n id.y -= floor(id.y / 256.0) * 256.0;\n\n id.w += floor(id.z / 256.0);\n id.z -= floor(id.z / 256.0) * 256.0;\n\n gl_FragColor = id/255.;\n}\n", + pickVertex: "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec2 position;\nattribute vec4 pickId;\nattribute vec2 weight;\n\nuniform vec2 shape;\nuniform mat3 viewTransform;\n\nvarying vec4 fragId;\nvarying vec2 vWeight;\n\nvoid main() {\n vWeight = weight;\n\n fragId = pickId;\n\n vec3 vPosition = viewTransform * vec3( position + (weight-.5)/(shape-1.) , 1.0);\n gl_Position = vec4(vPosition.xy, 0, vPosition.z);\n}\n" +} + +},{}],143:[function(require,module,exports){ +arguments[4][54][0].apply(exports,arguments) +},{"dup":54}],144:[function(require,module,exports){ + + +exports.lineVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo, dHi, dLo;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo, screenShape;\nuniform float width;\n\nvarying vec2 direction;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvec2 project_2_1(vec2 scHi, vec2 scLo, vec2 posHi, vec2 posLo) {\n return scHi * posHi\n + scLo * posHi\n + scHi * posLo\n + scLo * posLo;\n}\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n vec2 dir = project_2_1(scaleHi, scaleLo, dHi, dLo);\n vec2 n = 0.5 * width * normalize(screenShape.yx * vec2(dir.y, -dir.x)) / screenShape.xy;\n vec2 tangent = normalize(screenShape.xy * dir);\n if(dir.x < 0.0 || (dir.x == 0.0 && dir.y < 0.0)) {\n direction = -tangent;\n } else {\n direction = tangent;\n }\n gl_Position = vec4(p + n, 0.0, 1.0);\n}" +exports.lineFragment = "precision highp float;\n#define GLSLIFY 1\n\nuniform vec4 color;\nuniform vec2 screenShape;\nuniform sampler2D dashPattern;\nuniform float dashLength;\n\nvarying vec2 direction;\n\nvoid main() {\n float t = fract(dot(direction, gl_FragCoord.xy) / dashLength);\n vec4 pcolor = color * texture2D(dashPattern, vec2(t, 0.0)).r;\n gl_FragColor = vec4(pcolor.rgb * pcolor.a, pcolor.a);\n}" +exports.mitreVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo;\nuniform float radius;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n gl_Position = vec4(p, 0.0, 1.0);\n gl_PointSize = radius;\n}" +exports.mitreFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color;\n\nvoid main() {\n if(length(gl_PointCoord.xy - 0.5) > 0.25) {\n discard;\n }\n gl_FragColor = vec4(color.rgb, color.a);\n}" +exports.pickVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo, dHi;\nattribute vec4 pick0, pick1;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo, screenShape;\nuniform float width;\n\nvarying vec4 pickA, pickB;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n vec2 n = width * normalize(screenShape.yx * vec2(dHi.y, -dHi.x)) / screenShape.xy;\n gl_Position = vec4(p + n, 0, 1);\n pickA = pick0;\n pickB = pick1;\n}" +exports.pickFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 pickOffset;\n\nvarying vec4 pickA, pickB;\n\nvoid main() {\n vec4 fragId = vec4(pickA.xyz, 0.0);\n if(pickB.w > pickA.w) {\n fragId.xyz = pickB.xyz;\n }\n\n fragId += pickOffset;\n\n fragId.y += floor(fragId.x / 256.0);\n fragId.x -= floor(fragId.x / 256.0) * 256.0;\n\n fragId.z += floor(fragId.y / 256.0);\n fragId.y -= floor(fragId.y / 256.0) * 256.0;\n\n fragId.w += floor(fragId.z / 256.0);\n fragId.z -= floor(fragId.z / 256.0) * 256.0;\n\n gl_FragColor = fragId / 255.0;\n}" +exports.fillVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo, dHi;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo, projectAxis;\nuniform float projectValue, depth;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n if(dHi.y < 0.0 || (dHi.y == 0.0 && dHi.x < 0.0)) {\n if(dot(p, projectAxis) < projectValue) {\n p = p * (1.0 - abs(projectAxis)) + projectAxis * projectValue;\n }\n }\n gl_Position = vec4(p, depth, 1);\n}" +exports.fillFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color;\n\nvoid main() {\n gl_FragColor = vec4(color.rgb * color.a, color.a);\n}" +},{}],145:[function(require,module,exports){ +'use strict' + +module.exports = createLinePlot + +var createShader = require('gl-shader') +var createBuffer = require('gl-buffer') +var createTexture = require('gl-texture2d') +var ndarray = require('ndarray') +var pool = require('typedarray-pool') + +var SHADERS = require('./lib/shaders') + +function GLLine2D( + plot, + dashPattern, + lineBufferHi, + lineBufferLo, + pickBuffer, + lineShader, + mitreShader, + fillShader, + pickShader) { + + this.plot = plot + this.dashPattern = dashPattern + this.lineBufferHi = lineBufferHi + this.lineBufferLo = lineBufferLo + this.pickBuffer = pickBuffer + this.lineShader = lineShader + this.mitreShader = mitreShader + this.fillShader = fillShader + this.pickShader = pickShader + this.usingDashes = false + + this.bounds = [Infinity, Infinity, -Infinity, -Infinity] + this.width = 1 + this.color = [0, 0, 1, 1] + + //Fill to axes + this.fill = [false, false, false, false] + this.fillColor = [ + [0, 0, 0, 1], + [0, 0, 0, 1], + [0, 0, 0, 1], + [0, 0, 0, 1] + ] + + this.data = null + this.numPoints = 0 + this.vertCount = 0 + + this.pickOffset = 0 +} + +var proto = GLLine2D.prototype + +proto.setProjectionModel = (function() { + + var pm = { + scaleHi: new Float32Array([0, 0]), + scaleLo: new Float32Array([0, 0]), + translateHi: new Float32Array([0, 0]), + translateLo: new Float32Array([0, 0]), + screenShape: [0, 0] + } + + return function() { + + var bounds = this.bounds + var viewBox = this.plot.viewBox + var dataBox = this.plot.dataBox + + var boundX = bounds[2] - bounds[0] + var boundY = bounds[3] - bounds[1] + var dataX = dataBox[2] - dataBox[0] + var dataY = dataBox[3] - dataBox[1] + var screenX = viewBox[2] - viewBox[0] + var screenY = viewBox[3] - viewBox[1] + + var scaleX = 2 * boundX / dataX + var scaleY = 2 * boundY / dataY + var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX + var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY + + pm.scaleHi[0] = scaleX + pm.scaleHi[1] = scaleY + pm.scaleLo[0] = scaleX - pm.scaleHi[0] + pm.scaleLo[1] = scaleY - pm.scaleHi[1] + pm.translateHi[0] = translateX + pm.translateHi[1] = translateY + pm.translateLo[0] = translateX - pm.translateHi[0] + pm.translateLo[1] = translateY - pm.translateHi[1] + + pm.screenShape[0] = screenX + pm.screenShape[1] = screenY + + return pm + } +})() + +proto.setProjectionUniforms = function(uniforms, projectionModel) { + uniforms.scaleHi = projectionModel.scaleHi + uniforms.scaleLo = projectionModel.scaleLo + uniforms.translateHi = projectionModel.translateHi + uniforms.translateLo = projectionModel.translateLo + uniforms.screenShape = projectionModel.screenShape +} + +proto.draw = (function() { + + var PX_AXIS = [1, 0] + var NX_AXIS = [-1, 0] + var PY_AXIS = [0, 1] + var NY_AXIS = [0, -1] + + return function() { + var count = this.vertCount + + if(!count) { + return + } + + var projectionModel = this.setProjectionModel() + + var plot = this.plot + var width = this.width + var gl = plot.gl + var pixelRatio = plot.pixelRatio + + var color = this.color + + var fillAttributes = this.fillShader.attributes + + this.lineBufferLo.bind() + fillAttributes.aLo.pointer(gl.FLOAT, false, 16, 0) + + this.lineBufferHi.bind() + + var fill = this.fill + + if(fill[0] || fill[1] || fill[2] || fill[3]) { + + var fillShader = this.fillShader + fillShader.bind() + + var fillUniforms = fillShader.uniforms + this.setProjectionUniforms(fillUniforms, projectionModel) + fillUniforms.depth = plot.nextDepthValue() + + fillAttributes.aHi.pointer(gl.FLOAT, false, 16, 0) + fillAttributes.dHi.pointer(gl.FLOAT, false, 16, 8) + + gl.depthMask(true) + gl.enable(gl.DEPTH_TEST) + + var fillColor = this.fillColor + if(fill[0]) { + fillUniforms.color = fillColor[0] + fillUniforms.projectAxis = NX_AXIS + fillUniforms.projectValue = 1 + gl.drawArrays(gl.TRIANGLES, 0, count) + } + + if(fill[1]) { + fillUniforms.color = fillColor[1] + fillUniforms.projectAxis = NY_AXIS + fillUniforms.projectValue = 1 + gl.drawArrays(gl.TRIANGLES, 0, count) + } + + if(fill[2]) { + fillUniforms.color = fillColor[2] + fillUniforms.projectAxis = PX_AXIS + fillUniforms.projectValue = 1 + gl.drawArrays(gl.TRIANGLES, 0, count) + } + + if(fill[3]) { + fillUniforms.color = fillColor[3] + fillUniforms.projectAxis = PY_AXIS + fillUniforms.projectValue = 1 + gl.drawArrays(gl.TRIANGLES, 0, count) + } + + gl.depthMask(false) + gl.disable(gl.DEPTH_TEST) + } + + var shader = this.lineShader + shader.bind() + + this.lineBufferLo.bind() + shader.attributes.aLo.pointer(gl.FLOAT, false, 16, 0) + shader.attributes.dLo.pointer(gl.FLOAT, false, 16, 8) + + this.lineBufferHi.bind() + + var uniforms = shader.uniforms + this.setProjectionUniforms(uniforms, projectionModel) + uniforms.color = color + uniforms.width = width * pixelRatio + uniforms.dashPattern = this.dashPattern.bind() + uniforms.dashLength = this.dashLength * pixelRatio + + var attributes = shader.attributes + attributes.aHi.pointer(gl.FLOAT, false, 16, 0) + attributes.dHi.pointer(gl.FLOAT, false, 16, 8) + + gl.drawArrays(gl.TRIANGLES, 0, count) + + //Draw mitres + if(width > 2 && !this.usingDashes) { + var mshader = this.mitreShader + + this.lineBufferLo.bind() + mshader.attributes.aLo.pointer(gl.FLOAT, false, 48, 0) + + this.lineBufferHi.bind() + mshader.bind() + + var muniforms = mshader.uniforms + this.setProjectionUniforms(muniforms, projectionModel) + muniforms.color = color + muniforms.radius = width * pixelRatio + + mshader.attributes.aHi.pointer(gl.FLOAT, false, 48, 0) + gl.drawArrays(gl.POINTS, 0, (count / 3) | 0) + } + } +})() + +proto.drawPick = (function() { + + var PICK_OFFSET = [0, 0, 0, 0] + + return function(pickOffset) { + + var count = this.vertCount + var numPoints = this.numPoints + + this.pickOffset = pickOffset + if(!count) { + return pickOffset + numPoints + } + + var projectionModel = this.setProjectionModel() + + var plot = this.plot + var width = this.width + var gl = plot.gl + var pixelRatio = plot.pickPixelRatio + + var shader = this.pickShader + var pickBuffer = this.pickBuffer + + PICK_OFFSET[0] = pickOffset & 0xff + PICK_OFFSET[1] = (pickOffset >>> 8) & 0xff + PICK_OFFSET[2] = (pickOffset >>> 16) & 0xff + PICK_OFFSET[3] = pickOffset >>> 24 + + shader.bind() + + var uniforms = shader.uniforms + this.setProjectionUniforms(uniforms, projectionModel) + uniforms.width = width * pixelRatio + uniforms.pickOffset = PICK_OFFSET + + var attributes = shader.attributes + + this.lineBufferHi.bind() + attributes.aHi.pointer(gl.FLOAT, false, 16, 0) + attributes.dHi.pointer(gl.FLOAT, false, 16, 8) + + this.lineBufferLo.bind() + attributes.aLo.pointer(gl.FLOAT, false, 16, 0) + + //attributes.dLo.pointer(gl.FLOAT, false, 16, 8) + + pickBuffer.bind() + attributes.pick0.pointer(gl.UNSIGNED_BYTE, false, 8, 0) + attributes.pick1.pointer(gl.UNSIGNED_BYTE, false, 8, 4) + + gl.drawArrays(gl.TRIANGLES, 0, count) + + return pickOffset + numPoints + } +})() + +proto.pick = function(x, y, value) { + var pickOffset = this.pickOffset + var pointCount = this.numPoints + if(value < pickOffset || value >= pickOffset + pointCount) { + return null + } + var pointId = value - pickOffset + var points = this.data + return { + object: this, + pointId: pointId, + dataCoord: [points[2 * pointId], points[2 * pointId + 1]] + } +} + +function deepCopy(arr) { + return arr.map(function(x) { + return x.slice() + }) +} + +proto.update = function(options) { + options = options || {} + + var gl = this.plot.gl + var i, j, ptr, ax, ay + + this.color = (options.color || [0, 0, 1, 1]).slice() + this.width = +(options.width || 1) + this.fill = (options.fill || [false, false, false, false]).slice() + this.fillColor = deepCopy(options.fillColor || [ + [0, 0, 0, 1], + [0, 0, 0, 1], + [0, 0, 0, 1], + [0, 0, 0, 1] + ]) + + var dashes = options.dashes || [1] + var dashLength = 0 + for(i = 0; i < dashes.length; ++i) { + dashLength += dashes[i] + } + var dashData = pool.mallocUint8(dashLength) + ptr = 0 + var fillColor = 255 + for(i = 0; i < dashes.length; ++i) { + for(j = 0; j < dashes[i]; ++j) { + dashData[ptr++] = fillColor + } + fillColor ^= 255 + } + this.dashPattern.dispose() + this.usingDashes = dashes.length > 1 + + this.dashPattern = createTexture(gl, ndarray(dashData, [dashLength, 1, 4], [1, 0, 0])) + this.dashPattern.minFilter = gl.NEAREST + this.dashPattern.magFilter = gl.NEAREST + this.dashLength = dashLength + pool.free(dashData) + + var data = options.positions + this.data = data + + var bounds = this.bounds + bounds[0] = bounds[1] = Infinity + bounds[2] = bounds[3] = -Infinity + + var numPoints = this.numPoints = data.length >>> 1 + if(numPoints === 0) { + return + } + + for(i = 0; i < numPoints; ++i) { + ax = data[2 * i] + ay = data[2 * i + 1] + + if (isNaN(ax) || isNaN(ay)) { + continue + } + + bounds[0] = Math.min(bounds[0], ax) + bounds[1] = Math.min(bounds[1], ay) + bounds[2] = Math.max(bounds[2], ax) + bounds[3] = Math.max(bounds[3], ay) + } + + if(bounds[0] === bounds[2]) bounds[2] += 1 + if(bounds[3] === bounds[1]) bounds[3] += 1 + + //Generate line data + var lineData = pool.mallocFloat64(24 * (numPoints - 1)) + var lineDataHi = pool.mallocFloat32(24 * (numPoints - 1)) + var lineDataLo = pool.mallocFloat32(24 * (numPoints - 1)) + var pickData = pool.mallocUint32(12 * (numPoints - 1)) + var lineDataPtr = lineDataHi.length + var pickDataPtr = pickData.length + ptr = numPoints + + var count = 0 + + while(ptr > 1) { + var id = --ptr + ax = data[2 * ptr] + ay = data[2 * ptr + 1] + + var next = id - 1 + var bx = data[2 * next] + var by = data[2 * next + 1] + + if (isNaN(ax) || isNaN(ay) || isNaN(bx) || isNaN(by)) { + continue + } + + count += 1 + + ax = (ax - bounds[0]) / (bounds[2] - bounds[0]) + ay = (ay - bounds[1]) / (bounds[3] - bounds[1]) + + bx = (bx - bounds[0]) / (bounds[2] - bounds[0]) + by = (by - bounds[1]) / (bounds[3] - bounds[1]) + + var dx = bx - ax + var dy = by - ay + + var akey0 = id | (1 << 24) + var akey1 = (id - 1) + var bkey0 = id + var bkey1 = (id - 1) | (1 << 24) + + lineData[--lineDataPtr] = -dy + lineData[--lineDataPtr] = -dx + lineData[--lineDataPtr] = ay + lineData[--lineDataPtr] = ax + pickData[--pickDataPtr] = akey0 + pickData[--pickDataPtr] = akey1 + + lineData[--lineDataPtr] = dy + lineData[--lineDataPtr] = dx + lineData[--lineDataPtr] = by + lineData[--lineDataPtr] = bx + pickData[--pickDataPtr] = bkey0 + pickData[--pickDataPtr] = bkey1 + + lineData[--lineDataPtr] = -dy + lineData[--lineDataPtr] = -dx + lineData[--lineDataPtr] = by + lineData[--lineDataPtr] = bx + pickData[--pickDataPtr] = bkey0 + pickData[--pickDataPtr] = bkey1 + + lineData[--lineDataPtr] = dy + lineData[--lineDataPtr] = dx + lineData[--lineDataPtr] = by + lineData[--lineDataPtr] = bx + pickData[--pickDataPtr] = bkey0 + pickData[--pickDataPtr] = bkey1 + + lineData[--lineDataPtr] = -dy + lineData[--lineDataPtr] = -dx + lineData[--lineDataPtr] = ay + lineData[--lineDataPtr] = ax + pickData[--pickDataPtr] = akey0 + pickData[--pickDataPtr] = akey1 + + lineData[--lineDataPtr] = dy + lineData[--lineDataPtr] = dx + lineData[--lineDataPtr] = ay + lineData[--lineDataPtr] = ax + pickData[--pickDataPtr] = akey0 + pickData[--pickDataPtr] = akey1 + } + + for(i = 0; i < lineData.length; i++) { + lineDataHi[i] = lineData[i] + lineDataLo[i] = lineData[i] - lineDataHi[i] + } + + this.vertCount = 6 * count + this.lineBufferHi.update(lineDataHi.subarray(lineDataPtr)) + this.lineBufferLo.update(lineDataLo.subarray(lineDataPtr)) + this.pickBuffer.update(pickData.subarray(pickDataPtr)) + + pool.free(lineData) + pool.free(lineDataHi) + pool.free(lineDataLo) + pool.free(pickData) +} + +proto.dispose = function() { + this.plot.removeObject(this) + this.lineBufferLo.dispose() + this.lineBufferHi.dispose() + this.pickBuffer.dispose() + this.lineShader.dispose() + this.mitreShader.dispose() + this.fillShader.dispose() + this.pickShader.dispose() + this.dashPattern.dispose() +} + +function createLinePlot(plot, options) { + var gl = plot.gl + var lineBufferHi = createBuffer(gl) + var lineBufferLo = createBuffer(gl) + var pickBuffer = createBuffer(gl) + var dashPattern = createTexture(gl, [1, 1]) + var lineShader = createShader(gl, SHADERS.lineVertex, SHADERS.lineFragment) + var mitreShader = createShader(gl, SHADERS.mitreVertex, SHADERS.mitreFragment) + var fillShader = createShader(gl, SHADERS.fillVertex, SHADERS.fillFragment) + var pickShader = createShader(gl, SHADERS.pickVertex, SHADERS.pickFragment) + var linePlot = new GLLine2D( + plot, + dashPattern, + lineBufferHi, + lineBufferLo, + pickBuffer, + lineShader, + mitreShader, + fillShader, + pickShader) + plot.addObject(linePlot) + linePlot.update(options) + return linePlot +} +},{"./lib/shaders":144,"gl-buffer":132,"gl-shader":227,"gl-texture2d":239,"ndarray":443,"typedarray-pool":992}],146:[function(require,module,exports){ + +var createShader = require('gl-shader') + +var vertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position, nextPosition;\nattribute float arcLength, lineWidth;\nattribute vec4 color;\n\nuniform vec2 screenShape;\nuniform float pixelRatio;\nuniform mat4 model, view, projection;\n\nvarying vec4 fragColor;\nvarying vec3 worldPosition;\nvarying float pixelArcLength;\n\nvoid main() {\n vec4 projected = projection * view * model * vec4(position, 1.0);\n vec4 tangentClip = projection * view * model * vec4(nextPosition - position, 0.0);\n vec2 tangent = normalize(screenShape * tangentClip.xy);\n vec2 offset = 0.5 * pixelRatio * lineWidth * vec2(tangent.y, -tangent.x) / screenShape;\n\n gl_Position = vec4(projected.xy + projected.w * offset, projected.zw);\n\n worldPosition = position;\n pixelArcLength = arcLength;\n fragColor = color;\n}\n" +var forwardFrag = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 clipBounds[2];\nuniform sampler2D dashTexture;\nuniform float dashScale;\nuniform float opacity;\n\nvarying vec3 worldPosition;\nvarying float pixelArcLength;\nvarying vec4 fragColor;\n\nvoid main() {\n if(any(lessThan(worldPosition, clipBounds[0])) || any(greaterThan(worldPosition, clipBounds[1]))) {\n discard;\n }\n float dashWeight = texture2D(dashTexture, vec2(dashScale * pixelArcLength, 0)).r;\n if(dashWeight < 0.5) {\n discard;\n }\n gl_FragColor = fragColor * opacity;\n}\n" +var pickFrag = "precision mediump float;\n#define GLSLIFY 1\n\n#define FLOAT_MAX 1.70141184e38\n#define FLOAT_MIN 1.17549435e-38\n\nlowp vec4 encode_float_1_0(highp float v) {\n highp float av = abs(v);\n\n //Handle special cases\n if(av < FLOAT_MIN) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n } else if(v > FLOAT_MAX) {\n return vec4(127.0, 128.0, 0.0, 0.0) / 255.0;\n } else if(v < -FLOAT_MAX) {\n return vec4(255.0, 128.0, 0.0, 0.0) / 255.0;\n }\n\n highp vec4 c = vec4(0,0,0,0);\n\n //Compute exponent and mantissa\n highp float e = floor(log2(av));\n highp float m = av * pow(2.0, -e) - 1.0;\n \n //Unpack mantissa\n c[1] = floor(128.0 * m);\n m -= c[1] / 128.0;\n c[2] = floor(32768.0 * m);\n m -= c[2] / 32768.0;\n c[3] = floor(8388608.0 * m);\n \n //Unpack exponent\n highp float ebias = e + 127.0;\n c[0] = floor(ebias / 2.0);\n ebias -= c[0] * 2.0;\n c[1] += floor(ebias) * 128.0; \n\n //Unpack sign bit\n c[0] += 128.0 * step(0.0, -v);\n\n //Scale back to range\n return c / 255.0;\n}\n\n\n\nuniform float pickId;\nuniform vec3 clipBounds[2];\n\nvarying vec3 worldPosition;\nvarying float pixelArcLength;\nvarying vec4 fragColor;\n\nvoid main() {\n if(any(lessThan(worldPosition, clipBounds[0])) || any(greaterThan(worldPosition, clipBounds[1]))) {\n discard;\n }\n gl_FragColor = vec4(pickId/255.0, encode_float_1_0(pixelArcLength).xyz);\n}" + +var ATTRIBUTES = [ + {name: 'position', type: 'vec3'}, + {name: 'nextPosition', type: 'vec3'}, + {name: 'arcLength', type: 'float'}, + {name: 'lineWidth', type: 'float'}, + {name: 'color', type: 'vec4'} +] + +exports.createShader = function(gl) { + return createShader(gl, vertSrc, forwardFrag, null, ATTRIBUTES) +} + +exports.createPickShader = function(gl) { + return createShader(gl, vertSrc, pickFrag, null, ATTRIBUTES) +} + +},{"gl-shader":227}],147:[function(require,module,exports){ +'use strict' + +module.exports = createLinePlot + +var createBuffer = require('gl-buffer') +var createVAO = require('gl-vao') +var createTexture = require('gl-texture2d') +var unpackFloat = require('glsl-read-float') +var bsearch = require('binary-search-bounds') +var ndarray = require('ndarray') +var shaders = require('./lib/shaders') + +var createShader = shaders.createShader +var createPickShader = shaders.createPickShader + +var identity = [1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1] + +function distance (a, b) { + var s = 0.0 + for (var i = 0; i < 3; ++i) { + var d = a[i] - b[i] + s += d * d + } + return Math.sqrt(s) +} + +function filterClipBounds (bounds) { + var result = [[-1e6, -1e6, -1e6], [1e6, 1e6, 1e6]] + for (var i = 0; i < 3; ++i) { + result[0][i] = Math.max(bounds[0][i], result[0][i]) + result[1][i] = Math.min(bounds[1][i], result[1][i]) + } + return result +} + +function PickResult (tau, position, index, dataCoordinate) { + this.arcLength = tau + this.position = position + this.index = index + this.dataCoordinate = dataCoordinate +} + +function LinePlot (gl, shader, pickShader, buffer, vao, texture) { + this.gl = gl + this.shader = shader + this.pickShader = pickShader + this.buffer = buffer + this.vao = vao + this.clipBounds = [ + [ -Infinity, -Infinity, -Infinity ], + [ Infinity, Infinity, Infinity ]] + this.points = [] + this.arcLength = [] + this.vertexCount = 0 + this.bounds = [[0, 0, 0], [0, 0, 0]] + this.pickId = 0 + this.lineWidth = 1 + this.texture = texture + this.dashScale = 1 + this.opacity = 1 + this.dirty = true + this.pixelRatio = 1 +} + +var proto = LinePlot.prototype + +proto.isTransparent = function () { + return this.opacity < 1 +} + +proto.isOpaque = function () { + return this.opacity >= 1 +} + +proto.pickSlots = 1 + +proto.setPickBase = function (id) { + this.pickId = id +} + +proto.drawTransparent = proto.draw = function (camera) { + var gl = this.gl + var shader = this.shader + var vao = this.vao + shader.bind() + shader.uniforms = { + model: camera.model || identity, + view: camera.view || identity, + projection: camera.projection || identity, + clipBounds: filterClipBounds(this.clipBounds), + dashTexture: this.texture.bind(), + dashScale: this.dashScale / this.arcLength[this.arcLength.length - 1], + opacity: this.opacity, + screenShape: [gl.drawingBufferWidth, gl.drawingBufferHeight], + pixelRatio: this.pixelRatio + } + vao.bind() + vao.draw(gl.TRIANGLE_STRIP, this.vertexCount) +} + +proto.drawPick = function (camera) { + var gl = this.gl + var shader = this.pickShader + var vao = this.vao + shader.bind() + shader.uniforms = { + model: camera.model || identity, + view: camera.view || identity, + projection: camera.projection || identity, + pickId: this.pickId, + clipBounds: filterClipBounds(this.clipBounds), + screenShape: [gl.drawingBufferWidth, gl.drawingBufferHeight], + pixelRatio: this.pixelRatio + } + vao.bind() + vao.draw(gl.TRIANGLE_STRIP, this.vertexCount) +} + +proto.update = function (options) { + var i, j + + this.dirty = true + + var connectGaps = !!options.connectGaps + + if ('dashScale' in options) { + this.dashScale = options.dashScale + } + if ('opacity' in options) { + this.opacity = +options.opacity + } + + var positions = options.position || options.positions + if (!positions) { + return + } + + // Default color + var colors = options.color || options.colors || [0, 0, 0, 1] + + var lineWidth = options.lineWidth || 1 + + // Recalculate buffer data + var buffer = [] + var arcLengthArray = [] + var pointArray = [] + var arcLength = 0.0 + var vertexCount = 0 + var bounds = [ + [ Infinity, Infinity, Infinity ], + [ -Infinity, -Infinity, -Infinity ]] + var hadGap = false + + fill_loop: + for (i = 1; i < positions.length; ++i) { + var a = positions[i - 1] + var b = positions[i] + + arcLengthArray.push(arcLength) + pointArray.push(a.slice()) + + for (j = 0; j < 3; ++j) { + if (isNaN(a[j]) || isNaN(b[j]) || + !isFinite(a[j]) || !isFinite(b[j])) { + + if (!connectGaps && buffer.length > 0) { + for (var k = 0; k < 24; ++k) { + buffer.push(buffer[buffer.length - 12]) + } + vertexCount += 2 + hadGap = true + } + + continue fill_loop + } + bounds[0][j] = Math.min(bounds[0][j], a[j], b[j]) + bounds[1][j] = Math.max(bounds[1][j], a[j], b[j]) + } + + var acolor, bcolor + if (Array.isArray(colors[0])) { + acolor = colors[i - 1] + bcolor = colors[i] + } else { + acolor = bcolor = colors + } + if (acolor.length === 3) { + acolor = [acolor[0], acolor[1], acolor[2], 1] + } + if (bcolor.length === 3) { + bcolor = [bcolor[0], bcolor[1], bcolor[2], 1] + } + + var w0 + if (Array.isArray(lineWidth)) { + w0 = lineWidth[i - 1] + } else { + w0 = lineWidth + } + + var t0 = arcLength + arcLength += distance(a, b) + + if (hadGap) { + for (j = 0; j < 2; ++j) { + buffer.push( + a[0], a[1], a[2], b[0], b[1], b[2], t0, w0, acolor[0], acolor[1], acolor[2], acolor[3]) + } + vertexCount += 2 + hadGap = false + } + + buffer.push( + a[0], a[1], a[2], b[0], b[1], b[2], t0, w0, acolor[0], acolor[1], acolor[2], acolor[3], + a[0], a[1], a[2], b[0], b[1], b[2], t0, -w0, acolor[0], acolor[1], acolor[2], acolor[3], + b[0], b[1], b[2], a[0], a[1], a[2], arcLength, -w0, bcolor[0], bcolor[1], bcolor[2], bcolor[3], + b[0], b[1], b[2], a[0], a[1], a[2], arcLength, w0, bcolor[0], bcolor[1], bcolor[2], bcolor[3]) + + vertexCount += 4 + } + this.buffer.update(buffer) + + arcLengthArray.push(arcLength) + pointArray.push(positions[positions.length - 1].slice()) + + this.bounds = bounds + + this.vertexCount = vertexCount + + this.points = pointArray + this.arcLength = arcLengthArray + + if ('dashes' in options) { + var dashArray = options.dashes + + // Calculate prefix sum + var prefixSum = dashArray.slice() + prefixSum.unshift(0) + for (i = 1; i < prefixSum.length; ++i) { + prefixSum[i] = prefixSum[i - 1] + prefixSum[i] + } + + var dashTexture = ndarray(new Array(256 * 4), [256, 1, 4]) + for (i = 0; i < 256; ++i) { + for (j = 0; j < 4; ++j) { + dashTexture.set(i, 0, j, 0) + } + if (bsearch.le(prefixSum, prefixSum[prefixSum.length - 1] * i / 255.0) & 1) { + dashTexture.set(i, 0, 0, 0) + } else { + dashTexture.set(i, 0, 0, 255) + } + } + + this.texture.setPixels(dashTexture) + } +} + +proto.dispose = function () { + this.shader.dispose() + this.vao.dispose() + this.buffer.dispose() +} + +proto.pick = function (selection) { + if (!selection) { + return null + } + if (selection.id !== this.pickId) { + return null + } + var tau = unpackFloat( + selection.value[0], + selection.value[1], + selection.value[2], + 0) + var index = bsearch.le(this.arcLength, tau) + if (index < 0) { + return null + } + if (index === this.arcLength.length - 1) { + return new PickResult( + this.arcLength[this.arcLength.length - 1], + this.points[this.points.length - 1].slice(), + index) + } + var a = this.points[index] + var b = this.points[Math.min(index + 1, this.points.length - 1)] + var t = (tau - this.arcLength[index]) / (this.arcLength[index + 1] - this.arcLength[index]) + var ti = 1.0 - t + var x = [0, 0, 0] + for (var i = 0; i < 3; ++i) { + x[i] = ti * a[i] + t * b[i] + } + var dataIndex = Math.min((t < 0.5) ? index : (index + 1), this.points.length - 1) + return new PickResult( + tau, + x, + dataIndex, + this.points[dataIndex]) +} + +function createLinePlot (options) { + var gl = options.gl || (options.scene && options.scene.gl) + + var shader = createShader(gl) + shader.attributes.position.location = 0 + shader.attributes.nextPosition.location = 1 + shader.attributes.arcLength.location = 2 + shader.attributes.lineWidth.location = 3 + shader.attributes.color.location = 4 + + var pickShader = createPickShader(gl) + pickShader.attributes.position.location = 0 + pickShader.attributes.nextPosition.location = 1 + pickShader.attributes.arcLength.location = 2 + pickShader.attributes.lineWidth.location = 3 + pickShader.attributes.color.location = 4 + + var buffer = createBuffer(gl) + var vao = createVAO(gl, [ + { + 'buffer': buffer, + 'size': 3, + 'offset': 0, + 'stride': 48 + }, + { + 'buffer': buffer, + 'size': 3, + 'offset': 12, + 'stride': 48 + }, + { + 'buffer': buffer, + 'size': 1, + 'offset': 24, + 'stride': 48 + }, + { + 'buffer': buffer, + 'size': 1, + 'offset': 28, + 'stride': 48 + }, + { + 'buffer': buffer, + 'size': 4, + 'offset': 32, + 'stride': 48 + } + ]) + + // Create texture for dash pattern + var defaultTexture = ndarray(new Array(256 * 4), [256, 1, 4]) + for (var i = 0; i < 256 * 4; ++i) { + defaultTexture.data[i] = 255 + } + var texture = createTexture(gl, defaultTexture) + texture.wrap = gl.REPEAT + + var linePlot = new LinePlot(gl, shader, pickShader, buffer, vao, texture) + linePlot.update(options) + return linePlot +} + +},{"./lib/shaders":146,"binary-search-bounds":32,"gl-buffer":132,"gl-texture2d":239,"gl-vao":243,"glsl-read-float":250,"ndarray":443}],148:[function(require,module,exports){ +module.exports = invert + +/** + * Inverts a mat2 + * + * @alias mat2.invert + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +function invert(out, a) { + var a0 = a[0] + var a1 = a[1] + var a2 = a[2] + var a3 = a[3] + var det = a0 * a3 - a2 * a1 + + if (!det) return null + det = 1.0 / det + + out[0] = a3 * det + out[1] = -a1 * det + out[2] = -a2 * det + out[3] = a0 * det + + return out +} + +},{}],149:[function(require,module,exports){ +module.exports = invert + +/** + * Inverts a mat3 + * + * @alias mat3.invert + * @param {mat3} out the receiving matrix + * @param {mat3} a the source matrix + * @returns {mat3} out + */ +function invert(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 +} + +},{}],150:[function(require,module,exports){ +module.exports = clone; + +/** + * Creates a new mat4 initialized with values from an existing matrix + * + * @param {mat4} a matrix to clone + * @returns {mat4} a new 4x4 matrix + */ +function clone(a) { + var out = new Float32Array(16); + 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; +}; +},{}],151:[function(require,module,exports){ +module.exports = create; + +/** + * Creates a new identity mat4 + * + * @returns {mat4} a new 4x4 matrix + */ +function create() { + var out = new Float32Array(16); + 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; +}; +},{}],152:[function(require,module,exports){ +module.exports = determinant; + +/** + * Calculates the determinant of a mat4 + * + * @param {mat4} 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], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], + + b00 = a00 * a11 - a01 * a10, + b01 = a00 * a12 - a02 * a10, + b02 = a00 * a13 - a03 * a10, + b03 = a01 * a12 - a02 * a11, + b04 = a01 * a13 - a03 * a11, + b05 = a02 * a13 - a03 * a12, + b06 = a20 * a31 - a21 * a30, + b07 = a20 * a32 - a22 * a30, + b08 = a20 * a33 - a23 * a30, + b09 = a21 * a32 - a22 * a31, + b10 = a21 * a33 - a23 * a31, + b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; +}; +},{}],153:[function(require,module,exports){ +module.exports = fromQuat; + +/** + * Creates a matrix from a quaternion rotation. + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @returns {mat4} out + */ +function fromQuat(out, q) { + var x = q[0], y = q[1], z = q[2], w = q[3], + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + yx = y * x2, + yy = y * y2, + zx = z * x2, + zy = z * y2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + wz = w * z2; + + out[0] = 1 - yy - zz; + out[1] = yx + wz; + out[2] = zx - wy; + out[3] = 0; + + out[4] = yx - wz; + out[5] = 1 - xx - zz; + out[6] = zy + wx; + out[7] = 0; + + out[8] = zx + wy; + out[9] = zy - wx; + out[10] = 1 - xx - yy; + out[11] = 0; + + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + + return out; +}; +},{}],154:[function(require,module,exports){ +module.exports = fromRotationTranslation; + +/** + * Creates a matrix from a quaternion rotation and vector translation + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * var quatMat = mat4.create(); + * quat4.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @param {vec3} v Translation vector + * @returns {mat4} out + */ +function fromRotationTranslation(out, q, v) { + // Quaternion math + var x = q[0], y = q[1], z = q[2], w = q[3], + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + xy = x * y2, + xz = x * z2, + yy = y * y2, + yz = y * z2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + wz = w * z2; + + out[0] = 1 - (yy + zz); + out[1] = xy + wz; + out[2] = xz - wy; + out[3] = 0; + out[4] = xy - wz; + out[5] = 1 - (xx + zz); + out[6] = yz + wx; + out[7] = 0; + out[8] = xz + wy; + out[9] = yz - wx; + out[10] = 1 - (xx + yy); + out[11] = 0; + out[12] = v[0]; + out[13] = v[1]; + out[14] = v[2]; + out[15] = 1; + + return out; +}; +},{}],155:[function(require,module,exports){ +module.exports = identity; + +/** + * 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; +}; +},{}],156:[function(require,module,exports){ +module.exports = invert; + +/** + * Inverts a mat4 + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +function invert(out, a) { + var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], + + b00 = a00 * a11 - a01 * a10, + b01 = a00 * a12 - a02 * a10, + b02 = a00 * a13 - a03 * a10, + b03 = a01 * a12 - a02 * a11, + b04 = a01 * a13 - a03 * a11, + b05 = a02 * a13 - a03 * a12, + b06 = a20 * a31 - a21 * a30, + b07 = a20 * a32 - a22 * a30, + b08 = a20 * a33 - a23 * a30, + b09 = a21 * a32 - a22 * a31, + b10 = a21 * a33 - a23 * a31, + b11 = a22 * a33 - a23 * a32, + + // Calculate the determinant + 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; +}; +},{}],157:[function(require,module,exports){ +var identity = require('./identity'); + +module.exports = lookAt; + +/** + * Generates a look-at matrix with the given eye position, focal point, and up axis + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {vec3} eye Position of the viewer + * @param {vec3} center Point the viewer is looking at + * @param {vec3} up vec3 pointing up + * @returns {mat4} out + */ +function lookAt(out, eye, center, up) { + var x0, x1, x2, y0, y1, y2, z0, z1, z2, len, + eyex = eye[0], + eyey = eye[1], + eyez = eye[2], + upx = up[0], + upy = up[1], + upz = up[2], + centerx = center[0], + centery = center[1], + centerz = center[2]; + + if (Math.abs(eyex - centerx) < 0.000001 && + Math.abs(eyey - centery) < 0.000001 && + Math.abs(eyez - centerz) < 0.000001) { + return identity(out); + } + + z0 = eyex - centerx; + z1 = eyey - centery; + z2 = eyez - centerz; + + len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); + z0 *= len; + z1 *= len; + z2 *= len; + + x0 = upy * z2 - upz * z1; + x1 = upz * z0 - upx * z2; + x2 = upx * z1 - upy * z0; + len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); + if (!len) { + x0 = 0; + x1 = 0; + x2 = 0; + } else { + len = 1 / len; + x0 *= len; + x1 *= len; + x2 *= len; + } + + y0 = z1 * x2 - z2 * x1; + y1 = z2 * x0 - z0 * x2; + y2 = z0 * x1 - z1 * x0; + + len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); + if (!len) { + y0 = 0; + y1 = 0; + y2 = 0; + } else { + len = 1 / len; + y0 *= len; + y1 *= len; + y2 *= len; + } + + out[0] = x0; + out[1] = y0; + out[2] = z0; + out[3] = 0; + out[4] = x1; + out[5] = y1; + out[6] = z1; + out[7] = 0; + out[8] = x2; + out[9] = y2; + out[10] = z2; + out[11] = 0; + out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); + out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); + out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); + out[15] = 1; + + return out; +}; +},{"./identity":155}],158:[function(require,module,exports){ +module.exports = multiply; + +/** + * Multiplies two mat4's + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +function multiply(out, a, b) { + var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; + + // Cache only the current line of the second matrix + var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; + out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + + b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7]; + out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + + b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11]; + out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + + b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15]; + out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + return out; +}; +},{}],159:[function(require,module,exports){ +module.exports = perspective; + +/** + * Generates a perspective projection matrix with the given bounds + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {number} fovy Vertical field of view in radians + * @param {number} aspect Aspect ratio. typically viewport width/height + * @param {number} near Near bound of the frustum + * @param {number} far Far bound of the frustum + * @returns {mat4} out + */ +function perspective(out, fovy, aspect, near, far) { + var f = 1.0 / Math.tan(fovy / 2), + nf = 1 / (near - far); + out[0] = f / aspect; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = f; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = (far + near) * nf; + out[11] = -1; + out[12] = 0; + out[13] = 0; + out[14] = (2 * far * near) * nf; + out[15] = 0; + return out; +}; +},{}],160:[function(require,module,exports){ +module.exports = rotate; + +/** + * Rotates a mat4 by the given angle + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @param {vec3} axis the axis to rotate around + * @returns {mat4} out + */ +function rotate(out, a, rad, axis) { + var x = axis[0], y = axis[1], z = axis[2], + len = Math.sqrt(x * x + y * y + z * z), + s, c, t, + a00, a01, a02, a03, + a10, a11, a12, a13, + a20, a21, a22, a23, + b00, b01, b02, + b10, b11, b12, + b20, b21, b22; + + if (Math.abs(len) < 0.000001) { return null; } + + len = 1 / len; + x *= len; + y *= len; + z *= len; + + s = Math.sin(rad); + c = Math.cos(rad); + t = 1 - c; + + a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; + a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; + a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; + + // Construct the elements of the rotation matrix + b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s; + b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s; + b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c; + + // Perform rotation-specific matrix multiplication + out[0] = a00 * b00 + a10 * b01 + a20 * b02; + out[1] = a01 * b00 + a11 * b01 + a21 * b02; + out[2] = a02 * b00 + a12 * b01 + a22 * b02; + out[3] = a03 * b00 + a13 * b01 + a23 * b02; + out[4] = a00 * b10 + a10 * b11 + a20 * b12; + out[5] = a01 * b10 + a11 * b11 + a21 * b12; + out[6] = a02 * b10 + a12 * b11 + a22 * b12; + out[7] = a03 * b10 + a13 * b11 + a23 * b12; + out[8] = a00 * b20 + a10 * b21 + a20 * b22; + out[9] = a01 * b20 + a11 * b21 + a21 * b22; + out[10] = a02 * b20 + a12 * b21 + a22 * b22; + out[11] = a03 * b20 + a13 * b21 + a23 * b22; + + if (a !== out) { // If the source and destination differ, copy the unchanged last row + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + return out; +}; +},{}],161:[function(require,module,exports){ +module.exports = rotateX; + +/** + * Rotates a matrix by the given angle around the X axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function rotateX(out, a, rad) { + var s = Math.sin(rad), + c = Math.cos(rad), + a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7], + a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + out[4] = a10 * c + a20 * s; + out[5] = a11 * c + a21 * s; + out[6] = a12 * c + a22 * s; + out[7] = a13 * c + a23 * s; + out[8] = a20 * c - a10 * s; + out[9] = a21 * c - a11 * s; + out[10] = a22 * c - a12 * s; + out[11] = a23 * c - a13 * s; + return out; +}; +},{}],162:[function(require,module,exports){ +module.exports = rotateY; + +/** + * Rotates a matrix by the given angle around the Y axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function rotateY(out, a, rad) { + var s = Math.sin(rad), + c = Math.cos(rad), + a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3], + a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + out[0] = a00 * c - a20 * s; + out[1] = a01 * c - a21 * s; + out[2] = a02 * c - a22 * s; + out[3] = a03 * c - a23 * s; + out[8] = a00 * s + a20 * c; + out[9] = a01 * s + a21 * c; + out[10] = a02 * s + a22 * c; + out[11] = a03 * s + a23 * c; + return out; +}; +},{}],163:[function(require,module,exports){ +module.exports = rotateZ; + +/** + * Rotates a matrix by the given angle around the Z axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function rotateZ(out, a, rad) { + var s = Math.sin(rad), + c = Math.cos(rad), + a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3], + a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7]; + + if (a !== out) { // If the source and destination differ, copy the unchanged last row + 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]; + } + + // Perform axis-specific matrix multiplication + out[0] = a00 * c + a10 * s; + out[1] = a01 * c + a11 * s; + out[2] = a02 * c + a12 * s; + out[3] = a03 * c + a13 * s; + out[4] = a10 * c - a00 * s; + out[5] = a11 * c - a01 * s; + out[6] = a12 * c - a02 * s; + out[7] = a13 * c - a03 * s; + return out; +}; +},{}],164:[function(require,module,exports){ +module.exports = scale; + +/** + * Scales the mat4 by the dimensions in the given vec3 + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to scale + * @param {vec3} v the vec3 to scale the matrix by + * @returns {mat4} out + **/ +function scale(out, a, v) { + var x = v[0], y = v[1], z = v[2]; + + out[0] = a[0] * x; + out[1] = a[1] * x; + out[2] = a[2] * x; + out[3] = a[3] * x; + out[4] = a[4] * y; + out[5] = a[5] * y; + out[6] = a[6] * y; + out[7] = a[7] * y; + out[8] = a[8] * z; + out[9] = a[9] * z; + out[10] = a[10] * z; + out[11] = a[11] * z; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + return out; +}; +},{}],165:[function(require,module,exports){ +module.exports = translate; + +/** + * Translate a mat4 by the given vector + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to translate + * @param {vec3} v vector to translate by + * @returns {mat4} out + */ +function translate(out, a, v) { + var x = v[0], y = v[1], z = v[2], + a00, a01, a02, a03, + a10, a11, a12, a13, + a20, a21, a22, a23; + + if (a === out) { + out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; + out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; + out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; + out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; + } else { + a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; + a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; + a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; + + out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03; + out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13; + out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23; + + out[12] = a00 * x + a10 * y + a20 * z + a[12]; + out[13] = a01 * x + a11 * y + a21 * z + a[13]; + out[14] = a02 * x + a12 * y + a22 * z + a[14]; + out[15] = a03 * x + a13 * y + a23 * z + a[15]; + } + + return out; +}; +},{}],166:[function(require,module,exports){ +module.exports = transpose; + +/** + * Transpose the values of a mat4 + * + * @param {mat4} out the receiving matrix + * @param {mat4} 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], + a12 = a[6], a13 = a[7], + 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; +}; +},{}],167:[function(require,module,exports){ +'use strict' + +module.exports = invert + +var invert2 = require('gl-mat2/invert') +var invert3 = require('gl-mat3/invert') +var invert4 = require('gl-mat4/invert') + +function invert(out, M) { + switch(M.length) { + case 0: + break + case 1: + out[0] = 1.0 / M[0] + break + case 4: + invert2(out, M) + break + case 9: + invert3(out, M) + break + case 16: + invert4(out, M) + break + default: + throw new Error('currently supports matrices up to 4x4') + break + } + return out +} +},{"gl-mat2/invert":148,"gl-mat3/invert":149,"gl-mat4/invert":156}],168:[function(require,module,exports){ +/** + * @fileoverview gl-matrix - High performance matrix and vector operations + * @author Brandon Jones + * @author Colin MacKenzie IV + * @version 2.3.2 + */ + +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ +// END HEADER + +exports.glMatrix = require("./gl-matrix/common.js"); +exports.mat2 = require("./gl-matrix/mat2.js"); +exports.mat2d = require("./gl-matrix/mat2d.js"); +exports.mat3 = require("./gl-matrix/mat3.js"); +exports.mat4 = require("./gl-matrix/mat4.js"); +exports.quat = require("./gl-matrix/quat.js"); +exports.vec2 = require("./gl-matrix/vec2.js"); +exports.vec3 = require("./gl-matrix/vec3.js"); +exports.vec4 = require("./gl-matrix/vec4.js"); +},{"./gl-matrix/common.js":169,"./gl-matrix/mat2.js":170,"./gl-matrix/mat2d.js":171,"./gl-matrix/mat3.js":172,"./gl-matrix/mat4.js":173,"./gl-matrix/quat.js":174,"./gl-matrix/vec2.js":175,"./gl-matrix/vec3.js":176,"./gl-matrix/vec4.js":177}],169:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +/** + * @class Common utilities + * @name glMatrix + */ +var glMatrix = {}; + +// Configuration Constants +glMatrix.EPSILON = 0.000001; +glMatrix.ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array; +glMatrix.RANDOM = Math.random; +glMatrix.ENABLE_SIMD = false; + +// Capability detection +glMatrix.SIMD_AVAILABLE = (glMatrix.ARRAY_TYPE === Float32Array) && ('SIMD' in this); +glMatrix.USE_SIMD = glMatrix.ENABLE_SIMD && glMatrix.SIMD_AVAILABLE; + +/** + * Sets the type of array used when creating new vectors and matrices + * + * @param {Type} type Array type, such as Float32Array or Array + */ +glMatrix.setMatrixArrayType = function(type) { + glMatrix.ARRAY_TYPE = type; +} + +var degree = Math.PI / 180; + +/** +* Convert Degree To Radian +* +* @param {Number} Angle in Degrees +*/ +glMatrix.toRadian = function(a){ + return a * degree; +} + +/** + * Tests whether or not the arguments have approximately the same value, within an absolute + * or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less + * than or equal to 1.0, and a relative tolerance is used for larger values) + * + * @param {Number} a The first number to test. + * @param {Number} b The second number to test. + * @returns {Boolean} True if the numbers are approximately equal, false otherwise. + */ +glMatrix.equals = function(a, b) { + return Math.abs(a - b) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a), Math.abs(b)); +} + +module.exports = glMatrix; + +},{}],170:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); + +/** + * @class 2x2 Matrix + * @name mat2 + */ +var mat2 = {}; + +/** + * Creates a new identity mat2 + * + * @returns {mat2} a new 2x2 matrix + */ +mat2.create = function() { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +}; + +/** + * Creates a new mat2 initialized with values from an existing matrix + * + * @param {mat2} a matrix to clone + * @returns {mat2} a new 2x2 matrix + */ +mat2.clone = function(a) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +}; + +/** + * Copy the values from one mat2 to another + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +mat2.copy = function(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +}; + +/** + * Set a mat2 to the identity matrix + * + * @param {mat2} out the receiving matrix + * @returns {mat2} out + */ +mat2.identity = function(out) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +}; + +/** + * Create a new mat2 with the given values + * + * @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} m10 Component in column 1, row 0 position (index 2) + * @param {Number} m11 Component in column 1, row 1 position (index 3) + * @returns {mat2} out A new 2x2 matrix + */ +mat2.fromValues = function(m00, m01, m10, m11) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = m00; + out[1] = m01; + out[2] = m10; + out[3] = m11; + return out; +}; + +/** + * Set the components of a mat2 to the given values + * + * @param {mat2} 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} m10 Component in column 1, row 0 position (index 2) + * @param {Number} m11 Component in column 1, row 1 position (index 3) + * @returns {mat2} out + */ +mat2.set = function(out, m00, m01, m10, m11) { + out[0] = m00; + out[1] = m01; + out[2] = m10; + out[3] = m11; + return out; +}; + + +/** + * Transpose the values of a mat2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +mat2.transpose = function(out, a) { + // If we are transposing ourselves we can skip a few steps but have to cache some values + if (out === a) { + var a1 = a[1]; + out[1] = a[2]; + out[2] = a1; + } else { + out[0] = a[0]; + out[1] = a[2]; + out[2] = a[1]; + out[3] = a[3]; + } + + return out; +}; + +/** + * Inverts a mat2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +mat2.invert = function(out, a) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], + + // Calculate the determinant + det = a0 * a3 - a2 * a1; + + if (!det) { + return null; + } + det = 1.0 / det; + + out[0] = a3 * det; + out[1] = -a1 * det; + out[2] = -a2 * det; + out[3] = a0 * det; + + return out; +}; + +/** + * Calculates the adjugate of a mat2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +mat2.adjoint = function(out, a) { + // Caching this value is nessecary if out == a + var a0 = a[0]; + out[0] = a[3]; + out[1] = -a[1]; + out[2] = -a[2]; + out[3] = a0; + + return out; +}; + +/** + * Calculates the determinant of a mat2 + * + * @param {mat2} a the source matrix + * @returns {Number} determinant of a + */ +mat2.determinant = function (a) { + return a[0] * a[3] - a[2] * a[1]; +}; + +/** + * Multiplies two mat2's + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @returns {mat2} out + */ +mat2.multiply = function (out, a, b) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; + var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; + out[0] = a0 * b0 + a2 * b1; + out[1] = a1 * b0 + a3 * b1; + out[2] = a0 * b2 + a2 * b3; + out[3] = a1 * b2 + a3 * b3; + return out; +}; + +/** + * Alias for {@link mat2.multiply} + * @function + */ +mat2.mul = mat2.multiply; + +/** + * Rotates a mat2 by the given angle + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2} out + */ +mat2.rotate = function (out, a, rad) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], + s = Math.sin(rad), + c = Math.cos(rad); + out[0] = a0 * c + a2 * s; + out[1] = a1 * c + a3 * s; + out[2] = a0 * -s + a2 * c; + out[3] = a1 * -s + a3 * c; + return out; +}; + +/** + * Scales the mat2 by the dimensions in the given vec2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the matrix to rotate + * @param {vec2} v the vec2 to scale the matrix by + * @returns {mat2} out + **/ +mat2.scale = function(out, a, v) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], + v0 = v[0], v1 = v[1]; + out[0] = a0 * v0; + out[1] = a1 * v0; + out[2] = a2 * v1; + out[3] = a3 * v1; + return out; +}; + +/** + * Creates a matrix from a given angle + * This is equivalent to (but much faster than): + * + * mat2.identity(dest); + * mat2.rotate(dest, dest, rad); + * + * @param {mat2} out mat2 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2} out + */ +mat2.fromRotation = function(out, rad) { + var s = Math.sin(rad), + c = Math.cos(rad); + out[0] = c; + out[1] = s; + out[2] = -s; + out[3] = c; + return out; +} + +/** + * Creates a matrix from a vector scaling + * This is equivalent to (but much faster than): + * + * mat2.identity(dest); + * mat2.scale(dest, dest, vec); + * + * @param {mat2} out mat2 receiving operation result + * @param {vec2} v Scaling vector + * @returns {mat2} out + */ +mat2.fromScaling = function(out, v) { + out[0] = v[0]; + out[1] = 0; + out[2] = 0; + out[3] = v[1]; + return out; +} + +/** + * Returns a string representation of a mat2 + * + * @param {mat2} mat matrix to represent as a string + * @returns {String} string representation of the matrix + */ +mat2.str = function (a) { + return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; +}; + +/** + * Returns Frobenius norm of a mat2 + * + * @param {mat2} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +mat2.frob = function (a) { + return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2))) +}; + +/** + * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix + * @param {mat2} L the lower triangular matrix + * @param {mat2} D the diagonal matrix + * @param {mat2} U the upper triangular matrix + * @param {mat2} a the input matrix to factorize + */ + +mat2.LDU = function (L, D, U, a) { + L[2] = a[2]/a[0]; + U[0] = a[0]; + U[1] = a[1]; + U[3] = a[3] - L[2] * U[1]; + return [L, D, U]; +}; + +/** + * Adds two mat2's + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @returns {mat2} out + */ +mat2.add = function(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]; + return out; +}; + +/** + * Subtracts matrix b from matrix a + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @returns {mat2} out + */ +mat2.subtract = function(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]; + return out; +}; + +/** + * Alias for {@link mat2.subtract} + * @function + */ +mat2.sub = mat2.subtract; + +/** + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat2} a The first matrix. + * @param {mat2} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat2.exactEquals = function (a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; +}; + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat2} a The first matrix. + * @param {mat2} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat2.equals = function (a, b) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; + var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; + return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3))); +}; + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat2} out + */ +mat2.multiplyScalar = function(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + return out; +}; + +/** + * Adds two mat2's after multiplying each element of the second operand by a scalar value. + * + * @param {mat2} out the receiving vector + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat2} out + */ +mat2.multiplyScalarAndAdd = function(out, a, b, scale) { + out[0] = a[0] + (b[0] * scale); + out[1] = a[1] + (b[1] * scale); + out[2] = a[2] + (b[2] * scale); + out[3] = a[3] + (b[3] * scale); + return out; +}; + +module.exports = mat2; + +},{"./common.js":169}],171:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); + +/** + * @class 2x3 Matrix + * @name mat2d + * + * @description + * A mat2d contains six elements defined as: + *
+ * [a, c, tx,
+ *  b, d, ty]
+ * 
+ * This is a short form for the 3x3 matrix: + *
+ * [a, c, tx,
+ *  b, d, ty,
+ *  0, 0, 1]
+ * 
+ * The last row is ignored so the array is shorter and operations are faster. + */ +var mat2d = {}; + +/** + * Creates a new identity mat2d + * + * @returns {mat2d} a new 2x3 matrix + */ +mat2d.create = function() { + var out = new glMatrix.ARRAY_TYPE(6); + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + out[4] = 0; + out[5] = 0; + return out; +}; + +/** + * Creates a new mat2d initialized with values from an existing matrix + * + * @param {mat2d} a matrix to clone + * @returns {mat2d} a new 2x3 matrix + */ +mat2d.clone = function(a) { + var out = new glMatrix.ARRAY_TYPE(6); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[4] = a[4]; + out[5] = a[5]; + return out; +}; + +/** + * Copy the values from one mat2d to another + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the source matrix + * @returns {mat2d} out + */ +mat2d.copy = function(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]; + return out; +}; + +/** + * Set a mat2d to the identity matrix + * + * @param {mat2d} out the receiving matrix + * @returns {mat2d} out + */ +mat2d.identity = function(out) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + out[4] = 0; + out[5] = 0; + return out; +}; + +/** + * Create a new mat2d with the given values + * + * @param {Number} a Component A (index 0) + * @param {Number} b Component B (index 1) + * @param {Number} c Component C (index 2) + * @param {Number} d Component D (index 3) + * @param {Number} tx Component TX (index 4) + * @param {Number} ty Component TY (index 5) + * @returns {mat2d} A new mat2d + */ +mat2d.fromValues = function(a, b, c, d, tx, ty) { + var out = new glMatrix.ARRAY_TYPE(6); + out[0] = a; + out[1] = b; + out[2] = c; + out[3] = d; + out[4] = tx; + out[5] = ty; + return out; +}; + +/** + * Set the components of a mat2d to the given values + * + * @param {mat2d} out the receiving matrix + * @param {Number} a Component A (index 0) + * @param {Number} b Component B (index 1) + * @param {Number} c Component C (index 2) + * @param {Number} d Component D (index 3) + * @param {Number} tx Component TX (index 4) + * @param {Number} ty Component TY (index 5) + * @returns {mat2d} out + */ +mat2d.set = function(out, a, b, c, d, tx, ty) { + out[0] = a; + out[1] = b; + out[2] = c; + out[3] = d; + out[4] = tx; + out[5] = ty; + return out; +}; + +/** + * Inverts a mat2d + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the source matrix + * @returns {mat2d} out + */ +mat2d.invert = function(out, a) { + var aa = a[0], ab = a[1], ac = a[2], ad = a[3], + atx = a[4], aty = a[5]; + + var det = aa * ad - ab * ac; + if(!det){ + return null; + } + det = 1.0 / det; + + out[0] = ad * det; + out[1] = -ab * det; + out[2] = -ac * det; + out[3] = aa * det; + out[4] = (ac * aty - ad * atx) * det; + out[5] = (ab * atx - aa * aty) * det; + return out; +}; + +/** + * Calculates the determinant of a mat2d + * + * @param {mat2d} a the source matrix + * @returns {Number} determinant of a + */ +mat2d.determinant = function (a) { + return a[0] * a[3] - a[1] * a[2]; +}; + +/** + * Multiplies two mat2d's + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @returns {mat2d} out + */ +mat2d.multiply = function (out, a, b) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], + b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; + out[0] = a0 * b0 + a2 * b1; + out[1] = a1 * b0 + a3 * b1; + out[2] = a0 * b2 + a2 * b3; + out[3] = a1 * b2 + a3 * b3; + out[4] = a0 * b4 + a2 * b5 + a4; + out[5] = a1 * b4 + a3 * b5 + a5; + return out; +}; + +/** + * Alias for {@link mat2d.multiply} + * @function + */ +mat2d.mul = mat2d.multiply; + +/** + * Rotates a mat2d by the given angle + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2d} out + */ +mat2d.rotate = function (out, a, rad) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], + s = Math.sin(rad), + c = Math.cos(rad); + out[0] = a0 * c + a2 * s; + out[1] = a1 * c + a3 * s; + out[2] = a0 * -s + a2 * c; + out[3] = a1 * -s + a3 * c; + out[4] = a4; + out[5] = a5; + return out; +}; + +/** + * Scales the mat2d by the dimensions in the given vec2 + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to translate + * @param {vec2} v the vec2 to scale the matrix by + * @returns {mat2d} out + **/ +mat2d.scale = function(out, a, v) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], + v0 = v[0], v1 = v[1]; + out[0] = a0 * v0; + out[1] = a1 * v0; + out[2] = a2 * v1; + out[3] = a3 * v1; + out[4] = a4; + out[5] = a5; + return out; +}; + +/** + * Translates the mat2d by the dimensions in the given vec2 + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to translate + * @param {vec2} v the vec2 to translate the matrix by + * @returns {mat2d} out + **/ +mat2d.translate = function(out, a, v) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], + v0 = v[0], v1 = v[1]; + out[0] = a0; + out[1] = a1; + out[2] = a2; + out[3] = a3; + out[4] = a0 * v0 + a2 * v1 + a4; + out[5] = a1 * v0 + a3 * v1 + a5; + return out; +}; + +/** + * Creates a matrix from a given angle + * This is equivalent to (but much faster than): + * + * mat2d.identity(dest); + * mat2d.rotate(dest, dest, rad); + * + * @param {mat2d} out mat2d receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2d} out + */ +mat2d.fromRotation = function(out, rad) { + var s = Math.sin(rad), c = Math.cos(rad); + out[0] = c; + out[1] = s; + out[2] = -s; + out[3] = c; + out[4] = 0; + out[5] = 0; + return out; +} + +/** + * Creates a matrix from a vector scaling + * This is equivalent to (but much faster than): + * + * mat2d.identity(dest); + * mat2d.scale(dest, dest, vec); + * + * @param {mat2d} out mat2d receiving operation result + * @param {vec2} v Scaling vector + * @returns {mat2d} out + */ +mat2d.fromScaling = function(out, v) { + out[0] = v[0]; + out[1] = 0; + out[2] = 0; + out[3] = v[1]; + out[4] = 0; + out[5] = 0; + return out; +} + +/** + * Creates a matrix from a vector translation + * This is equivalent to (but much faster than): + * + * mat2d.identity(dest); + * mat2d.translate(dest, dest, vec); + * + * @param {mat2d} out mat2d receiving operation result + * @param {vec2} v Translation vector + * @returns {mat2d} out + */ +mat2d.fromTranslation = function(out, v) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + out[4] = v[0]; + out[5] = v[1]; + return out; +} + +/** + * Returns a string representation of a mat2d + * + * @param {mat2d} a matrix to represent as a string + * @returns {String} string representation of the matrix + */ +mat2d.str = function (a) { + return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + + a[3] + ', ' + a[4] + ', ' + a[5] + ')'; +}; + +/** + * Returns Frobenius norm of a mat2d + * + * @param {mat2d} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +mat2d.frob = function (a) { + return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + 1)) +}; + +/** + * Adds two mat2d's + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @returns {mat2d} out + */ +mat2d.add = function(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]; + return out; +}; + +/** + * Subtracts matrix b from matrix a + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @returns {mat2d} out + */ +mat2d.subtract = function(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]; + return out; +}; + +/** + * Alias for {@link mat2d.subtract} + * @function + */ +mat2d.sub = mat2d.subtract; + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat2d} out + */ +mat2d.multiplyScalar = function(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + out[4] = a[4] * b; + out[5] = a[5] * b; + return out; +}; + +/** + * Adds two mat2d's after multiplying each element of the second operand by a scalar value. + * + * @param {mat2d} out the receiving vector + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat2d} out + */ +mat2d.multiplyScalarAndAdd = function(out, a, b, scale) { + out[0] = a[0] + (b[0] * scale); + out[1] = a[1] + (b[1] * scale); + out[2] = a[2] + (b[2] * scale); + out[3] = a[3] + (b[3] * scale); + out[4] = a[4] + (b[4] * scale); + out[5] = a[5] + (b[5] * scale); + return out; +}; + +/** + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat2d} a The first matrix. + * @param {mat2d} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat2d.exactEquals = function (a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5]; +}; + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat2d} a The first matrix. + * @param {mat2d} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat2d.equals = function (a, b) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; + var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; + return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && + Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && + Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5))); +}; + +module.exports = mat2d; + +},{"./common.js":169}],172:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); + +/** + * @class 3x3 Matrix + * @name mat3 + */ +var mat3 = {}; + +/** + * Creates a new identity mat3 + * + * @returns {mat3} a new 3x3 matrix + */ +mat3.create = function() { + var out = new glMatrix.ARRAY_TYPE(9); + 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; +}; + +/** + * Copies the upper-left 3x3 values into the given mat3. + * + * @param {mat3} out the receiving 3x3 matrix + * @param {mat4} a the source 4x4 matrix + * @returns {mat3} out + */ +mat3.fromMat4 = function(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; +}; + +/** + * Creates a new mat3 initialized with values from an existing matrix + * + * @param {mat3} a matrix to clone + * @returns {mat3} a new 3x3 matrix + */ +mat3.clone = function(a) { + var out = new glMatrix.ARRAY_TYPE(9); + 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; +}; + +/** + * Copy the values from one mat3 to another + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the source matrix + * @returns {mat3} out + */ +mat3.copy = function(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; +}; + +/** + * Create a new mat3 with the given values + * + * @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} A new mat3 + */ +mat3.fromValues = function(m00, m01, m02, m10, m11, m12, m20, m21, m22) { + var out = new glMatrix.ARRAY_TYPE(9); + 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 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 + */ +mat3.set = function(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 + */ +mat3.identity = function(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 {mat3} a the source matrix + * @returns {mat3} out + */ +mat3.transpose = function(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 {mat3} a the source matrix + * @returns {mat3} out + */ +mat3.invert = function(out, a) { + 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], + + b01 = a22 * a11 - a12 * a21, + b11 = -a22 * a10 + a12 * a20, + b21 = a21 * a10 - a11 * a20, + + // Calculate the determinant + 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 {mat3} a the source matrix + * @returns {mat3} out + */ +mat3.adjoint = function(out, a) { + 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]; + + 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 {mat3} a the source matrix + * @returns {Number} determinant of a + */ +mat3.determinant = function (a) { + 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]; + + 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 {mat3} a the first operand + * @param {mat3} b the second operand + * @returns {mat3} out + */ +mat3.multiply = function (out, a, b) { + 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], + + b00 = b[0], b01 = b[1], b02 = b[2], + b10 = b[3], b11 = b[4], b12 = b[5], + 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; +}; + +/** + * Alias for {@link mat3.multiply} + * @function + */ +mat3.mul = mat3.multiply; + +/** + * Translate a mat3 by the given vector + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the matrix to translate + * @param {vec2} v vector to translate by + * @returns {mat3} out + */ +mat3.translate = function(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 {mat3} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat3} out + */ +mat3.rotate = function (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 {mat3} a the matrix to rotate + * @param {vec2} v the vec2 to scale the matrix by + * @returns {mat3} out + **/ +mat3.scale = function(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 {vec2} v Translation vector + * @returns {mat3} out + */ +mat3.fromTranslation = function(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 + */ +mat3.fromRotation = function(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 {vec2} v Scaling vector + * @returns {mat3} out + */ +mat3.fromScaling = function(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; +} + +/** + * Copies the values from a mat2d into a mat3 + * + * @param {mat3} out the receiving matrix + * @param {mat2d} a the matrix to copy + * @returns {mat3} out + **/ +mat3.fromMat2d = function(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = 0; + + out[3] = a[2]; + out[4] = a[3]; + out[5] = 0; + + out[6] = a[4]; + out[7] = a[5]; + out[8] = 1; + return out; +}; + +/** +* Calculates a 3x3 matrix from the given quaternion +* +* @param {mat3} out mat3 receiving operation result +* @param {quat} q Quaternion to create matrix from +* +* @returns {mat3} out +*/ +mat3.fromQuat = function (out, q) { + var x = q[0], y = q[1], z = q[2], w = q[3], + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + yx = y * x2, + yy = y * y2, + zx = z * x2, + zy = z * y2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + 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 {mat4} a Mat4 to derive the normal matrix from +* +* @returns {mat3} out +*/ +mat3.normalFromMat4 = function (out, a) { + var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], + + b00 = a00 * a11 - a01 * a10, + b01 = a00 * a12 - a02 * a10, + b02 = a00 * a13 - a03 * a10, + b03 = a01 * a12 - a02 * a11, + b04 = a01 * a13 - a03 * a11, + b05 = a02 * a13 - a03 * a12, + b06 = a20 * a31 - a21 * a30, + b07 = a20 * a32 - a22 * a30, + b08 = a20 * a33 - a23 * a30, + b09 = a21 * a32 - a22 * a31, + b10 = a21 * a33 - a23 * a31, + b11 = a22 * a33 - a23 * a32, + + // Calculate the determinant + 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 a string representation of a mat3 + * + * @param {mat3} mat matrix to represent as a string + * @returns {String} string representation of the matrix + */ +mat3.str = function (a) { + return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + + a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + + a[6] + ', ' + a[7] + ', ' + a[8] + ')'; +}; + +/** + * Returns Frobenius norm of a mat3 + * + * @param {mat3} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +mat3.frob = function (a) { + return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2))) +}; + +/** + * Adds two mat3's + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the first operand + * @param {mat3} b the second operand + * @returns {mat3} out + */ +mat3.add = function(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 {mat3} a the first operand + * @param {mat3} b the second operand + * @returns {mat3} out + */ +mat3.subtract = function(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; +}; + +/** + * Alias for {@link mat3.subtract} + * @function + */ +mat3.sub = mat3.subtract; + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat3} out + */ +mat3.multiplyScalar = function(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + out[4] = a[4] * b; + out[5] = a[5] * b; + out[6] = a[6] * b; + out[7] = a[7] * b; + out[8] = a[8] * b; + return out; +}; + +/** + * Adds two mat3's after multiplying each element of the second operand by a scalar value. + * + * @param {mat3} out the receiving vector + * @param {mat3} a the first operand + * @param {mat3} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat3} out + */ +mat3.multiplyScalarAndAdd = function(out, a, b, scale) { + out[0] = a[0] + (b[0] * scale); + out[1] = a[1] + (b[1] * scale); + out[2] = a[2] + (b[2] * scale); + out[3] = a[3] + (b[3] * scale); + out[4] = a[4] + (b[4] * scale); + out[5] = a[5] + (b[5] * scale); + out[6] = a[6] + (b[6] * scale); + out[7] = a[7] + (b[7] * scale); + out[8] = a[8] + (b[8] * scale); + return out; +}; + +/* + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat3} a The first matrix. + * @param {mat3} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat3.exactEquals = function (a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && + a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && + a[6] === b[6] && a[7] === b[7] && a[8] === b[8]; +}; + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat3} a The first matrix. + * @param {mat3} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat3.equals = function (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 = a[6], b7 = b[7], b8 = b[8]; + return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && + Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && + Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5)) && + Math.abs(a6 - b6) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a6), Math.abs(b6)) && + Math.abs(a7 - b7) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a7), Math.abs(b7)) && + Math.abs(a8 - b8) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a8), Math.abs(b8))); +}; + + +module.exports = mat3; + +},{"./common.js":169}],173:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); + +/** + * @class 4x4 Matrix + * @name mat4 + */ +var mat4 = { + scalar: {}, + SIMD: {}, +}; + +/** + * Creates a new identity mat4 + * + * @returns {mat4} a new 4x4 matrix + */ +mat4.create = function() { + var out = new glMatrix.ARRAY_TYPE(16); + 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; +}; + +/** + * Creates a new mat4 initialized with values from an existing matrix + * + * @param {mat4} a matrix to clone + * @returns {mat4} a new 4x4 matrix + */ +mat4.clone = function(a) { + var out = new glMatrix.ARRAY_TYPE(16); + 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; +}; + +/** + * Copy the values from one mat4 to another + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.copy = function(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; +}; + +/** + * Create a new mat4 with the given values + * + * @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} A new mat4 + */ +mat4.fromValues = function(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { + var out = new glMatrix.ARRAY_TYPE(16); + 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 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 + */ +mat4.set = function(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 + */ +mat4.identity = function(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 not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.scalar.transpose = function(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], + a12 = a[6], a13 = a[7], + 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; +}; + +/** + * Transpose the values of a mat4 using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.SIMD.transpose = function(out, a) { + var a0, a1, a2, a3, + tmp01, tmp23, + out0, out1, out2, out3; + + a0 = SIMD.Float32x4.load(a, 0); + a1 = SIMD.Float32x4.load(a, 4); + a2 = SIMD.Float32x4.load(a, 8); + a3 = SIMD.Float32x4.load(a, 12); + + tmp01 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); + tmp23 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); + out0 = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6); + out1 = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7); + SIMD.Float32x4.store(out, 0, out0); + SIMD.Float32x4.store(out, 4, out1); + + tmp01 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); + tmp23 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); + out2 = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6); + out3 = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7); + SIMD.Float32x4.store(out, 8, out2); + SIMD.Float32x4.store(out, 12, out3); + + return out; +}; + +/** + * Transpse a mat4 using SIMD if available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.transpose = glMatrix.USE_SIMD ? mat4.SIMD.transpose : mat4.scalar.transpose; + +/** + * Inverts a mat4 not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.scalar.invert = function(out, a) { + var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], + + b00 = a00 * a11 - a01 * a10, + b01 = a00 * a12 - a02 * a10, + b02 = a00 * a13 - a03 * a10, + b03 = a01 * a12 - a02 * a11, + b04 = a01 * a13 - a03 * a11, + b05 = a02 * a13 - a03 * a12, + b06 = a20 * a31 - a21 * a30, + b07 = a20 * a32 - a22 * a30, + b08 = a20 * a33 - a23 * a30, + b09 = a21 * a32 - a22 * a31, + b10 = a21 * a33 - a23 * a31, + b11 = a22 * a33 - a23 * a32, + + // Calculate the determinant + 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; +}; + +/** + * Inverts a mat4 using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.SIMD.invert = function(out, a) { + var row0, row1, row2, row3, + tmp1, + minor0, minor1, minor2, minor3, + det, + a0 = SIMD.Float32x4.load(a, 0), + a1 = SIMD.Float32x4.load(a, 4), + a2 = SIMD.Float32x4.load(a, 8), + a3 = SIMD.Float32x4.load(a, 12); + + // Compute matrix adjugate + tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); + row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); + row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6); + row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7); + tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); + row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); + row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6); + row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7); + + tmp1 = SIMD.Float32x4.mul(row2, row3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor0 = SIMD.Float32x4.mul(row1, tmp1); + minor1 = SIMD.Float32x4.mul(row0, tmp1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0); + minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1); + minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1); + + tmp1 = SIMD.Float32x4.mul(row1, row2); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0); + minor3 = SIMD.Float32x4.mul(row0, tmp1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1)); + minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3); + minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1); + + tmp1 = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + row2 = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0); + minor2 = SIMD.Float32x4.mul(row0, tmp1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1)); + minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2); + minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1); + + tmp1 = SIMD.Float32x4.mul(row0, row1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2); + minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2); + minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1)); + + tmp1 = SIMD.Float32x4.mul(row0, row3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1)); + minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1); + minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1)); + + tmp1 = SIMD.Float32x4.mul(row0, row2); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1); + minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1)); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1)); + minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3); + + // Compute matrix determinant + det = SIMD.Float32x4.mul(row0, minor0); + det = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 2, 3, 0, 1), det); + det = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 1, 0, 3, 2), det); + tmp1 = SIMD.Float32x4.reciprocalApproximation(det); + det = SIMD.Float32x4.sub( + SIMD.Float32x4.add(tmp1, tmp1), + SIMD.Float32x4.mul(det, SIMD.Float32x4.mul(tmp1, tmp1))); + det = SIMD.Float32x4.swizzle(det, 0, 0, 0, 0); + if (!det) { + return null; + } + + // Compute matrix inverse + SIMD.Float32x4.store(out, 0, SIMD.Float32x4.mul(det, minor0)); + SIMD.Float32x4.store(out, 4, SIMD.Float32x4.mul(det, minor1)); + SIMD.Float32x4.store(out, 8, SIMD.Float32x4.mul(det, minor2)); + SIMD.Float32x4.store(out, 12, SIMD.Float32x4.mul(det, minor3)); + return out; +} + +/** + * Inverts a mat4 using SIMD if available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.invert = glMatrix.USE_SIMD ? mat4.SIMD.invert : mat4.scalar.invert; + +/** + * Calculates the adjugate of a mat4 not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.scalar.adjoint = function(out, a) { + var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + 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 adjugate of a mat4 using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +mat4.SIMD.adjoint = function(out, a) { + var a0, a1, a2, a3; + var row0, row1, row2, row3; + var tmp1; + var minor0, minor1, minor2, minor3; + + var a0 = SIMD.Float32x4.load(a, 0); + var a1 = SIMD.Float32x4.load(a, 4); + var a2 = SIMD.Float32x4.load(a, 8); + var a3 = SIMD.Float32x4.load(a, 12); + + // Transpose the source matrix. Sort of. Not a true transpose operation + tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); + row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); + row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6); + row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7); + + tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); + row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); + row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6); + row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7); + + tmp1 = SIMD.Float32x4.mul(row2, row3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor0 = SIMD.Float32x4.mul(row1, tmp1); + minor1 = SIMD.Float32x4.mul(row0, tmp1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0); + minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1); + minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1); + + tmp1 = SIMD.Float32x4.mul(row1, row2); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0); + minor3 = SIMD.Float32x4.mul(row0, tmp1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1)); + minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3); + minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1); + + tmp1 = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + row2 = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0); + minor2 = SIMD.Float32x4.mul(row0, tmp1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1)); + minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2); + minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1); + + tmp1 = SIMD.Float32x4.mul(row0, row1); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2); + minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2); + minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1)); + + tmp1 = SIMD.Float32x4.mul(row0, row3); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1)); + minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1); + minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1)); + + tmp1 = SIMD.Float32x4.mul(row0, row2); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); + minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1); + minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1)); + tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); + minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1)); + minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3); + + SIMD.Float32x4.store(out, 0, minor0); + SIMD.Float32x4.store(out, 4, minor1); + SIMD.Float32x4.store(out, 8, minor2); + SIMD.Float32x4.store(out, 12, minor3); + return out; +}; + +/** + * Calculates the adjugate of a mat4 using SIMD if available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ + mat4.adjoint = glMatrix.USE_SIMD ? mat4.SIMD.adjoint : mat4.scalar.adjoint; + +/** + * Calculates the determinant of a mat4 + * + * @param {mat4} a the source matrix + * @returns {Number} determinant of a + */ +mat4.determinant = function (a) { + var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], + + b00 = a00 * a11 - a01 * a10, + b01 = a00 * a12 - a02 * a10, + b02 = a00 * a13 - a03 * a10, + b03 = a01 * a12 - a02 * a11, + b04 = a01 * a13 - a03 * a11, + b05 = a02 * a13 - a03 * a12, + b06 = a20 * a31 - a21 * a30, + b07 = a20 * a32 - a22 * a30, + b08 = a20 * a33 - a23 * a30, + b09 = a21 * a32 - a22 * a31, + b10 = a21 * a33 - a23 * a31, + b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; +}; + +/** + * Multiplies two mat4's explicitly using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand, must be a Float32Array + * @param {mat4} b the second operand, must be a Float32Array + * @returns {mat4} out + */ +mat4.SIMD.multiply = function (out, a, b) { + var a0 = SIMD.Float32x4.load(a, 0); + var a1 = SIMD.Float32x4.load(a, 4); + var a2 = SIMD.Float32x4.load(a, 8); + var a3 = SIMD.Float32x4.load(a, 12); + + var b0 = SIMD.Float32x4.load(b, 0); + var out0 = SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 0, 0, 0, 0), a0), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 1, 1, 1, 1), a1), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 2, 2, 2, 2), a2), + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 3, 3, 3, 3), a3)))); + SIMD.Float32x4.store(out, 0, out0); + + var b1 = SIMD.Float32x4.load(b, 4); + var out1 = SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 0, 0, 0, 0), a0), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 1, 1, 1, 1), a1), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 2, 2, 2, 2), a2), + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 3, 3, 3, 3), a3)))); + SIMD.Float32x4.store(out, 4, out1); + + var b2 = SIMD.Float32x4.load(b, 8); + var out2 = SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 0, 0, 0, 0), a0), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 1, 1, 1, 1), a1), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 2, 2, 2, 2), a2), + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 3, 3, 3, 3), a3)))); + SIMD.Float32x4.store(out, 8, out2); + + var b3 = SIMD.Float32x4.load(b, 12); + var out3 = SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 0, 0, 0, 0), a0), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 1, 1, 1, 1), a1), + SIMD.Float32x4.add( + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 2, 2, 2, 2), a2), + SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 3, 3, 3, 3), a3)))); + SIMD.Float32x4.store(out, 12, out3); + + return out; +}; + +/** + * Multiplies two mat4's explicitly not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +mat4.scalar.multiply = function (out, a, b) { + var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; + + // Cache only the current line of the second matrix + var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; + out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + + b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7]; + out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + + b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11]; + out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + + b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15]; + out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30; + out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31; + out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32; + out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + return out; +}; + +/** + * Multiplies two mat4's using SIMD if available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +mat4.multiply = glMatrix.USE_SIMD ? mat4.SIMD.multiply : mat4.scalar.multiply; + +/** + * Alias for {@link mat4.multiply} + * @function + */ +mat4.mul = mat4.multiply; + +/** + * Translate a mat4 by the given vector not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to translate + * @param {vec3} v vector to translate by + * @returns {mat4} out + */ +mat4.scalar.translate = function (out, a, v) { + var x = v[0], y = v[1], z = v[2], + a00, a01, a02, a03, + a10, a11, a12, a13, + a20, a21, a22, a23; + + if (a === out) { + out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; + out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; + out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; + out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; + } else { + a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; + a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; + a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; + + out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03; + out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13; + out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23; + + out[12] = a00 * x + a10 * y + a20 * z + a[12]; + out[13] = a01 * x + a11 * y + a21 * z + a[13]; + out[14] = a02 * x + a12 * y + a22 * z + a[14]; + out[15] = a03 * x + a13 * y + a23 * z + a[15]; + } + + return out; +}; + +/** + * Translates a mat4 by the given vector using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to translate + * @param {vec3} v vector to translate by + * @returns {mat4} out + */ +mat4.SIMD.translate = function (out, a, v) { + var a0 = SIMD.Float32x4.load(a, 0), + a1 = SIMD.Float32x4.load(a, 4), + a2 = SIMD.Float32x4.load(a, 8), + a3 = SIMD.Float32x4.load(a, 12), + vec = SIMD.Float32x4(v[0], v[1], v[2] , 0); + + if (a !== out) { + 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]; + } + + a0 = SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0)); + a1 = SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1)); + a2 = SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2)); + + var t0 = SIMD.Float32x4.add(a0, SIMD.Float32x4.add(a1, SIMD.Float32x4.add(a2, a3))); + SIMD.Float32x4.store(out, 12, t0); + + return out; +}; + +/** + * Translates a mat4 by the given vector using SIMD if available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to translate + * @param {vec3} v vector to translate by + * @returns {mat4} out + */ +mat4.translate = glMatrix.USE_SIMD ? mat4.SIMD.translate : mat4.scalar.translate; + +/** + * Scales the mat4 by the dimensions in the given vec3 not using vectorization + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to scale + * @param {vec3} v the vec3 to scale the matrix by + * @returns {mat4} out + **/ +mat4.scalar.scale = function(out, a, v) { + var x = v[0], y = v[1], z = v[2]; + + out[0] = a[0] * x; + out[1] = a[1] * x; + out[2] = a[2] * x; + out[3] = a[3] * x; + out[4] = a[4] * y; + out[5] = a[5] * y; + out[6] = a[6] * y; + out[7] = a[7] * y; + out[8] = a[8] * z; + out[9] = a[9] * z; + out[10] = a[10] * z; + out[11] = a[11] * z; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + return out; +}; + +/** + * Scales the mat4 by the dimensions in the given vec3 using vectorization + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to scale + * @param {vec3} v the vec3 to scale the matrix by + * @returns {mat4} out + **/ +mat4.SIMD.scale = function(out, a, v) { + var a0, a1, a2; + var vec = SIMD.Float32x4(v[0], v[1], v[2], 0); + + a0 = SIMD.Float32x4.load(a, 0); + SIMD.Float32x4.store( + out, 0, SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0))); + + a1 = SIMD.Float32x4.load(a, 4); + SIMD.Float32x4.store( + out, 4, SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1))); + + a2 = SIMD.Float32x4.load(a, 8); + SIMD.Float32x4.store( + out, 8, SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2))); + + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + return out; +}; + +/** + * Scales the mat4 by the dimensions in the given vec3 using SIMD if available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to scale + * @param {vec3} v the vec3 to scale the matrix by + * @returns {mat4} out + */ +mat4.scale = glMatrix.USE_SIMD ? mat4.SIMD.scale : mat4.scalar.scale; + +/** + * Rotates a mat4 by the given angle around the given axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @param {vec3} axis the axis to rotate around + * @returns {mat4} out + */ +mat4.rotate = function (out, a, rad, axis) { + var x = axis[0], y = axis[1], z = axis[2], + len = Math.sqrt(x * x + y * y + z * z), + s, c, t, + a00, a01, a02, a03, + a10, a11, a12, a13, + a20, a21, a22, a23, + b00, b01, b02, + b10, b11, b12, + b20, b21, b22; + + if (Math.abs(len) < glMatrix.EPSILON) { return null; } + + len = 1 / len; + x *= len; + y *= len; + z *= len; + + s = Math.sin(rad); + c = Math.cos(rad); + t = 1 - c; + + a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; + a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; + a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; + + // Construct the elements of the rotation matrix + b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s; + b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s; + b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c; + + // Perform rotation-specific matrix multiplication + out[0] = a00 * b00 + a10 * b01 + a20 * b02; + out[1] = a01 * b00 + a11 * b01 + a21 * b02; + out[2] = a02 * b00 + a12 * b01 + a22 * b02; + out[3] = a03 * b00 + a13 * b01 + a23 * b02; + out[4] = a00 * b10 + a10 * b11 + a20 * b12; + out[5] = a01 * b10 + a11 * b11 + a21 * b12; + out[6] = a02 * b10 + a12 * b11 + a22 * b12; + out[7] = a03 * b10 + a13 * b11 + a23 * b12; + out[8] = a00 * b20 + a10 * b21 + a20 * b22; + out[9] = a01 * b20 + a11 * b21 + a21 * b22; + out[10] = a02 * b20 + a12 * b21 + a22 * b22; + out[11] = a03 * b20 + a13 * b21 + a23 * b22; + + if (a !== out) { // If the source and destination differ, copy the unchanged last row + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + return out; +}; + +/** + * Rotates a matrix by the given angle around the X axis not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.scalar.rotateX = function (out, a, rad) { + var s = Math.sin(rad), + c = Math.cos(rad), + a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7], + a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + out[4] = a10 * c + a20 * s; + out[5] = a11 * c + a21 * s; + out[6] = a12 * c + a22 * s; + out[7] = a13 * c + a23 * s; + out[8] = a20 * c - a10 * s; + out[9] = a21 * c - a11 * s; + out[10] = a22 * c - a12 * s; + out[11] = a23 * c - a13 * s; + return out; +}; + +/** + * Rotates a matrix by the given angle around the X axis using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.SIMD.rotateX = function (out, a, rad) { + var s = SIMD.Float32x4.splat(Math.sin(rad)), + c = SIMD.Float32x4.splat(Math.cos(rad)); + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + var a_1 = SIMD.Float32x4.load(a, 4); + var a_2 = SIMD.Float32x4.load(a, 8); + SIMD.Float32x4.store(out, 4, + SIMD.Float32x4.add(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_2, s))); + SIMD.Float32x4.store(out, 8, + SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_2, c), SIMD.Float32x4.mul(a_1, s))); + return out; +}; + +/** + * Rotates a matrix by the given angle around the X axis using SIMD if availabe and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.rotateX = glMatrix.USE_SIMD ? mat4.SIMD.rotateX : mat4.scalar.rotateX; + +/** + * Rotates a matrix by the given angle around the Y axis not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.scalar.rotateY = function (out, a, rad) { + var s = Math.sin(rad), + c = Math.cos(rad), + a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3], + a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + out[0] = a00 * c - a20 * s; + out[1] = a01 * c - a21 * s; + out[2] = a02 * c - a22 * s; + out[3] = a03 * c - a23 * s; + out[8] = a00 * s + a20 * c; + out[9] = a01 * s + a21 * c; + out[10] = a02 * s + a22 * c; + out[11] = a03 * s + a23 * c; + return out; +}; + +/** + * Rotates a matrix by the given angle around the Y axis using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.SIMD.rotateY = function (out, a, rad) { + var s = SIMD.Float32x4.splat(Math.sin(rad)), + c = SIMD.Float32x4.splat(Math.cos(rad)); + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + var a_0 = SIMD.Float32x4.load(a, 0); + var a_2 = SIMD.Float32x4.load(a, 8); + SIMD.Float32x4.store(out, 0, + SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_2, s))); + SIMD.Float32x4.store(out, 8, + SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, s), SIMD.Float32x4.mul(a_2, c))); + return out; +}; + +/** + * Rotates a matrix by the given angle around the Y axis if SIMD available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ + mat4.rotateY = glMatrix.USE_SIMD ? mat4.SIMD.rotateY : mat4.scalar.rotateY; + +/** + * Rotates a matrix by the given angle around the Z axis not using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.scalar.rotateZ = function (out, a, rad) { + var s = Math.sin(rad), + c = Math.cos(rad), + a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3], + a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7]; + + if (a !== out) { // If the source and destination differ, copy the unchanged last row + 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]; + } + + // Perform axis-specific matrix multiplication + out[0] = a00 * c + a10 * s; + out[1] = a01 * c + a11 * s; + out[2] = a02 * c + a12 * s; + out[3] = a03 * c + a13 * s; + out[4] = a10 * c - a00 * s; + out[5] = a11 * c - a01 * s; + out[6] = a12 * c - a02 * s; + out[7] = a13 * c - a03 * s; + return out; +}; + +/** + * Rotates a matrix by the given angle around the Z axis using SIMD + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.SIMD.rotateZ = function (out, a, rad) { + var s = SIMD.Float32x4.splat(Math.sin(rad)), + c = SIMD.Float32x4.splat(Math.cos(rad)); + + if (a !== out) { // If the source and destination differ, copy the unchanged last row + 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]; + } + + // Perform axis-specific matrix multiplication + var a_0 = SIMD.Float32x4.load(a, 0); + var a_1 = SIMD.Float32x4.load(a, 4); + SIMD.Float32x4.store(out, 0, + SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_1, s))); + SIMD.Float32x4.store(out, 4, + SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_0, s))); + return out; +}; + +/** + * Rotates a matrix by the given angle around the Z axis if SIMD available and enabled + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ + mat4.rotateZ = glMatrix.USE_SIMD ? mat4.SIMD.rotateZ : mat4.scalar.rotateZ; + +/** + * Creates a matrix from a vector translation + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, dest, vec); + * + * @param {mat4} out mat4 receiving operation result + * @param {vec3} v Translation vector + * @returns {mat4} out + */ +mat4.fromTranslation = function(out, v) { + 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] = v[0]; + out[13] = v[1]; + out[14] = v[2]; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from a vector scaling + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.scale(dest, dest, vec); + * + * @param {mat4} out mat4 receiving operation result + * @param {vec3} v Scaling vector + * @returns {mat4} out + */ +mat4.fromScaling = function(out, v) { + out[0] = v[0]; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = v[1]; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = v[2]; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from a given angle around a given axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotate(dest, dest, rad, axis); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @param {vec3} axis the axis to rotate around + * @returns {mat4} out + */ +mat4.fromRotation = function(out, rad, axis) { + var x = axis[0], y = axis[1], z = axis[2], + len = Math.sqrt(x * x + y * y + z * z), + s, c, t; + + if (Math.abs(len) < glMatrix.EPSILON) { return null; } + + len = 1 / len; + x *= len; + y *= len; + z *= len; + + s = Math.sin(rad); + c = Math.cos(rad); + t = 1 - c; + + // Perform rotation-specific matrix multiplication + out[0] = x * x * t + c; + out[1] = y * x * t + z * s; + out[2] = z * x * t - y * s; + out[3] = 0; + out[4] = x * y * t - z * s; + out[5] = y * y * t + c; + out[6] = z * y * t + x * s; + out[7] = 0; + out[8] = x * z * t + y * s; + out[9] = y * z * t - x * s; + out[10] = z * z * t + c; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from the given angle around the X axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateX(dest, dest, rad); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.fromXRotation = function(out, rad) { + var s = Math.sin(rad), + c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = c; + out[6] = s; + out[7] = 0; + out[8] = 0; + out[9] = -s; + out[10] = c; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from the given angle around the Y axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateY(dest, dest, rad); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.fromYRotation = function(out, rad) { + var s = Math.sin(rad), + c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out[0] = c; + out[1] = 0; + out[2] = -s; + out[3] = 0; + out[4] = 0; + out[5] = 1; + out[6] = 0; + out[7] = 0; + out[8] = s; + out[9] = 0; + out[10] = c; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from the given angle around the Z axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateZ(dest, dest, rad); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +mat4.fromZRotation = function(out, rad) { + var s = Math.sin(rad), + c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out[0] = c; + out[1] = s; + out[2] = 0; + out[3] = 0; + out[4] = -s; + out[5] = c; + 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; +} + +/** + * Creates a matrix from a quaternion rotation and vector translation + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * var quatMat = mat4.create(); + * quat4.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @param {vec3} v Translation vector + * @returns {mat4} out + */ +mat4.fromRotationTranslation = function (out, q, v) { + // Quaternion math + var x = q[0], y = q[1], z = q[2], w = q[3], + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + xy = x * y2, + xz = x * z2, + yy = y * y2, + yz = y * z2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + wz = w * z2; + + out[0] = 1 - (yy + zz); + out[1] = xy + wz; + out[2] = xz - wy; + out[3] = 0; + out[4] = xy - wz; + out[5] = 1 - (xx + zz); + out[6] = yz + wx; + out[7] = 0; + out[8] = xz + wy; + out[9] = yz - wx; + out[10] = 1 - (xx + yy); + out[11] = 0; + out[12] = v[0]; + out[13] = v[1]; + out[14] = v[2]; + out[15] = 1; + + return out; +}; + +/** + * Returns the translation vector component of a transformation + * matrix. If a matrix is built with fromRotationTranslation, + * the returned vector will be the same as the translation vector + * originally supplied. + * @param {vec3} out Vector to receive translation component + * @param {mat4} mat Matrix to be decomposed (input) + * @return {vec3} out + */ +mat4.getTranslation = function (out, mat) { + out[0] = mat[12]; + out[1] = mat[13]; + out[2] = mat[14]; + + return out; +}; + +/** + * Returns a quaternion representing the rotational component + * of a transformation matrix. If a matrix is built with + * fromRotationTranslation, the returned quaternion will be the + * same as the quaternion originally supplied. + * @param {quat} out Quaternion to receive the rotation component + * @param {mat4} mat Matrix to be decomposed (input) + * @return {quat} out + */ +mat4.getRotation = function (out, mat) { + // Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm + var trace = mat[0] + mat[5] + mat[10]; + var S = 0; + + if (trace > 0) { + S = Math.sqrt(trace + 1.0) * 2; + out[3] = 0.25 * S; + out[0] = (mat[6] - mat[9]) / S; + out[1] = (mat[8] - mat[2]) / S; + out[2] = (mat[1] - mat[4]) / S; + } else if ((mat[0] > mat[5])&(mat[0] > mat[10])) { + S = Math.sqrt(1.0 + mat[0] - mat[5] - mat[10]) * 2; + out[3] = (mat[6] - mat[9]) / S; + out[0] = 0.25 * S; + out[1] = (mat[1] + mat[4]) / S; + out[2] = (mat[8] + mat[2]) / S; + } else if (mat[5] > mat[10]) { + S = Math.sqrt(1.0 + mat[5] - mat[0] - mat[10]) * 2; + out[3] = (mat[8] - mat[2]) / S; + out[0] = (mat[1] + mat[4]) / S; + out[1] = 0.25 * S; + out[2] = (mat[6] + mat[9]) / S; + } else { + S = Math.sqrt(1.0 + mat[10] - mat[0] - mat[5]) * 2; + out[3] = (mat[1] - mat[4]) / S; + out[0] = (mat[8] + mat[2]) / S; + out[1] = (mat[6] + mat[9]) / S; + out[2] = 0.25 * S; + } + + return out; +}; + +/** + * Creates a matrix from a quaternion rotation, vector translation and vector scale + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * var quatMat = mat4.create(); + * quat4.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * mat4.scale(dest, scale) + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @param {vec3} v Translation vector + * @param {vec3} s Scaling vector + * @returns {mat4} out + */ +mat4.fromRotationTranslationScale = function (out, q, v, s) { + // Quaternion math + var x = q[0], y = q[1], z = q[2], w = q[3], + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + xy = x * y2, + xz = x * z2, + yy = y * y2, + yz = y * z2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + wz = w * z2, + sx = s[0], + sy = s[1], + sz = s[2]; + + out[0] = (1 - (yy + zz)) * sx; + out[1] = (xy + wz) * sx; + out[2] = (xz - wy) * sx; + out[3] = 0; + out[4] = (xy - wz) * sy; + out[5] = (1 - (xx + zz)) * sy; + out[6] = (yz + wx) * sy; + out[7] = 0; + out[8] = (xz + wy) * sz; + out[9] = (yz - wx) * sz; + out[10] = (1 - (xx + yy)) * sz; + out[11] = 0; + out[12] = v[0]; + out[13] = v[1]; + out[14] = v[2]; + out[15] = 1; + + return out; +}; + +/** + * Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * mat4.translate(dest, origin); + * var quatMat = mat4.create(); + * quat4.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * mat4.scale(dest, scale) + * mat4.translate(dest, negativeOrigin); + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @param {vec3} v Translation vector + * @param {vec3} s Scaling vector + * @param {vec3} o The origin vector around which to scale and rotate + * @returns {mat4} out + */ +mat4.fromRotationTranslationScaleOrigin = function (out, q, v, s, o) { + // Quaternion math + var x = q[0], y = q[1], z = q[2], w = q[3], + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + xy = x * y2, + xz = x * z2, + yy = y * y2, + yz = y * z2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + wz = w * z2, + + sx = s[0], + sy = s[1], + sz = s[2], + + ox = o[0], + oy = o[1], + oz = o[2]; + + out[0] = (1 - (yy + zz)) * sx; + out[1] = (xy + wz) * sx; + out[2] = (xz - wy) * sx; + out[3] = 0; + out[4] = (xy - wz) * sy; + out[5] = (1 - (xx + zz)) * sy; + out[6] = (yz + wx) * sy; + out[7] = 0; + out[8] = (xz + wy) * sz; + out[9] = (yz - wx) * sz; + out[10] = (1 - (xx + yy)) * sz; + out[11] = 0; + out[12] = v[0] + ox - (out[0] * ox + out[4] * oy + out[8] * oz); + out[13] = v[1] + oy - (out[1] * ox + out[5] * oy + out[9] * oz); + out[14] = v[2] + oz - (out[2] * ox + out[6] * oy + out[10] * oz); + out[15] = 1; + + return out; +}; + +/** + * Calculates a 4x4 matrix from the given quaternion + * + * @param {mat4} out mat4 receiving operation result + * @param {quat} q Quaternion to create matrix from + * + * @returns {mat4} out + */ +mat4.fromQuat = function (out, q) { + var x = q[0], y = q[1], z = q[2], w = q[3], + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + yx = y * x2, + yy = y * y2, + zx = z * x2, + zy = z * y2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + wz = w * z2; + + out[0] = 1 - yy - zz; + out[1] = yx + wz; + out[2] = zx - wy; + out[3] = 0; + + out[4] = yx - wz; + out[5] = 1 - xx - zz; + out[6] = zy + wx; + out[7] = 0; + + out[8] = zx + wy; + out[9] = zy - wx; + out[10] = 1 - xx - yy; + out[11] = 0; + + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + + return out; +}; + +/** + * Generates a frustum matrix with the given bounds + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {Number} left Left bound of the frustum + * @param {Number} right Right bound of the frustum + * @param {Number} bottom Bottom bound of the frustum + * @param {Number} top Top bound of the frustum + * @param {Number} near Near bound of the frustum + * @param {Number} far Far bound of the frustum + * @returns {mat4} out + */ +mat4.frustum = function (out, left, right, bottom, top, near, far) { + var rl = 1 / (right - left), + tb = 1 / (top - bottom), + nf = 1 / (near - far); + out[0] = (near * 2) * rl; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = (near * 2) * tb; + out[6] = 0; + out[7] = 0; + out[8] = (right + left) * rl; + out[9] = (top + bottom) * tb; + out[10] = (far + near) * nf; + out[11] = -1; + out[12] = 0; + out[13] = 0; + out[14] = (far * near * 2) * nf; + out[15] = 0; + return out; +}; + +/** + * Generates a perspective projection matrix with the given bounds + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {number} fovy Vertical field of view in radians + * @param {number} aspect Aspect ratio. typically viewport width/height + * @param {number} near Near bound of the frustum + * @param {number} far Far bound of the frustum + * @returns {mat4} out + */ +mat4.perspective = function (out, fovy, aspect, near, far) { + var f = 1.0 / Math.tan(fovy / 2), + nf = 1 / (near - far); + out[0] = f / aspect; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = f; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = (far + near) * nf; + out[11] = -1; + out[12] = 0; + out[13] = 0; + out[14] = (2 * far * near) * nf; + out[15] = 0; + return out; +}; + +/** + * Generates a perspective projection matrix with the given field of view. + * This is primarily useful for generating projection matrices to be used + * with the still experiemental WebVR API. + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees + * @param {number} near Near bound of the frustum + * @param {number} far Far bound of the frustum + * @returns {mat4} out + */ +mat4.perspectiveFromFieldOfView = function (out, fov, near, far) { + var upTan = Math.tan(fov.upDegrees * Math.PI/180.0), + downTan = Math.tan(fov.downDegrees * Math.PI/180.0), + leftTan = Math.tan(fov.leftDegrees * Math.PI/180.0), + rightTan = Math.tan(fov.rightDegrees * Math.PI/180.0), + xScale = 2.0 / (leftTan + rightTan), + yScale = 2.0 / (upTan + downTan); + + out[0] = xScale; + out[1] = 0.0; + out[2] = 0.0; + out[3] = 0.0; + out[4] = 0.0; + out[5] = yScale; + out[6] = 0.0; + out[7] = 0.0; + out[8] = -((leftTan - rightTan) * xScale * 0.5); + out[9] = ((upTan - downTan) * yScale * 0.5); + out[10] = far / (near - far); + out[11] = -1.0; + out[12] = 0.0; + out[13] = 0.0; + out[14] = (far * near) / (near - far); + out[15] = 0.0; + return out; +} + +/** + * Generates a orthogonal projection matrix with the given bounds + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {number} left Left bound of the frustum + * @param {number} right Right bound of the frustum + * @param {number} bottom Bottom bound of the frustum + * @param {number} top Top bound of the frustum + * @param {number} near Near bound of the frustum + * @param {number} far Far bound of the frustum + * @returns {mat4} out + */ +mat4.ortho = function (out, left, right, bottom, top, near, far) { + var lr = 1 / (left - right), + bt = 1 / (bottom - top), + nf = 1 / (near - far); + out[0] = -2 * lr; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = -2 * bt; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = 2 * nf; + out[11] = 0; + out[12] = (left + right) * lr; + out[13] = (top + bottom) * bt; + out[14] = (far + near) * nf; + out[15] = 1; + return out; +}; + +/** + * Generates a look-at matrix with the given eye position, focal point, and up axis + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {vec3} eye Position of the viewer + * @param {vec3} center Point the viewer is looking at + * @param {vec3} up vec3 pointing up + * @returns {mat4} out + */ +mat4.lookAt = function (out, eye, center, up) { + var x0, x1, x2, y0, y1, y2, z0, z1, z2, len, + eyex = eye[0], + eyey = eye[1], + eyez = eye[2], + upx = up[0], + upy = up[1], + upz = up[2], + centerx = center[0], + centery = center[1], + centerz = center[2]; + + if (Math.abs(eyex - centerx) < glMatrix.EPSILON && + Math.abs(eyey - centery) < glMatrix.EPSILON && + Math.abs(eyez - centerz) < glMatrix.EPSILON) { + return mat4.identity(out); + } + + z0 = eyex - centerx; + z1 = eyey - centery; + z2 = eyez - centerz; + + len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); + z0 *= len; + z1 *= len; + z2 *= len; + + x0 = upy * z2 - upz * z1; + x1 = upz * z0 - upx * z2; + x2 = upx * z1 - upy * z0; + len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); + if (!len) { + x0 = 0; + x1 = 0; + x2 = 0; + } else { + len = 1 / len; + x0 *= len; + x1 *= len; + x2 *= len; + } + + y0 = z1 * x2 - z2 * x1; + y1 = z2 * x0 - z0 * x2; + y2 = z0 * x1 - z1 * x0; + + len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); + if (!len) { + y0 = 0; + y1 = 0; + y2 = 0; + } else { + len = 1 / len; + y0 *= len; + y1 *= len; + y2 *= len; + } + + out[0] = x0; + out[1] = y0; + out[2] = z0; + out[3] = 0; + out[4] = x1; + out[5] = y1; + out[6] = z1; + out[7] = 0; + out[8] = x2; + out[9] = y2; + out[10] = z2; + out[11] = 0; + out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); + out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); + out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); + out[15] = 1; + + return out; +}; + +/** + * Returns a string representation of a mat4 + * + * @param {mat4} mat matrix to represent as a string + * @returns {String} string representation of the matrix + */ +mat4.str = function (a) { + return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + + a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' + + a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' + + a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')'; +}; + +/** + * Returns Frobenius norm of a mat4 + * + * @param {mat4} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +mat4.frob = function (a) { + return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2) + Math.pow(a[9], 2) + Math.pow(a[10], 2) + Math.pow(a[11], 2) + Math.pow(a[12], 2) + Math.pow(a[13], 2) + Math.pow(a[14], 2) + Math.pow(a[15], 2) )) +}; + +/** + * Adds two mat4's + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +mat4.add = function(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]; + out[9] = a[9] + b[9]; + out[10] = a[10] + b[10]; + out[11] = a[11] + b[11]; + out[12] = a[12] + b[12]; + out[13] = a[13] + b[13]; + out[14] = a[14] + b[14]; + out[15] = a[15] + b[15]; + return out; +}; + +/** + * Subtracts matrix b from matrix a + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +mat4.subtract = function(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]; + out[9] = a[9] - b[9]; + out[10] = a[10] - b[10]; + out[11] = a[11] - b[11]; + out[12] = a[12] - b[12]; + out[13] = a[13] - b[13]; + out[14] = a[14] - b[14]; + out[15] = a[15] - b[15]; + return out; +}; + +/** + * Alias for {@link mat4.subtract} + * @function + */ +mat4.sub = mat4.subtract; + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat4} out + */ +mat4.multiplyScalar = function(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + out[4] = a[4] * b; + out[5] = a[5] * b; + out[6] = a[6] * b; + out[7] = a[7] * b; + out[8] = a[8] * b; + out[9] = a[9] * b; + out[10] = a[10] * b; + out[11] = a[11] * b; + out[12] = a[12] * b; + out[13] = a[13] * b; + out[14] = a[14] * b; + out[15] = a[15] * b; + return out; +}; + +/** + * Adds two mat4's after multiplying each element of the second operand by a scalar value. + * + * @param {mat4} out the receiving vector + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat4} out + */ +mat4.multiplyScalarAndAdd = function(out, a, b, scale) { + out[0] = a[0] + (b[0] * scale); + out[1] = a[1] + (b[1] * scale); + out[2] = a[2] + (b[2] * scale); + out[3] = a[3] + (b[3] * scale); + out[4] = a[4] + (b[4] * scale); + out[5] = a[5] + (b[5] * scale); + out[6] = a[6] + (b[6] * scale); + out[7] = a[7] + (b[7] * scale); + out[8] = a[8] + (b[8] * scale); + out[9] = a[9] + (b[9] * scale); + out[10] = a[10] + (b[10] * scale); + out[11] = a[11] + (b[11] * scale); + out[12] = a[12] + (b[12] * scale); + out[13] = a[13] + (b[13] * scale); + out[14] = a[14] + (b[14] * scale); + out[15] = a[15] + (b[15] * scale); + return out; +}; + +/** + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat4} a The first matrix. + * @param {mat4} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat4.exactEquals = function (a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && + a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && + a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && + a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15]; +}; + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat4} a The first matrix. + * @param {mat4} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +mat4.equals = function (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], a9 = a[9], a10 = a[10], a11 = a[11], + a12 = a[12], a13 = a[13], a14 = a[14], a15 = a[15]; + + 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], b9 = b[9], b10 = b[10], b11 = b[11], + b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15]; + + return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && + Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && + Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5)) && + Math.abs(a6 - b6) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a6), Math.abs(b6)) && + Math.abs(a7 - b7) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a7), Math.abs(b7)) && + Math.abs(a8 - b8) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a8), Math.abs(b8)) && + Math.abs(a9 - b9) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a9), Math.abs(b9)) && + Math.abs(a10 - b10) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a10), Math.abs(b10)) && + Math.abs(a11 - b11) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a11), Math.abs(b11)) && + Math.abs(a12 - b12) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a12), Math.abs(b12)) && + Math.abs(a13 - b13) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a13), Math.abs(b13)) && + Math.abs(a14 - b14) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a14), Math.abs(b14)) && + Math.abs(a15 - b15) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a15), Math.abs(b15))); +}; + + + +module.exports = mat4; + +},{"./common.js":169}],174:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); +var mat3 = require("./mat3.js"); +var vec3 = require("./vec3.js"); +var vec4 = require("./vec4.js"); + +/** + * @class Quaternion + * @name quat + */ +var quat = {}; + +/** + * Creates a new identity quat + * + * @returns {quat} a new quaternion + */ +quat.create = function() { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +}; + +/** + * Sets a quaternion to represent the shortest rotation from one + * vector to another. + * + * Both vectors are assumed to be unit length. + * + * @param {quat} out the receiving quaternion. + * @param {vec3} a the initial vector + * @param {vec3} b the destination vector + * @returns {quat} out + */ +quat.rotationTo = (function() { + var tmpvec3 = vec3.create(); + var xUnitVec3 = vec3.fromValues(1,0,0); + var yUnitVec3 = vec3.fromValues(0,1,0); + + return function(out, a, b) { + var dot = vec3.dot(a, b); + if (dot < -0.999999) { + vec3.cross(tmpvec3, xUnitVec3, a); + if (vec3.length(tmpvec3) < 0.000001) + vec3.cross(tmpvec3, yUnitVec3, a); + vec3.normalize(tmpvec3, tmpvec3); + quat.setAxisAngle(out, tmpvec3, Math.PI); + return out; + } else if (dot > 0.999999) { + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; + } else { + vec3.cross(tmpvec3, a, b); + out[0] = tmpvec3[0]; + out[1] = tmpvec3[1]; + out[2] = tmpvec3[2]; + out[3] = 1 + dot; + return quat.normalize(out, out); + } + }; +})(); + +/** + * Sets the specified quaternion with values corresponding to the given + * axes. Each axis is a vec3 and is expected to be unit length and + * perpendicular to all other specified axes. + * + * @param {vec3} view the vector representing the viewing direction + * @param {vec3} right the vector representing the local "right" direction + * @param {vec3} up the vector representing the local "up" direction + * @returns {quat} out + */ +quat.setAxes = (function() { + var matr = mat3.create(); + + return function(out, view, right, up) { + matr[0] = right[0]; + matr[3] = right[1]; + matr[6] = right[2]; + + matr[1] = up[0]; + matr[4] = up[1]; + matr[7] = up[2]; + + matr[2] = -view[0]; + matr[5] = -view[1]; + matr[8] = -view[2]; + + return quat.normalize(out, quat.fromMat3(out, matr)); + }; +})(); + +/** + * Creates a new quat initialized with values from an existing quaternion + * + * @param {quat} a quaternion to clone + * @returns {quat} a new quaternion + * @function + */ +quat.clone = vec4.clone; + +/** + * Creates a new quat initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {quat} a new quaternion + * @function + */ +quat.fromValues = vec4.fromValues; + +/** + * Copy the values from one quat to another + * + * @param {quat} out the receiving quaternion + * @param {quat} a the source quaternion + * @returns {quat} out + * @function + */ +quat.copy = vec4.copy; + +/** + * Set the components of a quat to the given values + * + * @param {quat} out the receiving quaternion + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {quat} out + * @function + */ +quat.set = vec4.set; + +/** + * Set a quat to the identity quaternion + * + * @param {quat} out the receiving quaternion + * @returns {quat} out + */ +quat.identity = function(out) { + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +}; + +/** + * Sets a quat from the given angle and rotation axis, + * then returns it. + * + * @param {quat} out the receiving quaternion + * @param {vec3} axis the axis around which to rotate + * @param {Number} rad the angle in radians + * @returns {quat} out + **/ +quat.setAxisAngle = function(out, axis, rad) { + rad = rad * 0.5; + var s = Math.sin(rad); + out[0] = s * axis[0]; + out[1] = s * axis[1]; + out[2] = s * axis[2]; + out[3] = Math.cos(rad); + return out; +}; + +/** + * Gets the rotation axis and angle for a given + * quaternion. If a quaternion is created with + * setAxisAngle, this method will return the same + * values as providied in the original parameter list + * OR functionally equivalent values. + * Example: The quaternion formed by axis [0, 0, 1] and + * angle -90 is the same as the quaternion formed by + * [0, 0, 1] and 270. This method favors the latter. + * @param {vec3} out_axis Vector receiving the axis of rotation + * @param {quat} q Quaternion to be decomposed + * @return {Number} Angle, in radians, of the rotation + */ +quat.getAxisAngle = function(out_axis, q) { + var rad = Math.acos(q[3]) * 2.0; + var s = Math.sin(rad / 2.0); + if (s != 0.0) { + out_axis[0] = q[0] / s; + out_axis[1] = q[1] / s; + out_axis[2] = q[2] / s; + } else { + // If s is zero, return any axis (no rotation - axis does not matter) + out_axis[0] = 1; + out_axis[1] = 0; + out_axis[2] = 0; + } + return rad; +}; + +/** + * Adds two quat's + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @returns {quat} out + * @function + */ +quat.add = vec4.add; + +/** + * Multiplies two quat's + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @returns {quat} out + */ +quat.multiply = function(out, a, b) { + var ax = a[0], ay = a[1], az = a[2], aw = a[3], + bx = b[0], by = b[1], bz = b[2], bw = b[3]; + + out[0] = ax * bw + aw * bx + ay * bz - az * by; + out[1] = ay * bw + aw * by + az * bx - ax * bz; + out[2] = az * bw + aw * bz + ax * by - ay * bx; + out[3] = aw * bw - ax * bx - ay * by - az * bz; + return out; +}; + +/** + * Alias for {@link quat.multiply} + * @function + */ +quat.mul = quat.multiply; + +/** + * Scales a quat by a scalar number + * + * @param {quat} out the receiving vector + * @param {quat} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {quat} out + * @function + */ +quat.scale = vec4.scale; + +/** + * Rotates a quaternion by the given angle about the X axis + * + * @param {quat} out quat receiving operation result + * @param {quat} a quat to rotate + * @param {number} rad angle (in radians) to rotate + * @returns {quat} out + */ +quat.rotateX = function (out, a, rad) { + rad *= 0.5; + + var ax = a[0], ay = a[1], az = a[2], aw = a[3], + bx = Math.sin(rad), bw = Math.cos(rad); + + out[0] = ax * bw + aw * bx; + out[1] = ay * bw + az * bx; + out[2] = az * bw - ay * bx; + out[3] = aw * bw - ax * bx; + return out; +}; + +/** + * Rotates a quaternion by the given angle about the Y axis + * + * @param {quat} out quat receiving operation result + * @param {quat} a quat to rotate + * @param {number} rad angle (in radians) to rotate + * @returns {quat} out + */ +quat.rotateY = function (out, a, rad) { + rad *= 0.5; + + var ax = a[0], ay = a[1], az = a[2], aw = a[3], + by = Math.sin(rad), bw = Math.cos(rad); + + out[0] = ax * bw - az * by; + out[1] = ay * bw + aw * by; + out[2] = az * bw + ax * by; + out[3] = aw * bw - ay * by; + return out; +}; + +/** + * Rotates a quaternion by the given angle about the Z axis + * + * @param {quat} out quat receiving operation result + * @param {quat} a quat to rotate + * @param {number} rad angle (in radians) to rotate + * @returns {quat} out + */ +quat.rotateZ = function (out, a, rad) { + rad *= 0.5; + + var ax = a[0], ay = a[1], az = a[2], aw = a[3], + bz = Math.sin(rad), bw = Math.cos(rad); + + out[0] = ax * bw + ay * bz; + out[1] = ay * bw - ax * bz; + out[2] = az * bw + aw * bz; + out[3] = aw * bw - az * bz; + return out; +}; + +/** + * Calculates the W component of a quat from the X, Y, and Z components. + * Assumes that quaternion is 1 unit in length. + * Any existing W component will be ignored. + * + * @param {quat} out the receiving quaternion + * @param {quat} a quat to calculate W component of + * @returns {quat} out + */ +quat.calculateW = function (out, a) { + var x = a[0], y = a[1], z = a[2]; + + out[0] = x; + out[1] = y; + out[2] = z; + out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); + return out; +}; + +/** + * Calculates the dot product of two quat's + * + * @param {quat} a the first operand + * @param {quat} b the second operand + * @returns {Number} dot product of a and b + * @function + */ +quat.dot = vec4.dot; + +/** + * Performs a linear interpolation between two quat's + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {quat} out + * @function + */ +quat.lerp = vec4.lerp; + +/** + * Performs a spherical linear interpolation between two quat + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {quat} out + */ +quat.slerp = function (out, a, b, t) { + // benchmarks: + // http://jsperf.com/quaternion-slerp-implementations + + var ax = a[0], ay = a[1], az = a[2], aw = a[3], + bx = b[0], by = b[1], bz = b[2], bw = b[3]; + + var omega, cosom, sinom, scale0, scale1; + + // calc cosine + cosom = ax * bx + ay * by + az * bz + aw * bw; + // adjust signs (if necessary) + if ( cosom < 0.0 ) { + cosom = -cosom; + bx = - bx; + by = - by; + bz = - bz; + bw = - bw; + } + // calculate coefficients + if ( (1.0 - cosom) > 0.000001 ) { + // standard case (slerp) + omega = Math.acos(cosom); + sinom = Math.sin(omega); + scale0 = Math.sin((1.0 - t) * omega) / sinom; + scale1 = Math.sin(t * omega) / sinom; + } else { + // "from" and "to" quaternions are very close + // ... so we can do a linear interpolation + scale0 = 1.0 - t; + scale1 = t; + } + // calculate final values + out[0] = scale0 * ax + scale1 * bx; + out[1] = scale0 * ay + scale1 * by; + out[2] = scale0 * az + scale1 * bz; + out[3] = scale0 * aw + scale1 * bw; + + return out; +}; + +/** + * Performs a spherical linear interpolation with two control points + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @param {quat} c the third operand + * @param {quat} d the fourth operand + * @param {Number} t interpolation amount + * @returns {quat} out + */ +quat.sqlerp = (function () { + var temp1 = quat.create(); + var temp2 = quat.create(); + + return function (out, a, b, c, d, t) { + quat.slerp(temp1, a, d, t); + quat.slerp(temp2, b, c, t); + quat.slerp(out, temp1, temp2, 2 * t * (1 - t)); + + return out; + }; +}()); + +/** + * Calculates the inverse of a quat + * + * @param {quat} out the receiving quaternion + * @param {quat} a quat to calculate inverse of + * @returns {quat} out + */ +quat.invert = function(out, a) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], + dot = a0*a0 + a1*a1 + a2*a2 + a3*a3, + invDot = dot ? 1.0/dot : 0; + + // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 + + out[0] = -a0*invDot; + out[1] = -a1*invDot; + out[2] = -a2*invDot; + out[3] = a3*invDot; + return out; +}; + +/** + * Calculates the conjugate of a quat + * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. + * + * @param {quat} out the receiving quaternion + * @param {quat} a quat to calculate conjugate of + * @returns {quat} out + */ +quat.conjugate = function (out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + out[2] = -a[2]; + out[3] = a[3]; + return out; +}; + +/** + * Calculates the length of a quat + * + * @param {quat} a vector to calculate length of + * @returns {Number} length of a + * @function + */ +quat.length = vec4.length; + +/** + * Alias for {@link quat.length} + * @function + */ +quat.len = quat.length; + +/** + * Calculates the squared length of a quat + * + * @param {quat} a vector to calculate squared length of + * @returns {Number} squared length of a + * @function + */ +quat.squaredLength = vec4.squaredLength; + +/** + * Alias for {@link quat.squaredLength} + * @function + */ +quat.sqrLen = quat.squaredLength; + +/** + * Normalize a quat + * + * @param {quat} out the receiving quaternion + * @param {quat} a quaternion to normalize + * @returns {quat} out + * @function + */ +quat.normalize = vec4.normalize; + +/** + * Creates a quaternion from the given 3x3 rotation matrix. + * + * NOTE: The resultant quaternion is not normalized, so you should be sure + * to renormalize the quaternion yourself where necessary. + * + * @param {quat} out the receiving quaternion + * @param {mat3} m rotation matrix + * @returns {quat} out + * @function + */ +quat.fromMat3 = function(out, m) { + // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes + // article "Quaternion Calculus and Fast Animation". + var fTrace = m[0] + m[4] + m[8]; + var fRoot; + + if ( fTrace > 0.0 ) { + // |w| > 1/2, may as well choose w > 1/2 + fRoot = Math.sqrt(fTrace + 1.0); // 2w + out[3] = 0.5 * fRoot; + fRoot = 0.5/fRoot; // 1/(4w) + out[0] = (m[5]-m[7])*fRoot; + out[1] = (m[6]-m[2])*fRoot; + out[2] = (m[1]-m[3])*fRoot; + } else { + // |w| <= 1/2 + var i = 0; + if ( m[4] > m[0] ) + i = 1; + if ( m[8] > m[i*3+i] ) + i = 2; + var j = (i+1)%3; + var k = (i+2)%3; + + fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0); + out[i] = 0.5 * fRoot; + fRoot = 0.5 / fRoot; + out[3] = (m[j*3+k] - m[k*3+j]) * fRoot; + out[j] = (m[j*3+i] + m[i*3+j]) * fRoot; + out[k] = (m[k*3+i] + m[i*3+k]) * fRoot; + } + + return out; +}; + +/** + * Returns a string representation of a quatenion + * + * @param {quat} vec vector to represent as a string + * @returns {String} string representation of the vector + */ +quat.str = function (a) { + return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; +}; + +/** + * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===) + * + * @param {quat} a The first quaternion. + * @param {quat} b The second quaternion. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +quat.exactEquals = vec4.exactEquals; + +/** + * Returns whether or not the quaternions have approximately the same elements in the same position. + * + * @param {quat} a The first vector. + * @param {quat} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +quat.equals = vec4.equals; + +module.exports = quat; + +},{"./common.js":169,"./mat3.js":172,"./vec3.js":176,"./vec4.js":177}],175:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); + +/** + * @class 2 Dimensional Vector + * @name vec2 + */ +var vec2 = {}; + +/** + * Creates a new, empty vec2 + * + * @returns {vec2} a new 2D vector + */ +vec2.create = function() { + var out = new glMatrix.ARRAY_TYPE(2); + out[0] = 0; + out[1] = 0; + return out; +}; + +/** + * Creates a new vec2 initialized with values from an existing vector + * + * @param {vec2} a vector to clone + * @returns {vec2} a new 2D vector + */ +vec2.clone = function(a) { + var out = new glMatrix.ARRAY_TYPE(2); + out[0] = a[0]; + out[1] = a[1]; + return out; +}; + +/** + * Creates a new vec2 initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @returns {vec2} a new 2D vector + */ +vec2.fromValues = function(x, y) { + var out = new glMatrix.ARRAY_TYPE(2); + out[0] = x; + out[1] = y; + return out; +}; + +/** + * Copy the values from one vec2 to another + * + * @param {vec2} out the receiving vector + * @param {vec2} a the source vector + * @returns {vec2} out + */ +vec2.copy = function(out, a) { + out[0] = a[0]; + out[1] = a[1]; + return out; +}; + +/** + * Set the components of a vec2 to the given values + * + * @param {vec2} out the receiving vector + * @param {Number} x X component + * @param {Number} y Y component + * @returns {vec2} out + */ +vec2.set = function(out, x, y) { + out[0] = x; + out[1] = y; + return out; +}; + +/** + * Adds two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +vec2.add = function(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + return out; +}; + +/** + * Subtracts vector b from vector a + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +vec2.subtract = function(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + return out; +}; + +/** + * Alias for {@link vec2.subtract} + * @function + */ +vec2.sub = vec2.subtract; + +/** + * Multiplies two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +vec2.multiply = function(out, a, b) { + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + return out; +}; + +/** + * Alias for {@link vec2.multiply} + * @function + */ +vec2.mul = vec2.multiply; + +/** + * Divides two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +vec2.divide = function(out, a, b) { + out[0] = a[0] / b[0]; + out[1] = a[1] / b[1]; + return out; +}; + +/** + * Alias for {@link vec2.divide} + * @function + */ +vec2.div = vec2.divide; + +/** + * Math.ceil the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to ceil + * @returns {vec2} out + */ +vec2.ceil = function (out, a) { + out[0] = Math.ceil(a[0]); + out[1] = Math.ceil(a[1]); + return out; +}; + +/** + * Math.floor the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to floor + * @returns {vec2} out + */ +vec2.floor = function (out, a) { + out[0] = Math.floor(a[0]); + out[1] = Math.floor(a[1]); + return out; +}; + +/** + * Returns the minimum of two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +vec2.min = function(out, a, b) { + out[0] = Math.min(a[0], b[0]); + out[1] = Math.min(a[1], b[1]); + return out; +}; + +/** + * Returns the maximum of two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +vec2.max = function(out, a, b) { + out[0] = Math.max(a[0], b[0]); + out[1] = Math.max(a[1], b[1]); + return out; +}; + +/** + * Math.round the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to round + * @returns {vec2} out + */ +vec2.round = function (out, a) { + out[0] = Math.round(a[0]); + out[1] = Math.round(a[1]); + return out; +}; + +/** + * Scales a vec2 by a scalar number + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {vec2} out + */ +vec2.scale = function(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + return out; +}; + +/** + * Adds two vec2's after scaling the second operand by a scalar value + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @param {Number} scale the amount to scale b by before adding + * @returns {vec2} out + */ +vec2.scaleAndAdd = function(out, a, b, scale) { + out[0] = a[0] + (b[0] * scale); + out[1] = a[1] + (b[1] * scale); + return out; +}; + +/** + * Calculates the euclidian distance between two vec2's + * + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {Number} distance between a and b + */ +vec2.distance = function(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1]; + return Math.sqrt(x*x + y*y); +}; + +/** + * Alias for {@link vec2.distance} + * @function + */ +vec2.dist = vec2.distance; + +/** + * Calculates the squared euclidian distance between two vec2's + * + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {Number} squared distance between a and b + */ +vec2.squaredDistance = function(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1]; + return x*x + y*y; +}; + +/** + * Alias for {@link vec2.squaredDistance} + * @function + */ +vec2.sqrDist = vec2.squaredDistance; + +/** + * Calculates the length of a vec2 + * + * @param {vec2} a vector to calculate length of + * @returns {Number} length of a + */ +vec2.length = function (a) { + var x = a[0], + y = a[1]; + return Math.sqrt(x*x + y*y); +}; + +/** + * Alias for {@link vec2.length} + * @function + */ +vec2.len = vec2.length; + +/** + * Calculates the squared length of a vec2 + * + * @param {vec2} a vector to calculate squared length of + * @returns {Number} squared length of a + */ +vec2.squaredLength = function (a) { + var x = a[0], + y = a[1]; + return x*x + y*y; +}; + +/** + * Alias for {@link vec2.squaredLength} + * @function + */ +vec2.sqrLen = vec2.squaredLength; + +/** + * Negates the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to negate + * @returns {vec2} out + */ +vec2.negate = function(out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + return out; +}; + +/** + * Returns the inverse of the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to invert + * @returns {vec2} out + */ +vec2.inverse = function(out, a) { + out[0] = 1.0 / a[0]; + out[1] = 1.0 / a[1]; + return out; +}; + +/** + * Normalize a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to normalize + * @returns {vec2} out + */ +vec2.normalize = function(out, a) { + var x = a[0], + y = a[1]; + var len = x*x + y*y; + if (len > 0) { + //TODO: evaluate use of glm_invsqrt here? + len = 1 / Math.sqrt(len); + out[0] = a[0] * len; + out[1] = a[1] * len; + } + return out; +}; + +/** + * Calculates the dot product of two vec2's + * + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {Number} dot product of a and b + */ +vec2.dot = function (a, b) { + return a[0] * b[0] + a[1] * b[1]; +}; + +/** + * Computes the cross product of two vec2's + * Note that the cross product must by definition produce a 3D vector + * + * @param {vec3} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec3} out + */ +vec2.cross = function(out, a, b) { + var z = a[0] * b[1] - a[1] * b[0]; + out[0] = out[1] = 0; + out[2] = z; + return out; +}; + +/** + * Performs a linear interpolation between two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec2} out + */ +vec2.lerp = function (out, a, b, t) { + var ax = a[0], + ay = a[1]; + out[0] = ax + t * (b[0] - ax); + out[1] = ay + t * (b[1] - ay); + return out; +}; + +/** + * Generates a random vector with the given scale + * + * @param {vec2} out the receiving vector + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned + * @returns {vec2} out + */ +vec2.random = function (out, scale) { + scale = scale || 1.0; + var r = glMatrix.RANDOM() * 2.0 * Math.PI; + out[0] = Math.cos(r) * scale; + out[1] = Math.sin(r) * scale; + return out; +}; + +/** + * Transforms the vec2 with a mat2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat2} m matrix to transform with + * @returns {vec2} out + */ +vec2.transformMat2 = function(out, a, m) { + var x = a[0], + y = a[1]; + out[0] = m[0] * x + m[2] * y; + out[1] = m[1] * x + m[3] * y; + return out; +}; + +/** + * Transforms the vec2 with a mat2d + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat2d} m matrix to transform with + * @returns {vec2} out + */ +vec2.transformMat2d = function(out, a, m) { + var x = a[0], + y = a[1]; + out[0] = m[0] * x + m[2] * y + m[4]; + out[1] = m[1] * x + m[3] * y + m[5]; + return out; +}; + +/** + * Transforms the vec2 with a mat3 + * 3rd vector component is implicitly '1' + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat3} m matrix to transform with + * @returns {vec2} out + */ +vec2.transformMat3 = function(out, a, m) { + var x = a[0], + y = a[1]; + out[0] = m[0] * x + m[3] * y + m[6]; + out[1] = m[1] * x + m[4] * y + m[7]; + return out; +}; + +/** + * Transforms the vec2 with a mat4 + * 3rd vector component is implicitly '0' + * 4th vector component is implicitly '1' + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat4} m matrix to transform with + * @returns {vec2} out + */ +vec2.transformMat4 = function(out, a, m) { + var x = a[0], + y = a[1]; + out[0] = m[0] * x + m[4] * y + m[12]; + out[1] = m[1] * x + m[5] * y + m[13]; + return out; +}; + +/** + * Perform some operation over an array of vec2s. + * + * @param {Array} a the array of vectors to iterate over + * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed + * @param {Number} offset Number of elements to skip at the beginning of the array + * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array + * @param {Function} fn Function to call for each vector in the array + * @param {Object} [arg] additional argument to pass to fn + * @returns {Array} a + * @function + */ +vec2.forEach = (function() { + var vec = vec2.create(); + + return function(a, stride, offset, count, fn, arg) { + var i, l; + if(!stride) { + stride = 2; + } + + if(!offset) { + offset = 0; + } + + if(count) { + l = Math.min((count * stride) + offset, a.length); + } else { + l = a.length; + } + + for(i = offset; i < l; i += stride) { + vec[0] = a[i]; vec[1] = a[i+1]; + fn(vec, vec, arg); + a[i] = vec[0]; a[i+1] = vec[1]; + } + + return a; + }; +})(); + +/** + * Returns a string representation of a vector + * + * @param {vec2} vec vector to represent as a string + * @returns {String} string representation of the vector + */ +vec2.str = function (a) { + return 'vec2(' + a[0] + ', ' + a[1] + ')'; +}; + +/** + * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===) + * + * @param {vec2} a The first vector. + * @param {vec2} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +vec2.exactEquals = function (a, b) { + return a[0] === b[0] && a[1] === b[1]; +}; + +/** + * Returns whether or not the vectors have approximately the same elements in the same position. + * + * @param {vec2} a The first vector. + * @param {vec2} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +vec2.equals = function (a, b) { + var a0 = a[0], a1 = a[1]; + var b0 = b[0], b1 = b[1]; + return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1))); +}; + +module.exports = vec2; + +},{"./common.js":169}],176:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); + +/** + * @class 3 Dimensional Vector + * @name vec3 + */ +var vec3 = {}; + +/** + * Creates a new, empty vec3 + * + * @returns {vec3} a new 3D vector + */ +vec3.create = function() { + var out = new glMatrix.ARRAY_TYPE(3); + out[0] = 0; + out[1] = 0; + out[2] = 0; + return out; +}; + +/** + * Creates a new vec3 initialized with values from an existing vector + * + * @param {vec3} a vector to clone + * @returns {vec3} a new 3D vector + */ +vec3.clone = function(a) { + var out = new glMatrix.ARRAY_TYPE(3); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + return out; +}; + +/** + * Creates a new vec3 initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @returns {vec3} a new 3D vector + */ +vec3.fromValues = function(x, y, z) { + var out = new glMatrix.ARRAY_TYPE(3); + out[0] = x; + out[1] = y; + out[2] = z; + return out; +}; + +/** + * Copy the values from one vec3 to another + * + * @param {vec3} out the receiving vector + * @param {vec3} a the source vector + * @returns {vec3} out + */ +vec3.copy = function(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + return out; +}; + +/** + * Set the components of a vec3 to the given values + * + * @param {vec3} out the receiving vector + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @returns {vec3} out + */ +vec3.set = function(out, x, y, z) { + out[0] = x; + out[1] = y; + out[2] = z; + return out; +}; + +/** + * Adds two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +vec3.add = function(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + return out; +}; + +/** + * Subtracts vector b from vector a + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +vec3.subtract = function(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + return out; +}; + +/** + * Alias for {@link vec3.subtract} + * @function + */ +vec3.sub = vec3.subtract; + +/** + * Multiplies two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +vec3.multiply = function(out, a, b) { + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + out[2] = a[2] * b[2]; + return out; +}; + +/** + * Alias for {@link vec3.multiply} + * @function + */ +vec3.mul = vec3.multiply; + +/** + * Divides two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +vec3.divide = function(out, a, b) { + out[0] = a[0] / b[0]; + out[1] = a[1] / b[1]; + out[2] = a[2] / b[2]; + return out; +}; + +/** + * Alias for {@link vec3.divide} + * @function + */ +vec3.div = vec3.divide; + +/** + * Math.ceil the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to ceil + * @returns {vec3} out + */ +vec3.ceil = function (out, a) { + out[0] = Math.ceil(a[0]); + out[1] = Math.ceil(a[1]); + out[2] = Math.ceil(a[2]); + return out; +}; + +/** + * Math.floor the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to floor + * @returns {vec3} out + */ +vec3.floor = function (out, a) { + out[0] = Math.floor(a[0]); + out[1] = Math.floor(a[1]); + out[2] = Math.floor(a[2]); + return out; +}; + +/** + * Returns the minimum of two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +vec3.min = function(out, a, b) { + out[0] = Math.min(a[0], b[0]); + out[1] = Math.min(a[1], b[1]); + out[2] = Math.min(a[2], b[2]); + return out; +}; + +/** + * Returns the maximum of two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +vec3.max = function(out, a, b) { + out[0] = Math.max(a[0], b[0]); + out[1] = Math.max(a[1], b[1]); + out[2] = Math.max(a[2], b[2]); + return out; +}; + +/** + * Math.round the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to round + * @returns {vec3} out + */ +vec3.round = function (out, a) { + out[0] = Math.round(a[0]); + out[1] = Math.round(a[1]); + out[2] = Math.round(a[2]); + return out; +}; + +/** + * Scales a vec3 by a scalar number + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {vec3} out + */ +vec3.scale = function(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + return out; +}; + +/** + * Adds two vec3's after scaling the second operand by a scalar value + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {Number} scale the amount to scale b by before adding + * @returns {vec3} out + */ +vec3.scaleAndAdd = function(out, a, b, scale) { + out[0] = a[0] + (b[0] * scale); + out[1] = a[1] + (b[1] * scale); + out[2] = a[2] + (b[2] * scale); + return out; +}; + +/** + * Calculates the euclidian distance between two vec3's + * + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {Number} distance between a and b + */ +vec3.distance = function(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1], + z = b[2] - a[2]; + return Math.sqrt(x*x + y*y + z*z); +}; + +/** + * Alias for {@link vec3.distance} + * @function + */ +vec3.dist = vec3.distance; + +/** + * Calculates the squared euclidian distance between two vec3's + * + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {Number} squared distance between a and b + */ +vec3.squaredDistance = function(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1], + z = b[2] - a[2]; + return x*x + y*y + z*z; +}; + +/** + * Alias for {@link vec3.squaredDistance} + * @function + */ +vec3.sqrDist = vec3.squaredDistance; + +/** + * Calculates the length of a vec3 + * + * @param {vec3} a vector to calculate length of + * @returns {Number} length of a + */ +vec3.length = function (a) { + var x = a[0], + y = a[1], + z = a[2]; + return Math.sqrt(x*x + y*y + z*z); +}; + +/** + * Alias for {@link vec3.length} + * @function + */ +vec3.len = vec3.length; + +/** + * Calculates the squared length of a vec3 + * + * @param {vec3} a vector to calculate squared length of + * @returns {Number} squared length of a + */ +vec3.squaredLength = function (a) { + var x = a[0], + y = a[1], + z = a[2]; + return x*x + y*y + z*z; +}; + +/** + * Alias for {@link vec3.squaredLength} + * @function + */ +vec3.sqrLen = vec3.squaredLength; + +/** + * Negates the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to negate + * @returns {vec3} out + */ +vec3.negate = function(out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + out[2] = -a[2]; + return out; +}; + +/** + * Returns the inverse of the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to invert + * @returns {vec3} out + */ +vec3.inverse = function(out, a) { + out[0] = 1.0 / a[0]; + out[1] = 1.0 / a[1]; + out[2] = 1.0 / a[2]; + return out; +}; + +/** + * Normalize a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to normalize + * @returns {vec3} out + */ +vec3.normalize = function(out, a) { + var x = a[0], + y = a[1], + z = a[2]; + var len = x*x + y*y + z*z; + if (len > 0) { + //TODO: evaluate use of glm_invsqrt here? + len = 1 / Math.sqrt(len); + out[0] = a[0] * len; + out[1] = a[1] * len; + out[2] = a[2] * len; + } + return out; +}; + +/** + * Calculates the dot product of two vec3's + * + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {Number} dot product of a and b + */ +vec3.dot = function (a, b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +}; + +/** + * Computes the cross product of two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +vec3.cross = function(out, a, b) { + var ax = a[0], ay = a[1], az = a[2], + bx = b[0], by = b[1], bz = b[2]; + + out[0] = ay * bz - az * by; + out[1] = az * bx - ax * bz; + out[2] = ax * by - ay * bx; + return out; +}; + +/** + * Performs a linear interpolation between two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec3} out + */ +vec3.lerp = function (out, a, b, t) { + var ax = a[0], + ay = a[1], + az = a[2]; + out[0] = ax + t * (b[0] - ax); + out[1] = ay + t * (b[1] - ay); + out[2] = az + t * (b[2] - az); + return out; +}; + +/** + * Performs a hermite interpolation with two control points + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {vec3} c the third operand + * @param {vec3} d the fourth operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec3} out + */ +vec3.hermite = function (out, a, b, c, d, t) { + var factorTimes2 = t * t, + factor1 = factorTimes2 * (2 * t - 3) + 1, + factor2 = factorTimes2 * (t - 2) + t, + factor3 = factorTimes2 * (t - 1), + factor4 = factorTimes2 * (3 - 2 * t); + + out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; + out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; + out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; + + return out; +}; + +/** + * Performs a bezier interpolation with two control points + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {vec3} c the third operand + * @param {vec3} d the fourth operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec3} out + */ +vec3.bezier = function (out, a, b, c, d, t) { + var inverseFactor = 1 - t, + inverseFactorTimesTwo = inverseFactor * inverseFactor, + factorTimes2 = t * t, + factor1 = inverseFactorTimesTwo * inverseFactor, + factor2 = 3 * t * inverseFactorTimesTwo, + factor3 = 3 * factorTimes2 * inverseFactor, + factor4 = factorTimes2 * t; + + out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; + out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; + out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; + + return out; +}; + +/** + * Generates a random vector with the given scale + * + * @param {vec3} out the receiving vector + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned + * @returns {vec3} out + */ +vec3.random = function (out, scale) { + scale = scale || 1.0; + + var r = glMatrix.RANDOM() * 2.0 * Math.PI; + var z = (glMatrix.RANDOM() * 2.0) - 1.0; + var zScale = Math.sqrt(1.0-z*z) * scale; + + out[0] = Math.cos(r) * zScale; + out[1] = Math.sin(r) * zScale; + out[2] = z * scale; + return out; +}; + +/** + * Transforms the vec3 with a mat4. + * 4th vector component is implicitly '1' + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to transform + * @param {mat4} m matrix to transform with + * @returns {vec3} out + */ +vec3.transformMat4 = function(out, a, m) { + var x = a[0], y = a[1], z = a[2], + w = m[3] * x + m[7] * y + m[11] * z + m[15]; + w = w || 1.0; + out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; + out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; + out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w; + return out; +}; + +/** + * Transforms the vec3 with a mat3. + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to transform + * @param {mat4} m the 3x3 matrix to transform with + * @returns {vec3} out + */ +vec3.transformMat3 = function(out, a, m) { + var x = a[0], y = a[1], z = a[2]; + out[0] = x * m[0] + y * m[3] + z * m[6]; + out[1] = x * m[1] + y * m[4] + z * m[7]; + out[2] = x * m[2] + y * m[5] + z * m[8]; + return out; +}; + +/** + * Transforms the vec3 with a quat + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to transform + * @param {quat} q quaternion to transform with + * @returns {vec3} out + */ +vec3.transformQuat = function(out, a, q) { + // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations + + var x = a[0], y = a[1], z = a[2], + qx = q[0], qy = q[1], qz = q[2], qw = q[3], + + // calculate quat * vec + ix = qw * x + qy * z - qz * y, + iy = qw * y + qz * x - qx * z, + iz = qw * z + qx * y - qy * x, + iw = -qx * x - qy * y - qz * z; + + // calculate result * inverse quat + out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; + out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; + out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; + return out; +}; + +/** + * Rotate a 3D vector around the x-axis + * @param {vec3} out The receiving vec3 + * @param {vec3} a The vec3 point to rotate + * @param {vec3} b The origin of the rotation + * @param {Number} c The angle of rotation + * @returns {vec3} out + */ +vec3.rotateX = function(out, a, b, c){ + var p = [], r=[]; + //Translate point to the origin + p[0] = a[0] - b[0]; + p[1] = a[1] - b[1]; + p[2] = a[2] - b[2]; + + //perform rotation + r[0] = p[0]; + r[1] = p[1]*Math.cos(c) - p[2]*Math.sin(c); + r[2] = p[1]*Math.sin(c) + p[2]*Math.cos(c); + + //translate to correct position + out[0] = r[0] + b[0]; + out[1] = r[1] + b[1]; + out[2] = r[2] + b[2]; + + return out; +}; + +/** + * Rotate a 3D vector around the y-axis + * @param {vec3} out The receiving vec3 + * @param {vec3} a The vec3 point to rotate + * @param {vec3} b The origin of the rotation + * @param {Number} c The angle of rotation + * @returns {vec3} out + */ +vec3.rotateY = function(out, a, b, c){ + var p = [], r=[]; + //Translate point to the origin + p[0] = a[0] - b[0]; + p[1] = a[1] - b[1]; + p[2] = a[2] - b[2]; + + //perform rotation + r[0] = p[2]*Math.sin(c) + p[0]*Math.cos(c); + r[1] = p[1]; + r[2] = p[2]*Math.cos(c) - p[0]*Math.sin(c); + + //translate to correct position + out[0] = r[0] + b[0]; + out[1] = r[1] + b[1]; + out[2] = r[2] + b[2]; + + return out; +}; + +/** + * Rotate a 3D vector around the z-axis + * @param {vec3} out The receiving vec3 + * @param {vec3} a The vec3 point to rotate + * @param {vec3} b The origin of the rotation + * @param {Number} c The angle of rotation + * @returns {vec3} out + */ +vec3.rotateZ = function(out, a, b, c){ + var p = [], r=[]; + //Translate point to the origin + p[0] = a[0] - b[0]; + p[1] = a[1] - b[1]; + p[2] = a[2] - b[2]; + + //perform rotation + r[0] = p[0]*Math.cos(c) - p[1]*Math.sin(c); + r[1] = p[0]*Math.sin(c) + p[1]*Math.cos(c); + r[2] = p[2]; + + //translate to correct position + out[0] = r[0] + b[0]; + out[1] = r[1] + b[1]; + out[2] = r[2] + b[2]; + + return out; +}; + +/** + * Perform some operation over an array of vec3s. + * + * @param {Array} a the array of vectors to iterate over + * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed + * @param {Number} offset Number of elements to skip at the beginning of the array + * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array + * @param {Function} fn Function to call for each vector in the array + * @param {Object} [arg] additional argument to pass to fn + * @returns {Array} a + * @function + */ +vec3.forEach = (function() { + var vec = vec3.create(); + + return function(a, stride, offset, count, fn, arg) { + var i, l; + if(!stride) { + stride = 3; + } + + if(!offset) { + offset = 0; + } + + if(count) { + l = Math.min((count * stride) + offset, a.length); + } else { + l = a.length; + } + + for(i = offset; i < l; i += stride) { + vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; + fn(vec, vec, arg); + a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; + } + + return a; + }; +})(); + +/** + * Get the angle between two 3D vectors + * @param {vec3} a The first operand + * @param {vec3} b The second operand + * @returns {Number} The angle in radians + */ +vec3.angle = function(a, b) { + + var tempA = vec3.fromValues(a[0], a[1], a[2]); + var tempB = vec3.fromValues(b[0], b[1], b[2]); + + vec3.normalize(tempA, tempA); + vec3.normalize(tempB, tempB); + + var cosine = vec3.dot(tempA, tempB); + + if(cosine > 1.0){ + return 0; + } else { + return Math.acos(cosine); + } +}; + +/** + * Returns a string representation of a vector + * + * @param {vec3} vec vector to represent as a string + * @returns {String} string representation of the vector + */ +vec3.str = function (a) { + return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')'; +}; + +/** + * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) + * + * @param {vec3} a The first vector. + * @param {vec3} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +vec3.exactEquals = function (a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; +}; + +/** + * Returns whether or not the vectors have approximately the same elements in the same position. + * + * @param {vec3} a The first vector. + * @param {vec3} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +vec3.equals = function (a, b) { + var a0 = a[0], a1 = a[1], a2 = a[2]; + var b0 = b[0], b1 = b[1], b2 = b[2]; + return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2))); +}; + +module.exports = vec3; + +},{"./common.js":169}],177:[function(require,module,exports){ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +var glMatrix = require("./common.js"); + +/** + * @class 4 Dimensional Vector + * @name vec4 + */ +var vec4 = {}; + +/** + * Creates a new, empty vec4 + * + * @returns {vec4} a new 4D vector + */ +vec4.create = function() { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 0; + return out; +}; + +/** + * Creates a new vec4 initialized with values from an existing vector + * + * @param {vec4} a vector to clone + * @returns {vec4} a new 4D vector + */ +vec4.clone = function(a) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +}; + +/** + * Creates a new vec4 initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {vec4} a new 4D vector + */ +vec4.fromValues = function(x, y, z, w) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = x; + out[1] = y; + out[2] = z; + out[3] = w; + return out; +}; + +/** + * Copy the values from one vec4 to another + * + * @param {vec4} out the receiving vector + * @param {vec4} a the source vector + * @returns {vec4} out + */ +vec4.copy = function(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +}; + +/** + * Set the components of a vec4 to the given values + * + * @param {vec4} out the receiving vector + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {vec4} out + */ +vec4.set = function(out, x, y, z, w) { + out[0] = x; + out[1] = y; + out[2] = z; + out[3] = w; + return out; +}; + +/** + * Adds two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +vec4.add = function(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]; + return out; +}; + +/** + * Subtracts vector b from vector a + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +vec4.subtract = function(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]; + return out; +}; + +/** + * Alias for {@link vec4.subtract} + * @function + */ +vec4.sub = vec4.subtract; + +/** + * Multiplies two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +vec4.multiply = function(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]; + return out; +}; + +/** + * Alias for {@link vec4.multiply} + * @function + */ +vec4.mul = vec4.multiply; + +/** + * Divides two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +vec4.divide = function(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]; + return out; +}; + +/** + * Alias for {@link vec4.divide} + * @function + */ +vec4.div = vec4.divide; + +/** + * Math.ceil the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to ceil + * @returns {vec4} out + */ +vec4.ceil = function (out, a) { + out[0] = Math.ceil(a[0]); + out[1] = Math.ceil(a[1]); + out[2] = Math.ceil(a[2]); + out[3] = Math.ceil(a[3]); + return out; +}; + +/** + * Math.floor the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to floor + * @returns {vec4} out + */ +vec4.floor = function (out, a) { + out[0] = Math.floor(a[0]); + out[1] = Math.floor(a[1]); + out[2] = Math.floor(a[2]); + out[3] = Math.floor(a[3]); + return out; +}; + +/** + * Returns the minimum of two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +vec4.min = function(out, a, b) { + out[0] = Math.min(a[0], b[0]); + out[1] = Math.min(a[1], b[1]); + out[2] = Math.min(a[2], b[2]); + out[3] = Math.min(a[3], b[3]); + return out; +}; + +/** + * Returns the maximum of two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +vec4.max = function(out, a, b) { + out[0] = Math.max(a[0], b[0]); + out[1] = Math.max(a[1], b[1]); + out[2] = Math.max(a[2], b[2]); + out[3] = Math.max(a[3], b[3]); + return out; +}; + +/** + * Math.round the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to round + * @returns {vec4} out + */ +vec4.round = function (out, a) { + out[0] = Math.round(a[0]); + out[1] = Math.round(a[1]); + out[2] = Math.round(a[2]); + out[3] = Math.round(a[3]); + return out; +}; + +/** + * Scales a vec4 by a scalar number + * + * @param {vec4} out the receiving vector + * @param {vec4} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {vec4} out + */ +vec4.scale = function(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + return out; +}; + +/** + * Adds two vec4's after scaling the second operand by a scalar value + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @param {Number} scale the amount to scale b by before adding + * @returns {vec4} out + */ +vec4.scaleAndAdd = function(out, a, b, scale) { + out[0] = a[0] + (b[0] * scale); + out[1] = a[1] + (b[1] * scale); + out[2] = a[2] + (b[2] * scale); + out[3] = a[3] + (b[3] * scale); + return out; +}; + +/** + * Calculates the euclidian distance between two vec4's + * + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {Number} distance between a and b + */ +vec4.distance = function(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1], + z = b[2] - a[2], + w = b[3] - a[3]; + return Math.sqrt(x*x + y*y + z*z + w*w); +}; + +/** + * Alias for {@link vec4.distance} + * @function + */ +vec4.dist = vec4.distance; + +/** + * Calculates the squared euclidian distance between two vec4's + * + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {Number} squared distance between a and b + */ +vec4.squaredDistance = function(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1], + z = b[2] - a[2], + w = b[3] - a[3]; + return x*x + y*y + z*z + w*w; +}; + +/** + * Alias for {@link vec4.squaredDistance} + * @function + */ +vec4.sqrDist = vec4.squaredDistance; + +/** + * Calculates the length of a vec4 + * + * @param {vec4} a vector to calculate length of + * @returns {Number} length of a + */ +vec4.length = function (a) { + var x = a[0], + y = a[1], + z = a[2], + w = a[3]; + return Math.sqrt(x*x + y*y + z*z + w*w); +}; + +/** + * Alias for {@link vec4.length} + * @function + */ +vec4.len = vec4.length; + +/** + * Calculates the squared length of a vec4 + * + * @param {vec4} a vector to calculate squared length of + * @returns {Number} squared length of a + */ +vec4.squaredLength = function (a) { + var x = a[0], + y = a[1], + z = a[2], + w = a[3]; + return x*x + y*y + z*z + w*w; +}; + +/** + * Alias for {@link vec4.squaredLength} + * @function + */ +vec4.sqrLen = vec4.squaredLength; + +/** + * Negates the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to negate + * @returns {vec4} out + */ +vec4.negate = function(out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + out[2] = -a[2]; + out[3] = -a[3]; + return out; +}; + +/** + * Returns the inverse of the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to invert + * @returns {vec4} out + */ +vec4.inverse = function(out, a) { + out[0] = 1.0 / a[0]; + out[1] = 1.0 / a[1]; + out[2] = 1.0 / a[2]; + out[3] = 1.0 / a[3]; + return out; +}; + +/** + * Normalize a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to normalize + * @returns {vec4} out + */ +vec4.normalize = function(out, a) { + var x = a[0], + y = a[1], + z = a[2], + w = a[3]; + var len = x*x + y*y + z*z + w*w; + if (len > 0) { + len = 1 / Math.sqrt(len); + out[0] = x * len; + out[1] = y * len; + out[2] = z * len; + out[3] = w * len; + } + return out; +}; + +/** + * Calculates the dot product of two vec4's + * + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {Number} dot product of a and b + */ +vec4.dot = function (a, b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; +}; + +/** + * Performs a linear interpolation between two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec4} out + */ +vec4.lerp = function (out, a, b, t) { + var ax = a[0], + ay = a[1], + az = a[2], + aw = a[3]; + out[0] = ax + t * (b[0] - ax); + out[1] = ay + t * (b[1] - ay); + out[2] = az + t * (b[2] - az); + out[3] = aw + t * (b[3] - aw); + return out; +}; + +/** + * Generates a random vector with the given scale + * + * @param {vec4} out the receiving vector + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned + * @returns {vec4} out + */ +vec4.random = function (out, scale) { + scale = scale || 1.0; + + //TODO: This is a pretty awful way of doing this. Find something better. + out[0] = glMatrix.RANDOM(); + out[1] = glMatrix.RANDOM(); + out[2] = glMatrix.RANDOM(); + out[3] = glMatrix.RANDOM(); + vec4.normalize(out, out); + vec4.scale(out, out, scale); + return out; +}; + +/** + * Transforms the vec4 with a mat4. + * + * @param {vec4} out the receiving vector + * @param {vec4} a the vector to transform + * @param {mat4} m matrix to transform with + * @returns {vec4} out + */ +vec4.transformMat4 = function(out, a, m) { + var x = a[0], y = a[1], z = a[2], w = a[3]; + out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; + out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; + out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; + out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; + return out; +}; + +/** + * Transforms the vec4 with a quat + * + * @param {vec4} out the receiving vector + * @param {vec4} a the vector to transform + * @param {quat} q quaternion to transform with + * @returns {vec4} out + */ +vec4.transformQuat = function(out, a, q) { + var x = a[0], y = a[1], z = a[2], + qx = q[0], qy = q[1], qz = q[2], qw = q[3], + + // calculate quat * vec + ix = qw * x + qy * z - qz * y, + iy = qw * y + qz * x - qx * z, + iz = qw * z + qx * y - qy * x, + iw = -qx * x - qy * y - qz * z; + + // calculate result * inverse quat + out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; + out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; + out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; + out[3] = a[3]; + return out; +}; + +/** + * Perform some operation over an array of vec4s. + * + * @param {Array} a the array of vectors to iterate over + * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed + * @param {Number} offset Number of elements to skip at the beginning of the array + * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array + * @param {Function} fn Function to call for each vector in the array + * @param {Object} [arg] additional argument to pass to fn + * @returns {Array} a + * @function + */ +vec4.forEach = (function() { + var vec = vec4.create(); + + return function(a, stride, offset, count, fn, arg) { + var i, l; + if(!stride) { + stride = 4; + } + + if(!offset) { + offset = 0; + } + + if(count) { + l = Math.min((count * stride) + offset, a.length); + } else { + l = a.length; + } + + for(i = offset; i < l; i += stride) { + vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; vec[3] = a[i+3]; + fn(vec, vec, arg); + a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; a[i+3] = vec[3]; + } + + return a; + }; +})(); + +/** + * Returns a string representation of a vector + * + * @param {vec4} vec vector to represent as a string + * @returns {String} string representation of the vector + */ +vec4.str = function (a) { + return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; +}; + +/** + * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) + * + * @param {vec4} a The first vector. + * @param {vec4} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +vec4.exactEquals = function (a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; +}; + +/** + * Returns whether or not the vectors have approximately the same elements in the same position. + * + * @param {vec4} a The first vector. + * @param {vec4} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +vec4.equals = function (a, b) { + var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; + var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; + return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3))); +}; + +module.exports = vec4; + +},{"./common.js":169}],178:[function(require,module,exports){ +'use strict' + +var barycentric = require('barycentric') +var closestPointToTriangle = require('polytope-closest-point/lib/closest_point_2d.js') + +module.exports = closestPointToPickLocation + +function xformMatrix(m, v) { + var out = [0,0,0,0] + for(var i=0; i<4; ++i) { + for(var j=0; j<4; ++j) { + out[j] += m[4*i + j] * v[i] + } + } + return out +} + +function projectVertex(v, model, view, projection, resolution) { + var p = xformMatrix(projection, + xformMatrix(view, + xformMatrix(model, [v[0], v[1], v[2], 1]))) + for(var i=0; i<3; ++i) { + p[i] /= p[3] + } + return [ 0.5 * resolution[0] * (1.0+p[0]), 0.5 * resolution[1] * (1.0-p[1]) ] +} + +function barycentricCoord(simplex, point) { + if(simplex.length === 2) { + var d0 = 0.0 + var d1 = 0.0 + for(var i=0; i<2; ++i) { + d0 += Math.pow(point[i] - simplex[0][i], 2) + d1 += Math.pow(point[i] - simplex[1][i], 2) + } + d0 = Math.sqrt(d0) + d1 = Math.sqrt(d1) + if(d0+d1 < 1e-6) { + return [1,0] + } + return [d1/(d0+d1),d0/(d1+d0)] + } else if(simplex.length === 3) { + var closestPoint = [0,0] + closestPointToTriangle(simplex[0], simplex[1], simplex[2], point, closestPoint) + return barycentric(simplex, closestPoint) + } + return [] +} + +function interpolate(simplex, weights) { + var result = [0,0,0] + for(var i=0; i 1.0001) { + return null + } + s += weights[i] + } + if(Math.abs(s - 1.0) > 0.001) { + return null + } + return [closestIndex, interpolate(simplex, weights), weights] +} +},{"barycentric":11,"polytope-closest-point/lib/closest_point_2d.js":921}],179:[function(require,module,exports){ + + +var triVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position, normal;\nattribute vec4 color;\nattribute vec2 uv;\n\nuniform mat4 model\n , view\n , projection;\nuniform vec3 eyePosition\n , lightPosition;\n\nvarying vec3 f_normal\n , f_lightDirection\n , f_eyeDirection\n , f_data;\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n vec4 m_position = model * vec4(position, 1.0);\n vec4 t_position = view * m_position;\n gl_Position = projection * t_position;\n f_color = color;\n f_normal = normal;\n f_data = position;\n f_eyeDirection = eyePosition - position;\n f_lightDirection = lightPosition - position;\n f_uv = uv;\n}" +var triFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nfloat beckmannDistribution_2_0(float x, float roughness) {\n float NdotH = max(x, 0.0001);\n float cos2Alpha = NdotH * NdotH;\n float tan2Alpha = (cos2Alpha - 1.0) / cos2Alpha;\n float roughness2 = roughness * roughness;\n float denom = 3.141592653589793 * roughness2 * cos2Alpha * cos2Alpha;\n return exp(tan2Alpha / roughness2) / denom;\n}\n\n\n\nfloat cookTorranceSpecular_1_1(\n vec3 lightDirection,\n vec3 viewDirection,\n vec3 surfaceNormal,\n float roughness,\n float fresnel) {\n\n float VdotN = max(dot(viewDirection, surfaceNormal), 0.0);\n float LdotN = max(dot(lightDirection, surfaceNormal), 0.0);\n\n //Half angle vector\n vec3 H = normalize(lightDirection + viewDirection);\n\n //Geometric term\n float NdotH = max(dot(surfaceNormal, H), 0.0);\n float VdotH = max(dot(viewDirection, H), 0.000001);\n float LdotH = max(dot(lightDirection, H), 0.000001);\n float G1 = (2.0 * NdotH * VdotN) / VdotH;\n float G2 = (2.0 * NdotH * LdotN) / LdotH;\n float G = min(1.0, min(G1, G2));\n \n //Distribution term\n float D = beckmannDistribution_2_0(NdotH, roughness);\n\n //Fresnel term\n float F = pow(1.0 - VdotN, fresnel);\n\n //Multiply terms and done\n return G * F * D / max(3.14159265 * VdotN, 0.000001);\n}\n\n\n\nuniform vec3 clipBounds[2];\nuniform float roughness\n , fresnel\n , kambient\n , kdiffuse\n , kspecular\n , opacity;\nuniform sampler2D texture;\n\nvarying vec3 f_normal\n , f_lightDirection\n , f_eyeDirection\n , f_data;\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n if(any(lessThan(f_data, clipBounds[0])) || \n any(greaterThan(f_data, clipBounds[1]))) {\n discard;\n }\n\n vec3 N = normalize(f_normal);\n vec3 L = normalize(f_lightDirection);\n vec3 V = normalize(f_eyeDirection);\n \n if(!gl_FrontFacing) {\n N = -N;\n }\n\n float specular = cookTorranceSpecular_1_1(L, V, N, roughness, fresnel);\n float diffuse = min(kambient + kdiffuse * max(dot(N, L), 0.0), 1.0);\n\n vec4 surfaceColor = f_color * texture2D(texture, f_uv);\n vec4 litColor = surfaceColor.a * vec4(diffuse * surfaceColor.rgb + kspecular * vec3(1,1,1) * specular, 1.0);\n\n gl_FragColor = litColor * opacity;\n}" +var edgeVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute vec4 color;\nattribute vec2 uv;\n\nuniform mat4 model, view, projection;\n\nvarying vec4 f_color;\nvarying vec3 f_data;\nvarying vec2 f_uv;\n\nvoid main() {\n gl_Position = projection * view * model * vec4(position, 1.0);\n f_color = color;\n f_data = position;\n f_uv = uv;\n}" +var edgeFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 clipBounds[2];\nuniform sampler2D texture;\nuniform float opacity;\n\nvarying vec4 f_color;\nvarying vec3 f_data;\nvarying vec2 f_uv;\n\nvoid main() {\n if(any(lessThan(f_data, clipBounds[0])) || \n any(greaterThan(f_data, clipBounds[1]))) {\n discard;\n }\n\n gl_FragColor = f_color * texture2D(texture, f_uv) * opacity;\n}" +var pointVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute vec4 color;\nattribute vec2 uv;\nattribute float pointSize;\n\nuniform mat4 model, view, projection;\nuniform vec3 clipBounds[2];\n\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n if(any(lessThan(position, clipBounds[0])) || \n any(greaterThan(position, clipBounds[1]))) {\n gl_Position = vec4(0,0,0,0);\n } else {\n gl_Position = projection * view * model * vec4(position, 1.0);\n }\n gl_PointSize = pointSize;\n f_color = color;\n f_uv = uv;\n}" +var pointFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform sampler2D texture;\nuniform float opacity;\n\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n vec2 pointR = gl_PointCoord.xy - vec2(0.5,0.5);\n if(dot(pointR, pointR) > 0.25) {\n discard;\n }\n gl_FragColor = f_color * texture2D(texture, f_uv) * opacity;\n}" +var pickVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute vec4 id;\n\nuniform mat4 model, view, projection;\n\nvarying vec3 f_position;\nvarying vec4 f_id;\n\nvoid main() {\n gl_Position = projection * view * model * vec4(position, 1.0);\n f_id = id;\n f_position = position;\n}" +var pickFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 clipBounds[2];\nuniform float pickId;\n\nvarying vec3 f_position;\nvarying vec4 f_id;\n\nvoid main() {\n if(any(lessThan(f_position, clipBounds[0])) || \n any(greaterThan(f_position, clipBounds[1]))) {\n discard;\n }\n gl_FragColor = vec4(pickId, f_id.xyz);\n}" +var pickPointVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute float pointSize;\nattribute vec4 id;\n\nuniform mat4 model, view, projection;\nuniform vec3 clipBounds[2];\n\nvarying vec3 f_position;\nvarying vec4 f_id;\n\nvoid main() {\n if(any(lessThan(position, clipBounds[0])) || \n any(greaterThan(position, clipBounds[1]))) {\n gl_Position = vec4(0,0,0,0);\n } else {\n gl_Position = projection * view * model * vec4(position, 1.0);\n gl_PointSize = pointSize;\n }\n f_id = id;\n f_position = position;\n}" +var contourVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\n\nuniform mat4 model, view, projection;\n\nvoid main() {\n gl_Position = projection * view * model * vec4(position, 1.0);\n}" +var contourFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 contourColor;\n\nvoid main() {\n gl_FragColor = vec4(contourColor,1);\n}\n" + +exports.meshShader = { + vertex: triVertSrc, + fragment: triFragSrc, + attributes: [ + {name: 'position', type: 'vec3'}, + {name: 'normal', type: 'vec3'}, + {name: 'color', type: 'vec4'}, + {name: 'uv', type: 'vec2'} + ] +} +exports.wireShader = { + vertex: edgeVertSrc, + fragment: edgeFragSrc, + attributes: [ + {name: 'position', type: 'vec3'}, + {name: 'color', type: 'vec4'}, + {name: 'uv', type: 'vec2'} + ] +} +exports.pointShader = { + vertex: pointVertSrc, + fragment: pointFragSrc, + attributes: [ + {name: 'position', type: 'vec3'}, + {name: 'color', type: 'vec4'}, + {name: 'uv', type: 'vec2'}, + {name: 'pointSize', type: 'float'} + ] +} +exports.pickShader = { + vertex: pickVertSrc, + fragment: pickFragSrc, + attributes: [ + {name: 'position', type: 'vec3'}, + {name: 'id', type: 'vec4'} + ] +} +exports.pointPickShader = { + vertex: pickPointVertSrc, + fragment: pickFragSrc, + attributes: [ + {name: 'position', type: 'vec3'}, + {name: 'pointSize', type: 'float'}, + {name: 'id', type: 'vec4'} + ] +} +exports.contourShader = { + vertex: contourVertSrc, + fragment: contourFragSrc, + attributes: [ + {name: 'position', type: 'vec3'} + ] +} + +},{}],180:[function(require,module,exports){ +'use strict' + +var DEFAULT_VERTEX_NORMALS_EPSILON = 1e-6; // may be too large if triangles are very small +var DEFAULT_FACE_NORMALS_EPSILON = 1e-6; + +var createShader = require('gl-shader') +var createBuffer = require('gl-buffer') +var createVAO = require('gl-vao') +var createTexture = require('gl-texture2d') +var normals = require('normals') +var multiply = require('gl-mat4/multiply') +var invert = require('gl-mat4/invert') +var ndarray = require('ndarray') +var colormap = require('colormap') +var getContour = require('simplicial-complex-contour') +var pool = require('typedarray-pool') +var shaders = require('./lib/shaders') +var closestPoint = require('./lib/closest-point') + +var meshShader = shaders.meshShader +var wireShader = shaders.wireShader +var pointShader = shaders.pointShader +var pickShader = shaders.pickShader +var pointPickShader = shaders.pointPickShader +var contourShader = shaders.contourShader + +var identityMatrix = [ + 1,0,0,0, + 0,1,0,0, + 0,0,1,0, + 0,0,0,1] + +function SimplicialMesh(gl + , texture + , triShader + , lineShader + , pointShader + , pickShader + , pointPickShader + , contourShader + , trianglePositions + , triangleIds + , triangleColors + , triangleUVs + , triangleNormals + , triangleVAO + , edgePositions + , edgeIds + , edgeColors + , edgeUVs + , edgeVAO + , pointPositions + , pointIds + , pointColors + , pointUVs + , pointSizes + , pointVAO + , contourPositions + , contourVAO) { + + this.gl = gl + this.cells = [] + this.positions = [] + this.intensity = [] + this.texture = texture + this.dirty = true + + this.triShader = triShader + this.lineShader = lineShader + this.pointShader = pointShader + this.pickShader = pickShader + this.pointPickShader = pointPickShader + this.contourShader = contourShader + + this.trianglePositions = trianglePositions + this.triangleColors = triangleColors + this.triangleNormals = triangleNormals + this.triangleUVs = triangleUVs + this.triangleIds = triangleIds + this.triangleVAO = triangleVAO + this.triangleCount = 0 + + this.lineWidth = 1 + this.edgePositions = edgePositions + this.edgeColors = edgeColors + this.edgeUVs = edgeUVs + this.edgeIds = edgeIds + this.edgeVAO = edgeVAO + this.edgeCount = 0 + + this.pointPositions = pointPositions + this.pointColors = pointColors + this.pointUVs = pointUVs + this.pointSizes = pointSizes + this.pointIds = pointIds + this.pointVAO = pointVAO + this.pointCount = 0 + + this.contourLineWidth = 1 + this.contourPositions = contourPositions + this.contourVAO = contourVAO + this.contourCount = 0 + this.contourColor = [0,0,0] + this.contourEnable = true + + this.pickId = 1 + this.bounds = [ + [ Infinity, Infinity, Infinity], + [-Infinity,-Infinity,-Infinity] ] + this.clipBounds = [ + [-Infinity,-Infinity,-Infinity], + [ Infinity, Infinity, Infinity] ] + + this.lightPosition = [1e5, 1e5, 0] + this.ambientLight = 0.8 + this.diffuseLight = 0.8 + this.specularLight = 2.0 + this.roughness = 0.5 + this.fresnel = 1.5 + + this.opacity = 1.0 + + this._model = identityMatrix + this._view = identityMatrix + this._projection = identityMatrix + this._resolution = [1,1] +} + +var proto = SimplicialMesh.prototype + +proto.isOpaque = function() { + return this.opacity >= 1 +} + +proto.isTransparent = function() { + return this.opacity < 1 +} + +proto.pickSlots = 1 + +proto.setPickBase = function(id) { + this.pickId = id +} + +function genColormap(param) { + var colors = colormap({ + colormap: param + , nshades: 256 + , format: 'rgba' + }) + + var result = new Uint8Array(256*4) + for(var i=0; i<256; ++i) { + var c = colors[i] + for(var j=0; j<3; ++j) { + result[4*i+j] = c[j] + } + result[4*i+3] = c[3]*255 + } + + return ndarray(result, [256,256,4], [4,0,1]) +} + +function unpackIntensity(cells, numVerts, cellIntensity) { + var result = new Array(numVerts) + for(var i=0; i 0) { + var shader = this.triShader + shader.bind() + shader.uniforms = uniforms + + this.triangleVAO.bind() + gl.drawArrays(gl.TRIANGLES, 0, this.triangleCount*3) + this.triangleVAO.unbind() + } + + if(this.edgeCount > 0 && this.lineWidth > 0) { + var shader = this.lineShader + shader.bind() + shader.uniforms = uniforms + + this.edgeVAO.bind() + gl.lineWidth(this.lineWidth) + gl.drawArrays(gl.LINES, 0, this.edgeCount*2) + this.edgeVAO.unbind() + } + + if(this.pointCount > 0) { + var shader = this.pointShader + shader.bind() + shader.uniforms = uniforms + + this.pointVAO.bind() + gl.drawArrays(gl.POINTS, 0, this.pointCount) + this.pointVAO.unbind() + } + + if(this.contourEnable && this.contourCount > 0 && this.contourLineWidth > 0) { + var shader = this.contourShader + shader.bind() + shader.uniforms = uniforms + + this.contourVAO.bind() + gl.drawArrays(gl.LINES, 0, this.contourCount) + this.contourVAO.unbind() + } +} + +proto.drawPick = function(params) { + params = params || {} + + var gl = this.gl + + var model = params.model || identityMatrix + var view = params.view || identityMatrix + var projection = params.projection || identityMatrix + + var clipBounds = [[-1e6,-1e6,-1e6],[1e6,1e6,1e6]] + for(var i=0; i<3; ++i) { + clipBounds[0][i] = Math.max(clipBounds[0][i], this.clipBounds[0][i]) + clipBounds[1][i] = Math.min(clipBounds[1][i], this.clipBounds[1][i]) + } + + //Save camera parameters + this._model = [].slice.call(model) + this._view = [].slice.call(view) + this._projection = [].slice.call(projection) + this._resolution = [gl.drawingBufferWidth, gl.drawingBufferHeight] + + var uniforms = { + model: model, + view: view, + projection: projection, + clipBounds: clipBounds, + pickId: this.pickId / 255.0, + } + + var shader = this.pickShader + shader.bind() + shader.uniforms = uniforms + + if(this.triangleCount > 0) { + this.triangleVAO.bind() + gl.drawArrays(gl.TRIANGLES, 0, this.triangleCount*3) + this.triangleVAO.unbind() + } + + if(this.edgeCount > 0) { + this.edgeVAO.bind() + gl.lineWidth(this.lineWidth) + gl.drawArrays(gl.LINES, 0, this.edgeCount*2) + this.edgeVAO.unbind() + } + + if(this.pointCount > 0) { + var shader = this.pointPickShader + shader.bind() + shader.uniforms = uniforms + + this.pointVAO.bind() + gl.drawArrays(gl.POINTS, 0, this.pointCount) + this.pointVAO.unbind() + } +} + + +proto.pick = function(pickData) { + if(!pickData) { + return null + } + if(pickData.id !== this.pickId) { + return null + } + + var cellId = pickData.value[0] + 256*pickData.value[1] + 65536*pickData.value[2] + var cell = this.cells[cellId] + var positions = this.positions + + var simplex = new Array(cell.length) + for(var i=0; i tickOffset[start]) { + shader.uniforms.dataAxis = DATA_AXIS + shader.uniforms.screenOffset = SCREEN_OFFSET + shader.uniforms.color = textColor[axis] + shader.uniforms.angle = textAngle[axis] + gl.drawArrays( + gl.TRIANGLES, + tickOffset[start], + tickOffset[end] - tickOffset[start]) + } + } + if(labelEnable[axis] && labelCount) { + SCREEN_OFFSET[axis^1] -= screenScale * pixelRatio * labelPad[axis] + shader.uniforms.dataAxis = ZERO_2 + shader.uniforms.screenOffset = SCREEN_OFFSET + shader.uniforms.color = labelColor[axis] + shader.uniforms.angle = labelAngle[axis] + gl.drawArrays( + gl.TRIANGLES, + labelOffset, + labelCount) + } + + SCREEN_OFFSET[axis^1] = screenScale * viewBox[2+(axis^1)] - 1.0 + if(tickEnable[axis+2]) { + SCREEN_OFFSET[axis^1] += screenScale * pixelRatio * tickPad[axis+2] + if(start < end && tickOffset[end] > tickOffset[start]) { + shader.uniforms.dataAxis = DATA_AXIS + shader.uniforms.screenOffset = SCREEN_OFFSET + shader.uniforms.color = textColor[axis+2] + shader.uniforms.angle = textAngle[axis+2] + gl.drawArrays( + gl.TRIANGLES, + tickOffset[start], + tickOffset[end] - tickOffset[start]) + } + } + if(labelEnable[axis+2] && labelCount) { + SCREEN_OFFSET[axis^1] += screenScale * pixelRatio * labelPad[axis+2] + shader.uniforms.dataAxis = ZERO_2 + shader.uniforms.screenOffset = SCREEN_OFFSET + shader.uniforms.color = labelColor[axis+2] + shader.uniforms.angle = labelAngle[axis+2] + gl.drawArrays( + gl.TRIANGLES, + labelOffset, + labelCount) + } + + } +})() + +proto.drawTitle = (function() { + var DATA_AXIS = [0,0] + var SCREEN_OFFSET = [0,0] + + return function() { + var plot = this.plot + var shader = this.shader + var gl = plot.gl + var screenBox = plot.screenBox + var titleCenter = plot.titleCenter + var titleAngle = plot.titleAngle + var titleColor = plot.titleColor + var pixelRatio = plot.pixelRatio + + if(!this.titleCount) { + return + } + + for(var i=0; i<2; ++i) { + SCREEN_OFFSET[i] = 2.0 * (titleCenter[i]*pixelRatio - screenBox[i]) / + (screenBox[2+i] - screenBox[i]) - 1 + } + + shader.bind() + shader.uniforms.dataAxis = DATA_AXIS + shader.uniforms.screenOffset = SCREEN_OFFSET + shader.uniforms.angle = titleAngle + shader.uniforms.color = titleColor + + gl.drawArrays(gl.TRIANGLES, this.titleOffset, this.titleCount) + } +})() + +proto.bind = (function() { + var DATA_SHIFT = [0,0] + var DATA_SCALE = [0,0] + var TEXT_SCALE = [0,0] + + return function() { + var plot = this.plot + var shader = this.shader + var bounds = plot._tickBounds + var dataBox = plot.dataBox + var screenBox = plot.screenBox + var viewBox = plot.viewBox + + shader.bind() + + //Set up coordinate scaling uniforms + for(var i=0; i<2; ++i) { + + var lo = bounds[i] + var hi = bounds[i+2] + var boundScale = hi - lo + var dataCenter = 0.5 * (dataBox[i+2] + dataBox[i]) + var dataWidth = (dataBox[i+2] - dataBox[i]) + + var viewLo = viewBox[i] + var viewHi = viewBox[i+2] + var viewScale = viewHi - viewLo + var screenLo = screenBox[i] + var screenHi = screenBox[i+2] + var screenScale = screenHi - screenLo + + DATA_SCALE[i] = 2.0 * boundScale / dataWidth * viewScale / screenScale + DATA_SHIFT[i] = 2.0 * (lo - dataCenter) / dataWidth * viewScale / screenScale + } + + TEXT_SCALE[1] = 2.0 * plot.pixelRatio / (screenBox[3] - screenBox[1]) + TEXT_SCALE[0] = TEXT_SCALE[1] * (screenBox[3] - screenBox[1]) / (screenBox[2] - screenBox[0]) + + shader.uniforms.dataScale = DATA_SCALE + shader.uniforms.dataShift = DATA_SHIFT + shader.uniforms.textScale = TEXT_SCALE + + //Set attributes + this.vbo.bind() + shader.attributes.textCoordinate.pointer() + } +})() + +proto.update = function(options) { + var vertices = [] + var axesTicks = options.ticks + var bounds = options.bounds + var i, j, k, data, scale, dimension + + for(dimension=0; dimension<2; ++dimension) { + var offsets = [Math.floor(vertices.length/3)], tickX = [-Infinity] + + //Copy vertices over to buffer + var ticks = axesTicks[dimension] + for(i=0; i oldAttribCount) { + for(i = oldAttribCount; i < newAttribCount; i++) { + this.gl.enableVertexAttribArray(i) + } + } else if(oldAttribCount > newAttribCount) { + for(i = newAttribCount; i < oldAttribCount; i++) { + this.gl.disableVertexAttribArray(i) + } + } + + this.gl.lastAttribCount = newAttribCount + + this.gl.useProgram(this.program) +} + +proto.dispose = function() { + + // disabling vertex attributes so new shader starts with zero + // and it's also useful if all shaders are disposed but the + // gl context is reused for subsequent replotting + var oldAttribCount = this.gl.lastAttribCount + for (var i = 0; i < oldAttribCount; i++) { + this.gl.disableVertexAttribArray(i) + } + this.gl.lastAttribCount = 0 + + if(this._fref) { + this._fref.dispose() + } + if(this._vref) { + this._vref.dispose() + } + this.attributes = + this.types = + this.vertShader = + this.fragShader = + this.program = + this._relink = + this._fref = + this._vref = null +} + +function compareAttributes(a, b) { + if(a.name < b.name) { + return -1 + } + return 1 +} + +//Update export hook for glslify-live +proto.update = function( + vertSource + , fragSource + , uniforms + , attributes) { + + //If only one object passed, assume glslify style output + if(!fragSource || arguments.length === 1) { + var obj = vertSource + vertSource = obj.vertex + fragSource = obj.fragment + uniforms = obj.uniforms + attributes = obj.attributes + } + + var wrapper = this + var gl = wrapper.gl + + //Compile vertex and fragment shaders + var pvref = wrapper._vref + wrapper._vref = shaderCache.shader(gl, gl.VERTEX_SHADER, vertSource) + if(pvref) { + pvref.dispose() + } + wrapper.vertShader = wrapper._vref.shader + var pfref = this._fref + wrapper._fref = shaderCache.shader(gl, gl.FRAGMENT_SHADER, fragSource) + if(pfref) { + pfref.dispose() + } + wrapper.fragShader = wrapper._fref.shader + + //If uniforms/attributes is not specified, use RT reflection + if(!uniforms || !attributes) { + + //Create initial test program + var testProgram = gl.createProgram() + gl.attachShader(testProgram, wrapper.fragShader) + gl.attachShader(testProgram, wrapper.vertShader) + gl.linkProgram(testProgram) + if(!gl.getProgramParameter(testProgram, gl.LINK_STATUS)) { + var errLog = gl.getProgramInfoLog(testProgram) + throw new GLError(errLog, 'Error linking program:' + errLog) + } + + //Load data from runtime + uniforms = uniforms || runtime.uniforms(gl, testProgram) + attributes = attributes || runtime.attributes(gl, testProgram) + + //Release test program + gl.deleteProgram(testProgram) + } + + //Sort attributes lexicographically + // overrides undefined WebGL behavior for attribute locations + attributes = attributes.slice() + attributes.sort(compareAttributes) + + //Convert attribute types, read out locations + var attributeUnpacked = [] + var attributeNames = [] + var attributeLocations = [] + var i + for(i=0; i= 0) { + var size = attr.type.charAt(attr.type.length-1)|0 + var locVector = new Array(size) + for(var j=0; j= 0) { + curLocation += 1 + } + attributeLocations[i] = curLocation + } + } + + //Rebuild program and recompute all uniform locations + var uniformLocations = new Array(uniforms.length) + function relink() { + wrapper.program = shaderCache.program( + gl + , wrapper._vref + , wrapper._fref + , attributeNames + , attributeLocations) + + for(var i=0; i= 0) { + var d = type.charCodeAt(type.length-1) - 48 + if(d < 2 || d > 4) { + throw new GLError('', 'Invalid data type for attribute ' + name + ': ' + type) + } + addVectorAttribute( + gl + , wrapper + , locs[0] + , locations + , d + , obj + , name) + } else if(type.indexOf('mat') >= 0) { + var d = type.charCodeAt(type.length-1) - 48 + if(d < 2 || d > 4) { + throw new GLError('', 'Invalid data type for attribute ' + name + ': ' + type) + } + addMatrixAttribute( + gl + , wrapper + , locs + , locations + , d + , obj + , name) + } else { + throw new GLError('', 'Unknown data type for attribute ' + name + ': ' + type) + } + break + } + } + return obj +} + +},{"./GLError":188}],190:[function(require,module,exports){ +'use strict' + +var coallesceUniforms = require('./reflect') +var GLError = require("./GLError") + +module.exports = createUniformWrapper + +//Binds a function and returns a value +function identity(x) { + var c = new Function('y', 'return function(){return y}') + return c(x) +} + +function makeVector(length, fill) { + var result = new Array(length) + for(var i=0; i 4) { + throw new GLError('', 'Invalid data type') + } + switch(type.charAt(0)) { + case 'b': + case 'i': + return 'gl.uniform' + d + 'iv(locations[' + index + '],obj' + path + ')' + case 'v': + return 'gl.uniform' + d + 'fv(locations[' + index + '],obj' + path + ')' + default: + throw new GLError('', 'Unrecognized data type for vector ' + name + ': ' + type) + } + } else if(type.indexOf('mat') === 0 && type.length === 4) { + var d = type.charCodeAt(type.length-1) - 48 + if(d < 2 || d > 4) { + throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + type) + } + return 'gl.uniformMatrix' + d + 'fv(locations[' + index + '],false,obj' + path + ')' + } else { + throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type) + } + break + } + } + + function enumerateIndices(prefix, type) { + if(typeof type !== 'object') { + return [ [prefix, type] ] + } + var indices = [] + for(var id in type) { + var prop = type[id] + var tprefix = prefix + if(parseInt(id) + '' === id) { + tprefix += '[' + id + ']' + } else { + tprefix += '.' + id + } + if(typeof prop === 'object') { + indices.push.apply(indices, enumerateIndices(tprefix, prop)) + } else { + indices.push([tprefix, prop]) + } + } + return indices + } + + function makeSetter(type) { + var code = [ 'return function updateProperty(obj){' ] + var indices = enumerateIndices('', type) + for(var i=0; i 4) { + throw new GLError('', 'Invalid data type') + } + if(type.charAt(0) === 'b') { + return makeVector(d, false) + } + return makeVector(d, 0) + } else if(type.indexOf('mat') === 0 && type.length === 4) { + var d = type.charCodeAt(type.length-1) - 48 + if(d < 2 || d > 4) { + throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + type) + } + return makeVector(d*d, 0) + } else { + throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type) + } + break + } + } + + function storeProperty(obj, prop, type) { + if(typeof type === 'object') { + var child = processObject(type) + Object.defineProperty(obj, prop, { + get: identity(child), + set: makeSetter(type), + enumerable: true, + configurable: false + }) + } else { + if(locations[type]) { + Object.defineProperty(obj, prop, { + get: makeGetter(type), + set: makeSetter(type), + enumerable: true, + configurable: false + }) + } else { + obj[prop] = defaultValue(uniforms[type].type) + } + } + } + + function processObject(obj) { + var result + if(Array.isArray(obj)) { + result = new Array(obj.length) + for(var i=0; i 1) { + if(!(x[0] in o)) { + o[x[0]] = [] + } + o = o[x[0]] + for(var k=1; k 1) { + for(var j=0; j= 0)) { + continue + } + + var zeroIntercept = screenBox[i] - + dataBox[i] * (screenBox[i+2] - screenBox[i]) / (dataBox[i+2] - dataBox[i]) + + if(i === 0) { + line.drawLine( + zeroIntercept, screenBox[1], zeroIntercept, screenBox[3], + zeroLineWidth[i], + zeroLineColor[i]) + } else { + line.drawLine( + screenBox[0], zeroIntercept, screenBox[2], zeroIntercept, + zeroLineWidth[i], + zeroLineColor[i]) + } + } + } + + //Draw traces + for(var i=0; i=0; --i) { + this.objects[i].dispose() + } + this.objects.length = 0 + for(var i=this.overlays.length-1; i>=0; --i) { + this.overlays[i].dispose() + } + this.overlays.length = 0 + + this.gl = null +} + +proto.addObject = function(object) { + if(this.objects.indexOf(object) < 0) { + this.objects.push(object) + this.setDirty() + } +} + +proto.removeObject = function(object) { + var objects = this.objects + for(var i=0; i 0) { + var base = Math.round(Math.pow(10, y)) + return Math.ceil(x/base) * base + } + return Math.ceil(x) +} + +function defaultBool(x) { + if(typeof x === 'boolean') { + return x + } + return true +} + +function createScene(options) { + options = options || {} + + var stopped = false + + var pixelRatio = options.pixelRatio || parseFloat(window.devicePixelRatio) + + var canvas = options.canvas + if(!canvas) { + canvas = document.createElement('canvas') + if(options.container) { + var container = options.container + container.appendChild(canvas) + } else { + document.body.appendChild(canvas) + } + } + + var gl = options.gl + if(!gl) { + gl = getContext(canvas, + options.glOptions || { + premultipliedAlpha: true, + antialias: true + }) + } + if(!gl) { + throw new Error('webgl not supported') + } + + //Initial bounds + var bounds = options.bounds || [[-10,-10,-10], [10,10,10]] + + //Create selection + var selection = new MouseSelect() + + //Accumulation buffer + var accumBuffer = createFBO(gl, + [gl.drawingBufferWidth, gl.drawingBufferHeight], { + preferFloat: !isMobile + }) + + var accumShader = createShader(gl) + + //Create a camera + var cameraOptions = options.camera || { + eye: [2,0,0], + center: [0,0,0], + up: [0,1,0], + zoomMin: 0.1, + zoomMax: 100, + mode: 'turntable' + } + + //Create axes + var axesOptions = options.axes || {} + var axes = createAxes(gl, axesOptions) + axes.enable = !axesOptions.disable + + //Create spikes + var spikeOptions = options.spikes || {} + var spikes = createSpikes(gl, spikeOptions) + + //Object list is empty initially + var objects = [] + var pickBufferIds = [] + var pickBufferCount = [] + var pickBuffers = [] + + //Dirty flag, skip redraw if scene static + var dirty = true + var pickDirty = true + + var projection = new Array(16) + var model = new Array(16) + + var cameraParams = { + view: null, + projection: projection, + model: model + } + + var pickDirty = true + + var viewShape = [ gl.drawingBufferWidth, gl.drawingBufferHeight ] + + //Create scene object + var scene = { + gl: gl, + contextLost: false, + pixelRatio: options.pixelRatio || parseFloat(window.devicePixelRatio), + canvas: canvas, + selection: selection, + camera: createCamera(canvas, cameraOptions), + axes: axes, + axesPixels: null, + spikes: spikes, + bounds: bounds, + objects: objects, + shape: viewShape, + aspect: options.aspectRatio || [1,1,1], + pickRadius: options.pickRadius || 10, + zNear: options.zNear || 0.01, + zFar: options.zFar || 1000, + fovy: options.fovy || Math.PI/4, + clearColor: options.clearColor || [0,0,0,0], + autoResize: defaultBool(options.autoResize), + autoBounds: defaultBool(options.autoBounds), + autoScale: !!options.autoScale, + autoCenter: defaultBool(options.autoCenter), + clipToBounds: defaultBool(options.clipToBounds), + snapToData: !!options.snapToData, + onselect: options.onselect || null, + onrender: options.onrender || null, + onclick: options.onclick || null, + cameraParams: cameraParams, + oncontextloss: null, + mouseListener: null + } + + var pickShape = [ (gl.drawingBufferWidth/scene.pixelRatio)|0, (gl.drawingBufferHeight/scene.pixelRatio)|0 ] + + function resizeListener() { + if(stopped) { + return + } + if(!scene.autoResize) { + return + } + var parent = canvas.parentNode + var width = 1 + var height = 1 + if(parent && parent !== document.body) { + width = parent.clientWidth + height = parent.clientHeight + } else { + width = window.innerWidth + height = window.innerHeight + } + var nextWidth = Math.ceil(width * scene.pixelRatio)|0 + var nextHeight = Math.ceil(height * scene.pixelRatio)|0 + if(nextWidth !== canvas.width || nextHeight !== canvas.height) { + canvas.width = nextWidth + canvas.height = nextHeight + var style = canvas.style + style.position = style.position || 'absolute' + style.left = '0px' + style.top = '0px' + style.width = width + 'px' + style.height = height + 'px' + dirty = true + } + } + if(scene.autoResize) { + resizeListener() + } + window.addEventListener('resize', resizeListener) + + function reallocPickIds() { + var numObjs = objects.length + var numPick = pickBuffers.length + for(var i=0; i 0 && pickBufferCount[numPick-1] === 0) { + pickBufferCount.pop() + pickBuffers.pop().dispose() + } + } + + scene.update = function(options) { + if(stopped) { + return + } + options = options || {} + dirty = true + pickDirty = true + } + + scene.add = function(obj) { + if(stopped) { + return + } + obj.axes = axes + objects.push(obj) + pickBufferIds.push(-1) + dirty = true + pickDirty = true + reallocPickIds() + } + + scene.remove = function(obj) { + if(stopped) { + return + } + var idx = objects.indexOf(obj) + if(idx < 0) { + return + } + objects.splice(idx, 1) + pickBufferIds.pop() + dirty = true + pickDirty = true + reallocPickIds() + } + + scene.dispose = function() { + if(stopped) { + return + } + + stopped = true + + window.removeEventListener('resize', resizeListener) + canvas.removeEventListener('webglcontextlost', checkContextLoss) + scene.mouseListener.enabled = false + + if(scene.contextLost) { + return + } + + //Destroy objects + axes.dispose() + spikes.dispose() + for(var i=0; i selection.distance) { + continue + } + for(var j=0; j>> 1 + var dataStraightThrough = options.positions instanceof Float32Array + var idStraightThrough = options.idToIndex instanceof Int32Array && options.idToIndex.length >= pointCount // permit larger to help reuse + + var data = options.positions + var packed = dataStraightThrough ? data : pool.mallocFloat32(data.length) + var packedId = idStraightThrough ? options.idToIndex : pool.mallocInt32(pointCount) + + if(!dataStraightThrough) { + packed.set(data) + } + + if(!idStraightThrough) { + packed.set(data) + for(i = 0; i < pointCount; i++) { + packedId[i] = i + } + } + + this.points = data + + this.offsetBuffer.update(packed) + this.pickBuffer.update(packedId) + + if(!dataStraightThrough) { + pool.free(packed) + } + + if(!idStraightThrough) { + pool.free(packedId) + } + + this.pointCount = pointCount + this.pickOffset = 0 +} + +function count(points, dataBox) { + var visiblePointCountEstimate = 0 + var length = points.length >>> 1 + var i + for(i = 0; i < length; i++) { + var x = points[i * 2] + var y = points[i * 2 + 1] + if(x >= dataBox[0] && x <= dataBox[2] && y >= dataBox[1] && y <= dataBox[3]) + visiblePointCountEstimate++ + } + return visiblePointCountEstimate +} + +proto.unifiedDraw = (function() { + var MATRIX = [1, 0, 0, + 0, 1, 0, + 0, 0, 1] + var PICK_VEC4 = [0, 0, 0, 0] +return function(pickOffset) { + + var pick = pickOffset !== void(0) + + var shader = pick ? this.pickShader : this.shader + var gl = this.plot.gl + var dataBox = this.plot.dataBox + + if(this.pointCount === 0) { + return pickOffset + } + + var dataX = dataBox[2] - dataBox[0] + var dataY = dataBox[3] - dataBox[1] + + var visiblePointCountEstimate = count(this.points, dataBox) + var basicPointSize = this.plot.pickPixelRatio * Math.max(Math.min(this.sizeMinCap, this.sizeMin), Math.min(this.sizeMax, this.sizeMax / Math.pow(visiblePointCountEstimate, 0.33333))) + + MATRIX[0] = 2.0 / dataX + MATRIX[4] = 2.0 / dataY + MATRIX[6] = -2.0 * dataBox[0] / dataX - 1.0 + MATRIX[7] = -2.0 * dataBox[1] / dataY - 1.0 + + this.offsetBuffer.bind() + + shader.bind() + shader.attributes.position.pointer() + shader.uniforms.matrix = MATRIX + shader.uniforms.color = this.color + shader.uniforms.borderColor = this.borderColor + shader.uniforms.pointCloud = basicPointSize < 5 + shader.uniforms.pointSize = basicPointSize + shader.uniforms.centerFraction = Math.min(1, Math.max(0, Math.sqrt(1 - this.areaRatio))) + + if(pick) { + + PICK_VEC4[0] = ( pickOffset & 0xff) + PICK_VEC4[1] = ((pickOffset >> 8) & 0xff) + PICK_VEC4[2] = ((pickOffset >> 16) & 0xff) + PICK_VEC4[3] = ((pickOffset >> 24) & 0xff) + + this.pickBuffer.bind() + shader.attributes.pickId.pointer(gl.UNSIGNED_BYTE) + shader.uniforms.pickOffset = PICK_VEC4 + this.pickOffset = pickOffset + } + + // Worth switching these off, but we can't make assumptions about other + // renderers, so let's restore it after each draw + var blend = gl.getParameter(gl.BLEND) + var dither = gl.getParameter(gl.DITHER) + + if(blend && !this.blend) + gl.disable(gl.BLEND) + if(dither) + gl.disable(gl.DITHER) + + gl.drawArrays(gl.POINTS, 0, this.pointCount) + + if(blend && !this.blend) + gl.enable(gl.BLEND) + if(dither) + gl.enable(gl.DITHER) + + return pickOffset + this.pointCount +} +})() + +proto.draw = proto.unifiedDraw +proto.drawPick = proto.unifiedDraw + +proto.pick = function(x, y, value) { + var pickOffset = this.pickOffset + var pointCount = this.pointCount + if(value < pickOffset || value >= pickOffset + pointCount) { + return null + } + var pointId = value - pickOffset + var points = this.points + return { + object: this, + pointId: pointId, + dataCoord: [points[2 * pointId], points[2 * pointId + 1] ] + } +} + +function createPointcloud2D(plot, options) { + var gl = plot.gl + var buffer = createBuffer(gl) + var pickBuffer = createBuffer(gl) + var shader = createShader(gl, SHADERS.pointVertex, SHADERS.pointFragment) + var pickShader = createShader(gl, SHADERS.pickVertex, SHADERS.pickFragment) + + var result = new Pointcloud2D(plot, buffer, pickBuffer, shader, pickShader) + result.update(options) + + //Register with plot + plot.addObject(result) + + return result +} + +},{"./lib/shader":197,"gl-buffer":132,"gl-shader":198,"typedarray-pool":992}],206:[function(require,module,exports){ +module.exports = slerp + +/** + * Performs a spherical linear interpolation between two quat + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {quat} out + */ +function slerp (out, a, b, t) { + // benchmarks: + // http://jsperf.com/quaternion-slerp-implementations + + var ax = a[0], ay = a[1], az = a[2], aw = a[3], + bx = b[0], by = b[1], bz = b[2], bw = b[3] + + var omega, cosom, sinom, scale0, scale1 + + // calc cosine + cosom = ax * bx + ay * by + az * bz + aw * bw + // adjust signs (if necessary) + if (cosom < 0.0) { + cosom = -cosom + bx = -bx + by = -by + bz = -bz + bw = -bw + } + // calculate coefficients + if ((1.0 - cosom) > 0.000001) { + // standard case (slerp) + omega = Math.acos(cosom) + sinom = Math.sin(omega) + scale0 = Math.sin((1.0 - t) * omega) / sinom + scale1 = Math.sin(t * omega) / sinom + } else { + // "from" and "to" quaternions are very close + // ... so we can do a linear interpolation + scale0 = 1.0 - t + scale1 = t + } + // calculate final values + out[0] = scale0 * ax + scale1 * bx + out[1] = scale0 * ay + scale1 * by + out[2] = scale0 * az + scale1 * bz + out[3] = scale0 * aw + scale1 * bw + + return out +} + +},{}],207:[function(require,module,exports){ +'use strict' + + + +module.exports = { + vertex: "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi, positionLo;\nattribute vec2 offset;\nattribute vec4 color;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale;\n\nvarying vec4 fragColor;\n\n\nvec4 computePosition_1_0(vec2 posHi, vec2 posLo, vec2 scHi, vec2 scLo, vec2 trHi, vec2 trLo, vec2 screenScale, vec2 screenOffset) {\n return vec4((posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo\n + screenScale * screenOffset, 0, 1);\n}\n\nvoid main() {\n fragColor = color;\n\n gl_Position = computePosition_1_0(\n positionHi, positionLo,\n scaleHi, scaleLo,\n translateHi, translateLo,\n pixelScale, offset);\n}\n", + fragment: "precision lowp float;\n#define GLSLIFY 1\nvarying vec4 fragColor;\nvoid main() {\n gl_FragColor = vec4(fragColor.rgb * fragColor.a, fragColor.a);\n}\n", + pickVertex: "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi, positionLo;\nattribute vec2 offset;\nattribute vec4 id;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale;\nuniform vec4 pickOffset;\n\nvarying vec4 fragColor;\n\n\nvec4 computePosition_1_0(vec2 posHi, vec2 posLo, vec2 scHi, vec2 scLo, vec2 trHi, vec2 trLo, vec2 screenScale, vec2 screenOffset) {\n return vec4((posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo\n + screenScale * screenOffset, 0, 1);\n}\n\nvoid main() {\n vec4 fragId = id + pickOffset;\n\n fragId.y += floor(fragId.x / 256.0);\n fragId.x -= floor(fragId.x / 256.0) * 256.0;\n\n fragId.z += floor(fragId.y / 256.0);\n fragId.y -= floor(fragId.y / 256.0) * 256.0;\n\n fragId.w += floor(fragId.z / 256.0);\n fragId.z -= floor(fragId.z / 256.0) * 256.0;\n\n fragColor = fragId / 255.0;\n\n gl_Position = computePosition_1_0(\n positionHi, positionLo,\n scaleHi, scaleLo,\n translateHi, translateLo,\n pixelScale, offset);\n}\n", + pickFragment: "precision lowp float;\n#define GLSLIFY 1\nvarying vec4 fragColor;\nvoid main() {\n gl_FragColor = fragColor;\n}\n" +} + +},{}],208:[function(require,module,exports){ +arguments[4][187][0].apply(exports,arguments) +},{"./lib/GLError":209,"./lib/create-attributes":210,"./lib/create-uniforms":211,"./lib/reflect":212,"./lib/runtime-reflect":213,"./lib/shader-cache":214,"dup":187}],209:[function(require,module,exports){ +arguments[4][188][0].apply(exports,arguments) +},{"dup":188}],210:[function(require,module,exports){ +arguments[4][189][0].apply(exports,arguments) +},{"./GLError":209,"dup":189}],211:[function(require,module,exports){ +arguments[4][190][0].apply(exports,arguments) +},{"./GLError":209,"./reflect":212,"dup":190}],212:[function(require,module,exports){ +arguments[4][191][0].apply(exports,arguments) +},{"dup":191}],213:[function(require,module,exports){ +arguments[4][192][0].apply(exports,arguments) +},{"dup":192}],214:[function(require,module,exports){ +arguments[4][193][0].apply(exports,arguments) +},{"./GLError":209,"dup":193,"gl-format-compiler-error":140,"weakmap-shim":1014}],215:[function(require,module,exports){ +'use strict' + +module.exports = createFancyScatter2D + +var createShader = require('gl-shader') +var createBuffer = require('gl-buffer') +var textCache = require('text-cache') +var pool = require('typedarray-pool') +var vectorizeText = require('vectorize-text') +var shaders = require('./lib/shaders') + +var BOUNDARIES = {} + +function getBoundary(glyph) { + if(glyph in BOUNDARIES) { + return BOUNDARIES[glyph] + } + + var polys = vectorizeText(glyph, { + polygons: true, + font: 'sans-serif', + textAlign: 'left', + textBaseline: 'alphabetic' + }) + + var coords = [] + var normals = [] + + polys.forEach(function(loops) { + loops.forEach(function(loop) { + for(var i=0; i < loop.length; ++i) { + var a = loop[(i + loop.length - 1) % loop.length] + var b = loop[i] + var c = loop[(i + 1) % loop.length] + var d = loop[(i + 2) % loop.length] + + var dx = b[0] - a[0] + var dy = b[1] - a[1] + var dl = Math.sqrt(dx * dx + dy * dy) + dx /= dl + dy /= dl + + coords.push(a[0], a[1] + 1.4) + normals.push(dy, -dx) + coords.push(a[0], a[1] + 1.4) + normals.push(-dy, dx) + coords.push(b[0], b[1] + 1.4) + normals.push(-dy, dx) + + coords.push(b[0], b[1] + 1.4) + normals.push(-dy, dx) + coords.push(a[0], a[1] + 1.4) + normals.push(dy, -dx) + coords.push(b[0], b[1] + 1.4) + normals.push(dy, -dx) + + var ex = d[0] - c[0] + var ey = d[1] - c[1] + var el = Math.sqrt(ex * ex + ey * ey) + ex /= el + ey /= el + + coords.push(b[0], b[1] + 1.4) + normals.push(dy, -dx) + coords.push(b[0], b[1] + 1.4) + normals.push(-dy, dx) + coords.push(c[0], c[1] + 1.4) + normals.push(-ey, ex) + + coords.push(c[0], c[1] + 1.4) + normals.push(-ey, ex) + coords.push(b[0], b[1] + 1.4) + normals.push(ey, -ex) + coords.push(c[0], c[1] + 1.4) + normals.push(ey, -ex) + } + }) + }) + + var bounds = [Infinity, Infinity, -Infinity, -Infinity] + for(var i = 0; i < coords.length; i += 2) { + for(var j = 0; j < 2; ++j) { + bounds[j] = Math.min(bounds[j], coords[i + j]) + bounds[2 + j] = Math.max(bounds[2 + j], coords[i + j]) + } + } + + return BOUNDARIES[glyph] = { + coords: coords, + normals: normals, + bounds: bounds + } +} + +function GLScatterFancy( + plot, + shader, + pickShader, + positionHiBuffer, + positionLoBuffer, + offsetBuffer, + colorBuffer, + idBuffer) { + this.plot = plot + this.shader = shader + this.pickShader = pickShader + this.posHiBuffer = positionHiBuffer + this.posLoBuffer = positionLoBuffer + this.offsetBuffer = offsetBuffer + this.colorBuffer = colorBuffer + this.idBuffer = idBuffer + this.bounds = [Infinity, Infinity, -Infinity, -Infinity] + this.numPoints = 0 + this.numVertices = 0 + this.pickOffset = 0 + this.points = null +} + +var proto = GLScatterFancy.prototype + +;(function() { + var SCALE_HI = new Float32Array([0, 0]) + var SCALE_LO = new Float32Array([0, 0]) + var TRANSLATE_HI = new Float32Array([0, 0]) + var TRANSLATE_LO = new Float32Array([0, 0]) + + var PIXEL_SCALE = [0, 0] + + function calcScales() { + var plot = this.plot + var bounds = this.bounds + var viewBox = plot.viewBox + var dataBox = plot.dataBox + var pixelRatio = plot.pixelRatio + + var boundX = bounds[2] - bounds[0] + var boundY = bounds[3] - bounds[1] + var dataX = dataBox[2] - dataBox[0] + var dataY = dataBox[3] - dataBox[1] + + var scaleX = 2 * boundX / dataX + var scaleY = 2 * boundY / dataY + var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX + var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY + + SCALE_HI[0] = scaleX + SCALE_LO[0] = scaleX - SCALE_HI[0] + SCALE_HI[1] = scaleY + SCALE_LO[1] = scaleY - SCALE_HI[1] + + TRANSLATE_HI[0] = translateX + TRANSLATE_LO[0] = translateX - TRANSLATE_HI[0] + TRANSLATE_HI[1] = translateY + TRANSLATE_LO[1] = translateY - TRANSLATE_HI[1] + + var screenX = viewBox[2] - viewBox[0] + var screenY = viewBox[3] - viewBox[1] + + PIXEL_SCALE[0] = 2 * pixelRatio / screenX + PIXEL_SCALE[1] = 2 * pixelRatio / screenY + } + + var PICK_OFFSET = [0, 0, 0, 0] + + proto.drawPick = function(offset) { + + var pick = offset !== undefined + var plot = this.plot + + var numVertices = this.numVertices + + if(!numVertices) { + return offset + } + + calcScales.call(this) + + var gl = plot.gl + var shader = pick ? this.pickShader : this.shader + + shader.bind() + + if(pick) { + + this.pickOffset = offset + + for (var i = 0; i < 4; ++i) { + PICK_OFFSET[i] = (offset >> (i * 8)) & 0xff + } + + shader.uniforms.pickOffset = PICK_OFFSET + + this.idBuffer.bind() + shader.attributes.id.pointer(gl.UNSIGNED_BYTE, false) + + } else { + + this.colorBuffer.bind() + shader.attributes.color.pointer(gl.UNSIGNED_BYTE, true) + + } + + this.posHiBuffer.bind() + shader.attributes.positionHi.pointer() + + this.posLoBuffer.bind() + shader.attributes.positionLo.pointer() + + this.offsetBuffer.bind() + shader.attributes.offset.pointer() + + shader.uniforms.pixelScale = PIXEL_SCALE + shader.uniforms.scaleHi = SCALE_HI + shader.uniforms.scaleLo = SCALE_LO + shader.uniforms.translateHi = TRANSLATE_HI + shader.uniforms.translateLo = TRANSLATE_LO + + gl.drawArrays(gl.TRIANGLES, 0, numVertices) + + if(pick) return offset + this.numPoints + } +})() + +proto.draw = proto.drawPick + +proto.pick = function(x, y, value) { + var pickOffset = this.pickOffset + var pointCount = this.numPoints + if(value < pickOffset || value >= pickOffset + pointCount) { + return null + } + var pointId = value - pickOffset + var points = this.points + return { + object: this, + pointId: pointId, + dataCoord: [points[2 * pointId], points[2 * pointId + 1]] + } +} + +proto.update = function(options) { + options = options || {} + + var positions = options.positions || [] + var colors = options.colors || [] + var glyphs = options.glyphs || [] + var sizes = options.sizes || [] + var borderWidths = options.borderWidths || [] + var borderColors = options.borderColors || [] + var i, j + + this.points = positions + + var bounds = this.bounds = [Infinity, Infinity, -Infinity, -Infinity] + var numVertices = 0 + + var glyphMeshes = [] + var glyphBoundaries = [] + var glyph, border + + for(i = 0; i < glyphs.length; ++i) { + glyph = textCache('sans-serif', glyphs[i]) + border = getBoundary(glyphs[i]) + glyphMeshes.push(glyph) + glyphBoundaries.push(border) + numVertices += (glyph.data.length + border.coords.length) >> 1 + for(j = 0; j < 2; ++j) { + bounds[j] = Math.min(bounds[j], positions[2 * i + j]) + bounds[2 + j] = Math.max(bounds[2 + j], positions[2 * i + j]) + } + } + + if(bounds[0] === bounds[2]) { + bounds[2] += 1 + } + if(bounds[3] === bounds[1]) { + bounds[3] += 1 + } + + var sx = 1 / (bounds[2] - bounds[0]) + var sy = 1 / (bounds[3] - bounds[1]) + var tx = bounds[0] + var ty = bounds[1] + + var v_position = pool.mallocFloat64(2 * numVertices) + var v_posHi = pool.mallocFloat32(2 * numVertices) + var v_posLo = pool.mallocFloat32(2 * numVertices) + var v_offset = pool.mallocFloat32(2 * numVertices) + var v_color = pool.mallocUint8(4 * numVertices) + var v_ids = pool.mallocUint32(numVertices) + var ptr = 0 + + for(i = 0; i < glyphs.length; ++i) { + glyph = glyphMeshes[i] + border = glyphBoundaries[i] + var x = sx * (positions[2 * i] - tx) + var y = sy * (positions[2 * i + 1] - ty) + var s = sizes[i] + var r = colors[4 * i] * 255 + var g = colors[4 * i + 1] * 255 + var b = colors[4 * i + 2] * 255 + var a = colors[4 * i + 3] * 255 + + var gx = 0.5 * (border.bounds[0] + border.bounds[2]) + var gy = 0.5 * (border.bounds[1] + border.bounds[3]) + + for(j = 0; j < glyph.data.length; j += 2) { + v_position[2 * ptr] = x + v_position[2 * ptr + 1] = y + v_offset[2 * ptr] = -s * (glyph.data[j] - gx) + v_offset[2 * ptr + 1] = -s * (glyph.data[j + 1] - gy) + v_color[4 * ptr] = r + v_color[4 * ptr + 1] = g + v_color[4 * ptr + 2] = b + v_color[4 * ptr + 3] = a + v_ids[ptr] = i + + ptr += 1 + } + + var w = borderWidths[i] + r = borderColors[4 * i] * 255 + g = borderColors[4 * i + 1] * 255 + b = borderColors[4 * i + 2] * 255 + a = borderColors[4 * i + 3] * 255 + + for(j = 0; j < border.coords.length; j += 2) { + v_position[2 * ptr] = x + v_position[2 * ptr + 1] = y + v_offset[2 * ptr] = - (s * (border.coords[j] - gx) + w * border.normals[j]) + v_offset[2 * ptr + 1] = - (s * (border.coords[j + 1] - gy) + w * border.normals[j + 1]) + v_color[4 * ptr] = r + v_color[4 * ptr + 1] = g + v_color[4 * ptr + 2] = b + v_color[4 * ptr + 3] = a + v_ids[ptr] = i + + ptr += 1 + } + } + + this.numPoints = glyphs.length + this.numVertices = numVertices + + v_posHi.set(v_position) + for(i = 0; i < v_position.length; i++) + v_posLo[i] = v_position[i] - v_posHi[i] + + this.posHiBuffer.update(v_posHi) + this.posLoBuffer.update(v_posLo) + this.offsetBuffer.update(v_offset) + this.colorBuffer.update(v_color) + this.idBuffer.update(v_ids) + + pool.free(v_position) + pool.free(v_posHi) + pool.free(v_posLo) + pool.free(v_offset) + pool.free(v_color) + pool.free(v_ids) +} + +proto.dispose = function() { + this.shader.dispose() + this.pickShader.dispose() + this.posHiBuffer.dispose() + this.posLoBuffer.dispose() + this.offsetBuffer.dispose() + this.colorBuffer.dispose() + this.idBuffer.dispose() + this.plot.removeObject(this) +} + +function createFancyScatter2D(plot, options) { + var gl = plot.gl + + var shader = createShader(gl, shaders.vertex, shaders.fragment) + var pickShader = createShader(gl, shaders.pickVertex, shaders.pickFragment) + + var positionHiBuffer = createBuffer(gl) + var positionLoBuffer = createBuffer(gl) + var offsetBuffer = createBuffer(gl) + var colorBuffer = createBuffer(gl) + var idBuffer = createBuffer(gl) + + var scatter = new GLScatterFancy( + plot, + shader, + pickShader, + positionHiBuffer, + positionLoBuffer, + offsetBuffer, + colorBuffer, + idBuffer) + + scatter.update(options) + + plot.addObject(scatter) + + return scatter +} +},{"./lib/shaders":207,"gl-buffer":132,"gl-shader":208,"text-cache":983,"typedarray-pool":992,"vectorize-text":1006}],216:[function(require,module,exports){ + + +exports.pointVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi, positionLo;\nattribute float weight;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo;\nuniform float pointSize, useWeight;\n\nvarying float fragWeight;\n\n\nvec4 pfx_1_0(vec2 scaleHi, vec2 scaleLo, vec2 translateHi, vec2 translateLo, vec2 positionHi, vec2 positionLo) {\n return vec4((positionHi + translateHi) * scaleHi\n + (positionLo + translateLo) * scaleHi\n + (positionHi + translateHi) * scaleLo\n + (positionLo + translateLo) * scaleLo, 0.0, 1.0);\n}\n\nvoid main() {\n gl_Position = pfx_1_0(scaleHi, scaleLo, translateHi, translateLo, positionHi, positionLo);\n gl_PointSize = pointSize;\n fragWeight = mix(1.0, weight, useWeight);\n}" +exports.pointFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color, borderColor;\nuniform float centerFraction;\n\nvarying float fragWeight;\n\nfloat smoothStep(float x, float y) {\n return 1.0 / (1.0 + exp(50.0*(x - y)));\n}\n\nvoid main() {\n float radius = length(2.0*gl_PointCoord.xy-1.0);\n if(radius > 1.0) {\n discard;\n }\n vec4 baseColor = mix(borderColor, color, smoothStep(radius, centerFraction));\n float alpha = 1.0 - pow(1.0 - baseColor.a, fragWeight);\n gl_FragColor = vec4(baseColor.rgb * alpha, alpha);\n}" +exports.pickVertex = "precision highp float;\n#define GLSLIFY 1\n\nvec4 pfx_1_0(vec2 scaleHi, vec2 scaleLo, vec2 translateHi, vec2 translateLo, vec2 positionHi, vec2 positionLo) {\n return vec4((positionHi + translateHi) * scaleHi\n + (positionLo + translateLo) * scaleHi\n + (positionHi + translateHi) * scaleLo\n + (positionLo + translateLo) * scaleLo, 0.0, 1.0);\n}\n\nattribute vec2 positionHi, positionLo;\nattribute vec4 pickId;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo;\nuniform float pointSize;\nuniform vec4 pickOffset;\n\nvarying vec4 fragId;\n\nvoid main() {\n\n vec4 id = pickId + pickOffset;\n id.y += floor(id.x / 256.0);\n id.x -= floor(id.x / 256.0) * 256.0;\n\n id.z += floor(id.y / 256.0);\n id.y -= floor(id.y / 256.0) * 256.0;\n\n id.w += floor(id.z / 256.0);\n id.z -= floor(id.z / 256.0) * 256.0;\n\n gl_Position = pfx_1_0(scaleHi, scaleLo, translateHi, translateLo, positionHi, positionLo);\n gl_PointSize = pointSize;\n fragId = id;\n}" +exports.pickFragment = "precision mediump float;\n#define GLSLIFY 1\n\nvarying vec4 fragId;\n\nvoid main() {\n float radius = length(2.0 * gl_PointCoord.xy - 1.0);\n if(radius > 1.0) {\n discard;\n }\n gl_FragColor = fragId / 255.0;\n}" +},{}],217:[function(require,module,exports){ +arguments[4][54][0].apply(exports,arguments) +},{"dup":54}],218:[function(require,module,exports){ +'use strict' + +module.exports = sortLevels + +var INSERT_SORT_CUTOFF = 32 + +function sortLevels(data_levels, data_points, data_ids, data_weights, n0) { + if (n0 <= 4*INSERT_SORT_CUTOFF) { + insertionSort(0, n0 - 1, data_levels, data_points, data_ids, data_weights) + } else { + quickSort(0, n0 - 1, data_levels, data_points, data_ids, data_weights) + } +} + +function insertionSort(left, right, data_levels, data_points, data_ids, data_weights) { + for(var i=left+1; i<=right; ++i) { + var a_level = data_levels[i] + var a_x = data_points[2*i] + var a_y = data_points[2*i+1] + var a_id = data_ids[i] + var a_weight = data_weights[i] + + var j = i + while(j > left) { + var b_level = data_levels[j-1] + var b_x = data_points[2*(j-1)] + if(((b_level - a_level) || (a_x - b_x)) >= 0) { + break + } + data_levels[j] = b_level + data_points[2*j] = b_x + data_points[2*j+1] = data_points[2*j-1] + data_ids[j] = data_ids[j-1] + data_weights[j] = data_weights[j-1] + j -= 1 + } + + data_levels[j] = a_level + data_points[2*j] = a_x + data_points[2*j+1] = a_y + data_ids[j] = a_id + data_weights[j] = a_weight + } +} + +function swap(i, j, data_levels, data_points, data_ids, data_weights) { + var a_level = data_levels[i] + var a_x = data_points[2*i] + var a_y = data_points[2*i+1] + var a_id = data_ids[i] + var a_weight = data_weights[i] + + data_levels[i] = data_levels[j] + data_points[2*i] = data_points[2*j] + data_points[2*i+1] = data_points[2*j+1] + data_ids[i] = data_ids[j] + data_weights[i] = data_weights[j] + + data_levels[j] = a_level + data_points[2*j] = a_x + data_points[2*j+1] = a_y + data_ids[j] = a_id + data_weights[j] = a_weight +} + +function move(i, j, data_levels, data_points, data_ids, data_weights) { + data_levels[i] = data_levels[j] + data_points[2*i] = data_points[2*j] + data_points[2*i+1] = data_points[2*j+1] + data_ids[i] = data_ids[j] + data_weights[i] = data_weights[j] +} + +function rotate(i, j, k, data_levels, data_points, data_ids, data_weights) { + var a_level = data_levels[i] + var a_x = data_points[2*i] + var a_y = data_points[2*i+1] + var a_id = data_ids[i] + var a_weight = data_weights[i] + + data_levels[i] = data_levels[j] + data_points[2*i] = data_points[2*j] + data_points[2*i+1] = data_points[2*j+1] + data_ids[i] = data_ids[j] + data_weights[i] = data_weights[j] + + data_levels[j] = data_levels[k] + data_points[2*j] = data_points[2*k] + data_points[2*j+1] = data_points[2*k+1] + data_ids[j] = data_ids[k] + data_weights[j] = data_weights[k] + + data_levels[k] = a_level + data_points[2*k] = a_x + data_points[2*k+1] = a_y + data_ids[k] = a_id + data_weights[k] = a_weight +} + +function shufflePivot( + i, j, + a_level, a_x, a_y, a_id, a_weight, + data_levels, data_points, data_ids, data_weights) { + + data_levels[i] = data_levels[j] + data_points[2*i] = data_points[2*j] + data_points[2*i+1] = data_points[2*j+1] + data_ids[i] = data_ids[j] + data_weights[i] = data_weights[j] + + data_levels[j] = a_level + data_points[2*j] = a_x + data_points[2*j+1] = a_y + data_ids[j] = a_id + data_weights[j] = a_weight +} + +function compare(i, j, data_levels, data_points, data_ids) { + return ((data_levels[i] - data_levels[j]) || + (data_points[2*j] - data_points[2*i]) || + (data_ids[i] - data_ids[j])) < 0 +} + +function comparePivot(i, level, x, y, id, data_levels, data_points, data_ids) { + return ((level - data_levels[i]) || + (data_points[2*i] - x) || + (id - data_ids[i])) < 0 +} + +function quickSort(left, right, data_levels, data_points, data_ids, data_weights) { + var sixth = (right - left + 1) / 6 | 0, + index1 = left + sixth, + index5 = right - sixth, + index3 = left + right >> 1, + index2 = index3 - sixth, + index4 = index3 + sixth, + el1 = index1, + el2 = index2, + el3 = index3, + el4 = index4, + el5 = index5, + less = left + 1, + great = right - 1, + tmp = 0 + if(compare(el1, el2, data_levels, data_points, data_ids, data_weights)) { + tmp = el1 + el1 = el2 + el2 = tmp + } + if(compare(el4, el5, data_levels, data_points, data_ids, data_weights)) { + tmp = el4 + el4 = el5 + el5 = tmp + } + if(compare(el1, el3, data_levels, data_points, data_ids, data_weights)) { + tmp = el1 + el1 = el3 + el3 = tmp + } + if(compare(el2, el3, data_levels, data_points, data_ids, data_weights)) { + tmp = el2 + el2 = el3 + el3 = tmp + } + if(compare(el1, el4, data_levels, data_points, data_ids, data_weights)) { + tmp = el1 + el1 = el4 + el4 = tmp + } + if(compare(el3, el4, data_levels, data_points, data_ids, data_weights)) { + tmp = el3 + el3 = el4 + el4 = tmp + } + if(compare(el2, el5, data_levels, data_points, data_ids, data_weights)) { + tmp = el2 + el2 = el5 + el5 = tmp + } + if(compare(el2, el3, data_levels, data_points, data_ids, data_weights)) { + tmp = el2 + el2 = el3 + el3 = tmp + } + if(compare(el4, el5, data_levels, data_points, data_ids, data_weights)) { + tmp = el4 + el4 = el5 + el5 = tmp + } + + var pivot1_level = data_levels[el2] + var pivot1_x = data_points[2*el2] + var pivot1_y = data_points[2*el2+1] + var pivot1_id = data_ids[el2] + var pivot1_weight = data_weights[el2] + + var pivot2_level = data_levels[el4] + var pivot2_x = data_points[2*el4] + var pivot2_y = data_points[2*el4+1] + var pivot2_id = data_ids[el4] + var pivot2_weight = data_weights[el4] + + var ptr0 = el1 + var ptr2 = el3 + var ptr4 = el5 + var ptr5 = index1 + var ptr6 = index3 + var ptr7 = index5 + + var level_x = data_levels[ptr0] + var level_y = data_levels[ptr2] + var level_z = data_levels[ptr4] + data_levels[ptr5] = level_x + data_levels[ptr6] = level_y + data_levels[ptr7] = level_z + + for (var i1 = 0; i1 < 2; ++i1) { + var x = data_points[2*ptr0+i1] + var y = data_points[2*ptr2+i1] + var z = data_points[2*ptr4+i1] + data_points[2*ptr5+i1] = x + data_points[2*ptr6+i1] = y + data_points[2*ptr7+i1] = z + } + + var id_x = data_ids[ptr0] + var id_y = data_ids[ptr2] + var id_z = data_ids[ptr4] + data_ids[ptr5] = id_x + data_ids[ptr6] = id_y + data_ids[ptr7] = id_z + + var weight_x = data_weights[ptr0] + var weight_y = data_weights[ptr2] + var weight_z = data_weights[ptr4] + data_weights[ptr5] = weight_x + data_weights[ptr6] = weight_y + data_weights[ptr7] = weight_z + + move(index2, left, data_levels, data_points, data_ids, data_weights) + move(index4, right, data_levels, data_points, data_ids, data_weights) + for (var k = less; k <= great; ++k) { + if (comparePivot(k, + pivot1_level, pivot1_x, pivot1_y, pivot1_id, + data_levels, data_points, data_ids)) { + if (k !== less) { + swap(k, less, data_levels, data_points, data_ids, data_weights) + } + ++less; + } else { + if (!comparePivot(k, + pivot2_level, pivot2_x, pivot2_y, pivot2_id, + data_levels, data_points, data_ids)) { + while (true) { + if (!comparePivot(great, + pivot2_level, pivot2_x, pivot2_y, pivot2_id, + data_levels, data_points, data_ids)) { + if (--great < k) { + break; + } + continue; + } else { + if (comparePivot(great, + pivot1_level, pivot1_x, pivot1_y, pivot1_id, + data_levels, data_points, data_ids)) { + rotate(k, less, great, data_levels, data_points, data_ids, data_weights) + ++less; + --great; + } else { + swap(k, great, data_levels, data_points, data_ids, data_weights) + --great; + } + break; + } + } + } + } + } + shufflePivot(left, less-1, pivot1_level, pivot1_x, pivot1_y, pivot1_id, pivot1_weight, data_levels, data_points, data_ids, data_weights) + shufflePivot(right, great+1, pivot2_level, pivot2_x, pivot2_y, pivot2_id, pivot2_weight, data_levels, data_points, data_ids, data_weights) + if (less - 2 - left <= INSERT_SORT_CUTOFF) { + insertionSort(left, less - 2, data_levels, data_points, data_ids, data_weights) + } else { + quickSort(left, less - 2, data_levels, data_points, data_ids, data_weights) + } + if (right - (great + 2) <= INSERT_SORT_CUTOFF) { + insertionSort(great + 2, right, data_levels, data_points, data_ids, data_weights) + } else { + quickSort(great + 2, right, data_levels, data_points, data_ids, data_weights) + } + if (great - less <= INSERT_SORT_CUTOFF) { + insertionSort(less, great, data_levels, data_points, data_ids, data_weights) + } else { + quickSort(less, great, data_levels, data_points, data_ids, data_weights) + } +} + +},{}],219:[function(require,module,exports){ +'use strict' + +var pool = require('typedarray-pool') + +var sortLevels = require('./lib/sort') + +module.exports = snapPoints + +function partition(points, ids, start, end, lox, loy, hix, hiy) { + var mid = start + for(var i=start; i>> 1 + if(n < 1) { + return [] + } + + var lox = Infinity, loy = Infinity + var hix = -Infinity, hiy = -Infinity + for(var i=0; i= Math.max(0.9 * count, 32)) { + var mid = (end + start)>>>1 + snapRec(nx, ny, diam_2, offset, mid, level+1) + offset = mid + } + snapRec(nx, ny, diam_2, offset, nextOffset, level+1) + offset = nextOffset + } + } + } + snapRec(lox, loy, diam, 0, n, 0) + sortLevels(levels, points, ids, weights, n) + + var lod = [] + var lastLevel = 0 + var prevOffset = n + for(var ptr=n-1; ptr>=0; --ptr) { + points[2*ptr] = (points[2*ptr] - lox) * scaleX + points[2*ptr+1] = (points[2*ptr+1] - loy) * scaleY + + var level = levels[ptr] + if(level === lastLevel) { + continue + } + + lod.push(new SnapInterval( + diam * Math.pow(0.5, level), + ptr+1, + prevOffset - (ptr+1) + )) + prevOffset = ptr+1 + + lastLevel = level + } + + lod.push(new SnapInterval(diam * Math.pow(0.5, level+1), 0, prevOffset)) + pool.free(levels) + + return lod +} + +},{"./lib/sort":218,"typedarray-pool":992}],220:[function(require,module,exports){ +'use strict' + +var createShader = require('gl-shader') +var createBuffer = require('gl-buffer') +var search = require('binary-search-bounds') +var snapPoints = require('snap-points-2d') +var pool = require('typedarray-pool') +var SHADERS = require('./lib/shader') + +module.exports = createScatter2D + +function Scatter2D(plot, positionBufferHi, positionBufferLo, pickBuffer, weightBuffer, shader, pickShader) { + this.plot = plot + this.positionBufferHi = positionBufferHi + this.positionBufferLo = positionBufferLo + this.pickBuffer = pickBuffer + this.weightBuffer = weightBuffer + this.shader = shader + this.pickShader = pickShader + this.scales = [] + this.size = 12.0 + this.borderSize = 1.0 + this.pointCount = 0 + this.color = [1, 0, 0, 1] + this.borderColor = [0, 0, 0, 1] + this.bounds = [Infinity, Infinity, -Infinity, -Infinity] + this.pickOffset = 0 + this.points = null + this.xCoords = null +} + +var proto = Scatter2D.prototype +var scaleHi = new Float32Array(2) +var scaleLo = new Float32Array(2) +var translateHi = new Float32Array(2) +var translateLo = new Float32Array(2) +var PICK_VEC4 = [0, 0, 0, 0] + +proto.dispose = function() { + this.shader.dispose() + this.pickShader.dispose() + this.positionBufferHi.dispose() + this.positionBufferLo.dispose() + this.pickBuffer.dispose() + if(this.xCoords) pool.free(this.xCoords) + this.plot.removeObject(this) +} + +proto.update = function(options) { + options = options || {} + + function dflt(opt, value) { + return opt in options ? options[opt] : value + } + + this.size = dflt('size', 12) + this.color = dflt('color', [1, 0, 0, 1]).slice() + this.borderSize = dflt('borderSize', 1) + this.borderColor = dflt('borderColor', [0, 0, 0, 1]).slice() + + if(this.xCoords) pool.free(this.xCoords) + + this.points = options.positions + var pointCount = this.points.length >>> 1 + var packedId = pool.mallocInt32(pointCount) + var packedW = pool.mallocFloat32(2 * pointCount) + var packed = pool.mallocFloat64(2 * pointCount) + packed.set(this.points) + this.scales = snapPoints(packed, packedId, packedW, this.bounds) + + var xCoords = pool.mallocFloat64(pointCount) + var packedHi = pool.mallocFloat32(2 * pointCount) + var packedLo = pool.mallocFloat32(2 * pointCount) + packedHi.set(packed) + for(var i = 0, j = 0; i < pointCount; i++, j += 2) { + packedLo[j] = packed[j] - packedHi[j] + packedLo[j + 1] = packed[j + 1] - packedHi[j + 1] + xCoords[i] = packed[j] + } + this.positionBufferHi.update(packedHi) + this.positionBufferLo.update(packedLo) + this.pickBuffer.update(packedId) + this.weightBuffer.update(packedW) + + pool.free(packedId) + pool.free(packed) + pool.free(packedHi) + pool.free(packedLo) + pool.free(packedW) + + this.xCoords = xCoords + this.pointCount = pointCount + this.pickOffset = 0 +} + +proto.draw = function(pickOffset) { + + var pick = pickOffset !== void(0) + + var plot = this.plot + var shader = pick ? this.pickShader : this.shader + var scales = this.scales + var positionBufferHi = this.positionBufferHi + var positionBufferLo = this.positionBufferLo + var pickBuffer = this.pickBuffer + var bounds = this.bounds + var size = this.size + var borderSize = this.borderSize + var gl = plot.gl + var pixelRatio = pick ? plot.pickPixelRatio : plot.pixelRatio + var viewBox = plot.viewBox + var dataBox = plot.dataBox + + if(this.pointCount === 0) + return pickOffset + + var boundX = bounds[2] - bounds[0] + var boundY = bounds[3] - bounds[1] + var dataX = dataBox[2] - dataBox[0] + var dataY = dataBox[3] - dataBox[1] + var screenX = (viewBox[2] - viewBox[0]) * pixelRatio / plot.pixelRatio + var screenY = (viewBox[3] - viewBox[1]) * pixelRatio / plot.pixelRatio + + var pixelSize = Math.min(dataX / screenX, dataY / screenY) + + var scaleX = 2 * boundX / dataX + var scaleY = 2 * boundY / dataY + + scaleHi[0] = scaleX + scaleHi[1] = scaleY + + scaleLo[0] = scaleX - scaleHi[0] + scaleLo[1] = scaleY - scaleHi[1] + + var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX + var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY + + translateHi[0] = translateX + translateHi[1] = translateY + + translateLo[0] = translateX - translateHi[0] + translateLo[1] = translateY - translateHi[1] + + shader.bind() + shader.uniforms.scaleHi = scaleHi + shader.uniforms.scaleLo = scaleLo + shader.uniforms.translateHi = translateHi + shader.uniforms.translateLo = translateLo + shader.uniforms.color = this.color + shader.uniforms.borderColor = this.borderColor + shader.uniforms.pointSize = pixelRatio * (size + borderSize) + shader.uniforms.centerFraction = this.borderSize === 0 ? 2 : size / (size + borderSize + 1.25) + + positionBufferHi.bind() + shader.attributes.positionHi.pointer() + + positionBufferLo.bind() + shader.attributes.positionLo.pointer() + + if(pick) { + + this.pickOffset = pickOffset + PICK_VEC4[0] = ( pickOffset & 0xff) + PICK_VEC4[1] = ((pickOffset >> 8) & 0xff) + PICK_VEC4[2] = ((pickOffset >> 16) & 0xff) + PICK_VEC4[3] = ((pickOffset >> 24) & 0xff) + shader.uniforms.pickOffset = PICK_VEC4 + + pickBuffer.bind() + shader.attributes.pickId.pointer(gl.UNSIGNED_BYTE) + + } else { + + shader.uniforms.useWeight = 1 + this.weightBuffer.bind() + shader.attributes.weight.pointer() + + } + + var xCoords = this.xCoords + var xStart = (dataBox[0] - bounds[0] - pixelSize * size * pixelRatio) / boundX + var xEnd = (dataBox[2] - bounds[0] + pixelSize * size * pixelRatio) / boundX + + var firstLevel = true + + for(var scaleNum = scales.length - 1; scaleNum >= 0; scaleNum--) { + var lod = scales[scaleNum] + if(lod.pixelSize < pixelSize && scaleNum > 1) + continue + + var intervalStart = lod.offset + var intervalEnd = lod.count + intervalStart + + var startOffset = search.ge(xCoords, xStart, intervalStart, intervalEnd - 1) + var endOffset = search.lt(xCoords, xEnd, startOffset, intervalEnd - 1) + 1 + + if(endOffset > startOffset) + gl.drawArrays(gl.POINTS, startOffset, endOffset - startOffset) + + if(!pick && firstLevel) { + firstLevel = false + shader.uniforms.useWeight = 0 + } + } + + return pickOffset + this.pointCount +} + +proto.drawPick = proto.draw + +proto.pick = function(x, y, value) { + var pointId = value - this.pickOffset + return pointId < 0 || pointId >= this.pointCount + ? null : { + object: this, + pointId: pointId, + dataCoord: [ this.points[2 * pointId], this.points[2 * pointId + 1] ] + } +} + +function createScatter2D(plot, options) { + var gl = plot.gl + var positionBufferHi = createBuffer(gl) + var positionBufferLo = createBuffer(gl) + var pickBuffer = createBuffer(gl) + var weightBuffer = createBuffer(gl) + var shader = createShader(gl, SHADERS.pointVertex, SHADERS.pointFragment) + var pickShader = createShader(gl, SHADERS.pickVertex, SHADERS.pickFragment) + + var result = new Scatter2D(plot, positionBufferHi, positionBufferLo, pickBuffer, weightBuffer, shader, pickShader) + result.update(options) + + plot.addObject(result) // register with plot + + return result +} +},{"./lib/shader":216,"binary-search-bounds":217,"gl-buffer":132,"gl-shader":227,"snap-points-2d":219,"typedarray-pool":992}],221:[function(require,module,exports){ +"use strict" + +var vectorizeText = require("vectorize-text") + +module.exports = getGlyph + +var GLYPH_CACHE = {} + +function getGlyph(symbol, font) { + var fontCache = GLYPH_CACHE[font] + if(!fontCache) { + fontCache = GLYPH_CACHE[font] = {} + } + if(symbol in fontCache) { + return fontCache[symbol] + } + + //Get line and triangle meshes for glyph + var lineSymbol = vectorizeText(symbol, { + textAlign: "center", + textBaseline: "middle", + lineHeight: 1.0, + font: font + }) + var triSymbol = vectorizeText(symbol, { + triangles: true, + textAlign: "center", + textBaseline: "middle", + lineHeight: 1.0, + font: font + }) + + //Calculate bounding box + var bounds = [[Infinity,Infinity], [-Infinity,-Infinity]] + for(var i=0; i= 1) { + return true + } + for(var i=0; i<3; ++i) { + if(this.axesProject[i] && this.projectOpacity[i] >= 1) { + return true + } + } + return false +} + +var VIEW_SHAPE = [0,0] +var U_VEC = [0,0,0] +var V_VEC = [0,0,0] +var MU_VEC = [0,0,0,1] +var MV_VEC = [0,0,0,1] +var SCRATCH_MATRIX = IDENTITY.slice() +var SCRATCH_VEC = [0,0,0] +var CLIP_BOUNDS = [[0,0,0], [0,0,0]] + +function zeroVec(a) { + a[0] = a[1] = a[2] = 0 + return a +} + +function augment(hg, af) { + hg[0] = af[0] + hg[1] = af[1] + hg[2] = af[2] + hg[3] = 1 + return hg +} + +function setComponent(out, v, i, x) { + out[0] = v[0] + out[1] = v[1] + out[2] = v[2] + out[i] = x + return out +} + +function getClipBounds(bounds) { + var result = CLIP_BOUNDS + for(var i=0; i<2; ++i) { + for(var j=0; j<3; ++j) { + result[i][j] = Math.max(Math.min(bounds[i][j], 1e8), -1e8) + } + } + return result +} + +function drawProject(shader, points, camera, transparent, forceDraw) { + var axesProject = points.axesProject + + var gl = points.gl + var uniforms = shader.uniforms + var model = camera.model || IDENTITY + var view = camera.view || IDENTITY + var projection = camera.projection || IDENTITY + var bounds = points.axesBounds + var clipBounds = getClipBounds(points.clipBounds) + + var cubeAxis + if(points.axes) { + cubeAxis = points.axes.lastCubeProps.axis + } else { + cubeAxis = [1,1,1] + } + + VIEW_SHAPE[0] = 2.0/gl.drawingBufferWidth + VIEW_SHAPE[1] = 2.0/gl.drawingBufferHeight + + shader.bind() + uniforms.view = view + uniforms.projection = projection + uniforms.screenSize = VIEW_SHAPE + uniforms.highlightId = points.highlightId + uniforms.highlightScale = points.highlightScale + uniforms.clipBounds = clipBounds + uniforms.pickGroup = points.pickId / 255.0 + uniforms.pixelRatio = points.pixelRatio + + for(var i=0; i<3; ++i) { + if(!axesProject[i]) { + continue + } + if((points.projectOpacity[i] < 1) !== transparent) { + continue + } + + uniforms.scale = points.projectScale[i] + uniforms.opacity = points.projectOpacity[i] + + //Project model matrix + var pmodel = SCRATCH_MATRIX + for(var j=0; j<16; ++j) { + pmodel[j] = 0 + } + for(var j=0; j<4; ++j) { + pmodel[5*j] = 1 + } + pmodel[5*i] = 0 + if(cubeAxis[i] < 0) { + pmodel[12+i] = bounds[0][i] + } else { + pmodel[12+i] = bounds[1][i] + } + mat4mult(pmodel, model, pmodel) + uniforms.model = pmodel + + //Compute initial axes + var u = (i+1)%3 + var v = (i+2)%3 + var du = zeroVec(U_VEC) + var dv = zeroVec(V_VEC) + du[u] = 1 + dv[v] = 1 + + //Align orientation relative to viewer + var mdu = project(projection, view, model, augment(MU_VEC, du)) + var mdv = project(projection, view, model, augment(MV_VEC, dv)) + if(Math.abs(mdu[1]) > Math.abs(mdv[1])) { + var tmp = mdu + mdu = mdv + mdv = tmp + tmp = du + du = dv + dv = tmp + var t = u + u = v + v = t + } + if(mdu[0] < 0) { + du[u] = -1 + } + if(mdv[1] > 0) { + dv[v] = -1 + } + var su = 0.0 + var sv = 0.0 + for(var j=0; j<4; ++j) { + su += Math.pow(model[4*u+j], 2) + sv += Math.pow(model[4*v+j], 2) + } + du[u] /= Math.sqrt(su) + dv[v] /= Math.sqrt(sv) + uniforms.axes[0] = du + uniforms.axes[1] = dv + + //Update fragment clip bounds + uniforms.fragClipBounds[0] = setComponent(SCRATCH_VEC, clipBounds[0], i, -1e8) + uniforms.fragClipBounds[1] = setComponent(SCRATCH_VEC, clipBounds[1], i, 1e8) + + //Draw interior + points.vao.draw(gl.TRIANGLES, points.vertexCount) + + //Draw edges + if(points.lineWidth > 0) { + gl.lineWidth(points.lineWidth) + points.vao.draw(gl.LINES, points.lineVertexCount, points.vertexCount) + } + } +} + + +var NEG_INFINITY3 = [-1e8, -1e8, -1e8] +var POS_INFINITY3 = [1e8, 1e8, 1e8] +var CLIP_GROUP = [NEG_INFINITY3, POS_INFINITY3] + +function drawFull(shader, pshader, points, camera, transparent, forceDraw) { + var gl = points.gl + + points.vao.bind() + + if(transparent === (points.opacity < 1) || forceDraw) { + shader.bind() + var uniforms = shader.uniforms + + uniforms.model = camera.model || IDENTITY + uniforms.view = camera.view || IDENTITY + uniforms.projection = camera.projection || IDENTITY + + VIEW_SHAPE[0] = 2.0/gl.drawingBufferWidth + VIEW_SHAPE[1] = 2.0/gl.drawingBufferHeight + uniforms.screenSize = VIEW_SHAPE + + uniforms.highlightId = points.highlightId + uniforms.highlightScale = points.highlightScale + + uniforms.fragClipBounds = CLIP_GROUP + uniforms.clipBounds = points.axes.bounds + + uniforms.opacity = points.opacity + uniforms.pickGroup = points.pickId / 255.0 + + uniforms.pixelRatio = points.pixelRatio + + //Draw interior + points.vao.draw(gl.TRIANGLES, points.vertexCount) + + //Draw edges + if(points.lineWidth > 0) { + gl.lineWidth(points.lineWidth) + points.vao.draw(gl.LINES, points.lineVertexCount, points.vertexCount) + } + } + + drawProject(pshader, points, camera, transparent, forceDraw) + + points.vao.unbind() +} + +proto.draw = function(camera) { + var shader = this.useOrtho ? this.orthoShader : this.shader + drawFull(shader, this.projectShader, this, camera, false, false) +} + +proto.drawTransparent = function(camera) { + var shader = this.useOrtho ? this.orthoShader : this.shader + drawFull(shader, this.projectShader, this, camera, true, false) +} + +proto.drawPick = function(camera) { + var shader = this.useOrtho ? this.pickOrthoShader : this.pickPerspectiveShader + drawFull(shader, this.pickProjectShader, this, camera, false, true) +} + +proto.pick = function(selected) { + if(!selected) { + return null + } + if(selected.id !== this.pickId) { + return null + } + var x = selected.value[2] + (selected.value[1]<<8) + (selected.value[0]<<16) + if(x >= this.pointCount || x < 0) { + return null + } + + //Unpack result + var coord = this.points[x] + var result = this._selectResult + result.index = x + for(var i=0; i<3; ++i) { + result.position[i] = result.dataCoordinate[i] = coord[i] + } + return result +} + +proto.highlight = function(selection) { + if(!selection) { + this.highlightId = [1,1,1,1] + } else { + var pointId = selection.index + var a0 = pointId &0xff + var a1 = (pointId>>8) &0xff + var a2 = (pointId>>16)&0xff + this.highlightId = [a0/255.0, a1/255.0, a2/255.0, 0] + } +} + +proto.update = function(options) { + + options = options || {} + + if('perspective' in options) { + this.useOrtho = !options.perspective + } + if('orthographic' in options) { + this.useOrtho = !!options.orthographic + } + if('lineWidth' in options) { + this.lineWidth = options.lineWidth + } + if('project' in options) { + if(Array.isArray(options.project)) { + this.axesProject = options.project + } else { + var v = !!options.project + this.axesProject = [v,v,v] + } + } + if('projectScale' in options) { + if(Array.isArray(options.projectScale)) { + this.projectScale = options.projectScale.slice() + } else { + var s = +options.projectScale + this.projectScale = [s,s,s] + } + } + if('projectOpacity' in options) { + if(Array.isArray(options.projectOpacity)) { + this.projectOpacity = options.projectOpacity.slice() + } else { + var s = +options.projectOpacity + this.projectOpacity = [s,s,s] + } + } + if('opacity' in options) { + this.opacity = options.opacity + } + + //Set dirty flag + this.dirty = true + + //Create new buffers + var points = options.position + if(!points) { + return + } + + //Text font + var font = options.font || 'normal' + var alignment = options.alignment || [0,0] + + //Bounds + var lowerBound = [ Infinity, Infinity, Infinity] + var upperBound = [-Infinity,-Infinity,-Infinity] + + //Unpack options + var glyphs = options.glyph + var colors = options.color + var sizes = options.size + var angles = options.angle + var lineColors = options.lineColor + + //Picking geometry + var pickCounter = 0 + + //First do pass to compute buffer sizes + var triVertexCount = 0 + var lineVertexCount = 0 + + //Count number of points and buffer size + var numPoints = points.length + +count_loop: + for(var i=0; i 0) { + textOffset[0] = -alignment[0] * (1+glyphBounds[0][0]) + } + + //Write out inner marker + var cells = glyphMesh.cells + var verts = glyphMesh.positions + + for(var j=0; j 0) { + + //Draw border + var w = lineWidth * pixelRatio + boxes.drawBox(loX-w, loY-w, hiX+w, loY+w, borderColor) + boxes.drawBox(loX-w, hiY-w, hiX+w, hiY+w, borderColor) + boxes.drawBox(loX-w, loY-w, loX+w, hiY+w, borderColor) + boxes.drawBox(hiX-w, loY-w, hiX+w, hiY+w, borderColor) + } +} + +proto.update = function(options) { + options = options || {} + + this.innerFill = !!options.innerFill + this.outerFill = !!options.outerFill + this.innerColor = (options.innerColor || [0,0,0,0.5]).slice() + this.outerColor = (options.outerColor || [0,0,0,0.5]).slice() + this.borderColor = (options.borderColor || [0,0,0,1]).slice() + this.borderWidth = options.borderWidth || 0 + this.selectBox = (options.selectBox || this.selectBox).slice() +} + +proto.dispose = function() { + this.boxBuffer.dispose() + this.boxShader.dispose() + this.plot.removeOverlay(this) +} + +function createSelectBox(plot, options) { + var gl = plot.gl + var buffer = createBuffer(gl, [ + 0, 0, + 0, 1, + 1, 0, + 1, 1 ]) + var shader = createShader(gl, SHADERS.boxVertex, SHADERS.boxFragment) + var selectBox = new SelectBox(plot, buffer, shader) + selectBox.update(options) + plot.addOverlay(selectBox) + return selectBox +} + +},{"./lib/shaders":224,"gl-buffer":132,"gl-shader":227}],226:[function(require,module,exports){ +'use strict' + +module.exports = createSelectBuffer + +var createFBO = require('gl-fbo') +var pool = require('typedarray-pool') +var ndarray = require('ndarray') + +var nextPow2 = require('bit-twiddle').nextPow2 + +var selectRange = require('cwise/lib/wrapper')({"args":["array",{"offset":[0,0,1],"array":0},{"offset":[0,0,2],"array":0},{"offset":[0,0,3],"array":0},"scalar","scalar","index"],"pre":{"body":"{this_closestD2=1e8,this_closestX=-1,this_closestY=-1}","args":[],"thisVars":["this_closestD2","this_closestX","this_closestY"],"localVars":[]},"body":{"body":"{if(_inline_16_arg0_<255||_inline_16_arg1_<255||_inline_16_arg2_<255||_inline_16_arg3_<255){var _inline_16_l=_inline_16_arg4_-_inline_16_arg6_[0],_inline_16_a=_inline_16_arg5_-_inline_16_arg6_[1],_inline_16_f=_inline_16_l*_inline_16_l+_inline_16_a*_inline_16_a;_inline_16_f this.buffer.length) { + pool.free(this.buffer) + var buffer = this.buffer = pool.mallocUint8(nextPow2(r*c*4)) + for(var i=0; i= 0) { + var size = attr.type.charAt(attr.type.length-1)|0 + var locVector = new Array(size) + for(var j=0; j= 0) { + curLocation += 1 + } + attributeLocations[i] = curLocation + } + } + + //Rebuild program and recompute all uniform locations + var uniformLocations = new Array(uniforms.length) + function relink() { + wrapper.program = shaderCache.program( + gl + , wrapper._vref + , wrapper._fref + , attributeNames + , attributeLocations) + + for(var i=0; i= 1) { + return true + } + for (var i = 0; i < 3; ++i) { + if (this._contourCounts[i].length > 0 || this._dynamicCounts[i] > 0) { + return true + } + } + return false +} + +proto.pickSlots = 1 + +proto.setPickBase = function (id) { + this.pickId = id +} + +var ZERO_VEC = [0, 0, 0] + +var PROJECT_DATA = { + showSurface: false, + showContour: false, + projections: [IDENTITY.slice(), IDENTITY.slice(), IDENTITY.slice()], + clipBounds: [ + [[0, 0, 0], [0, 0, 0]], + [[0, 0, 0], [0, 0, 0]], + [[0, 0, 0], [0, 0, 0]]] +} + +function computeProjectionData (camera, obj) { + var i, j, k + + // Compute cube properties + var cubeAxis = (obj.axes && obj.axes.lastCubeProps.axis) || ZERO_VEC + + var showSurface = obj.showSurface + var showContour = obj.showContour + + for (i = 0; i < 3; ++i) { + showSurface = showSurface || obj.surfaceProject[i] + for (j = 0; j < 3; ++j) { + showContour = showContour || obj.contourProject[i][j] + } + } + + for (i = 0; i < 3; ++i) { + // Construct projection onto axis + var axisSquish = PROJECT_DATA.projections[i] + for (j = 0; j < 16; ++j) { + axisSquish[j] = 0 + } + for (j = 0; j < 4; ++j) { + axisSquish[5 * j] = 1 + } + axisSquish[5 * i] = 0 + axisSquish[12 + i] = obj.axesBounds[+(cubeAxis[i] > 0)][i] + multiply(axisSquish, camera.model, axisSquish) + + var nclipBounds = PROJECT_DATA.clipBounds[i] + for (k = 0; k < 2; ++k) { + for (j = 0; j < 3; ++j) { + nclipBounds[k][j] = camera.clipBounds[k][j] + } + } + nclipBounds[0][i] = -1e8 + nclipBounds[1][i] = 1e8 + } + + PROJECT_DATA.showSurface = showSurface + PROJECT_DATA.showContour = showContour + + return PROJECT_DATA +} + +var UNIFORMS = { + model: IDENTITY, + view: IDENTITY, + projection: IDENTITY, + inverseModel: IDENTITY.slice(), + lowerBound: [0, 0, 0], + upperBound: [0, 0, 0], + colorMap: 0, + clipBounds: [[0, 0, 0], [0, 0, 0]], + height: 0.0, + contourTint: 0, + contourColor: [0, 0, 0, 1], + permutation: [1, 0, 0, 0, 1, 0, 0, 0, 1], + zOffset: -1e-4, + kambient: 1, + kdiffuse: 1, + kspecular: 1, + lightPosition: [1000, 1000, 1000], + eyePosition: [0, 0, 0], + roughness: 1, + fresnel: 1, + opacity: 1, + vertexColor: 0 +} + +var MATRIX_INVERSE = IDENTITY.slice() +var DEFAULT_PERM = [1, 0, 0, 0, 1, 0, 0, 0, 1] + +function drawCore (params, transparent) { + params = params || {} + var gl = this.gl + + gl.disable(gl.CULL_FACE) + + this._colorMap.bind(0) + + var uniforms = UNIFORMS + uniforms.model = params.model || IDENTITY + uniforms.view = params.view || IDENTITY + uniforms.projection = params.projection || IDENTITY + uniforms.lowerBound = [this.bounds[0][0], this.bounds[0][1], this.colorBounds[0] || this.bounds[0][2]] + uniforms.upperBound = [this.bounds[1][0], this.bounds[1][1], this.colorBounds[1] || this.bounds[1][2]] + uniforms.contourColor = this.contourColor[0] + + uniforms.inverseModel = invert(uniforms.inverseModel, uniforms.model) + + for (var i = 0; i < 2; ++i) { + var clipClamped = uniforms.clipBounds[i] + for (var j = 0; j < 3; ++j) { + clipClamped[j] = Math.min(Math.max(this.clipBounds[i][j], -1e8), 1e8) + } + } + + uniforms.kambient = this.ambientLight + uniforms.kdiffuse = this.diffuseLight + uniforms.kspecular = this.specularLight + + uniforms.roughness = this.roughness + uniforms.fresnel = this.fresnel + uniforms.opacity = this.opacity + + uniforms.height = 0.0 + uniforms.permutation = DEFAULT_PERM + + uniforms.vertexColor = this.vertexColor + + // Compute camera matrix inverse + var invCameraMatrix = MATRIX_INVERSE + multiply(invCameraMatrix, uniforms.view, uniforms.model) + multiply(invCameraMatrix, uniforms.projection, invCameraMatrix) + invert(invCameraMatrix, invCameraMatrix) + + for (i = 0; i < 3; ++i) { + uniforms.eyePosition[i] = invCameraMatrix[12 + i] / invCameraMatrix[15] + } + + var w = invCameraMatrix[15] + for (i = 0; i < 3; ++i) { + w += this.lightPosition[i] * invCameraMatrix[4 * i + 3] + } + for (i = 0; i < 3; ++i) { + var s = invCameraMatrix[12 + i] + for (j = 0; j < 3; ++j) { + s += invCameraMatrix[4 * j + i] * this.lightPosition[j] + } + uniforms.lightPosition[i] = s / w + } + + var projectData = computeProjectionData(uniforms, this) + + if (projectData.showSurface && (transparent === (this.opacity < 1))) { + // Set up uniforms + this._shader.bind() + this._shader.uniforms = uniforms + + // Draw it + this._vao.bind() + + if (this.showSurface && this._vertexCount) { + this._vao.draw(gl.TRIANGLES, this._vertexCount) + } + + // Draw projections of surface + for (i = 0; i < 3; ++i) { + if (!this.surfaceProject[i] || !this.vertexCount) { + continue + } + this._shader.uniforms.model = projectData.projections[i] + this._shader.uniforms.clipBounds = projectData.clipBounds[i] + this._vao.draw(gl.TRIANGLES, this._vertexCount) + } + + this._vao.unbind() + } + + if (projectData.showContour && !transparent) { + var shader = this._contourShader + + // Don't apply lighting to contours + uniforms.kambient = 1.0 + uniforms.kdiffuse = 0.0 + uniforms.kspecular = 0.0 + uniforms.opacity = 1.0 + + shader.bind() + shader.uniforms = uniforms + + // Draw contour lines + var vao = this._contourVAO + vao.bind() + + // Draw contour levels + for (i = 0; i < 3; ++i) { + shader.uniforms.permutation = PERMUTATIONS[i] + gl.lineWidth(this.contourWidth[i]) + + for (j = 0; j < this.contourLevels[i].length; ++j) { + if (!this._contourCounts[i][j]) { + continue + } + if (j === this.highlightLevel[i]) { + shader.uniforms.contourColor = this.highlightColor[i] + shader.uniforms.contourTint = this.highlightTint[i] + } else if (j === 0 || (j - 1) === this.highlightLevel[i]) { + shader.uniforms.contourColor = this.contourColor[i] + shader.uniforms.contourTint = this.contourTint[i] + } + shader.uniforms.height = this.contourLevels[i][j] + vao.draw(gl.LINES, this._contourCounts[i][j], this._contourOffsets[i][j]) + } + } + + // Draw projections of surface + for (i = 0; i < 3; ++i) { + shader.uniforms.model = projectData.projections[i] + shader.uniforms.clipBounds = projectData.clipBounds[i] + for (j = 0; j < 3; ++j) { + if (!this.contourProject[i][j]) { + continue + } + shader.uniforms.permutation = PERMUTATIONS[j] + gl.lineWidth(this.contourWidth[j]) + for (var k = 0; k < this.contourLevels[j].length; ++k) { + if (k === this.highlightLevel[j]) { + shader.uniforms.contourColor = this.highlightColor[j] + shader.uniforms.contourTint = this.highlightTint[j] + } else if (k === 0 || (k - 1) === this.highlightLevel[j]) { + shader.uniforms.contourColor = this.contourColor[j] + shader.uniforms.contourTint = this.contourTint[j] + } + shader.uniforms.height = this.contourLevels[j][k] + vao.draw(gl.LINES, this._contourCounts[j][k], this._contourOffsets[j][k]) + } + } + } + + // Draw dynamic contours + vao = this._dynamicVAO + vao.bind() + + // Draw contour levels + for (i = 0; i < 3; ++i) { + if (this._dynamicCounts[i] === 0) { + continue + } + + shader.uniforms.model = uniforms.model + shader.uniforms.clipBounds = uniforms.clipBounds + shader.uniforms.permutation = PERMUTATIONS[i] + gl.lineWidth(this.dynamicWidth[i]) + + shader.uniforms.contourColor = this.dynamicColor[i] + shader.uniforms.contourTint = this.dynamicTint[i] + shader.uniforms.height = this.dynamicLevel[i] + vao.draw(gl.LINES, this._dynamicCounts[i], this._dynamicOffsets[i]) + + for (j = 0; j < 3; ++j) { + if (!this.contourProject[j][i]) { + continue + } + + shader.uniforms.model = projectData.projections[j] + shader.uniforms.clipBounds = projectData.clipBounds[j] + vao.draw(gl.LINES, this._dynamicCounts[i], this._dynamicOffsets[i]) + } + } + + vao.unbind() + } +} + +proto.draw = function (params) { + return drawCore.call(this, params, false) +} + +proto.drawTransparent = function (params) { + return drawCore.call(this, params, true) +} + +var PICK_UNIFORMS = { + model: IDENTITY, + view: IDENTITY, + projection: IDENTITY, + inverseModel: IDENTITY, + clipBounds: [[0, 0, 0], [0, 0, 0]], + height: 0.0, + shape: [0, 0], + pickId: 0, + lowerBound: [0, 0, 0], + upperBound: [0, 0, 0], + zOffset: 0.0, + permutation: [1, 0, 0, 0, 1, 0, 0, 0, 1], + lightPosition: [0, 0, 0], + eyePosition: [0, 0, 0] +} + +proto.drawPick = function (params) { + params = params || {} + var gl = this.gl + gl.disable(gl.CULL_FACE) + + var uniforms = PICK_UNIFORMS + uniforms.model = params.model || IDENTITY + uniforms.view = params.view || IDENTITY + uniforms.projection = params.projection || IDENTITY + uniforms.shape = this._field[2].shape + uniforms.pickId = this.pickId / 255.0 + uniforms.lowerBound = this.bounds[0] + uniforms.upperBound = this.bounds[1] + uniforms.permutation = DEFAULT_PERM + + for (var i = 0; i < 2; ++i) { + var clipClamped = uniforms.clipBounds[i] + for (var j = 0; j < 3; ++j) { + clipClamped[j] = Math.min(Math.max(this.clipBounds[i][j], -1e8), 1e8) + } + } + + var projectData = computeProjectionData(uniforms, this) + + if (projectData.showSurface) { + // Set up uniforms + this._pickShader.bind() + this._pickShader.uniforms = uniforms + + // Draw it + this._vao.bind() + this._vao.draw(gl.TRIANGLES, this._vertexCount) + + // Draw projections of surface + for (i = 0; i < 3; ++i) { + if (!this.surfaceProject[i]) { + continue + } + this._pickShader.uniforms.model = projectData.projections[i] + this._pickShader.uniforms.clipBounds = projectData.clipBounds[i] + this._vao.draw(gl.TRIANGLES, this._vertexCount) + } + + this._vao.unbind() + } + + if (projectData.showContour) { + var shader = this._contourPickShader + + shader.bind() + shader.uniforms = uniforms + + var vao = this._contourVAO + vao.bind() + + for (j = 0; j < 3; ++j) { + gl.lineWidth(this.contourWidth[j]) + shader.uniforms.permutation = PERMUTATIONS[j] + for (i = 0; i < this.contourLevels[j].length; ++i) { + if (this._contourCounts[j][i]) { + shader.uniforms.height = this.contourLevels[j][i] + vao.draw(gl.LINES, this._contourCounts[j][i], this._contourOffsets[j][i]) + } + } + } + + // Draw projections of surface + for (i = 0; i < 3; ++i) { + shader.uniforms.model = projectData.projections[i] + shader.uniforms.clipBounds = projectData.clipBounds[i] + + for (j = 0; j < 3; ++j) { + if (!this.contourProject[i][j]) { + continue + } + + shader.uniforms.permutation = PERMUTATIONS[j] + gl.lineWidth(this.contourWidth[j]) + for (var k = 0; k < this.contourLevels[j].length; ++k) { + if (this._contourCounts[j][k]) { + shader.uniforms.height = this.contourLevels[j][k] + vao.draw(gl.LINES, this._contourCounts[j][k], this._contourOffsets[j][k]) + } + } + } + } + + vao.unbind() + } +} + +proto.pick = function (selection) { + if (!selection) { + return null + } + + if (selection.id !== this.pickId) { + return null + } + + var shape = this._field[2].shape + + var result = this._pickResult + + // Compute uv coordinate + var x = shape[0] * (selection.value[0] + (selection.value[2] >> 4) / 16.0) / 255.0 + var ix = Math.floor(x) + var fx = x - ix + + var y = shape[1] * (selection.value[1] + (selection.value[2] & 15) / 16.0) / 255.0 + var iy = Math.floor(y) + var fy = y - iy + + ix += 1 + iy += 1 + + // Compute xyz coordinate + var pos = result.position + pos[0] = pos[1] = pos[2] = 0 + for (var dx = 0; dx < 2; ++dx) { + var s = dx ? fx : 1.0 - fx + for (var dy = 0; dy < 2; ++dy) { + var t = dy ? fy : 1.0 - fy + + var r = ix + dx + var c = iy + dy + var w = s * t + + for (var i = 0; i < 3; ++i) { + pos[i] += this._field[i].get(r, c) * w + } + } + } + + // Find closest level + var levelIndex = this._pickResult.level + for (var j = 0; j < 3; ++j) { + levelIndex[j] = bsearch.le(this.contourLevels[j], pos[j]) + if (levelIndex[j] < 0) { + if (this.contourLevels[j].length > 0) { + levelIndex[j] = 0 + } + } else if (levelIndex[j] < this.contourLevels[j].length - 1) { + var a = this.contourLevels[j][levelIndex[j]] + var b = this.contourLevels[j][levelIndex[j] + 1] + if (Math.abs(a - pos[j]) > Math.abs(b - pos[j])) { + levelIndex[j] += 1 + } + } + } + + result.index[0] = fx < 0.5 ? ix : (ix + 1) + result.index[1] = fy < 0.5 ? iy : (iy + 1) + + result.uv[0] = x / shape[0] + result.uv[1] = y / shape[1] + + for (i = 0; i < 3; ++i) { + result.dataCoordinate[i] = this._field[i].get(result.index[0], result.index[1]) + } + + return result +} + +function padField (nfield, field) { + var shape = field.shape.slice() + var nshape = nfield.shape.slice() + + // Center + ops.assign(nfield.lo(1, 1).hi(shape[0], shape[1]), field) + + // Edges + ops.assign(nfield.lo(1).hi(shape[0], 1), + field.hi(shape[0], 1)) + ops.assign(nfield.lo(1, nshape[1] - 1).hi(shape[0], 1), + field.lo(0, shape[1] - 1).hi(shape[0], 1)) + ops.assign(nfield.lo(0, 1).hi(1, shape[1]), + field.hi(1)) + ops.assign(nfield.lo(nshape[0] - 1, 1).hi(1, shape[1]), + field.lo(shape[0] - 1)) + // Corners + nfield.set(0, 0, field.get(0, 0)) + nfield.set(0, nshape[1] - 1, field.get(0, shape[1] - 1)) + nfield.set(nshape[0] - 1, 0, field.get(shape[0] - 1, 0)) + nfield.set(nshape[0] - 1, nshape[1] - 1, field.get(shape[0] - 1, shape[1] - 1)) +} + +function handleArray (param, ctor) { + if (Array.isArray(param)) { + return [ ctor(param[0]), ctor(param[1]), ctor(param[2]) ] + } + return [ ctor(param), ctor(param), ctor(param) ] +} + +function toColor (x) { + if (Array.isArray(x)) { + if (x.length === 3) { + return [x[0], x[1], x[2], 1] + } + return [x[0], x[1], x[2], x[3]] + } + return [0, 0, 0, 1] +} + +function handleColor (param) { + if (Array.isArray(param)) { + if (Array.isArray(param)) { + return [ + toColor(param[0]), + toColor(param[1]), + toColor(param[2]) ] + } else { + var c = toColor(param) + return [ + c.slice(), + c.slice(), + c.slice() ] + } + } +} + +proto.update = function (params) { + params = params || {} + + this.dirty = true + + if ('contourWidth' in params) { + this.contourWidth = handleArray(params.contourWidth, Number) + } + if ('showContour' in params) { + this.showContour = handleArray(params.showContour, Boolean) + } + if ('showSurface' in params) { + this.showSurface = !!params.showSurface + } + if ('contourTint' in params) { + this.contourTint = handleArray(params.contourTint, Boolean) + } + if ('contourColor' in params) { + this.contourColor = handleColor(params.contourColor) + } + if ('contourProject' in params) { + this.contourProject = handleArray(params.contourProject, function (x) { + return handleArray(x, Boolean) + }) + } + if ('surfaceProject' in params) { + this.surfaceProject = params.surfaceProject + } + if ('dynamicColor' in params) { + this.dynamicColor = handleColor(params.dynamicColor) + } + if ('dynamicTint' in params) { + this.dynamicTint = handleArray(params.dynamicTint, Number) + } + if ('dynamicWidth' in params) { + this.dynamicWidth = handleArray(params.dynamicWidth, Number) + } + if ('opacity' in params) { + this.opacity = params.opacity + } + if ('colorBounds' in params) { + this.colorBounds = params.colorBounds + } + if ('vertexColor' in params) { + this.vertexColor = params.vertexColor ? 1 : 0; + } + + var field = params.field || (params.coords && params.coords[2]) || null + var levelsChanged = false + + if (!field) { + if (this._field[2].shape[0] || this._field[2].shape[2]) { + field = this._field[2].lo(1, 1).hi(this._field[2].shape[0] - 2, this._field[2].shape[1] - 2) + } else { + field = this._field[2].hi(0, 0) + } + } + + // Update field + if ('field' in params || 'coords' in params) { + var fsize = (field.shape[0] + 2) * (field.shape[1] + 2) + + // Resize if necessary + if (fsize > this._field[2].data.length) { + pool.freeFloat(this._field[2].data) + this._field[2].data = pool.mallocFloat(bits.nextPow2(fsize)) + } + + // Pad field + this._field[2] = ndarray(this._field[2].data, [field.shape[0] + 2, field.shape[1] + 2]) + padField(this._field[2], field) + + // Save shape of field + this.shape = field.shape.slice() + var shape = this.shape + + // Resize coordinate fields if necessary + for (var i = 0; i < 2; ++i) { + if (this._field[2].size > this._field[i].data.length) { + pool.freeFloat(this._field[i].data) + this._field[i].data = pool.mallocFloat(this._field[2].size) + } + this._field[i] = ndarray(this._field[i].data, [shape[0] + 2, shape[1] + 2]) + } + + // Generate x/y coordinates + if (params.coords) { + var coords = params.coords + if (!Array.isArray(coords) || coords.length !== 3) { + throw new Error('gl-surface: invalid coordinates for x/y') + } + for (i = 0; i < 2; ++i) { + var coord = coords[i] + for (j = 0; j < 2; ++j) { + if (coord.shape[j] !== shape[j]) { + throw new Error('gl-surface: coords have incorrect shape') + } + } + padField(this._field[i], coord) + } + } else if (params.ticks) { + var ticks = params.ticks + if (!Array.isArray(ticks) || ticks.length !== 2) { + throw new Error('gl-surface: invalid ticks') + } + for (i = 0; i < 2; ++i) { + var tick = ticks[i] + if (Array.isArray(tick) || tick.length) { + tick = ndarray(tick) + } + if (tick.shape[0] !== shape[i]) { + throw new Error('gl-surface: invalid tick length') + } + // Make a copy view of the tick array + var tick2 = ndarray(tick.data, shape) + tick2.stride[i] = tick.stride[0] + tick2.stride[i ^ 1] = 0 + + // Fill in field array + padField(this._field[i], tick2) + } + } else { + for (i = 0; i < 2; ++i) { + var offset = [0, 0] + offset[i] = 1 + this._field[i] = ndarray(this._field[i].data, [shape[0] + 2, shape[1] + 2], offset, 0) + } + this._field[0].set(0, 0, 0) + for (var j = 0; j < shape[0]; ++j) { + this._field[0].set(j + 1, 0, j) + } + this._field[0].set(shape[0] + 1, 0, shape[0] - 1) + this._field[1].set(0, 0, 0) + for (j = 0; j < shape[1]; ++j) { + this._field[1].set(0, j + 1, j) + } + this._field[1].set(0, shape[1] + 1, shape[1] - 1) + } + + // Save shape + var fields = this._field + + // Compute surface normals + var dfields = ndarray(pool.mallocFloat(fields[2].size * 3 * 2), [3, shape[0] + 2, shape[1] + 2, 2]) + for (i = 0; i < 3; ++i) { + gradient(dfields.pick(i), fields[i], 'mirror') + } + var normals = ndarray(pool.mallocFloat(fields[2].size * 3), [shape[0] + 2, shape[1] + 2, 3]) + for (i = 0; i < shape[0] + 2; ++i) { + for (j = 0; j < shape[1] + 2; ++j) { + var dxdu = dfields.get(0, i, j, 0) + var dxdv = dfields.get(0, i, j, 1) + var dydu = dfields.get(1, i, j, 0) + var dydv = dfields.get(1, i, j, 1) + var dzdu = dfields.get(2, i, j, 0) + var dzdv = dfields.get(2, i, j, 1) + + var nx = dydu * dzdv - dydv * dzdu + var ny = dzdu * dxdv - dzdv * dxdu + var nz = dxdu * dydv - dxdv * dydu + + var nl = Math.sqrt(nx * nx + ny * ny + nz * nz) + if (nl < 1e-8) { + nl = Math.max(Math.abs(nx), Math.abs(ny), Math.abs(nz)) + if (nl < 1e-8) { + nz = 1.0 + ny = nx = 0.0 + nl = 1.0 + } else { + nl = 1.0 / nl + } + } else { + nl = 1.0 / Math.sqrt(nl) + } + + normals.set(i, j, 0, nx * nl) + normals.set(i, j, 1, ny * nl) + normals.set(i, j, 2, nz * nl) + } + } + pool.free(dfields.data) + + // Initialize surface + var lo = [ Infinity, Infinity, Infinity ] + var hi = [ -Infinity, -Infinity, -Infinity ] + var lo_intensity = Infinity + var hi_intensity = -Infinity + var count = (shape[0] - 1) * (shape[1] - 1) * 6 + var tverts = pool.mallocFloat(bits.nextPow2(10 * count)) + var tptr = 0 + var vertexCount = 0 + for (i = 0; i < shape[0] - 1; ++i) { + j_loop: + for (j = 0; j < shape[1] - 1; ++j) { + // Test for NaNs + for (var dx = 0; dx < 2; ++dx) { + for (var dy = 0; dy < 2; ++dy) { + for (var k = 0; k < 3; ++k) { + var f = this._field[k].get(1 + i + dx, 1 + j + dy) + if (isNaN(f) || !isFinite(f)) { + continue j_loop + } + } + } + } + for (k = 0; k < 6; ++k) { + var r = i + QUAD[k][0] + var c = j + QUAD[k][1] + + var tx = this._field[0].get(r + 1, c + 1) + var ty = this._field[1].get(r + 1, c + 1) + f = this._field[2].get(r + 1, c + 1) + var vf = f + nx = normals.get(r + 1, c + 1, 0) + ny = normals.get(r + 1, c + 1, 1) + nz = normals.get(r + 1, c + 1, 2) + + if (params.intensity) { + vf = params.intensity.get(r, c) + } + + tverts[tptr++] = r + tverts[tptr++] = c + tverts[tptr++] = tx + tverts[tptr++] = ty + tverts[tptr++] = f + tverts[tptr++] = 0 + tverts[tptr++] = vf + tverts[tptr++] = nx + tverts[tptr++] = ny + tverts[tptr++] = nz + + lo[0] = Math.min(lo[0], tx) + lo[1] = Math.min(lo[1], ty) + lo[2] = Math.min(lo[2], f) + lo_intensity = Math.min(lo_intensity, vf) + + hi[0] = Math.max(hi[0], tx) + hi[1] = Math.max(hi[1], ty) + hi[2] = Math.max(hi[2], f) + hi_intensity = Math.max(hi_intensity, vf) + + vertexCount += 1 + } + } + } + + if (params.intensityBounds) { + lo_intensity = +params.intensityBounds[0] + hi_intensity = +params.intensityBounds[1] + } + + // Scale all vertex intensities + for (i = 6; i < tptr; i += 10) { + tverts[i] = (tverts[i] - lo_intensity) / (hi_intensity - lo_intensity) + } + + this._vertexCount = vertexCount + this._coordinateBuffer.update(tverts.subarray(0, tptr)) + pool.freeFloat(tverts) + pool.free(normals.data) + + // Update bounds + this.bounds = [lo, hi] + + // Save intensity + this.intensity = params.intensity || this._field[2] + + if(this.intensityBounds[0] !== lo_intensity || this.intensityBounds[1] !== hi_intensity) { + levelsChanged = true + } + + // Save intensity bound + this.intensityBounds = [lo_intensity, hi_intensity] + } + + // Update level crossings + if ('levels' in params) { + var levels = params.levels + if (!Array.isArray(levels[0])) { + levels = [ [], [], levels ] + } else { + levels = levels.slice() + } + for (i = 0; i < 3; ++i) { + levels[i] = levels[i].slice() + levels.sort(function (a, b) { + return a - b + }) + } + change_test: + for (i = 0; i < 3; ++i) { + if (levels[i].length !== this.contourLevels[i].length) { + levelsChanged = true + break + } + for (j = 0; j < levels[i].length; ++j) { + if (levels[i][j] !== this.contourLevels[i][j]) { + levelsChanged = true + break change_test + } + } + } + this.contourLevels = levels + } + + if (levelsChanged) { + fields = this._field + shape = this.shape + + // Update contour lines + var contourVerts = [] + + for (var dim = 0; dim < 3; ++dim) { + levels = this.contourLevels[dim] + var levelOffsets = [] + var levelCounts = [] + + var parts = [0, 0, 0] + + for (i = 0; i < levels.length; ++i) { + var graph = surfaceNets(this._field[dim], levels[i]) + levelOffsets.push((contourVerts.length / 5) | 0) + vertexCount = 0 + + edge_loop: + for (j = 0; j < graph.cells.length; ++j) { + var e = graph.cells[j] + for (k = 0; k < 2; ++k) { + var p = graph.positions[e[k]] + + var x = p[0] + var ix = Math.floor(x) | 0 + var fx = x - ix + + var y = p[1] + var iy = Math.floor(y) | 0 + var fy = y - iy + + var hole = false + dd_loop: + for (var dd = 0; dd < 3; ++dd) { + parts[dd] = 0.0 + var iu = (dim + dd + 1) % 3 + for (dx = 0; dx < 2; ++dx) { + var s = dx ? fx : 1.0 - fx + r = Math.min(Math.max(ix + dx, 0), shape[0]) | 0 + for (dy = 0; dy < 2; ++dy) { + var t = dy ? fy : 1.0 - fy + c = Math.min(Math.max(iy + dy, 0), shape[1]) | 0 + + if (dd < 2) { + f = this._field[iu].get(r, c) + } else { + f = (this.intensity.get(r, c) - this.intensityBounds[0]) / (this.intensityBounds[1] - this.intensityBounds[0]) + } + if (!isFinite(f) || isNaN(f)) { + hole = true + break dd_loop + } + + var w = s * t + parts[dd] += w * f + } + } + } + + if (!hole) { + contourVerts.push(parts[0], parts[1], p[0], p[1], parts[2]) + vertexCount += 1 + } else { + if (k > 0) { + // If we already added first edge, pop off verts + for (var l = 0; l < 5; ++l) { + contourVerts.pop() + } + vertexCount -= 1 + } + continue edge_loop + } + } + } + levelCounts.push(vertexCount) + } + + // Store results + this._contourOffsets[dim] = levelOffsets + this._contourCounts[dim] = levelCounts + } + + var floatBuffer = pool.mallocFloat(contourVerts.length) + for (i = 0; i < contourVerts.length; ++i) { + floatBuffer[i] = contourVerts[i] + } + this._contourBuffer.update(floatBuffer) + pool.freeFloat(floatBuffer) + } + + if (params.colormap) { + this._colorMap.setPixels(genColormap(params.colormap)) + } +} + +proto.dispose = function () { + this._shader.dispose() + this._vao.dispose() + this._coordinateBuffer.dispose() + this._colorMap.dispose() + this._contourBuffer.dispose() + this._contourVAO.dispose() + this._contourShader.dispose() + this._contourPickShader.dispose() + this._dynamicBuffer.dispose() + this._dynamicVAO.dispose() + for (var i = 0; i < 3; ++i) { + pool.freeFloat(this._field[i].data) + } +} + +proto.highlight = function (selection) { + if (!selection) { + this._dynamicCounts = [0, 0, 0] + this.dyanamicLevel = [NaN, NaN, NaN] + this.highlightLevel = [-1, -1, -1] + return + } + + for (var i = 0; i < 3; ++i) { + if (this.enableHighlight[i]) { + this.highlightLevel[i] = selection.level[i] + } else { + this.highlightLevel[i] = -1 + } + } + + var levels + if (this.snapToData) { + levels = selection.dataCoordinate + } else { + levels = selection.position + } + if ((!this.enableDynamic[0] || levels[0] === this.dynamicLevel[0]) && + (!this.enableDynamic[1] || levels[1] === this.dynamicLevel[1]) && + (!this.enableDynamic[2] || levels[2] === this.dynamicLevel[2])) { + return + } + + var vertexCount = 0 + var shape = this.shape + var scratchBuffer = pool.mallocFloat(12 * shape[0] * shape[1]) + + for (var d = 0; d < 3; ++d) { + if (!this.enableDynamic[d]) { + this.dynamicLevel[d] = NaN + this._dynamicCounts[d] = 0 + continue + } + + this.dynamicLevel[d] = levels[d] + + var u = (d + 1) % 3 + var v = (d + 2) % 3 + + var f = this._field[d] + var g = this._field[u] + var h = this._field[v] + var intensity = this.intensity + + var graph = surfaceNets(f, levels[d]) + var edges = graph.cells + var positions = graph.positions + + this._dynamicOffsets[d] = vertexCount + + for (i = 0; i < edges.length; ++i) { + var e = edges[i] + for (var j = 0; j < 2; ++j) { + var p = positions[e[j]] + + var x = +p[0] + var ix = x | 0 + var jx = Math.min(ix + 1, shape[0]) | 0 + var fx = x - ix + var hx = 1.0 - fx + + var y = +p[1] + var iy = y | 0 + var jy = Math.min(iy + 1, shape[1]) | 0 + var fy = y - iy + var hy = 1.0 - fy + + var w00 = hx * hy + var w01 = hx * fy + var w10 = fx * hy + var w11 = fx * fy + + var cu = w00 * g.get(ix, iy) + + w01 * g.get(ix, jy) + + w10 * g.get(jx, iy) + + w11 * g.get(jx, jy) + + var cv = w00 * h.get(ix, iy) + + w01 * h.get(ix, jy) + + w10 * h.get(jx, iy) + + w11 * h.get(jx, jy) + + if (isNaN(cu) || isNaN(cv)) { + if (j) { + vertexCount -= 1 + } + break + } + + scratchBuffer[2 * vertexCount + 0] = cu + scratchBuffer[2 * vertexCount + 1] = cv + + vertexCount += 1 + } + } + + this._dynamicCounts[d] = vertexCount - this._dynamicOffsets[d] + } + + this._dynamicBuffer.update(scratchBuffer.subarray(0, 2 * vertexCount)) + pool.freeFloat(scratchBuffer) +} + +function createSurfacePlot (params) { + var gl = params.gl + var shader = createShader(gl) + var pickShader = createPickShader(gl) + var contourShader = createContourShader(gl) + var contourPickShader = createPickContourShader(gl) + + var coordinateBuffer = createBuffer(gl) + var vao = createVAO(gl, [ + { buffer: coordinateBuffer, + size: 4, + stride: SURFACE_VERTEX_SIZE, + offset: 0 + }, + { buffer: coordinateBuffer, + size: 3, + stride: SURFACE_VERTEX_SIZE, + offset: 16 + }, + { + buffer: coordinateBuffer, + size: 3, + stride: SURFACE_VERTEX_SIZE, + offset: 28 + } + ]) + + var contourBuffer = createBuffer(gl) + var contourVAO = createVAO(gl, [ + { + buffer: contourBuffer, + size: 4, + stride: 20, + offset: 0 + }, + { + buffer: contourBuffer, + size: 1, + stride: 20, + offset: 16 + } + ]) + + var dynamicBuffer = createBuffer(gl) + var dynamicVAO = createVAO(gl, [ + { + buffer: dynamicBuffer, + size: 2, + type: gl.FLOAT + }]) + + var cmap = createTexture(gl, 1, N_COLORS, gl.RGBA, gl.UNSIGNED_BYTE) + cmap.minFilter = gl.LINEAR + cmap.magFilter = gl.LINEAR + + var surface = new SurfacePlot( + gl, + [0, 0], + [[0, 0, 0], [0, 0, 0]], + shader, + pickShader, + coordinateBuffer, + vao, + cmap, + contourShader, + contourPickShader, + contourBuffer, + contourVAO, + dynamicBuffer, + dynamicVAO + ) + + var nparams = { + levels: [[], [], []] + } + for (var id in params) { + nparams[id] = params[id] + } + nparams.colormap = nparams.colormap || 'jet' + + surface.update(nparams) + + return surface +} + +},{"./lib/shaders":237,"binary-search-bounds":32,"bit-twiddle":33,"colormap":62,"gl-buffer":132,"gl-mat4/invert":156,"gl-mat4/multiply":158,"gl-texture2d":239,"gl-vao":243,"ndarray":443,"ndarray-gradient":434,"ndarray-ops":437,"ndarray-pack":438,"surface-nets":982,"typedarray-pool":992}],239:[function(require,module,exports){ +'use strict' + +var ndarray = require('ndarray') +var ops = require('ndarray-ops') +var pool = require('typedarray-pool') + +module.exports = createTexture2D + +var linearTypes = null +var filterTypes = null +var wrapTypes = null + +function lazyInitLinearTypes(gl) { + linearTypes = [ + gl.LINEAR, + gl.NEAREST_MIPMAP_LINEAR, + gl.LINEAR_MIPMAP_NEAREST, + gl.LINEAR_MIPMAP_NEAREST + ] + filterTypes = [ + gl.NEAREST, + gl.LINEAR, + gl.NEAREST_MIPMAP_NEAREST, + gl.NEAREST_MIPMAP_LINEAR, + gl.LINEAR_MIPMAP_NEAREST, + gl.LINEAR_MIPMAP_LINEAR + ] + wrapTypes = [ + gl.REPEAT, + gl.CLAMP_TO_EDGE, + gl.MIRRORED_REPEAT + ] +} + +function acceptTextureDOM (obj) { + return ( + ('undefined' != typeof HTMLCanvasElement && obj instanceof HTMLCanvasElement) || + ('undefined' != typeof HTMLImageElement && obj instanceof HTMLImageElement) || + ('undefined' != typeof HTMLVideoElement && obj instanceof HTMLVideoElement) || + ('undefined' != typeof ImageData && obj instanceof ImageData)) +} + +var convertFloatToUint8 = function(out, inp) { + ops.muls(out, inp, 255.0) +} + +function reshapeTexture(tex, w, h) { + var gl = tex.gl + var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE) + if(w < 0 || w > maxSize || h < 0 || h > maxSize) { + throw new Error('gl-texture2d: Invalid texture size') + } + tex._shape = [w, h] + tex.bind() + gl.texImage2D(gl.TEXTURE_2D, 0, tex.format, w, h, 0, tex.format, tex.type, null) + tex._mipLevels = [0] + return tex +} + +function Texture2D(gl, handle, width, height, format, type) { + this.gl = gl + this.handle = handle + this.format = format + this.type = type + this._shape = [width, height] + this._mipLevels = [0] + this._magFilter = gl.NEAREST + this._minFilter = gl.NEAREST + this._wrapS = gl.CLAMP_TO_EDGE + this._wrapT = gl.CLAMP_TO_EDGE + this._anisoSamples = 1 + + var parent = this + var wrapVector = [this._wrapS, this._wrapT] + Object.defineProperties(wrapVector, [ + { + get: function() { + return parent._wrapS + }, + set: function(v) { + return parent.wrapS = v + } + }, + { + get: function() { + return parent._wrapT + }, + set: function(v) { + return parent.wrapT = v + } + } + ]) + this._wrapVector = wrapVector + + var shapeVector = [this._shape[0], this._shape[1]] + Object.defineProperties(shapeVector, [ + { + get: function() { + return parent._shape[0] + }, + set: function(v) { + return parent.width = v + } + }, + { + get: function() { + return parent._shape[1] + }, + set: function(v) { + return parent.height = v + } + } + ]) + this._shapeVector = shapeVector +} + +var proto = Texture2D.prototype + +Object.defineProperties(proto, { + minFilter: { + get: function() { + return this._minFilter + }, + set: function(v) { + this.bind() + var gl = this.gl + if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) { + if(!gl.getExtension('OES_texture_float_linear')) { + v = gl.NEAREST + } + } + if(filterTypes.indexOf(v) < 0) { + throw new Error('gl-texture2d: Unknown filter mode ' + v) + } + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, v) + return this._minFilter = v + } + }, + magFilter: { + get: function() { + return this._magFilter + }, + set: function(v) { + this.bind() + var gl = this.gl + if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) { + if(!gl.getExtension('OES_texture_float_linear')) { + v = gl.NEAREST + } + } + if(filterTypes.indexOf(v) < 0) { + throw new Error('gl-texture2d: Unknown filter mode ' + v) + } + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, v) + return this._magFilter = v + } + }, + mipSamples: { + get: function() { + return this._anisoSamples + }, + set: function(i) { + var psamples = this._anisoSamples + this._anisoSamples = Math.max(i, 1)|0 + if(psamples !== this._anisoSamples) { + var ext = this.gl.getExtension('EXT_texture_filter_anisotropic') + if(ext) { + this.gl.texParameterf(this.gl.TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT, this._anisoSamples) + } + } + return this._anisoSamples + } + }, + wrapS: { + get: function() { + return this._wrapS + }, + set: function(v) { + this.bind() + if(wrapTypes.indexOf(v) < 0) { + throw new Error('gl-texture2d: Unknown wrap mode ' + v) + } + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, v) + return this._wrapS = v + } + }, + wrapT: { + get: function() { + return this._wrapT + }, + set: function(v) { + this.bind() + if(wrapTypes.indexOf(v) < 0) { + throw new Error('gl-texture2d: Unknown wrap mode ' + v) + } + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, v) + return this._wrapT = v + } + }, + wrap: { + get: function() { + return this._wrapVector + }, + set: function(v) { + if(!Array.isArray(v)) { + v = [v,v] + } + if(v.length !== 2) { + throw new Error('gl-texture2d: Must specify wrap mode for rows and columns') + } + for(var i=0; i<2; ++i) { + if(wrapTypes.indexOf(v[i]) < 0) { + throw new Error('gl-texture2d: Unknown wrap mode ' + v) + } + } + this._wrapS = v[0] + this._wrapT = v[1] + + var gl = this.gl + this.bind() + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, this._wrapS) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, this._wrapT) + + return v + } + }, + shape: { + get: function() { + return this._shapeVector + }, + set: function(x) { + if(!Array.isArray(x)) { + x = [x|0,x|0] + } else { + if(x.length !== 2) { + throw new Error('gl-texture2d: Invalid texture shape') + } + } + reshapeTexture(this, x[0]|0, x[1]|0) + return [x[0]|0, x[1]|0] + } + }, + width: { + get: function() { + return this._shape[0] + }, + set: function(w) { + w = w|0 + reshapeTexture(this, w, this._shape[1]) + return w + } + }, + height: { + get: function() { + return this._shape[1] + }, + set: function(h) { + h = h|0 + reshapeTexture(this, this._shape[0], h) + return h + } + } +}) + +proto.bind = function(unit) { + var gl = this.gl + if(unit !== undefined) { + gl.activeTexture(gl.TEXTURE0 + (unit|0)) + } + gl.bindTexture(gl.TEXTURE_2D, this.handle) + if(unit !== undefined) { + return (unit|0) + } + return gl.getParameter(gl.ACTIVE_TEXTURE) - gl.TEXTURE0 +} + +proto.dispose = function() { + this.gl.deleteTexture(this.handle) +} + +proto.generateMipmap = function() { + this.bind() + this.gl.generateMipmap(this.gl.TEXTURE_2D) + + //Update mip levels + var l = Math.min(this._shape[0], this._shape[1]) + for(var i=0; l>0; ++i, l>>>=1) { + if(this._mipLevels.indexOf(i) < 0) { + this._mipLevels.push(i) + } + } +} + +proto.setPixels = function(data, x_off, y_off, mip_level) { + var gl = this.gl + this.bind() + if(Array.isArray(x_off)) { + mip_level = y_off + y_off = x_off[1]|0 + x_off = x_off[0]|0 + } else { + x_off = x_off || 0 + y_off = y_off || 0 + } + mip_level = mip_level || 0 + var directData = acceptTextureDOM(data) ? data : data.raw + if(directData) { + var needsMip = this._mipLevels.indexOf(mip_level) < 0 + if(needsMip) { + gl.texImage2D(gl.TEXTURE_2D, 0, this.format, this.format, this.type, directData) + this._mipLevels.push(mip_level) + } else { + gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, this.format, this.type, directData) + } + } else if(data.shape && data.stride && data.data) { + if(data.shape.length < 2 || + x_off + data.shape[1] > this._shape[1]>>>mip_level || + y_off + data.shape[0] > this._shape[0]>>>mip_level || + x_off < 0 || + y_off < 0) { + throw new Error('gl-texture2d: Texture dimensions are out of bounds') + } + texSubImageArray(gl, x_off, y_off, mip_level, this.format, this.type, this._mipLevels, data) + } else { + throw new Error('gl-texture2d: Unsupported data type') + } +} + + +function isPacked(shape, stride) { + if(shape.length === 3) { + return (stride[2] === 1) && + (stride[1] === shape[0]*shape[2]) && + (stride[0] === shape[2]) + } + return (stride[0] === 1) && + (stride[1] === shape[0]) +} + +function texSubImageArray(gl, x_off, y_off, mip_level, cformat, ctype, mipLevels, array) { + var dtype = array.dtype + var shape = array.shape.slice() + if(shape.length < 2 || shape.length > 3) { + throw new Error('gl-texture2d: Invalid ndarray, must be 2d or 3d') + } + var type = 0, format = 0 + var packed = isPacked(shape, array.stride.slice()) + if(dtype === 'float32') { + type = gl.FLOAT + } else if(dtype === 'float64') { + type = gl.FLOAT + packed = false + dtype = 'float32' + } else if(dtype === 'uint8') { + type = gl.UNSIGNED_BYTE + } else { + type = gl.UNSIGNED_BYTE + packed = false + dtype = 'uint8' + } + var channels = 1 + if(shape.length === 2) { + format = gl.LUMINANCE + shape = [shape[0], shape[1], 1] + array = ndarray(array.data, shape, [array.stride[0], array.stride[1], 1], array.offset) + } else if(shape.length === 3) { + if(shape[2] === 1) { + format = gl.ALPHA + } else if(shape[2] === 2) { + format = gl.LUMINANCE_ALPHA + } else if(shape[2] === 3) { + format = gl.RGB + } else if(shape[2] === 4) { + format = gl.RGBA + } else { + throw new Error('gl-texture2d: Invalid shape for pixel coords') + } + channels = shape[2] + } else { + throw new Error('gl-texture2d: Invalid shape for texture') + } + //For 1-channel textures allow conversion between formats + if((format === gl.LUMINANCE || format === gl.ALPHA) && + (cformat === gl.LUMINANCE || cformat === gl.ALPHA)) { + format = cformat + } + if(format !== cformat) { + throw new Error('gl-texture2d: Incompatible texture format for setPixels') + } + var size = array.size + var needsMip = mipLevels.indexOf(mip_level) < 0 + if(needsMip) { + mipLevels.push(mip_level) + } + if(type === ctype && packed) { + //Array data types are compatible, can directly copy into texture + if(array.offset === 0 && array.data.length === size) { + if(needsMip) { + gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, array.data) + } else { + gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, array.data) + } + } else { + if(needsMip) { + gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, array.data.subarray(array.offset, array.offset+size)) + } else { + gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, array.data.subarray(array.offset, array.offset+size)) + } + } + } else { + //Need to do type conversion to pack data into buffer + var pack_buffer + if(ctype === gl.FLOAT) { + pack_buffer = pool.mallocFloat32(size) + } else { + pack_buffer = pool.mallocUint8(size) + } + var pack_view = ndarray(pack_buffer, shape, [shape[2], shape[2]*shape[0], 1]) + if(type === gl.FLOAT && ctype === gl.UNSIGNED_BYTE) { + convertFloatToUint8(pack_view, array) + } else { + ops.assign(pack_view, array) + } + if(needsMip) { + gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, pack_buffer.subarray(0, size)) + } else { + gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, pack_buffer.subarray(0, size)) + } + if(ctype === gl.FLOAT) { + pool.freeFloat32(pack_buffer) + } else { + pool.freeUint8(pack_buffer) + } + } +} + +function initTexture(gl) { + var tex = gl.createTexture() + gl.bindTexture(gl.TEXTURE_2D, tex) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) + return tex +} + +function createTextureShape(gl, width, height, format, type) { + var maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE) + if(width < 0 || width > maxTextureSize || height < 0 || height > maxTextureSize) { + throw new Error('gl-texture2d: Invalid texture shape') + } + if(type === gl.FLOAT && !gl.getExtension('OES_texture_float')) { + throw new Error('gl-texture2d: Floating point textures not supported on this platform') + } + var tex = initTexture(gl) + gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, type, null) + return new Texture2D(gl, tex, width, height, format, type) +} + +function createTextureDOM(gl, directData, width, height, format, type) { + var tex = initTexture(gl) + gl.texImage2D(gl.TEXTURE_2D, 0, format, format, type, directData) + return new Texture2D(gl, tex, width, height, format, type) +} + +//Creates a texture from an ndarray +function createTextureArray(gl, array) { + var dtype = array.dtype + var shape = array.shape.slice() + var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE) + if(shape[0] < 0 || shape[0] > maxSize || shape[1] < 0 || shape[1] > maxSize) { + throw new Error('gl-texture2d: Invalid texture size') + } + var packed = isPacked(shape, array.stride.slice()) + var type = 0 + if(dtype === 'float32') { + type = gl.FLOAT + } else if(dtype === 'float64') { + type = gl.FLOAT + packed = false + dtype = 'float32' + } else if(dtype === 'uint8') { + type = gl.UNSIGNED_BYTE + } else { + type = gl.UNSIGNED_BYTE + packed = false + dtype = 'uint8' + } + var format = 0 + if(shape.length === 2) { + format = gl.LUMINANCE + shape = [shape[0], shape[1], 1] + array = ndarray(array.data, shape, [array.stride[0], array.stride[1], 1], array.offset) + } else if(shape.length === 3) { + if(shape[2] === 1) { + format = gl.ALPHA + } else if(shape[2] === 2) { + format = gl.LUMINANCE_ALPHA + } else if(shape[2] === 3) { + format = gl.RGB + } else if(shape[2] === 4) { + format = gl.RGBA + } else { + throw new Error('gl-texture2d: Invalid shape for pixel coords') + } + } else { + throw new Error('gl-texture2d: Invalid shape for texture') + } + if(type === gl.FLOAT && !gl.getExtension('OES_texture_float')) { + type = gl.UNSIGNED_BYTE + packed = false + } + var buffer, buf_store + var size = array.size + if(!packed) { + var stride = [shape[2], shape[2]*shape[0], 1] + buf_store = pool.malloc(size, dtype) + var buf_array = ndarray(buf_store, shape, stride, 0) + if((dtype === 'float32' || dtype === 'float64') && type === gl.UNSIGNED_BYTE) { + convertFloatToUint8(buf_array, array) + } else { + ops.assign(buf_array, array) + } + buffer = buf_store.subarray(0, size) + } else if (array.offset === 0 && array.data.length === size) { + buffer = array.data + } else { + buffer = array.data.subarray(array.offset, array.offset + size) + } + var tex = initTexture(gl) + gl.texImage2D(gl.TEXTURE_2D, 0, format, shape[0], shape[1], 0, format, type, buffer) + if(!packed) { + pool.free(buf_store) + } + return new Texture2D(gl, tex, shape[0], shape[1], format, type) +} + +function createTexture2D(gl) { + if(arguments.length <= 1) { + throw new Error('gl-texture2d: Missing arguments for texture2d constructor') + } + if(!linearTypes) { + lazyInitLinearTypes(gl) + } + if(typeof arguments[1] === 'number') { + return createTextureShape(gl, arguments[1], arguments[2], arguments[3]||gl.RGBA, arguments[4]||gl.UNSIGNED_BYTE) + } + if(Array.isArray(arguments[1])) { + return createTextureShape(gl, arguments[1][0]|0, arguments[1][1]|0, arguments[2]||gl.RGBA, arguments[3]||gl.UNSIGNED_BYTE) + } + if(typeof arguments[1] === 'object') { + var obj = arguments[1] + var directData = acceptTextureDOM(obj) ? obj : obj.raw + if (directData) { + return createTextureDOM(gl, directData, obj.width|0, obj.height|0, arguments[2]||gl.RGBA, arguments[3]||gl.UNSIGNED_BYTE) + } else if(obj.shape && obj.data && obj.stride) { + return createTextureArray(gl, obj) + } + } + throw new Error('gl-texture2d: Invalid arguments for texture2d constructor') +} + +},{"ndarray":443,"ndarray-ops":437,"typedarray-pool":992}],240:[function(require,module,exports){ +"use strict" + +function doBind(gl, elements, attributes) { + if(elements) { + elements.bind() + } else { + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null) + } + var nattribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS)|0 + if(attributes) { + if(attributes.length > nattribs) { + throw new Error("gl-vao: Too many vertex attributes") + } + for(var i=0; i 0) { + //TODO: evaluate use of glm_invsqrt here? + len = 1 / Math.sqrt(len) + out[0] = a[0] * len + out[1] = a[1] * len + out[2] = a[2] * len + } + return out +} +},{}],249:[function(require,module,exports){ +module.exports = transformMat4 + +/** + * Transforms the vec4 with a mat4. + * + * @param {vec4} out the receiving vector + * @param {vec4} a the vector to transform + * @param {mat4} m matrix to transform with + * @returns {vec4} out + */ +function transformMat4 (out, a, m) { + var x = a[0], y = a[1], z = a[2], w = a[3] + out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w + out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w + out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w + out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w + return out +} + +},{}],250:[function(require,module,exports){ +module.exports = decodeFloat + +var UINT8_VIEW = new Uint8Array(4) +var FLOAT_VIEW = new Float32Array(UINT8_VIEW.buffer) + +function decodeFloat(x, y, z, w) { + UINT8_VIEW[0] = w + UINT8_VIEW[1] = z + UINT8_VIEW[2] = y + UINT8_VIEW[3] = x + return FLOAT_VIEW[0] +} + +},{}],251:[function(require,module,exports){ +var tokenize = require('glsl-tokenizer') +var atob = require('atob-lite') + +module.exports = getName + +function getName(src) { + var tokens = Array.isArray(src) + ? src + : tokenize(src) + + for (var i = 0; i < tokens.length; i++) { + var token = tokens[i] + if (token.type !== 'preprocessor') continue + var match = token.data.match(/\#define\s+SHADER_NAME(_B64)?\s+(.+)$/) + if (!match) continue + if (!match[2]) continue + + var b64 = match[1] + var name = match[2] + + return (b64 ? atob(name) : name).trim() + } +} + +},{"atob-lite":10,"glsl-tokenizer":258}],252:[function(require,module,exports){ +module.exports = tokenize + +var literals100 = require('./lib/literals') + , operators = require('./lib/operators') + , builtins100 = require('./lib/builtins') + , literals300es = require('./lib/literals-300es') + , builtins300es = require('./lib/builtins-300es') + +var NORMAL = 999 // <-- never emitted + , TOKEN = 9999 // <-- never emitted + , BLOCK_COMMENT = 0 + , LINE_COMMENT = 1 + , PREPROCESSOR = 2 + , OPERATOR = 3 + , INTEGER = 4 + , FLOAT = 5 + , IDENT = 6 + , BUILTIN = 7 + , KEYWORD = 8 + , WHITESPACE = 9 + , EOF = 10 + , HEX = 11 + +var map = [ + 'block-comment' + , 'line-comment' + , 'preprocessor' + , 'operator' + , 'integer' + , 'float' + , 'ident' + , 'builtin' + , 'keyword' + , 'whitespace' + , 'eof' + , 'integer' +] + +function tokenize(opt) { + var i = 0 + , total = 0 + , mode = NORMAL + , c + , last + , content = [] + , tokens = [] + , token_idx = 0 + , token_offs = 0 + , line = 1 + , col = 0 + , start = 0 + , isnum = false + , isoperator = false + , input = '' + , len + + opt = opt || {} + var allBuiltins = builtins100 + var allLiterals = literals100 + if (opt.version === '300 es') { + allBuiltins = builtins300es + allLiterals = literals300es + } + + return function(data) { + tokens = [] + if (data !== null) return write(data.replace ? data.replace(/\r\n/g, '\n') : data) + return end() + } + + function token(data) { + if (data.length) { + tokens.push({ + type: map[mode] + , data: data + , position: start + , line: line + , column: col + }) + } + } + + function write(chunk) { + i = 0 + input += chunk + len = input.length + + var last + + while(c = input[i], i < len) { + last = i + + switch(mode) { + case BLOCK_COMMENT: i = block_comment(); break + case LINE_COMMENT: i = line_comment(); break + case PREPROCESSOR: i = preprocessor(); break + case OPERATOR: i = operator(); break + case INTEGER: i = integer(); break + case HEX: i = hex(); break + case FLOAT: i = decimal(); break + case TOKEN: i = readtoken(); break + case WHITESPACE: i = whitespace(); break + case NORMAL: i = normal(); break + } + + if(last !== i) { + switch(input[last]) { + case '\n': col = 0; ++line; break + default: ++col; break + } + } + } + + total += i + input = input.slice(i) + return tokens + } + + function end(chunk) { + if(content.length) { + token(content.join('')) + } + + mode = EOF + token('(eof)') + return tokens + } + + function normal() { + content = content.length ? [] : content + + if(last === '/' && c === '*') { + start = total + i - 1 + mode = BLOCK_COMMENT + last = c + return i + 1 + } + + if(last === '/' && c === '/') { + start = total + i - 1 + mode = LINE_COMMENT + last = c + return i + 1 + } + + if(c === '#') { + mode = PREPROCESSOR + start = total + i + return i + } + + if(/\s/.test(c)) { + mode = WHITESPACE + start = total + i + return i + } + + isnum = /\d/.test(c) + isoperator = /[^\w_]/.test(c) + + start = total + i + mode = isnum ? INTEGER : isoperator ? OPERATOR : TOKEN + return i + } + + function whitespace() { + if(/[^\s]/g.test(c)) { + token(content.join('')) + mode = NORMAL + return i + } + content.push(c) + last = c + return i + 1 + } + + function preprocessor() { + if((c === '\r' || c === '\n') && last !== '\\') { + token(content.join('')) + mode = NORMAL + return i + } + content.push(c) + last = c + return i + 1 + } + + function line_comment() { + return preprocessor() + } + + function block_comment() { + if(c === '/' && last === '*') { + content.push(c) + token(content.join('')) + mode = NORMAL + return i + 1 + } + + content.push(c) + last = c + return i + 1 + } + + function operator() { + if(last === '.' && /\d/.test(c)) { + mode = FLOAT + return i + } + + if(last === '/' && c === '*') { + mode = BLOCK_COMMENT + return i + } + + if(last === '/' && c === '/') { + mode = LINE_COMMENT + return i + } + + if(c === '.' && content.length) { + while(determine_operator(content)); + + mode = FLOAT + return i + } + + if(c === ';' || c === ')' || c === '(') { + if(content.length) while(determine_operator(content)); + token(c) + mode = NORMAL + return i + 1 + } + + var is_composite_operator = content.length === 2 && c !== '=' + if(/[\w_\d\s]/.test(c) || is_composite_operator) { + while(determine_operator(content)); + mode = NORMAL + return i + } + + content.push(c) + last = c + return i + 1 + } + + function determine_operator(buf) { + var j = 0 + , idx + , res + + do { + idx = operators.indexOf(buf.slice(0, buf.length + j).join('')) + res = operators[idx] + + if(idx === -1) { + if(j-- + buf.length > 0) continue + res = buf.slice(0, 1).join('') + } + + token(res) + + start += res.length + content = content.slice(res.length) + return content.length + } while(1) + } + + function hex() { + if(/[^a-fA-F0-9]/.test(c)) { + token(content.join('')) + mode = NORMAL + return i + } + + content.push(c) + last = c + return i + 1 + } + + function integer() { + if(c === '.') { + content.push(c) + mode = FLOAT + last = c + return i + 1 + } + + if(/[eE]/.test(c)) { + content.push(c) + mode = FLOAT + last = c + return i + 1 + } + + if(c === 'x' && content.length === 1 && content[0] === '0') { + mode = HEX + content.push(c) + last = c + return i + 1 + } + + if(/[^\d]/.test(c)) { + token(content.join('')) + mode = NORMAL + return i + } + + content.push(c) + last = c + return i + 1 + } + + function decimal() { + if(c === 'f') { + content.push(c) + last = c + i += 1 + } + + if(/[eE]/.test(c)) { + content.push(c) + last = c + return i + 1 + } + + if (c === '-' && /[eE]/.test(last)) { + content.push(c) + last = c + return i + 1 + } + + if(/[^\d]/.test(c)) { + token(content.join('')) + mode = NORMAL + return i + } + + content.push(c) + last = c + return i + 1 + } + + function readtoken() { + if(/[^\d\w_]/.test(c)) { + var contentstr = content.join('') + if(allLiterals.indexOf(contentstr) > -1) { + mode = KEYWORD + } else if(allBuiltins.indexOf(contentstr) > -1) { + mode = BUILTIN + } else { + mode = IDENT + } + token(content.join('')) + mode = NORMAL + return i + } + content.push(c) + last = c + return i + 1 + } +} + +},{"./lib/builtins":254,"./lib/builtins-300es":253,"./lib/literals":256,"./lib/literals-300es":255,"./lib/operators":257}],253:[function(require,module,exports){ +// 300es builtins/reserved words that were previously valid in v100 +var v100 = require('./builtins') + +// The texture2D|Cube functions have been removed +// And the gl_ features are updated +v100 = v100.slice().filter(function (b) { + return !/^(gl\_|texture)/.test(b) +}) + +module.exports = v100.concat([ + // the updated gl_ constants + 'gl_VertexID' + , 'gl_InstanceID' + , 'gl_Position' + , 'gl_PointSize' + , 'gl_FragCoord' + , 'gl_FrontFacing' + , 'gl_FragDepth' + , 'gl_PointCoord' + , 'gl_MaxVertexAttribs' + , 'gl_MaxVertexUniformVectors' + , 'gl_MaxVertexOutputVectors' + , 'gl_MaxFragmentInputVectors' + , 'gl_MaxVertexTextureImageUnits' + , 'gl_MaxCombinedTextureImageUnits' + , 'gl_MaxTextureImageUnits' + , 'gl_MaxFragmentUniformVectors' + , 'gl_MaxDrawBuffers' + , 'gl_MinProgramTexelOffset' + , 'gl_MaxProgramTexelOffset' + , 'gl_DepthRangeParameters' + , 'gl_DepthRange' + + // other builtins + , 'trunc' + , 'round' + , 'roundEven' + , 'isnan' + , 'isinf' + , 'floatBitsToInt' + , 'floatBitsToUint' + , 'intBitsToFloat' + , 'uintBitsToFloat' + , 'packSnorm2x16' + , 'unpackSnorm2x16' + , 'packUnorm2x16' + , 'unpackUnorm2x16' + , 'packHalf2x16' + , 'unpackHalf2x16' + , 'outerProduct' + , 'transpose' + , 'determinant' + , 'inverse' + , 'texture' + , 'textureSize' + , 'textureProj' + , 'textureLod' + , 'textureOffset' + , 'texelFetch' + , 'texelFetchOffset' + , 'textureProjOffset' + , 'textureLodOffset' + , 'textureProjLod' + , 'textureProjLodOffset' + , 'textureGrad' + , 'textureGradOffset' + , 'textureProjGrad' + , 'textureProjGradOffset' +]) + +},{"./builtins":254}],254:[function(require,module,exports){ +module.exports = [ + // Keep this list sorted + 'abs' + , 'acos' + , 'all' + , 'any' + , 'asin' + , 'atan' + , 'ceil' + , 'clamp' + , 'cos' + , 'cross' + , 'dFdx' + , 'dFdy' + , 'degrees' + , 'distance' + , 'dot' + , 'equal' + , 'exp' + , 'exp2' + , 'faceforward' + , 'floor' + , 'fract' + , 'gl_BackColor' + , 'gl_BackLightModelProduct' + , 'gl_BackLightProduct' + , 'gl_BackMaterial' + , 'gl_BackSecondaryColor' + , 'gl_ClipPlane' + , 'gl_ClipVertex' + , 'gl_Color' + , 'gl_DepthRange' + , 'gl_DepthRangeParameters' + , 'gl_EyePlaneQ' + , 'gl_EyePlaneR' + , 'gl_EyePlaneS' + , 'gl_EyePlaneT' + , 'gl_Fog' + , 'gl_FogCoord' + , 'gl_FogFragCoord' + , 'gl_FogParameters' + , 'gl_FragColor' + , 'gl_FragCoord' + , 'gl_FragData' + , 'gl_FragDepth' + , 'gl_FragDepthEXT' + , 'gl_FrontColor' + , 'gl_FrontFacing' + , 'gl_FrontLightModelProduct' + , 'gl_FrontLightProduct' + , 'gl_FrontMaterial' + , 'gl_FrontSecondaryColor' + , 'gl_LightModel' + , 'gl_LightModelParameters' + , 'gl_LightModelProducts' + , 'gl_LightProducts' + , 'gl_LightSource' + , 'gl_LightSourceParameters' + , 'gl_MaterialParameters' + , 'gl_MaxClipPlanes' + , 'gl_MaxCombinedTextureImageUnits' + , 'gl_MaxDrawBuffers' + , 'gl_MaxFragmentUniformComponents' + , 'gl_MaxLights' + , 'gl_MaxTextureCoords' + , 'gl_MaxTextureImageUnits' + , 'gl_MaxTextureUnits' + , 'gl_MaxVaryingFloats' + , 'gl_MaxVertexAttribs' + , 'gl_MaxVertexTextureImageUnits' + , 'gl_MaxVertexUniformComponents' + , 'gl_ModelViewMatrix' + , 'gl_ModelViewMatrixInverse' + , 'gl_ModelViewMatrixInverseTranspose' + , 'gl_ModelViewMatrixTranspose' + , 'gl_ModelViewProjectionMatrix' + , 'gl_ModelViewProjectionMatrixInverse' + , 'gl_ModelViewProjectionMatrixInverseTranspose' + , 'gl_ModelViewProjectionMatrixTranspose' + , 'gl_MultiTexCoord0' + , 'gl_MultiTexCoord1' + , 'gl_MultiTexCoord2' + , 'gl_MultiTexCoord3' + , 'gl_MultiTexCoord4' + , 'gl_MultiTexCoord5' + , 'gl_MultiTexCoord6' + , 'gl_MultiTexCoord7' + , 'gl_Normal' + , 'gl_NormalMatrix' + , 'gl_NormalScale' + , 'gl_ObjectPlaneQ' + , 'gl_ObjectPlaneR' + , 'gl_ObjectPlaneS' + , 'gl_ObjectPlaneT' + , 'gl_Point' + , 'gl_PointCoord' + , 'gl_PointParameters' + , 'gl_PointSize' + , 'gl_Position' + , 'gl_ProjectionMatrix' + , 'gl_ProjectionMatrixInverse' + , 'gl_ProjectionMatrixInverseTranspose' + , 'gl_ProjectionMatrixTranspose' + , 'gl_SecondaryColor' + , 'gl_TexCoord' + , 'gl_TextureEnvColor' + , 'gl_TextureMatrix' + , 'gl_TextureMatrixInverse' + , 'gl_TextureMatrixInverseTranspose' + , 'gl_TextureMatrixTranspose' + , 'gl_Vertex' + , 'greaterThan' + , 'greaterThanEqual' + , 'inversesqrt' + , 'length' + , 'lessThan' + , 'lessThanEqual' + , 'log' + , 'log2' + , 'matrixCompMult' + , 'max' + , 'min' + , 'mix' + , 'mod' + , 'normalize' + , 'not' + , 'notEqual' + , 'pow' + , 'radians' + , 'reflect' + , 'refract' + , 'sign' + , 'sin' + , 'smoothstep' + , 'sqrt' + , 'step' + , 'tan' + , 'texture2D' + , 'texture2DLod' + , 'texture2DProj' + , 'texture2DProjLod' + , 'textureCube' + , 'textureCubeLod' + , 'texture2DLodEXT' + , 'texture2DProjLodEXT' + , 'textureCubeLodEXT' + , 'texture2DGradEXT' + , 'texture2DProjGradEXT' + , 'textureCubeGradEXT' +] + +},{}],255:[function(require,module,exports){ +var v100 = require('./literals') + +module.exports = v100.slice().concat([ + 'layout' + , 'centroid' + , 'smooth' + , 'case' + , 'mat2x2' + , 'mat2x3' + , 'mat2x4' + , 'mat3x2' + , 'mat3x3' + , 'mat3x4' + , 'mat4x2' + , 'mat4x3' + , 'mat4x4' + , 'uint' + , 'uvec2' + , 'uvec3' + , 'uvec4' + , 'samplerCubeShadow' + , 'sampler2DArray' + , 'sampler2DArrayShadow' + , 'isampler2D' + , 'isampler3D' + , 'isamplerCube' + , 'isampler2DArray' + , 'usampler2D' + , 'usampler3D' + , 'usamplerCube' + , 'usampler2DArray' + , 'coherent' + , 'restrict' + , 'readonly' + , 'writeonly' + , 'resource' + , 'atomic_uint' + , 'noperspective' + , 'patch' + , 'sample' + , 'subroutine' + , 'common' + , 'partition' + , 'active' + , 'filter' + , 'image1D' + , 'image2D' + , 'image3D' + , 'imageCube' + , 'iimage1D' + , 'iimage2D' + , 'iimage3D' + , 'iimageCube' + , 'uimage1D' + , 'uimage2D' + , 'uimage3D' + , 'uimageCube' + , 'image1DArray' + , 'image2DArray' + , 'iimage1DArray' + , 'iimage2DArray' + , 'uimage1DArray' + , 'uimage2DArray' + , 'image1DShadow' + , 'image2DShadow' + , 'image1DArrayShadow' + , 'image2DArrayShadow' + , 'imageBuffer' + , 'iimageBuffer' + , 'uimageBuffer' + , 'sampler1DArray' + , 'sampler1DArrayShadow' + , 'isampler1D' + , 'isampler1DArray' + , 'usampler1D' + , 'usampler1DArray' + , 'isampler2DRect' + , 'usampler2DRect' + , 'samplerBuffer' + , 'isamplerBuffer' + , 'usamplerBuffer' + , 'sampler2DMS' + , 'isampler2DMS' + , 'usampler2DMS' + , 'sampler2DMSArray' + , 'isampler2DMSArray' + , 'usampler2DMSArray' +]) + +},{"./literals":256}],256:[function(require,module,exports){ +module.exports = [ + // current + 'precision' + , 'highp' + , 'mediump' + , 'lowp' + , 'attribute' + , 'const' + , 'uniform' + , 'varying' + , 'break' + , 'continue' + , 'do' + , 'for' + , 'while' + , 'if' + , 'else' + , 'in' + , 'out' + , 'inout' + , 'float' + , 'int' + , 'void' + , 'bool' + , 'true' + , 'false' + , 'discard' + , 'return' + , 'mat2' + , 'mat3' + , 'mat4' + , 'vec2' + , 'vec3' + , 'vec4' + , 'ivec2' + , 'ivec3' + , 'ivec4' + , 'bvec2' + , 'bvec3' + , 'bvec4' + , 'sampler1D' + , 'sampler2D' + , 'sampler3D' + , 'samplerCube' + , 'sampler1DShadow' + , 'sampler2DShadow' + , 'struct' + + // future + , 'asm' + , 'class' + , 'union' + , 'enum' + , 'typedef' + , 'template' + , 'this' + , 'packed' + , 'goto' + , 'switch' + , 'default' + , 'inline' + , 'noinline' + , 'volatile' + , 'public' + , 'static' + , 'extern' + , 'external' + , 'interface' + , 'long' + , 'short' + , 'double' + , 'half' + , 'fixed' + , 'unsigned' + , 'input' + , 'output' + , 'hvec2' + , 'hvec3' + , 'hvec4' + , 'dvec2' + , 'dvec3' + , 'dvec4' + , 'fvec2' + , 'fvec3' + , 'fvec4' + , 'sampler2DRect' + , 'sampler3DRect' + , 'sampler2DRectShadow' + , 'sizeof' + , 'cast' + , 'namespace' + , 'using' +] + +},{}],257:[function(require,module,exports){ +module.exports = [ + '<<=' + , '>>=' + , '++' + , '--' + , '<<' + , '>>' + , '<=' + , '>=' + , '==' + , '!=' + , '&&' + , '||' + , '+=' + , '-=' + , '*=' + , '/=' + , '%=' + , '&=' + , '^^' + , '^=' + , '|=' + , '(' + , ')' + , '[' + , ']' + , '.' + , '!' + , '~' + , '*' + , '/' + , '%' + , '+' + , '-' + , '<' + , '>' + , '&' + , '^' + , '|' + , '?' + , ':' + , '=' + , ',' + , ';' + , '{' + , '}' +] + +},{}],258:[function(require,module,exports){ +var tokenize = require('./index') + +module.exports = tokenizeString + +function tokenizeString(str, opt) { + var generator = tokenize(opt) + var tokens = [] + + tokens = tokens.concat(generator(str)) + tokens = tokens.concat(generator(null)) + + return tokens +} + +},{"./index":252}],259:[function(require,module,exports){ +'use strict'; + +module.exports = GridIndex; + +var NUM_PARAMS = 3; + +function GridIndex(extent, n, padding) { + var cells = this.cells = []; + + if (extent instanceof ArrayBuffer) { + this.arrayBuffer = extent; + var array = new Int32Array(this.arrayBuffer); + extent = array[0]; + n = array[1]; + padding = array[2]; + + this.d = n + 2 * padding; + for (var k = 0; k < this.d * this.d; k++) { + var start = array[NUM_PARAMS + k]; + var end = array[NUM_PARAMS + k + 1]; + cells.push(start === end ? + null : + array.subarray(start, end)); + } + var keysOffset = array[NUM_PARAMS + cells.length]; + var bboxesOffset = array[NUM_PARAMS + cells.length + 1]; + this.keys = array.subarray(keysOffset, bboxesOffset); + this.bboxes = array.subarray(bboxesOffset); + + this.insert = this._insertReadonly; + + } else { + this.d = n + 2 * padding; + for (var i = 0; i < this.d * this.d; i++) { + cells.push([]); + } + this.keys = []; + this.bboxes = []; + } + + this.n = n; + this.extent = extent; + this.padding = padding; + this.scale = n / extent; + this.uid = 0; + + var p = (padding / n) * extent; + this.min = -p; + this.max = extent + p; +} + + +GridIndex.prototype.insert = function(key, x1, y1, x2, y2) { + this._forEachCell(x1, y1, x2, y2, this._insertCell, this.uid++); + this.keys.push(key); + this.bboxes.push(x1); + this.bboxes.push(y1); + this.bboxes.push(x2); + this.bboxes.push(y2); +}; + +GridIndex.prototype._insertReadonly = function() { + throw 'Cannot insert into a GridIndex created from an ArrayBuffer.'; +}; + +GridIndex.prototype._insertCell = function(x1, y1, x2, y2, cellIndex, uid) { + this.cells[cellIndex].push(uid); +}; + +GridIndex.prototype.query = function(x1, y1, x2, y2) { + var min = this.min; + var max = this.max; + if (x1 <= min && y1 <= min && max <= x2 && max <= y2) { + // We use `Array#slice` because `this.keys` may be a `Int32Array` and + // some browsers (Safari and IE) do not support `TypedArray#slice` + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice#Browser_compatibility + return Array.prototype.slice.call(this.keys); + + } else { + var result = []; + var seenUids = {}; + this._forEachCell(x1, y1, x2, y2, this._queryCell, result, seenUids); + return result; + } +}; + +GridIndex.prototype._queryCell = function(x1, y1, x2, y2, cellIndex, result, seenUids) { + var cell = this.cells[cellIndex]; + if (cell !== null) { + var keys = this.keys; + var bboxes = this.bboxes; + for (var u = 0; u < cell.length; u++) { + var uid = cell[u]; + if (seenUids[uid] === undefined) { + var offset = uid * 4; + if ((x1 <= bboxes[offset + 2]) && + (y1 <= bboxes[offset + 3]) && + (x2 >= bboxes[offset + 0]) && + (y2 >= bboxes[offset + 1])) { + seenUids[uid] = true; + result.push(keys[uid]); + } else { + seenUids[uid] = false; + } + } + } + } +}; + +GridIndex.prototype._forEachCell = function(x1, y1, x2, y2, fn, arg1, arg2) { + var cx1 = this._convertToCellCoord(x1); + var cy1 = this._convertToCellCoord(y1); + var cx2 = this._convertToCellCoord(x2); + var cy2 = this._convertToCellCoord(y2); + for (var x = cx1; x <= cx2; x++) { + for (var y = cy1; y <= cy2; y++) { + var cellIndex = this.d * y + x; + if (fn.call(this, x1, y1, x2, y2, cellIndex, arg1, arg2)) return; + } + } +}; + +GridIndex.prototype._convertToCellCoord = function(x) { + return Math.max(0, Math.min(this.d - 1, Math.floor(x * this.scale) + this.padding)); +}; + +GridIndex.prototype.toArrayBuffer = function() { + if (this.arrayBuffer) return this.arrayBuffer; + + var cells = this.cells; + + var metadataLength = NUM_PARAMS + this.cells.length + 1 + 1; + var totalCellLength = 0; + for (var i = 0; i < this.cells.length; i++) { + totalCellLength += this.cells[i].length; + } + + var array = new Int32Array(metadataLength + totalCellLength + this.keys.length + this.bboxes.length); + array[0] = this.extent; + array[1] = this.n; + array[2] = this.padding; + + var offset = metadataLength; + for (var k = 0; k < cells.length; k++) { + var cell = cells[k]; + array[NUM_PARAMS + k] = offset; + array.set(cell, offset); + offset += cell.length; + } + + array[NUM_PARAMS + cells.length] = offset; + array.set(this.keys, offset); + offset += this.keys.length; + + array[NUM_PARAMS + cells.length + 1] = offset; + array.set(this.bboxes, offset); + offset += this.bboxes.length; + + return array.buffer; +}; + +},{}],260:[function(require,module,exports){ +exports.read = function (buffer, offset, isLE, mLen, nBytes) { + var e, m + var eLen = nBytes * 8 - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var nBits = -7 + var i = isLE ? (nBytes - 1) : 0 + var d = isLE ? -1 : 1 + var s = buffer[offset + i] + + i += d + + e = s & ((1 << (-nBits)) - 1) + s >>= (-nBits) + nBits += eLen + for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} + + m = e & ((1 << (-nBits)) - 1) + e >>= (-nBits) + nBits += mLen + for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} + + if (e === 0) { + e = 1 - eBias + } else if (e === eMax) { + return m ? NaN : ((s ? -1 : 1) * Infinity) + } else { + m = m + Math.pow(2, mLen) + e = e - eBias + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen) +} + +exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { + var e, m, c + var eLen = nBytes * 8 - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) + var i = isLE ? 0 : (nBytes - 1) + var d = isLE ? 1 : -1 + var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 + + value = Math.abs(value) + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0 + e = eMax + } else { + e = Math.floor(Math.log(value) / Math.LN2) + if (value * (c = Math.pow(2, -e)) < 1) { + e-- + c *= 2 + } + if (e + eBias >= 1) { + value += rt / c + } else { + value += rt * Math.pow(2, 1 - eBias) + } + if (value * c >= 2) { + e++ + c /= 2 + } + + if (e + eBias >= eMax) { + m = 0 + e = eMax + } else if (e + eBias >= 1) { + m = (value * c - 1) * Math.pow(2, mLen) + e = e + eBias + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) + e = 0 + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} + + e = (e << mLen) | m + eLen += mLen + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} + + buffer[offset + i - d] |= s * 128 +} + +},{}],261:[function(require,module,exports){ require('es6-promise/auto'); var work = require('webworkify'); @@ -17915,7 +54716,7 @@ exports.apply = function (worker, nWorkers, canvas, context, params) { }); }; -},{"es6-promise/auto":82,"webworkify":84}],86:[function(require,module,exports){ +},{"es6-promise/auto":93,"webworkify":1016}],262:[function(require,module,exports){ var imageFilterCore = require('image-filter-core'); var worker = require('./worker'); @@ -17950,7 +54751,7 @@ module.exports = function threshold(options) { ); }; -},{"./worker":88,"image-filter-core":85}],87:[function(require,module,exports){ +},{"./worker":264,"image-filter-core":261}],263:[function(require,module,exports){ /** * Iterate over the array applying the threshold transformation * @name transform @@ -17968,7 +54769,7 @@ module.exports = function transform (data, length, threshold) { } }; -},{}],88:[function(require,module,exports){ +},{}],264:[function(require,module,exports){ var transform = require('./threshold'); module.exports = function (self) { @@ -17992,7 +54793,909 @@ module.exports = function (self) { }); }; -},{"./threshold":87}],89:[function(require,module,exports){ +},{"./threshold":263}],265:[function(require,module,exports){ +"use strict" + +//High level idea: +// 1. Use Clarkson's incremental construction to find convex hull +// 2. Point location in triangulation by jump and walk + +module.exports = incrementalConvexHull + +var orient = require("robust-orientation") +var compareCell = require("simplicial-complex").compareCells + +function compareInt(a, b) { + return a - b +} + +function Simplex(vertices, adjacent, boundary) { + this.vertices = vertices + this.adjacent = adjacent + this.boundary = boundary + this.lastVisited = -1 +} + +Simplex.prototype.flip = function() { + var t = this.vertices[0] + this.vertices[0] = this.vertices[1] + this.vertices[1] = t + var u = this.adjacent[0] + this.adjacent[0] = this.adjacent[1] + this.adjacent[1] = u +} + +function GlueFacet(vertices, cell, index) { + this.vertices = vertices + this.cell = cell + this.index = index +} + +function compareGlue(a, b) { + return compareCell(a.vertices, b.vertices) +} + +function bakeOrient(d) { + var code = ["function orient(){var tuple=this.tuple;return test("] + for(var i=0; i<=d; ++i) { + if(i > 0) { + code.push(",") + } + code.push("tuple[", i, "]") + } + code.push(")}return orient") + var proc = new Function("test", code.join("")) + var test = orient[d+1] + if(!test) { + test = orient + } + return proc(test) +} + +var BAKED = [] + +function Triangulation(dimension, vertices, simplices) { + this.dimension = dimension + this.vertices = vertices + this.simplices = simplices + this.interior = simplices.filter(function(c) { + return !c.boundary + }) + + this.tuple = new Array(dimension+1) + for(var i=0; i<=dimension; ++i) { + this.tuple[i] = this.vertices[i] + } + + var o = BAKED[dimension] + if(!o) { + o = BAKED[dimension] = bakeOrient(dimension) + } + this.orient = o +} + +var proto = Triangulation.prototype + +//Degenerate situation where we are on boundary, but coplanar to face +proto.handleBoundaryDegeneracy = function(cell, point) { + var d = this.dimension + var n = this.vertices.length - 1 + var tuple = this.tuple + var verts = this.vertices + + //Dumb solution: Just do dfs from boundary cell until we find any peak, or terminate + var toVisit = [ cell ] + cell.lastVisited = -n + while(toVisit.length > 0) { + cell = toVisit.pop() + var cellVerts = cell.vertices + var cellAdj = cell.adjacent + for(var i=0; i<=d; ++i) { + var neighbor = cellAdj[i] + if(!neighbor.boundary || neighbor.lastVisited <= -n) { + continue + } + var nv = neighbor.vertices + for(var j=0; j<=d; ++j) { + var vv = nv[j] + if(vv < 0) { + tuple[j] = point + } else { + tuple[j] = verts[vv] + } + } + var o = this.orient() + if(o > 0) { + return neighbor + } + neighbor.lastVisited = -n + if(o === 0) { + toVisit.push(neighbor) + } + } + } + return null +} + +proto.walk = function(point, random) { + //Alias local properties + var n = this.vertices.length - 1 + var d = this.dimension + var verts = this.vertices + var tuple = this.tuple + + //Compute initial jump cell + var initIndex = random ? (this.interior.length * Math.random())|0 : (this.interior.length-1) + var cell = this.interior[ initIndex ] + + //Start walking +outerLoop: + while(!cell.boundary) { + var cellVerts = cell.vertices + var cellAdj = cell.adjacent + + for(var i=0; i<=d; ++i) { + tuple[i] = verts[cellVerts[i]] + } + cell.lastVisited = n + + //Find farthest adjacent cell + for(var i=0; i<=d; ++i) { + var neighbor = cellAdj[i] + if(neighbor.lastVisited >= n) { + continue + } + var prev = tuple[i] + tuple[i] = point + var o = this.orient() + tuple[i] = prev + if(o < 0) { + cell = neighbor + continue outerLoop + } else { + if(!neighbor.boundary) { + neighbor.lastVisited = n + } else { + neighbor.lastVisited = -n + } + } + } + return + } + + return cell +} + +proto.addPeaks = function(point, cell) { + var n = this.vertices.length - 1 + var d = this.dimension + var verts = this.vertices + var tuple = this.tuple + var interior = this.interior + var simplices = this.simplices + + //Walking finished at boundary, time to add peaks + var tovisit = [ cell ] + + //Stretch initial boundary cell into a peak + cell.lastVisited = n + cell.vertices[cell.vertices.indexOf(-1)] = n + cell.boundary = false + interior.push(cell) + + //Record a list of all new boundaries created by added peaks so we can glue them together when we are all done + var glueFacets = [] + + //Do a traversal of the boundary walking outward from starting peak + while(tovisit.length > 0) { + //Pop off peak and walk over adjacent cells + var cell = tovisit.pop() + var cellVerts = cell.vertices + var cellAdj = cell.adjacent + var indexOfN = cellVerts.indexOf(n) + if(indexOfN < 0) { + continue + } + + for(var i=0; i<=d; ++i) { + if(i === indexOfN) { + continue + } + + //For each boundary neighbor of the cell + var neighbor = cellAdj[i] + if(!neighbor.boundary || neighbor.lastVisited >= n) { + continue + } + + var nv = neighbor.vertices + + //Test if neighbor is a peak + if(neighbor.lastVisited !== -n) { + //Compute orientation of p relative to each boundary peak + var indexOfNeg1 = 0 + for(var j=0; j<=d; ++j) { + if(nv[j] < 0) { + indexOfNeg1 = j + tuple[j] = point + } else { + tuple[j] = verts[nv[j]] + } + } + var o = this.orient() + + //Test if neighbor cell is also a peak + if(o > 0) { + nv[indexOfNeg1] = n + neighbor.boundary = false + interior.push(neighbor) + tovisit.push(neighbor) + neighbor.lastVisited = n + continue + } else { + neighbor.lastVisited = -n + } + } + + var na = neighbor.adjacent + + //Otherwise, replace neighbor with new face + var vverts = cellVerts.slice() + var vadj = cellAdj.slice() + var ncell = new Simplex(vverts, vadj, true) + simplices.push(ncell) + + //Connect to neighbor + var opposite = na.indexOf(cell) + if(opposite < 0) { + continue + } + na[opposite] = ncell + vadj[indexOfN] = neighbor + + //Connect to cell + vverts[i] = -1 + vadj[i] = cell + cellAdj[i] = ncell + + //Flip facet + ncell.flip() + + //Add to glue list + for(var j=0; j<=d; ++j) { + var uu = vverts[j] + if(uu < 0 || uu === n) { + continue + } + var nface = new Array(d-1) + var nptr = 0 + for(var k=0; k<=d; ++k) { + var vv = vverts[k] + if(vv < 0 || k === j) { + continue + } + nface[nptr++] = vv + } + glueFacets.push(new GlueFacet(nface, ncell, j)) + } + } + } + + //Glue boundary facets together + glueFacets.sort(compareGlue) + + for(var i=0; i+1= 0) { + bcell[ptr++] = cv[j] + } else { + parity = j&1 + } + } + if(parity === (d&1)) { + var t = bcell[0] + bcell[0] = bcell[1] + bcell[1] = t + } + boundary.push(bcell) + } + } + return boundary +} + +function incrementalConvexHull(points, randomSearch) { + var n = points.length + if(n === 0) { + throw new Error("Must have at least d+1 points") + } + var d = points[0].length + if(n <= d) { + throw new Error("Must input at least d+1 points") + } + + //FIXME: This could be degenerate, but need to select d+1 non-coplanar points to bootstrap process + var initialSimplex = points.slice(0, d+1) + + //Make sure initial simplex is positively oriented + var o = orient.apply(void 0, initialSimplex) + if(o === 0) { + throw new Error("Input not in general position") + } + var initialCoords = new Array(d+1) + for(var i=0; i<=d; ++i) { + initialCoords[i] = i + } + if(o < 0) { + initialCoords[0] = 1 + initialCoords[1] = 0 + } + + //Create initial topological index, glue pointers together (kind of messy) + var initialCell = new Simplex(initialCoords, new Array(d+1), false) + var boundary = initialCell.adjacent + var list = new Array(d+2) + for(var i=0; i<=d; ++i) { + var verts = initialCoords.slice() + for(var j=0; j<=d; ++j) { + if(j === i) { + verts[j] = -1 + } + } + var t = verts[0] + verts[0] = verts[1] + verts[1] = t + var cell = new Simplex(verts, new Array(d+1), true) + boundary[i] = cell + list[i] = cell + } + list[d+1] = initialCell + for(var i=0; i<=d; ++i) { + var verts = boundary[i].vertices + var adj = boundary[i].adjacent + for(var j=0; j<=d; ++j) { + var v = verts[j] + if(v < 0) { + adj[j] = initialCell + continue + } + for(var k=0; k<=d; ++k) { + if(boundary[k].vertices.indexOf(v) < 0) { + adj[j] = boundary[k] + } + } + } + } + + //Initialize triangles + var triangles = new Triangulation(d, initialSimplex, list) + + //Insert remaining points + var useRandom = !!randomSearch + for(var i=d+1; i 3*(weight+1)) { + rebuildWithInterval(this, interval) + } else { + this.left.insert(interval) + } + } else { + this.left = createIntervalTree([interval]) + } + } else if(interval[0] > this.mid) { + if(this.right) { + if(4*(this.right.count+1) > 3*(weight+1)) { + rebuildWithInterval(this, interval) + } else { + this.right.insert(interval) + } + } else { + this.right = createIntervalTree([interval]) + } + } else { + var l = bounds.ge(this.leftPoints, interval, compareBegin) + var r = bounds.ge(this.rightPoints, interval, compareEnd) + this.leftPoints.splice(l, 0, interval) + this.rightPoints.splice(r, 0, interval) + } +} + +proto.remove = function(interval) { + var weight = this.count - this.leftPoints + if(interval[1] < this.mid) { + if(!this.left) { + return NOT_FOUND + } + var rw = this.right ? this.right.count : 0 + if(4 * rw > 3 * (weight-1)) { + return rebuildWithoutInterval(this, interval) + } + var r = this.left.remove(interval) + if(r === EMPTY) { + this.left = null + this.count -= 1 + return SUCCESS + } else if(r === SUCCESS) { + this.count -= 1 + } + return r + } else if(interval[0] > this.mid) { + if(!this.right) { + return NOT_FOUND + } + var lw = this.left ? this.left.count : 0 + if(4 * lw > 3 * (weight-1)) { + return rebuildWithoutInterval(this, interval) + } + var r = this.right.remove(interval) + if(r === EMPTY) { + this.right = null + this.count -= 1 + return SUCCESS + } else if(r === SUCCESS) { + this.count -= 1 + } + return r + } else { + if(this.count === 1) { + if(this.leftPoints[0] === interval) { + return EMPTY + } else { + return NOT_FOUND + } + } + if(this.leftPoints.length === 1 && this.leftPoints[0] === interval) { + if(this.left && this.right) { + var p = this + var n = this.left + while(n.right) { + p = n + n = n.right + } + if(p === this) { + n.right = this.right + } else { + var l = this.left + var r = this.right + p.count -= n.count + p.right = n.left + n.left = l + n.right = r + } + copy(this, n) + this.count = (this.left?this.left.count:0) + (this.right?this.right.count:0) + this.leftPoints.length + } else if(this.left) { + copy(this, this.left) + } else { + copy(this, this.right) + } + return SUCCESS + } + for(var l = bounds.ge(this.leftPoints, interval, compareBegin); l=0 && arr[i][1] >= lo; --i) { + var r = cb(arr[i]) + if(r) { return r } + } +} + +function reportRange(arr, cb) { + for(var i=0; i this.mid) { + if(this.right) { + var r = this.right.queryPoint(x, cb) + if(r) { return r } + } + return reportRightRange(this.rightPoints, x, cb) + } else { + return reportRange(this.leftPoints, cb) + } +} + +proto.queryInterval = function(lo, hi, cb) { + if(lo < this.mid && this.left) { + var r = this.left.queryInterval(lo, hi, cb) + if(r) { return r } + } + if(hi > this.mid && this.right) { + var r = this.right.queryInterval(lo, hi, cb) + if(r) { return r } + } + if(hi < this.mid) { + return reportLeftRange(this.leftPoints, hi, cb) + } else if(lo > this.mid) { + return reportRightRange(this.rightPoints, lo, cb) + } else { + return reportRange(this.leftPoints, cb) + } +} + +function compareNumbers(a, b) { + return a - b +} + +function compareBegin(a, b) { + var d = a[0] - b[0] + if(d) { return d } + return a[1] - b[1] +} + +function compareEnd(a, b) { + var d = a[1] - b[1] + if(d) { return d } + return a[0] - b[0] +} + +function createIntervalTree(intervals) { + if(intervals.length === 0) { + return null + } + var pts = [] + for(var i=0; i>1] + + var leftIntervals = [] + var rightIntervals = [] + var centerIntervals = [] + for(var i=0; i + * @license MIT + */ + +// The _isBuffer check is for Safari 5-7 support, because it's missing +// Object.prototype.constructor. Remove this eventually +module.exports = function (obj) { + return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) +} + +function isBuffer (obj) { + return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) +} + +// For Node v0.10 support. Remove this eventually. +function isSlowBuffer (obj) { + return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) +} + +},{}],271:[function(require,module,exports){ +module.exports = isMobile; + +function isMobile (ua) { + if (!ua && typeof navigator != 'undefined') ua = navigator.userAgent; + if (ua && ua.headers && typeof ua.headers['user-agent'] == 'string') { + ua = ua.headers['user-agent']; + } + if (typeof ua != 'string') return false; + + return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(ua) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(ua.substr(0,4)); +} + + +},{}],272:[function(require,module,exports){ +arguments[4][47][0].apply(exports,arguments) +},{"dup":47}],273:[function(require,module,exports){ /*! * jQuery JavaScript Library v2.2.4 * http://jquery.com/ @@ -27808,46453 +65511,1568 @@ if ( !noGlobal ) { return jQuery; })); -},{}],90:[function(require,module,exports){ +},{}],274:[function(require,module,exports){ 'use strict'; -var Lib = require('../src/lib'); -var rules = { - "X,X div": "font-family:'Open Sans', verdana, arial, sans-serif;margin:0;padding:0;", - "X input,X button": "font-family:'Open Sans', verdana, arial, sans-serif;", - "X input:focus,X button:focus": "outline:none;", - "X a": "text-decoration:none;", - "X a:hover": "text-decoration:none;", - "X .crisp": "shape-rendering:crispEdges;", - "X .user-select-none": "-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;-o-user-select:none;user-select:none;", - "X svg": "overflow:hidden;", - "X svg a": "fill:#447adb;", - "X svg a:hover": "fill:#3c6dc5;", - "X .main-svg": "position:absolute;top:0;left:0;pointer-events:none;", - "X .main-svg .draglayer": "pointer-events:all;", - "X .cursor-pointer": "cursor:pointer;", - "X .cursor-crosshair": "cursor:crosshair;", - "X .cursor-move": "cursor:move;", - "X .cursor-col-resize": "cursor:col-resize;", - "X .cursor-row-resize": "cursor:row-resize;", - "X .cursor-ns-resize": "cursor:ns-resize;", - "X .cursor-ew-resize": "cursor:ew-resize;", - "X .cursor-sw-resize": "cursor:sw-resize;", - "X .cursor-s-resize": "cursor:s-resize;", - "X .cursor-se-resize": "cursor:se-resize;", - "X .cursor-w-resize": "cursor:w-resize;", - "X .cursor-e-resize": "cursor:e-resize;", - "X .cursor-nw-resize": "cursor:nw-resize;", - "X .cursor-n-resize": "cursor:n-resize;", - "X .cursor-ne-resize": "cursor:ne-resize;", - "X .modebar": "position:absolute;top:2px;right:2px;z-index:1001;background:rgba(255,255,255,0.7);", - "X .modebar--hover": "opacity:0;-webkit-transition:opacity 0.3s ease 0s;-moz-transition:opacity 0.3s ease 0s;-ms-transition:opacity 0.3s ease 0s;-o-transition:opacity 0.3s ease 0s;transition:opacity 0.3s ease 0s;", - "X:hover .modebar--hover": "opacity:1;", - "X .modebar-group": "float:left;display:inline-block;box-sizing:border-box;margin-left:8px;position:relative;vertical-align:middle;white-space:nowrap;", - "X .modebar-group:first-child": "margin-left:0px;", - "X .modebar-btn": "position:relative;font-size:16px;padding:3px 4px;cursor:pointer;line-height:normal;box-sizing:border-box;", - "X .modebar-btn svg": "position:relative;top:2px;", - "X .modebar-btn path": "fill:rgba(0,31,95,0.3);", - "X .modebar-btn.active path,X .modebar-btn:hover path": "fill:rgba(0,22,72,0.5);", - "X .modebar-btn.modebar-btn--logo": "padding:3px 1px;", - "X .modebar-btn.modebar-btn--logo path": "fill:#447adb !important;", - "X [data-title]:before,X [data-title]:after": "position:absolute;-webkit-transform:translate3d(0, 0, 0);-moz-transform:translate3d(0, 0, 0);-ms-transform:translate3d(0, 0, 0);-o-transform:translate3d(0, 0, 0);transform:translate3d(0, 0, 0);display:none;opacity:0;z-index:1001;pointer-events:none;top:110%;right:50%;", - "X [data-title]:hover:before,X [data-title]:hover:after": "display:block;opacity:1;", - "X [data-title]:before": "content:'';position:absolute;background:transparent;border:6px solid transparent;z-index:1002;margin-top:-12px;border-bottom-color:#69738a;margin-right:-6px;", - "X [data-title]:after": "content:attr(data-title);background:#69738a;color:white;padding:8px 10px;font-size:12px;line-height:12px;white-space:nowrap;margin-right:-18px;border-radius:2px;", - "X .select-outline": "fill:none;stroke-width:1;shape-rendering:crispEdges;", - "X .select-outline-1": "stroke:white;", - "X .select-outline-2": "stroke:black;stroke-dasharray:2px 2px;", - Y: "font-family:'Open Sans';position:fixed;top:50px;right:20px;z-index:10000;font-size:10pt;max-width:180px;", - "Y p": "margin:0;", - "Y .notifier-note": "min-width:180px;max-width:250px;border:1px solid #fff;z-index:3000;margin:0;background-color:#8c97af;background-color:rgba(140,151,175,0.9);color:#fff;padding:10px;", - "Y .notifier-close": "color:#fff;opacity:0.8;float:right;padding:0 5px;background:none;border:none;font-size:20px;font-weight:bold;line-height:20px;", - "Y .notifier-close:hover": "color:#444;text-decoration:none;cursor:pointer;" -}; +var sort = require('./sort'); +var range = require('./range'); +var within = require('./within'); -for(var selector in rules) { - var fullSelector = selector.replace(/^,/,' ,') - .replace(/X/g, '.js-plotly-plot .plotly') - .replace(/Y/g, '.plotly-notifier'); - Lib.addStyleRule(fullSelector, rules[selector]); +module.exports = kdbush; + +function kdbush(points, getX, getY, nodeSize, ArrayType) { + return new KDBush(points, getX, getY, nodeSize, ArrayType); } -},{"../src/lib":1261}],91:[function(require,module,exports){ -'use strict'; +function KDBush(points, getX, getY, nodeSize, ArrayType) { + getX = getX || defaultGetX; + getY = getY || defaultGetY; + ArrayType = ArrayType || Array; -module.exports = { - 'undo': { - 'width': 857.1, - 'path': 'm857 350q0-87-34-166t-91-137-137-92-166-34q-96 0-183 41t-147 114q-4 6-4 13t5 11l76 77q6 5 14 5 9-1 13-7 41-53 100-82t126-29q58 0 110 23t92 61 61 91 22 111-22 111-61 91-92 61-110 23q-55 0-105-20t-90-57l77-77q17-16 8-38-10-23-33-23h-250q-15 0-25 11t-11 25v250q0 24 22 33 22 10 39-8l72-72q60 57 137 88t159 31q87 0 166-34t137-92 91-137 34-166z', - 'ascent': 850, - 'descent': -150 + this.nodeSize = nodeSize || 64; + this.points = points; + + this.ids = new ArrayType(points.length); + this.coords = new ArrayType(points.length * 2); + + for (var i = 0; i < points.length; i++) { + this.ids[i] = i; + this.coords[2 * i] = getX(points[i]); + this.coords[2 * i + 1] = getY(points[i]); + } + + sort(this.ids, this.coords, this.nodeSize, 0, this.ids.length - 1, 0); +} + +KDBush.prototype = { + range: function (minX, minY, maxX, maxY) { + return range(this.ids, this.coords, minX, minY, maxX, maxY, this.nodeSize); }, - 'home': { - 'width': 928.6, - 'path': 'm786 296v-267q0-15-11-26t-25-10h-214v214h-143v-214h-214q-15 0-25 10t-11 26v267q0 1 0 2t0 2l321 264 321-264q1-1 1-4z m124 39l-34-41q-5-5-12-6h-2q-7 0-12 3l-386 322-386-322q-7-4-13-4-7 2-12 7l-35 41q-4 5-3 13t6 12l401 334q18 15 42 15t43-15l136-114v109q0 8 5 13t13 5h107q8 0 13-5t5-13v-227l122-102q5-5 6-12t-4-13z', - 'ascent': 850, - 'descent': -150 - }, - 'camera-retro': { - 'width': 1000, - 'path': 'm518 386q0 8-5 13t-13 5q-37 0-63-27t-26-63q0-8 5-13t13-5 12 5 5 13q0 23 16 38t38 16q8 0 13 5t5 13z m125-73q0-59-42-101t-101-42-101 42-42 101 42 101 101 42 101-42 42-101z m-572-320h858v71h-858v-71z m643 320q0 89-62 152t-152 62-151-62-63-152 63-151 151-63 152 63 62 151z m-571 358h214v72h-214v-72z m-72-107h858v143h-462l-36-71h-360v-72z m929 143v-714q0-30-21-51t-50-21h-858q-29 0-50 21t-21 51v714q0 30 21 51t50 21h858q29 0 50-21t21-51z', - 'ascent': 850, - 'descent': -150 - }, - 'zoombox': { - 'width': 1000, - 'path': 'm1000-25l-250 251c40 63 63 138 63 218 0 224-182 406-407 406-224 0-406-182-406-406s183-406 407-406c80 0 155 22 218 62l250-250 125 125z m-812 250l0 438 437 0 0-438-437 0z m62 375l313 0 0-312-313 0 0 312z', - 'ascent': 850, - 'descent': -150 - }, - 'pan': { - 'width': 1000, - 'path': 'm1000 350l-187 188 0-125-250 0 0 250 125 0-188 187-187-187 125 0 0-250-250 0 0 125-188-188 186-187 0 125 252 0 0-250-125 0 187-188 188 188-125 0 0 250 250 0 0-126 187 188z', - 'ascent': 850, - 'descent': -150 - }, - 'zoom_plus': { - 'width': 1000, - 'path': 'm1 787l0-875 875 0 0 875-875 0z m687-500l-187 0 0-187-125 0 0 187-188 0 0 125 188 0 0 187 125 0 0-187 187 0 0-125z', - 'ascent': 850, - 'descent': -150 - }, - 'zoom_minus': { - 'width': 1000, - 'path': 'm0 788l0-876 875 0 0 876-875 0z m688-500l-500 0 0 125 500 0 0-125z', - 'ascent': 850, - 'descent': -150 - }, - 'autoscale': { - 'width': 1000, - 'path': 'm250 850l-187 0-63 0 0-62 0-188 63 0 0 188 187 0 0 62z m688 0l-188 0 0-62 188 0 0-188 62 0 0 188 0 62-62 0z m-875-938l0 188-63 0 0-188 0-62 63 0 187 0 0 62-187 0z m875 188l0-188-188 0 0-62 188 0 62 0 0 62 0 188-62 0z m-125 188l-1 0-93-94-156 156 156 156 92-93 2 0 0 250-250 0 0-2 93-92-156-156-156 156 94 92 0 2-250 0 0-250 0 0 93 93 157-156-157-156-93 94 0 0 0-250 250 0 0 0-94 93 156 157 156-157-93-93 0 0 250 0 0 250z', - 'ascent': 850, - 'descent': -150 - }, - 'tooltip_basic': { - 'width': 1500, - 'path': 'm375 725l0 0-375-375 375-374 0-1 1125 0 0 750-1125 0z', - 'ascent': 850, - 'descent': -150 - }, - 'tooltip_compare': { - 'width': 1125, - 'path': 'm187 786l0 2-187-188 188-187 0 0 937 0 0 373-938 0z m0-499l0 1-187-188 188-188 0 0 937 0 0 376-938-1z', - 'ascent': 850, - 'descent': -150 - }, - 'plotlylogo': { - 'width': 1542, - 'path': 'm0-10h182v-140h-182v140z m228 146h183v-286h-183v286z m225 714h182v-1000h-182v1000z m225-285h182v-715h-182v715z m225 142h183v-857h-183v857z m231-428h182v-429h-182v429z m225-291h183v-138h-183v138z', - 'ascent': 850, - 'descent': -150 - }, - 'z-axis': { - 'width': 1000, - 'path': 'm833 5l-17 108v41l-130-65 130-66c0 0 0 38 0 39 0-1 36-14 39-25 4-15-6-22-16-30-15-12-39-16-56-20-90-22-187-23-279-23-261 0-341 34-353 59 3 60 228 110 228 110-140-8-351-35-351-116 0-120 293-142 474-142 155 0 477 22 477 142 0 50-74 79-163 96z m-374 94c-58-5-99-21-99-40 0-24 65-43 144-43 79 0 143 19 143 43 0 19-42 34-98 40v216h87l-132 135-133-135h88v-216z m167 515h-136v1c16 16 31 34 46 52l84 109v54h-230v-71h124v-1c-16-17-28-32-44-51l-89-114v-51h245v72z', - 'ascent': 850, - 'descent': -150 - }, - '3d_rotate': { - 'width': 1000, - 'path': 'm922 660c-5 4-9 7-14 11-359 263-580-31-580-31l-102 28 58-400c0 1 1 1 2 2 118 108 351 249 351 249s-62 27-100 42c88 83 222 183 347 122 16-8 30-17 44-27-2 1-4 2-6 4z m36-329c0 0 64 229-88 296-62 27-124 14-175-11 157-78 225-208 249-266 8-19 11-31 11-31 2 5 6 15 11 32-5-13-8-20-8-20z m-775-239c70-31 117-50 198-32-121 80-199 346-199 346l-96-15-58-12c0 0 55-226 155-287z m603 133l-317-139c0 0 4-4 19-14 7-5 24-15 24-15s-177-147-389 4c235-287 536-112 536-112l31-22 100 299-4-1z m-298-153c6-4 14-9 24-15 0 0-17 10-24 15z', - 'ascent': 850, - 'descent': -150 - }, - 'camera': { - 'width': 1000, - 'path': 'm500 450c-83 0-150-67-150-150 0-83 67-150 150-150 83 0 150 67 150 150 0 83-67 150-150 150z m400 150h-120c-16 0-34 13-39 29l-31 93c-6 15-23 28-40 28h-340c-16 0-34-13-39-28l-31-94c-6-15-23-28-40-28h-120c-55 0-100-45-100-100v-450c0-55 45-100 100-100h800c55 0 100 45 100 100v450c0 55-45 100-100 100z m-400-550c-138 0-250 112-250 250 0 138 112 250 250 250 138 0 250-112 250-250 0-138-112-250-250-250z m365 380c-19 0-35 16-35 35 0 19 16 35 35 35 19 0 35-16 35-35 0-19-16-35-35-35z', - 'ascent': 850, - 'descent': -150 - }, - 'movie': { - 'width': 1000, - 'path': 'm938 413l-188-125c0 37-17 71-44 94 64 38 107 107 107 187 0 121-98 219-219 219-121 0-219-98-219-219 0-61 25-117 66-156h-115c30 33 49 76 49 125 0 103-84 187-187 187s-188-84-188-187c0-57 26-107 65-141-38-22-65-62-65-109v-250c0-70 56-126 125-126h500c69 0 125 56 125 126l188-126c34 0 62 28 62 63v375c0 35-28 63-62 63z m-750 0c-69 0-125 56-125 125s56 125 125 125 125-56 125-125-56-125-125-125z m406-1c-87 0-157 70-157 157 0 86 70 156 157 156s156-70 156-156-70-157-156-157z', - 'ascent': 850, - 'descent': -150 - }, - 'question': { - 'width': 857.1, - 'path': 'm500 82v107q0 8-5 13t-13 5h-107q-8 0-13-5t-5-13v-107q0-8 5-13t13-5h107q8 0 13 5t5 13z m143 375q0 49-31 91t-77 65-95 23q-136 0-207-119-9-14 4-24l74-55q4-4 10-4 9 0 14 7 30 38 48 51 19 14 48 14 27 0 48-15t21-33q0-21-11-34t-38-25q-35-16-65-48t-29-70v-20q0-8 5-13t13-5h107q8 0 13 5t5 13q0 10 12 27t30 28q18 10 28 16t25 19 25 27 16 34 7 45z m214-107q0-117-57-215t-156-156-215-58-216 58-155 156-58 215 58 215 155 156 216 58 215-58 156-156 57-215z', - 'ascent': 850, - 'descent': -150 - }, - 'disk': { - 'width': 857.1, - 'path': 'm214-7h429v214h-429v-214z m500 0h72v500q0 8-6 21t-11 20l-157 156q-5 6-19 12t-22 5v-232q0-22-15-38t-38-16h-322q-22 0-37 16t-16 38v232h-72v-714h72v232q0 22 16 38t37 16h465q22 0 38-16t15-38v-232z m-214 518v178q0 8-5 13t-13 5h-107q-7 0-13-5t-5-13v-178q0-8 5-13t13-5h107q7 0 13 5t5 13z m357-18v-518q0-22-15-38t-38-16h-750q-23 0-38 16t-16 38v750q0 22 16 38t38 16h517q23 0 50-12t42-26l156-157q16-15 27-42t11-49z', - 'ascent': 850, - 'descent': -150 - }, - 'lasso': { - 'width': 1031, - 'path': 'm1018 538c-36 207-290 336-568 286-277-48-473-256-436-463 10-57 36-108 76-151-13-66 11-137 68-183 34-28 75-41 114-42l-55-70 0 0c-2-1-3-2-4-3-10-14-8-34 5-45 14-11 34-8 45 4 1 1 2 3 2 5l0 0 113 140c16 11 31 24 45 40 4 3 6 7 8 11 48-3 100 0 151 9 278 48 473 255 436 462z m-624-379c-80 14-149 48-197 96 42 42 109 47 156 9 33-26 47-66 41-105z m-187-74c-19 16-33 37-39 60 50-32 109-55 174-68-42-25-95-24-135 8z m360 75c-34-7-69-9-102-8 8 62-16 128-68 170-73 59-175 54-244-5-9 20-16 40-20 61-28 159 121 317 333 354s407-60 434-217c28-159-121-318-333-355z', - 'ascent': 850, - 'descent': -150 - }, - 'selectbox': { - 'width': 1000, - 'path': 'm0 850l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m285 0l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m-857-286l0-143 143 0 0 143-143 0z m857 0l0-143 143 0 0 143-143 0z m-857-285l0-143 143 0 0 143-143 0z m857 0l0-143 143 0 0 143-143 0z m-857-286l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m285 0l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z', - 'ascent': 850, - 'descent': -150 + + within: function (x, y, r) { + return within(this.ids, this.coords, x, y, r, this.nodeSize); } }; -},{}],92:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ +function defaultGetX(p) { return p[0]; } +function defaultGetY(p) { return p[1]; } +},{"./range":275,"./sort":276,"./within":277}],275:[function(require,module,exports){ 'use strict'; -module.exports = require('../src/traces/bar'); - -},{"../src/traces/bar":1382}],93:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/box'); - -},{"../src/traces/box":1394}],94:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/components/calendars'); - -},{"../src/components/calendars":1159}],95:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/candlestick'); - -},{"../src/traces/candlestick":1402}],96:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/choropleth'); - -},{"../src/traces/choropleth":1407}],97:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/contour'); - -},{"../src/traces/contour":1416}],98:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/core'); - -},{"../src/core":1247}],99:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/transforms/filter'); - -},{"../src/transforms/filter":1547}],100:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/transforms/groupby'); - -},{"../src/transforms/groupby":1548}],101:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/heatmap'); - -},{"../src/traces/heatmap":1431}],102:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/heatmapgl'); - -},{"../src/traces/heatmapgl":1440}],103:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/histogram'); - -},{"../src/traces/histogram":1448}],104:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/histogram2d'); - -},{"../src/traces/histogram2d":1453}],105:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/histogram2dcontour'); - -},{"../src/traces/histogram2dcontour":1457}],106:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -var Plotly = require('./core'); - -// traces -Plotly.register([ - require('./bar'), - require('./box'), - require('./heatmap'), - require('./histogram'), - require('./histogram2d'), - require('./histogram2dcontour'), - require('./pie'), - require('./contour'), - require('./scatterternary'), - - require('./scatter3d'), - require('./surface'), - require('./mesh3d'), - - require('./scattergeo'), - require('./choropleth'), - - require('./scattergl'), - require('./pointcloud'), - require('./heatmapgl'), - - require('./scattermapbox'), - - require('./ohlc'), - require('./candlestick') -]); - -// transforms -// -// Please note that all *transform* methods are executed before -// all *calcTransform* methods - which could possibly lead to -// unexpected results when applying multiple transforms of different types -// to a given trace. -// -// For more info, see: -// https://github.com/plotly/plotly.js/pull/978#pullrequestreview-2403353 -// -Plotly.register([ - require('./filter'), - require('./groupby') -]); - -// components -Plotly.register([ - require('./calendars') -]); - -module.exports = Plotly; - -},{"./bar":92,"./box":93,"./calendars":94,"./candlestick":95,"./choropleth":96,"./contour":97,"./core":98,"./filter":99,"./groupby":100,"./heatmap":101,"./heatmapgl":102,"./histogram":103,"./histogram2d":104,"./histogram2dcontour":105,"./mesh3d":107,"./ohlc":108,"./pie":109,"./pointcloud":110,"./scatter3d":111,"./scattergeo":112,"./scattergl":113,"./scattermapbox":114,"./scatterternary":115,"./surface":116}],107:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/mesh3d'); - -},{"../src/traces/mesh3d":1461}],108:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/ohlc'); - -},{"../src/traces/ohlc":1466}],109:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/pie'); - -},{"../src/traces/pie":1474}],110:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/pointcloud'); - -},{"../src/traces/pointcloud":1483}],111:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/scatter3d'); - -},{"../src/traces/scatter3d":1513}],112:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/scattergeo'); - -},{"../src/traces/scattergeo":1519}],113:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/scattergl'); - -},{"../src/traces/scattergl":1524}],114:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/scattermapbox'); - -},{"../src/traces/scattermapbox":1531}],115:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/scatterternary'); - -},{"../src/traces/scatterternary":1537}],116:[function(require,module,exports){ -/** -* Copyright 2012-2016, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - -'use strict'; - -module.exports = require('../src/traces/surface'); - -},{"../src/traces/surface":1546}],117:[function(require,module,exports){ -'use strict' - -var bsearch = require('binary-search-bounds') -var m4interp = require('mat4-interpolate') -var invert44 = require('gl-mat4/invert') -var rotateX = require('gl-mat4/rotateX') -var rotateY = require('gl-mat4/rotateY') -var rotateZ = require('gl-mat4/rotateZ') -var lookAt = require('gl-mat4/lookAt') -var translate = require('gl-mat4/translate') -var scale = require('gl-mat4/scale') -var normalize = require('gl-vec3/normalize') - -var DEFAULT_CENTER = [0,0,0] - -module.exports = createMatrixCameraController - -function MatrixCameraController(initialMatrix) { - this._components = initialMatrix.slice() - this._time = [0] - this.prevMatrix = initialMatrix.slice() - this.nextMatrix = initialMatrix.slice() - this.computedMatrix = initialMatrix.slice() - this.computedInverse = initialMatrix.slice() - this.computedEye = [0,0,0] - this.computedUp = [0,0,0] - this.computedCenter = [0,0,0] - this.computedRadius = [0] - this._limits = [-Infinity, Infinity] -} - -var proto = MatrixCameraController.prototype - -proto.recalcMatrix = function(t) { - var time = this._time - var tidx = bsearch.le(time, t) - var mat = this.computedMatrix - if(tidx < 0) { - return - } - var comps = this._components - if(tidx === time.length-1) { - var ptr = 16*tidx - for(var i=0; i<16; ++i) { - mat[i] = comps[ptr++] - } - } else { - var dt = (time[tidx+1] - time[tidx]) - var ptr = 16*tidx - var prev = this.prevMatrix - var allEqual = true - for(var i=0; i<16; ++i) { - prev[i] = comps[ptr++] - } - var next = this.nextMatrix - for(var i=0; i<16; ++i) { - next[i] = comps[ptr++] - allEqual = allEqual && (prev[i] === next[i]) - } - if(dt < 1e-6 || allEqual) { - for(var i=0; i<16; ++i) { - mat[i] = prev[i] - } - } else { - m4interp(mat, prev, next, (t - time[tidx])/dt) - } - } - - var up = this.computedUp - up[0] = mat[1] - up[1] = mat[5] - up[2] = mat[6] - normalize(up, up) - - var imat = this.computedInverse - invert44(imat, mat) - var eye = this.computedEye - var w = imat[15] - eye[0] = imat[12]/w - eye[1] = imat[13]/w - eye[2] = imat[14]/w - - var center = this.computedCenter - var radius = Math.exp(this.computedRadius[0]) - for(var i=0; i<3; ++i) { - center[i] = eye[i] - mat[2+4*i] * radius - } -} - -proto.idle = function(t) { - if(t < this.lastT()) { - return - } - var mc = this._components - var ptr = mc.length-16 - for(var i=0; i<16; ++i) { - mc.push(mc[ptr++]) - } - this._time.push(t) -} - -proto.flush = function(t) { - var idx = bsearch.gt(this._time, t) - 2 - if(idx < 0) { - return - } - this._time.slice(0, idx) - this._components.slice(0, 16*idx) -} - -proto.lastT = function() { - return this._time[this._time.length-1] -} - -proto.lookAt = function(t, eye, center, up) { - this.recalcMatrix(t) - eye = eye || this.computedEye - center = center || DEFAULT_CENTER - up = up || this.computedUp - this.setMatrix(t, lookAt(this.computedMatrix, eye, center, up)) - var d2 = 0.0 - for(var i=0; i<3; ++i) { - d2 += Math.pow(center[i] - eye[i], 2) - } - d2 = Math.log(Math.sqrt(d2)) - this.computedRadius[0] = d2 -} - -proto.rotate = function(t, yaw, pitch, roll) { - this.recalcMatrix(t) - var mat = this.computedInverse - if(yaw) rotateY(mat, mat, yaw) - if(pitch) rotateX(mat, mat, pitch) - if(roll) rotateZ(mat, mat, roll) - this.setMatrix(t, invert44(this.computedMatrix, mat)) -} - -var tvec = [0,0,0] - -proto.pan = function(t, dx, dy, dz) { - tvec[0] = -(dx || 0.0) - tvec[1] = -(dy || 0.0) - tvec[2] = -(dz || 0.0) - this.recalcMatrix(t) - var mat = this.computedInverse - translate(mat, mat, tvec) - this.setMatrix(t, invert44(mat, mat)) -} - -proto.translate = function(t, dx, dy, dz) { - tvec[0] = dx || 0.0 - tvec[1] = dy || 0.0 - tvec[2] = dz || 0.0 - this.recalcMatrix(t) - var mat = this.computedMatrix - translate(mat, mat, tvec) - this.setMatrix(t, mat) -} - -proto.setMatrix = function(t, mat) { - if(t < this.lastT()) { - return - } - this._time.push(t) - for(var i=0; i<16; ++i) { - this._components.push(mat[i]) - } -} - -proto.setDistance = function(t, d) { - this.computedRadius[0] = d -} - -proto.setDistanceLimits = function(a,b) { - var lim = this._limits - lim[0] = a - lim[1] = b -} - -proto.getDistanceLimits = function(out) { - var lim = this._limits - if(out) { - out[0] = lim[0] - out[1] = lim[1] - return out - } - return lim -} - -function createMatrixCameraController(options) { - options = options || {} - var matrix = options.matrix || - [1,0,0,0, - 0,1,0,0, - 0,0,1,0, - 0,0,0,1] - return new MatrixCameraController(matrix) -} -},{"binary-search-bounds":118,"gl-mat4/invert":235,"gl-mat4/lookAt":236,"gl-mat4/rotateX":240,"gl-mat4/rotateY":241,"gl-mat4/rotateZ":242,"gl-mat4/scale":243,"gl-mat4/translate":244,"gl-vec3/normalize":123,"mat4-interpolate":124}],118:[function(require,module,exports){ -"use strict" - -function compileSearch(funcName, predicate, reversed, extraArgs, useNdarray, earlyOut) { - var code = [ - "function ", funcName, "(a,l,h,", extraArgs.join(","), "){", -earlyOut ? "" : "var i=", (reversed ? "l-1" : "h+1"), -";while(l<=h){\ -var m=(l+h)>>>1,x=a", useNdarray ? ".get(m)" : "[m]"] - if(earlyOut) { - if(predicate.indexOf("c") < 0) { - code.push(";if(x===y){return m}else if(x<=y){") - } else { - code.push(";var p=c(x,y);if(p===0){return m}else if(p<=0){") - } - } else { - code.push(";if(", predicate, "){i=m;") - } - if(reversed) { - code.push("l=m+1}else{h=m-1}") - } else { - code.push("h=m-1}else{l=m+1}") - } - code.push("}") - if(earlyOut) { - code.push("return -1};") - } else { - code.push("return i};") - } - return code.join("") -} - -function compileBoundsSearch(predicate, reversed, suffix, earlyOut) { - var result = new Function([ - compileSearch("A", "x" + predicate + "y", reversed, ["y"], false, earlyOut), - compileSearch("B", "x" + predicate + "y", reversed, ["y"], true, earlyOut), - compileSearch("P", "c(x,y)" + predicate + "0", reversed, ["y", "c"], false, earlyOut), - compileSearch("Q", "c(x,y)" + predicate + "0", reversed, ["y", "c"], true, earlyOut), -"function dispatchBsearch", suffix, "(a,y,c,l,h){\ -if(a.shape){\ -if(typeof(c)==='function'){\ -return Q(a,(l===undefined)?0:l|0,(h===undefined)?a.shape[0]-1:h|0,y,c)\ -}else{\ -return B(a,(c===undefined)?0:c|0,(l===undefined)?a.shape[0]-1:l|0,y)\ -}}else{\ -if(typeof(c)==='function'){\ -return P(a,(l===undefined)?0:l|0,(h===undefined)?a.length-1:h|0,y,c)\ -}else{\ -return A(a,(c===undefined)?0:c|0,(l===undefined)?a.length-1:l|0,y)\ -}}}\ -return dispatchBsearch", suffix].join("")) - return result() -} - -module.exports = { - ge: compileBoundsSearch(">=", false, "GE"), - gt: compileBoundsSearch(">", false, "GT"), - lt: compileBoundsSearch("<", true, "LT"), - le: compileBoundsSearch("<=", true, "LE"), - eq: compileBoundsSearch("-", true, "EQ", true) -} - -},{}],119:[function(require,module,exports){ -module.exports = cross; - -/** - * Computes the cross product of two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -function cross(out, a, b) { - var ax = a[0], ay = a[1], az = a[2], - bx = b[0], by = b[1], bz = b[2] - - out[0] = ay * bz - az * by - out[1] = az * bx - ax * bz - out[2] = ax * by - ay * bx - return out -} -},{}],120:[function(require,module,exports){ -module.exports = dot; - -/** - * Calculates the dot product of two vec3's - * - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {Number} dot product of a and b - */ -function dot(a, b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] -} -},{}],121:[function(require,module,exports){ -module.exports = length; - -/** - * Calculates the length of a vec3 - * - * @param {vec3} a vector to calculate length of - * @returns {Number} length of a - */ -function length(a) { - var x = a[0], - y = a[1], - z = a[2] - return Math.sqrt(x*x + y*y + z*z) -} -},{}],122:[function(require,module,exports){ -module.exports = lerp; - -/** - * Performs a linear interpolation between two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @param {Number} t interpolation amount between the two inputs - * @returns {vec3} out - */ -function lerp(out, a, b, t) { - var ax = a[0], - ay = a[1], - az = a[2] - out[0] = ax + t * (b[0] - ax) - out[1] = ay + t * (b[1] - ay) - out[2] = az + t * (b[2] - az) - return out -} -},{}],123:[function(require,module,exports){ -module.exports = normalize; - -/** - * Normalize a vec3 - * - * @param {vec3} out the receiving vector - * @param {vec3} a vector to normalize - * @returns {vec3} out - */ -function normalize(out, a) { - var x = a[0], - y = a[1], - z = a[2] - var len = x*x + y*y + z*z - if (len > 0) { - //TODO: evaluate use of glm_invsqrt here? - len = 1 / Math.sqrt(len) - out[0] = a[0] * len - out[1] = a[1] * len - out[2] = a[2] * len - } - return out -} -},{}],124:[function(require,module,exports){ -var lerp = require('gl-vec3/lerp') - -var recompose = require('mat4-recompose') -var decompose = require('mat4-decompose') -var determinant = require('gl-mat4/determinant') -var slerp = require('quat-slerp') - -var state0 = state() -var state1 = state() -var tmp = state() - -module.exports = interpolate -function interpolate(out, start, end, alpha) { - if (determinant(start) === 0 || determinant(end) === 0) - return false - - //decompose the start and end matrices into individual components - var r0 = decompose(start, state0.translate, state0.scale, state0.skew, state0.perspective, state0.quaternion) - var r1 = decompose(end, state1.translate, state1.scale, state1.skew, state1.perspective, state1.quaternion) - if (!r0 || !r1) - return false - - - //now lerp/slerp the start and end components into a temporary lerp(tmptranslate, state0.translate, state1.translate, alpha) - lerp(tmp.translate, state0.translate, state1.translate, alpha) - lerp(tmp.skew, state0.skew, state1.skew, alpha) - lerp(tmp.scale, state0.scale, state1.scale, alpha) - lerp(tmp.perspective, state0.perspective, state1.perspective, alpha) - slerp(tmp.quaternion, state0.quaternion, state1.quaternion, alpha) - - //and recompose into our 'out' matrix - recompose(out, tmp.translate, tmp.scale, tmp.skew, tmp.perspective, tmp.quaternion) - return true -} - -function state() { - return { - translate: vec3(), - scale: vec3(1), - skew: vec3(), - perspective: vec4(), - quaternion: vec4() - } -} - -function vec3(n) { - return [n||0,n||0,n||0] -} - -function vec4() { - return [0,0,0,1] -} -},{"gl-mat4/determinant":231,"gl-vec3/lerp":122,"mat4-decompose":125,"mat4-recompose":127,"quat-slerp":128}],125:[function(require,module,exports){ -/*jshint unused:true*/ -/* -Input: matrix ; a 4x4 matrix -Output: translation ; a 3 component vector - scale ; a 3 component vector - skew ; skew factors XY,XZ,YZ represented as a 3 component vector - perspective ; a 4 component vector - quaternion ; a 4 component vector -Returns false if the matrix cannot be decomposed, true if it can - - -References: -https://github.com/kamicane/matrix3d/blob/master/lib/Matrix3d.js -https://github.com/ChromiumWebApps/chromium/blob/master/ui/gfx/transform_util.cc -http://www.w3.org/TR/css3-transforms/#decomposing-a-3d-matrix -*/ - -var normalize = require('./normalize') - -var create = require('gl-mat4/create') -var clone = require('gl-mat4/clone') -var determinant = require('gl-mat4/determinant') -var invert = require('gl-mat4/invert') -var transpose = require('gl-mat4/transpose') -var vec3 = { - length: require('gl-vec3/length'), - normalize: require('gl-vec3/normalize'), - dot: require('gl-vec3/dot'), - cross: require('gl-vec3/cross') -} - -var tmp = create() -var perspectiveMatrix = create() -var tmpVec4 = [0, 0, 0, 0] -var row = [ [0,0,0], [0,0,0], [0,0,0] ] -var pdum3 = [0,0,0] - -module.exports = function decomposeMat4(matrix, translation, scale, skew, perspective, quaternion) { - if (!translation) translation = [0,0,0] - if (!scale) scale = [0,0,0] - if (!skew) skew = [0,0,0] - if (!perspective) perspective = [0,0,0,1] - if (!quaternion) quaternion = [0,0,0,1] - - //normalize, if not possible then bail out early - if (!normalize(tmp, matrix)) - return false - - // perspectiveMatrix is used to solve for perspective, but it also provides - // an easy way to test for singularity of the upper 3x3 component. - clone(perspectiveMatrix, tmp) - - perspectiveMatrix[3] = 0 - perspectiveMatrix[7] = 0 - perspectiveMatrix[11] = 0 - perspectiveMatrix[15] = 1 - - // If the perspectiveMatrix is not invertible, we are also unable to - // decompose, so we'll bail early. Constant taken from SkMatrix44::invert. - if (Math.abs(determinant(perspectiveMatrix) < 1e-8)) - return false - - var a03 = tmp[3], a13 = tmp[7], a23 = tmp[11], - a30 = tmp[12], a31 = tmp[13], a32 = tmp[14], a33 = tmp[15] - - // First, isolate perspective. - if (a03 !== 0 || a13 !== 0 || a23 !== 0) { - tmpVec4[0] = a03 - tmpVec4[1] = a13 - tmpVec4[2] = a23 - tmpVec4[3] = a33 - - // Solve the equation by inverting perspectiveMatrix and multiplying - // rightHandSide by the inverse. - // resuing the perspectiveMatrix here since it's no longer needed - var ret = invert(perspectiveMatrix, perspectiveMatrix) - if (!ret) return false - transpose(perspectiveMatrix, perspectiveMatrix) - - //multiply by transposed inverse perspective matrix, into perspective vec4 - vec4multMat4(perspective, tmpVec4, perspectiveMatrix) - } else { - //no perspective - perspective[0] = perspective[1] = perspective[2] = 0 - perspective[3] = 1 - } - - // Next take care of translation - translation[0] = a30 - translation[1] = a31 - translation[2] = a32 - - // Now get scale and shear. 'row' is a 3 element array of 3 component vectors - mat3from4(row, tmp) - - // Compute X scale factor and normalize first row. - scale[0] = vec3.length(row[0]) - vec3.normalize(row[0], row[0]) - - // Compute XY shear factor and make 2nd row orthogonal to 1st. - skew[0] = vec3.dot(row[0], row[1]) - combine(row[1], row[1], row[0], 1.0, -skew[0]) - - // Now, compute Y scale and normalize 2nd row. - scale[1] = vec3.length(row[1]) - vec3.normalize(row[1], row[1]) - skew[0] /= scale[1] - - // Compute XZ and YZ shears, orthogonalize 3rd row - skew[1] = vec3.dot(row[0], row[2]) - combine(row[2], row[2], row[0], 1.0, -skew[1]) - skew[2] = vec3.dot(row[1], row[2]) - combine(row[2], row[2], row[1], 1.0, -skew[2]) - - // Next, get Z scale and normalize 3rd row. - scale[2] = vec3.length(row[2]) - vec3.normalize(row[2], row[2]) - skew[1] /= scale[2] - skew[2] /= scale[2] - - - // At this point, the matrix (in rows) is orthonormal. - // Check for a coordinate system flip. If the determinant - // is -1, then negate the matrix and the scaling factors. - vec3.cross(pdum3, row[1], row[2]) - if (vec3.dot(row[0], pdum3) < 0) { - for (var i = 0; i < 3; i++) { - scale[i] *= -1; - row[i][0] *= -1 - row[i][1] *= -1 - row[i][2] *= -1 - } - } - - // Now, get the rotations out - quaternion[0] = 0.5 * Math.sqrt(Math.max(1 + row[0][0] - row[1][1] - row[2][2], 0)) - quaternion[1] = 0.5 * Math.sqrt(Math.max(1 - row[0][0] + row[1][1] - row[2][2], 0)) - quaternion[2] = 0.5 * Math.sqrt(Math.max(1 - row[0][0] - row[1][1] + row[2][2], 0)) - quaternion[3] = 0.5 * Math.sqrt(Math.max(1 + row[0][0] + row[1][1] + row[2][2], 0)) - - if (row[2][1] > row[1][2]) - quaternion[0] = -quaternion[0] - if (row[0][2] > row[2][0]) - quaternion[1] = -quaternion[1] - if (row[1][0] > row[0][1]) - quaternion[2] = -quaternion[2] - return true -} - -//will be replaced by gl-vec4 eventually -function vec4multMat4(out, a, m) { - var x = a[0], y = a[1], z = a[2], w = a[3]; - out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; - out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; - out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; - out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; - return out; -} - -//gets upper-left of a 4x4 matrix into a 3x3 of vectors -function mat3from4(out, mat4x4) { - out[0][0] = mat4x4[0] - out[0][1] = mat4x4[1] - out[0][2] = mat4x4[2] - - out[1][0] = mat4x4[4] - out[1][1] = mat4x4[5] - out[1][2] = mat4x4[6] - - out[2][0] = mat4x4[8] - out[2][1] = mat4x4[9] - out[2][2] = mat4x4[10] -} - -function combine(out, a, b, scale1, scale2) { - out[0] = a[0] * scale1 + b[0] * scale2 - out[1] = a[1] * scale1 + b[1] * scale2 - out[2] = a[2] * scale1 + b[2] * scale2 -} -},{"./normalize":126,"gl-mat4/clone":229,"gl-mat4/create":230,"gl-mat4/determinant":231,"gl-mat4/invert":235,"gl-mat4/transpose":245,"gl-vec3/cross":119,"gl-vec3/dot":120,"gl-vec3/length":121,"gl-vec3/normalize":123}],126:[function(require,module,exports){ -module.exports = function normalize(out, mat) { - var m44 = mat[15] - // Cannot normalize. - if (m44 === 0) - return false - var scale = 1 / m44 - for (var i=0; i<16; i++) - out[i] = mat[i] * scale - return true -} -},{}],127:[function(require,module,exports){ -/* -Input: translation ; a 3 component vector - scale ; a 3 component vector - skew ; skew factors XY,XZ,YZ represented as a 3 component vector - perspective ; a 4 component vector - quaternion ; a 4 component vector -Output: matrix ; a 4x4 matrix - -From: http://www.w3.org/TR/css3-transforms/#recomposing-to-a-3d-matrix -*/ - -var mat4 = { - identity: require('gl-mat4/identity'), - translate: require('gl-mat4/translate'), - multiply: require('gl-mat4/multiply'), - create: require('gl-mat4/create'), - scale: require('gl-mat4/scale'), - fromRotationTranslation: require('gl-mat4/fromRotationTranslation') -} - -var rotationMatrix = mat4.create() -var temp = mat4.create() - -module.exports = function recomposeMat4(matrix, translation, scale, skew, perspective, quaternion) { - mat4.identity(matrix) - - //apply translation & rotation - mat4.fromRotationTranslation(matrix, quaternion, translation) - - //apply perspective - matrix[3] = perspective[0] - matrix[7] = perspective[1] - matrix[11] = perspective[2] - matrix[15] = perspective[3] - - // apply skew - // temp is a identity 4x4 matrix initially - mat4.identity(temp) - - if (skew[2] !== 0) { - temp[9] = skew[2] - mat4.multiply(matrix, matrix, temp) - } - - if (skew[1] !== 0) { - temp[9] = 0 - temp[8] = skew[1] - mat4.multiply(matrix, matrix, temp) - } - - if (skew[0] !== 0) { - temp[8] = 0 - temp[4] = skew[0] - mat4.multiply(matrix, matrix, temp) - } - - //apply scale - mat4.scale(matrix, matrix, scale) - return matrix -} -},{"gl-mat4/create":230,"gl-mat4/fromRotationTranslation":233,"gl-mat4/identity":234,"gl-mat4/multiply":237,"gl-mat4/scale":243,"gl-mat4/translate":244}],128:[function(require,module,exports){ -module.exports = require('gl-quat/slerp') -},{"gl-quat/slerp":129}],129:[function(require,module,exports){ -module.exports = slerp - -/** - * Performs a spherical linear interpolation between two quat - * - * @param {quat} out the receiving quaternion - * @param {quat} a the first operand - * @param {quat} b the second operand - * @param {Number} t interpolation amount between the two inputs - * @returns {quat} out - */ -function slerp (out, a, b, t) { - // benchmarks: - // http://jsperf.com/quaternion-slerp-implementations - - var ax = a[0], ay = a[1], az = a[2], aw = a[3], - bx = b[0], by = b[1], bz = b[2], bw = b[3] - - var omega, cosom, sinom, scale0, scale1 - - // calc cosine - cosom = ax * bx + ay * by + az * bz + aw * bw - // adjust signs (if necessary) - if (cosom < 0.0) { - cosom = -cosom - bx = -bx - by = -by - bz = -bz - bw = -bw - } - // calculate coefficients - if ((1.0 - cosom) > 0.000001) { - // standard case (slerp) - omega = Math.acos(cosom) - sinom = Math.sin(omega) - scale0 = Math.sin((1.0 - t) * omega) / sinom - scale1 = Math.sin(t * omega) / sinom - } else { - // "from" and "to" quaternions are very close - // ... so we can do a linear interpolation - scale0 = 1.0 - t - scale1 = t - } - // calculate final values - out[0] = scale0 * ax + scale1 * bx - out[1] = scale0 * ay + scale1 * by - out[2] = scale0 * az + scale1 * bz - out[3] = scale0 * aw + scale1 * bw - - return out -} - -},{}],130:[function(require,module,exports){ -'use strict' - -module.exports = quatFromFrame - -function quatFromFrame( - out, - rx, ry, rz, - ux, uy, uz, - fx, fy, fz) { - var tr = rx + uy + fz - if(l > 0) { - var l = Math.sqrt(tr + 1.0) - out[0] = 0.5 * (uz - fy) / l - out[1] = 0.5 * (fx - rz) / l - out[2] = 0.5 * (ry - uy) / l - out[3] = 0.5 * l - } else { - var tf = Math.max(rx, uy, fz) - var l = Math.sqrt(2 * tf - tr + 1.0) - if(rx >= tf) { - //x y z order - out[0] = 0.5 * l - out[1] = 0.5 * (ux + ry) / l - out[2] = 0.5 * (fx + rz) / l - out[3] = 0.5 * (uz - fy) / l - } else if(uy >= tf) { - //y z x order - out[0] = 0.5 * (ry + ux) / l - out[1] = 0.5 * l - out[2] = 0.5 * (fy + uz) / l - out[3] = 0.5 * (fx - rz) / l - } else { - //z x y order - out[0] = 0.5 * (rz + fx) / l - out[1] = 0.5 * (uz + fy) / l - out[2] = 0.5 * l - out[3] = 0.5 * (ry - ux) / l - } - } - return out -} -},{}],131:[function(require,module,exports){ -'use strict' - -module.exports = createFilteredVector - -var cubicHermite = require('cubic-hermite') -var bsearch = require('binary-search-bounds') - -function clamp(lo, hi, x) { - return Math.min(hi, Math.max(lo, x)) -} - -function FilteredVector(state0, velocity0, t0) { - this.dimension = state0.length - this.bounds = [ new Array(this.dimension), new Array(this.dimension) ] - for(var i=0; i= n-1) { - var ptr = state.length-1 - var tf = t - time[n-1] - for(var i=0; i= n-1) { - var ptr = state.length-1 - var tf = t - time[n-1] - for(var i=0; i=0; --i) { - if(velocity[--ptr]) { - return false - } - } - return true -} - -proto.jump = function(t) { - var t0 = this.lastT() - var d = this.dimension - if(t < t0 || arguments.length !== d+1) { - return - } - var state = this._state - var velocity = this._velocity - var ptr = state.length-this.dimension - var bounds = this.bounds - var lo = bounds[0] - var hi = bounds[1] - this._time.push(t0, t) - for(var j=0; j<2; ++j) { - for(var i=0; i0; --i) { - state.push(clamp(lo[i-1], hi[i-1], arguments[i])) - velocity.push(0) - } -} - -proto.push = function(t) { - var t0 = this.lastT() - var d = this.dimension - if(t < t0 || arguments.length !== d+1) { - return - } - var state = this._state - var velocity = this._velocity - var ptr = state.length-this.dimension - var dt = t - t0 - var bounds = this.bounds - var lo = bounds[0] - var hi = bounds[1] - var sf = (dt > 1e-6) ? 1/dt : 0 - this._time.push(t) - for(var i=d; i>0; --i) { - var xc = clamp(lo[i-1], hi[i-1], arguments[i]) - state.push(xc) - velocity.push((xc - state[ptr++]) * sf) - } -} - -proto.set = function(t) { - var d = this.dimension - if(t < this.lastT() || arguments.length !== d+1) { - return - } - var state = this._state - var velocity = this._velocity - var bounds = this.bounds - var lo = bounds[0] - var hi = bounds[1] - this._time.push(t) - for(var i=d; i>0; --i) { - state.push(clamp(lo[i-1], hi[i-1], arguments[i])) - velocity.push(0) - } -} - -proto.move = function(t) { - var t0 = this.lastT() - var d = this.dimension - if(t <= t0 || arguments.length !== d+1) { - return - } - var state = this._state - var velocity = this._velocity - var statePtr = state.length - this.dimension - var bounds = this.bounds - var lo = bounds[0] - var hi = bounds[1] - var dt = t - t0 - var sf = (dt > 1e-6) ? 1/dt : 0.0 - this._time.push(t) - for(var i=d; i>0; --i) { - var dx = arguments[i] - state.push(clamp(lo[i-1], hi[i-1], state[statePtr++] + dx)) - velocity.push(dx * sf) - } -} - -proto.idle = function(t) { - var t0 = this.lastT() - if(t < t0) { - return - } - var d = this.dimension - var state = this._state - var velocity = this._velocity - var statePtr = state.length-d - var bounds = this.bounds - var lo = bounds[0] - var hi = bounds[1] - var dt = t - t0 - this._time.push(t) - for(var i=d-1; i>=0; --i) { - state.push(clamp(lo[i], hi[i], state[statePtr] + dt * velocity[statePtr])) - velocity.push(0) - statePtr += 1 - } -} - -function getZero(d) { - var result = new Array(d) - for(var i=0; i=0; --i) { - f[i] = dh00*p0[i] + dh10*v0[i] + dh01*p1[i] + dh11*v1[i] - } - return f - } - return dh00*p0 + dh10*v0 + dh01*p1[i] + dh11*v1 -} - -function cubicHermite(p0, v0, p1, v1, t, f) { - var ti = (t-1), t2 = t*t, ti2 = ti*ti, - h00 = (1+2*t)*ti2, - h10 = t*ti2, - h01 = t2*(3-2*t), - h11 = t2*ti - if(p0.length) { - if(!f) { - f = new Array(p0.length) - } - for(var i=p0.length-1; i>=0; --i) { - f[i] = h00*p0[i] + h10*v0[i] + h01*p1[i] + h11*v1[i] - } - return f - } - return h00*p0 + h10*v0 + h01*p1 + h11*v1 -} - -module.exports = cubicHermite -module.exports.derivative = dcubicHermite -},{}],134:[function(require,module,exports){ -'use strict' - -module.exports = createOrbitController - -var filterVector = require('filtered-vector') -var lookAt = require('gl-mat4/lookAt') -var mat4FromQuat = require('gl-mat4/fromQuat') -var invert44 = require('gl-mat4/invert') -var quatFromFrame = require('./lib/quatFromFrame') - -function len3(x,y,z) { - return Math.sqrt(Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2)) -} - -function len4(w,x,y,z) { - return Math.sqrt(Math.pow(w,2) + Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2)) -} - -function normalize4(out, a) { - var ax = a[0] - var ay = a[1] - var az = a[2] - var aw = a[3] - var al = len4(ax, ay, az, aw) - if(al > 1e-6) { - out[0] = ax/al - out[1] = ay/al - out[2] = az/al - out[3] = aw/al - } else { - out[0] = out[1] = out[2] = 0.0 - out[3] = 1.0 - } -} - -function OrbitCameraController(initQuat, initCenter, initRadius) { - this.radius = filterVector([initRadius]) - this.center = filterVector(initCenter) - this.rotation = filterVector(initQuat) - - this.computedRadius = this.radius.curve(0) - this.computedCenter = this.center.curve(0) - this.computedRotation = this.rotation.curve(0) - this.computedUp = [0.1,0,0] - this.computedEye = [0.1,0,0] - this.computedMatrix = [0.1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] - - this.recalcMatrix(0) -} - -var proto = OrbitCameraController.prototype - -proto.lastT = function() { - return Math.max( - this.radius.lastT(), - this.center.lastT(), - this.rotation.lastT()) -} - -proto.recalcMatrix = function(t) { - this.radius.curve(t) - this.center.curve(t) - this.rotation.curve(t) - - var quat = this.computedRotation - normalize4(quat, quat) - - var mat = this.computedMatrix - mat4FromQuat(mat, quat) - - var center = this.computedCenter - var eye = this.computedEye - var up = this.computedUp - var radius = Math.exp(this.computedRadius[0]) - - eye[0] = center[0] + radius * mat[2] - eye[1] = center[1] + radius * mat[6] - eye[2] = center[2] + radius * mat[10] - up[0] = mat[1] - up[1] = mat[5] - up[2] = mat[9] - - for(var i=0; i<3; ++i) { - var rr = 0.0 - for(var j=0; j<3; ++j) { - rr += mat[i+4*j] * eye[j] - } - mat[12+i] = -rr - } -} - -proto.getMatrix = function(t, result) { - this.recalcMatrix(t) - var m = this.computedMatrix - if(result) { - for(var i=0; i<16; ++i) { - result[i] = m[i] - } - return result - } - return m -} - -proto.idle = function(t) { - this.center.idle(t) - this.radius.idle(t) - this.rotation.idle(t) -} - -proto.flush = function(t) { - this.center.flush(t) - this.radius.flush(t) - this.rotation.flush(t) -} - -proto.pan = function(t, dx, dy, dz) { - dx = dx || 0.0 - dy = dy || 0.0 - dz = dz || 0.0 - - this.recalcMatrix(t) - var mat = this.computedMatrix - - var ux = mat[1] - var uy = mat[5] - var uz = mat[9] - var ul = len3(ux, uy, uz) - ux /= ul - uy /= ul - uz /= ul - - var rx = mat[0] - var ry = mat[4] - var rz = mat[8] - var ru = rx * ux + ry * uy + rz * uz - rx -= ux * ru - ry -= uy * ru - rz -= uz * ru - var rl = len3(rx, ry, rz) - rx /= rl - ry /= rl - rz /= rl - - var fx = mat[2] - var fy = mat[6] - var fz = mat[10] - var fu = fx * ux + fy * uy + fz * uz - var fr = fx * rx + fy * ry + fz * rz - fx -= fu * ux + fr * rx - fy -= fu * uy + fr * ry - fz -= fu * uz + fr * rz - var fl = len3(fx, fy, fz) - fx /= fl - fy /= fl - fz /= fl - - var vx = rx * dx + ux * dy - var vy = ry * dx + uy * dy - var vz = rz * dx + uz * dy - - this.center.move(t, vx, vy, vz) - - //Update z-component of radius - var radius = Math.exp(this.computedRadius[0]) - radius = Math.max(1e-4, radius + dz) - this.radius.set(t, Math.log(radius)) -} - -proto.rotate = function(t, dx, dy, dz) { - this.recalcMatrix(t) - - dx = dx||0.0 - dy = dy||0.0 - - var mat = this.computedMatrix - - var rx = mat[0] - var ry = mat[4] - var rz = mat[8] - - var ux = mat[1] - var uy = mat[5] - var uz = mat[9] - - var fx = mat[2] - var fy = mat[6] - var fz = mat[10] - - var qx = dx * rx + dy * ux - var qy = dx * ry + dy * uy - var qz = dx * rz + dy * uz - - var bx = -(fy * qz - fz * qy) - var by = -(fz * qx - fx * qz) - var bz = -(fx * qy - fy * qx) - var bw = Math.sqrt(Math.max(0.0, 1.0 - Math.pow(bx,2) - Math.pow(by,2) - Math.pow(bz,2))) - var bl = len4(bx, by, bz, bw) - if(bl > 1e-6) { - bx /= bl - by /= bl - bz /= bl - bw /= bl - } else { - bx = by = bz = 0.0 - bw = 1.0 - } - - var rotation = this.computedRotation - var ax = rotation[0] - var ay = rotation[1] - var az = rotation[2] - var aw = rotation[3] - - var cx = ax*bw + aw*bx + ay*bz - az*by - var cy = ay*bw + aw*by + az*bx - ax*bz - var cz = az*bw + aw*bz + ax*by - ay*bx - var cw = aw*bw - ax*bx - ay*by - az*bz - - //Apply roll - if(dz) { - bx = fx - by = fy - bz = fz - var s = Math.sin(dz) / len3(bx, by, bz) - bx *= s - by *= s - bz *= s - bw = Math.cos(dx) - cx = cx*bw + cw*bx + cy*bz - cz*by - cy = cy*bw + cw*by + cz*bx - cx*bz - cz = cz*bw + cw*bz + cx*by - cy*bx - cw = cw*bw - cx*bx - cy*by - cz*bz - } - - var cl = len4(cx, cy, cz, cw) - if(cl > 1e-6) { - cx /= cl - cy /= cl - cz /= cl - cw /= cl - } else { - cx = cy = cz = 0.0 - cw = 1.0 - } - - this.rotation.set(t, cx, cy, cz, cw) -} - -proto.lookAt = function(t, eye, center, up) { - this.recalcMatrix(t) - - center = center || this.computedCenter - eye = eye || this.computedEye - up = up || this.computedUp - - var mat = this.computedMatrix - lookAt(mat, eye, center, up) - - var rotation = this.computedRotation - quatFromFrame(rotation, - mat[0], mat[1], mat[2], - mat[4], mat[5], mat[6], - mat[8], mat[9], mat[10]) - normalize4(rotation, rotation) - this.rotation.set(t, rotation[0], rotation[1], rotation[2], rotation[3]) - - var fl = 0.0 - for(var i=0; i<3; ++i) { - fl += Math.pow(center[i] - eye[i], 2) - } - this.radius.set(t, 0.5 * Math.log(Math.max(fl, 1e-6))) - - this.center.set(t, center[0], center[1], center[2]) -} - -proto.translate = function(t, dx, dy, dz) { - this.center.move(t, - dx||0.0, - dy||0.0, - dz||0.0) -} - -proto.setMatrix = function(t, matrix) { - - var rotation = this.computedRotation - quatFromFrame(rotation, - matrix[0], matrix[1], matrix[2], - matrix[4], matrix[5], matrix[6], - matrix[8], matrix[9], matrix[10]) - normalize4(rotation, rotation) - this.rotation.set(t, rotation[0], rotation[1], rotation[2], rotation[3]) - - var mat = this.computedMatrix - invert44(mat, matrix) - var w = mat[15] - if(Math.abs(w) > 1e-6) { - var cx = mat[12]/w - var cy = mat[13]/w - var cz = mat[14]/w - - this.recalcMatrix(t) - var r = Math.exp(this.computedRadius[0]) - this.center.set(t, cx-mat[2]*r, cy-mat[6]*r, cz-mat[10]*r) - this.radius.idle(t) - } else { - this.center.idle(t) - this.radius.idle(t) - } -} - -proto.setDistance = function(t, d) { - if(d > 0) { - this.radius.set(t, Math.log(d)) - } -} - -proto.setDistanceLimits = function(lo, hi) { - if(lo > 0) { - lo = Math.log(lo) - } else { - lo = -Infinity - } - if(hi > 0) { - hi = Math.log(hi) - } else { - hi = Infinity - } - hi = Math.max(hi, lo) - this.radius.bounds[0][0] = lo - this.radius.bounds[1][0] = hi -} - -proto.getDistanceLimits = function(out) { - var bounds = this.radius.bounds - if(out) { - out[0] = Math.exp(bounds[0][0]) - out[1] = Math.exp(bounds[1][0]) - return out - } - return [ Math.exp(bounds[0][0]), Math.exp(bounds[1][0]) ] -} - -proto.toJSON = function() { - this.recalcMatrix(this.lastT()) - return { - center: this.computedCenter.slice(), - rotation: this.computedRotation.slice(), - distance: Math.log(this.computedRadius[0]), - zoomMin: this.radius.bounds[0][0], - zoomMax: this.radius.bounds[1][0] - } -} - -proto.fromJSON = function(options) { - var t = this.lastT() - var c = options.center - if(c) { - this.center.set(t, c[0], c[1], c[2]) - } - var r = options.rotation - if(r) { - this.rotation.set(t, r[0], r[1], r[2], r[3]) - } - var d = options.distance - if(d && d > 0) { - this.radius.set(t, Math.log(d)) - } - this.setDistanceLimits(options.zoomMin, options.zoomMax) -} - -function createOrbitController(options) { - options = options || {} - var center = options.center || [0,0,0] - var rotation = options.rotation || [0,0,0,1] - var radius = options.radius || 1.0 - - center = [].slice.call(center, 0, 3) - rotation = [].slice.call(rotation, 0, 4) - normalize4(rotation, rotation) - - var result = new OrbitCameraController( - rotation, - center, - Math.log(radius)) - - result.setDistanceLimits(options.zoomMin, options.zoomMax) - - if('eye' in options || 'up' in options) { - result.lookAt(0, options.eye, options.center, options.up) - } - - return result -} -},{"./lib/quatFromFrame":130,"filtered-vector":131,"gl-mat4/fromQuat":232,"gl-mat4/invert":235,"gl-mat4/lookAt":236}],135:[function(require,module,exports){ -arguments[4][131][0].apply(exports,arguments) -},{"binary-search-bounds":136,"cubic-hermite":137,"dup":131}],136:[function(require,module,exports){ -arguments[4][118][0].apply(exports,arguments) -},{"dup":118}],137:[function(require,module,exports){ -arguments[4][133][0].apply(exports,arguments) -},{"dup":133}],138:[function(require,module,exports){ -arguments[4][119][0].apply(exports,arguments) -},{"dup":119}],139:[function(require,module,exports){ -arguments[4][120][0].apply(exports,arguments) -},{"dup":120}],140:[function(require,module,exports){ -arguments[4][123][0].apply(exports,arguments) -},{"dup":123}],141:[function(require,module,exports){ -'use strict' - -module.exports = createTurntableController - -var filterVector = require('filtered-vector') -var invert44 = require('gl-mat4/invert') -var rotateM = require('gl-mat4/rotate') -var cross = require('gl-vec3/cross') -var normalize3 = require('gl-vec3/normalize') -var dot3 = require('gl-vec3/dot') - -function len3(x, y, z) { - return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)) -} - -function clamp1(x) { - return Math.min(1.0, Math.max(-1.0, x)) -} - -function findOrthoPair(v) { - var vx = Math.abs(v[0]) - var vy = Math.abs(v[1]) - var vz = Math.abs(v[2]) - - var u = [0,0,0] - if(vx > Math.max(vy, vz)) { - u[2] = 1 - } else if(vy > Math.max(vx, vz)) { - u[0] = 1 - } else { - u[1] = 1 - } - - var vv = 0 - var uv = 0 - for(var i=0; i<3; ++i ) { - vv += v[i] * v[i] - uv += u[i] * v[i] - } - for(var i=0; i<3; ++i) { - u[i] -= (uv / vv) * v[i] - } - normalize3(u, u) - return u -} - -function TurntableController(zoomMin, zoomMax, center, up, right, radius, theta, phi) { - this.center = filterVector(center) - this.up = filterVector(up) - this.right = filterVector(right) - this.radius = filterVector([radius]) - this.angle = filterVector([theta, phi]) - this.angle.bounds = [[-Infinity,-Math.PI/2], [Infinity,Math.PI/2]] - this.setDistanceLimits(zoomMin, zoomMax) - - this.computedCenter = this.center.curve(0) - this.computedUp = this.up.curve(0) - this.computedRight = this.right.curve(0) - this.computedRadius = this.radius.curve(0) - this.computedAngle = this.angle.curve(0) - this.computedToward = [0,0,0] - this.computedEye = [0,0,0] - this.computedMatrix = new Array(16) - for(var i=0; i<16; ++i) { - this.computedMatrix[i] = 0.5 - } - - this.recalcMatrix(0) -} - -var proto = TurntableController.prototype - -proto.setDistanceLimits = function(minDist, maxDist) { - if(minDist > 0) { - minDist = Math.log(minDist) - } else { - minDist = -Infinity - } - if(maxDist > 0) { - maxDist = Math.log(maxDist) - } else { - maxDist = Infinity - } - maxDist = Math.max(maxDist, minDist) - this.radius.bounds[0][0] = minDist - this.radius.bounds[1][0] = maxDist -} - -proto.getDistanceLimits = function(out) { - var bounds = this.radius.bounds[0] - if(out) { - out[0] = Math.exp(bounds[0][0]) - out[1] = Math.exp(bounds[1][0]) - return out - } - return [ Math.exp(bounds[0][0]), Math.exp(bounds[1][0]) ] -} - -proto.recalcMatrix = function(t) { - //Recompute curves - this.center.curve(t) - this.up.curve(t) - this.right.curve(t) - this.radius.curve(t) - this.angle.curve(t) - - //Compute frame for camera matrix - var up = this.computedUp - var right = this.computedRight - var uu = 0.0 - var ur = 0.0 - for(var i=0; i<3; ++i) { - ur += up[i] * right[i] - uu += up[i] * up[i] - } - var ul = Math.sqrt(uu) - var rr = 0.0 - for(var i=0; i<3; ++i) { - right[i] -= up[i] * ur / uu - rr += right[i] * right[i] - up[i] /= ul - } - var rl = Math.sqrt(rr) - for(var i=0; i<3; ++i) { - right[i] /= rl - } - - //Compute toward vector - var toward = this.computedToward - cross(toward, up, right) - normalize3(toward, toward) - - //Compute angular parameters - var radius = Math.exp(this.computedRadius[0]) - var theta = this.computedAngle[0] - var phi = this.computedAngle[1] - - var ctheta = Math.cos(theta) - var stheta = Math.sin(theta) - var cphi = Math.cos(phi) - var sphi = Math.sin(phi) - - var center = this.computedCenter - - var wx = ctheta * cphi - var wy = stheta * cphi - var wz = sphi - - var sx = -ctheta * sphi - var sy = -stheta * sphi - var sz = cphi - - var eye = this.computedEye - var mat = this.computedMatrix - for(var i=0; i<3; ++i) { - var x = wx * right[i] + wy * toward[i] + wz * up[i] - mat[4*i+1] = sx * right[i] + sy * toward[i] + sz * up[i] - mat[4*i+2] = x - mat[4*i+3] = 0.0 - } - - var ax = mat[1] - var ay = mat[5] - var az = mat[9] - var bx = mat[2] - var by = mat[6] - var bz = mat[10] - var cx = ay * bz - az * by - var cy = az * bx - ax * bz - var cz = ax * by - ay * bx - var cl = len3(cx, cy, cz) - cx /= cl - cy /= cl - cz /= cl - mat[0] = cx - mat[4] = cy - mat[8] = cz - - for(var i=0; i<3; ++i) { - eye[i] = center[i] + mat[2+4*i]*radius - } - - for(var i=0; i<3; ++i) { - var rr = 0.0 - for(var j=0; j<3; ++j) { - rr += mat[i+4*j] * eye[j] - } - mat[12+i] = -rr - } - mat[15] = 1.0 -} - -proto.getMatrix = function(t, result) { - this.recalcMatrix(t) - var mat = this.computedMatrix - if(result) { - for(var i=0; i<16; ++i) { - result[i] = mat[i] - } - return result - } - return mat -} - -var zAxis = [0,0,0] -proto.rotate = function(t, dtheta, dphi, droll) { - this.angle.move(t, dtheta, dphi) - if(droll) { - this.recalcMatrix(t) - - var mat = this.computedMatrix - zAxis[0] = mat[2] - zAxis[1] = mat[6] - zAxis[2] = mat[10] - - var up = this.computedUp - var right = this.computedRight - var toward = this.computedToward - - for(var i=0; i<3; ++i) { - mat[4*i] = up[i] - mat[4*i+1] = right[i] - mat[4*i+2] = toward[i] - } - rotateM(mat, mat, droll, zAxis) - for(var i=0; i<3; ++i) { - up[i] = mat[4*i] - right[i] = mat[4*i+1] - } - - this.up.set(t, up[0], up[1], up[2]) - this.right.set(t, right[0], right[1], right[2]) - } -} - -proto.pan = function(t, dx, dy, dz) { - dx = dx || 0.0 - dy = dy || 0.0 - dz = dz || 0.0 - - this.recalcMatrix(t) - var mat = this.computedMatrix - - var dist = Math.exp(this.computedRadius[0]) - - var ux = mat[1] - var uy = mat[5] - var uz = mat[9] - var ul = len3(ux, uy, uz) - ux /= ul - uy /= ul - uz /= ul - - var rx = mat[0] - var ry = mat[4] - var rz = mat[8] - var ru = rx * ux + ry * uy + rz * uz - rx -= ux * ru - ry -= uy * ru - rz -= uz * ru - var rl = len3(rx, ry, rz) - rx /= rl - ry /= rl - rz /= rl - - var vx = rx * dx + ux * dy - var vy = ry * dx + uy * dy - var vz = rz * dx + uz * dy - this.center.move(t, vx, vy, vz) - - //Update z-component of radius - var radius = Math.exp(this.computedRadius[0]) - radius = Math.max(1e-4, radius + dz) - this.radius.set(t, Math.log(radius)) -} - -proto.translate = function(t, dx, dy, dz) { - this.center.move(t, - dx||0.0, - dy||0.0, - dz||0.0) -} - -//Recenters the coordinate axes -proto.setMatrix = function(t, mat, axes, noSnap) { - - //Get the axes for tare - var ushift = 1 - if(typeof axes === 'number') { - ushift = (axes)|0 - } - if(ushift < 0 || ushift > 3) { - ushift = 1 - } - var vshift = (ushift + 2) % 3 - var fshift = (ushift + 1) % 3 - - //Recompute state for new t value - if(!mat) { - this.recalcMatrix(t) - mat = this.computedMatrix - } - - //Get right and up vectors - var ux = mat[ushift] - var uy = mat[ushift+4] - var uz = mat[ushift+8] - if(!noSnap) { - var ul = len3(ux, uy, uz) - ux /= ul - uy /= ul - uz /= ul - } else { - var ax = Math.abs(ux) - var ay = Math.abs(uy) - var az = Math.abs(uz) - var am = Math.max(ax,ay,az) - if(ax === am) { - ux = (ux < 0) ? -1 : 1 - uy = uz = 0 - } else if(az === am) { - uz = (uz < 0) ? -1 : 1 - ux = uy = 0 - } else { - uy = (uy < 0) ? -1 : 1 - ux = uz = 0 - } - } - - var rx = mat[vshift] - var ry = mat[vshift+4] - var rz = mat[vshift+8] - var ru = rx * ux + ry * uy + rz * uz - rx -= ux * ru - ry -= uy * ru - rz -= uz * ru - var rl = len3(rx, ry, rz) - rx /= rl - ry /= rl - rz /= rl - - var fx = uy * rz - uz * ry - var fy = uz * rx - ux * rz - var fz = ux * ry - uy * rx - var fl = len3(fx, fy, fz) - fx /= fl - fy /= fl - fz /= fl - - this.center.jump(t, ex, ey, ez) - this.radius.idle(t) - this.up.jump(t, ux, uy, uz) - this.right.jump(t, rx, ry, rz) - - var phi, theta - if(ushift === 2) { - var cx = mat[1] - var cy = mat[5] - var cz = mat[9] - var cr = cx * rx + cy * ry + cz * rz - var cf = cx * fx + cy * fy + cz * fz - if(tu < 0) { - phi = -Math.PI/2 - } else { - phi = Math.PI/2 - } - theta = Math.atan2(cf, cr) - } else { - var tx = mat[2] - var ty = mat[6] - var tz = mat[10] - var tu = tx * ux + ty * uy + tz * uz - var tr = tx * rx + ty * ry + tz * rz - var tf = tx * fx + ty * fy + tz * fz - - phi = Math.asin(clamp1(tu)) - theta = Math.atan2(tf, tr) - } - - this.angle.jump(t, theta, phi) - - this.recalcMatrix(t) - var dx = mat[2] - var dy = mat[6] - var dz = mat[10] - - var imat = this.computedMatrix - invert44(imat, mat) - var w = imat[15] - var ex = imat[12] / w - var ey = imat[13] / w - var ez = imat[14] / w - - var gs = Math.exp(this.computedRadius[0]) - this.center.jump(t, ex-dx*gs, ey-dy*gs, ez-dz*gs) -} - -proto.lastT = function() { - return Math.max( - this.center.lastT(), - this.up.lastT(), - this.right.lastT(), - this.radius.lastT(), - this.angle.lastT()) -} - -proto.idle = function(t) { - this.center.idle(t) - this.up.idle(t) - this.right.idle(t) - this.radius.idle(t) - this.angle.idle(t) -} - -proto.flush = function(t) { - this.center.flush(t) - this.up.flush(t) - this.right.flush(t) - this.radius.flush(t) - this.angle.flush(t) -} - -proto.setDistance = function(t, d) { - if(d > 0) { - this.radius.set(t, Math.log(d)) - } -} - -proto.lookAt = function(t, eye, center, up) { - this.recalcMatrix(t) - - eye = eye || this.computedEye - center = center || this.computedCenter - up = up || this.computedUp - - var ux = up[0] - var uy = up[1] - var uz = up[2] - var ul = len3(ux, uy, uz) - if(ul < 1e-6) { - return - } - ux /= ul - uy /= ul - uz /= ul - - var tx = eye[0] - center[0] - var ty = eye[1] - center[1] - var tz = eye[2] - center[2] - var tl = len3(tx, ty, tz) - if(tl < 1e-6) { - return - } - tx /= tl - ty /= tl - tz /= tl - - var right = this.computedRight - var rx = right[0] - var ry = right[1] - var rz = right[2] - var ru = ux*rx + uy*ry + uz*rz - rx -= ru * ux - ry -= ru * uy - rz -= ru * uz - var rl = len3(rx, ry, rz) - - if(rl < 0.01) { - rx = uy * tz - uz * ty - ry = uz * tx - ux * tz - rz = ux * ty - uy * tx - rl = len3(rx, ry, rz) - if(rl < 1e-6) { - return - } - } - rx /= rl - ry /= rl - rz /= rl - - this.up.set(t, ux, uy, uz) - this.right.set(t, rx, ry, rz) - this.center.set(t, center[0], center[1], center[2]) - this.radius.set(t, Math.log(tl)) - - var fx = uy * rz - uz * ry - var fy = uz * rx - ux * rz - var fz = ux * ry - uy * rx - var fl = len3(fx, fy, fz) - fx /= fl - fy /= fl - fz /= fl - - var tu = ux*tx + uy*ty + uz*tz - var tr = rx*tx + ry*ty + rz*tz - var tf = fx*tx + fy*ty + fz*tz - - var phi = Math.asin(clamp1(tu)) - var theta = Math.atan2(tf, tr) - - var angleState = this.angle._state - var lastTheta = angleState[angleState.length-1] - var lastPhi = angleState[angleState.length-2] - lastTheta = lastTheta % (2.0 * Math.PI) - var dp = Math.abs(lastTheta + 2.0 * Math.PI - theta) - var d0 = Math.abs(lastTheta - theta) - var dn = Math.abs(lastTheta - 2.0 * Math.PI - theta) - if(dp < d0) { - lastTheta += 2.0 * Math.PI - } - if(dn < d0) { - lastTheta -= 2.0 * Math.PI - } - - this.angle.jump(this.angle.lastT(), lastTheta, lastPhi) - this.angle.set(t, theta, phi) -} - -function createTurntableController(options) { - options = options || {} - - var center = options.center || [0,0,0] - var up = options.up || [0,1,0] - var right = options.right || findOrthoPair(up) - var radius = options.radius || 1.0 - var theta = options.theta || 0.0 - var phi = options.phi || 0.0 - - center = [].slice.call(center, 0, 3) - - up = [].slice.call(up, 0, 3) - normalize3(up, up) - - right = [].slice.call(right, 0, 3) - normalize3(right, right) - - if('eye' in options) { - var eye = options.eye - var toward = [ - eye[0]-center[0], - eye[1]-center[1], - eye[2]-center[2] - ] - cross(right, toward, up) - if(len3(right[0], right[1], right[2]) < 1e-6) { - right = findOrthoPair(up) - } else { - normalize3(right, right) - } - - radius = len3(toward[0], toward[1], toward[2]) - - var ut = dot3(up, toward) / radius - var rt = dot3(right, toward) / radius - phi = Math.acos(ut) - theta = Math.acos(rt) - } - - //Use logarithmic coordinates for radius - radius = Math.log(radius) - - //Return the controller - return new TurntableController( - options.zoomMin, - options.zoomMax, - center, - up, - right, - radius, - theta, - phi) -} -},{"filtered-vector":135,"gl-mat4/invert":235,"gl-mat4/rotate":239,"gl-vec3/cross":138,"gl-vec3/dot":139,"gl-vec3/normalize":140}],142:[function(require,module,exports){ -'use strict' - -module.exports = createViewController - -var createTurntable = require('turntable-camera-controller') -var createOrbit = require('orbit-camera-controller') -var createMatrix = require('matrix-camera-controller') - -function ViewController(controllers, mode) { - this._controllerNames = Object.keys(controllers) - this._controllerList = this._controllerNames.map(function(n) { - return controllers[n] - }) - this._mode = mode - this._active = controllers[mode] - if(!this._active) { - this._mode = 'turntable' - this._active = controllers.turntable - } - this.modes = this._controllerNames - this.computedMatrix = this._active.computedMatrix - this.computedEye = this._active.computedEye - this.computedUp = this._active.computedUp - this.computedCenter = this._active.computedCenter - this.computedRadius = this._active.computedRadius -} - -var proto = ViewController.prototype - -var COMMON_METHODS = [ - ['flush', 1], - ['idle', 1], - ['lookAt', 4], - ['rotate', 4], - ['pan', 4], - ['translate', 4], - ['setMatrix', 2], - ['setDistanceLimits', 2], - ['setDistance', 2] -] - -COMMON_METHODS.forEach(function(method) { - var name = method[0] - var argNames = [] - for(var i=0; i 0) { - return dupe_number(count|0, value) - } - break - case "object": - if(typeof (count.length) === "number") { - return dupe_array(count, value, 0) - } - break - } - return [] -} - -module.exports = dupe -},{}],148:[function(require,module,exports){ -"use strict" - -var determinant = require("robust-determinant") - -var NUM_EXPAND = 6 - -function generateSolver(n) { - var funcName = "robustLinearSolve" + n + "d" - var code = ["function ", funcName, "(A,b){return ["] - for(var i=0; i 0) { - code.push(",") - } - code.push("[") - for(var k=0; k 0) { - code.push(",") - } - if(k === i) { - code.push("+b[", j, "]") - } else { - code.push("+A[", j, "][", k, "]") - } - } - code.push("]") - } - code.push("]),") - } - code.push("det(A)]}return ", funcName) - var proc = new Function("det", code.join("")) - if(n < 6) { - return proc(determinant[n]) - } - return proc(determinant) -} - -function robustLinearSolve0d() { - return [ 0 ] -} - -function robustLinearSolve1d(A, b) { - return [ [ b[0] ], [ A[0][0] ] ] -} - -var CACHE = [ - robustLinearSolve0d, - robustLinearSolve1d -] - -function generateDispatch() { - while(CACHE.length < NUM_EXPAND) { - CACHE.push(generateSolver(CACHE.length)) - } - var procArgs = [] - var code = ["function dispatchLinearSolve(A,b){switch(A.length){"] - for(var i=0; i=0; --i) { - var a = Q - var b = e[i] - Q = a + b - var bv = Q - a - var q = b - bv - if(q) { - e[--bottom] = Q - Q = q - } - } - var top = 0 - for(var i=bottom; i= nf)) { - a = ei - eptr += 1 - if(eptr < ne) { - ei = e[eptr] - ea = abs(ei) - } - } else { - a = fi - fptr += 1 - if(fptr < nf) { - fi = f[fptr] - fa = abs(fi) - } - } - var x = a + b - var bv = x - a - var y = b - bv - var q0 = y - var q1 = x - var _x, _bv, _av, _br, _ar - while(eptr < ne && fptr < nf) { - if(ea < fa) { - a = ei - eptr += 1 - if(eptr < ne) { - ei = e[eptr] - ea = abs(ei) - } - } else { - a = fi - fptr += 1 - if(fptr < nf) { - fi = f[fptr] - fa = abs(fi) - } - } - b = q0 - x = a + b - bv = x - a - y = b - bv - if(y) { - g[count++] = y - } - _x = q1 + x - _bv = _x - q1 - _av = _x - _bv - _br = x - _bv - _ar = q1 - _av - q0 = _ar + _br - q1 = _x - } - while(eptr < ne) { - a = ei - b = q0 - x = a + b - bv = x - a - y = b - bv - if(y) { - g[count++] = y - } - _x = q1 + x - _bv = _x - q1 - _av = _x - _bv - _br = x - _bv - _ar = q1 - _av - q0 = _ar + _br - q1 = _x - eptr += 1 - if(eptr < ne) { - ei = e[eptr] - } - } - while(fptr < nf) { - a = fi - b = q0 - x = a + b - bv = x - a - y = b - bv - if(y) { - g[count++] = y - } - _x = q1 + x - _bv = _x - q1 - _av = _x - _bv - _br = x - _bv - _ar = q1 - _av - q0 = _ar + _br - q1 = _x - fptr += 1 - if(fptr < nf) { - fi = f[fptr] - } - } - if(q0) { - g[count++] = q0 - } - if(q1) { - g[count++] = q1 - } - if(!count) { - g[count++] = 0.0 - } - g.length = count - return g -} -},{}],153:[function(require,module,exports){ -"use strict" - -module.exports = twoProduct - -var SPLITTER = +(Math.pow(2, 27) + 1.0) - -function twoProduct(a, b, result) { - var x = a * b - - var c = SPLITTER * a - var abig = c - a - var ahi = c - abig - var alo = a - ahi - - var d = SPLITTER * b - var bbig = d - b - var bhi = d - bbig - var blo = b - bhi - - var err1 = x - (ahi * bhi) - var err2 = err1 - (alo * bhi) - var err3 = err2 - (ahi * blo) - - var y = alo * blo - err3 - - if(result) { - result[0] = y - result[1] = x - return result - } - - return [ y, x ] -} -},{}],154:[function(require,module,exports){ -"use strict" - -var twoProduct = require("two-product") -var robustSum = require("robust-sum") -var robustScale = require("robust-scale") -var compress = require("robust-compress") - -var NUM_EXPANDED = 6 - -function cofactor(m, c) { - var result = new Array(m.length-1) - for(var i=1; i>1 - return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") - } -} - -function determinant(m) { - if(m.length === 2) { - return ["sum(prod(", m[0][0], ",", m[1][1], "),prod(-", m[0][1], ",", m[1][0], "))"].join("") - } else { - var expr = [] - for(var i=0; i 0) { - var f = cells[ptr-1] - if(compareCell(c, f) === 0 && - orientation(f) !== o) { - ptr -= 1 - continue - } - } - cells[ptr++] = c - } - cells.length = ptr - return cells -} - -},{"cell-orientation":157,"compare-cell":158,"compare-oriented-cell":159}],161:[function(require,module,exports){ -'use strict'; - -var arraytools = function () { - - var that = {}; - - var RGB_REGEX = /^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,.*)?\)$/; - var RGB_GROUP_REGEX = /^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,?\s*(.*)?\)$/; - - function isPlainObject (v) { - return !Array.isArray(v) && v !== null && typeof v === 'object'; - } - - function linspace (start, end, num) { - var inc = (end - start) / Math.max(num - 1, 1); - var a = []; - for( var ii = 0; ii < num; ii++) - a.push(start + ii*inc); - return a; - } - - function zip () { - var arrays = [].slice.call(arguments); - var lengths = arrays.map(function (a) {return a.length;}); - var len = Math.min.apply(null, lengths); - var zipped = []; - for (var i = 0; i < len; i++) { - zipped[i] = []; - for (var j = 0; j < arrays.length; ++j) { - zipped[i][j] = arrays[j][i]; - } - } - return zipped; - } - - function zip3 (a, b, c) { - var len = Math.min.apply(null, [a.length, b.length, c.length]); - var result = []; - for (var n = 0; n < len; n++) { - result.push([a[n], b[n], c[n]]); - } - return result; - } - - function sum (A) { - var acc = 0; - accumulate(A, acc); - function accumulate(x) { - for (var i = 0; i < x.length; i++) { - if (Array.isArray(x[i])) - accumulate(x[i], acc); - else - acc += x[i]; - } - } - return acc; - } - - function copy2D (arr) { - var carr = []; - for (var i = 0; i < arr.length; ++i) { - carr[i] = []; - for (var j = 0; j < arr[i].length; ++j) { - carr[i][j] = arr[i][j]; - } - } - - return carr; - } - - - function copy1D (arr) { - var carr = []; - for (var i = 0; i < arr.length; ++i) { - carr[i] = arr[i]; - } - - return carr; - } - - - function isEqual(arr1, arr2) { - if(arr1.length !== arr2.length) - return false; - for(var i = arr1.length; i--;) { - if(arr1[i] !== arr2[i]) - return false; - } - - return true; - } - - - function str2RgbArray(str, twoFiftySix) { - // convert hex or rbg strings to 0->1 or 0->255 rgb array - var rgb, - match; - - if (typeof str !== 'string') return str; - - rgb = []; - // hex notation - if (str[0] === '#') { - str = str.substr(1) // remove hash - if (str.length === 3) str += str // fff -> ffffff - match = parseInt(str, 16); - rgb[0] = ((match >> 16) & 255); - rgb[1] = ((match >> 8) & 255); - rgb[2] = (match & 255); - } - - // rgb(34, 34, 127) or rgba(34, 34, 127, 0.1) notation - else if (RGB_REGEX.test(str)) { - match = str.match(RGB_GROUP_REGEX); - rgb[0] = parseInt(match[1]); - rgb[1] = parseInt(match[2]); - rgb[2] = parseInt(match[3]); - } - - if (!twoFiftySix) { - for (var j=0; j<3; ++j) rgb[j] = rgb[j]/255 - } - - - return rgb; - } - - - function str2RgbaArray(str, twoFiftySix) { - // convert hex or rbg strings to 0->1 or 0->255 rgb array - var rgb, - match; - - if (typeof str !== 'string') return str; - - rgb = []; - // hex notation - if (str[0] === '#') { - str = str.substr(1) // remove hash - if (str.length === 3) str += str // fff -> ffffff - match = parseInt(str, 16); - rgb[0] = ((match >> 16) & 255); - rgb[1] = ((match >> 8) & 255); - rgb[2] = (match & 255); - } - - // rgb(34, 34, 127) or rgba(34, 34, 127, 0.1) notation - else if (RGB_REGEX.test(str)) { - match = str.match(RGB_GROUP_REGEX); - rgb[0] = parseInt(match[1]); - rgb[1] = parseInt(match[2]); - rgb[2] = parseInt(match[3]); - if (match[4]) rgb[3] = parseFloat(match[4]); - else rgb[3] = 1.0; - } - - - - if (!twoFiftySix) { - for (var j=0; j<3; ++j) rgb[j] = rgb[j]/255 - } - - - return rgb; - } - - - - - - that.isPlainObject = isPlainObject; - that.linspace = linspace; - that.zip3 = zip3; - that.sum = sum; - that.zip = zip; - that.isEqual = isEqual; - that.copy2D = copy2D; - that.copy1D = copy1D; - that.str2RgbArray = str2RgbArray; - that.str2RgbaArray = str2RgbaArray; - - return that - -} - - -module.exports = arraytools(); - -},{}],162:[function(require,module,exports){ -"use strict" - -var convexHull1d = require('./lib/ch1d') -var convexHull2d = require('./lib/ch2d') -var convexHullnd = require('./lib/chnd') - -module.exports = convexHull - -function convexHull(points) { - var n = points.length - if(n === 0) { - return [] - } else if(n === 1) { - return [[0]] - } - var d = points[0].length - if(d === 0) { - return [] - } else if(d === 1) { - return convexHull1d(points) - } else if(d === 2) { - return convexHull2d(points) - } - return convexHullnd(points, d) -} -},{"./lib/ch1d":163,"./lib/ch2d":164,"./lib/chnd":165}],163:[function(require,module,exports){ -"use strict" - -module.exports = convexHull1d - -function convexHull1d(points) { - var lo = 0 - var hi = 0 - for(var i=1; i points[hi][0]) { - hi = i - } - } - if(lo < hi) { - return [[lo], [hi]] - } else if(lo > hi) { - return [[hi], [lo]] - } else { - return [[lo]] - } -} -},{}],164:[function(require,module,exports){ -'use strict' - -module.exports = convexHull2D - -var monotoneHull = require('monotone-convex-hull-2d') - -function convexHull2D(points) { - var hull = monotoneHull(points) - var h = hull.length - if(h <= 2) { - return [] - } - var edges = new Array(h) - var a = hull[h-1] - for(var i=0; i= front[k]) { - x += 1 - } - } - c[j] = x - } - } - } - return cells -} - -function convexHullnD(points, d) { - try { - return ich(points, true) - } catch(e) { - //If point set is degenerate, try to find a basis and rerun it - var ah = aff(points) - if(ah.length <= d) { - //No basis, no try - return [] - } - var npoints = permute(points, ah) - var nhull = ich(npoints, true) - return invPermute(nhull, ah) - } -} -},{"affine-hull":166,"incremental-convex-hull":167}],166:[function(require,module,exports){ -'use strict' - -module.exports = affineHull - -var orient = require('robust-orientation') - -function linearlyIndependent(points, d) { - var nhull = new Array(d+1) - for(var i=0; i 0) { - code.push(",") - } - code.push("tuple[", i, "]") - } - code.push(")}return orient") - var proc = new Function("test", code.join("")) - var test = orient[d+1] - if(!test) { - test = orient - } - return proc(test) -} - -var BAKED = [] - -function Triangulation(dimension, vertices, simplices) { - this.dimension = dimension - this.vertices = vertices - this.simplices = simplices - this.interior = simplices.filter(function(c) { - return !c.boundary - }) - - this.tuple = new Array(dimension+1) - for(var i=0; i<=dimension; ++i) { - this.tuple[i] = this.vertices[i] - } - - var o = BAKED[dimension] - if(!o) { - o = BAKED[dimension] = bakeOrient(dimension) - } - this.orient = o -} - -var proto = Triangulation.prototype - -//Degenerate situation where we are on boundary, but coplanar to face -proto.handleBoundaryDegeneracy = function(cell, point) { - var d = this.dimension - var n = this.vertices.length - 1 - var tuple = this.tuple - var verts = this.vertices - - //Dumb solution: Just do dfs from boundary cell until we find any peak, or terminate - var toVisit = [ cell ] - cell.lastVisited = -n - while(toVisit.length > 0) { - cell = toVisit.pop() - var cellVerts = cell.vertices - var cellAdj = cell.adjacent - for(var i=0; i<=d; ++i) { - var neighbor = cellAdj[i] - if(!neighbor.boundary || neighbor.lastVisited <= -n) { - continue - } - var nv = neighbor.vertices - for(var j=0; j<=d; ++j) { - var vv = nv[j] - if(vv < 0) { - tuple[j] = point - } else { - tuple[j] = verts[vv] - } - } - var o = this.orient() - if(o > 0) { - return neighbor - } - neighbor.lastVisited = -n - if(o === 0) { - toVisit.push(neighbor) - } - } - } - return null -} - -proto.walk = function(point, random) { - //Alias local properties - var n = this.vertices.length - 1 - var d = this.dimension - var verts = this.vertices - var tuple = this.tuple - - //Compute initial jump cell - var initIndex = random ? (this.interior.length * Math.random())|0 : (this.interior.length-1) - var cell = this.interior[ initIndex ] - - //Start walking -outerLoop: - while(!cell.boundary) { - var cellVerts = cell.vertices - var cellAdj = cell.adjacent - - for(var i=0; i<=d; ++i) { - tuple[i] = verts[cellVerts[i]] - } - cell.lastVisited = n - - //Find farthest adjacent cell - for(var i=0; i<=d; ++i) { - var neighbor = cellAdj[i] - if(neighbor.lastVisited >= n) { - continue - } - var prev = tuple[i] - tuple[i] = point - var o = this.orient() - tuple[i] = prev - if(o < 0) { - cell = neighbor - continue outerLoop - } else { - if(!neighbor.boundary) { - neighbor.lastVisited = n - } else { - neighbor.lastVisited = -n - } - } - } - return - } - - return cell -} - -proto.addPeaks = function(point, cell) { - var n = this.vertices.length - 1 - var d = this.dimension - var verts = this.vertices - var tuple = this.tuple - var interior = this.interior - var simplices = this.simplices - - //Walking finished at boundary, time to add peaks - var tovisit = [ cell ] - - //Stretch initial boundary cell into a peak - cell.lastVisited = n - cell.vertices[cell.vertices.indexOf(-1)] = n - cell.boundary = false - interior.push(cell) - - //Record a list of all new boundaries created by added peaks so we can glue them together when we are all done - var glueFacets = [] - - //Do a traversal of the boundary walking outward from starting peak - while(tovisit.length > 0) { - //Pop off peak and walk over adjacent cells - var cell = tovisit.pop() - var cellVerts = cell.vertices - var cellAdj = cell.adjacent - var indexOfN = cellVerts.indexOf(n) - if(indexOfN < 0) { - continue - } - - for(var i=0; i<=d; ++i) { - if(i === indexOfN) { - continue - } - - //For each boundary neighbor of the cell - var neighbor = cellAdj[i] - if(!neighbor.boundary || neighbor.lastVisited >= n) { - continue - } - - var nv = neighbor.vertices - - //Test if neighbor is a peak - if(neighbor.lastVisited !== -n) { - //Compute orientation of p relative to each boundary peak - var indexOfNeg1 = 0 - for(var j=0; j<=d; ++j) { - if(nv[j] < 0) { - indexOfNeg1 = j - tuple[j] = point - } else { - tuple[j] = verts[nv[j]] - } - } - var o = this.orient() - - //Test if neighbor cell is also a peak - if(o > 0) { - nv[indexOfNeg1] = n - neighbor.boundary = false - interior.push(neighbor) - tovisit.push(neighbor) - neighbor.lastVisited = n - continue - } else { - neighbor.lastVisited = -n - } - } - - var na = neighbor.adjacent - - //Otherwise, replace neighbor with new face - var vverts = cellVerts.slice() - var vadj = cellAdj.slice() - var ncell = new Simplex(vverts, vadj, true) - simplices.push(ncell) - - //Connect to neighbor - var opposite = na.indexOf(cell) - if(opposite < 0) { - continue - } - na[opposite] = ncell - vadj[indexOfN] = neighbor - - //Connect to cell - vverts[i] = -1 - vadj[i] = cell - cellAdj[i] = ncell - - //Flip facet - ncell.flip() - - //Add to glue list - for(var j=0; j<=d; ++j) { - var uu = vverts[j] - if(uu < 0 || uu === n) { - continue - } - var nface = new Array(d-1) - var nptr = 0 - for(var k=0; k<=d; ++k) { - var vv = vverts[k] - if(vv < 0 || k === j) { - continue - } - nface[nptr++] = vv - } - glueFacets.push(new GlueFacet(nface, ncell, j)) - } - } - } - - //Glue boundary facets together - glueFacets.sort(compareGlue) - - for(var i=0; i+1= 0) { - bcell[ptr++] = cv[j] - } else { - parity = j&1 - } - } - if(parity === (d&1)) { - var t = bcell[0] - bcell[0] = bcell[1] - bcell[1] = t - } - boundary.push(bcell) - } - } - return boundary -} - -function incrementalConvexHull(points, randomSearch) { - var n = points.length - if(n === 0) { - throw new Error("Must have at least d+1 points") - } - var d = points[0].length - if(n <= d) { - throw new Error("Must input at least d+1 points") - } - - //FIXME: This could be degenerate, but need to select d+1 non-coplanar points to bootstrap process - var initialSimplex = points.slice(0, d+1) - - //Make sure initial simplex is positively oriented - var o = orient.apply(void 0, initialSimplex) - if(o === 0) { - throw new Error("Input not in general position") - } - var initialCoords = new Array(d+1) - for(var i=0; i<=d; ++i) { - initialCoords[i] = i - } - if(o < 0) { - initialCoords[0] = 1 - initialCoords[1] = 0 - } - - //Create initial topological index, glue pointers together (kind of messy) - var initialCell = new Simplex(initialCoords, new Array(d+1), false) - var boundary = initialCell.adjacent - var list = new Array(d+2) - for(var i=0; i<=d; ++i) { - var verts = initialCoords.slice() - for(var j=0; j<=d; ++j) { - if(j === i) { - verts[j] = -1 - } - } - var t = verts[0] - verts[0] = verts[1] - verts[1] = t - var cell = new Simplex(verts, new Array(d+1), true) - boundary[i] = cell - list[i] = cell - } - list[d+1] = initialCell - for(var i=0; i<=d; ++i) { - var verts = boundary[i].vertices - var adj = boundary[i].adjacent - for(var j=0; j<=d; ++j) { - var v = verts[j] - if(v < 0) { - adj[j] = initialCell - continue - } - for(var k=0; k<=d; ++k) { - if(boundary[k].vertices.indexOf(v) < 0) { - adj[j] = boundary[k] - } - } - } - } - - //Initialize triangles - var triangles = new Triangulation(d, initialSimplex, list) - - //Insert remaining points - var useRandom = !!randomSearch - for(var i=d+1; i 0) - (v < 0); -} - -//Computes absolute value of integer -exports.abs = function(v) { - var mask = v >> (INT_BITS-1); - return (v ^ mask) - mask; -} - -//Computes minimum of integers x and y -exports.min = function(x, y) { - return y ^ ((x ^ y) & -(x < y)); -} - -//Computes maximum of integers x and y -exports.max = function(x, y) { - return x ^ ((x ^ y) & -(x < y)); -} - -//Checks if a number is a power of two -exports.isPow2 = function(v) { - return !(v & (v-1)) && (!!v); -} - -//Computes log base 2 of v -exports.log2 = function(v) { - var r, shift; - r = (v > 0xFFFF) << 4; v >>>= r; - shift = (v > 0xFF ) << 3; v >>>= shift; r |= shift; - shift = (v > 0xF ) << 2; v >>>= shift; r |= shift; - shift = (v > 0x3 ) << 1; v >>>= shift; r |= shift; - return r | (v >> 1); -} - -//Computes log base 10 of v -exports.log10 = function(v) { - return (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 : - (v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 : - (v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0; -} - -//Counts number of bits -exports.popCount = function(v) { - v = v - ((v >>> 1) & 0x55555555); - v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); - return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; -} - -//Counts number of trailing zeros -function countTrailingZeros(v) { - var c = 32; - v &= -v; - if (v) c--; - if (v & 0x0000FFFF) c -= 16; - if (v & 0x00FF00FF) c -= 8; - if (v & 0x0F0F0F0F) c -= 4; - if (v & 0x33333333) c -= 2; - if (v & 0x55555555) c -= 1; - return c; -} -exports.countTrailingZeros = countTrailingZeros; - -//Rounds to next power of 2 -exports.nextPow2 = function(v) { - v += v === 0; - --v; - v |= v >>> 1; - v |= v >>> 2; - v |= v >>> 4; - v |= v >>> 8; - v |= v >>> 16; - return v + 1; -} - -//Rounds down to previous power of 2 -exports.prevPow2 = function(v) { - v |= v >>> 1; - v |= v >>> 2; - v |= v >>> 4; - v |= v >>> 8; - v |= v >>> 16; - return v - (v>>>1); -} - -//Computes parity of word -exports.parity = function(v) { - v ^= v >>> 16; - v ^= v >>> 8; - v ^= v >>> 4; - v &= 0xf; - return (0x6996 >>> v) & 1; -} - -var REVERSE_TABLE = new Array(256); - -(function(tab) { - for(var i=0; i<256; ++i) { - var v = i, r = i, s = 7; - for (v >>>= 1; v; v >>>= 1) { - r <<= 1; - r |= v & 1; - --s; - } - tab[i] = (r << s) & 0xff; - } -})(REVERSE_TABLE); - -//Reverse bits in a 32 bit word -exports.reverse = function(v) { - return (REVERSE_TABLE[ v & 0xff] << 24) | - (REVERSE_TABLE[(v >>> 8) & 0xff] << 16) | - (REVERSE_TABLE[(v >>> 16) & 0xff] << 8) | - REVERSE_TABLE[(v >>> 24) & 0xff]; -} - -//Interleave bits of 2 coordinates with 16 bits. Useful for fast quadtree codes -exports.interleave2 = function(x, y) { - x &= 0xFFFF; - x = (x | (x << 8)) & 0x00FF00FF; - x = (x | (x << 4)) & 0x0F0F0F0F; - x = (x | (x << 2)) & 0x33333333; - x = (x | (x << 1)) & 0x55555555; - - y &= 0xFFFF; - y = (y | (y << 8)) & 0x00FF00FF; - y = (y | (y << 4)) & 0x0F0F0F0F; - y = (y | (y << 2)) & 0x33333333; - y = (y | (y << 1)) & 0x55555555; - - return x | (y << 1); -} - -//Extracts the nth interleaved component -exports.deinterleave2 = function(v, n) { - v = (v >>> n) & 0x55555555; - v = (v | (v >>> 1)) & 0x33333333; - v = (v | (v >>> 2)) & 0x0F0F0F0F; - v = (v | (v >>> 4)) & 0x00FF00FF; - v = (v | (v >>> 16)) & 0x000FFFF; - return (v << 16) >> 16; -} - - -//Interleave bits of 3 coordinates, each with 10 bits. Useful for fast octree codes -exports.interleave3 = function(x, y, z) { - x &= 0x3FF; - x = (x | (x<<16)) & 4278190335; - x = (x | (x<<8)) & 251719695; - x = (x | (x<<4)) & 3272356035; - x = (x | (x<<2)) & 1227133513; - - y &= 0x3FF; - y = (y | (y<<16)) & 4278190335; - y = (y | (y<<8)) & 251719695; - y = (y | (y<<4)) & 3272356035; - y = (y | (y<<2)) & 1227133513; - x |= (y << 1); - - z &= 0x3FF; - z = (z | (z<<16)) & 4278190335; - z = (z | (z<<8)) & 251719695; - z = (z | (z<<4)) & 3272356035; - z = (z | (z<<2)) & 1227133513; - - return x | (z << 2); -} - -//Extracts nth interleaved component of a 3-tuple -exports.deinterleave3 = function(v, n) { - v = (v >>> n) & 1227133513; - v = (v | (v>>>2)) & 3272356035; - v = (v | (v>>>4)) & 251719695; - v = (v | (v>>>8)) & 4278190335; - v = (v | (v>>>16)) & 0x3FF; - return (v<<22)>>22; -} - -//Computes next combination in colexicographic order (this is mistakenly called nextPermutation on the bit twiddling hacks page) -exports.nextCombination = function(v) { - var t = v | (v - 1); - return (t + 1) | (((~t & -~t) - 1) >>> (countTrailingZeros(v) + 1)); -} - - -},{}],169:[function(require,module,exports){ -"use strict"; "use restrict"; - -module.exports = UnionFind; - -function UnionFind(count) { - this.roots = new Array(count); - this.ranks = new Array(count); - - for(var i=0; i> 1 - , s = compareCells(cells[mid], c) - if(s <= 0) { - if(s === 0) { - r = mid - } - lo = mid + 1 - } else if(s > 0) { - hi = mid - 1 - } - } - return r -} -exports.findCell = findCell; - -//Builds an index for an n-cell. This is more general than dual, but less efficient -function incidence(from_cells, to_cells) { - var index = new Array(from_cells.length) - for(var i=0, il=index.length; i= from_cells.length || compareCells(from_cells[idx], b) !== 0) { - break - } - } - } - } - return index -} -exports.incidence = incidence - -//Computes the dual of the mesh. This is basically an optimized version of buildIndex for the situation where from_cells is just the list of vertices -function dual(cells, vertex_count) { - if(!vertex_count) { - return incidence(unique(skeleton(cells, 0)), cells, 0) - } - var res = new Array(vertex_count) - for(var i=0; i>> k) & 1) { - b.push(c[k]) - } - } - result.push(b) - } - } - return normalize(result) -} -exports.explode = explode - -//Enumerates all of the n-cells of a cell complex -function skeleton(cells, n) { - if(n < 0) { - return [] - } - var result = [] - , k0 = (1<<(n+1))-1 - for(var i=0; i 1 && orient( - points[lower[m-2]], - points[lower[m-1]], - p) <= 0) { - m -= 1 - lower.pop() - } - lower.push(idx) - - //Insert into upper list - m = upper.length - while(m > 1 && orient( - points[upper[m-2]], - points[upper[m-1]], - p) >= 0) { - m -= 1 - upper.pop() - } - upper.push(idx) - } - - //Merge lists together - var result = new Array(upper.length + lower.length - 2) - var ptr = 0 - for(var i=0, nl=lower.length; i0; --j) { - result[ptr++] = upper[j] - } - - //Return result - return result -} -},{"robust-orientation":1127}],172:[function(require,module,exports){ -module.exports = { - AFG: "afghan", - ALA: "\\b\\wland", - ALB: "albania", - DZA: "algeria", - ASM: "^(?=.*americ).*samoa", - AND: "andorra", - AGO: "angola", - AIA: "anguill?a", - ATA: "antarctica", - ATG: "antigua", - ARG: "argentin", - ARM: "armenia", - ABW: "^(?!.*bonaire).*\\baruba", - AUS: "australia", - AUT: "^(?!.*hungary).*austria|\\baustri.*\\bemp", - AZE: "azerbaijan", - BHS: "bahamas", - BHR: "bahrain", - BGD: "bangladesh|^(?=.*east).*paki?stan", - BRB: "barbados", - BLR: "belarus|byelo", - BEL: "^(?!.*luxem).*belgium", - BLZ: "belize|^(?=.*british).*honduras", - BEN: "benin|dahome", - BMU: "bermuda", - BTN: "bhutan", - BOL: "bolivia", - BES: "^(?=.*bonaire).*eustatius|^(?=.*carib).*netherlands|\\bbes.?islands", - BIH: "herzegovina|bosnia", - BWA: "botswana|bechuana", - BVT: "bouvet", - BRA: "brazil", - IOT: "british.?indian.?ocean", - BRN: "brunei", - BGR: "bulgaria", - BFA: "burkina|\\bfaso|upper.?volta", - BDI: "burundi", - KHM: "cambodia|kampuchea|khmer", - CMR: "cameroon", - CAN: "canada", - CPV: "verde", - CYM: "cayman", - CAF: "\\bcentral.african.republic", - TCD: "\\bchad", - CHL: "\\bchile", - CHN: "^(?!.*\\bmac)(?!.*\\bhong)(?!.*\\btai)(?!.*\\brep).*china|^(?=.*peo)(?=.*rep).*china", - CXR: "christmas", - CCK: "\\bcocos|keeling", - COL: "colombia", - COM: "comoro", - COD: "\\bdem.*congo|congo.*\\bdem|congo.*\\bdr|\\bdr.*congo|belgian.?congo|congo.?free.?state|kinshasa|zaire|l.opoldville|drc|droc|rdc", - COG: "^(?!.*\\bdem)(?!.*\\bdr)(?!.*kinshasa)(?!.*zaire)(?!.*belg)(?!.*l.opoldville)(?!.*free).*\\bcongo", - COK: "\\bcook", - CRI: "costa.?rica", - CIV: "ivoire|ivory", - HRV: "croatia", - CUB: "\\bcuba", - CUW: "^(?!.*bonaire).*\\bcura(c|ç)ao", - CYP: "cyprus", - CZE: "^(?=.*rep).*czech|czechia|bohemia", - CSK: "czechoslovakia", - DNK: "denmark", - DJI: "djibouti", - DMA: "dominica(?!n)", - DOM: "dominican.rep", - ECU: "ecuador", - EGY: "egypt", - SLV: "el.?salvador", - GNQ: "guine.*eq|eq.*guine|^(?=.*span).*guinea", - ERI: "eritrea", - EST: "estonia", - ETH: "ethiopia|abyssinia", - FLK: "falkland|malvinas", - FRO: "faroe|faeroe", - FJI: "fiji", - FIN: "finland", - FRA: "^(?!.*\\bdep)(?!.*martinique).*france|french.?republic|\\bgaul", - GUF: "^(?=.*french).*guiana", - PYF: "french.?polynesia|tahiti", - ATF: "french.?southern", - GAB: "gabon", - GMB: "gambia", - GEO: "^(?!.*south).*georgia", - DDR: "german.?democratic.?republic|democratic.?republic.*germany|east.germany", - DEU: "^(?!.*east).*germany|^(?=.*\\bfed.*\\brep).*german", - GHA: "ghana|gold.?coast", - GIB: "gibraltar", - GRC: "greece|hellenic|hellas", - GRL: "greenland", - GRD: "grenada", - GLP: "guadeloupe", - GUM: "\\bguam", - GTM: "guatemala", - GGY: "guernsey", - GIN: "^(?!.*eq)(?!.*span)(?!.*bissau)(?!.*portu)(?!.*new).*guinea", - GNB: "bissau|^(?=.*portu).*guinea", - GUY: "guyana|british.?guiana", - HTI: "haiti", - HMD: "heard.*mcdonald", - VAT: "holy.?see|vatican|papal.?st", - HND: "^(?!.*brit).*honduras", - HKG: "hong.?kong", - HUN: "^(?!.*austr).*hungary", - ISL: "iceland", - IND: "india(?!.*ocea)", - IDN: "indonesia", - IRN: "\\biran|persia", - IRQ: "\\biraq|mesopotamia", - IRL: "ireland", - IMN: "^(?=.*isle).*\\bman", - ISR: "israel", - ITA: "italy", - JAM: "jamaica", - JPN: "japan", - JEY: "jersey", - JOR: "jordan", - KAZ: "kazak", - KEN: "kenya|british.?east.?africa|east.?africa.?prot", - KIR: "kiribati", - PRK: "^(?=.*democrat).*\\bkorea|^(?=.*people).*\\bkorea|^(?=.*north).*\\bkorea|dprk", - KOR: "^(?!.*democrat)(?!.*people)(?!.*north).*\\bkorea", - KWT: "kuwait", - KGZ: "kyrgyz|kirghiz", - LAO: "\\blaos?\\b", - LVA: "latvia", - LBN: "lebanon", - LSO: "lesotho|basuto", - LBR: "liberia", - LBY: "libya", - LIE: "liechtenstein", - LTU: "lithuania", - LUX: "^(?!.*belg).*luxem", - MAC: "maca(o|u)", - MKD: "macedonia|fyrom", - MDG: "madagascar|malagasy", - MWI: "malawi|nyasa", - MYS: "malaysia", - MDV: "maldive", - MLI: "\\bmali\\b", - MLT: "\\bmalta", - MHL: "marshall", - MTQ: "martinique", - MRT: "mauritania", - MUS: "mauritius", - MYT: "\\bmayotte", - MEX: "\\bmexic", - FSM: "micronesia", - MDA: "moldov|b(a|e)ssarabia", - MCO: "monaco", - MNG: "mongolia", - MNE: "^(?!.*serbia).*montenegro", - MSR: "montserrat", - MAR: "morocco|\\bmaroc", - MOZ: "mozambique", - MMR: "myanmar|burma", - NAM: "namibia", - NRU: "nauru", - NPL: "nepal", - NLD: "^(?!.*\\bant)(?!.*\\bcarib).*netherlands", - ANT: "^(?=.*\\bant).*(nether|dutch)", - NCL: "new.?caledonia", - NZL: "new.?zealand", - NIC: "nicaragua", - NER: "\\bniger(?!ia)", - NGA: "nigeria", - NIU: "niue", - NFK: "norfolk", - MNP: "mariana", - NOR: "norway", - OMN: "\\boman|trucial", - PAK: "^(?!.*east).*paki?stan", - PLW: "palau", - PSE: "palestin|\\bgaza|west.?bank", - PAN: "panama", - PNG: "papua|new.?guinea", - PRY: "paraguay", - PER: "peru", - PHL: "philippines", - PCN: "pitcairn", - POL: "poland", - PRT: "portugal", - PRI: "puerto.?rico", - QAT: "qatar", - REU: "r(e|é)union", - ROU: "r(o|u|ou)mania", - RUS: "\\brussia|soviet.?union|u\\.?s\\.?s\\.?r|socialist.?republics", - RWA: "rwanda", - BLM: "barth(e|é)lemy", - SHN: "helena", - KNA: "kitts|\\bnevis", - LCA: "\\blucia", - MAF: "^(?=.*collectivity).*martin|^(?=.*france).*martin(?!ique)|^(?=.*french).*martin(?!ique)", - SPM: "miquelon", - VCT: "vincent", - WSM: "^(?!.*amer).*samoa", - SMR: "san.?marino", - STP: "\\bs(a|ã)o.?tom(e|é)", - SAU: "\\bsa\\w*.?arabia", - SEN: "senegal", - SRB: "^(?!.*monte).*serbia", - SYC: "seychell", - SLE: "sierra", - SGP: "singapore", - SXM: "^(?!.*martin)(?!.*saba).*maarten", - SVK: "^(?!.*cze).*slovak", - SVN: "slovenia", - SLB: "solomon", - SOM: "somali", - ZAF: "\\bs\\w*.?africa", - SGS: "south.?georgia|sandwich", - SSD: "\\bs\\w*.?sudan", - ESP: "spain", - LKA: "sri.?lanka|ceylon", - SDN: "^(?!.*\\bs(?!u)).*sudan", - SUR: "surinam|dutch.?guiana", - SJM: "svalbard", - SWZ: "swaziland", - SWE: "sweden", - CHE: "switz|swiss", - SYR: "syria", - TWN: "taiwan|taipei|formosa|^(?!.*peo)(?=.*rep).*china", - TJK: "tajik", - TZA: "tanzania", - THA: "thailand|\\bsiam", - TLS: "^(?=.*leste).*timor|^(?=.*east).*timor", - TGO: "togo", - TKL: "tokelau", - TON: "tonga", - TTO: "trinidad|tobago", - TUN: "tunisia", - TUR: "turkey", - TKM: "turkmen", - TCA: "turks", - TUV: "tuvalu", - UGA: "uganda", - UKR: "ukrain", - ARE: "emirates|^u\\.?a\\.?e\\.?$|united.?arab.?em", - GBR: "united.?kingdom|britain|^u\\.?k\\.?$", - USA: "united.?states|\\bu\\.?s\\.?a\\.?\\b|\\bu\\.?s\\.?\\b(?!.*islands)", - UMI: "minor.?outlying.?is", - URY: "uruguay", - UZB: "uzbek", - VUT: "vanuatu|new.?hebrides", - VEN: "venezuela", - VNM: "^(?!.*republic).*viet.?nam|^(?=.*socialist).*viet.?nam", - VGB: "^(?=.*\\bu\\.?\\s?k).*virgin|^(?=.*brit).*virgin|^(?=.*kingdom).*virgin", - VIR: "^(?=.*\\bu\\.?\\s?s).*virgin|^(?=.*states).*virgin", - WLF: "futuna|wallis", - ESH: "western.sahara", - YEM: "^(?!.*arab)(?!.*north)(?!.*sana)(?!.*peo)(?!.*dem)(?!.*south)(?!.*aden)(?!.*\\bp\\.?d\\.?r).*yemen", - YMD: "^(?=.*peo).*yemen|^(?!.*rep)(?=.*dem).*yemen|^(?=.*south).*yemen|^(?=.*aden).*yemen|^(?=.*\\bp\\.?d\\.?r).*yemen", - YUG: "yugoslavia", - ZMB: "zambia|northern.?rhodesia", - EAZ: "zanzibar", - ZWE: "zimbabwe|^(?!.*northern).*rhodesia" -}; - -},{}],173:[function(require,module,exports){ -!function() { - var d3 = { - version: "3.5.17" - }; - var d3_arraySlice = [].slice, d3_array = function(list) { - return d3_arraySlice.call(list); - }; - var d3_document = this.document; - function d3_documentElement(node) { - return node && (node.ownerDocument || node.document || node).documentElement; - } - function d3_window(node) { - return node && (node.ownerDocument && node.ownerDocument.defaultView || node.document && node || node.defaultView); - } - if (d3_document) { - try { - d3_array(d3_document.documentElement.childNodes)[0].nodeType; - } catch (e) { - d3_array = function(list) { - var i = list.length, array = new Array(i); - while (i--) array[i] = list[i]; - return array; - }; - } - } - if (!Date.now) Date.now = function() { - return +new Date(); - }; - if (d3_document) { - try { - d3_document.createElement("DIV").style.setProperty("opacity", 0, ""); - } catch (error) { - var d3_element_prototype = this.Element.prototype, d3_element_setAttribute = d3_element_prototype.setAttribute, d3_element_setAttributeNS = d3_element_prototype.setAttributeNS, d3_style_prototype = this.CSSStyleDeclaration.prototype, d3_style_setProperty = d3_style_prototype.setProperty; - d3_element_prototype.setAttribute = function(name, value) { - d3_element_setAttribute.call(this, name, value + ""); - }; - d3_element_prototype.setAttributeNS = function(space, local, value) { - d3_element_setAttributeNS.call(this, space, local, value + ""); - }; - d3_style_prototype.setProperty = function(name, value, priority) { - d3_style_setProperty.call(this, name, value + "", priority); - }; - } - } - d3.ascending = d3_ascending; - function d3_ascending(a, b) { - return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; - } - d3.descending = function(a, b) { - return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN; - }; - d3.min = function(array, f) { - var i = -1, n = array.length, a, b; - if (arguments.length === 1) { - while (++i < n) if ((b = array[i]) != null && b >= b) { - a = b; - break; - } - while (++i < n) if ((b = array[i]) != null && a > b) a = b; - } else { - while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { - a = b; - break; - } - while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b; - } - return a; - }; - d3.max = function(array, f) { - var i = -1, n = array.length, a, b; - if (arguments.length === 1) { - while (++i < n) if ((b = array[i]) != null && b >= b) { - a = b; - break; - } - while (++i < n) if ((b = array[i]) != null && b > a) a = b; - } else { - while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { - a = b; - break; - } - while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; - } - return a; - }; - d3.extent = function(array, f) { - var i = -1, n = array.length, a, b, c; - if (arguments.length === 1) { - while (++i < n) if ((b = array[i]) != null && b >= b) { - a = c = b; - break; - } - while (++i < n) if ((b = array[i]) != null) { - if (a > b) a = b; - if (c < b) c = b; - } - } else { - while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { - a = c = b; - break; - } - while (++i < n) if ((b = f.call(array, array[i], i)) != null) { - if (a > b) a = b; - if (c < b) c = b; - } - } - return [ a, c ]; - }; - function d3_number(x) { - return x === null ? NaN : +x; - } - function d3_numeric(x) { - return !isNaN(x); - } - d3.sum = function(array, f) { - var s = 0, n = array.length, a, i = -1; - if (arguments.length === 1) { - while (++i < n) if (d3_numeric(a = +array[i])) s += a; - } else { - while (++i < n) if (d3_numeric(a = +f.call(array, array[i], i))) s += a; - } - return s; - }; - d3.mean = function(array, f) { - var s = 0, n = array.length, a, i = -1, j = n; - if (arguments.length === 1) { - while (++i < n) if (d3_numeric(a = d3_number(array[i]))) s += a; else --j; - } else { - while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) s += a; else --j; - } - if (j) return s / j; - }; - d3.quantile = function(values, p) { - var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h; - return e ? v + e * (values[h] - v) : v; - }; - d3.median = function(array, f) { - var numbers = [], n = array.length, a, i = -1; - if (arguments.length === 1) { - while (++i < n) if (d3_numeric(a = d3_number(array[i]))) numbers.push(a); - } else { - while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) numbers.push(a); - } - if (numbers.length) return d3.quantile(numbers.sort(d3_ascending), .5); - }; - d3.variance = function(array, f) { - var n = array.length, m = 0, a, d, s = 0, i = -1, j = 0; - if (arguments.length === 1) { - while (++i < n) { - if (d3_numeric(a = d3_number(array[i]))) { - d = a - m; - m += d / ++j; - s += d * (a - m); - } - } - } else { - while (++i < n) { - if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) { - d = a - m; - m += d / ++j; - s += d * (a - m); - } - } - } - if (j > 1) return s / (j - 1); - }; - d3.deviation = function() { - var v = d3.variance.apply(this, arguments); - return v ? Math.sqrt(v) : v; - }; - function d3_bisector(compare) { - return { - left: function(a, x, lo, hi) { - if (arguments.length < 3) lo = 0; - if (arguments.length < 4) hi = a.length; - while (lo < hi) { - var mid = lo + hi >>> 1; - if (compare(a[mid], x) < 0) lo = mid + 1; else hi = mid; - } - return lo; - }, - right: function(a, x, lo, hi) { - if (arguments.length < 3) lo = 0; - if (arguments.length < 4) hi = a.length; - while (lo < hi) { - var mid = lo + hi >>> 1; - if (compare(a[mid], x) > 0) hi = mid; else lo = mid + 1; - } - return lo; - } - }; - } - var d3_bisect = d3_bisector(d3_ascending); - d3.bisectLeft = d3_bisect.left; - d3.bisect = d3.bisectRight = d3_bisect.right; - d3.bisector = function(f) { - return d3_bisector(f.length === 1 ? function(d, x) { - return d3_ascending(f(d), x); - } : f); - }; - d3.shuffle = function(array, i0, i1) { - if ((m = arguments.length) < 3) { - i1 = array.length; - if (m < 2) i0 = 0; - } - var m = i1 - i0, t, i; - while (m) { - i = Math.random() * m-- | 0; - t = array[m + i0], array[m + i0] = array[i + i0], array[i + i0] = t; - } - return array; - }; - d3.permute = function(array, indexes) { - var i = indexes.length, permutes = new Array(i); - while (i--) permutes[i] = array[indexes[i]]; - return permutes; - }; - d3.pairs = function(array) { - var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n); - while (i < n) pairs[i] = [ p0 = p1, p1 = array[++i] ]; - return pairs; - }; - d3.transpose = function(matrix) { - if (!(n = matrix.length)) return []; - for (var i = -1, m = d3.min(matrix, d3_transposeLength), transpose = new Array(m); ++i < m; ) { - for (var j = -1, n, row = transpose[i] = new Array(n); ++j < n; ) { - row[j] = matrix[j][i]; - } - } - return transpose; - }; - function d3_transposeLength(d) { - return d.length; - } - d3.zip = function() { - return d3.transpose(arguments); - }; - d3.keys = function(map) { - var keys = []; - for (var key in map) keys.push(key); - return keys; - }; - d3.values = function(map) { - var values = []; - for (var key in map) values.push(map[key]); - return values; - }; - d3.entries = function(map) { - var entries = []; - for (var key in map) entries.push({ - key: key, - value: map[key] - }); - return entries; - }; - d3.merge = function(arrays) { - var n = arrays.length, m, i = -1, j = 0, merged, array; - while (++i < n) j += arrays[i].length; - merged = new Array(j); - while (--n >= 0) { - array = arrays[n]; - m = array.length; - while (--m >= 0) { - merged[--j] = array[m]; - } - } - return merged; - }; - var abs = Math.abs; - d3.range = function(start, stop, step) { - if (arguments.length < 3) { - step = 1; - if (arguments.length < 2) { - stop = start; - start = 0; - } - } - if ((stop - start) / step === Infinity) throw new Error("infinite range"); - var range = [], k = d3_range_integerScale(abs(step)), i = -1, j; - start *= k, stop *= k, step *= k; - if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k); else while ((j = start + step * ++i) < stop) range.push(j / k); - return range; - }; - function d3_range_integerScale(x) { - var k = 1; - while (x * k % 1) k *= 10; - return k; - } - function d3_class(ctor, properties) { - for (var key in properties) { - Object.defineProperty(ctor.prototype, key, { - value: properties[key], - enumerable: false - }); - } - } - d3.map = function(object, f) { - var map = new d3_Map(); - if (object instanceof d3_Map) { - object.forEach(function(key, value) { - map.set(key, value); - }); - } else if (Array.isArray(object)) { - var i = -1, n = object.length, o; - if (arguments.length === 1) while (++i < n) map.set(i, object[i]); else while (++i < n) map.set(f.call(object, o = object[i], i), o); - } else { - for (var key in object) map.set(key, object[key]); - } - return map; - }; - function d3_Map() { - this._ = Object.create(null); - } - var d3_map_proto = "__proto__", d3_map_zero = "\x00"; - d3_class(d3_Map, { - has: d3_map_has, - get: function(key) { - return this._[d3_map_escape(key)]; - }, - set: function(key, value) { - return this._[d3_map_escape(key)] = value; - }, - remove: d3_map_remove, - keys: d3_map_keys, - values: function() { - var values = []; - for (var key in this._) values.push(this._[key]); - return values; - }, - entries: function() { - var entries = []; - for (var key in this._) entries.push({ - key: d3_map_unescape(key), - value: this._[key] - }); - return entries; - }, - size: d3_map_size, - empty: d3_map_empty, - forEach: function(f) { - for (var key in this._) f.call(this, d3_map_unescape(key), this._[key]); - } - }); - function d3_map_escape(key) { - return (key += "") === d3_map_proto || key[0] === d3_map_zero ? d3_map_zero + key : key; - } - function d3_map_unescape(key) { - return (key += "")[0] === d3_map_zero ? key.slice(1) : key; - } - function d3_map_has(key) { - return d3_map_escape(key) in this._; - } - function d3_map_remove(key) { - return (key = d3_map_escape(key)) in this._ && delete this._[key]; - } - function d3_map_keys() { - var keys = []; - for (var key in this._) keys.push(d3_map_unescape(key)); - return keys; - } - function d3_map_size() { - var size = 0; - for (var key in this._) ++size; - return size; - } - function d3_map_empty() { - for (var key in this._) return false; - return true; - } - d3.nest = function() { - var nest = {}, keys = [], sortKeys = [], sortValues, rollup; - function map(mapType, array, depth) { - if (depth >= keys.length) return rollup ? rollup.call(nest, array) : sortValues ? array.sort(sortValues) : array; - var i = -1, n = array.length, key = keys[depth++], keyValue, object, setter, valuesByKey = new d3_Map(), values; - while (++i < n) { - if (values = valuesByKey.get(keyValue = key(object = array[i]))) { - values.push(object); - } else { - valuesByKey.set(keyValue, [ object ]); - } - } - if (mapType) { - object = mapType(); - setter = function(keyValue, values) { - object.set(keyValue, map(mapType, values, depth)); - }; - } else { - object = {}; - setter = function(keyValue, values) { - object[keyValue] = map(mapType, values, depth); - }; - } - valuesByKey.forEach(setter); - return object; - } - function entries(map, depth) { - if (depth >= keys.length) return map; - var array = [], sortKey = sortKeys[depth++]; - map.forEach(function(key, keyMap) { - array.push({ - key: key, - values: entries(keyMap, depth) - }); - }); - return sortKey ? array.sort(function(a, b) { - return sortKey(a.key, b.key); - }) : array; - } - nest.map = function(array, mapType) { - return map(mapType, array, 0); - }; - nest.entries = function(array) { - return entries(map(d3.map, array, 0), 0); - }; - nest.key = function(d) { - keys.push(d); - return nest; - }; - nest.sortKeys = function(order) { - sortKeys[keys.length - 1] = order; - return nest; - }; - nest.sortValues = function(order) { - sortValues = order; - return nest; - }; - nest.rollup = function(f) { - rollup = f; - return nest; - }; - return nest; - }; - d3.set = function(array) { - var set = new d3_Set(); - if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]); - return set; - }; - function d3_Set() { - this._ = Object.create(null); - } - d3_class(d3_Set, { - has: d3_map_has, - add: function(key) { - this._[d3_map_escape(key += "")] = true; - return key; - }, - remove: d3_map_remove, - values: d3_map_keys, - size: d3_map_size, - empty: d3_map_empty, - forEach: function(f) { - for (var key in this._) f.call(this, d3_map_unescape(key)); - } - }); - d3.behavior = {}; - function d3_identity(d) { - return d; - } - d3.rebind = function(target, source) { - var i = 1, n = arguments.length, method; - while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]); - return target; - }; - function d3_rebind(target, source, method) { - return function() { - var value = method.apply(source, arguments); - return value === source ? target : value; - }; - } - function d3_vendorSymbol(object, name) { - if (name in object) return name; - name = name.charAt(0).toUpperCase() + name.slice(1); - for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) { - var prefixName = d3_vendorPrefixes[i] + name; - if (prefixName in object) return prefixName; - } - } - var d3_vendorPrefixes = [ "webkit", "ms", "moz", "Moz", "o", "O" ]; - function d3_noop() {} - d3.dispatch = function() { - var dispatch = new d3_dispatch(), i = -1, n = arguments.length; - while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); - return dispatch; - }; - function d3_dispatch() {} - d3_dispatch.prototype.on = function(type, listener) { - var i = type.indexOf("."), name = ""; - if (i >= 0) { - name = type.slice(i + 1); - type = type.slice(0, i); - } - if (type) return arguments.length < 2 ? this[type].on(name) : this[type].on(name, listener); - if (arguments.length === 2) { - if (listener == null) for (type in this) { - if (this.hasOwnProperty(type)) this[type].on(name, null); - } - return this; - } - }; - function d3_dispatch_event(dispatch) { - var listeners = [], listenerByName = new d3_Map(); - function event() { - var z = listeners, i = -1, n = z.length, l; - while (++i < n) if (l = z[i].on) l.apply(this, arguments); - return dispatch; - } - event.on = function(name, listener) { - var l = listenerByName.get(name), i; - if (arguments.length < 2) return l && l.on; - if (l) { - l.on = null; - listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1)); - listenerByName.remove(name); - } - if (listener) listeners.push(listenerByName.set(name, { - on: listener - })); - return dispatch; - }; - return event; - } - d3.event = null; - function d3_eventPreventDefault() { - d3.event.preventDefault(); - } - function d3_eventSource() { - var e = d3.event, s; - while (s = e.sourceEvent) e = s; - return e; - } - function d3_eventDispatch(target) { - var dispatch = new d3_dispatch(), i = 0, n = arguments.length; - while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch); - dispatch.of = function(thiz, argumentz) { - return function(e1) { - try { - var e0 = e1.sourceEvent = d3.event; - e1.target = target; - d3.event = e1; - dispatch[e1.type].apply(thiz, argumentz); - } finally { - d3.event = e0; - } - }; - }; - return dispatch; - } - d3.requote = function(s) { - return s.replace(d3_requote_re, "\\$&"); - }; - var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g; - var d3_subclass = {}.__proto__ ? function(object, prototype) { - object.__proto__ = prototype; - } : function(object, prototype) { - for (var property in prototype) object[property] = prototype[property]; - }; - function d3_selection(groups) { - d3_subclass(groups, d3_selectionPrototype); - return groups; - } - var d3_select = function(s, n) { - return n.querySelector(s); - }, d3_selectAll = function(s, n) { - return n.querySelectorAll(s); - }, d3_selectMatches = function(n, s) { - var d3_selectMatcher = n.matches || n[d3_vendorSymbol(n, "matchesSelector")]; - d3_selectMatches = function(n, s) { - return d3_selectMatcher.call(n, s); - }; - return d3_selectMatches(n, s); - }; - if (typeof Sizzle === "function") { - d3_select = function(s, n) { - return Sizzle(s, n)[0] || null; - }; - d3_selectAll = Sizzle; - d3_selectMatches = Sizzle.matchesSelector; - } - d3.selection = function() { - return d3.select(d3_document.documentElement); - }; - var d3_selectionPrototype = d3.selection.prototype = []; - d3_selectionPrototype.select = function(selector) { - var subgroups = [], subgroup, subnode, group, node; - selector = d3_selection_selector(selector); - for (var j = -1, m = this.length; ++j < m; ) { - subgroups.push(subgroup = []); - subgroup.parentNode = (group = this[j]).parentNode; - for (var i = -1, n = group.length; ++i < n; ) { - if (node = group[i]) { - subgroup.push(subnode = selector.call(node, node.__data__, i, j)); - if (subnode && "__data__" in node) subnode.__data__ = node.__data__; - } else { - subgroup.push(null); - } - } - } - return d3_selection(subgroups); - }; - function d3_selection_selector(selector) { - return typeof selector === "function" ? selector : function() { - return d3_select(selector, this); - }; - } - d3_selectionPrototype.selectAll = function(selector) { - var subgroups = [], subgroup, node; - selector = d3_selection_selectorAll(selector); - for (var j = -1, m = this.length; ++j < m; ) { - for (var group = this[j], i = -1, n = group.length; ++i < n; ) { - if (node = group[i]) { - subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j))); - subgroup.parentNode = node; - } - } - } - return d3_selection(subgroups); - }; - function d3_selection_selectorAll(selector) { - return typeof selector === "function" ? selector : function() { - return d3_selectAll(selector, this); - }; - } - var d3_nsXhtml = "http://www.w3.org/1999/xhtml"; - var d3_nsPrefix = { - svg: "http://www.w3.org/2000/svg", - xhtml: d3_nsXhtml, - xlink: "http://www.w3.org/1999/xlink", - xml: "http://www.w3.org/XML/1998/namespace", - xmlns: "http://www.w3.org/2000/xmlns/" - }; - d3.ns = { - prefix: d3_nsPrefix, - qualify: function(name) { - var i = name.indexOf(":"), prefix = name; - if (i >= 0 && (prefix = name.slice(0, i)) !== "xmlns") name = name.slice(i + 1); - return d3_nsPrefix.hasOwnProperty(prefix) ? { - space: d3_nsPrefix[prefix], - local: name - } : name; - } - }; - d3_selectionPrototype.attr = function(name, value) { - if (arguments.length < 2) { - if (typeof name === "string") { - var node = this.node(); - name = d3.ns.qualify(name); - return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name); - } - for (value in name) this.each(d3_selection_attr(value, name[value])); - return this; - } - return this.each(d3_selection_attr(name, value)); - }; - function d3_selection_attr(name, value) { - name = d3.ns.qualify(name); - function attrNull() { - this.removeAttribute(name); - } - function attrNullNS() { - this.removeAttributeNS(name.space, name.local); - } - function attrConstant() { - this.setAttribute(name, value); - } - function attrConstantNS() { - this.setAttributeNS(name.space, name.local, value); - } - function attrFunction() { - var x = value.apply(this, arguments); - if (x == null) this.removeAttribute(name); else this.setAttribute(name, x); - } - function attrFunctionNS() { - var x = value.apply(this, arguments); - if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x); - } - return value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant; - } - function d3_collapse(s) { - return s.trim().replace(/\s+/g, " "); - } - d3_selectionPrototype.classed = function(name, value) { - if (arguments.length < 2) { - if (typeof name === "string") { - var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1; - if (value = node.classList) { - while (++i < n) if (!value.contains(name[i])) return false; - } else { - value = node.getAttribute("class"); - while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false; - } - return true; - } - for (value in name) this.each(d3_selection_classed(value, name[value])); - return this; - } - return this.each(d3_selection_classed(name, value)); - }; - function d3_selection_classedRe(name) { - return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g"); - } - function d3_selection_classes(name) { - return (name + "").trim().split(/^|\s+/); - } - function d3_selection_classed(name, value) { - name = d3_selection_classes(name).map(d3_selection_classedName); - var n = name.length; - function classedConstant() { - var i = -1; - while (++i < n) name[i](this, value); - } - function classedFunction() { - var i = -1, x = value.apply(this, arguments); - while (++i < n) name[i](this, x); - } - return typeof value === "function" ? classedFunction : classedConstant; - } - function d3_selection_classedName(name) { - var re = d3_selection_classedRe(name); - return function(node, value) { - if (c = node.classList) return value ? c.add(name) : c.remove(name); - var c = node.getAttribute("class") || ""; - if (value) { - re.lastIndex = 0; - if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name)); - } else { - node.setAttribute("class", d3_collapse(c.replace(re, " "))); - } - }; - } - d3_selectionPrototype.style = function(name, value, priority) { - var n = arguments.length; - if (n < 3) { - if (typeof name !== "string") { - if (n < 2) value = ""; - for (priority in name) this.each(d3_selection_style(priority, name[priority], value)); - return this; - } - if (n < 2) { - var node = this.node(); - return d3_window(node).getComputedStyle(node, null).getPropertyValue(name); - } - priority = ""; - } - return this.each(d3_selection_style(name, value, priority)); - }; - function d3_selection_style(name, value, priority) { - function styleNull() { - this.style.removeProperty(name); - } - function styleConstant() { - this.style.setProperty(name, value, priority); - } - function styleFunction() { - var x = value.apply(this, arguments); - if (x == null) this.style.removeProperty(name); else this.style.setProperty(name, x, priority); - } - return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant; - } - d3_selectionPrototype.property = function(name, value) { - if (arguments.length < 2) { - if (typeof name === "string") return this.node()[name]; - for (value in name) this.each(d3_selection_property(value, name[value])); - return this; - } - return this.each(d3_selection_property(name, value)); - }; - function d3_selection_property(name, value) { - function propertyNull() { - delete this[name]; - } - function propertyConstant() { - this[name] = value; - } - function propertyFunction() { - var x = value.apply(this, arguments); - if (x == null) delete this[name]; else this[name] = x; - } - return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant; - } - d3_selectionPrototype.text = function(value) { - return arguments.length ? this.each(typeof value === "function" ? function() { - var v = value.apply(this, arguments); - this.textContent = v == null ? "" : v; - } : value == null ? function() { - this.textContent = ""; - } : function() { - this.textContent = value; - }) : this.node().textContent; - }; - d3_selectionPrototype.html = function(value) { - return arguments.length ? this.each(typeof value === "function" ? function() { - var v = value.apply(this, arguments); - this.innerHTML = v == null ? "" : v; - } : value == null ? function() { - this.innerHTML = ""; - } : function() { - this.innerHTML = value; - }) : this.node().innerHTML; - }; - d3_selectionPrototype.append = function(name) { - name = d3_selection_creator(name); - return this.select(function() { - return this.appendChild(name.apply(this, arguments)); - }); - }; - function d3_selection_creator(name) { - function create() { - var document = this.ownerDocument, namespace = this.namespaceURI; - return namespace === d3_nsXhtml && document.documentElement.namespaceURI === d3_nsXhtml ? document.createElement(name) : document.createElementNS(namespace, name); - } - function createNS() { - return this.ownerDocument.createElementNS(name.space, name.local); - } - return typeof name === "function" ? name : (name = d3.ns.qualify(name)).local ? createNS : create; - } - d3_selectionPrototype.insert = function(name, before) { - name = d3_selection_creator(name); - before = d3_selection_selector(before); - return this.select(function() { - return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments) || null); - }); - }; - d3_selectionPrototype.remove = function() { - return this.each(d3_selectionRemove); - }; - function d3_selectionRemove() { - var parent = this.parentNode; - if (parent) parent.removeChild(this); - } - d3_selectionPrototype.data = function(value, key) { - var i = -1, n = this.length, group, node; - if (!arguments.length) { - value = new Array(n = (group = this[0]).length); - while (++i < n) { - if (node = group[i]) { - value[i] = node.__data__; - } - } - return value; - } - function bind(group, groupData) { - var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData; - if (key) { - var nodeByKeyValue = new d3_Map(), keyValues = new Array(n), keyValue; - for (i = -1; ++i < n; ) { - if (node = group[i]) { - if (nodeByKeyValue.has(keyValue = key.call(node, node.__data__, i))) { - exitNodes[i] = node; - } else { - nodeByKeyValue.set(keyValue, node); +module.exports = range; + +function range(ids, coords, minX, minY, maxX, maxY, nodeSize) { + var stack = [0, ids.length - 1, 0]; + var result = []; + var x, y; + + while (stack.length) { + var axis = stack.pop(); + var right = stack.pop(); + var left = stack.pop(); + + if (right - left <= nodeSize) { + for (var i = left; i <= right; i++) { + x = coords[2 * i]; + y = coords[2 * i + 1]; + if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]); } - keyValues[i] = keyValue; - } - } - for (i = -1; ++i < m; ) { - if (!(node = nodeByKeyValue.get(keyValue = key.call(groupData, nodeData = groupData[i], i)))) { - enterNodes[i] = d3_selection_dataNode(nodeData); - } else if (node !== true) { - updateNodes[i] = node; - node.__data__ = nodeData; - } - nodeByKeyValue.set(keyValue, true); - } - for (i = -1; ++i < n; ) { - if (i in keyValues && nodeByKeyValue.get(keyValues[i]) !== true) { - exitNodes[i] = group[i]; - } - } - } else { - for (i = -1; ++i < n0; ) { - node = group[i]; - nodeData = groupData[i]; - if (node) { - node.__data__ = nodeData; - updateNodes[i] = node; - } else { - enterNodes[i] = d3_selection_dataNode(nodeData); - } - } - for (;i < m; ++i) { - enterNodes[i] = d3_selection_dataNode(groupData[i]); - } - for (;i < n; ++i) { - exitNodes[i] = group[i]; - } - } - enterNodes.update = updateNodes; - enterNodes.parentNode = updateNodes.parentNode = exitNodes.parentNode = group.parentNode; - enter.push(enterNodes); - update.push(updateNodes); - exit.push(exitNodes); - } - var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]); - if (typeof value === "function") { - while (++i < n) { - bind(group = this[i], value.call(group, group.parentNode.__data__, i)); - } - } else { - while (++i < n) { - bind(group = this[i], value); - } - } - update.enter = function() { - return enter; - }; - update.exit = function() { - return exit; - }; - return update; - }; - function d3_selection_dataNode(data) { - return { - __data__: data - }; - } - d3_selectionPrototype.datum = function(value) { - return arguments.length ? this.property("__data__", value) : this.property("__data__"); - }; - d3_selectionPrototype.filter = function(filter) { - var subgroups = [], subgroup, group, node; - if (typeof filter !== "function") filter = d3_selection_filter(filter); - for (var j = 0, m = this.length; j < m; j++) { - subgroups.push(subgroup = []); - subgroup.parentNode = (group = this[j]).parentNode; - for (var i = 0, n = group.length; i < n; i++) { - if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { - subgroup.push(node); - } - } - } - return d3_selection(subgroups); - }; - function d3_selection_filter(selector) { - return function() { - return d3_selectMatches(this, selector); - }; - } - d3_selectionPrototype.order = function() { - for (var j = -1, m = this.length; ++j < m; ) { - for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0; ) { - if (node = group[i]) { - if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next); - next = node; - } - } - } - return this; - }; - d3_selectionPrototype.sort = function(comparator) { - comparator = d3_selection_sortComparator.apply(this, arguments); - for (var j = -1, m = this.length; ++j < m; ) this[j].sort(comparator); - return this.order(); - }; - function d3_selection_sortComparator(comparator) { - if (!arguments.length) comparator = d3_ascending; - return function(a, b) { - return a && b ? comparator(a.__data__, b.__data__) : !a - !b; - }; - } - d3_selectionPrototype.each = function(callback) { - return d3_selection_each(this, function(node, i, j) { - callback.call(node, node.__data__, i, j); - }); - }; - function d3_selection_each(groups, callback) { - for (var j = 0, m = groups.length; j < m; j++) { - for (var group = groups[j], i = 0, n = group.length, node; i < n; i++) { - if (node = group[i]) callback(node, i, j); - } - } - return groups; - } - d3_selectionPrototype.call = function(callback) { - var args = d3_array(arguments); - callback.apply(args[0] = this, args); - return this; - }; - d3_selectionPrototype.empty = function() { - return !this.node(); - }; - d3_selectionPrototype.node = function() { - for (var j = 0, m = this.length; j < m; j++) { - for (var group = this[j], i = 0, n = group.length; i < n; i++) { - var node = group[i]; - if (node) return node; - } - } - return null; - }; - d3_selectionPrototype.size = function() { - var n = 0; - d3_selection_each(this, function() { - ++n; - }); - return n; - }; - function d3_selection_enter(selection) { - d3_subclass(selection, d3_selection_enterPrototype); - return selection; - } - var d3_selection_enterPrototype = []; - d3.selection.enter = d3_selection_enter; - d3.selection.enter.prototype = d3_selection_enterPrototype; - d3_selection_enterPrototype.append = d3_selectionPrototype.append; - d3_selection_enterPrototype.empty = d3_selectionPrototype.empty; - d3_selection_enterPrototype.node = d3_selectionPrototype.node; - d3_selection_enterPrototype.call = d3_selectionPrototype.call; - d3_selection_enterPrototype.size = d3_selectionPrototype.size; - d3_selection_enterPrototype.select = function(selector) { - var subgroups = [], subgroup, subnode, upgroup, group, node; - for (var j = -1, m = this.length; ++j < m; ) { - upgroup = (group = this[j]).update; - subgroups.push(subgroup = []); - subgroup.parentNode = group.parentNode; - for (var i = -1, n = group.length; ++i < n; ) { - if (node = group[i]) { - subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i, j)); - subnode.__data__ = node.__data__; - } else { - subgroup.push(null); - } - } - } - return d3_selection(subgroups); - }; - d3_selection_enterPrototype.insert = function(name, before) { - if (arguments.length < 2) before = d3_selection_enterInsertBefore(this); - return d3_selectionPrototype.insert.call(this, name, before); - }; - function d3_selection_enterInsertBefore(enter) { - var i0, j0; - return function(d, i, j) { - var group = enter[j].update, n = group.length, node; - if (j != j0) j0 = j, i0 = 0; - if (i >= i0) i0 = i + 1; - while (!(node = group[i0]) && ++i0 < n) ; - return node; - }; - } - d3.select = function(node) { - var group; - if (typeof node === "string") { - group = [ d3_select(node, d3_document) ]; - group.parentNode = d3_document.documentElement; - } else { - group = [ node ]; - group.parentNode = d3_documentElement(node); - } - return d3_selection([ group ]); - }; - d3.selectAll = function(nodes) { - var group; - if (typeof nodes === "string") { - group = d3_array(d3_selectAll(nodes, d3_document)); - group.parentNode = d3_document.documentElement; - } else { - group = d3_array(nodes); - group.parentNode = null; - } - return d3_selection([ group ]); - }; - d3_selectionPrototype.on = function(type, listener, capture) { - var n = arguments.length; - if (n < 3) { - if (typeof type !== "string") { - if (n < 2) listener = false; - for (capture in type) this.each(d3_selection_on(capture, type[capture], listener)); - return this; - } - if (n < 2) return (n = this.node()["__on" + type]) && n._; - capture = false; - } - return this.each(d3_selection_on(type, listener, capture)); - }; - function d3_selection_on(type, listener, capture) { - var name = "__on" + type, i = type.indexOf("."), wrap = d3_selection_onListener; - if (i > 0) type = type.slice(0, i); - var filter = d3_selection_onFilters.get(type); - if (filter) type = filter, wrap = d3_selection_onFilter; - function onRemove() { - var l = this[name]; - if (l) { - this.removeEventListener(type, l, l.$); - delete this[name]; - } - } - function onAdd() { - var l = wrap(listener, d3_array(arguments)); - onRemove.call(this); - this.addEventListener(type, this[name] = l, l.$ = capture); - l._ = listener; - } - function removeAll() { - var re = new RegExp("^__on([^.]+)" + d3.requote(type) + "$"), match; - for (var name in this) { - if (match = name.match(re)) { - var l = this[name]; - this.removeEventListener(match[1], l, l.$); - delete this[name]; - } - } - } - return i ? listener ? onAdd : onRemove : listener ? d3_noop : removeAll; - } - var d3_selection_onFilters = d3.map({ - mouseenter: "mouseover", - mouseleave: "mouseout" - }); - if (d3_document) { - d3_selection_onFilters.forEach(function(k) { - if ("on" + k in d3_document) d3_selection_onFilters.remove(k); - }); - } - function d3_selection_onListener(listener, argumentz) { - return function(e) { - var o = d3.event; - d3.event = e; - argumentz[0] = this.__data__; - try { - listener.apply(this, argumentz); - } finally { - d3.event = o; - } - }; - } - function d3_selection_onFilter(listener, argumentz) { - var l = d3_selection_onListener(listener, argumentz); - return function(e) { - var target = this, related = e.relatedTarget; - if (!related || related !== target && !(related.compareDocumentPosition(target) & 8)) { - l.call(target, e); - } - }; - } - var d3_event_dragSelect, d3_event_dragId = 0; - function d3_event_dragSuppress(node) { - var name = ".dragsuppress-" + ++d3_event_dragId, click = "click" + name, w = d3.select(d3_window(node)).on("touchmove" + name, d3_eventPreventDefault).on("dragstart" + name, d3_eventPreventDefault).on("selectstart" + name, d3_eventPreventDefault); - if (d3_event_dragSelect == null) { - d3_event_dragSelect = "onselectstart" in node ? false : d3_vendorSymbol(node.style, "userSelect"); - } - if (d3_event_dragSelect) { - var style = d3_documentElement(node).style, select = style[d3_event_dragSelect]; - style[d3_event_dragSelect] = "none"; - } - return function(suppressClick) { - w.on(name, null); - if (d3_event_dragSelect) style[d3_event_dragSelect] = select; - if (suppressClick) { - var off = function() { - w.on(click, null); - }; - w.on(click, function() { - d3_eventPreventDefault(); - off(); - }, true); - setTimeout(off, 0); - } - }; - } - d3.mouse = function(container) { - return d3_mousePoint(container, d3_eventSource()); - }; - var d3_mouse_bug44083 = this.navigator && /WebKit/.test(this.navigator.userAgent) ? -1 : 0; - function d3_mousePoint(container, e) { - if (e.changedTouches) e = e.changedTouches[0]; - var svg = container.ownerSVGElement || container; - if (svg.createSVGPoint) { - var point = svg.createSVGPoint(); - if (d3_mouse_bug44083 < 0) { - var window = d3_window(container); - if (window.scrollX || window.scrollY) { - svg = d3.select("body").append("svg").style({ - position: "absolute", - top: 0, - left: 0, - margin: 0, - padding: 0, - border: "none" - }, "important"); - var ctm = svg[0][0].getScreenCTM(); - d3_mouse_bug44083 = !(ctm.f || ctm.e); - svg.remove(); - } - } - if (d3_mouse_bug44083) point.x = e.pageX, point.y = e.pageY; else point.x = e.clientX, - point.y = e.clientY; - point = point.matrixTransform(container.getScreenCTM().inverse()); - return [ point.x, point.y ]; - } - var rect = container.getBoundingClientRect(); - return [ e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop ]; - } - d3.touch = function(container, touches, identifier) { - if (arguments.length < 3) identifier = touches, touches = d3_eventSource().changedTouches; - if (touches) for (var i = 0, n = touches.length, touch; i < n; ++i) { - if ((touch = touches[i]).identifier === identifier) { - return d3_mousePoint(container, touch); - } - } - }; - d3.behavior.drag = function() { - var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"), origin = null, mousedown = dragstart(d3_noop, d3.mouse, d3_window, "mousemove", "mouseup"), touchstart = dragstart(d3_behavior_dragTouchId, d3.touch, d3_identity, "touchmove", "touchend"); - function drag() { - this.on("mousedown.drag", mousedown).on("touchstart.drag", touchstart); - } - function dragstart(id, position, subject, move, end) { - return function() { - var that = this, target = d3.event.target.correspondingElement || d3.event.target, parent = that.parentNode, dispatch = event.of(that, arguments), dragged = 0, dragId = id(), dragName = ".drag" + (dragId == null ? "" : "-" + dragId), dragOffset, dragSubject = d3.select(subject(target)).on(move + dragName, moved).on(end + dragName, ended), dragRestore = d3_event_dragSuppress(target), position0 = position(parent, dragId); - if (origin) { - dragOffset = origin.apply(that, arguments); - dragOffset = [ dragOffset.x - position0[0], dragOffset.y - position0[1] ]; - } else { - dragOffset = [ 0, 0 ]; - } - dispatch({ - type: "dragstart" - }); - function moved() { - var position1 = position(parent, dragId), dx, dy; - if (!position1) return; - dx = position1[0] - position0[0]; - dy = position1[1] - position0[1]; - dragged |= dx | dy; - position0 = position1; - dispatch({ - type: "drag", - x: position1[0] + dragOffset[0], - y: position1[1] + dragOffset[1], - dx: dx, - dy: dy - }); - } - function ended() { - if (!position(parent, dragId)) return; - dragSubject.on(move + dragName, null).on(end + dragName, null); - dragRestore(dragged); - dispatch({ - type: "dragend" - }); - } - }; - } - drag.origin = function(x) { - if (!arguments.length) return origin; - origin = x; - return drag; - }; - return d3.rebind(drag, event, "on"); - }; - function d3_behavior_dragTouchId() { - return d3.event.changedTouches[0].identifier; - } - d3.touches = function(container, touches) { - if (arguments.length < 2) touches = d3_eventSource().touches; - return touches ? d3_array(touches).map(function(touch) { - var point = d3_mousePoint(container, touch); - point.identifier = touch.identifier; - return point; - }) : []; - }; - var ε = 1e-6, ε2 = ε * ε, π = Math.PI, τ = 2 * π, τε = τ - ε, halfπ = π / 2, d3_radians = π / 180, d3_degrees = 180 / π; - function d3_sgn(x) { - return x > 0 ? 1 : x < 0 ? -1 : 0; - } - function d3_cross2d(a, b, c) { - return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); - } - function d3_acos(x) { - return x > 1 ? 0 : x < -1 ? π : Math.acos(x); - } - function d3_asin(x) { - return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x); - } - function d3_sinh(x) { - return ((x = Math.exp(x)) - 1 / x) / 2; - } - function d3_cosh(x) { - return ((x = Math.exp(x)) + 1 / x) / 2; - } - function d3_tanh(x) { - return ((x = Math.exp(2 * x)) - 1) / (x + 1); - } - function d3_haversin(x) { - return (x = Math.sin(x / 2)) * x; - } - var ρ = Math.SQRT2, ρ2 = 2, ρ4 = 4; - d3.interpolateZoom = function(p0, p1) { - var ux0 = p0[0], uy0 = p0[1], w0 = p0[2], ux1 = p1[0], uy1 = p1[1], w1 = p1[2], dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, i, S; - if (d2 < ε2) { - S = Math.log(w1 / w0) / ρ; - i = function(t) { - return [ ux0 + t * dx, uy0 + t * dy, w0 * Math.exp(ρ * t * S) ]; - }; - } else { - var d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1), b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0), r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1); - S = (r1 - r0) / ρ; - i = function(t) { - var s = t * S, coshr0 = d3_cosh(r0), u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0)); - return [ ux0 + u * dx, uy0 + u * dy, w0 * coshr0 / d3_cosh(ρ * s + r0) ]; - }; - } - i.duration = S * 1e3; - return i; - }; - d3.behavior.zoom = function() { - var view = { - x: 0, - y: 0, - k: 1 - }, translate0, center0, center, size = [ 960, 500 ], scaleExtent = d3_behavior_zoomInfinity, duration = 250, zooming = 0, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", mousewheelTimer, touchstart = "touchstart.zoom", touchtime, event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"), x0, x1, y0, y1; - if (!d3_behavior_zoomWheel) { - d3_behavior_zoomWheel = "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { - return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); - }, "wheel") : "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { - return d3.event.wheelDelta; - }, "mousewheel") : (d3_behavior_zoomDelta = function() { - return -d3.event.detail; - }, "MozMousePixelScroll"); - } - function zoom(g) { - g.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on("dblclick.zoom", dblclicked).on(touchstart, touchstarted); - } - zoom.event = function(g) { - g.each(function() { - var dispatch = event.of(this, arguments), view1 = view; - if (d3_transitionInheritId) { - d3.select(this).transition().each("start.zoom", function() { - view = this.__chart__ || { - x: 0, - y: 0, - k: 1 - }; - zoomstarted(dispatch); - }).tween("zoom:zoom", function() { - var dx = size[0], dy = size[1], cx = center0 ? center0[0] : dx / 2, cy = center0 ? center0[1] : dy / 2, i = d3.interpolateZoom([ (cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k ], [ (cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k ]); - return function(t) { - var l = i(t), k = dx / l[2]; - this.__chart__ = view = { - x: cx - l[0] * k, - y: cy - l[1] * k, - k: k - }; - zoomed(dispatch); - }; - }).each("interrupt.zoom", function() { - zoomended(dispatch); - }).each("end.zoom", function() { - zoomended(dispatch); - }); - } else { - this.__chart__ = view; - zoomstarted(dispatch); - zoomed(dispatch); - zoomended(dispatch); - } - }); - }; - zoom.translate = function(_) { - if (!arguments.length) return [ view.x, view.y ]; - view = { - x: +_[0], - y: +_[1], - k: view.k - }; - rescale(); - return zoom; - }; - zoom.scale = function(_) { - if (!arguments.length) return view.k; - view = { - x: view.x, - y: view.y, - k: null - }; - scaleTo(+_); - rescale(); - return zoom; - }; - zoom.scaleExtent = function(_) { - if (!arguments.length) return scaleExtent; - scaleExtent = _ == null ? d3_behavior_zoomInfinity : [ +_[0], +_[1] ]; - return zoom; - }; - zoom.center = function(_) { - if (!arguments.length) return center; - center = _ && [ +_[0], +_[1] ]; - return zoom; - }; - zoom.size = function(_) { - if (!arguments.length) return size; - size = _ && [ +_[0], +_[1] ]; - return zoom; - }; - zoom.duration = function(_) { - if (!arguments.length) return duration; - duration = +_; - return zoom; - }; - zoom.x = function(z) { - if (!arguments.length) return x1; - x1 = z; - x0 = z.copy(); - view = { - x: 0, - y: 0, - k: 1 - }; - return zoom; - }; - zoom.y = function(z) { - if (!arguments.length) return y1; - y1 = z; - y0 = z.copy(); - view = { - x: 0, - y: 0, - k: 1 - }; - return zoom; - }; - function location(p) { - return [ (p[0] - view.x) / view.k, (p[1] - view.y) / view.k ]; - } - function point(l) { - return [ l[0] * view.k + view.x, l[1] * view.k + view.y ]; - } - function scaleTo(s) { - view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s)); - } - function translateTo(p, l) { - l = point(l); - view.x += p[0] - l[0]; - view.y += p[1] - l[1]; - } - function zoomTo(that, p, l, k) { - that.__chart__ = { - x: view.x, - y: view.y, - k: view.k - }; - scaleTo(Math.pow(2, k)); - translateTo(center0 = p, l); - that = d3.select(that); - if (duration > 0) that = that.transition().duration(duration); - that.call(zoom.event); - } - function rescale() { - if (x1) x1.domain(x0.range().map(function(x) { - return (x - view.x) / view.k; - }).map(x0.invert)); - if (y1) y1.domain(y0.range().map(function(y) { - return (y - view.y) / view.k; - }).map(y0.invert)); - } - function zoomstarted(dispatch) { - if (!zooming++) dispatch({ - type: "zoomstart" - }); - } - function zoomed(dispatch) { - rescale(); - dispatch({ - type: "zoom", - scale: view.k, - translate: [ view.x, view.y ] - }); - } - function zoomended(dispatch) { - if (!--zooming) dispatch({ - type: "zoomend" - }), center0 = null; - } - function mousedowned() { - var that = this, dispatch = event.of(that, arguments), dragged = 0, subject = d3.select(d3_window(that)).on(mousemove, moved).on(mouseup, ended), location0 = location(d3.mouse(that)), dragRestore = d3_event_dragSuppress(that); - d3_selection_interrupt.call(that); - zoomstarted(dispatch); - function moved() { - dragged = 1; - translateTo(d3.mouse(that), location0); - zoomed(dispatch); - } - function ended() { - subject.on(mousemove, null).on(mouseup, null); - dragRestore(dragged); - zoomended(dispatch); - } - } - function touchstarted() { - var that = this, dispatch = event.of(that, arguments), locations0 = {}, distance0 = 0, scale0, zoomName = ".zoom-" + d3.event.changedTouches[0].identifier, touchmove = "touchmove" + zoomName, touchend = "touchend" + zoomName, targets = [], subject = d3.select(that), dragRestore = d3_event_dragSuppress(that); - started(); - zoomstarted(dispatch); - subject.on(mousedown, null).on(touchstart, started); - function relocate() { - var touches = d3.touches(that); - scale0 = view.k; - touches.forEach(function(t) { - if (t.identifier in locations0) locations0[t.identifier] = location(t); - }); - return touches; - } - function started() { - var target = d3.event.target; - d3.select(target).on(touchmove, moved).on(touchend, ended); - targets.push(target); - var changed = d3.event.changedTouches; - for (var i = 0, n = changed.length; i < n; ++i) { - locations0[changed[i].identifier] = null; - } - var touches = relocate(), now = Date.now(); - if (touches.length === 1) { - if (now - touchtime < 500) { - var p = touches[0]; - zoomTo(that, p, locations0[p.identifier], Math.floor(Math.log(view.k) / Math.LN2) + 1); - d3_eventPreventDefault(); - } - touchtime = now; - } else if (touches.length > 1) { - var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1]; - distance0 = dx * dx + dy * dy; - } - } - function moved() { - var touches = d3.touches(that), p0, l0, p1, l1; - d3_selection_interrupt.call(that); - for (var i = 0, n = touches.length; i < n; ++i, l1 = null) { - p1 = touches[i]; - if (l1 = locations0[p1.identifier]) { - if (l0) break; - p0 = p1, l0 = l1; - } - } - if (l1) { - var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1, scale1 = distance0 && Math.sqrt(distance1 / distance0); - p0 = [ (p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2 ]; - l0 = [ (l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2 ]; - scaleTo(scale1 * scale0); - } - touchtime = null; - translateTo(p0, l0); - zoomed(dispatch); - } - function ended() { - if (d3.event.touches.length) { - var changed = d3.event.changedTouches; - for (var i = 0, n = changed.length; i < n; ++i) { - delete locations0[changed[i].identifier]; - } - for (var identifier in locations0) { - return void relocate(); - } - } - d3.selectAll(targets).on(zoomName, null); - subject.on(mousedown, mousedowned).on(touchstart, touchstarted); - dragRestore(); - zoomended(dispatch); - } - } - function mousewheeled() { - var dispatch = event.of(this, arguments); - if (mousewheelTimer) clearTimeout(mousewheelTimer); else d3_selection_interrupt.call(this), - translate0 = location(center0 = center || d3.mouse(this)), zoomstarted(dispatch); - mousewheelTimer = setTimeout(function() { - mousewheelTimer = null; - zoomended(dispatch); - }, 50); - d3_eventPreventDefault(); - scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k); - translateTo(center0, translate0); - zoomed(dispatch); - } - function dblclicked() { - var p = d3.mouse(this), k = Math.log(view.k) / Math.LN2; - zoomTo(this, p, location(p), d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1); - } - return d3.rebind(zoom, event, "on"); - }; - var d3_behavior_zoomInfinity = [ 0, Infinity ], d3_behavior_zoomDelta, d3_behavior_zoomWheel; - d3.color = d3_color; - function d3_color() {} - d3_color.prototype.toString = function() { - return this.rgb() + ""; - }; - d3.hsl = d3_hsl; - function d3_hsl(h, s, l) { - return this instanceof d3_hsl ? void (this.h = +h, this.s = +s, this.l = +l) : arguments.length < 2 ? h instanceof d3_hsl ? new d3_hsl(h.h, h.s, h.l) : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl) : new d3_hsl(h, s, l); - } - var d3_hslPrototype = d3_hsl.prototype = new d3_color(); - d3_hslPrototype.brighter = function(k) { - k = Math.pow(.7, arguments.length ? k : 1); - return new d3_hsl(this.h, this.s, this.l / k); - }; - d3_hslPrototype.darker = function(k) { - k = Math.pow(.7, arguments.length ? k : 1); - return new d3_hsl(this.h, this.s, k * this.l); - }; - d3_hslPrototype.rgb = function() { - return d3_hsl_rgb(this.h, this.s, this.l); - }; - function d3_hsl_rgb(h, s, l) { - var m1, m2; - h = isNaN(h) ? 0 : (h %= 360) < 0 ? h + 360 : h; - s = isNaN(s) ? 0 : s < 0 ? 0 : s > 1 ? 1 : s; - l = l < 0 ? 0 : l > 1 ? 1 : l; - m2 = l <= .5 ? l * (1 + s) : l + s - l * s; - m1 = 2 * l - m2; - function v(h) { - if (h > 360) h -= 360; else if (h < 0) h += 360; - if (h < 60) return m1 + (m2 - m1) * h / 60; - if (h < 180) return m2; - if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60; - return m1; - } - function vv(h) { - return Math.round(v(h) * 255); - } - return new d3_rgb(vv(h + 120), vv(h), vv(h - 120)); - } - d3.hcl = d3_hcl; - function d3_hcl(h, c, l) { - return this instanceof d3_hcl ? void (this.h = +h, this.c = +c, this.l = +l) : arguments.length < 2 ? h instanceof d3_hcl ? new d3_hcl(h.h, h.c, h.l) : h instanceof d3_lab ? d3_lab_hcl(h.l, h.a, h.b) : d3_lab_hcl((h = d3_rgb_lab((h = d3.rgb(h)).r, h.g, h.b)).l, h.a, h.b) : new d3_hcl(h, c, l); - } - var d3_hclPrototype = d3_hcl.prototype = new d3_color(); - d3_hclPrototype.brighter = function(k) { - return new d3_hcl(this.h, this.c, Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1))); - }; - d3_hclPrototype.darker = function(k) { - return new d3_hcl(this.h, this.c, Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1))); - }; - d3_hclPrototype.rgb = function() { - return d3_hcl_lab(this.h, this.c, this.l).rgb(); - }; - function d3_hcl_lab(h, c, l) { - if (isNaN(h)) h = 0; - if (isNaN(c)) c = 0; - return new d3_lab(l, Math.cos(h *= d3_radians) * c, Math.sin(h) * c); - } - d3.lab = d3_lab; - function d3_lab(l, a, b) { - return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b) : arguments.length < 2 ? l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b) : l instanceof d3_hcl ? d3_hcl_lab(l.h, l.c, l.l) : d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b) : new d3_lab(l, a, b); - } - var d3_lab_K = 18; - var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883; - var d3_labPrototype = d3_lab.prototype = new d3_color(); - d3_labPrototype.brighter = function(k) { - return new d3_lab(Math.min(100, this.l + d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); - }; - d3_labPrototype.darker = function(k) { - return new d3_lab(Math.max(0, this.l - d3_lab_K * (arguments.length ? k : 1)), this.a, this.b); - }; - d3_labPrototype.rgb = function() { - return d3_lab_rgb(this.l, this.a, this.b); - }; - function d3_lab_rgb(l, a, b) { - var y = (l + 16) / 116, x = y + a / 500, z = y - b / 200; - x = d3_lab_xyz(x) * d3_lab_X; - y = d3_lab_xyz(y) * d3_lab_Y; - z = d3_lab_xyz(z) * d3_lab_Z; - return new d3_rgb(d3_xyz_rgb(3.2404542 * x - 1.5371385 * y - .4985314 * z), d3_xyz_rgb(-.969266 * x + 1.8760108 * y + .041556 * z), d3_xyz_rgb(.0556434 * x - .2040259 * y + 1.0572252 * z)); - } - function d3_lab_hcl(l, a, b) { - return l > 0 ? new d3_hcl(Math.atan2(b, a) * d3_degrees, Math.sqrt(a * a + b * b), l) : new d3_hcl(NaN, NaN, l); - } - function d3_lab_xyz(x) { - return x > .206893034 ? x * x * x : (x - 4 / 29) / 7.787037; - } - function d3_xyz_lab(x) { - return x > .008856 ? Math.pow(x, 1 / 3) : 7.787037 * x + 4 / 29; - } - function d3_xyz_rgb(r) { - return Math.round(255 * (r <= .00304 ? 12.92 * r : 1.055 * Math.pow(r, 1 / 2.4) - .055)); - } - d3.rgb = d3_rgb; - function d3_rgb(r, g, b) { - return this instanceof d3_rgb ? void (this.r = ~~r, this.g = ~~g, this.b = ~~b) : arguments.length < 2 ? r instanceof d3_rgb ? new d3_rgb(r.r, r.g, r.b) : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb) : new d3_rgb(r, g, b); - } - function d3_rgbNumber(value) { - return new d3_rgb(value >> 16, value >> 8 & 255, value & 255); - } - function d3_rgbString(value) { - return d3_rgbNumber(value) + ""; - } - var d3_rgbPrototype = d3_rgb.prototype = new d3_color(); - d3_rgbPrototype.brighter = function(k) { - k = Math.pow(.7, arguments.length ? k : 1); - var r = this.r, g = this.g, b = this.b, i = 30; - if (!r && !g && !b) return new d3_rgb(i, i, i); - if (r && r < i) r = i; - if (g && g < i) g = i; - if (b && b < i) b = i; - return new d3_rgb(Math.min(255, r / k), Math.min(255, g / k), Math.min(255, b / k)); - }; - d3_rgbPrototype.darker = function(k) { - k = Math.pow(.7, arguments.length ? k : 1); - return new d3_rgb(k * this.r, k * this.g, k * this.b); - }; - d3_rgbPrototype.hsl = function() { - return d3_rgb_hsl(this.r, this.g, this.b); - }; - d3_rgbPrototype.toString = function() { - return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b); - }; - function d3_rgb_hex(v) { - return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16); - } - function d3_rgb_parse(format, rgb, hsl) { - var r = 0, g = 0, b = 0, m1, m2, color; - m1 = /([a-z]+)\((.*)\)/.exec(format = format.toLowerCase()); - if (m1) { - m2 = m1[2].split(","); - switch (m1[1]) { - case "hsl": - { - return hsl(parseFloat(m2[0]), parseFloat(m2[1]) / 100, parseFloat(m2[2]) / 100); + continue; } - case "rgb": - { - return rgb(d3_rgb_parseNumber(m2[0]), d3_rgb_parseNumber(m2[1]), d3_rgb_parseNumber(m2[2])); + var m = Math.floor((left + right) / 2); + + x = coords[2 * m]; + y = coords[2 * m + 1]; + + if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]); + + var nextAxis = (axis + 1) % 2; + + if (axis === 0 ? minX <= x : minY <= y) { + stack.push(left); + stack.push(m - 1); + stack.push(nextAxis); } - } - } - if (color = d3_rgb_names.get(format)) { - return rgb(color.r, color.g, color.b); - } - if (format != null && format.charAt(0) === "#" && !isNaN(color = parseInt(format.slice(1), 16))) { - if (format.length === 4) { - r = (color & 3840) >> 4; - r = r >> 4 | r; - g = color & 240; - g = g >> 4 | g; - b = color & 15; - b = b << 4 | b; - } else if (format.length === 7) { - r = (color & 16711680) >> 16; - g = (color & 65280) >> 8; - b = color & 255; - } - } - return rgb(r, g, b); - } - function d3_rgb_hsl(r, g, b) { - var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h, s, l = (max + min) / 2; - if (d) { - s = l < .5 ? d / (max + min) : d / (2 - max - min); - if (r == max) h = (g - b) / d + (g < b ? 6 : 0); else if (g == max) h = (b - r) / d + 2; else h = (r - g) / d + 4; - h *= 60; - } else { - h = NaN; - s = l > 0 && l < 1 ? 0 : h; - } - return new d3_hsl(h, s, l); - } - function d3_rgb_lab(r, g, b) { - r = d3_rgb_xyz(r); - g = d3_rgb_xyz(g); - b = d3_rgb_xyz(b); - var x = d3_xyz_lab((.4124564 * r + .3575761 * g + .1804375 * b) / d3_lab_X), y = d3_xyz_lab((.2126729 * r + .7151522 * g + .072175 * b) / d3_lab_Y), z = d3_xyz_lab((.0193339 * r + .119192 * g + .9503041 * b) / d3_lab_Z); - return d3_lab(116 * y - 16, 500 * (x - y), 200 * (y - z)); - } - function d3_rgb_xyz(r) { - return (r /= 255) <= .04045 ? r / 12.92 : Math.pow((r + .055) / 1.055, 2.4); - } - function d3_rgb_parseNumber(c) { - var f = parseFloat(c); - return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f; - } - var d3_rgb_names = d3.map({ - aliceblue: 15792383, - antiquewhite: 16444375, - aqua: 65535, - aquamarine: 8388564, - azure: 15794175, - beige: 16119260, - bisque: 16770244, - black: 0, - blanchedalmond: 16772045, - blue: 255, - blueviolet: 9055202, - brown: 10824234, - burlywood: 14596231, - cadetblue: 6266528, - chartreuse: 8388352, - chocolate: 13789470, - coral: 16744272, - cornflowerblue: 6591981, - cornsilk: 16775388, - crimson: 14423100, - cyan: 65535, - darkblue: 139, - darkcyan: 35723, - darkgoldenrod: 12092939, - darkgray: 11119017, - darkgreen: 25600, - darkgrey: 11119017, - darkkhaki: 12433259, - darkmagenta: 9109643, - darkolivegreen: 5597999, - darkorange: 16747520, - darkorchid: 10040012, - darkred: 9109504, - darksalmon: 15308410, - darkseagreen: 9419919, - darkslateblue: 4734347, - darkslategray: 3100495, - darkslategrey: 3100495, - darkturquoise: 52945, - darkviolet: 9699539, - deeppink: 16716947, - deepskyblue: 49151, - dimgray: 6908265, - dimgrey: 6908265, - dodgerblue: 2003199, - firebrick: 11674146, - floralwhite: 16775920, - forestgreen: 2263842, - fuchsia: 16711935, - gainsboro: 14474460, - ghostwhite: 16316671, - gold: 16766720, - goldenrod: 14329120, - gray: 8421504, - green: 32768, - greenyellow: 11403055, - grey: 8421504, - honeydew: 15794160, - hotpink: 16738740, - indianred: 13458524, - indigo: 4915330, - ivory: 16777200, - khaki: 15787660, - lavender: 15132410, - lavenderblush: 16773365, - lawngreen: 8190976, - lemonchiffon: 16775885, - lightblue: 11393254, - lightcoral: 15761536, - lightcyan: 14745599, - lightgoldenrodyellow: 16448210, - lightgray: 13882323, - lightgreen: 9498256, - lightgrey: 13882323, - lightpink: 16758465, - lightsalmon: 16752762, - lightseagreen: 2142890, - lightskyblue: 8900346, - lightslategray: 7833753, - lightslategrey: 7833753, - lightsteelblue: 11584734, - lightyellow: 16777184, - lime: 65280, - limegreen: 3329330, - linen: 16445670, - magenta: 16711935, - maroon: 8388608, - mediumaquamarine: 6737322, - mediumblue: 205, - mediumorchid: 12211667, - mediumpurple: 9662683, - mediumseagreen: 3978097, - mediumslateblue: 8087790, - mediumspringgreen: 64154, - mediumturquoise: 4772300, - mediumvioletred: 13047173, - midnightblue: 1644912, - mintcream: 16121850, - mistyrose: 16770273, - moccasin: 16770229, - navajowhite: 16768685, - navy: 128, - oldlace: 16643558, - olive: 8421376, - olivedrab: 7048739, - orange: 16753920, - orangered: 16729344, - orchid: 14315734, - palegoldenrod: 15657130, - palegreen: 10025880, - paleturquoise: 11529966, - palevioletred: 14381203, - papayawhip: 16773077, - peachpuff: 16767673, - peru: 13468991, - pink: 16761035, - plum: 14524637, - powderblue: 11591910, - purple: 8388736, - rebeccapurple: 6697881, - red: 16711680, - rosybrown: 12357519, - royalblue: 4286945, - saddlebrown: 9127187, - salmon: 16416882, - sandybrown: 16032864, - seagreen: 3050327, - seashell: 16774638, - sienna: 10506797, - silver: 12632256, - skyblue: 8900331, - slateblue: 6970061, - slategray: 7372944, - slategrey: 7372944, - snow: 16775930, - springgreen: 65407, - steelblue: 4620980, - tan: 13808780, - teal: 32896, - thistle: 14204888, - tomato: 16737095, - turquoise: 4251856, - violet: 15631086, - wheat: 16113331, - white: 16777215, - whitesmoke: 16119285, - yellow: 16776960, - yellowgreen: 10145074 - }); - d3_rgb_names.forEach(function(key, value) { - d3_rgb_names.set(key, d3_rgbNumber(value)); - }); - function d3_functor(v) { - return typeof v === "function" ? v : function() { - return v; - }; - } - d3.functor = d3_functor; - d3.xhr = d3_xhrType(d3_identity); - function d3_xhrType(response) { - return function(url, mimeType, callback) { - if (arguments.length === 2 && typeof mimeType === "function") callback = mimeType, - mimeType = null; - return d3_xhr(url, mimeType, response, callback); - }; - } - function d3_xhr(url, mimeType, response, callback) { - var xhr = {}, dispatch = d3.dispatch("beforesend", "progress", "load", "error"), headers = {}, request = new XMLHttpRequest(), responseType = null; - if (this.XDomainRequest && !("withCredentials" in request) && /^(http(s)?:)?\/\//.test(url)) request = new XDomainRequest(); - "onload" in request ? request.onload = request.onerror = respond : request.onreadystatechange = function() { - request.readyState > 3 && respond(); - }; - function respond() { - var status = request.status, result; - if (!status && d3_xhrHasResponse(request) || status >= 200 && status < 300 || status === 304) { - try { - result = response.call(xhr, request); - } catch (e) { - dispatch.error.call(xhr, e); - return; + if (axis === 0 ? maxX >= x : maxY >= y) { + stack.push(m + 1); + stack.push(right); + stack.push(nextAxis); } - dispatch.load.call(xhr, result); - } else { - dispatch.error.call(xhr, request); - } } - request.onprogress = function(event) { - var o = d3.event; - d3.event = event; - try { - dispatch.progress.call(xhr, request); - } finally { - d3.event = o; - } - }; - xhr.header = function(name, value) { - name = (name + "").toLowerCase(); - if (arguments.length < 2) return headers[name]; - if (value == null) delete headers[name]; else headers[name] = value + ""; - return xhr; - }; - xhr.mimeType = function(value) { - if (!arguments.length) return mimeType; - mimeType = value == null ? null : value + ""; - return xhr; - }; - xhr.responseType = function(value) { - if (!arguments.length) return responseType; - responseType = value; - return xhr; - }; - xhr.response = function(value) { - response = value; - return xhr; - }; - [ "get", "post" ].forEach(function(method) { - xhr[method] = function() { - return xhr.send.apply(xhr, [ method ].concat(d3_array(arguments))); - }; - }); - xhr.send = function(method, data, callback) { - if (arguments.length === 2 && typeof data === "function") callback = data, data = null; - request.open(method, url, true); - if (mimeType != null && !("accept" in headers)) headers["accept"] = mimeType + ",*/*"; - if (request.setRequestHeader) for (var name in headers) request.setRequestHeader(name, headers[name]); - if (mimeType != null && request.overrideMimeType) request.overrideMimeType(mimeType); - if (responseType != null) request.responseType = responseType; - if (callback != null) xhr.on("error", callback).on("load", function(request) { - callback(null, request); - }); - dispatch.beforesend.call(xhr, request); - request.send(data == null ? null : data); - return xhr; - }; - xhr.abort = function() { - request.abort(); - return xhr; - }; - d3.rebind(xhr, dispatch, "on"); - return callback == null ? xhr : xhr.get(d3_xhr_fixCallback(callback)); - } - function d3_xhr_fixCallback(callback) { - return callback.length === 1 ? function(error, request) { - callback(error == null ? request : null); - } : callback; - } - function d3_xhrHasResponse(request) { - var type = request.responseType; - return type && type !== "text" ? request.response : request.responseText; - } - d3.dsv = function(delimiter, mimeType) { - var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0); - function dsv(url, row, callback) { - if (arguments.length < 3) callback = row, row = null; - var xhr = d3_xhr(url, mimeType, row == null ? response : typedResponse(row), callback); - xhr.row = function(_) { - return arguments.length ? xhr.response((row = _) == null ? response : typedResponse(_)) : row; - }; - return xhr; + + return result; +} + +},{}],276:[function(require,module,exports){ +'use strict'; + +module.exports = sortKD; + +function sortKD(ids, coords, nodeSize, left, right, depth) { + if (right - left <= nodeSize) return; + + var m = Math.floor((left + right) / 2); + + select(ids, coords, m, left, right, depth % 2); + + sortKD(ids, coords, nodeSize, left, m - 1, depth + 1); + sortKD(ids, coords, nodeSize, m + 1, right, depth + 1); +} + +function select(ids, coords, k, left, right, inc) { + + while (right > left) { + if (right - left > 600) { + var n = right - left + 1; + var m = k - left + 1; + var z = Math.log(n); + var s = 0.5 * Math.exp(2 * z / 3); + var sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1); + var newLeft = Math.max(left, Math.floor(k - m * s / n + sd)); + var newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd)); + select(ids, coords, k, newLeft, newRight, inc); + } + + var t = coords[2 * k + inc]; + var i = left; + var j = right; + + swapItem(ids, coords, left, k); + if (coords[2 * right + inc] > t) swapItem(ids, coords, left, right); + + while (i < j) { + swapItem(ids, coords, i, j); + i++; + j--; + while (coords[2 * i + inc] < t) i++; + while (coords[2 * j + inc] > t) j--; + } + + if (coords[2 * left + inc] === t) swapItem(ids, coords, left, j); + else { + j++; + swapItem(ids, coords, j, right); + } + + if (j <= k) left = j + 1; + if (k <= j) right = j - 1; } - function response(request) { - return dsv.parse(request.responseText); - } - function typedResponse(f) { - return function(request) { - return dsv.parse(request.responseText, f); - }; - } - dsv.parse = function(text, f) { - var o; - return dsv.parseRows(text, function(row, i) { - if (o) return o(row, i - 1); - var a = new Function("d", "return {" + row.map(function(name, i) { - return JSON.stringify(name) + ": d[" + i + "]"; - }).join(",") + "}"); - o = f ? function(row, i) { - return f(a(row), i); - } : a; - }); - }; - dsv.parseRows = function(text, f) { - var EOL = {}, EOF = {}, rows = [], N = text.length, I = 0, n = 0, t, eol; - function token() { - if (I >= N) return EOF; - if (eol) return eol = false, EOL; - var j = I; - if (text.charCodeAt(j) === 34) { - var i = j; - while (i++ < N) { - if (text.charCodeAt(i) === 34) { - if (text.charCodeAt(i + 1) !== 34) break; - ++i; +} + +function swapItem(ids, coords, i, j) { + swap(ids, i, j); + swap(coords, 2 * i, 2 * j); + swap(coords, 2 * i + 1, 2 * j + 1); +} + +function swap(arr, i, j) { + var tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; +} + +},{}],277:[function(require,module,exports){ +'use strict'; + +module.exports = within; + +function within(ids, coords, qx, qy, r, nodeSize) { + var stack = [0, ids.length - 1, 0]; + var result = []; + var r2 = r * r; + + while (stack.length) { + var axis = stack.pop(); + var right = stack.pop(); + var left = stack.pop(); + + if (right - left <= nodeSize) { + for (var i = left; i <= right; i++) { + if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]); } - } - I = i + 2; - var c = text.charCodeAt(i + 1); - if (c === 13) { - eol = true; - if (text.charCodeAt(i + 2) === 10) ++I; - } else if (c === 10) { - eol = true; - } - return text.slice(j + 1, i).replace(/""/g, '"'); + continue; } - while (I < N) { - var c = text.charCodeAt(I++), k = 1; - if (c === 10) eol = true; else if (c === 13) { - eol = true; - if (text.charCodeAt(I) === 10) ++I, ++k; - } else if (c !== delimiterCode) continue; - return text.slice(j, I - k); + + var m = Math.floor((left + right) / 2); + + var x = coords[2 * m]; + var y = coords[2 * m + 1]; + + if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]); + + var nextAxis = (axis + 1) % 2; + + if (axis === 0 ? qx - r <= x : qy - r <= y) { + stack.push(left); + stack.push(m - 1); + stack.push(nextAxis); } - return text.slice(j); - } - while ((t = token()) !== EOF) { - var a = []; - while (t !== EOL && t !== EOF) { - a.push(t); - t = token(); + if (axis === 0 ? qx + r >= x : qy + r >= y) { + stack.push(m + 1); + stack.push(right); + stack.push(nextAxis); } - if (f && (a = f(a, n++)) == null) continue; - rows.push(a); - } - return rows; - }; - dsv.format = function(rows) { - if (Array.isArray(rows[0])) return dsv.formatRows(rows); - var fieldSet = new d3_Set(), fields = []; - rows.forEach(function(row) { - for (var field in row) { - if (!fieldSet.has(field)) { - fields.push(fieldSet.add(field)); - } - } - }); - return [ fields.map(formatValue).join(delimiter) ].concat(rows.map(function(row) { - return fields.map(function(field) { - return formatValue(row[field]); - }).join(delimiter); - })).join("\n"); - }; - dsv.formatRows = function(rows) { - return rows.map(formatRow).join("\n"); - }; - function formatRow(row) { - return row.map(formatValue).join(delimiter); } - function formatValue(text) { - return reFormat.test(text) ? '"' + text.replace(/\"/g, '""') + '"' : text; - } - return dsv; - }; - d3.csv = d3.dsv(",", "text/csv"); - d3.tsv = d3.dsv(" ", "text/tab-separated-values"); - var d3_timer_queueHead, d3_timer_queueTail, d3_timer_interval, d3_timer_timeout, d3_timer_frame = this[d3_vendorSymbol(this, "requestAnimationFrame")] || function(callback) { - setTimeout(callback, 17); - }; - d3.timer = function() { - d3_timer.apply(this, arguments); - }; - function d3_timer(callback, delay, then) { - var n = arguments.length; - if (n < 2) delay = 0; - if (n < 3) then = Date.now(); - var time = then + delay, timer = { - c: callback, - t: time, - n: null - }; - if (d3_timer_queueTail) d3_timer_queueTail.n = timer; else d3_timer_queueHead = timer; - d3_timer_queueTail = timer; - if (!d3_timer_interval) { - d3_timer_timeout = clearTimeout(d3_timer_timeout); - d3_timer_interval = 1; - d3_timer_frame(d3_timer_step); - } - return timer; - } - function d3_timer_step() { - var now = d3_timer_mark(), delay = d3_timer_sweep() - now; - if (delay > 24) { - if (isFinite(delay)) { - clearTimeout(d3_timer_timeout); - d3_timer_timeout = setTimeout(d3_timer_step, delay); - } - d3_timer_interval = 0; + + return result; +} + +function sqDist(ax, ay, bx, by) { + var dx = ax - bx; + var dy = ay - by; + return dx * dx + dy * dy; +} + +},{}],278:[function(require,module,exports){ +'use strict'; + +function createFunction(parameters, defaultType) { + var fun; + + if (!isFunctionDefinition(parameters)) { + fun = function() { return parameters; }; + fun.isFeatureConstant = true; + fun.isZoomConstant = true; + } else { - d3_timer_interval = 1; - d3_timer_frame(d3_timer_step); + var zoomAndFeatureDependent = parameters.stops && typeof parameters.stops[0][0] === 'object'; + var featureDependent = zoomAndFeatureDependent || parameters.property !== undefined; + var zoomDependent = zoomAndFeatureDependent || !featureDependent; + var type = parameters.type || defaultType || 'exponential'; + + var innerFun; + if (type === 'exponential') { + innerFun = evaluateExponentialFunction; + } else if (type === 'interval') { + innerFun = evaluateIntervalFunction; + } else if (type === 'categorical') { + innerFun = evaluateCategoricalFunction; + } else if (type === 'identity') { + innerFun = evaluateIdentityFunction; + } else { + throw new Error('Unknown function type "' + type + '"'); + } + + if (zoomAndFeatureDependent) { + var featureFunctions = {}; + var featureFunctionStops = []; + for (var s = 0; s < parameters.stops.length; s++) { + var stop = parameters.stops[s]; + if (featureFunctions[stop[0].zoom] === undefined) { + featureFunctions[stop[0].zoom] = { + zoom: stop[0].zoom, + type: parameters.type, + property: parameters.property, + stops: [] + }; + } + featureFunctions[stop[0].zoom].stops.push([stop[0].value, stop[1]]); + } + + for (var z in featureFunctions) { + featureFunctionStops.push([featureFunctions[z].zoom, createFunction(featureFunctions[z])]); + } + fun = function(zoom, feature) { + return evaluateExponentialFunction({ stops: featureFunctionStops, base: parameters.base }, zoom)(zoom, feature); + }; + fun.isFeatureConstant = false; + fun.isZoomConstant = false; + + } else if (zoomDependent) { + fun = function(zoom) { + return innerFun(parameters, zoom); + }; + fun.isFeatureConstant = true; + fun.isZoomConstant = false; + } else { + fun = function(zoom, feature) { + return innerFun(parameters, feature[parameters.property]); + }; + fun.isFeatureConstant = false; + fun.isZoomConstant = true; + } } - } - d3.timer.flush = function() { - d3_timer_mark(); - d3_timer_sweep(); - }; - function d3_timer_mark() { - var now = Date.now(), timer = d3_timer_queueHead; - while (timer) { - if (now >= timer.t && timer.c(now - timer.t)) timer.c = null; - timer = timer.n; + + return fun; +} + +function evaluateCategoricalFunction(parameters, input) { + for (var i = 0; i < parameters.stops.length; i++) { + if (input === parameters.stops[i][0]) { + return parameters.stops[i][1]; + } } - return now; - } - function d3_timer_sweep() { - var t0, t1 = d3_timer_queueHead, time = Infinity; - while (t1) { - if (t1.c) { - if (t1.t < time) time = t1.t; - t1 = (t0 = t1).n; - } else { - t1 = t0 ? t0.n = t1.n : d3_timer_queueHead = t1.n; - } + return parameters.stops[0][1]; +} + +function evaluateIntervalFunction(parameters, input) { + for (var i = 0; i < parameters.stops.length; i++) { + if (input < parameters.stops[i][0]) break; } - d3_timer_queueTail = t0; - return time; - } - function d3_format_precision(x, p) { - return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1); - } - d3.round = function(x, n) { - return n ? Math.round(x * (n = Math.pow(10, n))) / n : Math.round(x); - }; - var d3_formatPrefixes = [ "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" ].map(d3_formatPrefix); - d3.formatPrefix = function(value, precision) { + return parameters.stops[Math.max(i - 1, 0)][1]; +} + +function evaluateExponentialFunction(parameters, input) { + var base = parameters.base !== undefined ? parameters.base : 1; + var i = 0; - if (value = +value) { - if (value < 0) value *= -1; - if (precision) value = d3.round(value, d3_format_precision(value, precision)); - i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10); - i = Math.max(-24, Math.min(24, Math.floor((i - 1) / 3) * 3)); - } - return d3_formatPrefixes[8 + i / 3]; - }; - function d3_formatPrefix(d, i) { - var k = Math.pow(10, abs(8 - i) * 3); - return { - scale: i > 8 ? function(d) { - return d / k; - } : function(d) { - return d * k; - }, - symbol: d - }; - } - function d3_locale_numberFormat(locale) { - var locale_decimal = locale.decimal, locale_thousands = locale.thousands, locale_grouping = locale.grouping, locale_currency = locale.currency, formatGroup = locale_grouping && locale_thousands ? function(value, width) { - var i = value.length, t = [], j = 0, g = locale_grouping[0], length = 0; - while (i > 0 && g > 0) { - if (length + g + 1 > width) g = Math.max(1, width - length); - t.push(value.substring(i -= g, i + g)); - if ((length += g + 1) > width) break; - g = locale_grouping[j = (j + 1) % locale_grouping.length]; - } - return t.reverse().join(locale_thousands); - } : d3_identity; - return function(specifier) { - var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "-", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false, exponent = true; - if (precision) precision = +precision.substring(1); - if (zfill || fill === "0" && align === "=") { - zfill = fill = "0"; - align = "="; - } - switch (type) { - case "n": - comma = true; - type = "g"; - break; - - case "%": - scale = 100; - suffix = "%"; - type = "f"; - break; - - case "p": - scale = 100; - suffix = "%"; - type = "r"; - break; - - case "b": - case "o": - case "x": - case "X": - if (symbol === "#") prefix = "0" + type.toLowerCase(); - - case "c": - exponent = false; - - case "d": - integer = true; - precision = 0; - break; - - case "s": - scale = -1; - type = "r"; - break; - } - if (symbol === "$") prefix = locale_currency[0], suffix = locale_currency[1]; - if (type == "r" && !precision) type = "g"; - if (precision != null) { - if (type == "g") precision = Math.max(1, Math.min(21, precision)); else if (type == "e" || type == "f") precision = Math.max(0, Math.min(20, precision)); - } - type = d3_format_types.get(type) || d3_format_typeDefault; - var zcomma = zfill && comma; - return function(value) { - var fullSuffix = suffix; - if (integer && value % 1) return ""; - var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign === "-" ? "" : sign; - if (scale < 0) { - var unit = d3.formatPrefix(value, precision); - value = unit.scale(value); - fullSuffix = unit.symbol + suffix; - } else { - value *= scale; - } - value = type(value, precision); - var i = value.lastIndexOf("."), before, after; - if (i < 0) { - var j = exponent ? value.lastIndexOf("e") : -1; - if (j < 0) before = value, after = ""; else before = value.substring(0, j), after = value.substring(j); - } else { - before = value.substring(0, i); - after = locale_decimal + value.substring(i + 1); - } - if (!zfill && comma) before = formatGroup(before, Infinity); - var length = prefix.length + before.length + after.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : ""; - if (zcomma) before = formatGroup(padding + before, padding.length ? width - after.length : Infinity); - negative += prefix; - value = before + after; - return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + fullSuffix; - }; - }; - } - var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i; - var d3_format_types = d3.map({ - b: function(x) { - return x.toString(2); - }, - c: function(x) { - return String.fromCharCode(x); - }, - o: function(x) { - return x.toString(8); - }, - x: function(x) { - return x.toString(16); - }, - X: function(x) { - return x.toString(16).toUpperCase(); - }, - g: function(x, p) { - return x.toPrecision(p); - }, - e: function(x, p) { - return x.toExponential(p); - }, - f: function(x, p) { - return x.toFixed(p); - }, - r: function(x, p) { - return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + 1e-15), p)))); - } - }); - function d3_format_typeDefault(x) { - return x + ""; - } - var d3_time = d3.time = {}, d3_date = Date; - function d3_date_utc() { - this._ = new Date(arguments.length > 1 ? Date.UTC.apply(this, arguments) : arguments[0]); - } - d3_date_utc.prototype = { - getDate: function() { - return this._.getUTCDate(); - }, - getDay: function() { - return this._.getUTCDay(); - }, - getFullYear: function() { - return this._.getUTCFullYear(); - }, - getHours: function() { - return this._.getUTCHours(); - }, - getMilliseconds: function() { - return this._.getUTCMilliseconds(); - }, - getMinutes: function() { - return this._.getUTCMinutes(); - }, - getMonth: function() { - return this._.getUTCMonth(); - }, - getSeconds: function() { - return this._.getUTCSeconds(); - }, - getTime: function() { - return this._.getTime(); - }, - getTimezoneOffset: function() { - return 0; - }, - valueOf: function() { - return this._.valueOf(); - }, - setDate: function() { - d3_time_prototype.setUTCDate.apply(this._, arguments); - }, - setDay: function() { - d3_time_prototype.setUTCDay.apply(this._, arguments); - }, - setFullYear: function() { - d3_time_prototype.setUTCFullYear.apply(this._, arguments); - }, - setHours: function() { - d3_time_prototype.setUTCHours.apply(this._, arguments); - }, - setMilliseconds: function() { - d3_time_prototype.setUTCMilliseconds.apply(this._, arguments); - }, - setMinutes: function() { - d3_time_prototype.setUTCMinutes.apply(this._, arguments); - }, - setMonth: function() { - d3_time_prototype.setUTCMonth.apply(this._, arguments); - }, - setSeconds: function() { - d3_time_prototype.setUTCSeconds.apply(this._, arguments); - }, - setTime: function() { - d3_time_prototype.setTime.apply(this._, arguments); - } - }; - var d3_time_prototype = Date.prototype; - function d3_time_interval(local, step, number) { - function round(date) { - var d0 = local(date), d1 = offset(d0, 1); - return date - d0 < d1 - date ? d0 : d1; - } - function ceil(date) { - step(date = local(new d3_date(date - 1)), 1); - return date; - } - function offset(date, k) { - step(date = new d3_date(+date), k); - return date; - } - function range(t0, t1, dt) { - var time = ceil(t0), times = []; - if (dt > 1) { - while (time < t1) { - if (!(number(time) % dt)) times.push(new Date(+time)); - step(time, 1); - } - } else { - while (time < t1) times.push(new Date(+time)), step(time, 1); - } - return times; - } - function range_utc(t0, t1, dt) { - try { - d3_date = d3_date_utc; - var utc = new d3_date_utc(); - utc._ = t0; - return range(utc, t1, dt); - } finally { - d3_date = Date; - } - } - local.floor = local; - local.round = round; - local.ceil = ceil; - local.offset = offset; - local.range = range; - var utc = local.utc = d3_time_interval_utc(local); - utc.floor = utc; - utc.round = d3_time_interval_utc(round); - utc.ceil = d3_time_interval_utc(ceil); - utc.offset = d3_time_interval_utc(offset); - utc.range = range_utc; - return local; - } - function d3_time_interval_utc(method) { - return function(date, k) { - try { - d3_date = d3_date_utc; - var utc = new d3_date_utc(); - utc._ = date; - return method(utc, k)._; - } finally { - d3_date = Date; - } - }; - } - d3_time.year = d3_time_interval(function(date) { - date = d3_time.day(date); - date.setMonth(0, 1); - return date; - }, function(date, offset) { - date.setFullYear(date.getFullYear() + offset); - }, function(date) { - return date.getFullYear(); - }); - d3_time.years = d3_time.year.range; - d3_time.years.utc = d3_time.year.utc.range; - d3_time.day = d3_time_interval(function(date) { - var day = new d3_date(2e3, 0); - day.setFullYear(date.getFullYear(), date.getMonth(), date.getDate()); - return day; - }, function(date, offset) { - date.setDate(date.getDate() + offset); - }, function(date) { - return date.getDate() - 1; - }); - d3_time.days = d3_time.day.range; - d3_time.days.utc = d3_time.day.utc.range; - d3_time.dayOfYear = function(date) { - var year = d3_time.year(date); - return Math.floor((date - year - (date.getTimezoneOffset() - year.getTimezoneOffset()) * 6e4) / 864e5); - }; - [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ].forEach(function(day, i) { - i = 7 - i; - var interval = d3_time[day] = d3_time_interval(function(date) { - (date = d3_time.day(date)).setDate(date.getDate() - (date.getDay() + i) % 7); - return date; - }, function(date, offset) { - date.setDate(date.getDate() + Math.floor(offset) * 7); - }, function(date) { - var day = d3_time.year(date).getDay(); - return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7) - (day !== i); - }); - d3_time[day + "s"] = interval.range; - d3_time[day + "s"].utc = interval.utc.range; - d3_time[day + "OfYear"] = function(date) { - var day = d3_time.year(date).getDay(); - return Math.floor((d3_time.dayOfYear(date) + (day + i) % 7) / 7); - }; - }); - d3_time.week = d3_time.sunday; - d3_time.weeks = d3_time.sunday.range; - d3_time.weeks.utc = d3_time.sunday.utc.range; - d3_time.weekOfYear = d3_time.sundayOfYear; - function d3_locale_timeFormat(locale) { - var locale_dateTime = locale.dateTime, locale_date = locale.date, locale_time = locale.time, locale_periods = locale.periods, locale_days = locale.days, locale_shortDays = locale.shortDays, locale_months = locale.months, locale_shortMonths = locale.shortMonths; - function d3_time_format(template) { - var n = template.length; - function format(date) { - var string = [], i = -1, j = 0, c, p, f; - while (++i < n) { - if (template.charCodeAt(i) === 37) { - string.push(template.slice(j, i)); - if ((p = d3_time_formatPads[c = template.charAt(++i)]) != null) c = template.charAt(++i); - if (f = d3_time_formats[c]) c = f(date, p == null ? c === "e" ? " " : "0" : p); - string.push(c); - j = i + 1; - } - } - string.push(template.slice(j, i)); - return string.join(""); - } - format.parse = function(string) { - var d = { - y: 1900, - m: 0, - d: 1, - H: 0, - M: 0, - S: 0, - L: 0, - Z: null - }, i = d3_time_parse(d, template, string, 0); - if (i != string.length) return null; - if ("p" in d) d.H = d.H % 12 + d.p * 12; - var localZ = d.Z != null && d3_date !== d3_date_utc, date = new (localZ ? d3_date_utc : d3_date)(); - if ("j" in d) date.setFullYear(d.y, 0, d.j); else if ("W" in d || "U" in d) { - if (!("w" in d)) d.w = "W" in d ? 1 : 0; - date.setFullYear(d.y, 0, 1); - date.setFullYear(d.y, 0, "W" in d ? (d.w + 6) % 7 + d.W * 7 - (date.getDay() + 5) % 7 : d.w + d.U * 7 - (date.getDay() + 6) % 7); - } else date.setFullYear(d.y, d.m, d.d); - date.setHours(d.H + (d.Z / 100 | 0), d.M + d.Z % 100, d.S, d.L); - return localZ ? date._ : date; - }; - format.toString = function() { - return template; - }; - return format; - } - function d3_time_parse(date, template, string, j) { - var c, p, t, i = 0, n = template.length, m = string.length; - while (i < n) { - if (j >= m) return -1; - c = template.charCodeAt(i++); - if (c === 37) { - t = template.charAt(i++); - p = d3_time_parsers[t in d3_time_formatPads ? template.charAt(i++) : t]; - if (!p || (j = p(date, string, j)) < 0) return -1; - } else if (c != string.charCodeAt(j++)) { - return -1; - } - } - return j; - } - d3_time_format.utc = function(template) { - var local = d3_time_format(template); - function format(date) { - try { - d3_date = d3_date_utc; - var utc = new d3_date(); - utc._ = date; - return local(utc); - } finally { - d3_date = Date; - } - } - format.parse = function(string) { - try { - d3_date = d3_date_utc; - var date = local.parse(string); - return date && date._; - } finally { - d3_date = Date; - } - }; - format.toString = local.toString; - return format; - }; - d3_time_format.multi = d3_time_format.utc.multi = d3_time_formatMulti; - var d3_time_periodLookup = d3.map(), d3_time_dayRe = d3_time_formatRe(locale_days), d3_time_dayLookup = d3_time_formatLookup(locale_days), d3_time_dayAbbrevRe = d3_time_formatRe(locale_shortDays), d3_time_dayAbbrevLookup = d3_time_formatLookup(locale_shortDays), d3_time_monthRe = d3_time_formatRe(locale_months), d3_time_monthLookup = d3_time_formatLookup(locale_months), d3_time_monthAbbrevRe = d3_time_formatRe(locale_shortMonths), d3_time_monthAbbrevLookup = d3_time_formatLookup(locale_shortMonths); - locale_periods.forEach(function(p, i) { - d3_time_periodLookup.set(p.toLowerCase(), i); - }); - var d3_time_formats = { - a: function(d) { - return locale_shortDays[d.getDay()]; - }, - A: function(d) { - return locale_days[d.getDay()]; - }, - b: function(d) { - return locale_shortMonths[d.getMonth()]; - }, - B: function(d) { - return locale_months[d.getMonth()]; - }, - c: d3_time_format(locale_dateTime), - d: function(d, p) { - return d3_time_formatPad(d.getDate(), p, 2); - }, - e: function(d, p) { - return d3_time_formatPad(d.getDate(), p, 2); - }, - H: function(d, p) { - return d3_time_formatPad(d.getHours(), p, 2); - }, - I: function(d, p) { - return d3_time_formatPad(d.getHours() % 12 || 12, p, 2); - }, - j: function(d, p) { - return d3_time_formatPad(1 + d3_time.dayOfYear(d), p, 3); - }, - L: function(d, p) { - return d3_time_formatPad(d.getMilliseconds(), p, 3); - }, - m: function(d, p) { - return d3_time_formatPad(d.getMonth() + 1, p, 2); - }, - M: function(d, p) { - return d3_time_formatPad(d.getMinutes(), p, 2); - }, - p: function(d) { - return locale_periods[+(d.getHours() >= 12)]; - }, - S: function(d, p) { - return d3_time_formatPad(d.getSeconds(), p, 2); - }, - U: function(d, p) { - return d3_time_formatPad(d3_time.sundayOfYear(d), p, 2); - }, - w: function(d) { - return d.getDay(); - }, - W: function(d, p) { - return d3_time_formatPad(d3_time.mondayOfYear(d), p, 2); - }, - x: d3_time_format(locale_date), - X: d3_time_format(locale_time), - y: function(d, p) { - return d3_time_formatPad(d.getFullYear() % 100, p, 2); - }, - Y: function(d, p) { - return d3_time_formatPad(d.getFullYear() % 1e4, p, 4); - }, - Z: d3_time_zone, - "%": function() { - return "%"; - } - }; - var d3_time_parsers = { - a: d3_time_parseWeekdayAbbrev, - A: d3_time_parseWeekday, - b: d3_time_parseMonthAbbrev, - B: d3_time_parseMonth, - c: d3_time_parseLocaleFull, - d: d3_time_parseDay, - e: d3_time_parseDay, - H: d3_time_parseHour24, - I: d3_time_parseHour24, - j: d3_time_parseDayOfYear, - L: d3_time_parseMilliseconds, - m: d3_time_parseMonthNumber, - M: d3_time_parseMinutes, - p: d3_time_parseAmPm, - S: d3_time_parseSeconds, - U: d3_time_parseWeekNumberSunday, - w: d3_time_parseWeekdayNumber, - W: d3_time_parseWeekNumberMonday, - x: d3_time_parseLocaleDate, - X: d3_time_parseLocaleTime, - y: d3_time_parseYear, - Y: d3_time_parseFullYear, - Z: d3_time_parseZone, - "%": d3_time_parseLiteralPercent - }; - function d3_time_parseWeekdayAbbrev(date, string, i) { - d3_time_dayAbbrevRe.lastIndex = 0; - var n = d3_time_dayAbbrevRe.exec(string.slice(i)); - return n ? (date.w = d3_time_dayAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; - } - function d3_time_parseWeekday(date, string, i) { - d3_time_dayRe.lastIndex = 0; - var n = d3_time_dayRe.exec(string.slice(i)); - return n ? (date.w = d3_time_dayLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; - } - function d3_time_parseMonthAbbrev(date, string, i) { - d3_time_monthAbbrevRe.lastIndex = 0; - var n = d3_time_monthAbbrevRe.exec(string.slice(i)); - return n ? (date.m = d3_time_monthAbbrevLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; - } - function d3_time_parseMonth(date, string, i) { - d3_time_monthRe.lastIndex = 0; - var n = d3_time_monthRe.exec(string.slice(i)); - return n ? (date.m = d3_time_monthLookup.get(n[0].toLowerCase()), i + n[0].length) : -1; - } - function d3_time_parseLocaleFull(date, string, i) { - return d3_time_parse(date, d3_time_formats.c.toString(), string, i); - } - function d3_time_parseLocaleDate(date, string, i) { - return d3_time_parse(date, d3_time_formats.x.toString(), string, i); - } - function d3_time_parseLocaleTime(date, string, i) { - return d3_time_parse(date, d3_time_formats.X.toString(), string, i); - } - function d3_time_parseAmPm(date, string, i) { - var n = d3_time_periodLookup.get(string.slice(i, i += 2).toLowerCase()); - return n == null ? -1 : (date.p = n, i); - } - return d3_time_format; - } - var d3_time_formatPads = { - "-": "", - _: " ", - "0": "0" - }, d3_time_numberRe = /^\s*\d+/, d3_time_percentRe = /^%/; - function d3_time_formatPad(value, fill, width) { - var sign = value < 0 ? "-" : "", string = (sign ? -value : value) + "", length = string.length; - return sign + (length < width ? new Array(width - length + 1).join(fill) + string : string); - } - function d3_time_formatRe(names) { - return new RegExp("^(?:" + names.map(d3.requote).join("|") + ")", "i"); - } - function d3_time_formatLookup(names) { - var map = new d3_Map(), i = -1, n = names.length; - while (++i < n) map.set(names[i].toLowerCase(), i); - return map; - } - function d3_time_parseWeekdayNumber(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 1)); - return n ? (date.w = +n[0], i + n[0].length) : -1; - } - function d3_time_parseWeekNumberSunday(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i)); - return n ? (date.U = +n[0], i + n[0].length) : -1; - } - function d3_time_parseWeekNumberMonday(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i)); - return n ? (date.W = +n[0], i + n[0].length) : -1; - } - function d3_time_parseFullYear(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 4)); - return n ? (date.y = +n[0], i + n[0].length) : -1; - } - function d3_time_parseYear(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 2)); - return n ? (date.y = d3_time_expandYear(+n[0]), i + n[0].length) : -1; - } - function d3_time_parseZone(date, string, i) { - return /^[+-]\d{4}$/.test(string = string.slice(i, i + 5)) ? (date.Z = -string, - i + 5) : -1; - } - function d3_time_expandYear(d) { - return d + (d > 68 ? 1900 : 2e3); - } - function d3_time_parseMonthNumber(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 2)); - return n ? (date.m = n[0] - 1, i + n[0].length) : -1; - } - function d3_time_parseDay(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 2)); - return n ? (date.d = +n[0], i + n[0].length) : -1; - } - function d3_time_parseDayOfYear(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 3)); - return n ? (date.j = +n[0], i + n[0].length) : -1; - } - function d3_time_parseHour24(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 2)); - return n ? (date.H = +n[0], i + n[0].length) : -1; - } - function d3_time_parseMinutes(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 2)); - return n ? (date.M = +n[0], i + n[0].length) : -1; - } - function d3_time_parseSeconds(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 2)); - return n ? (date.S = +n[0], i + n[0].length) : -1; - } - function d3_time_parseMilliseconds(date, string, i) { - d3_time_numberRe.lastIndex = 0; - var n = d3_time_numberRe.exec(string.slice(i, i + 3)); - return n ? (date.L = +n[0], i + n[0].length) : -1; - } - function d3_time_zone(d) { - var z = d.getTimezoneOffset(), zs = z > 0 ? "-" : "+", zh = abs(z) / 60 | 0, zm = abs(z) % 60; - return zs + d3_time_formatPad(zh, "0", 2) + d3_time_formatPad(zm, "0", 2); - } - function d3_time_parseLiteralPercent(date, string, i) { - d3_time_percentRe.lastIndex = 0; - var n = d3_time_percentRe.exec(string.slice(i, i + 1)); - return n ? i + n[0].length : -1; - } - function d3_time_formatMulti(formats) { - var n = formats.length, i = -1; - while (++i < n) formats[i][0] = this(formats[i][0]); - return function(date) { - var i = 0, f = formats[i]; - while (!f[1](date)) f = formats[++i]; - return f[0](date); - }; - } - d3.locale = function(locale) { - return { - numberFormat: d3_locale_numberFormat(locale), - timeFormat: d3_locale_timeFormat(locale) - }; - }; - var d3_locale_enUS = d3.locale({ - decimal: ".", - thousands: ",", - grouping: [ 3 ], - currency: [ "$", "" ], - dateTime: "%a %b %e %X %Y", - date: "%m/%d/%Y", - time: "%H:%M:%S", - periods: [ "AM", "PM" ], - days: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], - shortDays: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], - months: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], - shortMonths: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] - }); - d3.format = d3_locale_enUS.numberFormat; - d3.geo = {}; - function d3_adder() {} - d3_adder.prototype = { - s: 0, - t: 0, - add: function(y) { - d3_adderSum(y, this.t, d3_adderTemp); - d3_adderSum(d3_adderTemp.s, this.s, this); - if (this.s) this.t += d3_adderTemp.t; else this.s = d3_adderTemp.t; - }, - reset: function() { - this.s = this.t = 0; - }, - valueOf: function() { - return this.s; - } - }; - var d3_adderTemp = new d3_adder(); - function d3_adderSum(a, b, o) { - var x = o.s = a + b, bv = x - a, av = x - bv; - o.t = a - av + (b - bv); - } - d3.geo.stream = function(object, listener) { - if (object && d3_geo_streamObjectType.hasOwnProperty(object.type)) { - d3_geo_streamObjectType[object.type](object, listener); - } else { - d3_geo_streamGeometry(object, listener); - } - }; - function d3_geo_streamGeometry(geometry, listener) { - if (geometry && d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) { - d3_geo_streamGeometryType[geometry.type](geometry, listener); - } - } - var d3_geo_streamObjectType = { - Feature: function(feature, listener) { - d3_geo_streamGeometry(feature.geometry, listener); - }, - FeatureCollection: function(object, listener) { - var features = object.features, i = -1, n = features.length; - while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener); - } - }; - var d3_geo_streamGeometryType = { - Sphere: function(object, listener) { - listener.sphere(); - }, - Point: function(object, listener) { - object = object.coordinates; - listener.point(object[0], object[1], object[2]); - }, - MultiPoint: function(object, listener) { - var coordinates = object.coordinates, i = -1, n = coordinates.length; - while (++i < n) object = coordinates[i], listener.point(object[0], object[1], object[2]); - }, - LineString: function(object, listener) { - d3_geo_streamLine(object.coordinates, listener, 0); - }, - MultiLineString: function(object, listener) { - var coordinates = object.coordinates, i = -1, n = coordinates.length; - while (++i < n) d3_geo_streamLine(coordinates[i], listener, 0); - }, - Polygon: function(object, listener) { - d3_geo_streamPolygon(object.coordinates, listener); - }, - MultiPolygon: function(object, listener) { - var coordinates = object.coordinates, i = -1, n = coordinates.length; - while (++i < n) d3_geo_streamPolygon(coordinates[i], listener); - }, - GeometryCollection: function(object, listener) { - var geometries = object.geometries, i = -1, n = geometries.length; - while (++i < n) d3_geo_streamGeometry(geometries[i], listener); - } - }; - function d3_geo_streamLine(coordinates, listener, closed) { - var i = -1, n = coordinates.length - closed, coordinate; - listener.lineStart(); - while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1], coordinate[2]); - listener.lineEnd(); - } - function d3_geo_streamPolygon(coordinates, listener) { - var i = -1, n = coordinates.length; - listener.polygonStart(); - while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1); - listener.polygonEnd(); - } - d3.geo.area = function(object) { - d3_geo_areaSum = 0; - d3.geo.stream(object, d3_geo_area); - return d3_geo_areaSum; - }; - var d3_geo_areaSum, d3_geo_areaRingSum = new d3_adder(); - var d3_geo_area = { - sphere: function() { - d3_geo_areaSum += 4 * π; - }, - point: d3_noop, - lineStart: d3_noop, - lineEnd: d3_noop, - polygonStart: function() { - d3_geo_areaRingSum.reset(); - d3_geo_area.lineStart = d3_geo_areaRingStart; - }, - polygonEnd: function() { - var area = 2 * d3_geo_areaRingSum; - d3_geo_areaSum += area < 0 ? 4 * π + area : area; - d3_geo_area.lineStart = d3_geo_area.lineEnd = d3_geo_area.point = d3_noop; - } - }; - function d3_geo_areaRingStart() { - var λ00, φ00, λ0, cosφ0, sinφ0; - d3_geo_area.point = function(λ, φ) { - d3_geo_area.point = nextPoint; - λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4), - sinφ0 = Math.sin(φ); - }; - function nextPoint(λ, φ) { - λ *= d3_radians; - φ = φ * d3_radians / 2 + π / 4; - var dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, cosφ = Math.cos(φ), sinφ = Math.sin(φ), k = sinφ0 * sinφ, u = cosφ0 * cosφ + k * Math.cos(adλ), v = k * sdλ * Math.sin(adλ); - d3_geo_areaRingSum.add(Math.atan2(v, u)); - λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ; - } - d3_geo_area.lineEnd = function() { - nextPoint(λ00, φ00); - }; - } - function d3_geo_cartesian(spherical) { - var λ = spherical[0], φ = spherical[1], cosφ = Math.cos(φ); - return [ cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ) ]; - } - function d3_geo_cartesianDot(a, b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; - } - function d3_geo_cartesianCross(a, b) { - return [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ]; - } - function d3_geo_cartesianAdd(a, b) { - a[0] += b[0]; - a[1] += b[1]; - a[2] += b[2]; - } - function d3_geo_cartesianScale(vector, k) { - return [ vector[0] * k, vector[1] * k, vector[2] * k ]; - } - function d3_geo_cartesianNormalize(d) { - var l = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); - d[0] /= l; - d[1] /= l; - d[2] /= l; - } - function d3_geo_spherical(cartesian) { - return [ Math.atan2(cartesian[1], cartesian[0]), d3_asin(cartesian[2]) ]; - } - function d3_geo_sphericalEqual(a, b) { - return abs(a[0] - b[0]) < ε && abs(a[1] - b[1]) < ε; - } - d3.geo.bounds = function() { - var λ0, φ0, λ1, φ1, λ_, λ__, φ__, p0, dλSum, ranges, range; - var bound = { - point: point, - lineStart: lineStart, - lineEnd: lineEnd, - polygonStart: function() { - bound.point = ringPoint; - bound.lineStart = ringStart; - bound.lineEnd = ringEnd; - dλSum = 0; - d3_geo_area.polygonStart(); - }, - polygonEnd: function() { - d3_geo_area.polygonEnd(); - bound.point = point; - bound.lineStart = lineStart; - bound.lineEnd = lineEnd; - if (d3_geo_areaRingSum < 0) λ0 = -(λ1 = 180), φ0 = -(φ1 = 90); else if (dλSum > ε) φ1 = 90; else if (dλSum < -ε) φ0 = -90; - range[0] = λ0, range[1] = λ1; - } - }; - function point(λ, φ) { - ranges.push(range = [ λ0 = λ, λ1 = λ ]); - if (φ < φ0) φ0 = φ; - if (φ > φ1) φ1 = φ; - } - function linePoint(λ, φ) { - var p = d3_geo_cartesian([ λ * d3_radians, φ * d3_radians ]); - if (p0) { - var normal = d3_geo_cartesianCross(p0, p), equatorial = [ normal[1], -normal[0], 0 ], inflection = d3_geo_cartesianCross(equatorial, normal); - d3_geo_cartesianNormalize(inflection); - inflection = d3_geo_spherical(inflection); - var dλ = λ - λ_, s = dλ > 0 ? 1 : -1, λi = inflection[0] * d3_degrees * s, antimeridian = abs(dλ) > 180; - if (antimeridian ^ (s * λ_ < λi && λi < s * λ)) { - var φi = inflection[1] * d3_degrees; - if (φi > φ1) φ1 = φi; - } else if (λi = (λi + 360) % 360 - 180, antimeridian ^ (s * λ_ < λi && λi < s * λ)) { - var φi = -inflection[1] * d3_degrees; - if (φi < φ0) φ0 = φi; - } else { - if (φ < φ0) φ0 = φ; - if (φ > φ1) φ1 = φ; - } - if (antimeridian) { - if (λ < λ_) { - if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; - } else { - if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; - } - } else { - if (λ1 >= λ0) { - if (λ < λ0) λ0 = λ; - if (λ > λ1) λ1 = λ; - } else { - if (λ > λ_) { - if (angle(λ0, λ) > angle(λ0, λ1)) λ1 = λ; - } else { - if (angle(λ, λ1) > angle(λ0, λ1)) λ0 = λ; - } - } - } - } else { - point(λ, φ); - } - p0 = p, λ_ = λ; - } - function lineStart() { - bound.point = linePoint; - } - function lineEnd() { - range[0] = λ0, range[1] = λ1; - bound.point = point; - p0 = null; - } - function ringPoint(λ, φ) { - if (p0) { - var dλ = λ - λ_; - dλSum += abs(dλ) > 180 ? dλ + (dλ > 0 ? 360 : -360) : dλ; - } else λ__ = λ, φ__ = φ; - d3_geo_area.point(λ, φ); - linePoint(λ, φ); - } - function ringStart() { - d3_geo_area.lineStart(); - } - function ringEnd() { - ringPoint(λ__, φ__); - d3_geo_area.lineEnd(); - if (abs(dλSum) > ε) λ0 = -(λ1 = 180); - range[0] = λ0, range[1] = λ1; - p0 = null; - } - function angle(λ0, λ1) { - return (λ1 -= λ0) < 0 ? λ1 + 360 : λ1; - } - function compareRanges(a, b) { - return a[0] - b[0]; - } - function withinRange(x, range) { - return range[0] <= range[1] ? range[0] <= x && x <= range[1] : x < range[0] || range[1] < x; - } - return function(feature) { - φ1 = λ1 = -(λ0 = φ0 = Infinity); - ranges = []; - d3.geo.stream(feature, bound); - var n = ranges.length; - if (n) { - ranges.sort(compareRanges); - for (var i = 1, a = ranges[0], b, merged = [ a ]; i < n; ++i) { - b = ranges[i]; - if (withinRange(b[0], a) || withinRange(b[1], a)) { - if (angle(a[0], b[1]) > angle(a[0], a[1])) a[1] = b[1]; - if (angle(b[0], a[1]) > angle(a[0], a[1])) a[0] = b[0]; - } else { - merged.push(a = b); - } - } - var best = -Infinity, dλ; - for (var n = merged.length - 1, i = 0, a = merged[n], b; i <= n; a = b, ++i) { - b = merged[i]; - if ((dλ = angle(a[1], b[0])) > best) best = dλ, λ0 = b[0], λ1 = a[1]; - } - } - ranges = range = null; - return λ0 === Infinity || φ0 === Infinity ? [ [ NaN, NaN ], [ NaN, NaN ] ] : [ [ λ0, φ0 ], [ λ1, φ1 ] ]; - }; - }(); - d3.geo.centroid = function(object) { - d3_geo_centroidW0 = d3_geo_centroidW1 = d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; - d3.geo.stream(object, d3_geo_centroid); - var x = d3_geo_centroidX2, y = d3_geo_centroidY2, z = d3_geo_centroidZ2, m = x * x + y * y + z * z; - if (m < ε2) { - x = d3_geo_centroidX1, y = d3_geo_centroidY1, z = d3_geo_centroidZ1; - if (d3_geo_centroidW1 < ε) x = d3_geo_centroidX0, y = d3_geo_centroidY0, z = d3_geo_centroidZ0; - m = x * x + y * y + z * z; - if (m < ε2) return [ NaN, NaN ]; - } - return [ Math.atan2(y, x) * d3_degrees, d3_asin(z / Math.sqrt(m)) * d3_degrees ]; - }; - var d3_geo_centroidW0, d3_geo_centroidW1, d3_geo_centroidX0, d3_geo_centroidY0, d3_geo_centroidZ0, d3_geo_centroidX1, d3_geo_centroidY1, d3_geo_centroidZ1, d3_geo_centroidX2, d3_geo_centroidY2, d3_geo_centroidZ2; - var d3_geo_centroid = { - sphere: d3_noop, - point: d3_geo_centroidPoint, - lineStart: d3_geo_centroidLineStart, - lineEnd: d3_geo_centroidLineEnd, - polygonStart: function() { - d3_geo_centroid.lineStart = d3_geo_centroidRingStart; - }, - polygonEnd: function() { - d3_geo_centroid.lineStart = d3_geo_centroidLineStart; - } - }; - function d3_geo_centroidPoint(λ, φ) { - λ *= d3_radians; - var cosφ = Math.cos(φ *= d3_radians); - d3_geo_centroidPointXYZ(cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ)); - } - function d3_geo_centroidPointXYZ(x, y, z) { - ++d3_geo_centroidW0; - d3_geo_centroidX0 += (x - d3_geo_centroidX0) / d3_geo_centroidW0; - d3_geo_centroidY0 += (y - d3_geo_centroidY0) / d3_geo_centroidW0; - d3_geo_centroidZ0 += (z - d3_geo_centroidZ0) / d3_geo_centroidW0; - } - function d3_geo_centroidLineStart() { - var x0, y0, z0; - d3_geo_centroid.point = function(λ, φ) { - λ *= d3_radians; - var cosφ = Math.cos(φ *= d3_radians); - x0 = cosφ * Math.cos(λ); - y0 = cosφ * Math.sin(λ); - z0 = Math.sin(φ); - d3_geo_centroid.point = nextPoint; - d3_geo_centroidPointXYZ(x0, y0, z0); - }; - function nextPoint(λ, φ) { - λ *= d3_radians; - var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z); - d3_geo_centroidW1 += w; - d3_geo_centroidX1 += w * (x0 + (x0 = x)); - d3_geo_centroidY1 += w * (y0 + (y0 = y)); - d3_geo_centroidZ1 += w * (z0 + (z0 = z)); - d3_geo_centroidPointXYZ(x0, y0, z0); - } - } - function d3_geo_centroidLineEnd() { - d3_geo_centroid.point = d3_geo_centroidPoint; - } - function d3_geo_centroidRingStart() { - var λ00, φ00, x0, y0, z0; - d3_geo_centroid.point = function(λ, φ) { - λ00 = λ, φ00 = φ; - d3_geo_centroid.point = nextPoint; - λ *= d3_radians; - var cosφ = Math.cos(φ *= d3_radians); - x0 = cosφ * Math.cos(λ); - y0 = cosφ * Math.sin(λ); - z0 = Math.sin(φ); - d3_geo_centroidPointXYZ(x0, y0, z0); - }; - d3_geo_centroid.lineEnd = function() { - nextPoint(λ00, φ00); - d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd; - d3_geo_centroid.point = d3_geo_centroidPoint; - }; - function nextPoint(λ, φ) { - λ *= d3_radians; - var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), cx = y0 * z - z0 * y, cy = z0 * x - x0 * z, cz = x0 * y - y0 * x, m = Math.sqrt(cx * cx + cy * cy + cz * cz), u = x0 * x + y0 * y + z0 * z, v = m && -d3_acos(u) / m, w = Math.atan2(m, u); - d3_geo_centroidX2 += v * cx; - d3_geo_centroidY2 += v * cy; - d3_geo_centroidZ2 += v * cz; - d3_geo_centroidW1 += w; - d3_geo_centroidX1 += w * (x0 + (x0 = x)); - d3_geo_centroidY1 += w * (y0 + (y0 = y)); - d3_geo_centroidZ1 += w * (z0 + (z0 = z)); - d3_geo_centroidPointXYZ(x0, y0, z0); - } - } - function d3_geo_compose(a, b) { - function compose(x, y) { - return x = a(x, y), b(x[0], x[1]); - } - if (a.invert && b.invert) compose.invert = function(x, y) { - return x = b.invert(x, y), x && a.invert(x[0], x[1]); - }; - return compose; - } - function d3_true() { - return true; - } - function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) { - var subject = [], clip = []; - segments.forEach(function(segment) { - if ((n = segment.length - 1) <= 0) return; - var n, p0 = segment[0], p1 = segment[n]; - if (d3_geo_sphericalEqual(p0, p1)) { - listener.lineStart(); - for (var i = 0; i < n; ++i) listener.point((p0 = segment[i])[0], p0[1]); - listener.lineEnd(); - return; - } - var a = new d3_geo_clipPolygonIntersection(p0, segment, null, true), b = new d3_geo_clipPolygonIntersection(p0, null, a, false); - a.o = b; - subject.push(a); - clip.push(b); - a = new d3_geo_clipPolygonIntersection(p1, segment, null, false); - b = new d3_geo_clipPolygonIntersection(p1, null, a, true); - a.o = b; - subject.push(a); - clip.push(b); - }); - clip.sort(compare); - d3_geo_clipPolygonLinkCircular(subject); - d3_geo_clipPolygonLinkCircular(clip); - if (!subject.length) return; - for (var i = 0, entry = clipStartInside, n = clip.length; i < n; ++i) { - clip[i].e = entry = !entry; - } - var start = subject[0], points, point; - while (1) { - var current = start, isSubject = true; - while (current.v) if ((current = current.n) === start) return; - points = current.z; - listener.lineStart(); - do { - current.v = current.o.v = true; - if (current.e) { - if (isSubject) { - for (var i = 0, n = points.length; i < n; ++i) listener.point((point = points[i])[0], point[1]); - } else { - interpolate(current.x, current.n.x, 1, listener); - } - current = current.n; - } else { - if (isSubject) { - points = current.p.z; - for (var i = points.length - 1; i >= 0; --i) listener.point((point = points[i])[0], point[1]); - } else { - interpolate(current.x, current.p.x, -1, listener); - } - current = current.p; - } - current = current.o; - points = current.z; - isSubject = !isSubject; - } while (!current.v); - listener.lineEnd(); - } - } - function d3_geo_clipPolygonLinkCircular(array) { - if (!(n = array.length)) return; - var n, i = 0, a = array[0], b; - while (++i < n) { - a.n = b = array[i]; - b.p = a; - a = b; - } - a.n = b = array[0]; - b.p = a; - } - function d3_geo_clipPolygonIntersection(point, points, other, entry) { - this.x = point; - this.z = points; - this.o = other; - this.e = entry; - this.v = false; - this.n = this.p = null; - } - function d3_geo_clip(pointVisible, clipLine, interpolate, clipStart) { - return function(rotate, listener) { - var line = clipLine(listener), rotatedClipStart = rotate.invert(clipStart[0], clipStart[1]); - var clip = { - point: point, - lineStart: lineStart, - lineEnd: lineEnd, - polygonStart: function() { - clip.point = pointRing; - clip.lineStart = ringStart; - clip.lineEnd = ringEnd; - segments = []; - polygon = []; - }, - polygonEnd: function() { - clip.point = point; - clip.lineStart = lineStart; - clip.lineEnd = lineEnd; - segments = d3.merge(segments); - var clipStartInside = d3_geo_pointInPolygon(rotatedClipStart, polygon); - if (segments.length) { - if (!polygonStarted) listener.polygonStart(), polygonStarted = true; - d3_geo_clipPolygon(segments, d3_geo_clipSort, clipStartInside, interpolate, listener); - } else if (clipStartInside) { - if (!polygonStarted) listener.polygonStart(), polygonStarted = true; - listener.lineStart(); - interpolate(null, null, 1, listener); - listener.lineEnd(); - } - if (polygonStarted) listener.polygonEnd(), polygonStarted = false; - segments = polygon = null; - }, - sphere: function() { - listener.polygonStart(); - listener.lineStart(); - interpolate(null, null, 1, listener); - listener.lineEnd(); - listener.polygonEnd(); - } - }; - function point(λ, φ) { - var point = rotate(λ, φ); - if (pointVisible(λ = point[0], φ = point[1])) listener.point(λ, φ); - } - function pointLine(λ, φ) { - var point = rotate(λ, φ); - line.point(point[0], point[1]); - } - function lineStart() { - clip.point = pointLine; - line.lineStart(); - } - function lineEnd() { - clip.point = point; - line.lineEnd(); - } - var segments; - var buffer = d3_geo_clipBufferListener(), ringListener = clipLine(buffer), polygonStarted = false, polygon, ring; - function pointRing(λ, φ) { - ring.push([ λ, φ ]); - var point = rotate(λ, φ); - ringListener.point(point[0], point[1]); - } - function ringStart() { - ringListener.lineStart(); - ring = []; - } - function ringEnd() { - pointRing(ring[0][0], ring[0][1]); - ringListener.lineEnd(); - var clean = ringListener.clean(), ringSegments = buffer.buffer(), segment, n = ringSegments.length; - ring.pop(); - polygon.push(ring); - ring = null; - if (!n) return; - if (clean & 1) { - segment = ringSegments[0]; - var n = segment.length - 1, i = -1, point; - if (n > 0) { - if (!polygonStarted) listener.polygonStart(), polygonStarted = true; - listener.lineStart(); - while (++i < n) listener.point((point = segment[i])[0], point[1]); - listener.lineEnd(); - } - return; - } - if (n > 1 && clean & 2) ringSegments.push(ringSegments.pop().concat(ringSegments.shift())); - segments.push(ringSegments.filter(d3_geo_clipSegmentLength1)); - } - return clip; - }; - } - function d3_geo_clipSegmentLength1(segment) { - return segment.length > 1; - } - function d3_geo_clipBufferListener() { - var lines = [], line; - return { - lineStart: function() { - lines.push(line = []); - }, - point: function(λ, φ) { - line.push([ λ, φ ]); - }, - lineEnd: d3_noop, - buffer: function() { - var buffer = lines; - lines = []; - line = null; - return buffer; - }, - rejoin: function() { - if (lines.length > 1) lines.push(lines.pop().concat(lines.shift())); - } - }; - } - function d3_geo_clipSort(a, b) { - return ((a = a.x)[0] < 0 ? a[1] - halfπ - ε : halfπ - a[1]) - ((b = b.x)[0] < 0 ? b[1] - halfπ - ε : halfπ - b[1]); - } - var d3_geo_clipAntimeridian = d3_geo_clip(d3_true, d3_geo_clipAntimeridianLine, d3_geo_clipAntimeridianInterpolate, [ -π, -π / 2 ]); - function d3_geo_clipAntimeridianLine(listener) { - var λ0 = NaN, φ0 = NaN, sλ0 = NaN, clean; - return { - lineStart: function() { - listener.lineStart(); - clean = 1; - }, - point: function(λ1, φ1) { - var sλ1 = λ1 > 0 ? π : -π, dλ = abs(λ1 - λ0); - if (abs(dλ - π) < ε) { - listener.point(λ0, φ0 = (φ0 + φ1) / 2 > 0 ? halfπ : -halfπ); - listener.point(sλ0, φ0); - listener.lineEnd(); - listener.lineStart(); - listener.point(sλ1, φ0); - listener.point(λ1, φ0); - clean = 0; - } else if (sλ0 !== sλ1 && dλ >= π) { - if (abs(λ0 - sλ0) < ε) λ0 -= sλ0 * ε; - if (abs(λ1 - sλ1) < ε) λ1 -= sλ1 * ε; - φ0 = d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1); - listener.point(sλ0, φ0); - listener.lineEnd(); - listener.lineStart(); - listener.point(sλ1, φ0); - clean = 0; - } - listener.point(λ0 = λ1, φ0 = φ1); - sλ0 = sλ1; - }, - lineEnd: function() { - listener.lineEnd(); - λ0 = φ0 = NaN; - }, - clean: function() { - return 2 - clean; - } - }; - } - function d3_geo_clipAntimeridianIntersect(λ0, φ0, λ1, φ1) { - var cosφ0, cosφ1, sinλ0_λ1 = Math.sin(λ0 - λ1); - return abs(sinλ0_λ1) > ε ? Math.atan((Math.sin(φ0) * (cosφ1 = Math.cos(φ1)) * Math.sin(λ1) - Math.sin(φ1) * (cosφ0 = Math.cos(φ0)) * Math.sin(λ0)) / (cosφ0 * cosφ1 * sinλ0_λ1)) : (φ0 + φ1) / 2; - } - function d3_geo_clipAntimeridianInterpolate(from, to, direction, listener) { - var φ; - if (from == null) { - φ = direction * halfπ; - listener.point(-π, φ); - listener.point(0, φ); - listener.point(π, φ); - listener.point(π, 0); - listener.point(π, -φ); - listener.point(0, -φ); - listener.point(-π, -φ); - listener.point(-π, 0); - listener.point(-π, φ); - } else if (abs(from[0] - to[0]) > ε) { - var s = from[0] < to[0] ? π : -π; - φ = direction * s / 2; - listener.point(-s, φ); - listener.point(0, φ); - listener.point(s, φ); - } else { - listener.point(to[0], to[1]); - } - } - function d3_geo_pointInPolygon(point, polygon) { - var meridian = point[0], parallel = point[1], meridianNormal = [ Math.sin(meridian), -Math.cos(meridian), 0 ], polarAngle = 0, winding = 0; - d3_geo_areaRingSum.reset(); - for (var i = 0, n = polygon.length; i < n; ++i) { - var ring = polygon[i], m = ring.length; - if (!m) continue; - var point0 = ring[0], λ0 = point0[0], φ0 = point0[1] / 2 + π / 4, sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), j = 1; - while (true) { - if (j === m) j = 0; - point = ring[j]; - var λ = point[0], φ = point[1] / 2 + π / 4, sinφ = Math.sin(φ), cosφ = Math.cos(φ), dλ = λ - λ0, sdλ = dλ >= 0 ? 1 : -1, adλ = sdλ * dλ, antimeridian = adλ > π, k = sinφ0 * sinφ; - d3_geo_areaRingSum.add(Math.atan2(k * sdλ * Math.sin(adλ), cosφ0 * cosφ + k * Math.cos(adλ))); - polarAngle += antimeridian ? dλ + sdλ * τ : dλ; - if (antimeridian ^ λ0 >= meridian ^ λ >= meridian) { - var arc = d3_geo_cartesianCross(d3_geo_cartesian(point0), d3_geo_cartesian(point)); - d3_geo_cartesianNormalize(arc); - var intersection = d3_geo_cartesianCross(meridianNormal, arc); - d3_geo_cartesianNormalize(intersection); - var φarc = (antimeridian ^ dλ >= 0 ? -1 : 1) * d3_asin(intersection[2]); - if (parallel > φarc || parallel === φarc && (arc[0] || arc[1])) { - winding += antimeridian ^ dλ >= 0 ? 1 : -1; - } - } - if (!j++) break; - λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ, point0 = point; - } - } - return (polarAngle < -ε || polarAngle < ε && d3_geo_areaRingSum < -ε) ^ winding & 1; - } - function d3_geo_clipCircle(radius) { - var cr = Math.cos(radius), smallRadius = cr > 0, notHemisphere = abs(cr) > ε, interpolate = d3_geo_circleInterpolate(radius, 6 * d3_radians); - return d3_geo_clip(visible, clipLine, interpolate, smallRadius ? [ 0, -radius ] : [ -π, radius - π ]); - function visible(λ, φ) { - return Math.cos(λ) * Math.cos(φ) > cr; - } - function clipLine(listener) { - var point0, c0, v0, v00, clean; - return { - lineStart: function() { - v00 = v0 = false; - clean = 1; - }, - point: function(λ, φ) { - var point1 = [ λ, φ ], point2, v = visible(λ, φ), c = smallRadius ? v ? 0 : code(λ, φ) : v ? code(λ + (λ < 0 ? π : -π), φ) : 0; - if (!point0 && (v00 = v0 = v)) listener.lineStart(); - if (v !== v0) { - point2 = intersect(point0, point1); - if (d3_geo_sphericalEqual(point0, point2) || d3_geo_sphericalEqual(point1, point2)) { - point1[0] += ε; - point1[1] += ε; - v = visible(point1[0], point1[1]); - } - } - if (v !== v0) { - clean = 0; - if (v) { - listener.lineStart(); - point2 = intersect(point1, point0); - listener.point(point2[0], point2[1]); - } else { - point2 = intersect(point0, point1); - listener.point(point2[0], point2[1]); - listener.lineEnd(); - } - point0 = point2; - } else if (notHemisphere && point0 && smallRadius ^ v) { - var t; - if (!(c & c0) && (t = intersect(point1, point0, true))) { - clean = 0; - if (smallRadius) { - listener.lineStart(); - listener.point(t[0][0], t[0][1]); - listener.point(t[1][0], t[1][1]); - listener.lineEnd(); - } else { - listener.point(t[1][0], t[1][1]); - listener.lineEnd(); - listener.lineStart(); - listener.point(t[0][0], t[0][1]); - } - } - } - if (v && (!point0 || !d3_geo_sphericalEqual(point0, point1))) { - listener.point(point1[0], point1[1]); - } - point0 = point1, v0 = v, c0 = c; - }, - lineEnd: function() { - if (v0) listener.lineEnd(); - point0 = null; - }, - clean: function() { - return clean | (v00 && v0) << 1; - } - }; - } - function intersect(a, b, two) { - var pa = d3_geo_cartesian(a), pb = d3_geo_cartesian(b); - var n1 = [ 1, 0, 0 ], n2 = d3_geo_cartesianCross(pa, pb), n2n2 = d3_geo_cartesianDot(n2, n2), n1n2 = n2[0], determinant = n2n2 - n1n2 * n1n2; - if (!determinant) return !two && a; - var c1 = cr * n2n2 / determinant, c2 = -cr * n1n2 / determinant, n1xn2 = d3_geo_cartesianCross(n1, n2), A = d3_geo_cartesianScale(n1, c1), B = d3_geo_cartesianScale(n2, c2); - d3_geo_cartesianAdd(A, B); - var u = n1xn2, w = d3_geo_cartesianDot(A, u), uu = d3_geo_cartesianDot(u, u), t2 = w * w - uu * (d3_geo_cartesianDot(A, A) - 1); - if (t2 < 0) return; - var t = Math.sqrt(t2), q = d3_geo_cartesianScale(u, (-w - t) / uu); - d3_geo_cartesianAdd(q, A); - q = d3_geo_spherical(q); - if (!two) return q; - var λ0 = a[0], λ1 = b[0], φ0 = a[1], φ1 = b[1], z; - if (λ1 < λ0) z = λ0, λ0 = λ1, λ1 = z; - var δλ = λ1 - λ0, polar = abs(δλ - π) < ε, meridian = polar || δλ < ε; - if (!polar && φ1 < φ0) z = φ0, φ0 = φ1, φ1 = z; - if (meridian ? polar ? φ0 + φ1 > 0 ^ q[1] < (abs(q[0] - λ0) < ε ? φ0 : φ1) : φ0 <= q[1] && q[1] <= φ1 : δλ > π ^ (λ0 <= q[0] && q[0] <= λ1)) { - var q1 = d3_geo_cartesianScale(u, (-w + t) / uu); - d3_geo_cartesianAdd(q1, A); - return [ q, d3_geo_spherical(q1) ]; - } - } - function code(λ, φ) { - var r = smallRadius ? radius : π - radius, code = 0; - if (λ < -r) code |= 1; else if (λ > r) code |= 2; - if (φ < -r) code |= 4; else if (φ > r) code |= 8; - return code; - } - } - function d3_geom_clipLine(x0, y0, x1, y1) { - return function(line) { - var a = line.a, b = line.b, ax = a.x, ay = a.y, bx = b.x, by = b.y, t0 = 0, t1 = 1, dx = bx - ax, dy = by - ay, r; - r = x0 - ax; - if (!dx && r > 0) return; - r /= dx; - if (dx < 0) { - if (r < t0) return; - if (r < t1) t1 = r; - } else if (dx > 0) { - if (r > t1) return; - if (r > t0) t0 = r; - } - r = x1 - ax; - if (!dx && r < 0) return; - r /= dx; - if (dx < 0) { - if (r > t1) return; - if (r > t0) t0 = r; - } else if (dx > 0) { - if (r < t0) return; - if (r < t1) t1 = r; - } - r = y0 - ay; - if (!dy && r > 0) return; - r /= dy; - if (dy < 0) { - if (r < t0) return; - if (r < t1) t1 = r; - } else if (dy > 0) { - if (r > t1) return; - if (r > t0) t0 = r; - } - r = y1 - ay; - if (!dy && r < 0) return; - r /= dy; - if (dy < 0) { - if (r > t1) return; - if (r > t0) t0 = r; - } else if (dy > 0) { - if (r < t0) return; - if (r < t1) t1 = r; - } - if (t0 > 0) line.a = { - x: ax + t0 * dx, - y: ay + t0 * dy - }; - if (t1 < 1) line.b = { - x: ax + t1 * dx, - y: ay + t1 * dy - }; - return line; - }; - } - var d3_geo_clipExtentMAX = 1e9; - d3.geo.clipExtent = function() { - var x0, y0, x1, y1, stream, clip, clipExtent = { - stream: function(output) { - if (stream) stream.valid = false; - stream = clip(output); - stream.valid = true; - return stream; - }, - extent: function(_) { - if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; - clip = d3_geo_clipExtent(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]); - if (stream) stream.valid = false, stream = null; - return clipExtent; - } - }; - return clipExtent.extent([ [ 0, 0 ], [ 960, 500 ] ]); - }; - function d3_geo_clipExtent(x0, y0, x1, y1) { - return function(listener) { - var listener_ = listener, bufferListener = d3_geo_clipBufferListener(), clipLine = d3_geom_clipLine(x0, y0, x1, y1), segments, polygon, ring; - var clip = { - point: point, - lineStart: lineStart, - lineEnd: lineEnd, - polygonStart: function() { - listener = bufferListener; - segments = []; - polygon = []; - clean = true; - }, - polygonEnd: function() { - listener = listener_; - segments = d3.merge(segments); - var clipStartInside = insidePolygon([ x0, y1 ]), inside = clean && clipStartInside, visible = segments.length; - if (inside || visible) { - listener.polygonStart(); - if (inside) { - listener.lineStart(); - interpolate(null, null, 1, listener); - listener.lineEnd(); - } - if (visible) { - d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener); - } - listener.polygonEnd(); - } - segments = polygon = ring = null; - } - }; - function insidePolygon(p) { - var wn = 0, n = polygon.length, y = p[1]; - for (var i = 0; i < n; ++i) { - for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) { - b = v[j]; - if (a[1] <= y) { - if (b[1] > y && d3_cross2d(a, b, p) > 0) ++wn; - } else { - if (b[1] <= y && d3_cross2d(a, b, p) < 0) --wn; - } - a = b; - } - } - return wn !== 0; - } - function interpolate(from, to, direction, listener) { - var a = 0, a1 = 0; - if (from == null || (a = corner(from, direction)) !== (a1 = corner(to, direction)) || comparePoints(from, to) < 0 ^ direction > 0) { - do { - listener.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0); - } while ((a = (a + direction + 4) % 4) !== a1); - } else { - listener.point(to[0], to[1]); - } - } - function pointVisible(x, y) { - return x0 <= x && x <= x1 && y0 <= y && y <= y1; - } - function point(x, y) { - if (pointVisible(x, y)) listener.point(x, y); - } - var x__, y__, v__, x_, y_, v_, first, clean; - function lineStart() { - clip.point = linePoint; - if (polygon) polygon.push(ring = []); - first = true; - v_ = false; - x_ = y_ = NaN; - } - function lineEnd() { - if (segments) { - linePoint(x__, y__); - if (v__ && v_) bufferListener.rejoin(); - segments.push(bufferListener.buffer()); - } - clip.point = point; - if (v_) listener.lineEnd(); - } - function linePoint(x, y) { - x = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, x)); - y = Math.max(-d3_geo_clipExtentMAX, Math.min(d3_geo_clipExtentMAX, y)); - var v = pointVisible(x, y); - if (polygon) ring.push([ x, y ]); - if (first) { - x__ = x, y__ = y, v__ = v; - first = false; - if (v) { - listener.lineStart(); - listener.point(x, y); - } - } else { - if (v && v_) listener.point(x, y); else { - var l = { - a: { - x: x_, - y: y_ - }, - b: { - x: x, - y: y - } - }; - if (clipLine(l)) { - if (!v_) { - listener.lineStart(); - listener.point(l.a.x, l.a.y); - } - listener.point(l.b.x, l.b.y); - if (!v) listener.lineEnd(); - clean = false; - } else if (v) { - listener.lineStart(); - listener.point(x, y); - clean = false; - } - } - } - x_ = x, y_ = y, v_ = v; - } - return clip; - }; - function corner(p, direction) { - return abs(p[0] - x0) < ε ? direction > 0 ? 0 : 3 : abs(p[0] - x1) < ε ? direction > 0 ? 2 : 1 : abs(p[1] - y0) < ε ? direction > 0 ? 1 : 0 : direction > 0 ? 3 : 2; - } - function compare(a, b) { - return comparePoints(a.x, b.x); - } - function comparePoints(a, b) { - var ca = corner(a, 1), cb = corner(b, 1); - return ca !== cb ? ca - cb : ca === 0 ? b[1] - a[1] : ca === 1 ? a[0] - b[0] : ca === 2 ? a[1] - b[1] : b[0] - a[0]; - } - } - function d3_geo_conic(projectAt) { - var φ0 = 0, φ1 = π / 3, m = d3_geo_projectionMutator(projectAt), p = m(φ0, φ1); - p.parallels = function(_) { - if (!arguments.length) return [ φ0 / π * 180, φ1 / π * 180 ]; - return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180); - }; - return p; - } - function d3_geo_conicEqualArea(φ0, φ1) { - var sinφ0 = Math.sin(φ0), n = (sinφ0 + Math.sin(φ1)) / 2, C = 1 + sinφ0 * (2 * n - sinφ0), ρ0 = Math.sqrt(C) / n; - function forward(λ, φ) { - var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n; - return [ ρ * Math.sin(λ *= n), ρ0 - ρ * Math.cos(λ) ]; - } - forward.invert = function(x, y) { - var ρ0_y = ρ0 - y; - return [ Math.atan2(x, ρ0_y) / n, d3_asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n)) ]; - }; - return forward; - } - (d3.geo.conicEqualArea = function() { - return d3_geo_conic(d3_geo_conicEqualArea); - }).raw = d3_geo_conicEqualArea; - d3.geo.albers = function() { - return d3.geo.conicEqualArea().rotate([ 96, 0 ]).center([ -.6, 38.7 ]).parallels([ 29.5, 45.5 ]).scale(1070); - }; - d3.geo.albersUsa = function() { - var lower48 = d3.geo.albers(); - var alaska = d3.geo.conicEqualArea().rotate([ 154, 0 ]).center([ -2, 58.5 ]).parallels([ 55, 65 ]); - var hawaii = d3.geo.conicEqualArea().rotate([ 157, 0 ]).center([ -3, 19.9 ]).parallels([ 8, 18 ]); - var point, pointStream = { - point: function(x, y) { - point = [ x, y ]; - } - }, lower48Point, alaskaPoint, hawaiiPoint; - function albersUsa(coordinates) { - var x = coordinates[0], y = coordinates[1]; - point = null; - (lower48Point(x, y), point) || (alaskaPoint(x, y), point) || hawaiiPoint(x, y); - return point; - } - albersUsa.invert = function(coordinates) { - var k = lower48.scale(), t = lower48.translate(), x = (coordinates[0] - t[0]) / k, y = (coordinates[1] - t[1]) / k; - return (y >= .12 && y < .234 && x >= -.425 && x < -.214 ? alaska : y >= .166 && y < .234 && x >= -.214 && x < -.115 ? hawaii : lower48).invert(coordinates); - }; - albersUsa.stream = function(stream) { - var lower48Stream = lower48.stream(stream), alaskaStream = alaska.stream(stream), hawaiiStream = hawaii.stream(stream); - return { - point: function(x, y) { - lower48Stream.point(x, y); - alaskaStream.point(x, y); - hawaiiStream.point(x, y); - }, - sphere: function() { - lower48Stream.sphere(); - alaskaStream.sphere(); - hawaiiStream.sphere(); - }, - lineStart: function() { - lower48Stream.lineStart(); - alaskaStream.lineStart(); - hawaiiStream.lineStart(); - }, - lineEnd: function() { - lower48Stream.lineEnd(); - alaskaStream.lineEnd(); - hawaiiStream.lineEnd(); - }, - polygonStart: function() { - lower48Stream.polygonStart(); - alaskaStream.polygonStart(); - hawaiiStream.polygonStart(); - }, - polygonEnd: function() { - lower48Stream.polygonEnd(); - alaskaStream.polygonEnd(); - hawaiiStream.polygonEnd(); - } - }; - }; - albersUsa.precision = function(_) { - if (!arguments.length) return lower48.precision(); - lower48.precision(_); - alaska.precision(_); - hawaii.precision(_); - return albersUsa; - }; - albersUsa.scale = function(_) { - if (!arguments.length) return lower48.scale(); - lower48.scale(_); - alaska.scale(_ * .35); - hawaii.scale(_); - return albersUsa.translate(lower48.translate()); - }; - albersUsa.translate = function(_) { - if (!arguments.length) return lower48.translate(); - var k = lower48.scale(), x = +_[0], y = +_[1]; - lower48Point = lower48.translate(_).clipExtent([ [ x - .455 * k, y - .238 * k ], [ x + .455 * k, y + .238 * k ] ]).stream(pointStream).point; - alaskaPoint = alaska.translate([ x - .307 * k, y + .201 * k ]).clipExtent([ [ x - .425 * k + ε, y + .12 * k + ε ], [ x - .214 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; - hawaiiPoint = hawaii.translate([ x - .205 * k, y + .212 * k ]).clipExtent([ [ x - .214 * k + ε, y + .166 * k + ε ], [ x - .115 * k - ε, y + .234 * k - ε ] ]).stream(pointStream).point; - return albersUsa; - }; - return albersUsa.scale(1070); - }; - var d3_geo_pathAreaSum, d3_geo_pathAreaPolygon, d3_geo_pathArea = { - point: d3_noop, - lineStart: d3_noop, - lineEnd: d3_noop, - polygonStart: function() { - d3_geo_pathAreaPolygon = 0; - d3_geo_pathArea.lineStart = d3_geo_pathAreaRingStart; - }, - polygonEnd: function() { - d3_geo_pathArea.lineStart = d3_geo_pathArea.lineEnd = d3_geo_pathArea.point = d3_noop; - d3_geo_pathAreaSum += abs(d3_geo_pathAreaPolygon / 2); - } - }; - function d3_geo_pathAreaRingStart() { - var x00, y00, x0, y0; - d3_geo_pathArea.point = function(x, y) { - d3_geo_pathArea.point = nextPoint; - x00 = x0 = x, y00 = y0 = y; - }; - function nextPoint(x, y) { - d3_geo_pathAreaPolygon += y0 * x - x0 * y; - x0 = x, y0 = y; - } - d3_geo_pathArea.lineEnd = function() { - nextPoint(x00, y00); - }; - } - var d3_geo_pathBoundsX0, d3_geo_pathBoundsY0, d3_geo_pathBoundsX1, d3_geo_pathBoundsY1; - var d3_geo_pathBounds = { - point: d3_geo_pathBoundsPoint, - lineStart: d3_noop, - lineEnd: d3_noop, - polygonStart: d3_noop, - polygonEnd: d3_noop - }; - function d3_geo_pathBoundsPoint(x, y) { - if (x < d3_geo_pathBoundsX0) d3_geo_pathBoundsX0 = x; - if (x > d3_geo_pathBoundsX1) d3_geo_pathBoundsX1 = x; - if (y < d3_geo_pathBoundsY0) d3_geo_pathBoundsY0 = y; - if (y > d3_geo_pathBoundsY1) d3_geo_pathBoundsY1 = y; - } - function d3_geo_pathBuffer() { - var pointCircle = d3_geo_pathBufferCircle(4.5), buffer = []; - var stream = { - point: point, - lineStart: function() { - stream.point = pointLineStart; - }, - lineEnd: lineEnd, - polygonStart: function() { - stream.lineEnd = lineEndPolygon; - }, - polygonEnd: function() { - stream.lineEnd = lineEnd; - stream.point = point; - }, - pointRadius: function(_) { - pointCircle = d3_geo_pathBufferCircle(_); - return stream; - }, - result: function() { - if (buffer.length) { - var result = buffer.join(""); - buffer = []; - return result; - } - } - }; - function point(x, y) { - buffer.push("M", x, ",", y, pointCircle); - } - function pointLineStart(x, y) { - buffer.push("M", x, ",", y); - stream.point = pointLine; - } - function pointLine(x, y) { - buffer.push("L", x, ",", y); - } - function lineEnd() { - stream.point = point; - } - function lineEndPolygon() { - buffer.push("Z"); - } - return stream; - } - function d3_geo_pathBufferCircle(radius) { - return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + 2 * radius + "z"; - } - var d3_geo_pathCentroid = { - point: d3_geo_pathCentroidPoint, - lineStart: d3_geo_pathCentroidLineStart, - lineEnd: d3_geo_pathCentroidLineEnd, - polygonStart: function() { - d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidRingStart; - }, - polygonEnd: function() { - d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; - d3_geo_pathCentroid.lineStart = d3_geo_pathCentroidLineStart; - d3_geo_pathCentroid.lineEnd = d3_geo_pathCentroidLineEnd; - } - }; - function d3_geo_pathCentroidPoint(x, y) { - d3_geo_centroidX0 += x; - d3_geo_centroidY0 += y; - ++d3_geo_centroidZ0; - } - function d3_geo_pathCentroidLineStart() { - var x0, y0; - d3_geo_pathCentroid.point = function(x, y) { - d3_geo_pathCentroid.point = nextPoint; - d3_geo_pathCentroidPoint(x0 = x, y0 = y); - }; - function nextPoint(x, y) { - var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); - d3_geo_centroidX1 += z * (x0 + x) / 2; - d3_geo_centroidY1 += z * (y0 + y) / 2; - d3_geo_centroidZ1 += z; - d3_geo_pathCentroidPoint(x0 = x, y0 = y); - } - } - function d3_geo_pathCentroidLineEnd() { - d3_geo_pathCentroid.point = d3_geo_pathCentroidPoint; - } - function d3_geo_pathCentroidRingStart() { - var x00, y00, x0, y0; - d3_geo_pathCentroid.point = function(x, y) { - d3_geo_pathCentroid.point = nextPoint; - d3_geo_pathCentroidPoint(x00 = x0 = x, y00 = y0 = y); - }; - function nextPoint(x, y) { - var dx = x - x0, dy = y - y0, z = Math.sqrt(dx * dx + dy * dy); - d3_geo_centroidX1 += z * (x0 + x) / 2; - d3_geo_centroidY1 += z * (y0 + y) / 2; - d3_geo_centroidZ1 += z; - z = y0 * x - x0 * y; - d3_geo_centroidX2 += z * (x0 + x); - d3_geo_centroidY2 += z * (y0 + y); - d3_geo_centroidZ2 += z * 3; - d3_geo_pathCentroidPoint(x0 = x, y0 = y); - } - d3_geo_pathCentroid.lineEnd = function() { - nextPoint(x00, y00); - }; - } - function d3_geo_pathContext(context) { - var pointRadius = 4.5; - var stream = { - point: point, - lineStart: function() { - stream.point = pointLineStart; - }, - lineEnd: lineEnd, - polygonStart: function() { - stream.lineEnd = lineEndPolygon; - }, - polygonEnd: function() { - stream.lineEnd = lineEnd; - stream.point = point; - }, - pointRadius: function(_) { - pointRadius = _; - return stream; - }, - result: d3_noop - }; - function point(x, y) { - context.moveTo(x + pointRadius, y); - context.arc(x, y, pointRadius, 0, τ); - } - function pointLineStart(x, y) { - context.moveTo(x, y); - stream.point = pointLine; - } - function pointLine(x, y) { - context.lineTo(x, y); - } - function lineEnd() { - stream.point = point; - } - function lineEndPolygon() { - context.closePath(); - } - return stream; - } - function d3_geo_resample(project) { - var δ2 = .5, cosMinDistance = Math.cos(30 * d3_radians), maxDepth = 16; - function resample(stream) { - return (maxDepth ? resampleRecursive : resampleNone)(stream); - } - function resampleNone(stream) { - return d3_geo_transformPoint(stream, function(x, y) { - x = project(x, y); - stream.point(x[0], x[1]); - }); - } - function resampleRecursive(stream) { - var λ00, φ00, x00, y00, a00, b00, c00, λ0, x0, y0, a0, b0, c0; - var resample = { - point: point, - lineStart: lineStart, - lineEnd: lineEnd, - polygonStart: function() { - stream.polygonStart(); - resample.lineStart = ringStart; - }, - polygonEnd: function() { - stream.polygonEnd(); - resample.lineStart = lineStart; - } - }; - function point(x, y) { - x = project(x, y); - stream.point(x[0], x[1]); - } - function lineStart() { - x0 = NaN; - resample.point = linePoint; - stream.lineStart(); - } - function linePoint(λ, φ) { - var c = d3_geo_cartesian([ λ, φ ]), p = project(λ, φ); - resampleLineTo(x0, y0, λ0, a0, b0, c0, x0 = p[0], y0 = p[1], λ0 = λ, a0 = c[0], b0 = c[1], c0 = c[2], maxDepth, stream); - stream.point(x0, y0); - } - function lineEnd() { - resample.point = point; - stream.lineEnd(); - } - function ringStart() { - lineStart(); - resample.point = ringPoint; - resample.lineEnd = ringEnd; - } - function ringPoint(λ, φ) { - linePoint(λ00 = λ, φ00 = φ), x00 = x0, y00 = y0, a00 = a0, b00 = b0, c00 = c0; - resample.point = linePoint; - } - function ringEnd() { - resampleLineTo(x0, y0, λ0, a0, b0, c0, x00, y00, λ00, a00, b00, c00, maxDepth, stream); - resample.lineEnd = lineEnd; - lineEnd(); - } - return resample; - } - function resampleLineTo(x0, y0, λ0, a0, b0, c0, x1, y1, λ1, a1, b1, c1, depth, stream) { - var dx = x1 - x0, dy = y1 - y0, d2 = dx * dx + dy * dy; - if (d2 > 4 * δ2 && depth--) { - var a = a0 + a1, b = b0 + b1, c = c0 + c1, m = Math.sqrt(a * a + b * b + c * c), φ2 = Math.asin(c /= m), λ2 = abs(abs(c) - 1) < ε || abs(λ0 - λ1) < ε ? (λ0 + λ1) / 2 : Math.atan2(b, a), p = project(λ2, φ2), x2 = p[0], y2 = p[1], dx2 = x2 - x0, dy2 = y2 - y0, dz = dy * dx2 - dx * dy2; - if (dz * dz / d2 > δ2 || abs((dx * dx2 + dy * dy2) / d2 - .5) > .3 || a0 * a1 + b0 * b1 + c0 * c1 < cosMinDistance) { - resampleLineTo(x0, y0, λ0, a0, b0, c0, x2, y2, λ2, a /= m, b /= m, c, depth, stream); - stream.point(x2, y2); - resampleLineTo(x2, y2, λ2, a, b, c, x1, y1, λ1, a1, b1, c1, depth, stream); - } - } - } - resample.precision = function(_) { - if (!arguments.length) return Math.sqrt(δ2); - maxDepth = (δ2 = _ * _) > 0 && 16; - return resample; - }; - return resample; - } - d3.geo.path = function() { - var pointRadius = 4.5, projection, context, projectStream, contextStream, cacheStream; - function path(object) { - if (object) { - if (typeof pointRadius === "function") contextStream.pointRadius(+pointRadius.apply(this, arguments)); - if (!cacheStream || !cacheStream.valid) cacheStream = projectStream(contextStream); - d3.geo.stream(object, cacheStream); - } - return contextStream.result(); - } - path.area = function(object) { - d3_geo_pathAreaSum = 0; - d3.geo.stream(object, projectStream(d3_geo_pathArea)); - return d3_geo_pathAreaSum; - }; - path.centroid = function(object) { - d3_geo_centroidX0 = d3_geo_centroidY0 = d3_geo_centroidZ0 = d3_geo_centroidX1 = d3_geo_centroidY1 = d3_geo_centroidZ1 = d3_geo_centroidX2 = d3_geo_centroidY2 = d3_geo_centroidZ2 = 0; - d3.geo.stream(object, projectStream(d3_geo_pathCentroid)); - return d3_geo_centroidZ2 ? [ d3_geo_centroidX2 / d3_geo_centroidZ2, d3_geo_centroidY2 / d3_geo_centroidZ2 ] : d3_geo_centroidZ1 ? [ d3_geo_centroidX1 / d3_geo_centroidZ1, d3_geo_centroidY1 / d3_geo_centroidZ1 ] : d3_geo_centroidZ0 ? [ d3_geo_centroidX0 / d3_geo_centroidZ0, d3_geo_centroidY0 / d3_geo_centroidZ0 ] : [ NaN, NaN ]; - }; - path.bounds = function(object) { - d3_geo_pathBoundsX1 = d3_geo_pathBoundsY1 = -(d3_geo_pathBoundsX0 = d3_geo_pathBoundsY0 = Infinity); - d3.geo.stream(object, projectStream(d3_geo_pathBounds)); - return [ [ d3_geo_pathBoundsX0, d3_geo_pathBoundsY0 ], [ d3_geo_pathBoundsX1, d3_geo_pathBoundsY1 ] ]; - }; - path.projection = function(_) { - if (!arguments.length) return projection; - projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity; - return reset(); - }; - path.context = function(_) { - if (!arguments.length) return context; - contextStream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_); - if (typeof pointRadius !== "function") contextStream.pointRadius(pointRadius); - return reset(); - }; - path.pointRadius = function(_) { - if (!arguments.length) return pointRadius; - pointRadius = typeof _ === "function" ? _ : (contextStream.pointRadius(+_), +_); - return path; - }; - function reset() { - cacheStream = null; - return path; - } - return path.projection(d3.geo.albersUsa()).context(null); - }; - function d3_geo_pathProjectStream(project) { - var resample = d3_geo_resample(function(x, y) { - return project([ x * d3_degrees, y * d3_degrees ]); - }); - return function(stream) { - return d3_geo_projectionRadians(resample(stream)); - }; - } - d3.geo.transform = function(methods) { - return { - stream: function(stream) { - var transform = new d3_geo_transform(stream); - for (var k in methods) transform[k] = methods[k]; - return transform; - } - }; - }; - function d3_geo_transform(stream) { - this.stream = stream; - } - d3_geo_transform.prototype = { - point: function(x, y) { - this.stream.point(x, y); - }, - sphere: function() { - this.stream.sphere(); - }, - lineStart: function() { - this.stream.lineStart(); - }, - lineEnd: function() { - this.stream.lineEnd(); - }, - polygonStart: function() { - this.stream.polygonStart(); - }, - polygonEnd: function() { - this.stream.polygonEnd(); - } - }; - function d3_geo_transformPoint(stream, point) { - return { - point: point, - sphere: function() { - stream.sphere(); - }, - lineStart: function() { - stream.lineStart(); - }, - lineEnd: function() { - stream.lineEnd(); - }, - polygonStart: function() { - stream.polygonStart(); - }, - polygonEnd: function() { - stream.polygonEnd(); - } - }; - } - d3.geo.projection = d3_geo_projection; - d3.geo.projectionMutator = d3_geo_projectionMutator; - function d3_geo_projection(project) { - return d3_geo_projectionMutator(function() { - return project; - })(); - } - function d3_geo_projectionMutator(projectAt) { - var project, rotate, projectRotate, projectResample = d3_geo_resample(function(x, y) { - x = project(x, y); - return [ x[0] * k + δx, δy - x[1] * k ]; - }), k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx, δy, preclip = d3_geo_clipAntimeridian, postclip = d3_identity, clipAngle = null, clipExtent = null, stream; - function projection(point) { - point = projectRotate(point[0] * d3_radians, point[1] * d3_radians); - return [ point[0] * k + δx, δy - point[1] * k ]; - } - function invert(point) { - point = projectRotate.invert((point[0] - δx) / k, (δy - point[1]) / k); - return point && [ point[0] * d3_degrees, point[1] * d3_degrees ]; - } - projection.stream = function(output) { - if (stream) stream.valid = false; - stream = d3_geo_projectionRadians(preclip(rotate, projectResample(postclip(output)))); - stream.valid = true; - return stream; - }; - projection.clipAngle = function(_) { - if (!arguments.length) return clipAngle; - preclip = _ == null ? (clipAngle = _, d3_geo_clipAntimeridian) : d3_geo_clipCircle((clipAngle = +_) * d3_radians); - return invalidate(); - }; - projection.clipExtent = function(_) { - if (!arguments.length) return clipExtent; - clipExtent = _; - postclip = _ ? d3_geo_clipExtent(_[0][0], _[0][1], _[1][0], _[1][1]) : d3_identity; - return invalidate(); - }; - projection.scale = function(_) { - if (!arguments.length) return k; - k = +_; - return reset(); - }; - projection.translate = function(_) { - if (!arguments.length) return [ x, y ]; - x = +_[0]; - y = +_[1]; - return reset(); - }; - projection.center = function(_) { - if (!arguments.length) return [ λ * d3_degrees, φ * d3_degrees ]; - λ = _[0] % 360 * d3_radians; - φ = _[1] % 360 * d3_radians; - return reset(); - }; - projection.rotate = function(_) { - if (!arguments.length) return [ δλ * d3_degrees, δφ * d3_degrees, δγ * d3_degrees ]; - δλ = _[0] % 360 * d3_radians; - δφ = _[1] % 360 * d3_radians; - δγ = _.length > 2 ? _[2] % 360 * d3_radians : 0; - return reset(); - }; - d3.rebind(projection, projectResample, "precision"); - function reset() { - projectRotate = d3_geo_compose(rotate = d3_geo_rotation(δλ, δφ, δγ), project); - var center = project(λ, φ); - δx = x - center[0] * k; - δy = y + center[1] * k; - return invalidate(); - } - function invalidate() { - if (stream) stream.valid = false, stream = null; - return projection; - } - return function() { - project = projectAt.apply(this, arguments); - projection.invert = project.invert && invert; - return reset(); - }; - } - function d3_geo_projectionRadians(stream) { - return d3_geo_transformPoint(stream, function(x, y) { - stream.point(x * d3_radians, y * d3_radians); - }); - } - function d3_geo_equirectangular(λ, φ) { - return [ λ, φ ]; - } - (d3.geo.equirectangular = function() { - return d3_geo_projection(d3_geo_equirectangular); - }).raw = d3_geo_equirectangular.invert = d3_geo_equirectangular; - d3.geo.rotation = function(rotate) { - rotate = d3_geo_rotation(rotate[0] % 360 * d3_radians, rotate[1] * d3_radians, rotate.length > 2 ? rotate[2] * d3_radians : 0); - function forward(coordinates) { - coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians); - return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; - } - forward.invert = function(coordinates) { - coordinates = rotate.invert(coordinates[0] * d3_radians, coordinates[1] * d3_radians); - return coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees, coordinates; - }; - return forward; - }; - function d3_geo_identityRotation(λ, φ) { - return [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; - } - d3_geo_identityRotation.invert = d3_geo_equirectangular; - function d3_geo_rotation(δλ, δφ, δγ) { - return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation; - } - function d3_geo_forwardRotationλ(δλ) { - return function(λ, φ) { - return λ += δλ, [ λ > π ? λ - τ : λ < -π ? λ + τ : λ, φ ]; - }; - } - function d3_geo_rotationλ(δλ) { - var rotation = d3_geo_forwardRotationλ(δλ); - rotation.invert = d3_geo_forwardRotationλ(-δλ); - return rotation; - } - function d3_geo_rotationφγ(δφ, δγ) { - var cosδφ = Math.cos(δφ), sinδφ = Math.sin(δφ), cosδγ = Math.cos(δγ), sinδγ = Math.sin(δγ); - function rotation(λ, φ) { - var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδφ + x * sinδφ; - return [ Math.atan2(y * cosδγ - k * sinδγ, x * cosδφ - z * sinδφ), d3_asin(k * cosδγ + y * sinδγ) ]; - } - rotation.invert = function(λ, φ) { - var cosφ = Math.cos(φ), x = Math.cos(λ) * cosφ, y = Math.sin(λ) * cosφ, z = Math.sin(φ), k = z * cosδγ - y * sinδγ; - return [ Math.atan2(y * cosδγ + z * sinδγ, x * cosδφ + k * sinδφ), d3_asin(k * cosδφ - x * sinδφ) ]; - }; - return rotation; - } - d3.geo.circle = function() { - var origin = [ 0, 0 ], angle, precision = 6, interpolate; - function circle() { - var center = typeof origin === "function" ? origin.apply(this, arguments) : origin, rotate = d3_geo_rotation(-center[0] * d3_radians, -center[1] * d3_radians, 0).invert, ring = []; - interpolate(null, null, 1, { - point: function(x, y) { - ring.push(x = rotate(x, y)); - x[0] *= d3_degrees, x[1] *= d3_degrees; - } - }); - return { - type: "Polygon", - coordinates: [ ring ] - }; - } - circle.origin = function(x) { - if (!arguments.length) return origin; - origin = x; - return circle; - }; - circle.angle = function(x) { - if (!arguments.length) return angle; - interpolate = d3_geo_circleInterpolate((angle = +x) * d3_radians, precision * d3_radians); - return circle; - }; - circle.precision = function(_) { - if (!arguments.length) return precision; - interpolate = d3_geo_circleInterpolate(angle * d3_radians, (precision = +_) * d3_radians); - return circle; - }; - return circle.angle(90); - }; - function d3_geo_circleInterpolate(radius, precision) { - var cr = Math.cos(radius), sr = Math.sin(radius); - return function(from, to, direction, listener) { - var step = direction * precision; - if (from != null) { - from = d3_geo_circleAngle(cr, from); - to = d3_geo_circleAngle(cr, to); - if (direction > 0 ? from < to : from > to) from += direction * τ; - } else { - from = radius + direction * τ; - to = radius - .5 * step; - } - for (var point, t = from; direction > 0 ? t > to : t < to; t -= step) { - listener.point((point = d3_geo_spherical([ cr, -sr * Math.cos(t), -sr * Math.sin(t) ]))[0], point[1]); - } - }; - } - function d3_geo_circleAngle(cr, point) { - var a = d3_geo_cartesian(point); - a[0] -= cr; - d3_geo_cartesianNormalize(a); - var angle = d3_acos(-a[1]); - return ((-a[2] < 0 ? -angle : angle) + 2 * Math.PI - ε) % (2 * Math.PI); - } - d3.geo.distance = function(a, b) { - var Δλ = (b[0] - a[0]) * d3_radians, φ0 = a[1] * d3_radians, φ1 = b[1] * d3_radians, sinΔλ = Math.sin(Δλ), cosΔλ = Math.cos(Δλ), sinφ0 = Math.sin(φ0), cosφ0 = Math.cos(φ0), sinφ1 = Math.sin(φ1), cosφ1 = Math.cos(φ1), t; - return Math.atan2(Math.sqrt((t = cosφ1 * sinΔλ) * t + (t = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * t), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ); - }; - d3.geo.graticule = function() { - var x1, x0, X1, X0, y1, y0, Y1, Y0, dx = 10, dy = dx, DX = 90, DY = 360, x, y, X, Y, precision = 2.5; - function graticule() { - return { - type: "MultiLineString", - coordinates: lines() - }; - } - function lines() { - return d3.range(Math.ceil(X0 / DX) * DX, X1, DX).map(X).concat(d3.range(Math.ceil(Y0 / DY) * DY, Y1, DY).map(Y)).concat(d3.range(Math.ceil(x0 / dx) * dx, x1, dx).filter(function(x) { - return abs(x % DX) > ε; - }).map(x)).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).filter(function(y) { - return abs(y % DY) > ε; - }).map(y)); - } - graticule.lines = function() { - return lines().map(function(coordinates) { - return { - type: "LineString", - coordinates: coordinates - }; - }); - }; - graticule.outline = function() { - return { - type: "Polygon", - coordinates: [ X(X0).concat(Y(Y1).slice(1), X(X1).reverse().slice(1), Y(Y0).reverse().slice(1)) ] - }; - }; - graticule.extent = function(_) { - if (!arguments.length) return graticule.minorExtent(); - return graticule.majorExtent(_).minorExtent(_); - }; - graticule.majorExtent = function(_) { - if (!arguments.length) return [ [ X0, Y0 ], [ X1, Y1 ] ]; - X0 = +_[0][0], X1 = +_[1][0]; - Y0 = +_[0][1], Y1 = +_[1][1]; - if (X0 > X1) _ = X0, X0 = X1, X1 = _; - if (Y0 > Y1) _ = Y0, Y0 = Y1, Y1 = _; - return graticule.precision(precision); - }; - graticule.minorExtent = function(_) { - if (!arguments.length) return [ [ x0, y0 ], [ x1, y1 ] ]; - x0 = +_[0][0], x1 = +_[1][0]; - y0 = +_[0][1], y1 = +_[1][1]; - if (x0 > x1) _ = x0, x0 = x1, x1 = _; - if (y0 > y1) _ = y0, y0 = y1, y1 = _; - return graticule.precision(precision); - }; - graticule.step = function(_) { - if (!arguments.length) return graticule.minorStep(); - return graticule.majorStep(_).minorStep(_); - }; - graticule.majorStep = function(_) { - if (!arguments.length) return [ DX, DY ]; - DX = +_[0], DY = +_[1]; - return graticule; - }; - graticule.minorStep = function(_) { - if (!arguments.length) return [ dx, dy ]; - dx = +_[0], dy = +_[1]; - return graticule; - }; - graticule.precision = function(_) { - if (!arguments.length) return precision; - precision = +_; - x = d3_geo_graticuleX(y0, y1, 90); - y = d3_geo_graticuleY(x0, x1, precision); - X = d3_geo_graticuleX(Y0, Y1, 90); - Y = d3_geo_graticuleY(X0, X1, precision); - return graticule; - }; - return graticule.majorExtent([ [ -180, -90 + ε ], [ 180, 90 - ε ] ]).minorExtent([ [ -180, -80 - ε ], [ 180, 80 + ε ] ]); - }; - function d3_geo_graticuleX(y0, y1, dy) { - var y = d3.range(y0, y1 - ε, dy).concat(y1); - return function(x) { - return y.map(function(y) { - return [ x, y ]; - }); - }; - } - function d3_geo_graticuleY(x0, x1, dx) { - var x = d3.range(x0, x1 - ε, dx).concat(x1); - return function(y) { - return x.map(function(x) { - return [ x, y ]; - }); - }; - } - function d3_source(d) { - return d.source; - } - function d3_target(d) { - return d.target; - } - d3.geo.greatArc = function() { - var source = d3_source, source_, target = d3_target, target_; - function greatArc() { - return { - type: "LineString", - coordinates: [ source_ || source.apply(this, arguments), target_ || target.apply(this, arguments) ] - }; - } - greatArc.distance = function() { - return d3.geo.distance(source_ || source.apply(this, arguments), target_ || target.apply(this, arguments)); - }; - greatArc.source = function(_) { - if (!arguments.length) return source; - source = _, source_ = typeof _ === "function" ? null : _; - return greatArc; - }; - greatArc.target = function(_) { - if (!arguments.length) return target; - target = _, target_ = typeof _ === "function" ? null : _; - return greatArc; - }; - greatArc.precision = function() { - return arguments.length ? greatArc : 0; - }; - return greatArc; - }; - d3.geo.interpolate = function(source, target) { - return d3_geo_interpolate(source[0] * d3_radians, source[1] * d3_radians, target[0] * d3_radians, target[1] * d3_radians); - }; - function d3_geo_interpolate(x0, y0, x1, y1) { - var cy0 = Math.cos(y0), sy0 = Math.sin(y0), cy1 = Math.cos(y1), sy1 = Math.sin(y1), kx0 = cy0 * Math.cos(x0), ky0 = cy0 * Math.sin(x0), kx1 = cy1 * Math.cos(x1), ky1 = cy1 * Math.sin(x1), d = 2 * Math.asin(Math.sqrt(d3_haversin(y1 - y0) + cy0 * cy1 * d3_haversin(x1 - x0))), k = 1 / Math.sin(d); - var interpolate = d ? function(t) { - var B = Math.sin(t *= d) * k, A = Math.sin(d - t) * k, x = A * kx0 + B * kx1, y = A * ky0 + B * ky1, z = A * sy0 + B * sy1; - return [ Math.atan2(y, x) * d3_degrees, Math.atan2(z, Math.sqrt(x * x + y * y)) * d3_degrees ]; - } : function() { - return [ x0 * d3_degrees, y0 * d3_degrees ]; - }; - interpolate.distance = d; - return interpolate; - } - d3.geo.length = function(object) { - d3_geo_lengthSum = 0; - d3.geo.stream(object, d3_geo_length); - return d3_geo_lengthSum; - }; - var d3_geo_lengthSum; - var d3_geo_length = { - sphere: d3_noop, - point: d3_noop, - lineStart: d3_geo_lengthLineStart, - lineEnd: d3_noop, - polygonStart: d3_noop, - polygonEnd: d3_noop - }; - function d3_geo_lengthLineStart() { - var λ0, sinφ0, cosφ0; - d3_geo_length.point = function(λ, φ) { - λ0 = λ * d3_radians, sinφ0 = Math.sin(φ *= d3_radians), cosφ0 = Math.cos(φ); - d3_geo_length.point = nextPoint; - }; - d3_geo_length.lineEnd = function() { - d3_geo_length.point = d3_geo_length.lineEnd = d3_noop; - }; - function nextPoint(λ, φ) { - var sinφ = Math.sin(φ *= d3_radians), cosφ = Math.cos(φ), t = abs((λ *= d3_radians) - λ0), cosΔλ = Math.cos(t); - d3_geo_lengthSum += Math.atan2(Math.sqrt((t = cosφ * Math.sin(t)) * t + (t = cosφ0 * sinφ - sinφ0 * cosφ * cosΔλ) * t), sinφ0 * sinφ + cosφ0 * cosφ * cosΔλ); - λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ; - } - } - function d3_geo_azimuthal(scale, angle) { - function azimuthal(λ, φ) { - var cosλ = Math.cos(λ), cosφ = Math.cos(φ), k = scale(cosλ * cosφ); - return [ k * cosφ * Math.sin(λ), k * Math.sin(φ) ]; - } - azimuthal.invert = function(x, y) { - var ρ = Math.sqrt(x * x + y * y), c = angle(ρ), sinc = Math.sin(c), cosc = Math.cos(c); - return [ Math.atan2(x * sinc, ρ * cosc), Math.asin(ρ && y * sinc / ρ) ]; - }; - return azimuthal; - } - var d3_geo_azimuthalEqualArea = d3_geo_azimuthal(function(cosλcosφ) { - return Math.sqrt(2 / (1 + cosλcosφ)); - }, function(ρ) { - return 2 * Math.asin(ρ / 2); - }); - (d3.geo.azimuthalEqualArea = function() { - return d3_geo_projection(d3_geo_azimuthalEqualArea); - }).raw = d3_geo_azimuthalEqualArea; - var d3_geo_azimuthalEquidistant = d3_geo_azimuthal(function(cosλcosφ) { - var c = Math.acos(cosλcosφ); - return c && c / Math.sin(c); - }, d3_identity); - (d3.geo.azimuthalEquidistant = function() { - return d3_geo_projection(d3_geo_azimuthalEquidistant); - }).raw = d3_geo_azimuthalEquidistant; - function d3_geo_conicConformal(φ0, φ1) { - var cosφ0 = Math.cos(φ0), t = function(φ) { - return Math.tan(π / 4 + φ / 2); - }, n = φ0 === φ1 ? Math.sin(φ0) : Math.log(cosφ0 / Math.cos(φ1)) / Math.log(t(φ1) / t(φ0)), F = cosφ0 * Math.pow(t(φ0), n) / n; - if (!n) return d3_geo_mercator; - function forward(λ, φ) { - if (F > 0) { - if (φ < -halfπ + ε) φ = -halfπ + ε; - } else { - if (φ > halfπ - ε) φ = halfπ - ε; - } - var ρ = F / Math.pow(t(φ), n); - return [ ρ * Math.sin(n * λ), F - ρ * Math.cos(n * λ) ]; - } - forward.invert = function(x, y) { - var ρ0_y = F - y, ρ = d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y); - return [ Math.atan2(x, ρ0_y) / n, 2 * Math.atan(Math.pow(F / ρ, 1 / n)) - halfπ ]; - }; - return forward; - } - (d3.geo.conicConformal = function() { - return d3_geo_conic(d3_geo_conicConformal); - }).raw = d3_geo_conicConformal; - function d3_geo_conicEquidistant(φ0, φ1) { - var cosφ0 = Math.cos(φ0), n = φ0 === φ1 ? Math.sin(φ0) : (cosφ0 - Math.cos(φ1)) / (φ1 - φ0), G = cosφ0 / n + φ0; - if (abs(n) < ε) return d3_geo_equirectangular; - function forward(λ, φ) { - var ρ = G - φ; - return [ ρ * Math.sin(n * λ), G - ρ * Math.cos(n * λ) ]; - } - forward.invert = function(x, y) { - var ρ0_y = G - y; - return [ Math.atan2(x, ρ0_y) / n, G - d3_sgn(n) * Math.sqrt(x * x + ρ0_y * ρ0_y) ]; - }; - return forward; - } - (d3.geo.conicEquidistant = function() { - return d3_geo_conic(d3_geo_conicEquidistant); - }).raw = d3_geo_conicEquidistant; - var d3_geo_gnomonic = d3_geo_azimuthal(function(cosλcosφ) { - return 1 / cosλcosφ; - }, Math.atan); - (d3.geo.gnomonic = function() { - return d3_geo_projection(d3_geo_gnomonic); - }).raw = d3_geo_gnomonic; - function d3_geo_mercator(λ, φ) { - return [ λ, Math.log(Math.tan(π / 4 + φ / 2)) ]; - } - d3_geo_mercator.invert = function(x, y) { - return [ x, 2 * Math.atan(Math.exp(y)) - halfπ ]; - }; - function d3_geo_mercatorProjection(project) { - var m = d3_geo_projection(project), scale = m.scale, translate = m.translate, clipExtent = m.clipExtent, clipAuto; - m.scale = function() { - var v = scale.apply(m, arguments); - return v === m ? clipAuto ? m.clipExtent(null) : m : v; - }; - m.translate = function() { - var v = translate.apply(m, arguments); - return v === m ? clipAuto ? m.clipExtent(null) : m : v; - }; - m.clipExtent = function(_) { - var v = clipExtent.apply(m, arguments); - if (v === m) { - if (clipAuto = _ == null) { - var k = π * scale(), t = translate(); - clipExtent([ [ t[0] - k, t[1] - k ], [ t[0] + k, t[1] + k ] ]); - } - } else if (clipAuto) { - v = null; - } - return v; - }; - return m.clipExtent(null); - } - (d3.geo.mercator = function() { - return d3_geo_mercatorProjection(d3_geo_mercator); - }).raw = d3_geo_mercator; - var d3_geo_orthographic = d3_geo_azimuthal(function() { - return 1; - }, Math.asin); - (d3.geo.orthographic = function() { - return d3_geo_projection(d3_geo_orthographic); - }).raw = d3_geo_orthographic; - var d3_geo_stereographic = d3_geo_azimuthal(function(cosλcosφ) { - return 1 / (1 + cosλcosφ); - }, function(ρ) { - return 2 * Math.atan(ρ); - }); - (d3.geo.stereographic = function() { - return d3_geo_projection(d3_geo_stereographic); - }).raw = d3_geo_stereographic; - function d3_geo_transverseMercator(λ, φ) { - return [ Math.log(Math.tan(π / 4 + φ / 2)), -λ ]; - } - d3_geo_transverseMercator.invert = function(x, y) { - return [ -y, 2 * Math.atan(Math.exp(x)) - halfπ ]; - }; - (d3.geo.transverseMercator = function() { - var projection = d3_geo_mercatorProjection(d3_geo_transverseMercator), center = projection.center, rotate = projection.rotate; - projection.center = function(_) { - return _ ? center([ -_[1], _[0] ]) : (_ = center(), [ _[1], -_[0] ]); - }; - projection.rotate = function(_) { - return _ ? rotate([ _[0], _[1], _.length > 2 ? _[2] + 90 : 90 ]) : (_ = rotate(), - [ _[0], _[1], _[2] - 90 ]); - }; - return rotate([ 0, 0, 90 ]); - }).raw = d3_geo_transverseMercator; - d3.geom = {}; - function d3_geom_pointX(d) { - return d[0]; - } - function d3_geom_pointY(d) { - return d[1]; - } - d3.geom.hull = function(vertices) { - var x = d3_geom_pointX, y = d3_geom_pointY; - if (arguments.length) return hull(vertices); - function hull(data) { - if (data.length < 3) return []; - var fx = d3_functor(x), fy = d3_functor(y), i, n = data.length, points = [], flippedPoints = []; - for (i = 0; i < n; i++) { - points.push([ +fx.call(this, data[i], i), +fy.call(this, data[i], i), i ]); - } - points.sort(d3_geom_hullOrder); - for (i = 0; i < n; i++) flippedPoints.push([ points[i][0], -points[i][1] ]); - var upper = d3_geom_hullUpper(points), lower = d3_geom_hullUpper(flippedPoints); - var skipLeft = lower[0] === upper[0], skipRight = lower[lower.length - 1] === upper[upper.length - 1], polygon = []; - for (i = upper.length - 1; i >= 0; --i) polygon.push(data[points[upper[i]][2]]); - for (i = +skipLeft; i < lower.length - skipRight; ++i) polygon.push(data[points[lower[i]][2]]); - return polygon; - } - hull.x = function(_) { - return arguments.length ? (x = _, hull) : x; - }; - hull.y = function(_) { - return arguments.length ? (y = _, hull) : y; - }; - return hull; - }; - function d3_geom_hullUpper(points) { - var n = points.length, hull = [ 0, 1 ], hs = 2; - for (var i = 2; i < n; i++) { - while (hs > 1 && d3_cross2d(points[hull[hs - 2]], points[hull[hs - 1]], points[i]) <= 0) --hs; - hull[hs++] = i; - } - return hull.slice(0, hs); - } - function d3_geom_hullOrder(a, b) { - return a[0] - b[0] || a[1] - b[1]; - } - d3.geom.polygon = function(coordinates) { - d3_subclass(coordinates, d3_geom_polygonPrototype); - return coordinates; - }; - var d3_geom_polygonPrototype = d3.geom.polygon.prototype = []; - d3_geom_polygonPrototype.area = function() { - var i = -1, n = this.length, a, b = this[n - 1], area = 0; - while (++i < n) { - a = b; - b = this[i]; - area += a[1] * b[0] - a[0] * b[1]; - } - return area * .5; - }; - d3_geom_polygonPrototype.centroid = function(k) { - var i = -1, n = this.length, x = 0, y = 0, a, b = this[n - 1], c; - if (!arguments.length) k = -1 / (6 * this.area()); - while (++i < n) { - a = b; - b = this[i]; - c = a[0] * b[1] - b[0] * a[1]; - x += (a[0] + b[0]) * c; - y += (a[1] + b[1]) * c; - } - return [ x * k, y * k ]; - }; - d3_geom_polygonPrototype.clip = function(subject) { - var input, closed = d3_geom_polygonClosed(subject), i = -1, n = this.length - d3_geom_polygonClosed(this), j, m, a = this[n - 1], b, c, d; - while (++i < n) { - input = subject.slice(); - subject.length = 0; - b = this[i]; - c = input[(m = input.length - closed) - 1]; - j = -1; - while (++j < m) { - d = input[j]; - if (d3_geom_polygonInside(d, a, b)) { - if (!d3_geom_polygonInside(c, a, b)) { - subject.push(d3_geom_polygonIntersect(c, d, a, b)); - } - subject.push(d); - } else if (d3_geom_polygonInside(c, a, b)) { - subject.push(d3_geom_polygonIntersect(c, d, a, b)); - } - c = d; - } - if (closed) subject.push(subject[0]); - a = b; - } - return subject; - }; - function d3_geom_polygonInside(p, a, b) { - return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]); - } - function d3_geom_polygonIntersect(c, d, a, b) { - var x1 = c[0], x3 = a[0], x21 = d[0] - x1, x43 = b[0] - x3, y1 = c[1], y3 = a[1], y21 = d[1] - y1, y43 = b[1] - y3, ua = (x43 * (y1 - y3) - y43 * (x1 - x3)) / (y43 * x21 - x43 * y21); - return [ x1 + ua * x21, y1 + ua * y21 ]; - } - function d3_geom_polygonClosed(coordinates) { - var a = coordinates[0], b = coordinates[coordinates.length - 1]; - return !(a[0] - b[0] || a[1] - b[1]); - } - var d3_geom_voronoiEdges, d3_geom_voronoiCells, d3_geom_voronoiBeaches, d3_geom_voronoiBeachPool = [], d3_geom_voronoiFirstCircle, d3_geom_voronoiCircles, d3_geom_voronoiCirclePool = []; - function d3_geom_voronoiBeach() { - d3_geom_voronoiRedBlackNode(this); - this.edge = this.site = this.circle = null; - } - function d3_geom_voronoiCreateBeach(site) { - var beach = d3_geom_voronoiBeachPool.pop() || new d3_geom_voronoiBeach(); - beach.site = site; - return beach; - } - function d3_geom_voronoiDetachBeach(beach) { - d3_geom_voronoiDetachCircle(beach); - d3_geom_voronoiBeaches.remove(beach); - d3_geom_voronoiBeachPool.push(beach); - d3_geom_voronoiRedBlackNode(beach); - } - function d3_geom_voronoiRemoveBeach(beach) { - var circle = beach.circle, x = circle.x, y = circle.cy, vertex = { - x: x, - y: y - }, previous = beach.P, next = beach.N, disappearing = [ beach ]; - d3_geom_voronoiDetachBeach(beach); - var lArc = previous; - while (lArc.circle && abs(x - lArc.circle.x) < ε && abs(y - lArc.circle.cy) < ε) { - previous = lArc.P; - disappearing.unshift(lArc); - d3_geom_voronoiDetachBeach(lArc); - lArc = previous; - } - disappearing.unshift(lArc); - d3_geom_voronoiDetachCircle(lArc); - var rArc = next; - while (rArc.circle && abs(x - rArc.circle.x) < ε && abs(y - rArc.circle.cy) < ε) { - next = rArc.N; - disappearing.push(rArc); - d3_geom_voronoiDetachBeach(rArc); - rArc = next; - } - disappearing.push(rArc); - d3_geom_voronoiDetachCircle(rArc); - var nArcs = disappearing.length, iArc; - for (iArc = 1; iArc < nArcs; ++iArc) { - rArc = disappearing[iArc]; - lArc = disappearing[iArc - 1]; - d3_geom_voronoiSetEdgeEnd(rArc.edge, lArc.site, rArc.site, vertex); - } - lArc = disappearing[0]; - rArc = disappearing[nArcs - 1]; - rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, rArc.site, null, vertex); - d3_geom_voronoiAttachCircle(lArc); - d3_geom_voronoiAttachCircle(rArc); - } - function d3_geom_voronoiAddBeach(site) { - var x = site.x, directrix = site.y, lArc, rArc, dxl, dxr, node = d3_geom_voronoiBeaches._; - while (node) { - dxl = d3_geom_voronoiLeftBreakPoint(node, directrix) - x; - if (dxl > ε) node = node.L; else { - dxr = x - d3_geom_voronoiRightBreakPoint(node, directrix); - if (dxr > ε) { - if (!node.R) { - lArc = node; - break; - } - node = node.R; - } else { - if (dxl > -ε) { - lArc = node.P; - rArc = node; - } else if (dxr > -ε) { - lArc = node; - rArc = node.N; - } else { - lArc = rArc = node; - } - break; - } - } - } - var newArc = d3_geom_voronoiCreateBeach(site); - d3_geom_voronoiBeaches.insert(lArc, newArc); - if (!lArc && !rArc) return; - if (lArc === rArc) { - d3_geom_voronoiDetachCircle(lArc); - rArc = d3_geom_voronoiCreateBeach(lArc.site); - d3_geom_voronoiBeaches.insert(newArc, rArc); - newArc.edge = rArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); - d3_geom_voronoiAttachCircle(lArc); - d3_geom_voronoiAttachCircle(rArc); - return; - } - if (!rArc) { - newArc.edge = d3_geom_voronoiCreateEdge(lArc.site, newArc.site); - return; - } - d3_geom_voronoiDetachCircle(lArc); - d3_geom_voronoiDetachCircle(rArc); - var lSite = lArc.site, ax = lSite.x, ay = lSite.y, bx = site.x - ax, by = site.y - ay, rSite = rArc.site, cx = rSite.x - ax, cy = rSite.y - ay, d = 2 * (bx * cy - by * cx), hb = bx * bx + by * by, hc = cx * cx + cy * cy, vertex = { - x: (cy * hb - by * hc) / d + ax, - y: (bx * hc - cx * hb) / d + ay - }; - d3_geom_voronoiSetEdgeEnd(rArc.edge, lSite, rSite, vertex); - newArc.edge = d3_geom_voronoiCreateEdge(lSite, site, null, vertex); - rArc.edge = d3_geom_voronoiCreateEdge(site, rSite, null, vertex); - d3_geom_voronoiAttachCircle(lArc); - d3_geom_voronoiAttachCircle(rArc); - } - function d3_geom_voronoiLeftBreakPoint(arc, directrix) { - var site = arc.site, rfocx = site.x, rfocy = site.y, pby2 = rfocy - directrix; - if (!pby2) return rfocx; - var lArc = arc.P; - if (!lArc) return -Infinity; - site = lArc.site; - var lfocx = site.x, lfocy = site.y, plby2 = lfocy - directrix; - if (!plby2) return lfocx; - var hl = lfocx - rfocx, aby2 = 1 / pby2 - 1 / plby2, b = hl / plby2; - if (aby2) return (-b + Math.sqrt(b * b - 2 * aby2 * (hl * hl / (-2 * plby2) - lfocy + plby2 / 2 + rfocy - pby2 / 2))) / aby2 + rfocx; - return (rfocx + lfocx) / 2; - } - function d3_geom_voronoiRightBreakPoint(arc, directrix) { - var rArc = arc.N; - if (rArc) return d3_geom_voronoiLeftBreakPoint(rArc, directrix); - var site = arc.site; - return site.y === directrix ? site.x : Infinity; - } - function d3_geom_voronoiCell(site) { - this.site = site; - this.edges = []; - } - d3_geom_voronoiCell.prototype.prepare = function() { - var halfEdges = this.edges, iHalfEdge = halfEdges.length, edge; - while (iHalfEdge--) { - edge = halfEdges[iHalfEdge].edge; - if (!edge.b || !edge.a) halfEdges.splice(iHalfEdge, 1); - } - halfEdges.sort(d3_geom_voronoiHalfEdgeOrder); - return halfEdges.length; - }; - function d3_geom_voronoiCloseCells(extent) { - var x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], x2, y2, x3, y3, cells = d3_geom_voronoiCells, iCell = cells.length, cell, iHalfEdge, halfEdges, nHalfEdges, start, end; - while (iCell--) { - cell = cells[iCell]; - if (!cell || !cell.prepare()) continue; - halfEdges = cell.edges; - nHalfEdges = halfEdges.length; - iHalfEdge = 0; - while (iHalfEdge < nHalfEdges) { - end = halfEdges[iHalfEdge].end(), x3 = end.x, y3 = end.y; - start = halfEdges[++iHalfEdge % nHalfEdges].start(), x2 = start.x, y2 = start.y; - if (abs(x3 - x2) > ε || abs(y3 - y2) > ε) { - halfEdges.splice(iHalfEdge, 0, new d3_geom_voronoiHalfEdge(d3_geom_voronoiCreateBorderEdge(cell.site, end, abs(x3 - x0) < ε && y1 - y3 > ε ? { - x: x0, - y: abs(x2 - x0) < ε ? y2 : y1 - } : abs(y3 - y1) < ε && x1 - x3 > ε ? { - x: abs(y2 - y1) < ε ? x2 : x1, - y: y1 - } : abs(x3 - x1) < ε && y3 - y0 > ε ? { - x: x1, - y: abs(x2 - x1) < ε ? y2 : y0 - } : abs(y3 - y0) < ε && x3 - x0 > ε ? { - x: abs(y2 - y0) < ε ? x2 : x0, - y: y0 - } : null), cell.site, null)); - ++nHalfEdges; - } - } - } - } - function d3_geom_voronoiHalfEdgeOrder(a, b) { - return b.angle - a.angle; - } - function d3_geom_voronoiCircle() { - d3_geom_voronoiRedBlackNode(this); - this.x = this.y = this.arc = this.site = this.cy = null; - } - function d3_geom_voronoiAttachCircle(arc) { - var lArc = arc.P, rArc = arc.N; - if (!lArc || !rArc) return; - var lSite = lArc.site, cSite = arc.site, rSite = rArc.site; - if (lSite === rSite) return; - var bx = cSite.x, by = cSite.y, ax = lSite.x - bx, ay = lSite.y - by, cx = rSite.x - bx, cy = rSite.y - by; - var d = 2 * (ax * cy - ay * cx); - if (d >= -ε2) return; - var ha = ax * ax + ay * ay, hc = cx * cx + cy * cy, x = (cy * ha - ay * hc) / d, y = (ax * hc - cx * ha) / d, cy = y + by; - var circle = d3_geom_voronoiCirclePool.pop() || new d3_geom_voronoiCircle(); - circle.arc = arc; - circle.site = cSite; - circle.x = x + bx; - circle.y = cy + Math.sqrt(x * x + y * y); - circle.cy = cy; - arc.circle = circle; - var before = null, node = d3_geom_voronoiCircles._; - while (node) { - if (circle.y < node.y || circle.y === node.y && circle.x <= node.x) { - if (node.L) node = node.L; else { - before = node.P; - break; - } - } else { - if (node.R) node = node.R; else { - before = node; - break; - } - } - } - d3_geom_voronoiCircles.insert(before, circle); - if (!before) d3_geom_voronoiFirstCircle = circle; - } - function d3_geom_voronoiDetachCircle(arc) { - var circle = arc.circle; - if (circle) { - if (!circle.P) d3_geom_voronoiFirstCircle = circle.N; - d3_geom_voronoiCircles.remove(circle); - d3_geom_voronoiCirclePool.push(circle); - d3_geom_voronoiRedBlackNode(circle); - arc.circle = null; - } - } - function d3_geom_voronoiClipEdges(extent) { - var edges = d3_geom_voronoiEdges, clip = d3_geom_clipLine(extent[0][0], extent[0][1], extent[1][0], extent[1][1]), i = edges.length, e; - while (i--) { - e = edges[i]; - if (!d3_geom_voronoiConnectEdge(e, extent) || !clip(e) || abs(e.a.x - e.b.x) < ε && abs(e.a.y - e.b.y) < ε) { - e.a = e.b = null; - edges.splice(i, 1); - } - } - } - function d3_geom_voronoiConnectEdge(edge, extent) { - var vb = edge.b; - if (vb) return true; - var va = edge.a, x0 = extent[0][0], x1 = extent[1][0], y0 = extent[0][1], y1 = extent[1][1], lSite = edge.l, rSite = edge.r, lx = lSite.x, ly = lSite.y, rx = rSite.x, ry = rSite.y, fx = (lx + rx) / 2, fy = (ly + ry) / 2, fm, fb; - if (ry === ly) { - if (fx < x0 || fx >= x1) return; - if (lx > rx) { - if (!va) va = { - x: fx, - y: y0 - }; else if (va.y >= y1) return; - vb = { - x: fx, - y: y1 - }; - } else { - if (!va) va = { - x: fx, - y: y1 - }; else if (va.y < y0) return; - vb = { - x: fx, - y: y0 - }; - } - } else { - fm = (lx - rx) / (ry - ly); - fb = fy - fm * fx; - if (fm < -1 || fm > 1) { - if (lx > rx) { - if (!va) va = { - x: (y0 - fb) / fm, - y: y0 - }; else if (va.y >= y1) return; - vb = { - x: (y1 - fb) / fm, - y: y1 - }; - } else { - if (!va) va = { - x: (y1 - fb) / fm, - y: y1 - }; else if (va.y < y0) return; - vb = { - x: (y0 - fb) / fm, - y: y0 - }; - } - } else { - if (ly < ry) { - if (!va) va = { - x: x0, - y: fm * x0 + fb - }; else if (va.x >= x1) return; - vb = { - x: x1, - y: fm * x1 + fb - }; - } else { - if (!va) va = { - x: x1, - y: fm * x1 + fb - }; else if (va.x < x0) return; - vb = { - x: x0, - y: fm * x0 + fb - }; - } - } - } - edge.a = va; - edge.b = vb; - return true; - } - function d3_geom_voronoiEdge(lSite, rSite) { - this.l = lSite; - this.r = rSite; - this.a = this.b = null; - } - function d3_geom_voronoiCreateEdge(lSite, rSite, va, vb) { - var edge = new d3_geom_voronoiEdge(lSite, rSite); - d3_geom_voronoiEdges.push(edge); - if (va) d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, va); - if (vb) d3_geom_voronoiSetEdgeEnd(edge, rSite, lSite, vb); - d3_geom_voronoiCells[lSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, lSite, rSite)); - d3_geom_voronoiCells[rSite.i].edges.push(new d3_geom_voronoiHalfEdge(edge, rSite, lSite)); - return edge; - } - function d3_geom_voronoiCreateBorderEdge(lSite, va, vb) { - var edge = new d3_geom_voronoiEdge(lSite, null); - edge.a = va; - edge.b = vb; - d3_geom_voronoiEdges.push(edge); - return edge; - } - function d3_geom_voronoiSetEdgeEnd(edge, lSite, rSite, vertex) { - if (!edge.a && !edge.b) { - edge.a = vertex; - edge.l = lSite; - edge.r = rSite; - } else if (edge.l === rSite) { - edge.b = vertex; - } else { - edge.a = vertex; - } - } - function d3_geom_voronoiHalfEdge(edge, lSite, rSite) { - var va = edge.a, vb = edge.b; - this.edge = edge; - this.site = lSite; - this.angle = rSite ? Math.atan2(rSite.y - lSite.y, rSite.x - lSite.x) : edge.l === lSite ? Math.atan2(vb.x - va.x, va.y - vb.y) : Math.atan2(va.x - vb.x, vb.y - va.y); - } - d3_geom_voronoiHalfEdge.prototype = { - start: function() { - return this.edge.l === this.site ? this.edge.a : this.edge.b; - }, - end: function() { - return this.edge.l === this.site ? this.edge.b : this.edge.a; - } - }; - function d3_geom_voronoiRedBlackTree() { - this._ = null; - } - function d3_geom_voronoiRedBlackNode(node) { - node.U = node.C = node.L = node.R = node.P = node.N = null; - } - d3_geom_voronoiRedBlackTree.prototype = { - insert: function(after, node) { - var parent, grandpa, uncle; - if (after) { - node.P = after; - node.N = after.N; - if (after.N) after.N.P = node; - after.N = node; - if (after.R) { - after = after.R; - while (after.L) after = after.L; - after.L = node; - } else { - after.R = node; - } - parent = after; - } else if (this._) { - after = d3_geom_voronoiRedBlackFirst(this._); - node.P = null; - node.N = after; - after.P = after.L = node; - parent = after; - } else { - node.P = node.N = null; - this._ = node; - parent = null; - } - node.L = node.R = null; - node.U = parent; - node.C = true; - after = node; - while (parent && parent.C) { - grandpa = parent.U; - if (parent === grandpa.L) { - uncle = grandpa.R; - if (uncle && uncle.C) { - parent.C = uncle.C = false; - grandpa.C = true; - after = grandpa; - } else { - if (after === parent.R) { - d3_geom_voronoiRedBlackRotateLeft(this, parent); - after = parent; - parent = after.U; - } - parent.C = false; - grandpa.C = true; - d3_geom_voronoiRedBlackRotateRight(this, grandpa); - } - } else { - uncle = grandpa.L; - if (uncle && uncle.C) { - parent.C = uncle.C = false; - grandpa.C = true; - after = grandpa; - } else { - if (after === parent.L) { - d3_geom_voronoiRedBlackRotateRight(this, parent); - after = parent; - parent = after.U; - } - parent.C = false; - grandpa.C = true; - d3_geom_voronoiRedBlackRotateLeft(this, grandpa); - } - } - parent = after.U; - } - this._.C = false; - }, - remove: function(node) { - if (node.N) node.N.P = node.P; - if (node.P) node.P.N = node.N; - node.N = node.P = null; - var parent = node.U, sibling, left = node.L, right = node.R, next, red; - if (!left) next = right; else if (!right) next = left; else next = d3_geom_voronoiRedBlackFirst(right); - if (parent) { - if (parent.L === node) parent.L = next; else parent.R = next; - } else { - this._ = next; - } - if (left && right) { - red = next.C; - next.C = node.C; - next.L = left; - left.U = next; - if (next !== right) { - parent = next.U; - next.U = node.U; - node = next.R; - parent.L = node; - next.R = right; - right.U = next; - } else { - next.U = parent; - parent = next; - node = next.R; - } - } else { - red = node.C; - node = next; - } - if (node) node.U = parent; - if (red) return; - if (node && node.C) { - node.C = false; - return; - } - do { - if (node === this._) break; - if (node === parent.L) { - sibling = parent.R; - if (sibling.C) { - sibling.C = false; - parent.C = true; - d3_geom_voronoiRedBlackRotateLeft(this, parent); - sibling = parent.R; - } - if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { - if (!sibling.R || !sibling.R.C) { - sibling.L.C = false; - sibling.C = true; - d3_geom_voronoiRedBlackRotateRight(this, sibling); - sibling = parent.R; - } - sibling.C = parent.C; - parent.C = sibling.R.C = false; - d3_geom_voronoiRedBlackRotateLeft(this, parent); - node = this._; - break; - } - } else { - sibling = parent.L; - if (sibling.C) { - sibling.C = false; - parent.C = true; - d3_geom_voronoiRedBlackRotateRight(this, parent); - sibling = parent.L; - } - if (sibling.L && sibling.L.C || sibling.R && sibling.R.C) { - if (!sibling.L || !sibling.L.C) { - sibling.R.C = false; - sibling.C = true; - d3_geom_voronoiRedBlackRotateLeft(this, sibling); - sibling = parent.L; - } - sibling.C = parent.C; - parent.C = sibling.L.C = false; - d3_geom_voronoiRedBlackRotateRight(this, parent); - node = this._; - break; - } - } - sibling.C = true; - node = parent; - parent = parent.U; - } while (!node.C); - if (node) node.C = false; - } - }; - function d3_geom_voronoiRedBlackRotateLeft(tree, node) { - var p = node, q = node.R, parent = p.U; - if (parent) { - if (parent.L === p) parent.L = q; else parent.R = q; - } else { - tree._ = q; - } - q.U = parent; - p.U = q; - p.R = q.L; - if (p.R) p.R.U = p; - q.L = p; - } - function d3_geom_voronoiRedBlackRotateRight(tree, node) { - var p = node, q = node.L, parent = p.U; - if (parent) { - if (parent.L === p) parent.L = q; else parent.R = q; - } else { - tree._ = q; - } - q.U = parent; - p.U = q; - p.L = q.R; - if (p.L) p.L.U = p; - q.R = p; - } - function d3_geom_voronoiRedBlackFirst(node) { - while (node.L) node = node.L; - return node; - } - function d3_geom_voronoi(sites, bbox) { - var site = sites.sort(d3_geom_voronoiVertexOrder).pop(), x0, y0, circle; - d3_geom_voronoiEdges = []; - d3_geom_voronoiCells = new Array(sites.length); - d3_geom_voronoiBeaches = new d3_geom_voronoiRedBlackTree(); - d3_geom_voronoiCircles = new d3_geom_voronoiRedBlackTree(); while (true) { - circle = d3_geom_voronoiFirstCircle; - if (site && (!circle || site.y < circle.y || site.y === circle.y && site.x < circle.x)) { - if (site.x !== x0 || site.y !== y0) { - d3_geom_voronoiCells[site.i] = new d3_geom_voronoiCell(site); - d3_geom_voronoiAddBeach(site); - x0 = site.x, y0 = site.y; - } - site = sites.pop(); - } else if (circle) { - d3_geom_voronoiRemoveBeach(circle.arc); - } else { - break; - } + if (i >= parameters.stops.length) break; + else if (input <= parameters.stops[i][0]) break; + else i++; } - if (bbox) d3_geom_voronoiClipEdges(bbox), d3_geom_voronoiCloseCells(bbox); - var diagram = { - cells: d3_geom_voronoiCells, - edges: d3_geom_voronoiEdges - }; - d3_geom_voronoiBeaches = d3_geom_voronoiCircles = d3_geom_voronoiEdges = d3_geom_voronoiCells = null; - return diagram; - } - function d3_geom_voronoiVertexOrder(a, b) { - return b.y - a.y || b.x - a.x; - } - d3.geom.voronoi = function(points) { - var x = d3_geom_pointX, y = d3_geom_pointY, fx = x, fy = y, clipExtent = d3_geom_voronoiClipExtent; - if (points) return voronoi(points); - function voronoi(data) { - var polygons = new Array(data.length), x0 = clipExtent[0][0], y0 = clipExtent[0][1], x1 = clipExtent[1][0], y1 = clipExtent[1][1]; - d3_geom_voronoi(sites(data), clipExtent).cells.forEach(function(cell, i) { - var edges = cell.edges, site = cell.site, polygon = polygons[i] = edges.length ? edges.map(function(e) { - var s = e.start(); - return [ s.x, s.y ]; - }) : site.x >= x0 && site.x <= x1 && site.y >= y0 && site.y <= y1 ? [ [ x0, y1 ], [ x1, y1 ], [ x1, y0 ], [ x0, y0 ] ] : []; - polygon.point = data[i]; - }); - return polygons; - } - function sites(data) { - return data.map(function(d, i) { - return { - x: Math.round(fx(d, i) / ε) * ε, - y: Math.round(fy(d, i) / ε) * ε, - i: i - }; - }); - } - voronoi.links = function(data) { - return d3_geom_voronoi(sites(data)).edges.filter(function(edge) { - return edge.l && edge.r; - }).map(function(edge) { - return { - source: data[edge.l.i], - target: data[edge.r.i] - }; - }); - }; - voronoi.triangles = function(data) { - var triangles = []; - d3_geom_voronoi(sites(data)).cells.forEach(function(cell, i) { - var site = cell.site, edges = cell.edges.sort(d3_geom_voronoiHalfEdgeOrder), j = -1, m = edges.length, e0, s0, e1 = edges[m - 1].edge, s1 = e1.l === site ? e1.r : e1.l; - while (++j < m) { - e0 = e1; - s0 = s1; - e1 = edges[j].edge; - s1 = e1.l === site ? e1.r : e1.l; - if (i < s0.i && i < s1.i && d3_geom_voronoiTriangleArea(site, s0, s1) < 0) { - triangles.push([ data[i], data[s0.i], data[s1.i] ]); - } - } - }); - return triangles; - }; - voronoi.x = function(_) { - return arguments.length ? (fx = d3_functor(x = _), voronoi) : x; - }; - voronoi.y = function(_) { - return arguments.length ? (fy = d3_functor(y = _), voronoi) : y; - }; - voronoi.clipExtent = function(_) { - if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent; - clipExtent = _ == null ? d3_geom_voronoiClipExtent : _; - return voronoi; - }; - voronoi.size = function(_) { - if (!arguments.length) return clipExtent === d3_geom_voronoiClipExtent ? null : clipExtent && clipExtent[1]; - return voronoi.clipExtent(_ && [ [ 0, 0 ], _ ]); - }; - return voronoi; - }; - var d3_geom_voronoiClipExtent = [ [ -1e6, -1e6 ], [ 1e6, 1e6 ] ]; - function d3_geom_voronoiTriangleArea(a, b, c) { - return (a.x - c.x) * (b.y - a.y) - (a.x - b.x) * (c.y - a.y); - } - d3.geom.delaunay = function(vertices) { - return d3.geom.voronoi().triangles(vertices); - }; - d3.geom.quadtree = function(points, x1, y1, x2, y2) { - var x = d3_geom_pointX, y = d3_geom_pointY, compat; - if (compat = arguments.length) { - x = d3_geom_quadtreeCompatX; - y = d3_geom_quadtreeCompatY; - if (compat === 3) { - y2 = y1; - x2 = x1; - y1 = x1 = 0; - } - return quadtree(points); - } - function quadtree(data) { - var d, fx = d3_functor(x), fy = d3_functor(y), xs, ys, i, n, x1_, y1_, x2_, y2_; - if (x1 != null) { - x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2; - } else { - x2_ = y2_ = -(x1_ = y1_ = Infinity); - xs = [], ys = []; - n = data.length; - if (compat) for (i = 0; i < n; ++i) { - d = data[i]; - if (d.x < x1_) x1_ = d.x; - if (d.y < y1_) y1_ = d.y; - if (d.x > x2_) x2_ = d.x; - if (d.y > y2_) y2_ = d.y; - xs.push(d.x); - ys.push(d.y); - } else for (i = 0; i < n; ++i) { - var x_ = +fx(d = data[i], i), y_ = +fy(d, i); - if (x_ < x1_) x1_ = x_; - if (y_ < y1_) y1_ = y_; - if (x_ > x2_) x2_ = x_; - if (y_ > y2_) y2_ = y_; - xs.push(x_); - ys.push(y_); - } - } - var dx = x2_ - x1_, dy = y2_ - y1_; - if (dx > dy) y2_ = y1_ + dx; else x2_ = x1_ + dy; - function insert(n, d, x, y, x1, y1, x2, y2) { - if (isNaN(x) || isNaN(y)) return; - if (n.leaf) { - var nx = n.x, ny = n.y; - if (nx != null) { - if (abs(nx - x) + abs(ny - y) < .01) { - insertChild(n, d, x, y, x1, y1, x2, y2); - } else { - var nPoint = n.point; - n.x = n.y = n.point = null; - insertChild(n, nPoint, nx, ny, x1, y1, x2, y2); - insertChild(n, d, x, y, x1, y1, x2, y2); - } - } else { - n.x = x, n.y = y, n.point = d; - } - } else { - insertChild(n, d, x, y, x1, y1, x2, y2); - } - } - function insertChild(n, d, x, y, x1, y1, x2, y2) { - var xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym, i = below << 1 | right; - n.leaf = false; - n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode()); - if (right) x1 = xm; else x2 = xm; - if (below) y1 = ym; else y2 = ym; - insert(n, d, x, y, x1, y1, x2, y2); - } - var root = d3_geom_quadtreeNode(); - root.add = function(d) { - insert(root, d, +fx(d, ++i), +fy(d, i), x1_, y1_, x2_, y2_); - }; - root.visit = function(f) { - d3_geom_quadtreeVisit(f, root, x1_, y1_, x2_, y2_); - }; - root.find = function(point) { - return d3_geom_quadtreeFind(root, point[0], point[1], x1_, y1_, x2_, y2_); - }; - i = -1; - if (x1 == null) { - while (++i < n) { - insert(root, data[i], xs[i], ys[i], x1_, y1_, x2_, y2_); - } - --i; - } else data.forEach(root.add); - xs = ys = data = d = null; - return root; - } - quadtree.x = function(_) { - return arguments.length ? (x = _, quadtree) : x; - }; - quadtree.y = function(_) { - return arguments.length ? (y = _, quadtree) : y; - }; - quadtree.extent = function(_) { - if (!arguments.length) return x1 == null ? null : [ [ x1, y1 ], [ x2, y2 ] ]; - if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0], - y2 = +_[1][1]; - return quadtree; - }; - quadtree.size = function(_) { - if (!arguments.length) return x1 == null ? null : [ x2 - x1, y2 - y1 ]; - if (_ == null) x1 = y1 = x2 = y2 = null; else x1 = y1 = 0, x2 = +_[0], y2 = +_[1]; - return quadtree; - }; - return quadtree; - }; - function d3_geom_quadtreeCompatX(d) { - return d.x; - } - function d3_geom_quadtreeCompatY(d) { - return d.y; - } - function d3_geom_quadtreeNode() { - return { - leaf: true, - nodes: [], - point: null, - x: null, - y: null - }; - } - function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) { - if (!f(node, x1, y1, x2, y2)) { - var sx = (x1 + x2) * .5, sy = (y1 + y2) * .5, children = node.nodes; - if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy); - if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy); - if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2); - if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2); - } - } - function d3_geom_quadtreeFind(root, x, y, x0, y0, x3, y3) { - var minDistance2 = Infinity, closestPoint; - (function find(node, x1, y1, x2, y2) { - if (x1 > x3 || y1 > y3 || x2 < x0 || y2 < y0) return; - if (point = node.point) { - var point, dx = x - node.x, dy = y - node.y, distance2 = dx * dx + dy * dy; - if (distance2 < minDistance2) { - var distance = Math.sqrt(minDistance2 = distance2); - x0 = x - distance, y0 = y - distance; - x3 = x + distance, y3 = y + distance; - closestPoint = point; - } - } - var children = node.nodes, xm = (x1 + x2) * .5, ym = (y1 + y2) * .5, right = x >= xm, below = y >= ym; - for (var i = below << 1 | right, j = i + 4; i < j; ++i) { - if (node = children[i & 3]) switch (i & 3) { - case 0: - find(node, x1, y1, xm, ym); - break; - - case 1: - find(node, xm, y1, x2, ym); - break; - - case 2: - find(node, x1, ym, xm, y2); - break; - - case 3: - find(node, xm, ym, x2, y2); - break; - } - } - })(root, x0, y0, x3, y3); - return closestPoint; - } - d3.interpolateRgb = d3_interpolateRgb; - function d3_interpolateRgb(a, b) { - a = d3.rgb(a); - b = d3.rgb(b); - var ar = a.r, ag = a.g, ab = a.b, br = b.r - ar, bg = b.g - ag, bb = b.b - ab; - return function(t) { - return "#" + d3_rgb_hex(Math.round(ar + br * t)) + d3_rgb_hex(Math.round(ag + bg * t)) + d3_rgb_hex(Math.round(ab + bb * t)); - }; - } - d3.interpolateObject = d3_interpolateObject; - function d3_interpolateObject(a, b) { - var i = {}, c = {}, k; - for (k in a) { - if (k in b) { - i[k] = d3_interpolate(a[k], b[k]); - } else { - c[k] = a[k]; - } - } - for (k in b) { - if (!(k in a)) { - c[k] = b[k]; - } - } - return function(t) { - for (k in i) c[k] = i[k](t); - return c; - }; - } - d3.interpolateNumber = d3_interpolateNumber; - function d3_interpolateNumber(a, b) { - a = +a, b = +b; - return function(t) { - return a * (1 - t) + b * t; - }; - } - d3.interpolateString = d3_interpolateString; - function d3_interpolateString(a, b) { - var bi = d3_interpolate_numberA.lastIndex = d3_interpolate_numberB.lastIndex = 0, am, bm, bs, i = -1, s = [], q = []; - a = a + "", b = b + ""; - while ((am = d3_interpolate_numberA.exec(a)) && (bm = d3_interpolate_numberB.exec(b))) { - if ((bs = bm.index) > bi) { - bs = b.slice(bi, bs); - if (s[i]) s[i] += bs; else s[++i] = bs; - } - if ((am = am[0]) === (bm = bm[0])) { - if (s[i]) s[i] += bm; else s[++i] = bm; - } else { - s[++i] = null; - q.push({ - i: i, - x: d3_interpolateNumber(am, bm) - }); - } - bi = d3_interpolate_numberB.lastIndex; - } - if (bi < b.length) { - bs = b.slice(bi); - if (s[i]) s[i] += bs; else s[++i] = bs; - } - return s.length < 2 ? q[0] ? (b = q[0].x, function(t) { - return b(t) + ""; - }) : function() { - return b; - } : (b = q.length, function(t) { - for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t); - return s.join(""); - }); - } - var d3_interpolate_numberA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g, d3_interpolate_numberB = new RegExp(d3_interpolate_numberA.source, "g"); - d3.interpolate = d3_interpolate; - function d3_interpolate(a, b) { - var i = d3.interpolators.length, f; - while (--i >= 0 && !(f = d3.interpolators[i](a, b))) ; - return f; - } - d3.interpolators = [ function(a, b) { - var t = typeof b; - return (t === "string" ? d3_rgb_names.has(b.toLowerCase()) || /^(#|rgb\(|hsl\()/i.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_color ? d3_interpolateRgb : Array.isArray(b) ? d3_interpolateArray : t === "object" && isNaN(b) ? d3_interpolateObject : d3_interpolateNumber)(a, b); - } ]; - d3.interpolateArray = d3_interpolateArray; - function d3_interpolateArray(a, b) { - var x = [], c = [], na = a.length, nb = b.length, n0 = Math.min(a.length, b.length), i; - for (i = 0; i < n0; ++i) x.push(d3_interpolate(a[i], b[i])); - for (;i < na; ++i) c[i] = a[i]; - for (;i < nb; ++i) c[i] = b[i]; - return function(t) { - for (i = 0; i < n0; ++i) c[i] = x[i](t); - return c; - }; - } - var d3_ease_default = function() { - return d3_identity; - }; - var d3_ease = d3.map({ - linear: d3_ease_default, - poly: d3_ease_poly, - quad: function() { - return d3_ease_quad; - }, - cubic: function() { - return d3_ease_cubic; - }, - sin: function() { - return d3_ease_sin; - }, - exp: function() { - return d3_ease_exp; - }, - circle: function() { - return d3_ease_circle; - }, - elastic: d3_ease_elastic, - back: d3_ease_back, - bounce: function() { - return d3_ease_bounce; - } - }); - var d3_ease_mode = d3.map({ - "in": d3_identity, - out: d3_ease_reverse, - "in-out": d3_ease_reflect, - "out-in": function(f) { - return d3_ease_reflect(d3_ease_reverse(f)); - } - }); - d3.ease = function(name) { - var i = name.indexOf("-"), t = i >= 0 ? name.slice(0, i) : name, m = i >= 0 ? name.slice(i + 1) : "in"; - t = d3_ease.get(t) || d3_ease_default; - m = d3_ease_mode.get(m) || d3_identity; - return d3_ease_clamp(m(t.apply(null, d3_arraySlice.call(arguments, 1)))); - }; - function d3_ease_clamp(f) { - return function(t) { - return t <= 0 ? 0 : t >= 1 ? 1 : f(t); - }; - } - function d3_ease_reverse(f) { - return function(t) { - return 1 - f(1 - t); - }; - } - function d3_ease_reflect(f) { - return function(t) { - return .5 * (t < .5 ? f(2 * t) : 2 - f(2 - 2 * t)); - }; - } - function d3_ease_quad(t) { - return t * t; - } - function d3_ease_cubic(t) { - return t * t * t; - } - function d3_ease_cubicInOut(t) { - if (t <= 0) return 0; - if (t >= 1) return 1; - var t2 = t * t, t3 = t2 * t; - return 4 * (t < .5 ? t3 : 3 * (t - t2) + t3 - .75); - } - function d3_ease_poly(e) { - return function(t) { - return Math.pow(t, e); - }; - } - function d3_ease_sin(t) { - return 1 - Math.cos(t * halfπ); - } - function d3_ease_exp(t) { - return Math.pow(2, 10 * (t - 1)); - } - function d3_ease_circle(t) { - return 1 - Math.sqrt(1 - t * t); - } - function d3_ease_elastic(a, p) { - var s; - if (arguments.length < 2) p = .45; - if (arguments.length) s = p / τ * Math.asin(1 / a); else a = 1, s = p / 4; - return function(t) { - return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * τ / p); - }; - } - function d3_ease_back(s) { - if (!s) s = 1.70158; - return function(t) { - return t * t * ((s + 1) * t - s); - }; - } - function d3_ease_bounce(t) { - return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; - } - d3.interpolateHcl = d3_interpolateHcl; - function d3_interpolateHcl(a, b) { - a = d3.hcl(a); - b = d3.hcl(b); - var ah = a.h, ac = a.c, al = a.l, bh = b.h - ah, bc = b.c - ac, bl = b.l - al; - if (isNaN(bc)) bc = 0, ac = isNaN(ac) ? b.c : ac; - if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; - return function(t) { - return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + ""; - }; - } - d3.interpolateHsl = d3_interpolateHsl; - function d3_interpolateHsl(a, b) { - a = d3.hsl(a); - b = d3.hsl(b); - var ah = a.h, as = a.s, al = a.l, bh = b.h - ah, bs = b.s - as, bl = b.l - al; - if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as; - if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; - return function(t) { - return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + ""; - }; - } - d3.interpolateLab = d3_interpolateLab; - function d3_interpolateLab(a, b) { - a = d3.lab(a); - b = d3.lab(b); - var al = a.l, aa = a.a, ab = a.b, bl = b.l - al, ba = b.a - aa, bb = b.b - ab; - return function(t) { - return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + ""; - }; - } - d3.interpolateRound = d3_interpolateRound; - function d3_interpolateRound(a, b) { - b -= a; - return function(t) { - return Math.round(a + b * t); - }; - } - d3.transform = function(string) { - var g = d3_document.createElementNS(d3.ns.prefix.svg, "g"); - return (d3.transform = function(string) { - if (string != null) { - g.setAttribute("transform", string); - var t = g.transform.baseVal.consolidate(); - } - return new d3_transform(t ? t.matrix : d3_transformIdentity); - })(string); - }; - function d3_transform(m) { - var r0 = [ m.a, m.b ], r1 = [ m.c, m.d ], kx = d3_transformNormalize(r0), kz = d3_transformDot(r0, r1), ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0; - if (r0[0] * r1[1] < r1[0] * r0[1]) { - r0[0] *= -1; - r0[1] *= -1; - kx *= -1; - kz *= -1; - } - this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_degrees; - this.translate = [ m.e, m.f ]; - this.scale = [ kx, ky ]; - this.skew = ky ? Math.atan2(kz, ky) * d3_degrees : 0; - } - d3_transform.prototype.toString = function() { - return "translate(" + this.translate + ")rotate(" + this.rotate + ")skewX(" + this.skew + ")scale(" + this.scale + ")"; - }; - function d3_transformDot(a, b) { - return a[0] * b[0] + a[1] * b[1]; - } - function d3_transformNormalize(a) { - var k = Math.sqrt(d3_transformDot(a, a)); - if (k) { - a[0] /= k; - a[1] /= k; - } - return k; - } - function d3_transformCombine(a, b, k) { - a[0] += k * b[0]; - a[1] += k * b[1]; - return a; - } - var d3_transformIdentity = { - a: 1, - b: 0, - c: 0, - d: 1, - e: 0, - f: 0 - }; - d3.interpolateTransform = d3_interpolateTransform; - function d3_interpolateTransformPop(s) { - return s.length ? s.pop() + "," : ""; - } - function d3_interpolateTranslate(ta, tb, s, q) { - if (ta[0] !== tb[0] || ta[1] !== tb[1]) { - var i = s.push("translate(", null, ",", null, ")"); - q.push({ - i: i - 4, - x: d3_interpolateNumber(ta[0], tb[0]) - }, { - i: i - 2, - x: d3_interpolateNumber(ta[1], tb[1]) - }); - } else if (tb[0] || tb[1]) { - s.push("translate(" + tb + ")"); - } - } - function d3_interpolateRotate(ra, rb, s, q) { - if (ra !== rb) { - if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; - q.push({ - i: s.push(d3_interpolateTransformPop(s) + "rotate(", null, ")") - 2, - x: d3_interpolateNumber(ra, rb) - }); - } else if (rb) { - s.push(d3_interpolateTransformPop(s) + "rotate(" + rb + ")"); - } - } - function d3_interpolateSkew(wa, wb, s, q) { - if (wa !== wb) { - q.push({ - i: s.push(d3_interpolateTransformPop(s) + "skewX(", null, ")") - 2, - x: d3_interpolateNumber(wa, wb) - }); - } else if (wb) { - s.push(d3_interpolateTransformPop(s) + "skewX(" + wb + ")"); - } - } - function d3_interpolateScale(ka, kb, s, q) { - if (ka[0] !== kb[0] || ka[1] !== kb[1]) { - var i = s.push(d3_interpolateTransformPop(s) + "scale(", null, ",", null, ")"); - q.push({ - i: i - 4, - x: d3_interpolateNumber(ka[0], kb[0]) - }, { - i: i - 2, - x: d3_interpolateNumber(ka[1], kb[1]) - }); - } else if (kb[0] !== 1 || kb[1] !== 1) { - s.push(d3_interpolateTransformPop(s) + "scale(" + kb + ")"); - } - } - function d3_interpolateTransform(a, b) { - var s = [], q = []; - a = d3.transform(a), b = d3.transform(b); - d3_interpolateTranslate(a.translate, b.translate, s, q); - d3_interpolateRotate(a.rotate, b.rotate, s, q); - d3_interpolateSkew(a.skew, b.skew, s, q); - d3_interpolateScale(a.scale, b.scale, s, q); - a = b = null; - return function(t) { - var i = -1, n = q.length, o; - while (++i < n) s[(o = q[i]).i] = o.x(t); - return s.join(""); - }; - } - function d3_uninterpolateNumber(a, b) { - b = (b -= a = +a) || 1 / b; - return function(x) { - return (x - a) / b; - }; - } - function d3_uninterpolateClamp(a, b) { - b = (b -= a = +a) || 1 / b; - return function(x) { - return Math.max(0, Math.min(1, (x - a) / b)); - }; - } - d3.layout = {}; - d3.layout.bundle = function() { - return function(links) { - var paths = [], i = -1, n = links.length; - while (++i < n) paths.push(d3_layout_bundlePath(links[i])); - return paths; - }; - }; - function d3_layout_bundlePath(link) { - var start = link.source, end = link.target, lca = d3_layout_bundleLeastCommonAncestor(start, end), points = [ start ]; - while (start !== lca) { - start = start.parent; - points.push(start); - } - var k = points.length; - while (end !== lca) { - points.splice(k, 0, end); - end = end.parent; - } - return points; - } - function d3_layout_bundleAncestors(node) { - var ancestors = [], parent = node.parent; - while (parent != null) { - ancestors.push(node); - node = parent; - parent = parent.parent; - } - ancestors.push(node); - return ancestors; - } - function d3_layout_bundleLeastCommonAncestor(a, b) { - if (a === b) return a; - var aNodes = d3_layout_bundleAncestors(a), bNodes = d3_layout_bundleAncestors(b), aNode = aNodes.pop(), bNode = bNodes.pop(), sharedNode = null; - while (aNode === bNode) { - sharedNode = aNode; - aNode = aNodes.pop(); - bNode = bNodes.pop(); - } - return sharedNode; - } - d3.layout.chord = function() { - var chord = {}, chords, groups, matrix, n, padding = 0, sortGroups, sortSubgroups, sortChords; - function relayout() { - var subgroups = {}, groupSums = [], groupIndex = d3.range(n), subgroupIndex = [], k, x, x0, i, j; - chords = []; - groups = []; - k = 0, i = -1; - while (++i < n) { - x = 0, j = -1; - while (++j < n) { - x += matrix[i][j]; - } - groupSums.push(x); - subgroupIndex.push(d3.range(n)); - k += x; - } - if (sortGroups) { - groupIndex.sort(function(a, b) { - return sortGroups(groupSums[a], groupSums[b]); - }); - } - if (sortSubgroups) { - subgroupIndex.forEach(function(d, i) { - d.sort(function(a, b) { - return sortSubgroups(matrix[i][a], matrix[i][b]); - }); - }); - } - k = (τ - padding * n) / k; - x = 0, i = -1; - while (++i < n) { - x0 = x, j = -1; - while (++j < n) { - var di = groupIndex[i], dj = subgroupIndex[di][j], v = matrix[di][dj], a0 = x, a1 = x += v * k; - subgroups[di + "-" + dj] = { - index: di, - subindex: dj, - startAngle: a0, - endAngle: a1, - value: v - }; - } - groups[di] = { - index: di, - startAngle: x0, - endAngle: x, - value: groupSums[di] - }; - x += padding; - } - i = -1; - while (++i < n) { - j = i - 1; - while (++j < n) { - var source = subgroups[i + "-" + j], target = subgroups[j + "-" + i]; - if (source.value || target.value) { - chords.push(source.value < target.value ? { - source: target, - target: source - } : { - source: source, - target: target - }); - } - } - } - if (sortChords) resort(); - } - function resort() { - chords.sort(function(a, b) { - return sortChords((a.source.value + a.target.value) / 2, (b.source.value + b.target.value) / 2); - }); - } - chord.matrix = function(x) { - if (!arguments.length) return matrix; - n = (matrix = x) && matrix.length; - chords = groups = null; - return chord; - }; - chord.padding = function(x) { - if (!arguments.length) return padding; - padding = x; - chords = groups = null; - return chord; - }; - chord.sortGroups = function(x) { - if (!arguments.length) return sortGroups; - sortGroups = x; - chords = groups = null; - return chord; - }; - chord.sortSubgroups = function(x) { - if (!arguments.length) return sortSubgroups; - sortSubgroups = x; - chords = null; - return chord; - }; - chord.sortChords = function(x) { - if (!arguments.length) return sortChords; - sortChords = x; - if (chords) resort(); - return chord; - }; - chord.chords = function() { - if (!chords) relayout(); - return chords; - }; - chord.groups = function() { - if (!groups) relayout(); - return groups; - }; - return chord; - }; - d3.layout.force = function() { - var force = {}, event = d3.dispatch("start", "tick", "end"), timer, size = [ 1, 1 ], drag, alpha, friction = .9, linkDistance = d3_layout_forceLinkDistance, linkStrength = d3_layout_forceLinkStrength, charge = -30, chargeDistance2 = d3_layout_forceChargeDistance2, gravity = .1, theta2 = .64, nodes = [], links = [], distances, strengths, charges; - function repulse(node) { - return function(quad, x1, _, x2) { - if (quad.point !== node) { - var dx = quad.cx - node.x, dy = quad.cy - node.y, dw = x2 - x1, dn = dx * dx + dy * dy; - if (dw * dw / theta2 < dn) { - if (dn < chargeDistance2) { - var k = quad.charge / dn; - node.px -= dx * k; - node.py -= dy * k; - } - return true; - } - if (quad.point && dn && dn < chargeDistance2) { - var k = quad.pointCharge / dn; - node.px -= dx * k; - node.py -= dy * k; - } - } - return !quad.charge; - }; - } - force.tick = function() { - if ((alpha *= .99) < .005) { - timer = null; - event.end({ - type: "end", - alpha: alpha = 0 - }); - return true; - } - var n = nodes.length, m = links.length, q, i, o, s, t, l, k, x, y; - for (i = 0; i < m; ++i) { - o = links[i]; - s = o.source; - t = o.target; - x = t.x - s.x; - y = t.y - s.y; - if (l = x * x + y * y) { - l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l; - x *= l; - y *= l; - t.x -= x * (k = s.weight + t.weight ? s.weight / (s.weight + t.weight) : .5); - t.y -= y * k; - s.x += x * (k = 1 - k); - s.y += y * k; - } - } - if (k = alpha * gravity) { - x = size[0] / 2; - y = size[1] / 2; - i = -1; - if (k) while (++i < n) { - o = nodes[i]; - o.x += (x - o.x) * k; - o.y += (y - o.y) * k; - } - } - if (charge) { - d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges); - i = -1; - while (++i < n) { - if (!(o = nodes[i]).fixed) { - q.visit(repulse(o)); - } - } - } - i = -1; - while (++i < n) { - o = nodes[i]; - if (o.fixed) { - o.x = o.px; - o.y = o.py; - } else { - o.x -= (o.px - (o.px = o.x)) * friction; - o.y -= (o.py - (o.py = o.y)) * friction; - } - } - event.tick({ - type: "tick", - alpha: alpha - }); - }; - force.nodes = function(x) { - if (!arguments.length) return nodes; - nodes = x; - return force; - }; - force.links = function(x) { - if (!arguments.length) return links; - links = x; - return force; - }; - force.size = function(x) { - if (!arguments.length) return size; - size = x; - return force; - }; - force.linkDistance = function(x) { - if (!arguments.length) return linkDistance; - linkDistance = typeof x === "function" ? x : +x; - return force; - }; - force.distance = force.linkDistance; - force.linkStrength = function(x) { - if (!arguments.length) return linkStrength; - linkStrength = typeof x === "function" ? x : +x; - return force; - }; - force.friction = function(x) { - if (!arguments.length) return friction; - friction = +x; - return force; - }; - force.charge = function(x) { - if (!arguments.length) return charge; - charge = typeof x === "function" ? x : +x; - return force; - }; - force.chargeDistance = function(x) { - if (!arguments.length) return Math.sqrt(chargeDistance2); - chargeDistance2 = x * x; - return force; - }; - force.gravity = function(x) { - if (!arguments.length) return gravity; - gravity = +x; - return force; - }; - force.theta = function(x) { - if (!arguments.length) return Math.sqrt(theta2); - theta2 = x * x; - return force; - }; - force.alpha = function(x) { - if (!arguments.length) return alpha; - x = +x; - if (alpha) { - if (x > 0) { - alpha = x; - } else { - timer.c = null, timer.t = NaN, timer = null; - event.end({ - type: "end", - alpha: alpha = 0 - }); - } - } else if (x > 0) { - event.start({ - type: "start", - alpha: alpha = x - }); - timer = d3_timer(force.tick); - } - return force; - }; - force.start = function() { - var i, n = nodes.length, m = links.length, w = size[0], h = size[1], neighbors, o; - for (i = 0; i < n; ++i) { - (o = nodes[i]).index = i; - o.weight = 0; - } - for (i = 0; i < m; ++i) { - o = links[i]; - if (typeof o.source == "number") o.source = nodes[o.source]; - if (typeof o.target == "number") o.target = nodes[o.target]; - ++o.source.weight; - ++o.target.weight; - } - for (i = 0; i < n; ++i) { - o = nodes[i]; - if (isNaN(o.x)) o.x = position("x", w); - if (isNaN(o.y)) o.y = position("y", h); - if (isNaN(o.px)) o.px = o.x; - if (isNaN(o.py)) o.py = o.y; - } - distances = []; - if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i); else for (i = 0; i < m; ++i) distances[i] = linkDistance; - strengths = []; - if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i); else for (i = 0; i < m; ++i) strengths[i] = linkStrength; - charges = []; - if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge; - function position(dimension, size) { - if (!neighbors) { - neighbors = new Array(n); - for (j = 0; j < n; ++j) { - neighbors[j] = []; - } - for (j = 0; j < m; ++j) { - var o = links[j]; - neighbors[o.source.index].push(o.target); - neighbors[o.target.index].push(o.source); - } - } - var candidates = neighbors[i], j = -1, l = candidates.length, x; - while (++j < l) if (!isNaN(x = candidates[j][dimension])) return x; - return Math.random() * size; - } - return force.resume(); - }; - force.resume = function() { - return force.alpha(.1); - }; - force.stop = function() { - return force.alpha(0); - }; - force.drag = function() { - if (!drag) drag = d3.behavior.drag().origin(d3_identity).on("dragstart.force", d3_layout_forceDragstart).on("drag.force", dragmove).on("dragend.force", d3_layout_forceDragend); - if (!arguments.length) return drag; - this.on("mouseover.force", d3_layout_forceMouseover).on("mouseout.force", d3_layout_forceMouseout).call(drag); - }; - function dragmove(d) { - d.px = d3.event.x, d.py = d3.event.y; - force.resume(); - } - return d3.rebind(force, event, "on"); - }; - function d3_layout_forceDragstart(d) { - d.fixed |= 2; - } - function d3_layout_forceDragend(d) { - d.fixed &= ~6; - } - function d3_layout_forceMouseover(d) { - d.fixed |= 4; - d.px = d.x, d.py = d.y; - } - function d3_layout_forceMouseout(d) { - d.fixed &= ~4; - } - function d3_layout_forceAccumulate(quad, alpha, charges) { - var cx = 0, cy = 0; - quad.charge = 0; - if (!quad.leaf) { - var nodes = quad.nodes, n = nodes.length, i = -1, c; - while (++i < n) { - c = nodes[i]; - if (c == null) continue; - d3_layout_forceAccumulate(c, alpha, charges); - quad.charge += c.charge; - cx += c.charge * c.cx; - cy += c.charge * c.cy; - } - } - if (quad.point) { - if (!quad.leaf) { - quad.point.x += Math.random() - .5; - quad.point.y += Math.random() - .5; - } - var k = alpha * charges[quad.point.index]; - quad.charge += quad.pointCharge = k; - cx += k * quad.point.x; - cy += k * quad.point.y; - } - quad.cx = cx / quad.charge; - quad.cy = cy / quad.charge; - } - var d3_layout_forceLinkDistance = 20, d3_layout_forceLinkStrength = 1, d3_layout_forceChargeDistance2 = Infinity; - d3.layout.hierarchy = function() { - var sort = d3_layout_hierarchySort, children = d3_layout_hierarchyChildren, value = d3_layout_hierarchyValue; - function hierarchy(root) { - var stack = [ root ], nodes = [], node; - root.depth = 0; - while ((node = stack.pop()) != null) { - nodes.push(node); - if ((childs = children.call(hierarchy, node, node.depth)) && (n = childs.length)) { - var n, childs, child; - while (--n >= 0) { - stack.push(child = childs[n]); - child.parent = node; - child.depth = node.depth + 1; - } - if (value) node.value = 0; - node.children = childs; - } else { - if (value) node.value = +value.call(hierarchy, node, node.depth) || 0; - delete node.children; - } - } - d3_layout_hierarchyVisitAfter(root, function(node) { - var childs, parent; - if (sort && (childs = node.children)) childs.sort(sort); - if (value && (parent = node.parent)) parent.value += node.value; - }); - return nodes; - } - hierarchy.sort = function(x) { - if (!arguments.length) return sort; - sort = x; - return hierarchy; - }; - hierarchy.children = function(x) { - if (!arguments.length) return children; - children = x; - return hierarchy; - }; - hierarchy.value = function(x) { - if (!arguments.length) return value; - value = x; - return hierarchy; - }; - hierarchy.revalue = function(root) { - if (value) { - d3_layout_hierarchyVisitBefore(root, function(node) { - if (node.children) node.value = 0; - }); - d3_layout_hierarchyVisitAfter(root, function(node) { - var parent; - if (!node.children) node.value = +value.call(hierarchy, node, node.depth) || 0; - if (parent = node.parent) parent.value += node.value; - }); - } - return root; - }; - return hierarchy; - }; - function d3_layout_hierarchyRebind(object, hierarchy) { - d3.rebind(object, hierarchy, "sort", "children", "value"); - object.nodes = object; - object.links = d3_layout_hierarchyLinks; - return object; - } - function d3_layout_hierarchyVisitBefore(node, callback) { - var nodes = [ node ]; - while ((node = nodes.pop()) != null) { - callback(node); - if ((children = node.children) && (n = children.length)) { - var n, children; - while (--n >= 0) nodes.push(children[n]); - } - } - } - function d3_layout_hierarchyVisitAfter(node, callback) { - var nodes = [ node ], nodes2 = []; - while ((node = nodes.pop()) != null) { - nodes2.push(node); - if ((children = node.children) && (n = children.length)) { - var i = -1, n, children; - while (++i < n) nodes.push(children[i]); - } - } - while ((node = nodes2.pop()) != null) { - callback(node); - } - } - function d3_layout_hierarchyChildren(d) { - return d.children; - } - function d3_layout_hierarchyValue(d) { - return d.value; - } - function d3_layout_hierarchySort(a, b) { - return b.value - a.value; - } - function d3_layout_hierarchyLinks(nodes) { - return d3.merge(nodes.map(function(parent) { - return (parent.children || []).map(function(child) { - return { - source: parent, - target: child - }; - }); - })); - } - d3.layout.partition = function() { - var hierarchy = d3.layout.hierarchy(), size = [ 1, 1 ]; - function position(node, x, dx, dy) { - var children = node.children; - node.x = x; - node.y = node.depth * dy; - node.dx = dx; - node.dy = dy; - if (children && (n = children.length)) { - var i = -1, n, c, d; - dx = node.value ? dx / node.value : 0; - while (++i < n) { - position(c = children[i], x, d = c.value * dx, dy); - x += d; - } - } - } - function depth(node) { - var children = node.children, d = 0; - if (children && (n = children.length)) { - var i = -1, n; - while (++i < n) d = Math.max(d, depth(children[i])); - } - return 1 + d; - } - function partition(d, i) { - var nodes = hierarchy.call(this, d, i); - position(nodes[0], 0, size[0], size[1] / depth(nodes[0])); - return nodes; - } - partition.size = function(x) { - if (!arguments.length) return size; - size = x; - return partition; - }; - return d3_layout_hierarchyRebind(partition, hierarchy); - }; - d3.layout.pie = function() { - var value = Number, sort = d3_layout_pieSortByValue, startAngle = 0, endAngle = τ, padAngle = 0; - function pie(data) { - var n = data.length, values = data.map(function(d, i) { - return +value.call(pie, d, i); - }), a = +(typeof startAngle === "function" ? startAngle.apply(this, arguments) : startAngle), da = (typeof endAngle === "function" ? endAngle.apply(this, arguments) : endAngle) - a, p = Math.min(Math.abs(da) / n, +(typeof padAngle === "function" ? padAngle.apply(this, arguments) : padAngle)), pa = p * (da < 0 ? -1 : 1), sum = d3.sum(values), k = sum ? (da - n * pa) / sum : 0, index = d3.range(n), arcs = [], v; - if (sort != null) index.sort(sort === d3_layout_pieSortByValue ? function(i, j) { - return values[j] - values[i]; - } : function(i, j) { - return sort(data[i], data[j]); - }); - index.forEach(function(i) { - arcs[i] = { - data: data[i], - value: v = values[i], - startAngle: a, - endAngle: a += v * k + pa, - padAngle: p - }; - }); - return arcs; - } - pie.value = function(_) { - if (!arguments.length) return value; - value = _; - return pie; - }; - pie.sort = function(_) { - if (!arguments.length) return sort; - sort = _; - return pie; - }; - pie.startAngle = function(_) { - if (!arguments.length) return startAngle; - startAngle = _; - return pie; - }; - pie.endAngle = function(_) { - if (!arguments.length) return endAngle; - endAngle = _; - return pie; - }; - pie.padAngle = function(_) { - if (!arguments.length) return padAngle; - padAngle = _; - return pie; - }; - return pie; - }; - var d3_layout_pieSortByValue = {}; - d3.layout.stack = function() { - var values = d3_identity, order = d3_layout_stackOrderDefault, offset = d3_layout_stackOffsetZero, out = d3_layout_stackOut, x = d3_layout_stackX, y = d3_layout_stackY; - function stack(data, index) { - if (!(n = data.length)) return data; - var series = data.map(function(d, i) { - return values.call(stack, d, i); - }); - var points = series.map(function(d) { - return d.map(function(v, i) { - return [ x.call(stack, v, i), y.call(stack, v, i) ]; - }); - }); - var orders = order.call(stack, points, index); - series = d3.permute(series, orders); - points = d3.permute(points, orders); - var offsets = offset.call(stack, points, index); - var m = series[0].length, n, i, j, o; - for (j = 0; j < m; ++j) { - out.call(stack, series[0][j], o = offsets[j], points[0][j][1]); - for (i = 1; i < n; ++i) { - out.call(stack, series[i][j], o += points[i - 1][j][1], points[i][j][1]); - } - } - return data; - } - stack.values = function(x) { - if (!arguments.length) return values; - values = x; - return stack; - }; - stack.order = function(x) { - if (!arguments.length) return order; - order = typeof x === "function" ? x : d3_layout_stackOrders.get(x) || d3_layout_stackOrderDefault; - return stack; - }; - stack.offset = function(x) { - if (!arguments.length) return offset; - offset = typeof x === "function" ? x : d3_layout_stackOffsets.get(x) || d3_layout_stackOffsetZero; - return stack; - }; - stack.x = function(z) { - if (!arguments.length) return x; - x = z; - return stack; - }; - stack.y = function(z) { - if (!arguments.length) return y; - y = z; - return stack; - }; - stack.out = function(z) { - if (!arguments.length) return out; - out = z; - return stack; - }; - return stack; - }; - function d3_layout_stackX(d) { - return d.x; - } - function d3_layout_stackY(d) { - return d.y; - } - function d3_layout_stackOut(d, y0, y) { - d.y0 = y0; - d.y = y; - } - var d3_layout_stackOrders = d3.map({ - "inside-out": function(data) { - var n = data.length, i, j, max = data.map(d3_layout_stackMaxIndex), sums = data.map(d3_layout_stackReduceSum), index = d3.range(n).sort(function(a, b) { - return max[a] - max[b]; - }), top = 0, bottom = 0, tops = [], bottoms = []; - for (i = 0; i < n; ++i) { - j = index[i]; - if (top < bottom) { - top += sums[j]; - tops.push(j); - } else { - bottom += sums[j]; - bottoms.push(j); - } - } - return bottoms.reverse().concat(tops); - }, - reverse: function(data) { - return d3.range(data.length).reverse(); - }, - "default": d3_layout_stackOrderDefault - }); - var d3_layout_stackOffsets = d3.map({ - silhouette: function(data) { - var n = data.length, m = data[0].length, sums = [], max = 0, i, j, o, y0 = []; - for (j = 0; j < m; ++j) { - for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; - if (o > max) max = o; - sums.push(o); - } - for (j = 0; j < m; ++j) { - y0[j] = (max - sums[j]) / 2; - } - return y0; - }, - wiggle: function(data) { - var n = data.length, x = data[0], m = x.length, i, j, k, s1, s2, s3, dx, o, o0, y0 = []; - y0[0] = o = o0 = 0; - for (j = 1; j < m; ++j) { - for (i = 0, s1 = 0; i < n; ++i) s1 += data[i][j][1]; - for (i = 0, s2 = 0, dx = x[j][0] - x[j - 1][0]; i < n; ++i) { - for (k = 0, s3 = (data[i][j][1] - data[i][j - 1][1]) / (2 * dx); k < i; ++k) { - s3 += (data[k][j][1] - data[k][j - 1][1]) / dx; - } - s2 += s3 * data[i][j][1]; - } - y0[j] = o -= s1 ? s2 / s1 * dx : 0; - if (o < o0) o0 = o; - } - for (j = 0; j < m; ++j) y0[j] -= o0; - return y0; - }, - expand: function(data) { - var n = data.length, m = data[0].length, k = 1 / n, i, j, o, y0 = []; - for (j = 0; j < m; ++j) { - for (i = 0, o = 0; i < n; i++) o += data[i][j][1]; - if (o) for (i = 0; i < n; i++) data[i][j][1] /= o; else for (i = 0; i < n; i++) data[i][j][1] = k; - } - for (j = 0; j < m; ++j) y0[j] = 0; - return y0; - }, - zero: d3_layout_stackOffsetZero - }); - function d3_layout_stackOrderDefault(data) { - return d3.range(data.length); - } - function d3_layout_stackOffsetZero(data) { - var j = -1, m = data[0].length, y0 = []; - while (++j < m) y0[j] = 0; - return y0; - } - function d3_layout_stackMaxIndex(array) { - var i = 1, j = 0, v = array[0][1], k, n = array.length; - for (;i < n; ++i) { - if ((k = array[i][1]) > v) { - j = i; - v = k; - } - } - return j; - } - function d3_layout_stackReduceSum(d) { - return d.reduce(d3_layout_stackSum, 0); - } - function d3_layout_stackSum(p, d) { - return p + d[1]; - } - d3.layout.histogram = function() { - var frequency = true, valuer = Number, ranger = d3_layout_histogramRange, binner = d3_layout_histogramBinSturges; - function histogram(data, i) { - var bins = [], values = data.map(valuer, this), range = ranger.call(this, values, i), thresholds = binner.call(this, range, values, i), bin, i = -1, n = values.length, m = thresholds.length - 1, k = frequency ? 1 : 1 / n, x; - while (++i < m) { - bin = bins[i] = []; - bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]); - bin.y = 0; - } - if (m > 0) { - i = -1; - while (++i < n) { - x = values[i]; - if (x >= range[0] && x <= range[1]) { - bin = bins[d3.bisect(thresholds, x, 1, m) - 1]; - bin.y += k; - bin.push(data[i]); - } - } - } - return bins; - } - histogram.value = function(x) { - if (!arguments.length) return valuer; - valuer = x; - return histogram; - }; - histogram.range = function(x) { - if (!arguments.length) return ranger; - ranger = d3_functor(x); - return histogram; - }; - histogram.bins = function(x) { - if (!arguments.length) return binner; - binner = typeof x === "number" ? function(range) { - return d3_layout_histogramBinFixed(range, x); - } : d3_functor(x); - return histogram; - }; - histogram.frequency = function(x) { - if (!arguments.length) return frequency; - frequency = !!x; - return histogram; - }; - return histogram; - }; - function d3_layout_histogramBinSturges(range, values) { - return d3_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1)); - } - function d3_layout_histogramBinFixed(range, n) { - var x = -1, b = +range[0], m = (range[1] - b) / n, f = []; - while (++x <= n) f[x] = m * x + b; - return f; - } - function d3_layout_histogramRange(values) { - return [ d3.min(values), d3.max(values) ]; - } - d3.layout.pack = function() { - var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius; - function pack(d, i) { - var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() { - return radius; - }; - root.x = root.y = 0; - d3_layout_hierarchyVisitAfter(root, function(d) { - d.r = +r(d.value); - }); - d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings); - if (padding) { - var dr = padding * (radius ? 1 : Math.max(2 * root.r / w, 2 * root.r / h)) / 2; - d3_layout_hierarchyVisitAfter(root, function(d) { - d.r += dr; - }); - d3_layout_hierarchyVisitAfter(root, d3_layout_packSiblings); - d3_layout_hierarchyVisitAfter(root, function(d) { - d.r -= dr; - }); - } - d3_layout_packTransform(root, w / 2, h / 2, radius ? 1 : 1 / Math.max(2 * root.r / w, 2 * root.r / h)); - return nodes; - } - pack.size = function(_) { - if (!arguments.length) return size; - size = _; - return pack; - }; - pack.radius = function(_) { - if (!arguments.length) return radius; - radius = _ == null || typeof _ === "function" ? _ : +_; - return pack; - }; - pack.padding = function(_) { - if (!arguments.length) return padding; - padding = +_; - return pack; - }; - return d3_layout_hierarchyRebind(pack, hierarchy); - }; - function d3_layout_packSort(a, b) { - return a.value - b.value; - } - function d3_layout_packInsert(a, b) { - var c = a._pack_next; - a._pack_next = b; - b._pack_prev = a; - b._pack_next = c; - c._pack_prev = b; - } - function d3_layout_packSplice(a, b) { - a._pack_next = b; - b._pack_prev = a; - } - function d3_layout_packIntersects(a, b) { - var dx = b.x - a.x, dy = b.y - a.y, dr = a.r + b.r; - return .999 * dr * dr > dx * dx + dy * dy; - } - function d3_layout_packSiblings(node) { - if (!(nodes = node.children) || !(n = nodes.length)) return; - var nodes, xMin = Infinity, xMax = -Infinity, yMin = Infinity, yMax = -Infinity, a, b, c, i, j, k, n; - function bound(node) { - xMin = Math.min(node.x - node.r, xMin); - xMax = Math.max(node.x + node.r, xMax); - yMin = Math.min(node.y - node.r, yMin); - yMax = Math.max(node.y + node.r, yMax); - } - nodes.forEach(d3_layout_packLink); - a = nodes[0]; - a.x = -a.r; - a.y = 0; - bound(a); - if (n > 1) { - b = nodes[1]; - b.x = b.r; - b.y = 0; - bound(b); - if (n > 2) { - c = nodes[2]; - d3_layout_packPlace(a, b, c); - bound(c); - d3_layout_packInsert(a, c); - a._pack_prev = c; - d3_layout_packInsert(c, b); - b = a._pack_next; - for (i = 3; i < n; i++) { - d3_layout_packPlace(a, b, c = nodes[i]); - var isect = 0, s1 = 1, s2 = 1; - for (j = b._pack_next; j !== b; j = j._pack_next, s1++) { - if (d3_layout_packIntersects(j, c)) { - isect = 1; - break; - } - } - if (isect == 1) { - for (k = a._pack_prev; k !== j._pack_prev; k = k._pack_prev, s2++) { - if (d3_layout_packIntersects(k, c)) { - break; - } - } - } - if (isect) { - if (s1 < s2 || s1 == s2 && b.r < a.r) d3_layout_packSplice(a, b = j); else d3_layout_packSplice(a = k, b); - i--; - } else { - d3_layout_packInsert(a, c); - b = c; - bound(c); - } - } - } - } - var cx = (xMin + xMax) / 2, cy = (yMin + yMax) / 2, cr = 0; - for (i = 0; i < n; i++) { - c = nodes[i]; - c.x -= cx; - c.y -= cy; - cr = Math.max(cr, c.r + Math.sqrt(c.x * c.x + c.y * c.y)); - } - node.r = cr; - nodes.forEach(d3_layout_packUnlink); - } - function d3_layout_packLink(node) { - node._pack_next = node._pack_prev = node; - } - function d3_layout_packUnlink(node) { - delete node._pack_next; - delete node._pack_prev; - } - function d3_layout_packTransform(node, x, y, k) { - var children = node.children; - node.x = x += k * node.x; - node.y = y += k * node.y; - node.r *= k; - if (children) { - var i = -1, n = children.length; - while (++i < n) d3_layout_packTransform(children[i], x, y, k); - } - } - function d3_layout_packPlace(a, b, c) { - var db = a.r + c.r, dx = b.x - a.x, dy = b.y - a.y; - if (db && (dx || dy)) { - var da = b.r + c.r, dc = dx * dx + dy * dy; - da *= da; - db *= db; - var x = .5 + (db - da) / (2 * dc), y = Math.sqrt(Math.max(0, 2 * da * (db + dc) - (db -= dc) * db - da * da)) / (2 * dc); - c.x = a.x + x * dx + y * dy; - c.y = a.y + x * dy - y * dx; - } else { - c.x = a.x + db; - c.y = a.y; - } - } - d3.layout.tree = function() { - var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = null; - function tree(d, i) { - var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0); - d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z; - d3_layout_hierarchyVisitBefore(root1, secondWalk); - if (nodeSize) d3_layout_hierarchyVisitBefore(root0, sizeNode); else { - var left = root0, right = root0, bottom = root0; - d3_layout_hierarchyVisitBefore(root0, function(node) { - if (node.x < left.x) left = node; - if (node.x > right.x) right = node; - if (node.depth > bottom.depth) bottom = node; - }); - var tx = separation(left, right) / 2 - left.x, kx = size[0] / (right.x + separation(right, left) / 2 + tx), ky = size[1] / (bottom.depth || 1); - d3_layout_hierarchyVisitBefore(root0, function(node) { - node.x = (node.x + tx) * kx; - node.y = node.depth * ky; - }); - } - return nodes; - } - function wrapTree(root0) { - var root1 = { - A: null, - children: [ root0 ] - }, queue = [ root1 ], node1; - while ((node1 = queue.pop()) != null) { - for (var children = node1.children, child, i = 0, n = children.length; i < n; ++i) { - queue.push((children[i] = child = { - _: children[i], - parent: node1, - children: (child = children[i].children) && child.slice() || [], - A: null, - a: null, - z: 0, - m: 0, - c: 0, - s: 0, - t: null, - i: i - }).a = child); - } - } - return root1.children[0]; - } - function firstWalk(v) { - var children = v.children, siblings = v.parent.children, w = v.i ? siblings[v.i - 1] : null; - if (children.length) { - d3_layout_treeShift(v); - var midpoint = (children[0].z + children[children.length - 1].z) / 2; - if (w) { - v.z = w.z + separation(v._, w._); - v.m = v.z - midpoint; - } else { - v.z = midpoint; - } - } else if (w) { - v.z = w.z + separation(v._, w._); - } - v.parent.A = apportion(v, w, v.parent.A || siblings[0]); - } - function secondWalk(v) { - v._.x = v.z + v.parent.m; - v.m += v.parent.m; - } - function apportion(v, w, ancestor) { - if (w) { - var vip = v, vop = v, vim = w, vom = vip.parent.children[0], sip = vip.m, sop = vop.m, sim = vim.m, som = vom.m, shift; - while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) { - vom = d3_layout_treeLeft(vom); - vop = d3_layout_treeRight(vop); - vop.a = v; - shift = vim.z + sim - vip.z - sip + separation(vim._, vip._); - if (shift > 0) { - d3_layout_treeMove(d3_layout_treeAncestor(vim, v, ancestor), v, shift); - sip += shift; - sop += shift; - } - sim += vim.m; - sip += vip.m; - som += vom.m; - sop += vop.m; - } - if (vim && !d3_layout_treeRight(vop)) { - vop.t = vim; - vop.m += sim - sop; - } - if (vip && !d3_layout_treeLeft(vom)) { - vom.t = vip; - vom.m += sip - som; - ancestor = v; - } - } - return ancestor; - } - function sizeNode(node) { - node.x *= size[0]; - node.y = node.depth * size[1]; - } - tree.separation = function(x) { - if (!arguments.length) return separation; - separation = x; - return tree; - }; - tree.size = function(x) { - if (!arguments.length) return nodeSize ? null : size; - nodeSize = (size = x) == null ? sizeNode : null; - return tree; - }; - tree.nodeSize = function(x) { - if (!arguments.length) return nodeSize ? size : null; - nodeSize = (size = x) == null ? null : sizeNode; - return tree; - }; - return d3_layout_hierarchyRebind(tree, hierarchy); - }; - function d3_layout_treeSeparation(a, b) { - return a.parent == b.parent ? 1 : 2; - } - function d3_layout_treeLeft(v) { - var children = v.children; - return children.length ? children[0] : v.t; - } - function d3_layout_treeRight(v) { - var children = v.children, n; - return (n = children.length) ? children[n - 1] : v.t; - } - function d3_layout_treeMove(wm, wp, shift) { - var change = shift / (wp.i - wm.i); - wp.c -= change; - wp.s += shift; - wm.c += change; - wp.z += shift; - wp.m += shift; - } - function d3_layout_treeShift(v) { - var shift = 0, change = 0, children = v.children, i = children.length, w; - while (--i >= 0) { - w = children[i]; - w.z += shift; - w.m += shift; - shift += w.s + (change += w.c); - } - } - function d3_layout_treeAncestor(vim, v, ancestor) { - return vim.a.parent === v.parent ? vim.a : ancestor; - } - d3.layout.cluster = function() { - var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false; - function cluster(d, i) { - var nodes = hierarchy.call(this, d, i), root = nodes[0], previousNode, x = 0; - d3_layout_hierarchyVisitAfter(root, function(node) { - var children = node.children; - if (children && children.length) { - node.x = d3_layout_clusterX(children); - node.y = d3_layout_clusterY(children); - } else { - node.x = previousNode ? x += separation(node, previousNode) : 0; - node.y = 0; - previousNode = node; - } - }); - var left = d3_layout_clusterLeft(root), right = d3_layout_clusterRight(root), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2; - d3_layout_hierarchyVisitAfter(root, nodeSize ? function(node) { - node.x = (node.x - root.x) * size[0]; - node.y = (root.y - node.y) * size[1]; - } : function(node) { - node.x = (node.x - x0) / (x1 - x0) * size[0]; - node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1]; - }); - return nodes; - } - cluster.separation = function(x) { - if (!arguments.length) return separation; - separation = x; - return cluster; - }; - cluster.size = function(x) { - if (!arguments.length) return nodeSize ? null : size; - nodeSize = (size = x) == null; - return cluster; - }; - cluster.nodeSize = function(x) { - if (!arguments.length) return nodeSize ? size : null; - nodeSize = (size = x) != null; - return cluster; - }; - return d3_layout_hierarchyRebind(cluster, hierarchy); - }; - function d3_layout_clusterY(children) { - return 1 + d3.max(children, function(child) { - return child.y; - }); - } - function d3_layout_clusterX(children) { - return children.reduce(function(x, child) { - return x + child.x; - }, 0) / children.length; - } - function d3_layout_clusterLeft(node) { - var children = node.children; - return children && children.length ? d3_layout_clusterLeft(children[0]) : node; - } - function d3_layout_clusterRight(node) { - var children = node.children, n; - return children && (n = children.length) ? d3_layout_clusterRight(children[n - 1]) : node; - } - d3.layout.treemap = function() { - var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, mode = "squarify", ratio = .5 * (1 + Math.sqrt(5)); - function scale(children, k) { - var i = -1, n = children.length, child, area; - while (++i < n) { - area = (child = children[i]).value * (k < 0 ? 0 : k); - child.area = isNaN(area) || area <= 0 ? 0 : area; - } - } - function squarify(node) { - var children = node.children; - if (children && children.length) { - var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" ? rect.dy : mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx : Math.min(rect.dx, rect.dy), n; - scale(remaining, rect.dx * rect.dy / node.value); - row.area = 0; - while ((n = remaining.length) > 0) { - row.push(child = remaining[n - 1]); - row.area += child.area; - if (mode !== "squarify" || (score = worst(row, u)) <= best) { - remaining.pop(); - best = score; - } else { - row.area -= row.pop().area; - position(row, u, rect, false); - u = Math.min(rect.dx, rect.dy); - row.length = row.area = 0; - best = Infinity; - } - } - if (row.length) { - position(row, u, rect, true); - row.length = row.area = 0; - } - children.forEach(squarify); - } - } - function stickify(node) { - var children = node.children; - if (children && children.length) { - var rect = pad(node), remaining = children.slice(), child, row = []; - scale(remaining, rect.dx * rect.dy / node.value); - row.area = 0; - while (child = remaining.pop()) { - row.push(child); - row.area += child.area; - if (child.z != null) { - position(row, child.z ? rect.dx : rect.dy, rect, !remaining.length); - row.length = row.area = 0; - } - } - children.forEach(stickify); - } - } - function worst(row, u) { - var s = row.area, r, rmax = 0, rmin = Infinity, i = -1, n = row.length; - while (++i < n) { - if (!(r = row[i].area)) continue; - if (r < rmin) rmin = r; - if (r > rmax) rmax = r; - } - s *= s; - u *= u; - return s ? Math.max(u * rmax * ratio / s, s / (u * rmin * ratio)) : Infinity; - } - function position(row, u, rect, flush) { - var i = -1, n = row.length, x = rect.x, y = rect.y, v = u ? round(row.area / u) : 0, o; - if (u == rect.dx) { - if (flush || v > rect.dy) v = rect.dy; - while (++i < n) { - o = row[i]; - o.x = x; - o.y = y; - o.dy = v; - x += o.dx = Math.min(rect.x + rect.dx - x, v ? round(o.area / v) : 0); - } - o.z = true; - o.dx += rect.x + rect.dx - x; - rect.y += v; - rect.dy -= v; - } else { - if (flush || v > rect.dx) v = rect.dx; - while (++i < n) { - o = row[i]; - o.x = x; - o.y = y; - o.dx = v; - y += o.dy = Math.min(rect.y + rect.dy - y, v ? round(o.area / v) : 0); - } - o.z = false; - o.dy += rect.y + rect.dy - y; - rect.x += v; - rect.dx -= v; - } - } - function treemap(d) { - var nodes = stickies || hierarchy(d), root = nodes[0]; - root.x = root.y = 0; - if (root.value) root.dx = size[0], root.dy = size[1]; else root.dx = root.dy = 0; - if (stickies) hierarchy.revalue(root); - scale([ root ], root.dx * root.dy / root.value); - (stickies ? stickify : squarify)(root); - if (sticky) stickies = nodes; - return nodes; - } - treemap.size = function(x) { - if (!arguments.length) return size; - size = x; - return treemap; - }; - treemap.padding = function(x) { - if (!arguments.length) return padding; - function padFunction(node) { - var p = x.call(treemap, node, node.depth); - return p == null ? d3_layout_treemapPadNull(node) : d3_layout_treemapPad(node, typeof p === "number" ? [ p, p, p, p ] : p); - } - function padConstant(node) { - return d3_layout_treemapPad(node, x); - } - var type; - pad = (padding = x) == null ? d3_layout_treemapPadNull : (type = typeof x) === "function" ? padFunction : type === "number" ? (x = [ x, x, x, x ], - padConstant) : padConstant; - return treemap; - }; - treemap.round = function(x) { - if (!arguments.length) return round != Number; - round = x ? Math.round : Number; - return treemap; - }; - treemap.sticky = function(x) { - if (!arguments.length) return sticky; - sticky = x; - stickies = null; - return treemap; - }; - treemap.ratio = function(x) { - if (!arguments.length) return ratio; - ratio = x; - return treemap; - }; - treemap.mode = function(x) { - if (!arguments.length) return mode; - mode = x + ""; - return treemap; - }; - return d3_layout_hierarchyRebind(treemap, hierarchy); - }; - function d3_layout_treemapPadNull(node) { - return { - x: node.x, - y: node.y, - dx: node.dx, - dy: node.dy - }; - } - function d3_layout_treemapPad(node, padding) { - var x = node.x + padding[3], y = node.y + padding[0], dx = node.dx - padding[1] - padding[3], dy = node.dy - padding[0] - padding[2]; - if (dx < 0) { - x += dx / 2; - dx = 0; - } - if (dy < 0) { - y += dy / 2; - dy = 0; - } - return { - x: x, - y: y, - dx: dx, - dy: dy - }; - } - d3.random = { - normal: function(µ, σ) { - var n = arguments.length; - if (n < 2) σ = 1; - if (n < 1) µ = 0; - return function() { - var x, y, r; - do { - x = Math.random() * 2 - 1; - y = Math.random() * 2 - 1; - r = x * x + y * y; - } while (!r || r > 1); - return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r); - }; - }, - logNormal: function() { - var random = d3.random.normal.apply(d3, arguments); - return function() { - return Math.exp(random()); - }; - }, - bates: function(m) { - var random = d3.random.irwinHall(m); - return function() { - return random() / m; - }; - }, - irwinHall: function(m) { - return function() { - for (var s = 0, j = 0; j < m; j++) s += Math.random(); - return s; - }; - } - }; - d3.scale = {}; - function d3_scaleExtent(domain) { - var start = domain[0], stop = domain[domain.length - 1]; - return start < stop ? [ start, stop ] : [ stop, start ]; - } - function d3_scaleRange(scale) { - return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range()); - } - function d3_scale_bilinear(domain, range, uninterpolate, interpolate) { - var u = uninterpolate(domain[0], domain[1]), i = interpolate(range[0], range[1]); - return function(x) { - return i(u(x)); - }; - } - function d3_scale_nice(domain, nice) { - var i0 = 0, i1 = domain.length - 1, x0 = domain[i0], x1 = domain[i1], dx; - if (x1 < x0) { - dx = i0, i0 = i1, i1 = dx; - dx = x0, x0 = x1, x1 = dx; - } - domain[i0] = nice.floor(x0); - domain[i1] = nice.ceil(x1); - return domain; - } - function d3_scale_niceStep(step) { - return step ? { - floor: function(x) { - return Math.floor(x / step) * step; - }, - ceil: function(x) { - return Math.ceil(x / step) * step; - } - } : d3_scale_niceIdentity; - } - var d3_scale_niceIdentity = { - floor: d3_identity, - ceil: d3_identity - }; - function d3_scale_polylinear(domain, range, uninterpolate, interpolate) { - var u = [], i = [], j = 0, k = Math.min(domain.length, range.length) - 1; - if (domain[k] < domain[0]) { - domain = domain.slice().reverse(); - range = range.slice().reverse(); - } - while (++j <= k) { - u.push(uninterpolate(domain[j - 1], domain[j])); - i.push(interpolate(range[j - 1], range[j])); - } - return function(x) { - var j = d3.bisect(domain, x, 1, k) - 1; - return i[j](u[j](x)); - }; - } - d3.scale.linear = function() { - return d3_scale_linear([ 0, 1 ], [ 0, 1 ], d3_interpolate, false); - }; - function d3_scale_linear(domain, range, interpolate, clamp) { - var output, input; - function rescale() { - var linear = Math.min(domain.length, range.length) > 2 ? d3_scale_polylinear : d3_scale_bilinear, uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber; - output = linear(domain, range, uninterpolate, interpolate); - input = linear(range, domain, uninterpolate, d3_interpolate); - return scale; - } - function scale(x) { - return output(x); - } - scale.invert = function(y) { - return input(y); - }; - scale.domain = function(x) { - if (!arguments.length) return domain; - domain = x.map(Number); - return rescale(); - }; - scale.range = function(x) { - if (!arguments.length) return range; - range = x; - return rescale(); - }; - scale.rangeRound = function(x) { - return scale.range(x).interpolate(d3_interpolateRound); - }; - scale.clamp = function(x) { - if (!arguments.length) return clamp; - clamp = x; - return rescale(); - }; - scale.interpolate = function(x) { - if (!arguments.length) return interpolate; - interpolate = x; - return rescale(); - }; - scale.ticks = function(m) { - return d3_scale_linearTicks(domain, m); - }; - scale.tickFormat = function(m, format) { - return d3_scale_linearTickFormat(domain, m, format); - }; - scale.nice = function(m) { - d3_scale_linearNice(domain, m); - return rescale(); - }; - scale.copy = function() { - return d3_scale_linear(domain, range, interpolate, clamp); - }; - return rescale(); - } - function d3_scale_linearRebind(scale, linear) { - return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp"); - } - function d3_scale_linearNice(domain, m) { - d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); - d3_scale_nice(domain, d3_scale_niceStep(d3_scale_linearTickRange(domain, m)[2])); - return domain; - } - function d3_scale_linearTickRange(domain, m) { - if (m == null) m = 10; - var extent = d3_scaleExtent(domain), span = extent[1] - extent[0], step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), err = m / span * step; - if (err <= .15) step *= 10; else if (err <= .35) step *= 5; else if (err <= .75) step *= 2; - extent[0] = Math.ceil(extent[0] / step) * step; - extent[1] = Math.floor(extent[1] / step) * step + step * .5; - extent[2] = step; - return extent; - } - function d3_scale_linearTicks(domain, m) { - return d3.range.apply(d3, d3_scale_linearTickRange(domain, m)); - } - function d3_scale_linearTickFormat(domain, m, format) { - var range = d3_scale_linearTickRange(domain, m); - if (format) { - var match = d3_format_re.exec(format); - match.shift(); - if (match[8] === "s") { - var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1]))); - if (!match[7]) match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2])); - match[8] = "f"; - format = d3.format(match.join("")); - return function(d) { - return format(prefix.scale(d)) + prefix.symbol; - }; - } - if (!match[7]) match[7] = "." + d3_scale_linearFormatPrecision(match[8], range); - format = match.join(""); - } else { - format = ",." + d3_scale_linearPrecision(range[2]) + "f"; - } - return d3.format(format); - } - var d3_scale_linearFormatSignificant = { - s: 1, - g: 1, - p: 1, - r: 1, - e: 1 - }; - function d3_scale_linearPrecision(value) { - return -Math.floor(Math.log(value) / Math.LN10 + .01); - } - function d3_scale_linearFormatPrecision(type, range) { - var p = d3_scale_linearPrecision(range[2]); - return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(abs(range[0]), abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2; - } - d3.scale.log = function() { - return d3_scale_log(d3.scale.linear().domain([ 0, 1 ]), 10, true, [ 1, 10 ]); - }; - function d3_scale_log(linear, base, positive, domain) { - function log(x) { - return (positive ? Math.log(x < 0 ? 0 : x) : -Math.log(x > 0 ? 0 : -x)) / Math.log(base); - } - function pow(x) { - return positive ? Math.pow(base, x) : -Math.pow(base, -x); - } - function scale(x) { - return linear(log(x)); - } - scale.invert = function(x) { - return pow(linear.invert(x)); - }; - scale.domain = function(x) { - if (!arguments.length) return domain; - positive = x[0] >= 0; - linear.domain((domain = x.map(Number)).map(log)); - return scale; - }; - scale.base = function(_) { - if (!arguments.length) return base; - base = +_; - linear.domain(domain.map(log)); - return scale; - }; - scale.nice = function() { - var niced = d3_scale_nice(domain.map(log), positive ? Math : d3_scale_logNiceNegative); - linear.domain(niced); - domain = niced.map(pow); - return scale; - }; - scale.ticks = function() { - var extent = d3_scaleExtent(domain), ticks = [], u = extent[0], v = extent[1], i = Math.floor(log(u)), j = Math.ceil(log(v)), n = base % 1 ? 2 : base; - if (isFinite(j - i)) { - if (positive) { - for (;i < j; i++) for (var k = 1; k < n; k++) ticks.push(pow(i) * k); - ticks.push(pow(i)); - } else { - ticks.push(pow(i)); - for (;i++ < j; ) for (var k = n - 1; k > 0; k--) ticks.push(pow(i) * k); - } - for (i = 0; ticks[i] < u; i++) {} - for (j = ticks.length; ticks[j - 1] > v; j--) {} - ticks = ticks.slice(i, j); - } - return ticks; - }; - scale.tickFormat = function(n, format) { - if (!arguments.length) return d3_scale_logFormat; - if (arguments.length < 2) format = d3_scale_logFormat; else if (typeof format !== "function") format = d3.format(format); - var k = Math.max(1, base * n / scale.ticks().length); - return function(d) { - var i = d / pow(Math.round(log(d))); - if (i * base < base - .5) i *= base; - return i <= k ? format(d) : ""; - }; - }; - scale.copy = function() { - return d3_scale_log(linear.copy(), base, positive, domain); - }; - return d3_scale_linearRebind(scale, linear); - } - var d3_scale_logFormat = d3.format(".0e"), d3_scale_logNiceNegative = { - floor: function(x) { - return -Math.ceil(-x); - }, - ceil: function(x) { - return -Math.floor(-x); - } - }; - d3.scale.pow = function() { - return d3_scale_pow(d3.scale.linear(), 1, [ 0, 1 ]); - }; - function d3_scale_pow(linear, exponent, domain) { - var powp = d3_scale_powPow(exponent), powb = d3_scale_powPow(1 / exponent); - function scale(x) { - return linear(powp(x)); - } - scale.invert = function(x) { - return powb(linear.invert(x)); - }; - scale.domain = function(x) { - if (!arguments.length) return domain; - linear.domain((domain = x.map(Number)).map(powp)); - return scale; - }; - scale.ticks = function(m) { - return d3_scale_linearTicks(domain, m); - }; - scale.tickFormat = function(m, format) { - return d3_scale_linearTickFormat(domain, m, format); - }; - scale.nice = function(m) { - return scale.domain(d3_scale_linearNice(domain, m)); - }; - scale.exponent = function(x) { - if (!arguments.length) return exponent; - powp = d3_scale_powPow(exponent = x); - powb = d3_scale_powPow(1 / exponent); - linear.domain(domain.map(powp)); - return scale; - }; - scale.copy = function() { - return d3_scale_pow(linear.copy(), exponent, domain); - }; - return d3_scale_linearRebind(scale, linear); - } - function d3_scale_powPow(e) { - return function(x) { - return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e); - }; - } - d3.scale.sqrt = function() { - return d3.scale.pow().exponent(.5); - }; - d3.scale.ordinal = function() { - return d3_scale_ordinal([], { - t: "range", - a: [ [] ] - }); - }; - function d3_scale_ordinal(domain, ranger) { - var index, range, rangeBand; - function scale(x) { - return range[((index.get(x) || (ranger.t === "range" ? index.set(x, domain.push(x)) : NaN)) - 1) % range.length]; - } - function steps(start, step) { - return d3.range(domain.length).map(function(i) { - return start + step * i; - }); - } - scale.domain = function(x) { - if (!arguments.length) return domain; - domain = []; - index = new d3_Map(); - var i = -1, n = x.length, xi; - while (++i < n) if (!index.has(xi = x[i])) index.set(xi, domain.push(xi)); - return scale[ranger.t].apply(scale, ranger.a); - }; - scale.range = function(x) { - if (!arguments.length) return range; - range = x; - rangeBand = 0; - ranger = { - t: "range", - a: arguments - }; - return scale; - }; - scale.rangePoints = function(x, padding) { - if (arguments.length < 2) padding = 0; - var start = x[0], stop = x[1], step = domain.length < 2 ? (start = (start + stop) / 2, - 0) : (stop - start) / (domain.length - 1 + padding); - range = steps(start + step * padding / 2, step); - rangeBand = 0; - ranger = { - t: "rangePoints", - a: arguments - }; - return scale; - }; - scale.rangeRoundPoints = function(x, padding) { - if (arguments.length < 2) padding = 0; - var start = x[0], stop = x[1], step = domain.length < 2 ? (start = stop = Math.round((start + stop) / 2), - 0) : (stop - start) / (domain.length - 1 + padding) | 0; - range = steps(start + Math.round(step * padding / 2 + (stop - start - (domain.length - 1 + padding) * step) / 2), step); - rangeBand = 0; - ranger = { - t: "rangeRoundPoints", - a: arguments - }; - return scale; - }; - scale.rangeBands = function(x, padding, outerPadding) { - if (arguments.length < 2) padding = 0; - if (arguments.length < 3) outerPadding = padding; - var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = (stop - start) / (domain.length - padding + 2 * outerPadding); - range = steps(start + step * outerPadding, step); - if (reverse) range.reverse(); - rangeBand = step * (1 - padding); - ranger = { - t: "rangeBands", - a: arguments - }; - return scale; - }; - scale.rangeRoundBands = function(x, padding, outerPadding) { - if (arguments.length < 2) padding = 0; - if (arguments.length < 3) outerPadding = padding; - var reverse = x[1] < x[0], start = x[reverse - 0], stop = x[1 - reverse], step = Math.floor((stop - start) / (domain.length - padding + 2 * outerPadding)); - range = steps(start + Math.round((stop - start - (domain.length - padding) * step) / 2), step); - if (reverse) range.reverse(); - rangeBand = Math.round(step * (1 - padding)); - ranger = { - t: "rangeRoundBands", - a: arguments - }; - return scale; - }; - scale.rangeBand = function() { - return rangeBand; - }; - scale.rangeExtent = function() { - return d3_scaleExtent(ranger.a[0]); - }; - scale.copy = function() { - return d3_scale_ordinal(domain, ranger); - }; - return scale.domain(domain); - } - d3.scale.category10 = function() { - return d3.scale.ordinal().range(d3_category10); - }; - d3.scale.category20 = function() { - return d3.scale.ordinal().range(d3_category20); - }; - d3.scale.category20b = function() { - return d3.scale.ordinal().range(d3_category20b); - }; - d3.scale.category20c = function() { - return d3.scale.ordinal().range(d3_category20c); - }; - var d3_category10 = [ 2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175 ].map(d3_rgbString); - var d3_category20 = [ 2062260, 11454440, 16744206, 16759672, 2924588, 10018698, 14034728, 16750742, 9725885, 12955861, 9197131, 12885140, 14907330, 16234194, 8355711, 13092807, 12369186, 14408589, 1556175, 10410725 ].map(d3_rgbString); - var d3_category20b = [ 3750777, 5395619, 7040719, 10264286, 6519097, 9216594, 11915115, 13556636, 9202993, 12426809, 15186514, 15190932, 8666169, 11356490, 14049643, 15177372, 8077683, 10834324, 13528509, 14589654 ].map(d3_rgbString); - var d3_category20c = [ 3244733, 7057110, 10406625, 13032431, 15095053, 16616764, 16625259, 16634018, 3253076, 7652470, 10607003, 13101504, 7695281, 10394312, 12369372, 14342891, 6513507, 9868950, 12434877, 14277081 ].map(d3_rgbString); - d3.scale.quantile = function() { - return d3_scale_quantile([], []); - }; - function d3_scale_quantile(domain, range) { - var thresholds; - function rescale() { - var k = 0, q = range.length; - thresholds = []; - while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q); - return scale; - } - function scale(x) { - if (!isNaN(x = +x)) return range[d3.bisect(thresholds, x)]; - } - scale.domain = function(x) { - if (!arguments.length) return domain; - domain = x.map(d3_number).filter(d3_numeric).sort(d3_ascending); - return rescale(); - }; - scale.range = function(x) { - if (!arguments.length) return range; - range = x; - return rescale(); - }; - scale.quantiles = function() { - return thresholds; - }; - scale.invertExtent = function(y) { - y = range.indexOf(y); - return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ]; - }; - scale.copy = function() { - return d3_scale_quantile(domain, range); - }; - return rescale(); - } - d3.scale.quantize = function() { - return d3_scale_quantize(0, 1, [ 0, 1 ]); - }; - function d3_scale_quantize(x0, x1, range) { - var kx, i; - function scale(x) { - return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))]; - } - function rescale() { - kx = range.length / (x1 - x0); - i = range.length - 1; - return scale; - } - scale.domain = function(x) { - if (!arguments.length) return [ x0, x1 ]; - x0 = +x[0]; - x1 = +x[x.length - 1]; - return rescale(); - }; - scale.range = function(x) { - if (!arguments.length) return range; - range = x; - return rescale(); - }; - scale.invertExtent = function(y) { - y = range.indexOf(y); - y = y < 0 ? NaN : y / kx + x0; - return [ y, y + 1 / kx ]; - }; - scale.copy = function() { - return d3_scale_quantize(x0, x1, range); - }; - return rescale(); - } - d3.scale.threshold = function() { - return d3_scale_threshold([ .5 ], [ 0, 1 ]); - }; - function d3_scale_threshold(domain, range) { - function scale(x) { - if (x <= x) return range[d3.bisect(domain, x)]; - } - scale.domain = function(_) { - if (!arguments.length) return domain; - domain = _; - return scale; - }; - scale.range = function(_) { - if (!arguments.length) return range; - range = _; - return scale; - }; - scale.invertExtent = function(y) { - y = range.indexOf(y); - return [ domain[y - 1], domain[y] ]; - }; - scale.copy = function() { - return d3_scale_threshold(domain, range); - }; - return scale; - } - d3.scale.identity = function() { - return d3_scale_identity([ 0, 1 ]); - }; - function d3_scale_identity(domain) { - function identity(x) { - return +x; - } - identity.invert = identity; - identity.domain = identity.range = function(x) { - if (!arguments.length) return domain; - domain = x.map(identity); - return identity; - }; - identity.ticks = function(m) { - return d3_scale_linearTicks(domain, m); - }; - identity.tickFormat = function(m, format) { - return d3_scale_linearTickFormat(domain, m, format); - }; - identity.copy = function() { - return d3_scale_identity(domain); - }; - return identity; - } - d3.svg = {}; - function d3_zero() { - return 0; - } - d3.svg.arc = function() { - var innerRadius = d3_svg_arcInnerRadius, outerRadius = d3_svg_arcOuterRadius, cornerRadius = d3_zero, padRadius = d3_svg_arcAuto, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle, padAngle = d3_svg_arcPadAngle; - function arc() { - var r0 = Math.max(0, +innerRadius.apply(this, arguments)), r1 = Math.max(0, +outerRadius.apply(this, arguments)), a0 = startAngle.apply(this, arguments) - halfπ, a1 = endAngle.apply(this, arguments) - halfπ, da = Math.abs(a1 - a0), cw = a0 > a1 ? 0 : 1; - if (r1 < r0) rc = r1, r1 = r0, r0 = rc; - if (da >= τε) return circleSegment(r1, cw) + (r0 ? circleSegment(r0, 1 - cw) : "") + "Z"; - var rc, cr, rp, ap, p0 = 0, p1 = 0, x0, y0, x1, y1, x2, y2, x3, y3, path = []; - if (ap = (+padAngle.apply(this, arguments) || 0) / 2) { - rp = padRadius === d3_svg_arcAuto ? Math.sqrt(r0 * r0 + r1 * r1) : +padRadius.apply(this, arguments); - if (!cw) p1 *= -1; - if (r1) p1 = d3_asin(rp / r1 * Math.sin(ap)); - if (r0) p0 = d3_asin(rp / r0 * Math.sin(ap)); - } - if (r1) { - x0 = r1 * Math.cos(a0 + p1); - y0 = r1 * Math.sin(a0 + p1); - x1 = r1 * Math.cos(a1 - p1); - y1 = r1 * Math.sin(a1 - p1); - var l1 = Math.abs(a1 - a0 - 2 * p1) <= π ? 0 : 1; - if (p1 && d3_svg_arcSweep(x0, y0, x1, y1) === cw ^ l1) { - var h1 = (a0 + a1) / 2; - x0 = r1 * Math.cos(h1); - y0 = r1 * Math.sin(h1); - x1 = y1 = null; - } - } else { - x0 = y0 = 0; - } - if (r0) { - x2 = r0 * Math.cos(a1 - p0); - y2 = r0 * Math.sin(a1 - p0); - x3 = r0 * Math.cos(a0 + p0); - y3 = r0 * Math.sin(a0 + p0); - var l0 = Math.abs(a0 - a1 + 2 * p0) <= π ? 0 : 1; - if (p0 && d3_svg_arcSweep(x2, y2, x3, y3) === 1 - cw ^ l0) { - var h0 = (a0 + a1) / 2; - x2 = r0 * Math.cos(h0); - y2 = r0 * Math.sin(h0); - x3 = y3 = null; - } - } else { - x2 = y2 = 0; - } - if (da > ε && (rc = Math.min(Math.abs(r1 - r0) / 2, +cornerRadius.apply(this, arguments))) > .001) { - cr = r0 < r1 ^ cw ? 0 : 1; - var rc1 = rc, rc0 = rc; - if (da < π) { - var oc = x3 == null ? [ x2, y2 ] : x1 == null ? [ x0, y0 ] : d3_geom_polygonIntersect([ x0, y0 ], [ x3, y3 ], [ x1, y1 ], [ x2, y2 ]), ax = x0 - oc[0], ay = y0 - oc[1], bx = x1 - oc[0], by = y1 - oc[1], kc = 1 / Math.sin(Math.acos((ax * bx + ay * by) / (Math.sqrt(ax * ax + ay * ay) * Math.sqrt(bx * bx + by * by))) / 2), lc = Math.sqrt(oc[0] * oc[0] + oc[1] * oc[1]); - rc0 = Math.min(rc, (r0 - lc) / (kc - 1)); - rc1 = Math.min(rc, (r1 - lc) / (kc + 1)); - } - if (x1 != null) { - var t30 = d3_svg_arcCornerTangents(x3 == null ? [ x2, y2 ] : [ x3, y3 ], [ x0, y0 ], r1, rc1, cw), t12 = d3_svg_arcCornerTangents([ x1, y1 ], [ x2, y2 ], r1, rc1, cw); - if (rc === rc1) { - path.push("M", t30[0], "A", rc1, ",", rc1, " 0 0,", cr, " ", t30[1], "A", r1, ",", r1, " 0 ", 1 - cw ^ d3_svg_arcSweep(t30[1][0], t30[1][1], t12[1][0], t12[1][1]), ",", cw, " ", t12[1], "A", rc1, ",", rc1, " 0 0,", cr, " ", t12[0]); - } else { - path.push("M", t30[0], "A", rc1, ",", rc1, " 0 1,", cr, " ", t12[0]); - } - } else { - path.push("M", x0, ",", y0); - } - if (x3 != null) { - var t03 = d3_svg_arcCornerTangents([ x0, y0 ], [ x3, y3 ], r0, -rc0, cw), t21 = d3_svg_arcCornerTangents([ x2, y2 ], x1 == null ? [ x0, y0 ] : [ x1, y1 ], r0, -rc0, cw); - if (rc === rc0) { - path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t21[1], "A", r0, ",", r0, " 0 ", cw ^ d3_svg_arcSweep(t21[1][0], t21[1][1], t03[1][0], t03[1][1]), ",", 1 - cw, " ", t03[1], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]); - } else { - path.push("L", t21[0], "A", rc0, ",", rc0, " 0 0,", cr, " ", t03[0]); - } - } else { - path.push("L", x2, ",", y2); - } - } else { - path.push("M", x0, ",", y0); - if (x1 != null) path.push("A", r1, ",", r1, " 0 ", l1, ",", cw, " ", x1, ",", y1); - path.push("L", x2, ",", y2); - if (x3 != null) path.push("A", r0, ",", r0, " 0 ", l0, ",", 1 - cw, " ", x3, ",", y3); - } - path.push("Z"); - return path.join(""); - } - function circleSegment(r1, cw) { - return "M0," + r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + -r1 + "A" + r1 + "," + r1 + " 0 1," + cw + " 0," + r1; - } - arc.innerRadius = function(v) { - if (!arguments.length) return innerRadius; - innerRadius = d3_functor(v); - return arc; - }; - arc.outerRadius = function(v) { - if (!arguments.length) return outerRadius; - outerRadius = d3_functor(v); - return arc; - }; - arc.cornerRadius = function(v) { - if (!arguments.length) return cornerRadius; - cornerRadius = d3_functor(v); - return arc; - }; - arc.padRadius = function(v) { - if (!arguments.length) return padRadius; - padRadius = v == d3_svg_arcAuto ? d3_svg_arcAuto : d3_functor(v); - return arc; - }; - arc.startAngle = function(v) { - if (!arguments.length) return startAngle; - startAngle = d3_functor(v); - return arc; - }; - arc.endAngle = function(v) { - if (!arguments.length) return endAngle; - endAngle = d3_functor(v); - return arc; - }; - arc.padAngle = function(v) { - if (!arguments.length) return padAngle; - padAngle = d3_functor(v); - return arc; - }; - arc.centroid = function() { - var r = (+innerRadius.apply(this, arguments) + +outerRadius.apply(this, arguments)) / 2, a = (+startAngle.apply(this, arguments) + +endAngle.apply(this, arguments)) / 2 - halfπ; - return [ Math.cos(a) * r, Math.sin(a) * r ]; - }; - return arc; - }; - var d3_svg_arcAuto = "auto"; - function d3_svg_arcInnerRadius(d) { - return d.innerRadius; - } - function d3_svg_arcOuterRadius(d) { - return d.outerRadius; - } - function d3_svg_arcStartAngle(d) { - return d.startAngle; - } - function d3_svg_arcEndAngle(d) { - return d.endAngle; - } - function d3_svg_arcPadAngle(d) { - return d && d.padAngle; - } - function d3_svg_arcSweep(x0, y0, x1, y1) { - return (x0 - x1) * y0 - (y0 - y1) * x0 > 0 ? 0 : 1; - } - function d3_svg_arcCornerTangents(p0, p1, r1, rc, cw) { - var x01 = p0[0] - p1[0], y01 = p0[1] - p1[1], lo = (cw ? rc : -rc) / Math.sqrt(x01 * x01 + y01 * y01), ox = lo * y01, oy = -lo * x01, x1 = p0[0] + ox, y1 = p0[1] + oy, x2 = p1[0] + ox, y2 = p1[1] + oy, x3 = (x1 + x2) / 2, y3 = (y1 + y2) / 2, dx = x2 - x1, dy = y2 - y1, d2 = dx * dx + dy * dy, r = r1 - rc, D = x1 * y2 - x2 * y1, d = (dy < 0 ? -1 : 1) * Math.sqrt(Math.max(0, r * r * d2 - D * D)), cx0 = (D * dy - dx * d) / d2, cy0 = (-D * dx - dy * d) / d2, cx1 = (D * dy + dx * d) / d2, cy1 = (-D * dx + dy * d) / d2, dx0 = cx0 - x3, dy0 = cy0 - y3, dx1 = cx1 - x3, dy1 = cy1 - y3; - if (dx0 * dx0 + dy0 * dy0 > dx1 * dx1 + dy1 * dy1) cx0 = cx1, cy0 = cy1; - return [ [ cx0 - ox, cy0 - oy ], [ cx0 * r1 / r, cy0 * r1 / r ] ]; - } - function d3_svg_line(projection) { - var x = d3_geom_pointX, y = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, tension = .7; - function line(data) { - var segments = [], points = [], i = -1, n = data.length, d, fx = d3_functor(x), fy = d3_functor(y); - function segment() { - segments.push("M", interpolate(projection(points), tension)); - } - while (++i < n) { - if (defined.call(this, d = data[i], i)) { - points.push([ +fx.call(this, d, i), +fy.call(this, d, i) ]); - } else if (points.length) { - segment(); - points = []; - } - } - if (points.length) segment(); - return segments.length ? segments.join("") : null; - } - line.x = function(_) { - if (!arguments.length) return x; - x = _; - return line; - }; - line.y = function(_) { - if (!arguments.length) return y; - y = _; - return line; - }; - line.defined = function(_) { - if (!arguments.length) return defined; - defined = _; - return line; - }; - line.interpolate = function(_) { - if (!arguments.length) return interpolateKey; - if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; - return line; - }; - line.tension = function(_) { - if (!arguments.length) return tension; - tension = _; - return line; - }; - return line; - } - d3.svg.line = function() { - return d3_svg_line(d3_identity); - }; - var d3_svg_lineInterpolators = d3.map({ - linear: d3_svg_lineLinear, - "linear-closed": d3_svg_lineLinearClosed, - step: d3_svg_lineStep, - "step-before": d3_svg_lineStepBefore, - "step-after": d3_svg_lineStepAfter, - basis: d3_svg_lineBasis, - "basis-open": d3_svg_lineBasisOpen, - "basis-closed": d3_svg_lineBasisClosed, - bundle: d3_svg_lineBundle, - cardinal: d3_svg_lineCardinal, - "cardinal-open": d3_svg_lineCardinalOpen, - "cardinal-closed": d3_svg_lineCardinalClosed, - monotone: d3_svg_lineMonotone - }); - d3_svg_lineInterpolators.forEach(function(key, value) { - value.key = key; - value.closed = /-closed$/.test(key); - }); - function d3_svg_lineLinear(points) { - return points.length > 1 ? points.join("L") : points + "Z"; - } - function d3_svg_lineLinearClosed(points) { - return points.join("L") + "Z"; - } - function d3_svg_lineStep(points) { - var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; - while (++i < n) path.push("H", (p[0] + (p = points[i])[0]) / 2, "V", p[1]); - if (n > 1) path.push("H", p[0]); - return path.join(""); - } - function d3_svg_lineStepBefore(points) { - var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; - while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]); - return path.join(""); - } - function d3_svg_lineStepAfter(points) { - var i = 0, n = points.length, p = points[0], path = [ p[0], ",", p[1] ]; - while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]); - return path.join(""); - } - function d3_svg_lineCardinalOpen(points, tension) { - return points.length < 4 ? d3_svg_lineLinear(points) : points[1] + d3_svg_lineHermite(points.slice(1, -1), d3_svg_lineCardinalTangents(points, tension)); - } - function d3_svg_lineCardinalClosed(points, tension) { - return points.length < 3 ? d3_svg_lineLinearClosed(points) : points[0] + d3_svg_lineHermite((points.push(points[0]), - points), d3_svg_lineCardinalTangents([ points[points.length - 2] ].concat(points, [ points[1] ]), tension)); - } - function d3_svg_lineCardinal(points, tension) { - return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineCardinalTangents(points, tension)); - } - function d3_svg_lineHermite(points, tangents) { - if (tangents.length < 1 || points.length != tangents.length && points.length != tangents.length + 2) { - return d3_svg_lineLinear(points); - } - var quad = points.length != tangents.length, path = "", p0 = points[0], p = points[1], t0 = tangents[0], t = t0, pi = 1; - if (quad) { - path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3) + "," + p[0] + "," + p[1]; - p0 = points[1]; - pi = 2; - } - if (tangents.length > 1) { - t = tangents[1]; - p = points[pi]; - pi++; - path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1]) + "," + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; - for (var i = 2; i < tangents.length; i++, pi++) { - p = points[pi]; - t = tangents[i]; - path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1]) + "," + p[0] + "," + p[1]; - } - } - if (quad) { - var lp = points[pi]; - path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3) + "," + lp[0] + "," + lp[1]; - } - return path; - } - function d3_svg_lineCardinalTangents(points, tension) { - var tangents = [], a = (1 - tension) / 2, p0, p1 = points[0], p2 = points[1], i = 1, n = points.length; - while (++i < n) { - p0 = p1; - p1 = p2; - p2 = points[i]; - tangents.push([ a * (p2[0] - p0[0]), a * (p2[1] - p0[1]) ]); - } - return tangents; - } - function d3_svg_lineBasis(points) { - if (points.length < 3) return d3_svg_lineLinear(points); - var i = 1, n = points.length, pi = points[0], x0 = pi[0], y0 = pi[1], px = [ x0, x0, x0, (pi = points[1])[0] ], py = [ y0, y0, y0, pi[1] ], path = [ x0, ",", y0, "L", d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; - points.push(points[n - 1]); - while (++i <= n) { - pi = points[i]; - px.shift(); - px.push(pi[0]); - py.shift(); - py.push(pi[1]); - d3_svg_lineBasisBezier(path, px, py); - } - points.pop(); - path.push("L", pi); - return path.join(""); - } - function d3_svg_lineBasisOpen(points) { - if (points.length < 4) return d3_svg_lineLinear(points); - var path = [], i = -1, n = points.length, pi, px = [ 0 ], py = [ 0 ]; - while (++i < 3) { - pi = points[i]; - px.push(pi[0]); - py.push(pi[1]); - } - path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px) + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)); - --i; - while (++i < n) { - pi = points[i]; - px.shift(); - px.push(pi[0]); - py.shift(); - py.push(pi[1]); - d3_svg_lineBasisBezier(path, px, py); - } - return path.join(""); - } - function d3_svg_lineBasisClosed(points) { - var path, i = -1, n = points.length, m = n + 4, pi, px = [], py = []; - while (++i < 4) { - pi = points[i % n]; - px.push(pi[0]); - py.push(pi[1]); - } - path = [ d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, py) ]; - --i; - while (++i < m) { - pi = points[i % n]; - px.shift(); - px.push(pi[0]); - py.shift(); - py.push(pi[1]); - d3_svg_lineBasisBezier(path, px, py); - } - return path.join(""); - } - function d3_svg_lineBundle(points, tension) { - var n = points.length - 1; - if (n) { - var x0 = points[0][0], y0 = points[0][1], dx = points[n][0] - x0, dy = points[n][1] - y0, i = -1, p, t; - while (++i <= n) { - p = points[i]; - t = i / n; - p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx); - p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy); - } - } - return d3_svg_lineBasis(points); - } - function d3_svg_lineDot4(a, b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; - } - var d3_svg_lineBasisBezier1 = [ 0, 2 / 3, 1 / 3, 0 ], d3_svg_lineBasisBezier2 = [ 0, 1 / 3, 2 / 3, 0 ], d3_svg_lineBasisBezier3 = [ 0, 1 / 6, 2 / 3, 1 / 6 ]; - function d3_svg_lineBasisBezier(path, x, y) { - path.push("C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x), ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y)); - } - function d3_svg_lineSlope(p0, p1) { - return (p1[1] - p0[1]) / (p1[0] - p0[0]); - } - function d3_svg_lineFiniteDifferences(points) { - var i = 0, j = points.length - 1, m = [], p0 = points[0], p1 = points[1], d = m[0] = d3_svg_lineSlope(p0, p1); - while (++i < j) { - m[i] = (d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]))) / 2; - } - m[i] = d; - return m; - } - function d3_svg_lineMonotoneTangents(points) { - var tangents = [], d, a, b, s, m = d3_svg_lineFiniteDifferences(points), i = -1, j = points.length - 1; - while (++i < j) { - d = d3_svg_lineSlope(points[i], points[i + 1]); - if (abs(d) < ε) { - m[i] = m[i + 1] = 0; - } else { - a = m[i] / d; - b = m[i + 1] / d; - s = a * a + b * b; - if (s > 9) { - s = d * 3 / Math.sqrt(s); - m[i] = s * a; - m[i + 1] = s * b; - } - } - } - i = -1; - while (++i <= j) { - s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0]) / (6 * (1 + m[i] * m[i])); - tangents.push([ s || 0, m[i] * s || 0 ]); - } - return tangents; - } - function d3_svg_lineMonotone(points) { - return points.length < 3 ? d3_svg_lineLinear(points) : points[0] + d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points)); - } - d3.svg.line.radial = function() { - var line = d3_svg_line(d3_svg_lineRadial); - line.radius = line.x, delete line.x; - line.angle = line.y, delete line.y; - return line; - }; - function d3_svg_lineRadial(points) { - var point, i = -1, n = points.length, r, a; - while (++i < n) { - point = points[i]; - r = point[0]; - a = point[1] - halfπ; - point[0] = r * Math.cos(a); - point[1] = r * Math.sin(a); - } - return points; - } - function d3_svg_area(projection) { - var x0 = d3_geom_pointX, x1 = d3_geom_pointX, y0 = 0, y1 = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, interpolateReverse = interpolate, L = "L", tension = .7; - function area(data) { - var segments = [], points0 = [], points1 = [], i = -1, n = data.length, d, fx0 = d3_functor(x0), fy0 = d3_functor(y0), fx1 = x0 === x1 ? function() { - return x; - } : d3_functor(x1), fy1 = y0 === y1 ? function() { - return y; - } : d3_functor(y1), x, y; - function segment() { - segments.push("M", interpolate(projection(points1), tension), L, interpolateReverse(projection(points0.reverse()), tension), "Z"); - } - while (++i < n) { - if (defined.call(this, d = data[i], i)) { - points0.push([ x = +fx0.call(this, d, i), y = +fy0.call(this, d, i) ]); - points1.push([ +fx1.call(this, d, i), +fy1.call(this, d, i) ]); - } else if (points0.length) { - segment(); - points0 = []; - points1 = []; - } - } - if (points0.length) segment(); - return segments.length ? segments.join("") : null; - } - area.x = function(_) { - if (!arguments.length) return x1; - x0 = x1 = _; - return area; - }; - area.x0 = function(_) { - if (!arguments.length) return x0; - x0 = _; - return area; - }; - area.x1 = function(_) { - if (!arguments.length) return x1; - x1 = _; - return area; - }; - area.y = function(_) { - if (!arguments.length) return y1; - y0 = y1 = _; - return area; - }; - area.y0 = function(_) { - if (!arguments.length) return y0; - y0 = _; - return area; - }; - area.y1 = function(_) { - if (!arguments.length) return y1; - y1 = _; - return area; - }; - area.defined = function(_) { - if (!arguments.length) return defined; - defined = _; - return area; - }; - area.interpolate = function(_) { - if (!arguments.length) return interpolateKey; - if (typeof _ === "function") interpolateKey = interpolate = _; else interpolateKey = (interpolate = d3_svg_lineInterpolators.get(_) || d3_svg_lineLinear).key; - interpolateReverse = interpolate.reverse || interpolate; - L = interpolate.closed ? "M" : "L"; - return area; - }; - area.tension = function(_) { - if (!arguments.length) return tension; - tension = _; - return area; - }; - return area; - } - d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter; - d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore; - d3.svg.area = function() { - return d3_svg_area(d3_identity); - }; - d3.svg.area.radial = function() { - var area = d3_svg_area(d3_svg_lineRadial); - area.radius = area.x, delete area.x; - area.innerRadius = area.x0, delete area.x0; - area.outerRadius = area.x1, delete area.x1; - area.angle = area.y, delete area.y; - area.startAngle = area.y0, delete area.y0; - area.endAngle = area.y1, delete area.y1; - return area; - }; - d3.svg.chord = function() { - var source = d3_source, target = d3_target, radius = d3_svg_chordRadius, startAngle = d3_svg_arcStartAngle, endAngle = d3_svg_arcEndAngle; - function chord(d, i) { - var s = subgroup(this, source, d, i), t = subgroup(this, target, d, i); - return "M" + s.p0 + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t) ? curve(s.r, s.p1, s.r, s.p0) : curve(s.r, s.p1, t.r, t.p0) + arc(t.r, t.p1, t.a1 - t.a0) + curve(t.r, t.p1, s.r, s.p0)) + "Z"; - } - function subgroup(self, f, d, i) { - var subgroup = f.call(self, d, i), r = radius.call(self, subgroup, i), a0 = startAngle.call(self, subgroup, i) - halfπ, a1 = endAngle.call(self, subgroup, i) - halfπ; - return { - r: r, - a0: a0, - a1: a1, - p0: [ r * Math.cos(a0), r * Math.sin(a0) ], - p1: [ r * Math.cos(a1), r * Math.sin(a1) ] - }; - } - function equals(a, b) { - return a.a0 == b.a0 && a.a1 == b.a1; - } - function arc(r, p, a) { - return "A" + r + "," + r + " 0 " + +(a > π) + ",1 " + p; - } - function curve(r0, p0, r1, p1) { - return "Q 0,0 " + p1; - } - chord.radius = function(v) { - if (!arguments.length) return radius; - radius = d3_functor(v); - return chord; - }; - chord.source = function(v) { - if (!arguments.length) return source; - source = d3_functor(v); - return chord; - }; - chord.target = function(v) { - if (!arguments.length) return target; - target = d3_functor(v); - return chord; - }; - chord.startAngle = function(v) { - if (!arguments.length) return startAngle; - startAngle = d3_functor(v); - return chord; - }; - chord.endAngle = function(v) { - if (!arguments.length) return endAngle; - endAngle = d3_functor(v); - return chord; - }; - return chord; - }; - function d3_svg_chordRadius(d) { - return d.radius; - } - d3.svg.diagonal = function() { - var source = d3_source, target = d3_target, projection = d3_svg_diagonalProjection; - function diagonal(d, i) { - var p0 = source.call(this, d, i), p3 = target.call(this, d, i), m = (p0.y + p3.y) / 2, p = [ p0, { - x: p0.x, - y: m - }, { - x: p3.x, - y: m - }, p3 ]; - p = p.map(projection); - return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3]; - } - diagonal.source = function(x) { - if (!arguments.length) return source; - source = d3_functor(x); - return diagonal; - }; - diagonal.target = function(x) { - if (!arguments.length) return target; - target = d3_functor(x); - return diagonal; - }; - diagonal.projection = function(x) { - if (!arguments.length) return projection; - projection = x; - return diagonal; - }; - return diagonal; - }; - function d3_svg_diagonalProjection(d) { - return [ d.x, d.y ]; - } - d3.svg.diagonal.radial = function() { - var diagonal = d3.svg.diagonal(), projection = d3_svg_diagonalProjection, projection_ = diagonal.projection; - diagonal.projection = function(x) { - return arguments.length ? projection_(d3_svg_diagonalRadialProjection(projection = x)) : projection; - }; - return diagonal; - }; - function d3_svg_diagonalRadialProjection(projection) { - return function() { - var d = projection.apply(this, arguments), r = d[0], a = d[1] - halfπ; - return [ r * Math.cos(a), r * Math.sin(a) ]; - }; - } - d3.svg.symbol = function() { - var type = d3_svg_symbolType, size = d3_svg_symbolSize; - function symbol(d, i) { - return (d3_svg_symbols.get(type.call(this, d, i)) || d3_svg_symbolCircle)(size.call(this, d, i)); - } - symbol.type = function(x) { - if (!arguments.length) return type; - type = d3_functor(x); - return symbol; - }; - symbol.size = function(x) { - if (!arguments.length) return size; - size = d3_functor(x); - return symbol; - }; - return symbol; - }; - function d3_svg_symbolSize() { - return 64; - } - function d3_svg_symbolType() { - return "circle"; - } - function d3_svg_symbolCircle(size) { - var r = Math.sqrt(size / π); - return "M0," + r + "A" + r + "," + r + " 0 1,1 0," + -r + "A" + r + "," + r + " 0 1,1 0," + r + "Z"; - } - var d3_svg_symbols = d3.map({ - circle: d3_svg_symbolCircle, - cross: function(size) { - var r = Math.sqrt(size / 5) / 2; - return "M" + -3 * r + "," + -r + "H" + -r + "V" + -3 * r + "H" + r + "V" + -r + "H" + 3 * r + "V" + r + "H" + r + "V" + 3 * r + "H" + -r + "V" + r + "H" + -3 * r + "Z"; - }, - diamond: function(size) { - var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)), rx = ry * d3_svg_symbolTan30; - return "M0," + -ry + "L" + rx + ",0" + " 0," + ry + " " + -rx + ",0" + "Z"; - }, - square: function(size) { - var r = Math.sqrt(size) / 2; - return "M" + -r + "," + -r + "L" + r + "," + -r + " " + r + "," + r + " " + -r + "," + r + "Z"; - }, - "triangle-down": function(size) { - var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; - return "M0," + ry + "L" + rx + "," + -ry + " " + -rx + "," + -ry + "Z"; - }, - "triangle-up": function(size) { - var rx = Math.sqrt(size / d3_svg_symbolSqrt3), ry = rx * d3_svg_symbolSqrt3 / 2; - return "M0," + -ry + "L" + rx + "," + ry + " " + -rx + "," + ry + "Z"; - } - }); - d3.svg.symbolTypes = d3_svg_symbols.keys(); - var d3_svg_symbolSqrt3 = Math.sqrt(3), d3_svg_symbolTan30 = Math.tan(30 * d3_radians); - d3_selectionPrototype.transition = function(name) { - var id = d3_transitionInheritId || ++d3_transitionId, ns = d3_transitionNamespace(name), subgroups = [], subgroup, node, transition = d3_transitionInherit || { - time: Date.now(), - ease: d3_ease_cubicInOut, - delay: 0, - duration: 250 - }; - for (var j = -1, m = this.length; ++j < m; ) { - subgroups.push(subgroup = []); - for (var group = this[j], i = -1, n = group.length; ++i < n; ) { - if (node = group[i]) d3_transitionNode(node, i, ns, id, transition); - subgroup.push(node); - } - } - return d3_transition(subgroups, ns, id); - }; - d3_selectionPrototype.interrupt = function(name) { - return this.each(name == null ? d3_selection_interrupt : d3_selection_interruptNS(d3_transitionNamespace(name))); - }; - var d3_selection_interrupt = d3_selection_interruptNS(d3_transitionNamespace()); - function d3_selection_interruptNS(ns) { - return function() { - var lock, activeId, active; - if ((lock = this[ns]) && (active = lock[activeId = lock.active])) { - active.timer.c = null; - active.timer.t = NaN; - if (--lock.count) delete lock[activeId]; else delete this[ns]; - lock.active += .5; - active.event && active.event.interrupt.call(this, this.__data__, active.index); - } - }; - } - function d3_transition(groups, ns, id) { - d3_subclass(groups, d3_transitionPrototype); - groups.namespace = ns; - groups.id = id; - return groups; - } - var d3_transitionPrototype = [], d3_transitionId = 0, d3_transitionInheritId, d3_transitionInherit; - d3_transitionPrototype.call = d3_selectionPrototype.call; - d3_transitionPrototype.empty = d3_selectionPrototype.empty; - d3_transitionPrototype.node = d3_selectionPrototype.node; - d3_transitionPrototype.size = d3_selectionPrototype.size; - d3.transition = function(selection, name) { - return selection && selection.transition ? d3_transitionInheritId ? selection.transition(name) : selection : d3.selection().transition(selection); - }; - d3.transition.prototype = d3_transitionPrototype; - d3_transitionPrototype.select = function(selector) { - var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnode, node; - selector = d3_selection_selector(selector); - for (var j = -1, m = this.length; ++j < m; ) { - subgroups.push(subgroup = []); - for (var group = this[j], i = -1, n = group.length; ++i < n; ) { - if ((node = group[i]) && (subnode = selector.call(node, node.__data__, i, j))) { - if ("__data__" in node) subnode.__data__ = node.__data__; - d3_transitionNode(subnode, i, ns, id, node[ns][id]); - subgroup.push(subnode); - } else { - subgroup.push(null); - } - } - } - return d3_transition(subgroups, ns, id); - }; - d3_transitionPrototype.selectAll = function(selector) { - var id = this.id, ns = this.namespace, subgroups = [], subgroup, subnodes, node, subnode, transition; - selector = d3_selection_selectorAll(selector); - for (var j = -1, m = this.length; ++j < m; ) { - for (var group = this[j], i = -1, n = group.length; ++i < n; ) { - if (node = group[i]) { - transition = node[ns][id]; - subnodes = selector.call(node, node.__data__, i, j); - subgroups.push(subgroup = []); - for (var k = -1, o = subnodes.length; ++k < o; ) { - if (subnode = subnodes[k]) d3_transitionNode(subnode, k, ns, id, transition); - subgroup.push(subnode); - } - } - } - } - return d3_transition(subgroups, ns, id); - }; - d3_transitionPrototype.filter = function(filter) { - var subgroups = [], subgroup, group, node; - if (typeof filter !== "function") filter = d3_selection_filter(filter); - for (var j = 0, m = this.length; j < m; j++) { - subgroups.push(subgroup = []); - for (var group = this[j], i = 0, n = group.length; i < n; i++) { - if ((node = group[i]) && filter.call(node, node.__data__, i, j)) { - subgroup.push(node); - } - } - } - return d3_transition(subgroups, this.namespace, this.id); - }; - d3_transitionPrototype.tween = function(name, tween) { - var id = this.id, ns = this.namespace; - if (arguments.length < 2) return this.node()[ns][id].tween.get(name); - return d3_selection_each(this, tween == null ? function(node) { - node[ns][id].tween.remove(name); - } : function(node) { - node[ns][id].tween.set(name, tween); - }); - }; - function d3_transition_tween(groups, name, value, tween) { - var id = groups.id, ns = groups.namespace; - return d3_selection_each(groups, typeof value === "function" ? function(node, i, j) { - node[ns][id].tween.set(name, tween(value.call(node, node.__data__, i, j))); - } : (value = tween(value), function(node) { - node[ns][id].tween.set(name, value); - })); - } - d3_transitionPrototype.attr = function(nameNS, value) { - if (arguments.length < 2) { - for (value in nameNS) this.attr(value, nameNS[value]); - return this; - } - var interpolate = nameNS == "transform" ? d3_interpolateTransform : d3_interpolate, name = d3.ns.qualify(nameNS); - function attrNull() { - this.removeAttribute(name); - } - function attrNullNS() { - this.removeAttributeNS(name.space, name.local); - } - function attrTween(b) { - return b == null ? attrNull : (b += "", function() { - var a = this.getAttribute(name), i; - return a !== b && (i = interpolate(a, b), function(t) { - this.setAttribute(name, i(t)); - }); - }); - } - function attrTweenNS(b) { - return b == null ? attrNullNS : (b += "", function() { - var a = this.getAttributeNS(name.space, name.local), i; - return a !== b && (i = interpolate(a, b), function(t) { - this.setAttributeNS(name.space, name.local, i(t)); - }); - }); - } - return d3_transition_tween(this, "attr." + nameNS, value, name.local ? attrTweenNS : attrTween); - }; - d3_transitionPrototype.attrTween = function(nameNS, tween) { - var name = d3.ns.qualify(nameNS); - function attrTween(d, i) { - var f = tween.call(this, d, i, this.getAttribute(name)); - return f && function(t) { - this.setAttribute(name, f(t)); - }; - } - function attrTweenNS(d, i) { - var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local)); - return f && function(t) { - this.setAttributeNS(name.space, name.local, f(t)); - }; - } - return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween); - }; - d3_transitionPrototype.style = function(name, value, priority) { - var n = arguments.length; - if (n < 3) { - if (typeof name !== "string") { - if (n < 2) value = ""; - for (priority in name) this.style(priority, name[priority], value); - return this; - } - priority = ""; - } - function styleNull() { - this.style.removeProperty(name); - } - function styleString(b) { - return b == null ? styleNull : (b += "", function() { - var a = d3_window(this).getComputedStyle(this, null).getPropertyValue(name), i; - return a !== b && (i = d3_interpolate(a, b), function(t) { - this.style.setProperty(name, i(t), priority); - }); - }); - } - return d3_transition_tween(this, "style." + name, value, styleString); - }; - d3_transitionPrototype.styleTween = function(name, tween, priority) { - if (arguments.length < 3) priority = ""; - function styleTween(d, i) { - var f = tween.call(this, d, i, d3_window(this).getComputedStyle(this, null).getPropertyValue(name)); - return f && function(t) { - this.style.setProperty(name, f(t), priority); - }; - } - return this.tween("style." + name, styleTween); - }; - d3_transitionPrototype.text = function(value) { - return d3_transition_tween(this, "text", value, d3_transition_text); - }; - function d3_transition_text(b) { - if (b == null) b = ""; - return function() { - this.textContent = b; - }; - } - d3_transitionPrototype.remove = function() { - var ns = this.namespace; - return this.each("end.transition", function() { - var p; - if (this[ns].count < 2 && (p = this.parentNode)) p.removeChild(this); - }); - }; - d3_transitionPrototype.ease = function(value) { - var id = this.id, ns = this.namespace; - if (arguments.length < 1) return this.node()[ns][id].ease; - if (typeof value !== "function") value = d3.ease.apply(d3, arguments); - return d3_selection_each(this, function(node) { - node[ns][id].ease = value; - }); - }; - d3_transitionPrototype.delay = function(value) { - var id = this.id, ns = this.namespace; - if (arguments.length < 1) return this.node()[ns][id].delay; - return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { - node[ns][id].delay = +value.call(node, node.__data__, i, j); - } : (value = +value, function(node) { - node[ns][id].delay = value; - })); - }; - d3_transitionPrototype.duration = function(value) { - var id = this.id, ns = this.namespace; - if (arguments.length < 1) return this.node()[ns][id].duration; - return d3_selection_each(this, typeof value === "function" ? function(node, i, j) { - node[ns][id].duration = Math.max(1, value.call(node, node.__data__, i, j)); - } : (value = Math.max(1, value), function(node) { - node[ns][id].duration = value; - })); - }; - d3_transitionPrototype.each = function(type, listener) { - var id = this.id, ns = this.namespace; - if (arguments.length < 2) { - var inherit = d3_transitionInherit, inheritId = d3_transitionInheritId; - try { - d3_transitionInheritId = id; - d3_selection_each(this, function(node, i, j) { - d3_transitionInherit = node[ns][id]; - type.call(node, node.__data__, i, j); - }); - } finally { - d3_transitionInherit = inherit; - d3_transitionInheritId = inheritId; - } - } else { - d3_selection_each(this, function(node) { - var transition = node[ns][id]; - (transition.event || (transition.event = d3.dispatch("start", "end", "interrupt"))).on(type, listener); - }); - } - return this; - }; - d3_transitionPrototype.transition = function() { - var id0 = this.id, id1 = ++d3_transitionId, ns = this.namespace, subgroups = [], subgroup, group, node, transition; - for (var j = 0, m = this.length; j < m; j++) { - subgroups.push(subgroup = []); - for (var group = this[j], i = 0, n = group.length; i < n; i++) { - if (node = group[i]) { - transition = node[ns][id0]; - d3_transitionNode(node, i, ns, id1, { - time: transition.time, - ease: transition.ease, - delay: transition.delay + transition.duration, - duration: transition.duration - }); - } - subgroup.push(node); - } - } - return d3_transition(subgroups, ns, id1); - }; - function d3_transitionNamespace(name) { - return name == null ? "__transition__" : "__transition_" + name + "__"; - } - function d3_transitionNode(node, i, ns, id, inherit) { - var lock = node[ns] || (node[ns] = { - active: 0, - count: 0 - }), transition = lock[id], time, timer, duration, ease, tweens; - function schedule(elapsed) { - var delay = transition.delay; - timer.t = delay + time; - if (delay <= elapsed) return start(elapsed - delay); - timer.c = start; - } - function start(elapsed) { - var activeId = lock.active, active = lock[activeId]; - if (active) { - active.timer.c = null; - active.timer.t = NaN; - --lock.count; - delete lock[activeId]; - active.event && active.event.interrupt.call(node, node.__data__, active.index); - } - for (var cancelId in lock) { - if (+cancelId < id) { - var cancel = lock[cancelId]; - cancel.timer.c = null; - cancel.timer.t = NaN; - --lock.count; - delete lock[cancelId]; - } - } - timer.c = tick; - d3_timer(function() { - if (timer.c && tick(elapsed || 1)) { - timer.c = null; - timer.t = NaN; - } - return 1; - }, 0, time); - lock.active = id; - transition.event && transition.event.start.call(node, node.__data__, i); - tweens = []; - transition.tween.forEach(function(key, value) { - if (value = value.call(node, node.__data__, i)) { - tweens.push(value); - } - }); - ease = transition.ease; - duration = transition.duration; - } - function tick(elapsed) { - var t = elapsed / duration, e = ease(t), n = tweens.length; - while (n > 0) { - tweens[--n].call(node, e); - } - if (t >= 1) { - transition.event && transition.event.end.call(node, node.__data__, i); - if (--lock.count) delete lock[id]; else delete node[ns]; - return 1; - } - } - if (!transition) { - time = inherit.time; - timer = d3_timer(schedule, 0, time); - transition = lock[id] = { - tween: new d3_Map(), - time: time, - timer: timer, - delay: inherit.delay, - duration: inherit.duration, - ease: inherit.ease, - index: i - }; - inherit = null; - ++lock.count; - } - } - d3.svg.axis = function() { - var scale = d3.scale.linear(), orient = d3_svg_axisDefaultOrient, innerTickSize = 6, outerTickSize = 6, tickPadding = 3, tickArguments_ = [ 10 ], tickValues = null, tickFormat_; - function axis(g) { - g.each(function() { - var g = d3.select(this); - var scale0 = this.__chart__ || scale, scale1 = this.__chart__ = scale.copy(); - var ticks = tickValues == null ? scale1.ticks ? scale1.ticks.apply(scale1, tickArguments_) : scale1.domain() : tickValues, tickFormat = tickFormat_ == null ? scale1.tickFormat ? scale1.tickFormat.apply(scale1, tickArguments_) : d3_identity : tickFormat_, tick = g.selectAll(".tick").data(ticks, scale1), tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", ε), tickExit = d3.transition(tick.exit()).style("opacity", ε).remove(), tickUpdate = d3.transition(tick.order()).style("opacity", 1), tickSpacing = Math.max(innerTickSize, 0) + tickPadding, tickTransform; - var range = d3_scaleRange(scale1), path = g.selectAll(".domain").data([ 0 ]), pathUpdate = (path.enter().append("path").attr("class", "domain"), - d3.transition(path)); - tickEnter.append("line"); - tickEnter.append("text"); - var lineEnter = tickEnter.select("line"), lineUpdate = tickUpdate.select("line"), text = tick.select("text").text(tickFormat), textEnter = tickEnter.select("text"), textUpdate = tickUpdate.select("text"), sign = orient === "top" || orient === "left" ? -1 : 1, x1, x2, y1, y2; - if (orient === "bottom" || orient === "top") { - tickTransform = d3_svg_axisX, x1 = "x", y1 = "y", x2 = "x2", y2 = "y2"; - text.attr("dy", sign < 0 ? "0em" : ".71em").style("text-anchor", "middle"); - pathUpdate.attr("d", "M" + range[0] + "," + sign * outerTickSize + "V0H" + range[1] + "V" + sign * outerTickSize); - } else { - tickTransform = d3_svg_axisY, x1 = "y", y1 = "x", x2 = "y2", y2 = "x2"; - text.attr("dy", ".32em").style("text-anchor", sign < 0 ? "end" : "start"); - pathUpdate.attr("d", "M" + sign * outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + sign * outerTickSize); - } - lineEnter.attr(y2, sign * innerTickSize); - textEnter.attr(y1, sign * tickSpacing); - lineUpdate.attr(x2, 0).attr(y2, sign * innerTickSize); - textUpdate.attr(x1, 0).attr(y1, sign * tickSpacing); - if (scale1.rangeBand) { - var x = scale1, dx = x.rangeBand() / 2; - scale0 = scale1 = function(d) { - return x(d) + dx; - }; - } else if (scale0.rangeBand) { - scale0 = scale1; - } else { - tickExit.call(tickTransform, scale1, scale0); - } - tickEnter.call(tickTransform, scale0, scale1); - tickUpdate.call(tickTransform, scale1, scale1); - }); - } - axis.scale = function(x) { - if (!arguments.length) return scale; - scale = x; - return axis; - }; - axis.orient = function(x) { - if (!arguments.length) return orient; - orient = x in d3_svg_axisOrients ? x + "" : d3_svg_axisDefaultOrient; - return axis; - }; - axis.ticks = function() { - if (!arguments.length) return tickArguments_; - tickArguments_ = d3_array(arguments); - return axis; - }; - axis.tickValues = function(x) { - if (!arguments.length) return tickValues; - tickValues = x; - return axis; - }; - axis.tickFormat = function(x) { - if (!arguments.length) return tickFormat_; - tickFormat_ = x; - return axis; - }; - axis.tickSize = function(x) { - var n = arguments.length; - if (!n) return innerTickSize; - innerTickSize = +x; - outerTickSize = +arguments[n - 1]; - return axis; - }; - axis.innerTickSize = function(x) { - if (!arguments.length) return innerTickSize; - innerTickSize = +x; - return axis; - }; - axis.outerTickSize = function(x) { - if (!arguments.length) return outerTickSize; - outerTickSize = +x; - return axis; - }; - axis.tickPadding = function(x) { - if (!arguments.length) return tickPadding; - tickPadding = +x; - return axis; - }; - axis.tickSubdivide = function() { - return arguments.length && axis; - }; - return axis; - }; - var d3_svg_axisDefaultOrient = "bottom", d3_svg_axisOrients = { - top: 1, - right: 1, - bottom: 1, - left: 1 - }; - function d3_svg_axisX(selection, x0, x1) { - selection.attr("transform", function(d) { - var v0 = x0(d); - return "translate(" + (isFinite(v0) ? v0 : x1(d)) + ",0)"; - }); - } - function d3_svg_axisY(selection, y0, y1) { - selection.attr("transform", function(d) { - var v0 = y0(d); - return "translate(0," + (isFinite(v0) ? v0 : y1(d)) + ")"; - }); - } - d3.svg.brush = function() { - var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"), x = null, y = null, xExtent = [ 0, 0 ], yExtent = [ 0, 0 ], xExtentDomain, yExtentDomain, xClamp = true, yClamp = true, resizes = d3_svg_brushResizes[0]; - function brush(g) { - g.each(function() { - var g = d3.select(this).style("pointer-events", "all").style("-webkit-tap-highlight-color", "rgba(0,0,0,0)").on("mousedown.brush", brushstart).on("touchstart.brush", brushstart); - var background = g.selectAll(".background").data([ 0 ]); - background.enter().append("rect").attr("class", "background").style("visibility", "hidden").style("cursor", "crosshair"); - g.selectAll(".extent").data([ 0 ]).enter().append("rect").attr("class", "extent").style("cursor", "move"); - var resize = g.selectAll(".resize").data(resizes, d3_identity); - resize.exit().remove(); - resize.enter().append("g").attr("class", function(d) { - return "resize " + d; - }).style("cursor", function(d) { - return d3_svg_brushCursor[d]; - }).append("rect").attr("x", function(d) { - return /[ew]$/.test(d) ? -3 : null; - }).attr("y", function(d) { - return /^[ns]/.test(d) ? -3 : null; - }).attr("width", 6).attr("height", 6).style("visibility", "hidden"); - resize.style("display", brush.empty() ? "none" : null); - var gUpdate = d3.transition(g), backgroundUpdate = d3.transition(background), range; - if (x) { - range = d3_scaleRange(x); - backgroundUpdate.attr("x", range[0]).attr("width", range[1] - range[0]); - redrawX(gUpdate); - } - if (y) { - range = d3_scaleRange(y); - backgroundUpdate.attr("y", range[0]).attr("height", range[1] - range[0]); - redrawY(gUpdate); - } - redraw(gUpdate); - }); - } - brush.event = function(g) { - g.each(function() { - var event_ = event.of(this, arguments), extent1 = { - x: xExtent, - y: yExtent, - i: xExtentDomain, - j: yExtentDomain - }, extent0 = this.__chart__ || extent1; - this.__chart__ = extent1; - if (d3_transitionInheritId) { - d3.select(this).transition().each("start.brush", function() { - xExtentDomain = extent0.i; - yExtentDomain = extent0.j; - xExtent = extent0.x; - yExtent = extent0.y; - event_({ - type: "brushstart" - }); - }).tween("brush:brush", function() { - var xi = d3_interpolateArray(xExtent, extent1.x), yi = d3_interpolateArray(yExtent, extent1.y); - xExtentDomain = yExtentDomain = null; - return function(t) { - xExtent = extent1.x = xi(t); - yExtent = extent1.y = yi(t); - event_({ - type: "brush", - mode: "resize" - }); - }; - }).each("end.brush", function() { - xExtentDomain = extent1.i; - yExtentDomain = extent1.j; - event_({ - type: "brush", - mode: "resize" - }); - event_({ - type: "brushend" - }); - }); - } else { - event_({ - type: "brushstart" - }); - event_({ - type: "brush", - mode: "resize" - }); - event_({ - type: "brushend" - }); - } - }); - }; - function redraw(g) { - g.selectAll(".resize").attr("transform", function(d) { - return "translate(" + xExtent[+/e$/.test(d)] + "," + yExtent[+/^s/.test(d)] + ")"; - }); - } - function redrawX(g) { - g.select(".extent").attr("x", xExtent[0]); - g.selectAll(".extent,.n>rect,.s>rect").attr("width", xExtent[1] - xExtent[0]); - } - function redrawY(g) { - g.select(".extent").attr("y", yExtent[0]); - g.selectAll(".extent,.e>rect,.w>rect").attr("height", yExtent[1] - yExtent[0]); - } - function brushstart() { - var target = this, eventTarget = d3.select(d3.event.target), event_ = event.of(target, arguments), g = d3.select(target), resizing = eventTarget.datum(), resizingX = !/^(n|s)$/.test(resizing) && x, resizingY = !/^(e|w)$/.test(resizing) && y, dragging = eventTarget.classed("extent"), dragRestore = d3_event_dragSuppress(target), center, origin = d3.mouse(target), offset; - var w = d3.select(d3_window(target)).on("keydown.brush", keydown).on("keyup.brush", keyup); - if (d3.event.changedTouches) { - w.on("touchmove.brush", brushmove).on("touchend.brush", brushend); - } else { - w.on("mousemove.brush", brushmove).on("mouseup.brush", brushend); - } - g.interrupt().selectAll("*").interrupt(); - if (dragging) { - origin[0] = xExtent[0] - origin[0]; - origin[1] = yExtent[0] - origin[1]; - } else if (resizing) { - var ex = +/w$/.test(resizing), ey = +/^n/.test(resizing); - offset = [ xExtent[1 - ex] - origin[0], yExtent[1 - ey] - origin[1] ]; - origin[0] = xExtent[ex]; - origin[1] = yExtent[ey]; - } else if (d3.event.altKey) center = origin.slice(); - g.style("pointer-events", "none").selectAll(".resize").style("display", null); - d3.select("body").style("cursor", eventTarget.style("cursor")); - event_({ - type: "brushstart" - }); - brushmove(); - function keydown() { - if (d3.event.keyCode == 32) { - if (!dragging) { - center = null; - origin[0] -= xExtent[1]; - origin[1] -= yExtent[1]; - dragging = 2; - } - d3_eventPreventDefault(); - } - } - function keyup() { - if (d3.event.keyCode == 32 && dragging == 2) { - origin[0] += xExtent[1]; - origin[1] += yExtent[1]; - dragging = 0; - d3_eventPreventDefault(); - } - } - function brushmove() { - var point = d3.mouse(target), moved = false; - if (offset) { - point[0] += offset[0]; - point[1] += offset[1]; - } - if (!dragging) { - if (d3.event.altKey) { - if (!center) center = [ (xExtent[0] + xExtent[1]) / 2, (yExtent[0] + yExtent[1]) / 2 ]; - origin[0] = xExtent[+(point[0] < center[0])]; - origin[1] = yExtent[+(point[1] < center[1])]; - } else center = null; - } - if (resizingX && move1(point, x, 0)) { - redrawX(g); - moved = true; - } - if (resizingY && move1(point, y, 1)) { - redrawY(g); - moved = true; - } - if (moved) { - redraw(g); - event_({ - type: "brush", - mode: dragging ? "move" : "resize" - }); - } - } - function move1(point, scale, i) { - var range = d3_scaleRange(scale), r0 = range[0], r1 = range[1], position = origin[i], extent = i ? yExtent : xExtent, size = extent[1] - extent[0], min, max; - if (dragging) { - r0 -= position; - r1 -= size + position; - } - min = (i ? yClamp : xClamp) ? Math.max(r0, Math.min(r1, point[i])) : point[i]; - if (dragging) { - max = (min += position) + size; - } else { - if (center) position = Math.max(r0, Math.min(r1, 2 * center[i] - min)); - if (position < min) { - max = min; - min = position; - } else { - max = position; - } - } - if (extent[0] != min || extent[1] != max) { - if (i) yExtentDomain = null; else xExtentDomain = null; - extent[0] = min; - extent[1] = max; - return true; - } - } - function brushend() { - brushmove(); - g.style("pointer-events", "all").selectAll(".resize").style("display", brush.empty() ? "none" : null); - d3.select("body").style("cursor", null); - w.on("mousemove.brush", null).on("mouseup.brush", null).on("touchmove.brush", null).on("touchend.brush", null).on("keydown.brush", null).on("keyup.brush", null); - dragRestore(); - event_({ - type: "brushend" - }); - } - } - brush.x = function(z) { - if (!arguments.length) return x; - x = z; - resizes = d3_svg_brushResizes[!x << 1 | !y]; - return brush; - }; - brush.y = function(z) { - if (!arguments.length) return y; - y = z; - resizes = d3_svg_brushResizes[!x << 1 | !y]; - return brush; - }; - brush.clamp = function(z) { - if (!arguments.length) return x && y ? [ xClamp, yClamp ] : x ? xClamp : y ? yClamp : null; - if (x && y) xClamp = !!z[0], yClamp = !!z[1]; else if (x) xClamp = !!z; else if (y) yClamp = !!z; - return brush; - }; - brush.extent = function(z) { - var x0, x1, y0, y1, t; - if (!arguments.length) { - if (x) { - if (xExtentDomain) { - x0 = xExtentDomain[0], x1 = xExtentDomain[1]; - } else { - x0 = xExtent[0], x1 = xExtent[1]; - if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1); - if (x1 < x0) t = x0, x0 = x1, x1 = t; - } - } - if (y) { - if (yExtentDomain) { - y0 = yExtentDomain[0], y1 = yExtentDomain[1]; - } else { - y0 = yExtent[0], y1 = yExtent[1]; - if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1); - if (y1 < y0) t = y0, y0 = y1, y1 = t; - } - } - return x && y ? [ [ x0, y0 ], [ x1, y1 ] ] : x ? [ x0, x1 ] : y && [ y0, y1 ]; - } - if (x) { - x0 = z[0], x1 = z[1]; - if (y) x0 = x0[0], x1 = x1[0]; - xExtentDomain = [ x0, x1 ]; - if (x.invert) x0 = x(x0), x1 = x(x1); - if (x1 < x0) t = x0, x0 = x1, x1 = t; - if (x0 != xExtent[0] || x1 != xExtent[1]) xExtent = [ x0, x1 ]; - } - if (y) { - y0 = z[0], y1 = z[1]; - if (x) y0 = y0[1], y1 = y1[1]; - yExtentDomain = [ y0, y1 ]; - if (y.invert) y0 = y(y0), y1 = y(y1); - if (y1 < y0) t = y0, y0 = y1, y1 = t; - if (y0 != yExtent[0] || y1 != yExtent[1]) yExtent = [ y0, y1 ]; - } - return brush; - }; - brush.clear = function() { - if (!brush.empty()) { - xExtent = [ 0, 0 ], yExtent = [ 0, 0 ]; - xExtentDomain = yExtentDomain = null; - } - return brush; - }; - brush.empty = function() { - return !!x && xExtent[0] == xExtent[1] || !!y && yExtent[0] == yExtent[1]; - }; - return d3.rebind(brush, event, "on"); - }; - var d3_svg_brushCursor = { - n: "ns-resize", - e: "ew-resize", - s: "ns-resize", - w: "ew-resize", - nw: "nwse-resize", - ne: "nesw-resize", - se: "nwse-resize", - sw: "nesw-resize" - }; - var d3_svg_brushResizes = [ [ "n", "e", "s", "w", "nw", "ne", "se", "sw" ], [ "e", "w" ], [ "n", "s" ], [] ]; - var d3_time_format = d3_time.format = d3_locale_enUS.timeFormat; - var d3_time_formatUtc = d3_time_format.utc; - var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ"); - d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z") ? d3_time_formatIsoNative : d3_time_formatIso; - function d3_time_formatIsoNative(date) { - return date.toISOString(); - } - d3_time_formatIsoNative.parse = function(string) { - var date = new Date(string); - return isNaN(date) ? null : date; - }; - d3_time_formatIsoNative.toString = d3_time_formatIso.toString; - d3_time.second = d3_time_interval(function(date) { - return new d3_date(Math.floor(date / 1e3) * 1e3); - }, function(date, offset) { - date.setTime(date.getTime() + Math.floor(offset) * 1e3); - }, function(date) { - return date.getSeconds(); - }); - d3_time.seconds = d3_time.second.range; - d3_time.seconds.utc = d3_time.second.utc.range; - d3_time.minute = d3_time_interval(function(date) { - return new d3_date(Math.floor(date / 6e4) * 6e4); - }, function(date, offset) { - date.setTime(date.getTime() + Math.floor(offset) * 6e4); - }, function(date) { - return date.getMinutes(); - }); - d3_time.minutes = d3_time.minute.range; - d3_time.minutes.utc = d3_time.minute.utc.range; - d3_time.hour = d3_time_interval(function(date) { - var timezone = date.getTimezoneOffset() / 60; - return new d3_date((Math.floor(date / 36e5 - timezone) + timezone) * 36e5); - }, function(date, offset) { - date.setTime(date.getTime() + Math.floor(offset) * 36e5); - }, function(date) { - return date.getHours(); - }); - d3_time.hours = d3_time.hour.range; - d3_time.hours.utc = d3_time.hour.utc.range; - d3_time.month = d3_time_interval(function(date) { - date = d3_time.day(date); - date.setDate(1); - return date; - }, function(date, offset) { - date.setMonth(date.getMonth() + offset); - }, function(date) { - return date.getMonth(); - }); - d3_time.months = d3_time.month.range; - d3_time.months.utc = d3_time.month.utc.range; - function d3_time_scale(linear, methods, format) { - function scale(x) { - return linear(x); - } - scale.invert = function(x) { - return d3_time_scaleDate(linear.invert(x)); - }; - scale.domain = function(x) { - if (!arguments.length) return linear.domain().map(d3_time_scaleDate); - linear.domain(x); - return scale; - }; - function tickMethod(extent, count) { - var span = extent[1] - extent[0], target = span / count, i = d3.bisect(d3_time_scaleSteps, target); - return i == d3_time_scaleSteps.length ? [ methods.year, d3_scale_linearTickRange(extent.map(function(d) { - return d / 31536e6; - }), count)[2] ] : !i ? [ d3_time_scaleMilliseconds, d3_scale_linearTickRange(extent, count)[2] ] : methods[target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target ? i - 1 : i]; - } - scale.nice = function(interval, skip) { - var domain = scale.domain(), extent = d3_scaleExtent(domain), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" && tickMethod(extent, interval); - if (method) interval = method[0], skip = method[1]; - function skipped(date) { - return !isNaN(date) && !interval.range(date, d3_time_scaleDate(+date + 1), skip).length; - } - return scale.domain(d3_scale_nice(domain, skip > 1 ? { - floor: function(date) { - while (skipped(date = interval.floor(date))) date = d3_time_scaleDate(date - 1); - return date; - }, - ceil: function(date) { - while (skipped(date = interval.ceil(date))) date = d3_time_scaleDate(+date + 1); - return date; - } - } : interval)); - }; - scale.ticks = function(interval, skip) { - var extent = d3_scaleExtent(scale.domain()), method = interval == null ? tickMethod(extent, 10) : typeof interval === "number" ? tickMethod(extent, interval) : !interval.range && [ { - range: interval - }, skip ]; - if (method) interval = method[0], skip = method[1]; - return interval.range(extent[0], d3_time_scaleDate(+extent[1] + 1), skip < 1 ? 1 : skip); - }; - scale.tickFormat = function() { - return format; - }; - scale.copy = function() { - return d3_time_scale(linear.copy(), methods, format); - }; - return d3_scale_linearRebind(scale, linear); - } - function d3_time_scaleDate(t) { - return new Date(t); - } - var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ]; - var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ]; - var d3_time_scaleLocalFormat = d3_time_format.multi([ [ ".%L", function(d) { - return d.getMilliseconds(); - } ], [ ":%S", function(d) { - return d.getSeconds(); - } ], [ "%I:%M", function(d) { - return d.getMinutes(); - } ], [ "%I %p", function(d) { - return d.getHours(); - } ], [ "%a %d", function(d) { - return d.getDay() && d.getDate() != 1; - } ], [ "%b %d", function(d) { - return d.getDate() != 1; - } ], [ "%B", function(d) { - return d.getMonth(); - } ], [ "%Y", d3_true ] ]); - var d3_time_scaleMilliseconds = { - range: function(start, stop, step) { - return d3.range(Math.ceil(start / step) * step, +stop, step).map(d3_time_scaleDate); - }, - floor: d3_identity, - ceil: d3_identity - }; - d3_time_scaleLocalMethods.year = d3_time.year; - d3_time.scale = function() { - return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat); - }; - var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) { - return [ m[0].utc, m[1] ]; - }); - var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([ [ ".%L", function(d) { - return d.getUTCMilliseconds(); - } ], [ ":%S", function(d) { - return d.getUTCSeconds(); - } ], [ "%I:%M", function(d) { - return d.getUTCMinutes(); - } ], [ "%I %p", function(d) { - return d.getUTCHours(); - } ], [ "%a %d", function(d) { - return d.getUTCDay() && d.getUTCDate() != 1; - } ], [ "%b %d", function(d) { - return d.getUTCDate() != 1; - } ], [ "%B", function(d) { - return d.getUTCMonth(); - } ], [ "%Y", d3_true ] ]); - d3_time_scaleUtcMethods.year = d3_time.year.utc; - d3_time.scale.utc = function() { - return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat); - }; - d3.text = d3_xhrType(function(request) { - return request.responseText; - }); - d3.json = function(url, callback) { - return d3_xhr(url, "application/json", d3_json, callback); - }; - function d3_json(request) { - return JSON.parse(request.responseText); - } - d3.html = function(url, callback) { - return d3_xhr(url, "text/html", d3_html, callback); - }; - function d3_html(request) { - var range = d3_document.createRange(); - range.selectNode(d3_document.body); - return range.createContextualFragment(request.responseText); - } - d3.xml = d3_xhrType(function(request) { - return request.responseXML; - }); - if (typeof define === "function" && define.amd) this.d3 = d3, define(d3); else if (typeof module === "object" && module.exports) module.exports = d3; else this.d3 = d3; -}(); -},{}],174:[function(require,module,exports){ -arguments[4][167][0].apply(exports,arguments) -},{"dup":167,"robust-orientation":1127,"simplicial-complex":177}],175:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],176:[function(require,module,exports){ -arguments[4][169][0].apply(exports,arguments) -},{"dup":169}],177:[function(require,module,exports){ -arguments[4][170][0].apply(exports,arguments) -},{"bit-twiddle":175,"dup":170,"union-find":176}],178:[function(require,module,exports){ -arguments[4][76][0].apply(exports,arguments) -},{"dup":76}],179:[function(require,module,exports){ -"use strict" - -var ch = require("incremental-convex-hull") -var uniq = require("uniq") - -module.exports = triangulate - -function LiftedPoint(p, i) { - this.point = p - this.index = i -} - -function compareLifted(a, b) { - var ap = a.point - var bp = b.point - var d = ap.length - for(var i=0; i= 2) { - return false - } - } - cell[j] = v - } - return true - }) - } else { - hull = hull.filter(function(cell) { - for(var i=0; i<=d; ++i) { - var v = dindex[cell[i]] - if(v < 0) { - return false - } - cell[i] = v - } - return true - }) - } - - if(d & 1) { - for(var i=0; i postsJSON - values[1] // => commentsJSON - - return values; - }); - ``` - - @class Promise - @param {function} resolver - Useful for tooling. - @constructor -*/ -function Promise(resolver) { - this[PROMISE_ID] = nextId(); - this._result = this._state = undefined; - this._subscribers = []; - - if (noop !== resolver) { - typeof resolver !== 'function' && needsResolver(); - this instanceof Promise ? initializePromise(this, resolver) : needsNew(); - } -} - -Promise.all = all; -Promise.race = race; -Promise.resolve = resolve; -Promise.reject = reject; -Promise._setScheduler = setScheduler; -Promise._setAsap = setAsap; -Promise._asap = asap; - -Promise.prototype = { - constructor: Promise, - - /** - The primary way of interacting with a promise is through its `then` method, - which registers callbacks to receive either a promise's eventual value or the - reason why the promise cannot be fulfilled. - - ```js - findUser().then(function(user){ - // user is available - }, function(reason){ - // user is unavailable, and you are given the reason why - }); - ``` - - Chaining - -------- - - The return value of `then` is itself a promise. This second, 'downstream' - promise is resolved with the return value of the first promise's fulfillment - or rejection handler, or rejected if the handler throws an exception. - - ```js - findUser().then(function (user) { - return user.name; - }, function (reason) { - return 'default name'; - }).then(function (userName) { - // If `findUser` fulfilled, `userName` will be the user's name, otherwise it - // will be `'default name'` - }); - - findUser().then(function (user) { - throw new Error('Found user, but still unhappy'); - }, function (reason) { - throw new Error('`findUser` rejected and we're unhappy'); - }).then(function (value) { - // never reached - }, function (reason) { - // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'. - // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'. - }); - ``` - If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream. - - ```js - findUser().then(function (user) { - throw new PedagogicalException('Upstream error'); - }).then(function (value) { - // never reached - }).then(function (value) { - // never reached - }, function (reason) { - // The `PedgagocialException` is propagated all the way down to here - }); - ``` - - Assimilation - ------------ - - Sometimes the value you want to propagate to a downstream promise can only be - retrieved asynchronously. This can be achieved by returning a promise in the - fulfillment or rejection handler. The downstream promise will then be pending - until the returned promise is settled. This is called *assimilation*. - - ```js - findUser().then(function (user) { - return findCommentsByAuthor(user); - }).then(function (comments) { - // The user's comments are now available - }); - ``` - - If the assimliated promise rejects, then the downstream promise will also reject. - - ```js - findUser().then(function (user) { - return findCommentsByAuthor(user); - }).then(function (comments) { - // If `findCommentsByAuthor` fulfills, we'll have the value here - }, function (reason) { - // If `findCommentsByAuthor` rejects, we'll have the reason here - }); - ``` - - Simple Example - -------------- - - Synchronous Example - - ```javascript - let result; - - try { - result = findResult(); - // success - } catch(reason) { - // failure - } - ``` - - Errback Example - - ```js - findResult(function(result, err){ - if (err) { - // failure - } else { - // success - } - }); - ``` - - Promise Example; - - ```javascript - findResult().then(function(result){ - // success - }, function(reason){ - // failure - }); - ``` - - Advanced Example - -------------- - - Synchronous Example - - ```javascript - let author, books; - - try { - author = findAuthor(); - books = findBooksByAuthor(author); - // success - } catch(reason) { - // failure - } - ``` - - Errback Example - - ```js - - function foundBooks(books) { - - } - - function failure(reason) { - - } - - findAuthor(function(author, err){ - if (err) { - failure(err); - // failure - } else { - try { - findBoooksByAuthor(author, function(books, err) { - if (err) { - failure(err); - } else { - try { - foundBooks(books); - } catch(reason) { - failure(reason); - } - } - }); - } catch(error) { - failure(err); - } - // success - } - }); - ``` - - Promise Example; - - ```javascript - findAuthor(). - then(findBooksByAuthor). - then(function(books){ - // found books - }).catch(function(reason){ - // something went wrong - }); - ``` - - @method then - @param {Function} onFulfilled - @param {Function} onRejected - Useful for tooling. - @return {Promise} - */ - then: then, - - /** - `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same - as the catch block of a try/catch statement. - - ```js - function findAuthor(){ - throw new Error('couldn't find that author'); - } - - // synchronous - try { - findAuthor(); - } catch(reason) { - // something went wrong - } - - // async with promises - findAuthor().catch(function(reason){ - // something went wrong - }); - ``` - - @method catch - @param {Function} onRejection - Useful for tooling. - @return {Promise} - */ - 'catch': function _catch(onRejection) { - return this.then(null, onRejection); - } -}; - -function polyfill() { - var local = undefined; - - if (typeof global !== 'undefined') { - local = global; - } else if (typeof self !== 'undefined') { - local = self; - } else { - try { - local = Function('return this')(); - } catch (e) { - throw new Error('polyfill failed because global object is unavailable in this environment'); - } - } - - var P = local.Promise; - - if (P) { - var promiseToString = null; - try { - promiseToString = Object.prototype.toString.call(P.resolve()); - } catch (e) { - // silently ignored - } - - if (promiseToString === '[object Promise]' && !P.cast) { - return; - } - } - - local.Promise = Promise; -} - -polyfill(); -// Strange compat.. -Promise.polyfill = polyfill; -Promise.Promise = Promise; - -return Promise; - -}))); - -}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{"_process":41}],181:[function(require,module,exports){ -/** - * inspired by is-number - * but significantly simplified and sped up by ignoring number and string constructors - * ie these return false: - * new Number(1) - * new String('1') - */ - -'use strict'; - -/** - * Is this string all whitespace? - * This solution kind of makes my brain hurt, but it's significantly faster - * than !str.trim() or any other solution I could find. - * - * whitespace codes from: http://en.wikipedia.org/wiki/Whitespace_character - * and verified with: - * - * for(var i = 0; i < 65536; i++) { - * var s = String.fromCharCode(i); - * if(+s===0 && !s.trim()) console.log(i, s); - * } - * - * which counts a couple of these as *not* whitespace, but finds nothing else - * that *is* whitespace. Note that charCodeAt stops at 16 bits, but it appears - * that there are no whitespace characters above this, and code points above - * this do not map onto white space characters. - */ -function allBlankCharCodes(str){ - var l = str.length, - a; - for(var i = 0; i < l; i++) { - a = str.charCodeAt(i); - if((a < 9 || a > 13) && (a !== 32) && (a !== 133) && (a !== 160) && - (a !== 5760) && (a !== 6158) && (a < 8192 || a > 8205) && - (a !== 8232) && (a !== 8233) && (a !== 8239) && (a !== 8287) && - (a !== 8288) && (a !== 12288) && (a !== 65279)) { - return false; - } - } - return true; -} - -module.exports = function(n) { - var type = typeof n; - if(type === 'string') { - var original = n; - n = +n; - // whitespace strings cast to zero - filter them out - if(n===0 && allBlankCharCodes(original)) return false; - } - else if(type !== 'number') return false; - - return n - n < 1; -}; - -},{}],182:[function(require,module,exports){ -'use strict' - -var createShader = require('gl-shader') -var createBuffer = require('gl-buffer') -var pool = require('typedarray-pool') -var shaders = require('./lib/shaders') - -module.exports = createError2D - -var WEIGHTS = [ - // x-error bar - [1, 0, 0, 1, 0, 0], - [1, 0, 0, -1, 0, 0], - [-1, 0, 0, -1, 0, 0], - - [-1, 0, 0, -1, 0, 0], - [-1, 0, 0, 1, 0, 0], - [1, 0, 0, 1, 0, 0], - - // x-error right cap - [1, 0, -1, 0, 0, 1], - [1, 0, -1, 0, 0, -1], - [1, 0, 1, 0, 0, -1], - - [1, 0, 1, 0, 0, -1], - [1, 0, 1, 0, 0, 1], - [1, 0, -1, 0, 0, 1], - - // x-error left cap - [-1, 0, -1, 0, 0, 1], - [-1, 0, -1, 0, 0, -1], - [-1, 0, 1, 0, 0, -1], - - [-1, 0, 1, 0, 0, -1], - [-1, 0, 1, 0, 0, 1], - [-1, 0, -1, 0, 0, 1], - - // y-error bar - [0, 1, 1, 0, 0, 0], - [0, 1, -1, 0, 0, 0], - [0, -1, -1, 0, 0, 0], - - [0, -1, -1, 0, 0, 0], - [0, 1, 1, 0, 0, 0], - [0, -1, 1, 0, 0, 0], - - // y-error top cap - [0, 1, 0, -1, 1, 0], - [0, 1, 0, -1, -1, 0], - [0, 1, 0, 1, -1, 0], - - [0, 1, 0, 1, 1, 0], - [0, 1, 0, -1, 1, 0], - [0, 1, 0, 1, -1, 0], - - // y-error bottom cap - [0, -1, 0, -1, 1, 0], - [0, -1, 0, -1, -1, 0], - [0, -1, 0, 1, -1, 0], - - [0, -1, 0, 1, 1, 0], - [0, -1, 0, -1, 1, 0], - [0, -1, 0, 1, -1, 0] -] - -function GLError2D (plot, shader, bufferHi, bufferLo) { - this.plot = plot - - this.shader = shader - this.bufferHi = bufferHi - this.bufferLo = bufferLo - - this.bounds = [Infinity, Infinity, -Infinity, -Infinity] - - this.numPoints = 0 - - this.color = [0, 0, 0, 1] -} - -var proto = GLError2D.prototype - -proto.draw = (function () { - var SCALE_HI = new Float32Array([0, 0]) - var SCALE_LO = new Float32Array([0, 0]) - var TRANSLATE_HI = new Float32Array([0, 0]) - var TRANSLATE_LO = new Float32Array([0, 0]) - - var PIXEL_SCALE = [1, 1] - - return function () { - var plot = this.plot - var shader = this.shader - var bounds = this.bounds - var numPoints = this.numPoints - - if (!numPoints) { - return - } - - var gl = plot.gl - var dataBox = plot.dataBox - var viewBox = plot.viewBox - var pixelRatio = plot.pixelRatio - - var boundX = bounds[2] - bounds[0] - var boundY = bounds[3] - bounds[1] - var dataX = dataBox[2] - dataBox[0] - var dataY = dataBox[3] - dataBox[1] - - var scaleX = 2 * boundX / dataX - var scaleY = 2 * boundY / dataY - var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX - var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY - - SCALE_HI[0] = scaleX - SCALE_HI[1] = scaleY - SCALE_LO[0] = scaleX - SCALE_HI[0] - SCALE_LO[1] = scaleY - SCALE_HI[1] - TRANSLATE_HI[0] = translateX - TRANSLATE_HI[1] = translateY - TRANSLATE_LO[0] = translateX - TRANSLATE_HI[0] - TRANSLATE_LO[1] = translateY - TRANSLATE_HI[1] - - var screenX = viewBox[2] - viewBox[0] - var screenY = viewBox[3] - viewBox[1] - - PIXEL_SCALE[0] = 2.0 * pixelRatio / screenX - PIXEL_SCALE[1] = 2.0 * pixelRatio / screenY - - shader.bind() - - shader.uniforms.scaleHi = SCALE_HI - shader.uniforms.scaleLo = SCALE_LO - shader.uniforms.translateHi = TRANSLATE_HI - shader.uniforms.translateLo = TRANSLATE_LO - shader.uniforms.pixelScale = PIXEL_SCALE - shader.uniforms.color = this.color - - this.bufferLo.bind() - shader.attributes.positionLo.pointer(gl.FLOAT, false, 16, 0) - - this.bufferHi.bind() - shader.attributes.positionHi.pointer(gl.FLOAT, false, 16, 0) - - shader.attributes.pixelOffset.pointer(gl.FLOAT, false, 16, 8) - - gl.drawArrays(gl.TRIANGLES, 0, numPoints * WEIGHTS.length) - } -})() - -proto.drawPick = function (offset) { return offset } -proto.pick = function () { - return null -} - -proto.update = function (options) { - options = options || {} - - var i, x, y - - var positions = options.positions || [] - var errors = options.errors || [] - - var lineWidth = 1 - if ('lineWidth' in options) { - lineWidth = +options.lineWidth - } - - var capSize = 5 - if ('capSize' in options) { - capSize = +options.capSize - } - - this.color = (options.color || [0, 0, 0, 1]).slice() - - var bounds = this.bounds = [Infinity, Infinity, -Infinity, -Infinity] - - var numPoints = this.numPoints = positions.length >> 1 - for (i = 0; i < numPoints; ++i) { - x = positions[i * 2] - y = positions[i * 2 + 1] - - bounds[0] = Math.min(x, bounds[0]) - bounds[1] = Math.min(y, bounds[1]) - bounds[2] = Math.max(x, bounds[2]) - bounds[3] = Math.max(y, bounds[3]) - } - if (bounds[2] === bounds[0]) { - bounds[2] += 1 - } - if (bounds[3] === bounds[1]) { - bounds[3] += 1 - } - var sx = 1.0 / (bounds[2] - bounds[0]) - var sy = 1.0 / (bounds[3] - bounds[1]) - var tx = bounds[0] - var ty = bounds[1] - - var bufferData = pool.mallocFloat64(numPoints * WEIGHTS.length * 4) - var bufferDataHi = pool.mallocFloat32(numPoints * WEIGHTS.length * 4) - var bufferDataLo = pool.mallocFloat32(numPoints * WEIGHTS.length * 4) - var ptr = 0 - for (i = 0; i < numPoints; ++i) { - x = positions[2 * i] - y = positions[2 * i + 1] - var ex0 = errors[4 * i] - var ex1 = errors[4 * i + 1] - var ey0 = errors[4 * i + 2] - var ey1 = errors[4 * i + 3] - - for (var j = 0; j < WEIGHTS.length; ++j) { - var w = WEIGHTS[j] - - var dx = w[0] - var dy = w[1] - - if (dx < 0) { - dx *= ex0 - } else if (dx > 0) { - dx *= ex1 - } - - if (dy < 0) { - dy *= ey0 - } else if (dy > 0) { - dy *= ey1 - } - - bufferData[ptr++] = sx * ((x - tx) + dx) - bufferData[ptr++] = sy * ((y - ty) + dy) - bufferData[ptr++] = lineWidth * w[2] + (capSize + lineWidth) * w[4] - bufferData[ptr++] = lineWidth * w[3] + (capSize + lineWidth) * w[5] - } - } - for(i = 0; i < bufferData.length; i++) { - bufferDataHi[i] = bufferData[i] - bufferDataLo[i] = bufferData[i] - bufferDataHi[i] - } - this.bufferHi.update(bufferDataHi) - this.bufferLo.update(bufferDataLo) - pool.free(bufferData) -} - -proto.dispose = function () { - this.plot.removeObject(this) - this.shader.dispose() - this.bufferHi.dispose() - this.bufferLo.dispose() -} - -function createError2D (plot, options) { - var shader = createShader(plot.gl, shaders.vertex, shaders.fragment) - var bufferHi = createBuffer(plot.gl) - var bufferLo = createBuffer(plot.gl) - - var errorBars = new GLError2D(plot, shader, bufferHi, bufferLo) - - errorBars.update(options) - - plot.addObject(errorBars) - - return errorBars -} - -},{"./lib/shaders":183,"gl-buffer":184,"gl-shader":835,"typedarray-pool":187}],183:[function(require,module,exports){ - - -module.exports = { - vertex: "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi;\nattribute vec2 positionLo;\nattribute vec2 pixelOffset;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale;\n\nvec2 project(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\nvoid main() {\n vec3 scrPosition = vec3(\n project(scaleHi, translateHi, scaleLo, translateLo, positionHi, positionLo),\n 1);\n gl_Position = vec4(\n scrPosition.xy + scrPosition.z * pixelScale * pixelOffset,\n 0,\n scrPosition.z);\n}\n", - fragment: "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color;\n\nvoid main() {\n gl_FragColor = vec4(color.rgb * color.a, color.a);\n}\n" -} - -},{}],184:[function(require,module,exports){ -"use strict" - -var pool = require("typedarray-pool") -var ops = require("ndarray-ops") -var ndarray = require("ndarray") - -var SUPPORTED_TYPES = [ - "uint8", - "uint8_clamped", - "uint16", - "uint32", - "int8", - "int16", - "int32", - "float32" ] - -function GLBuffer(gl, type, handle, length, usage) { - this.gl = gl - this.type = type - this.handle = handle - this.length = length - this.usage = usage -} - -var proto = GLBuffer.prototype - -proto.bind = function() { - this.gl.bindBuffer(this.type, this.handle) -} - -proto.unbind = function() { - this.gl.bindBuffer(this.type, null) -} - -proto.dispose = function() { - this.gl.deleteBuffer(this.handle) -} - -function updateTypeArray(gl, type, len, usage, data, offset) { - var dataLen = data.length * data.BYTES_PER_ELEMENT - if(offset < 0) { - gl.bufferData(type, data, usage) - return dataLen - } - if(dataLen + offset > len) { - throw new Error("gl-buffer: If resizing buffer, must not specify offset") - } - gl.bufferSubData(type, offset, data) - return len -} - -function makeScratchTypeArray(array, dtype) { - var res = pool.malloc(array.length, dtype) - var n = array.length - for(var i=0; i=0; --i) { - if(stride[i] !== n) { - return false - } - n *= shape[i] - } - return true -} - -proto.update = function(array, offset) { - if(typeof offset !== "number") { - offset = -1 - } - this.bind() - if(typeof array === "object" && typeof array.shape !== "undefined") { //ndarray - var dtype = array.dtype - if(SUPPORTED_TYPES.indexOf(dtype) < 0) { - dtype = "float32" - } - if(this.type === this.gl.ELEMENT_ARRAY_BUFFER) { - var ext = gl.getExtension('OES_element_index_uint') - if(ext && dtype !== "uint16") { - dtype = "uint32" - } else { - dtype = "uint16" - } - } - if(dtype === array.dtype && isPacked(array.shape, array.stride)) { - if(array.offset === 0 && array.data.length === array.shape[0]) { - this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array.data, offset) - } else { - this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array.data.subarray(array.offset, array.shape[0]), offset) - } - } else { - var tmp = pool.malloc(array.size, dtype) - var ndt = ndarray(tmp, array.shape) - ops.assign(ndt, array) - if(offset < 0) { - this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, tmp, offset) - } else { - this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, tmp.subarray(0, array.size), offset) - } - pool.free(tmp) - } - } else if(Array.isArray(array)) { //Vanilla array - var t - if(this.type === this.gl.ELEMENT_ARRAY_BUFFER) { - t = makeScratchTypeArray(array, "uint16") - } else { - t = makeScratchTypeArray(array, "float32") - } - if(offset < 0) { - this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, t, offset) - } else { - this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, t.subarray(0, array.length), offset) - } - pool.free(t) - } else if(typeof array === "object" && typeof array.length === "number") { //Typed array - this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array, offset) - } else if(typeof array === "number" || array === undefined) { //Number/default - if(offset >= 0) { - throw new Error("gl-buffer: Cannot specify offset when resizing buffer") - } - array = array | 0 - if(array <= 0) { - array = 1 - } - this.gl.bufferData(this.type, array|0, this.usage) - this.length = array - } else { //Error, case should not happen - throw new Error("gl-buffer: Invalid data type") - } -} - -function createBuffer(gl, data, type, usage) { - type = type || gl.ARRAY_BUFFER - usage = usage || gl.DYNAMIC_DRAW - if(type !== gl.ARRAY_BUFFER && type !== gl.ELEMENT_ARRAY_BUFFER) { - throw new Error("gl-buffer: Invalid type for webgl buffer, must be either gl.ARRAY_BUFFER or gl.ELEMENT_ARRAY_BUFFER") - } - if(usage !== gl.DYNAMIC_DRAW && usage !== gl.STATIC_DRAW && usage !== gl.STREAM_DRAW) { - throw new Error("gl-buffer: Invalid usage for buffer, must be either gl.DYNAMIC_DRAW, gl.STATIC_DRAW or gl.STREAM_DRAW") - } - var handle = gl.createBuffer() - var result = new GLBuffer(gl, type, handle, 0, usage) - result.update(data) - return result -} - -module.exports = createBuffer - -},{"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":187}],185:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],186:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],187:[function(require,module,exports){ -(function (global,Buffer){ -'use strict' - -var bits = require('bit-twiddle') -var dup = require('dup') - -//Legacy pool support -if(!global.__TYPEDARRAY_POOL) { - global.__TYPEDARRAY_POOL = { - UINT8 : dup([32, 0]) - , UINT16 : dup([32, 0]) - , UINT32 : dup([32, 0]) - , INT8 : dup([32, 0]) - , INT16 : dup([32, 0]) - , INT32 : dup([32, 0]) - , FLOAT : dup([32, 0]) - , DOUBLE : dup([32, 0]) - , DATA : dup([32, 0]) - , UINT8C : dup([32, 0]) - , BUFFER : dup([32, 0]) - } -} - -var hasUint8C = (typeof Uint8ClampedArray) !== 'undefined' -var POOL = global.__TYPEDARRAY_POOL - -//Upgrade pool -if(!POOL.UINT8C) { - POOL.UINT8C = dup([32, 0]) -} -if(!POOL.BUFFER) { - POOL.BUFFER = dup([32, 0]) -} - -//New technique: Only allocate from ArrayBufferView and Buffer -var DATA = POOL.DATA - , BUFFER = POOL.BUFFER - -exports.free = function free(array) { - if(Buffer.isBuffer(array)) { - BUFFER[bits.log2(array.length)].push(array) - } else { - if(Object.prototype.toString.call(array) !== '[object ArrayBuffer]') { - array = array.buffer - } - if(!array) { - return - } - var n = array.length || array.byteLength - var log_n = bits.log2(n)|0 - DATA[log_n].push(array) - } -} - -function freeArrayBuffer(buffer) { - if(!buffer) { - return - } - var n = buffer.length || buffer.byteLength - var log_n = bits.log2(n) - DATA[log_n].push(buffer) -} - -function freeTypedArray(array) { - freeArrayBuffer(array.buffer) -} - -exports.freeUint8 = -exports.freeUint16 = -exports.freeUint32 = -exports.freeInt8 = -exports.freeInt16 = -exports.freeInt32 = -exports.freeFloat32 = -exports.freeFloat = -exports.freeFloat64 = -exports.freeDouble = -exports.freeUint8Clamped = -exports.freeDataView = freeTypedArray - -exports.freeArrayBuffer = freeArrayBuffer - -exports.freeBuffer = function freeBuffer(array) { - BUFFER[bits.log2(array.length)].push(array) -} - -exports.malloc = function malloc(n, dtype) { - if(dtype === undefined || dtype === 'arraybuffer') { - return mallocArrayBuffer(n) - } else { - switch(dtype) { - case 'uint8': - return mallocUint8(n) - case 'uint16': - return mallocUint16(n) - case 'uint32': - return mallocUint32(n) - case 'int8': - return mallocInt8(n) - case 'int16': - return mallocInt16(n) - case 'int32': - return mallocInt32(n) - case 'float': - case 'float32': - return mallocFloat(n) - case 'double': - case 'float64': - return mallocDouble(n) - case 'uint8_clamped': - return mallocUint8Clamped(n) - case 'buffer': - return mallocBuffer(n) - case 'data': - case 'dataview': - return mallocDataView(n) - - default: - return null - } - } - return null -} - -function mallocArrayBuffer(n) { - var n = bits.nextPow2(n) - var log_n = bits.log2(n) - var d = DATA[log_n] - if(d.length > 0) { - return d.pop() - } - return new ArrayBuffer(n) -} -exports.mallocArrayBuffer = mallocArrayBuffer - -function mallocUint8(n) { - return new Uint8Array(mallocArrayBuffer(n), 0, n) -} -exports.mallocUint8 = mallocUint8 - -function mallocUint16(n) { - return new Uint16Array(mallocArrayBuffer(2*n), 0, n) -} -exports.mallocUint16 = mallocUint16 - -function mallocUint32(n) { - return new Uint32Array(mallocArrayBuffer(4*n), 0, n) -} -exports.mallocUint32 = mallocUint32 - -function mallocInt8(n) { - return new Int8Array(mallocArrayBuffer(n), 0, n) -} -exports.mallocInt8 = mallocInt8 - -function mallocInt16(n) { - return new Int16Array(mallocArrayBuffer(2*n), 0, n) -} -exports.mallocInt16 = mallocInt16 - -function mallocInt32(n) { - return new Int32Array(mallocArrayBuffer(4*n), 0, n) -} -exports.mallocInt32 = mallocInt32 - -function mallocFloat(n) { - return new Float32Array(mallocArrayBuffer(4*n), 0, n) -} -exports.mallocFloat32 = exports.mallocFloat = mallocFloat - -function mallocDouble(n) { - return new Float64Array(mallocArrayBuffer(8*n), 0, n) -} -exports.mallocFloat64 = exports.mallocDouble = mallocDouble - -function mallocUint8Clamped(n) { - if(hasUint8C) { - return new Uint8ClampedArray(mallocArrayBuffer(n), 0, n) - } else { - return mallocUint8(n) - } -} -exports.mallocUint8Clamped = mallocUint8Clamped - -function mallocDataView(n) { - return new DataView(mallocArrayBuffer(n), 0, n) -} -exports.mallocDataView = mallocDataView - -function mallocBuffer(n) { - n = bits.nextPow2(n) - var log_n = bits.log2(n) - var cache = BUFFER[log_n] - if(cache.length > 0) { - return cache.pop() - } - return new Buffer(n) -} -exports.mallocBuffer = mallocBuffer - -exports.clearCache = function clearCache() { - for(var i=0; i<32; ++i) { - POOL.UINT8[i].length = 0 - POOL.UINT16[i].length = 0 - POOL.UINT32[i].length = 0 - POOL.INT8[i].length = 0 - POOL.INT16[i].length = 0 - POOL.INT32[i].length = 0 - POOL.FLOAT[i].length = 0 - POOL.DOUBLE[i].length = 0 - POOL.UINT8C[i].length = 0 - DATA[i].length = 0 - BUFFER[i].length = 0 - } -} -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) -},{"bit-twiddle":185,"buffer":33,"dup":186}],188:[function(require,module,exports){ -'use strict' - -module.exports = createErrorBars - -var createBuffer = require('gl-buffer') -var createVAO = require('gl-vao') -var createShader = require('./shaders/index') - -var IDENTITY = [1,0,0,0, - 0,1,0,0, - 0,0,1,0, - 0,0,0,1] - -function ErrorBars(gl, buffer, vao, shader) { - this.gl = gl - this.shader = shader - this.buffer = buffer - this.vao = vao - this.pixelRatio = 1 - this.bounds = [[ Infinity, Infinity, Infinity], [-Infinity,-Infinity,-Infinity]] - this.clipBounds = [[-Infinity,-Infinity,-Infinity], [ Infinity, Infinity, Infinity]] - this.lineWidth = [1,1,1] - this.capSize = [10,10,10] - this.lineCount = [0,0,0] - this.lineOffset = [0,0,0] - this.opacity = 1 -} - -var proto = ErrorBars.prototype - -proto.isOpaque = function() { - return this.opacity >= 1 -} - -proto.isTransparent = function() { - return this.opacity < 1 -} - -proto.drawTransparent = proto.draw = function(cameraParams) { - var gl = this.gl - var uniforms = this.shader.uniforms - - this.shader.bind() - var view = uniforms.view = cameraParams.view || IDENTITY - var projection = uniforms.projection = cameraParams.projection || IDENTITY - uniforms.model = cameraParams.model || IDENTITY - uniforms.clipBounds = this.clipBounds - uniforms.opacity = this.opacity - - - var cx = view[12] - var cy = view[13] - var cz = view[14] - var cw = view[15] - var pixelScaleF = this.pixelRatio * (projection[3]*cx + projection[7]*cy + projection[11]*cz + projection[15]*cw) / gl.drawingBufferHeight - - - this.vao.bind() - for(var i=0; i<3; ++i) { - gl.lineWidth(this.lineWidth[i]) - uniforms.capSize = this.capSize[i] * pixelScaleF - gl.drawArrays(gl.LINES, this.lineOffset[i], this.lineCount[i]) - } - this.vao.unbind() -} - -function updateBounds(bounds, point) { - for(var i=0; i<3; ++i) { - bounds[0][i] = Math.min(bounds[0][i], point[i]) - bounds[1][i] = Math.max(bounds[1][i], point[i]) - } -} - -var FACE_TABLE = (function(){ - var table = new Array(3) - for(var d=0; d<3; ++d) { - var row = [] - for(var j=1; j<=2; ++j) { - for(var s=-1; s<=1; s+=2) { - var u = (j+d) % 3 - var y = [0,0,0] - y[u] = s - row.push(y) - } - } - table[d] = row - } - return table -})() - - -function emitFace(verts, x, c, d) { - var offsets = FACE_TABLE[d] - for(var i=0; i 0) { - var x = p.slice() - x[j] += e[1][j] - verts.push(p[0], p[1], p[2], - c[0], c[1], c[2], c[3], - 0, 0, 0, - x[0], x[1], x[2], - c[0], c[1], c[2], c[3], - 0, 0, 0) - updateBounds(this.bounds, x) - vertexCount += 2 + emitFace(verts, x, c, j) - } - } - this.lineCount[j] = vertexCount - this.lineOffset[j] - } - this.buffer.update(verts) - } -} - -proto.dispose = function() { - this.shader.dispose() - this.buffer.dispose() - this.vao.dispose() -} - -function createErrorBars(options) { - var gl = options.gl - var buffer = createBuffer(gl) - var vao = createVAO(gl, [ - { - buffer: buffer, - type: gl.FLOAT, - size: 3, - offset: 0, - stride: 40 - }, - { - buffer: buffer, - type: gl.FLOAT, - size: 4, - offset: 12, - stride: 40 - }, - { - buffer: buffer, - type: gl.FLOAT, - size: 3, - offset: 28, - stride: 40 - } - ]) - - var shader = createShader(gl) - shader.attributes.position.location = 0 - shader.attributes.color.location = 1 - shader.attributes.offset.location = 2 - - var result = new ErrorBars(gl, buffer, vao, shader) - result.update(options) - return result -} - -},{"./shaders/index":197,"gl-buffer":189,"gl-vao":196}],189:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":192}],190:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],191:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],192:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":190,"buffer":33,"dup":187}],193:[function(require,module,exports){ -"use strict" - -function doBind(gl, elements, attributes) { - if(elements) { - elements.bind() - } else { - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null) - } - var nattribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS)|0 - if(attributes) { - if(attributes.length > nattribs) { - throw new Error("gl-vao: Too many vertex attributes") - } - for(var i=0; i> (i * 8)) & 0xff - } - - this.pickOffset = pickOffset - - shader.bind() - - var uniforms = shader.uniforms - uniforms.viewTransform = MATRIX - uniforms.pickOffset = PICK_VECTOR - uniforms.shape = this.shape - - var attributes = shader.attributes - this.positionBuffer.bind() - attributes.position.pointer() - - this.weightBuffer.bind() - attributes.weight.pointer(gl.UNSIGNED_BYTE, false) - - this.idBuffer.bind() - attributes.pickId.pointer(gl.UNSIGNED_BYTE, false) - - gl.drawArrays(gl.TRIANGLES, 0, numVertices) - - return pickOffset + this.shape[0] * this.shape[1] - } -})() - -proto.pick = function (x, y, value) { - var pickOffset = this.pickOffset - var pointCount = this.shape[0] * this.shape[1] - if (value < pickOffset || value >= pickOffset + pointCount) { - return null - } - var pointId = value - pickOffset - var xData = this.xData - var yData = this.yData - return { - object: this, - pointId: pointId, - dataCoord: [ - xData[pointId % this.shape[0]], - yData[(pointId / this.shape[0]) | 0]] - } -} - -proto.update = function (options) { - options = options || {} - - var shape = options.shape || [0, 0] - - var x = options.x || iota(shape[0]) - var y = options.y || iota(shape[1]) - var z = options.z || new Float32Array(shape[0] * shape[1]) - - this.xData = x - this.yData = y - - var colorLevels = options.colorLevels || [0] - var colorValues = options.colorValues || [0, 0, 0, 1] - var colorCount = colorLevels.length - - var bounds = this.bounds - var lox = bounds[0] = x[0] - var loy = bounds[1] = y[0] - var hix = bounds[2] = x[x.length - 1] - var hiy = bounds[3] = y[y.length - 1] - - var xs = 1.0 / (hix - lox) - var ys = 1.0 / (hiy - loy) - - var numX = shape[0] - var numY = shape[1] - - this.shape = [numX, numY] - - var numVerts = (numX - 1) * (numY - 1) * (WEIGHTS.length >>> 1) - - this.numVertices = numVerts - - var colors = pool.mallocUint8(numVerts * 4) - var positions = pool.mallocFloat32(numVerts * 2) - var weights = pool.mallocUint8 (numVerts * 2) - var ids = pool.mallocUint32(numVerts) - - var ptr = 0 - - for (var j = 0; j < numY - 1; ++j) { - var yc0 = ys * (y[j] - loy) - var yc1 = ys * (y[j + 1] - loy) - for (var i = 0; i < numX - 1; ++i) { - var xc0 = xs * (x[i] - lox) - var xc1 = xs * (x[i + 1] - lox) - - for (var dd = 0; dd < WEIGHTS.length; dd += 2) { - var dx = WEIGHTS[dd] - var dy = WEIGHTS[dd + 1] - var offset = (j + dy) * numX + (i + dx) - var zc = z[offset] - var colorIdx = bsearch.le(colorLevels, zc) - var r, g, b, a - if (colorIdx < 0) { - r = colorValues[0] - g = colorValues[1] - b = colorValues[2] - a = colorValues[3] - } else if (colorIdx === colorCount - 1) { - r = colorValues[4 * colorCount - 4] - g = colorValues[4 * colorCount - 3] - b = colorValues[4 * colorCount - 2] - a = colorValues[4 * colorCount - 1] - } else { - var t = (zc - colorLevels[colorIdx]) / - (colorLevels[colorIdx + 1] - colorLevels[colorIdx]) - var ti = 1.0 - t - var i0 = 4 * colorIdx - var i1 = 4 * (colorIdx + 1) - r = ti * colorValues[i0] + t * colorValues[i1] - g = ti * colorValues[i0 + 1] + t * colorValues[i1 + 1] - b = ti * colorValues[i0 + 2] + t * colorValues[i1 + 2] - a = ti * colorValues[i0 + 3] + t * colorValues[i1 + 3] - } - - colors[4 * ptr] = 255 * r - colors[4 * ptr + 1] = 255 * g - colors[4 * ptr + 2] = 255 * b - colors[4 * ptr + 3] = 255 * a - - positions[2*ptr] = xc0*.5 + xc1*.5; - positions[2*ptr+1] = yc0*.5 + yc1*.5; - - weights[2*ptr] = dx; - weights[2*ptr+1] = dy; - - ids[ptr] = j * numX + i - - ptr += 1 - } - } - } - - this.positionBuffer.update(positions) - this.weightBuffer.update(weights) - this.colorBuffer.update(colors) - this.idBuffer.update(ids) - - pool.free(positions) - pool.free(colors) - pool.free(weights) - pool.free(ids) -} - -proto.dispose = function () { - this.shader.dispose() - this.pickShader.dispose() - this.positionBuffer.dispose() - this.weightBuffer.dispose() - this.colorBuffer.dispose() - this.idBuffer.dispose() - this.plot.removeObject(this) -} - -function createHeatmap2D (plot, options) { - var gl = plot.gl - - var shader = createShader(gl, shaders.vertex, shaders.fragment) - var pickShader = createShader(gl, shaders.pickVertex, shaders.pickFragment) - - var positionBuffer = createBuffer(gl) - var weightBuffer = createBuffer(gl) - var colorBuffer = createBuffer(gl) - var idBuffer = createBuffer(gl) - - var heatmap = new GLHeatmap2D( - plot, - shader, - pickShader, - positionBuffer, - weightBuffer, - colorBuffer, - idBuffer) - - heatmap.update(options) - plot.addObject(heatmap) - - return heatmap -} - -},{"./lib/shaders":199,"binary-search-bounds":200,"gl-buffer":201,"gl-shader":835,"iota-array":202,"typedarray-pool":205}],199:[function(require,module,exports){ -'use strict' - - - -module.exports = { - fragment: "precision lowp float;\n#define GLSLIFY 1\nvarying vec4 fragColor;\nvoid main() {\n gl_FragColor = vec4(fragColor.rgb * fragColor.a, fragColor.a);\n}\n", - vertex: "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec2 position;\nattribute vec4 color;\nattribute vec2 weight;\n\nuniform vec2 shape;\nuniform mat3 viewTransform;\n\nvarying vec4 fragColor;\n\nvoid main() {\n vec3 vPosition = viewTransform * vec3( position + (weight-.5)/(shape-1.) , 1.0);\n fragColor = color;\n gl_Position = vec4(vPosition.xy, 0, vPosition.z);\n}\n", - pickFragment: "precision mediump float;\n#define GLSLIFY 1\n\nvarying vec4 fragId;\nvarying vec2 vWeight;\n\nuniform vec2 shape;\nuniform vec4 pickOffset;\n\nvoid main() {\n vec2 d = step(.5, vWeight);\n vec4 id = fragId + pickOffset;\n id.x += d.x + d.y*shape.x;\n\n id.y += floor(id.x / 256.0);\n id.x -= floor(id.x / 256.0) * 256.0;\n\n id.z += floor(id.y / 256.0);\n id.y -= floor(id.y / 256.0) * 256.0;\n\n id.w += floor(id.z / 256.0);\n id.z -= floor(id.z / 256.0) * 256.0;\n\n gl_FragColor = id/255.;\n}\n", - pickVertex: "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec2 position;\nattribute vec4 pickId;\nattribute vec2 weight;\n\nuniform vec2 shape;\nuniform mat3 viewTransform;\n\nvarying vec4 fragId;\nvarying vec2 vWeight;\n\nvoid main() {\n vWeight = weight;\n\n fragId = pickId;\n\n vec3 vPosition = viewTransform * vec3( position + (weight-.5)/(shape-1.) , 1.0);\n gl_Position = vec4(vPosition.xy, 0, vPosition.z);\n}\n" -} - -},{}],200:[function(require,module,exports){ -"use strict" - -function compileSearch(funcName, predicate, reversed, extraArgs, earlyOut) { - var code = [ - "function ", funcName, "(a,l,h,", extraArgs.join(","), "){", -earlyOut ? "" : "var i=", (reversed ? "l-1" : "h+1"), -";while(l<=h){\ -var m=(l+h)>>>1,x=a[m]"] - if(earlyOut) { - if(predicate.indexOf("c") < 0) { - code.push(";if(x===y){return m}else if(x<=y){") - } else { - code.push(";var p=c(x,y);if(p===0){return m}else if(p<=0){") - } - } else { - code.push(";if(", predicate, "){i=m;") - } - if(reversed) { - code.push("l=m+1}else{h=m-1}") - } else { - code.push("h=m-1}else{l=m+1}") - } - code.push("}") - if(earlyOut) { - code.push("return -1};") - } else { - code.push("return i};") - } - return code.join("") -} - -function compileBoundsSearch(predicate, reversed, suffix, earlyOut) { - var result = new Function([ - compileSearch("A", "x" + predicate + "y", reversed, ["y"], earlyOut), - compileSearch("P", "c(x,y)" + predicate + "0", reversed, ["y", "c"], earlyOut), -"function dispatchBsearch", suffix, "(a,y,c,l,h){\ -if(typeof(c)==='function'){\ -return P(a,(l===void 0)?0:l|0,(h===void 0)?a.length-1:h|0,y,c)\ -}else{\ -return A(a,(c===void 0)?0:c|0,(l===void 0)?a.length-1:l|0,y)\ -}}\ -return dispatchBsearch", suffix].join("")) - return result() -} - -module.exports = { - ge: compileBoundsSearch(">=", false, "GE"), - gt: compileBoundsSearch(">", false, "GT"), - lt: compileBoundsSearch("<", true, "LT"), - le: compileBoundsSearch("<=", true, "LE"), - eq: compileBoundsSearch("-", true, "EQ", true) -} - -},{}],201:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":205}],202:[function(require,module,exports){ -arguments[4][78][0].apply(exports,arguments) -},{"dup":78}],203:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],204:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],205:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":203,"buffer":33,"dup":187}],206:[function(require,module,exports){ - - -exports.lineVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo, dHi, dLo;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo, screenShape;\nuniform float width;\n\nvarying vec2 direction;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvec2 project_2_1(vec2 scHi, vec2 scLo, vec2 posHi, vec2 posLo) {\n return scHi * posHi\n + scLo * posHi\n + scHi * posLo\n + scLo * posLo;\n}\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n vec2 dir = project_2_1(scaleHi, scaleLo, dHi, dLo);\n vec2 n = 0.5 * width * normalize(screenShape.yx * vec2(dir.y, -dir.x)) / screenShape.xy;\n vec2 tangent = normalize(screenShape.xy * dir);\n if(dir.x < 0.0 || (dir.x == 0.0 && dir.y < 0.0)) {\n direction = -tangent;\n } else {\n direction = tangent;\n }\n gl_Position = vec4(p + n, 0.0, 1.0);\n}" -exports.lineFragment = "precision highp float;\n#define GLSLIFY 1\n\nuniform vec4 color;\nuniform vec2 screenShape;\nuniform sampler2D dashPattern;\nuniform float dashLength;\n\nvarying vec2 direction;\n\nvoid main() {\n float t = fract(dot(direction, gl_FragCoord.xy) / dashLength);\n vec4 pcolor = color * texture2D(dashPattern, vec2(t, 0.0)).r;\n gl_FragColor = vec4(pcolor.rgb * pcolor.a, pcolor.a);\n}" -exports.mitreVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo;\nuniform float radius;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n gl_Position = vec4(p, 0.0, 1.0);\n gl_PointSize = radius;\n}" -exports.mitreFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color;\n\nvoid main() {\n if(length(gl_PointCoord.xy - 0.5) > 0.25) {\n discard;\n }\n gl_FragColor = vec4(color.rgb, color.a);\n}" -exports.pickVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo, dHi;\nattribute vec4 pick0, pick1;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo, screenShape;\nuniform float width;\n\nvarying vec4 pickA, pickB;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n vec2 n = width * normalize(screenShape.yx * vec2(dHi.y, -dHi.x)) / screenShape.xy;\n gl_Position = vec4(p + n, 0, 1);\n pickA = pick0;\n pickB = pick1;\n}" -exports.pickFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 pickOffset;\n\nvarying vec4 pickA, pickB;\n\nvoid main() {\n vec4 fragId = vec4(pickA.xyz, 0.0);\n if(pickB.w > pickA.w) {\n fragId.xyz = pickB.xyz;\n }\n\n fragId += pickOffset;\n\n fragId.y += floor(fragId.x / 256.0);\n fragId.x -= floor(fragId.x / 256.0) * 256.0;\n\n fragId.z += floor(fragId.y / 256.0);\n fragId.y -= floor(fragId.y / 256.0) * 256.0;\n\n fragId.w += floor(fragId.z / 256.0);\n fragId.z -= floor(fragId.z / 256.0) * 256.0;\n\n gl_FragColor = fragId / 255.0;\n}" -exports.fillVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 aHi, aLo, dHi;\n\nuniform vec2 scaleHi, translateHi, scaleLo, translateLo, projectAxis;\nuniform float projectValue, depth;\n\n\nvec2 project_1_0(vec2 scHi, vec2 trHi, vec2 scLo, vec2 trLo, vec2 posHi, vec2 posLo) {\n return (posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo;\n}\n\n\nvoid main() {\n vec2 p = project_1_0(scaleHi, translateHi, scaleLo, translateLo, aHi, aLo);\n if(dHi.y < 0.0 || (dHi.y == 0.0 && dHi.x < 0.0)) {\n if(dot(p, projectAxis) < projectValue) {\n p = p * (1.0 - abs(projectAxis)) + projectAxis * projectValue;\n }\n }\n gl_Position = vec4(p, depth, 1);\n}" -exports.fillFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color;\n\nvoid main() {\n gl_FragColor = vec4(color.rgb * color.a, color.a);\n}" -},{}],207:[function(require,module,exports){ -'use strict' - -module.exports = createLinePlot - -var createShader = require('gl-shader') -var createBuffer = require('gl-buffer') -var createTexture = require('gl-texture2d') -var ndarray = require('ndarray') -var pool = require('typedarray-pool') - -var SHADERS = require('./lib/shaders') - -function GLLine2D( - plot, - dashPattern, - lineBufferHi, - lineBufferLo, - pickBuffer, - lineShader, - mitreShader, - fillShader, - pickShader) { - - this.plot = plot - this.dashPattern = dashPattern - this.lineBufferHi = lineBufferHi - this.lineBufferLo = lineBufferLo - this.pickBuffer = pickBuffer - this.lineShader = lineShader - this.mitreShader = mitreShader - this.fillShader = fillShader - this.pickShader = pickShader - this.usingDashes = false - - this.bounds = [Infinity, Infinity, -Infinity, -Infinity] - this.width = 1 - this.color = [0, 0, 1, 1] - - //Fill to axes - this.fill = [false, false, false, false] - this.fillColor = [ - [0, 0, 0, 1], - [0, 0, 0, 1], - [0, 0, 0, 1], - [0, 0, 0, 1] - ] - - this.data = null - this.numPoints = 0 - this.vertCount = 0 - - this.pickOffset = 0 -} - -var proto = GLLine2D.prototype - -proto.setProjectionModel = (function() { - - var pm = { - scaleHi: new Float32Array([0, 0]), - scaleLo: new Float32Array([0, 0]), - translateHi: new Float32Array([0, 0]), - translateLo: new Float32Array([0, 0]), - screenShape: [0, 0] - } - - return function() { - - var bounds = this.bounds - var viewBox = this.plot.viewBox - var dataBox = this.plot.dataBox - - var boundX = bounds[2] - bounds[0] - var boundY = bounds[3] - bounds[1] - var dataX = dataBox[2] - dataBox[0] - var dataY = dataBox[3] - dataBox[1] - var screenX = viewBox[2] - viewBox[0] - var screenY = viewBox[3] - viewBox[1] - - var scaleX = 2 * boundX / dataX - var scaleY = 2 * boundY / dataY - var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX - var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY - - pm.scaleHi[0] = scaleX - pm.scaleHi[1] = scaleY - pm.scaleLo[0] = scaleX - pm.scaleHi[0] - pm.scaleLo[1] = scaleY - pm.scaleHi[1] - pm.translateHi[0] = translateX - pm.translateHi[1] = translateY - pm.translateLo[0] = translateX - pm.translateHi[0] - pm.translateLo[1] = translateY - pm.translateHi[1] - - pm.screenShape[0] = screenX - pm.screenShape[1] = screenY - - return pm - } -})() - -proto.setProjectionUniforms = function(uniforms, projectionModel) { - uniforms.scaleHi = projectionModel.scaleHi - uniforms.scaleLo = projectionModel.scaleLo - uniforms.translateHi = projectionModel.translateHi - uniforms.translateLo = projectionModel.translateLo - uniforms.screenShape = projectionModel.screenShape -} - -proto.draw = (function() { - - var PX_AXIS = [1, 0] - var NX_AXIS = [-1, 0] - var PY_AXIS = [0, 1] - var NY_AXIS = [0, -1] - - return function() { - var count = this.vertCount - - if(!count) { - return - } - - var projectionModel = this.setProjectionModel() - - var plot = this.plot - var width = this.width - var gl = plot.gl - var pixelRatio = plot.pixelRatio - - var color = this.color - - var fillAttributes = this.fillShader.attributes - - this.lineBufferLo.bind() - fillAttributes.aLo.pointer(gl.FLOAT, false, 16, 0) - - this.lineBufferHi.bind() - - var fill = this.fill - - if(fill[0] || fill[1] || fill[2] || fill[3]) { - - var fillShader = this.fillShader - fillShader.bind() - - var fillUniforms = fillShader.uniforms - this.setProjectionUniforms(fillUniforms, projectionModel) - fillUniforms.depth = plot.nextDepthValue() - - fillAttributes.aHi.pointer(gl.FLOAT, false, 16, 0) - fillAttributes.dHi.pointer(gl.FLOAT, false, 16, 8) - - gl.depthMask(true) - gl.enable(gl.DEPTH_TEST) - - var fillColor = this.fillColor - if(fill[0]) { - fillUniforms.color = fillColor[0] - fillUniforms.projectAxis = NX_AXIS - fillUniforms.projectValue = 1 - gl.drawArrays(gl.TRIANGLES, 0, count) - } - - if(fill[1]) { - fillUniforms.color = fillColor[1] - fillUniforms.projectAxis = NY_AXIS - fillUniforms.projectValue = 1 - gl.drawArrays(gl.TRIANGLES, 0, count) - } - - if(fill[2]) { - fillUniforms.color = fillColor[2] - fillUniforms.projectAxis = PX_AXIS - fillUniforms.projectValue = 1 - gl.drawArrays(gl.TRIANGLES, 0, count) - } - - if(fill[3]) { - fillUniforms.color = fillColor[3] - fillUniforms.projectAxis = PY_AXIS - fillUniforms.projectValue = 1 - gl.drawArrays(gl.TRIANGLES, 0, count) - } - - gl.depthMask(false) - gl.disable(gl.DEPTH_TEST) - } - - var shader = this.lineShader - shader.bind() - - this.lineBufferLo.bind() - shader.attributes.aLo.pointer(gl.FLOAT, false, 16, 0) - shader.attributes.dLo.pointer(gl.FLOAT, false, 16, 8) - - this.lineBufferHi.bind() - - var uniforms = shader.uniforms - this.setProjectionUniforms(uniforms, projectionModel) - uniforms.color = color - uniforms.width = width * pixelRatio - uniforms.dashPattern = this.dashPattern.bind() - uniforms.dashLength = this.dashLength * pixelRatio - - var attributes = shader.attributes - attributes.aHi.pointer(gl.FLOAT, false, 16, 0) - attributes.dHi.pointer(gl.FLOAT, false, 16, 8) - - gl.drawArrays(gl.TRIANGLES, 0, count) - - //Draw mitres - if(width > 2 && !this.usingDashes) { - var mshader = this.mitreShader - - this.lineBufferLo.bind() - mshader.attributes.aLo.pointer(gl.FLOAT, false, 48, 0) - - this.lineBufferHi.bind() - mshader.bind() - - var muniforms = mshader.uniforms - this.setProjectionUniforms(muniforms, projectionModel) - muniforms.color = color - muniforms.radius = width * pixelRatio - - mshader.attributes.aHi.pointer(gl.FLOAT, false, 48, 0) - gl.drawArrays(gl.POINTS, 0, (count / 3) | 0) - } - } -})() - -proto.drawPick = (function() { - - var PICK_OFFSET = [0, 0, 0, 0] - - return function(pickOffset) { - - var count = this.vertCount - var numPoints = this.numPoints - - this.pickOffset = pickOffset - if(!count) { - return pickOffset + numPoints - } - - var projectionModel = this.setProjectionModel() - - var plot = this.plot - var width = this.width - var gl = plot.gl - var pixelRatio = plot.pickPixelRatio - - var shader = this.pickShader - var pickBuffer = this.pickBuffer - - PICK_OFFSET[0] = pickOffset & 0xff - PICK_OFFSET[1] = (pickOffset >>> 8) & 0xff - PICK_OFFSET[2] = (pickOffset >>> 16) & 0xff - PICK_OFFSET[3] = pickOffset >>> 24 - - shader.bind() - - var uniforms = shader.uniforms - this.setProjectionUniforms(uniforms, projectionModel) - uniforms.width = width * pixelRatio - uniforms.pickOffset = PICK_OFFSET - - var attributes = shader.attributes - - this.lineBufferHi.bind() - attributes.aHi.pointer(gl.FLOAT, false, 16, 0) - attributes.dHi.pointer(gl.FLOAT, false, 16, 8) - - this.lineBufferLo.bind() - attributes.aLo.pointer(gl.FLOAT, false, 16, 0) - - //attributes.dLo.pointer(gl.FLOAT, false, 16, 8) - - pickBuffer.bind() - attributes.pick0.pointer(gl.UNSIGNED_BYTE, false, 8, 0) - attributes.pick1.pointer(gl.UNSIGNED_BYTE, false, 8, 4) - - gl.drawArrays(gl.TRIANGLES, 0, count) - - return pickOffset + numPoints - } -})() - -proto.pick = function(x, y, value) { - var pickOffset = this.pickOffset - var pointCount = this.numPoints - if(value < pickOffset || value >= pickOffset + pointCount) { - return null - } - var pointId = value - pickOffset - var points = this.data - return { - object: this, - pointId: pointId, - dataCoord: [points[2 * pointId], points[2 * pointId + 1]] - } -} - -function deepCopy(arr) { - return arr.map(function(x) { - return x.slice() - }) -} - -proto.update = function(options) { - options = options || {} - - var gl = this.plot.gl - var i, j, ptr, ax, ay - - this.color = (options.color || [0, 0, 1, 1]).slice() - this.width = +(options.width || 1) - this.fill = (options.fill || [false, false, false, false]).slice() - this.fillColor = deepCopy(options.fillColor || [ - [0, 0, 0, 1], - [0, 0, 0, 1], - [0, 0, 0, 1], - [0, 0, 0, 1] - ]) - - var dashes = options.dashes || [1] - var dashLength = 0 - for(i = 0; i < dashes.length; ++i) { - dashLength += dashes[i] - } - var dashData = pool.mallocUint8(dashLength) - ptr = 0 - var fillColor = 255 - for(i = 0; i < dashes.length; ++i) { - for(j = 0; j < dashes[i]; ++j) { - dashData[ptr++] = fillColor - } - fillColor ^= 255 - } - this.dashPattern.dispose() - this.usingDashes = dashes.length > 1 - - this.dashPattern = createTexture(gl, ndarray(dashData, [dashLength, 1, 4], [1, 0, 0])) - this.dashPattern.minFilter = gl.NEAREST - this.dashPattern.magFilter = gl.NEAREST - this.dashLength = dashLength - pool.free(dashData) - - var data = options.positions - this.data = data - - var bounds = this.bounds - bounds[0] = bounds[1] = Infinity - bounds[2] = bounds[3] = -Infinity - - var numPoints = this.numPoints = data.length >>> 1 - if(numPoints === 0) { - return - } - - for(i = 0; i < numPoints; ++i) { - ax = data[2 * i] - ay = data[2 * i + 1] - - if (isNaN(ax) || isNaN(ay)) { - continue - } - - bounds[0] = Math.min(bounds[0], ax) - bounds[1] = Math.min(bounds[1], ay) - bounds[2] = Math.max(bounds[2], ax) - bounds[3] = Math.max(bounds[3], ay) - } - - if(bounds[0] === bounds[2]) bounds[2] += 1 - if(bounds[3] === bounds[1]) bounds[3] += 1 - - //Generate line data - var lineData = pool.mallocFloat64(24 * (numPoints - 1)) - var lineDataHi = pool.mallocFloat32(24 * (numPoints - 1)) - var lineDataLo = pool.mallocFloat32(24 * (numPoints - 1)) - var pickData = pool.mallocUint32(12 * (numPoints - 1)) - var lineDataPtr = lineDataHi.length - var pickDataPtr = pickData.length - ptr = numPoints - - var count = 0 - - while(ptr > 1) { - var id = --ptr - ax = data[2 * ptr] - ay = data[2 * ptr + 1] - - var next = id - 1 - var bx = data[2 * next] - var by = data[2 * next + 1] - - if (isNaN(ax) || isNaN(ay) || isNaN(bx) || isNaN(by)) { - continue - } - - count += 1 - - ax = (ax - bounds[0]) / (bounds[2] - bounds[0]) - ay = (ay - bounds[1]) / (bounds[3] - bounds[1]) - - bx = (bx - bounds[0]) / (bounds[2] - bounds[0]) - by = (by - bounds[1]) / (bounds[3] - bounds[1]) - - var dx = bx - ax - var dy = by - ay - - var akey0 = id | (1 << 24) - var akey1 = (id - 1) - var bkey0 = id - var bkey1 = (id - 1) | (1 << 24) - - lineData[--lineDataPtr] = -dy - lineData[--lineDataPtr] = -dx - lineData[--lineDataPtr] = ay - lineData[--lineDataPtr] = ax - pickData[--pickDataPtr] = akey0 - pickData[--pickDataPtr] = akey1 - - lineData[--lineDataPtr] = dy - lineData[--lineDataPtr] = dx - lineData[--lineDataPtr] = by - lineData[--lineDataPtr] = bx - pickData[--pickDataPtr] = bkey0 - pickData[--pickDataPtr] = bkey1 - - lineData[--lineDataPtr] = -dy - lineData[--lineDataPtr] = -dx - lineData[--lineDataPtr] = by - lineData[--lineDataPtr] = bx - pickData[--pickDataPtr] = bkey0 - pickData[--pickDataPtr] = bkey1 - - lineData[--lineDataPtr] = dy - lineData[--lineDataPtr] = dx - lineData[--lineDataPtr] = by - lineData[--lineDataPtr] = bx - pickData[--pickDataPtr] = bkey0 - pickData[--pickDataPtr] = bkey1 - - lineData[--lineDataPtr] = -dy - lineData[--lineDataPtr] = -dx - lineData[--lineDataPtr] = ay - lineData[--lineDataPtr] = ax - pickData[--pickDataPtr] = akey0 - pickData[--pickDataPtr] = akey1 - - lineData[--lineDataPtr] = dy - lineData[--lineDataPtr] = dx - lineData[--lineDataPtr] = ay - lineData[--lineDataPtr] = ax - pickData[--pickDataPtr] = akey0 - pickData[--pickDataPtr] = akey1 - } - - for(i = 0; i < lineData.length; i++) { - lineDataHi[i] = lineData[i] - lineDataLo[i] = lineData[i] - lineDataHi[i] - } - - this.vertCount = 6 * count - this.lineBufferHi.update(lineDataHi.subarray(lineDataPtr)) - this.lineBufferLo.update(lineDataLo.subarray(lineDataPtr)) - this.pickBuffer.update(pickData.subarray(pickDataPtr)) - - pool.free(lineData) - pool.free(lineDataHi) - pool.free(lineDataLo) - pool.free(pickData) -} - -proto.dispose = function() { - this.plot.removeObject(this) - this.lineBufferLo.dispose() - this.lineBufferHi.dispose() - this.pickBuffer.dispose() - this.lineShader.dispose() - this.mitreShader.dispose() - this.fillShader.dispose() - this.pickShader.dispose() - this.dashPattern.dispose() -} - -function createLinePlot(plot, options) { - var gl = plot.gl - var lineBufferHi = createBuffer(gl) - var lineBufferLo = createBuffer(gl) - var pickBuffer = createBuffer(gl) - var dashPattern = createTexture(gl, [1, 1]) - var lineShader = createShader(gl, SHADERS.lineVertex, SHADERS.lineFragment) - var mitreShader = createShader(gl, SHADERS.mitreVertex, SHADERS.mitreFragment) - var fillShader = createShader(gl, SHADERS.fillVertex, SHADERS.fillFragment) - var pickShader = createShader(gl, SHADERS.pickVertex, SHADERS.pickFragment) - var linePlot = new GLLine2D( - plot, - dashPattern, - lineBufferHi, - lineBufferLo, - pickBuffer, - lineShader, - mitreShader, - fillShader, - pickShader) - plot.addObject(linePlot) - linePlot.update(options) - return linePlot -} -},{"./lib/shaders":206,"gl-buffer":208,"gl-shader":835,"gl-texture2d":209,"ndarray":1118,"typedarray-pool":212}],208:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":212}],209:[function(require,module,exports){ -'use strict' - -var ndarray = require('ndarray') -var ops = require('ndarray-ops') -var pool = require('typedarray-pool') - -module.exports = createTexture2D - -var linearTypes = null -var filterTypes = null -var wrapTypes = null - -function lazyInitLinearTypes(gl) { - linearTypes = [ - gl.LINEAR, - gl.NEAREST_MIPMAP_LINEAR, - gl.LINEAR_MIPMAP_NEAREST, - gl.LINEAR_MIPMAP_NEAREST - ] - filterTypes = [ - gl.NEAREST, - gl.LINEAR, - gl.NEAREST_MIPMAP_NEAREST, - gl.NEAREST_MIPMAP_LINEAR, - gl.LINEAR_MIPMAP_NEAREST, - gl.LINEAR_MIPMAP_LINEAR - ] - wrapTypes = [ - gl.REPEAT, - gl.CLAMP_TO_EDGE, - gl.MIRRORED_REPEAT - ] -} - -function acceptTextureDOM (obj) { - return ( - ('undefined' != typeof HTMLCanvasElement && obj instanceof HTMLCanvasElement) || - ('undefined' != typeof HTMLImageElement && obj instanceof HTMLImageElement) || - ('undefined' != typeof HTMLVideoElement && obj instanceof HTMLVideoElement) || - ('undefined' != typeof ImageData && obj instanceof ImageData)) -} - -var convertFloatToUint8 = function(out, inp) { - ops.muls(out, inp, 255.0) -} - -function reshapeTexture(tex, w, h) { - var gl = tex.gl - var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE) - if(w < 0 || w > maxSize || h < 0 || h > maxSize) { - throw new Error('gl-texture2d: Invalid texture size') - } - tex._shape = [w, h] - tex.bind() - gl.texImage2D(gl.TEXTURE_2D, 0, tex.format, w, h, 0, tex.format, tex.type, null) - tex._mipLevels = [0] - return tex -} - -function Texture2D(gl, handle, width, height, format, type) { - this.gl = gl - this.handle = handle - this.format = format - this.type = type - this._shape = [width, height] - this._mipLevels = [0] - this._magFilter = gl.NEAREST - this._minFilter = gl.NEAREST - this._wrapS = gl.CLAMP_TO_EDGE - this._wrapT = gl.CLAMP_TO_EDGE - this._anisoSamples = 1 - - var parent = this - var wrapVector = [this._wrapS, this._wrapT] - Object.defineProperties(wrapVector, [ - { - get: function() { - return parent._wrapS - }, - set: function(v) { - return parent.wrapS = v - } - }, - { - get: function() { - return parent._wrapT - }, - set: function(v) { - return parent.wrapT = v - } - } - ]) - this._wrapVector = wrapVector - - var shapeVector = [this._shape[0], this._shape[1]] - Object.defineProperties(shapeVector, [ - { - get: function() { - return parent._shape[0] - }, - set: function(v) { - return parent.width = v - } - }, - { - get: function() { - return parent._shape[1] - }, - set: function(v) { - return parent.height = v - } - } - ]) - this._shapeVector = shapeVector -} - -var proto = Texture2D.prototype - -Object.defineProperties(proto, { - minFilter: { - get: function() { - return this._minFilter - }, - set: function(v) { - this.bind() - var gl = this.gl - if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) { - if(!gl.getExtension('OES_texture_float_linear')) { - v = gl.NEAREST - } - } - if(filterTypes.indexOf(v) < 0) { - throw new Error('gl-texture2d: Unknown filter mode ' + v) - } - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, v) - return this._minFilter = v - } - }, - magFilter: { - get: function() { - return this._magFilter - }, - set: function(v) { - this.bind() - var gl = this.gl - if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) { - if(!gl.getExtension('OES_texture_float_linear')) { - v = gl.NEAREST - } - } - if(filterTypes.indexOf(v) < 0) { - throw new Error('gl-texture2d: Unknown filter mode ' + v) - } - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, v) - return this._magFilter = v - } - }, - mipSamples: { - get: function() { - return this._anisoSamples - }, - set: function(i) { - var psamples = this._anisoSamples - this._anisoSamples = Math.max(i, 1)|0 - if(psamples !== this._anisoSamples) { - var ext = this.gl.getExtension('EXT_texture_filter_anisotropic') - if(ext) { - this.gl.texParameterf(this.gl.TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT, this._anisoSamples) - } - } - return this._anisoSamples - } - }, - wrapS: { - get: function() { - return this._wrapS - }, - set: function(v) { - this.bind() - if(wrapTypes.indexOf(v) < 0) { - throw new Error('gl-texture2d: Unknown wrap mode ' + v) - } - this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, v) - return this._wrapS = v - } - }, - wrapT: { - get: function() { - return this._wrapT - }, - set: function(v) { - this.bind() - if(wrapTypes.indexOf(v) < 0) { - throw new Error('gl-texture2d: Unknown wrap mode ' + v) - } - this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, v) - return this._wrapT = v - } - }, - wrap: { - get: function() { - return this._wrapVector - }, - set: function(v) { - if(!Array.isArray(v)) { - v = [v,v] - } - if(v.length !== 2) { - throw new Error('gl-texture2d: Must specify wrap mode for rows and columns') - } - for(var i=0; i<2; ++i) { - if(wrapTypes.indexOf(v[i]) < 0) { - throw new Error('gl-texture2d: Unknown wrap mode ' + v) - } - } - this._wrapS = v[0] - this._wrapT = v[1] - - var gl = this.gl - this.bind() - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, this._wrapS) - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, this._wrapT) - - return v - } - }, - shape: { - get: function() { - return this._shapeVector - }, - set: function(x) { - if(!Array.isArray(x)) { - x = [x|0,x|0] - } else { - if(x.length !== 2) { - throw new Error('gl-texture2d: Invalid texture shape') - } - } - reshapeTexture(this, x[0]|0, x[1]|0) - return [x[0]|0, x[1]|0] - } - }, - width: { - get: function() { - return this._shape[0] - }, - set: function(w) { - w = w|0 - reshapeTexture(this, w, this._shape[1]) - return w - } - }, - height: { - get: function() { - return this._shape[1] - }, - set: function(h) { - h = h|0 - reshapeTexture(this, this._shape[0], h) - return h - } - } -}) - -proto.bind = function(unit) { - var gl = this.gl - if(unit !== undefined) { - gl.activeTexture(gl.TEXTURE0 + (unit|0)) - } - gl.bindTexture(gl.TEXTURE_2D, this.handle) - if(unit !== undefined) { - return (unit|0) - } - return gl.getParameter(gl.ACTIVE_TEXTURE) - gl.TEXTURE0 -} - -proto.dispose = function() { - this.gl.deleteTexture(this.handle) -} - -proto.generateMipmap = function() { - this.bind() - this.gl.generateMipmap(this.gl.TEXTURE_2D) - - //Update mip levels - var l = Math.min(this._shape[0], this._shape[1]) - for(var i=0; l>0; ++i, l>>>=1) { - if(this._mipLevels.indexOf(i) < 0) { - this._mipLevels.push(i) - } - } -} - -proto.setPixels = function(data, x_off, y_off, mip_level) { - var gl = this.gl - this.bind() - if(Array.isArray(x_off)) { - mip_level = y_off - y_off = x_off[1]|0 - x_off = x_off[0]|0 - } else { - x_off = x_off || 0 - y_off = y_off || 0 - } - mip_level = mip_level || 0 - var directData = acceptTextureDOM(data) ? data : data.raw - if(directData) { - var needsMip = this._mipLevels.indexOf(mip_level) < 0 - if(needsMip) { - gl.texImage2D(gl.TEXTURE_2D, 0, this.format, this.format, this.type, directData) - this._mipLevels.push(mip_level) - } else { - gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, this.format, this.type, directData) - } - } else if(data.shape && data.stride && data.data) { - if(data.shape.length < 2 || - x_off + data.shape[1] > this._shape[1]>>>mip_level || - y_off + data.shape[0] > this._shape[0]>>>mip_level || - x_off < 0 || - y_off < 0) { - throw new Error('gl-texture2d: Texture dimensions are out of bounds') - } - texSubImageArray(gl, x_off, y_off, mip_level, this.format, this.type, this._mipLevels, data) - } else { - throw new Error('gl-texture2d: Unsupported data type') - } -} - - -function isPacked(shape, stride) { - if(shape.length === 3) { - return (stride[2] === 1) && - (stride[1] === shape[0]*shape[2]) && - (stride[0] === shape[2]) - } - return (stride[0] === 1) && - (stride[1] === shape[0]) -} - -function texSubImageArray(gl, x_off, y_off, mip_level, cformat, ctype, mipLevels, array) { - var dtype = array.dtype - var shape = array.shape.slice() - if(shape.length < 2 || shape.length > 3) { - throw new Error('gl-texture2d: Invalid ndarray, must be 2d or 3d') - } - var type = 0, format = 0 - var packed = isPacked(shape, array.stride.slice()) - if(dtype === 'float32') { - type = gl.FLOAT - } else if(dtype === 'float64') { - type = gl.FLOAT - packed = false - dtype = 'float32' - } else if(dtype === 'uint8') { - type = gl.UNSIGNED_BYTE - } else { - type = gl.UNSIGNED_BYTE - packed = false - dtype = 'uint8' - } - var channels = 1 - if(shape.length === 2) { - format = gl.LUMINANCE - shape = [shape[0], shape[1], 1] - array = ndarray(array.data, shape, [array.stride[0], array.stride[1], 1], array.offset) - } else if(shape.length === 3) { - if(shape[2] === 1) { - format = gl.ALPHA - } else if(shape[2] === 2) { - format = gl.LUMINANCE_ALPHA - } else if(shape[2] === 3) { - format = gl.RGB - } else if(shape[2] === 4) { - format = gl.RGBA - } else { - throw new Error('gl-texture2d: Invalid shape for pixel coords') - } - channels = shape[2] - } else { - throw new Error('gl-texture2d: Invalid shape for texture') - } - //For 1-channel textures allow conversion between formats - if((format === gl.LUMINANCE || format === gl.ALPHA) && - (cformat === gl.LUMINANCE || cformat === gl.ALPHA)) { - format = cformat - } - if(format !== cformat) { - throw new Error('gl-texture2d: Incompatible texture format for setPixels') - } - var size = array.size - var needsMip = mipLevels.indexOf(mip_level) < 0 - if(needsMip) { - mipLevels.push(mip_level) - } - if(type === ctype && packed) { - //Array data types are compatible, can directly copy into texture - if(array.offset === 0 && array.data.length === size) { - if(needsMip) { - gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, array.data) - } else { - gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, array.data) - } - } else { - if(needsMip) { - gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, array.data.subarray(array.offset, array.offset+size)) - } else { - gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, array.data.subarray(array.offset, array.offset+size)) - } - } - } else { - //Need to do type conversion to pack data into buffer - var pack_buffer - if(ctype === gl.FLOAT) { - pack_buffer = pool.mallocFloat32(size) - } else { - pack_buffer = pool.mallocUint8(size) - } - var pack_view = ndarray(pack_buffer, shape, [shape[2], shape[2]*shape[0], 1]) - if(type === gl.FLOAT && ctype === gl.UNSIGNED_BYTE) { - convertFloatToUint8(pack_view, array) - } else { - ops.assign(pack_view, array) - } - if(needsMip) { - gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, pack_buffer.subarray(0, size)) - } else { - gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, pack_buffer.subarray(0, size)) - } - if(ctype === gl.FLOAT) { - pool.freeFloat32(pack_buffer) - } else { - pool.freeUint8(pack_buffer) - } - } -} - -function initTexture(gl) { - var tex = gl.createTexture() - gl.bindTexture(gl.TEXTURE_2D, tex) - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) - return tex -} - -function createTextureShape(gl, width, height, format, type) { - var maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE) - if(width < 0 || width > maxTextureSize || height < 0 || height > maxTextureSize) { - throw new Error('gl-texture2d: Invalid texture shape') - } - if(type === gl.FLOAT && !gl.getExtension('OES_texture_float')) { - throw new Error('gl-texture2d: Floating point textures not supported on this platform') - } - var tex = initTexture(gl) - gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, type, null) - return new Texture2D(gl, tex, width, height, format, type) -} - -function createTextureDOM(gl, directData, width, height, format, type) { - var tex = initTexture(gl) - gl.texImage2D(gl.TEXTURE_2D, 0, format, format, type, directData) - return new Texture2D(gl, tex, width, height, format, type) -} - -//Creates a texture from an ndarray -function createTextureArray(gl, array) { - var dtype = array.dtype - var shape = array.shape.slice() - var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE) - if(shape[0] < 0 || shape[0] > maxSize || shape[1] < 0 || shape[1] > maxSize) { - throw new Error('gl-texture2d: Invalid texture size') - } - var packed = isPacked(shape, array.stride.slice()) - var type = 0 - if(dtype === 'float32') { - type = gl.FLOAT - } else if(dtype === 'float64') { - type = gl.FLOAT - packed = false - dtype = 'float32' - } else if(dtype === 'uint8') { - type = gl.UNSIGNED_BYTE - } else { - type = gl.UNSIGNED_BYTE - packed = false - dtype = 'uint8' - } - var format = 0 - if(shape.length === 2) { - format = gl.LUMINANCE - shape = [shape[0], shape[1], 1] - array = ndarray(array.data, shape, [array.stride[0], array.stride[1], 1], array.offset) - } else if(shape.length === 3) { - if(shape[2] === 1) { - format = gl.ALPHA - } else if(shape[2] === 2) { - format = gl.LUMINANCE_ALPHA - } else if(shape[2] === 3) { - format = gl.RGB - } else if(shape[2] === 4) { - format = gl.RGBA - } else { - throw new Error('gl-texture2d: Invalid shape for pixel coords') - } - } else { - throw new Error('gl-texture2d: Invalid shape for texture') - } - if(type === gl.FLOAT && !gl.getExtension('OES_texture_float')) { - type = gl.UNSIGNED_BYTE - packed = false - } - var buffer, buf_store - var size = array.size - if(!packed) { - var stride = [shape[2], shape[2]*shape[0], 1] - buf_store = pool.malloc(size, dtype) - var buf_array = ndarray(buf_store, shape, stride, 0) - if((dtype === 'float32' || dtype === 'float64') && type === gl.UNSIGNED_BYTE) { - convertFloatToUint8(buf_array, array) - } else { - ops.assign(buf_array, array) - } - buffer = buf_store.subarray(0, size) - } else if (array.offset === 0 && array.data.length === size) { - buffer = array.data - } else { - buffer = array.data.subarray(array.offset, array.offset + size) - } - var tex = initTexture(gl) - gl.texImage2D(gl.TEXTURE_2D, 0, format, shape[0], shape[1], 0, format, type, buffer) - if(!packed) { - pool.free(buf_store) - } - return new Texture2D(gl, tex, shape[0], shape[1], format, type) -} - -function createTexture2D(gl) { - if(arguments.length <= 1) { - throw new Error('gl-texture2d: Missing arguments for texture2d constructor') - } - if(!linearTypes) { - lazyInitLinearTypes(gl) - } - if(typeof arguments[1] === 'number') { - return createTextureShape(gl, arguments[1], arguments[2], arguments[3]||gl.RGBA, arguments[4]||gl.UNSIGNED_BYTE) - } - if(Array.isArray(arguments[1])) { - return createTextureShape(gl, arguments[1][0]|0, arguments[1][1]|0, arguments[2]||gl.RGBA, arguments[3]||gl.UNSIGNED_BYTE) - } - if(typeof arguments[1] === 'object') { - var obj = arguments[1] - var directData = acceptTextureDOM(obj) ? obj : obj.raw - if (directData) { - return createTextureDOM(gl, directData, obj.width|0, obj.height|0, arguments[2]||gl.RGBA, arguments[3]||gl.UNSIGNED_BYTE) - } else if(obj.shape && obj.data && obj.stride) { - return createTextureArray(gl, obj) - } - } - throw new Error('gl-texture2d: Invalid arguments for texture2d constructor') -} - -},{"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":212}],210:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],211:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],212:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":210,"buffer":33,"dup":187}],213:[function(require,module,exports){ - -var createShader = require('gl-shader') - -var vertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position, nextPosition;\nattribute float arcLength, lineWidth;\nattribute vec4 color;\n\nuniform vec2 screenShape;\nuniform float pixelRatio;\nuniform mat4 model, view, projection;\n\nvarying vec4 fragColor;\nvarying vec3 worldPosition;\nvarying float pixelArcLength;\n\nvoid main() {\n vec4 projected = projection * view * model * vec4(position, 1.0);\n vec4 tangentClip = projection * view * model * vec4(nextPosition - position, 0.0);\n vec2 tangent = normalize(screenShape * tangentClip.xy);\n vec2 offset = 0.5 * pixelRatio * lineWidth * vec2(tangent.y, -tangent.x) / screenShape;\n\n gl_Position = vec4(projected.xy + projected.w * offset, projected.zw);\n\n worldPosition = position;\n pixelArcLength = arcLength;\n fragColor = color;\n}\n" -var forwardFrag = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 clipBounds[2];\nuniform sampler2D dashTexture;\nuniform float dashScale;\nuniform float opacity;\n\nvarying vec3 worldPosition;\nvarying float pixelArcLength;\nvarying vec4 fragColor;\n\nvoid main() {\n if(any(lessThan(worldPosition, clipBounds[0])) || any(greaterThan(worldPosition, clipBounds[1]))) {\n discard;\n }\n float dashWeight = texture2D(dashTexture, vec2(dashScale * pixelArcLength, 0)).r;\n if(dashWeight < 0.5) {\n discard;\n }\n gl_FragColor = fragColor * opacity;\n}\n" -var pickFrag = "precision mediump float;\n#define GLSLIFY 1\n\n#define FLOAT_MAX 1.70141184e38\n#define FLOAT_MIN 1.17549435e-38\n\nlowp vec4 encode_float_1_0(highp float v) {\n highp float av = abs(v);\n\n //Handle special cases\n if(av < FLOAT_MIN) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n } else if(v > FLOAT_MAX) {\n return vec4(127.0, 128.0, 0.0, 0.0) / 255.0;\n } else if(v < -FLOAT_MAX) {\n return vec4(255.0, 128.0, 0.0, 0.0) / 255.0;\n }\n\n highp vec4 c = vec4(0,0,0,0);\n\n //Compute exponent and mantissa\n highp float e = floor(log2(av));\n highp float m = av * pow(2.0, -e) - 1.0;\n \n //Unpack mantissa\n c[1] = floor(128.0 * m);\n m -= c[1] / 128.0;\n c[2] = floor(32768.0 * m);\n m -= c[2] / 32768.0;\n c[3] = floor(8388608.0 * m);\n \n //Unpack exponent\n highp float ebias = e + 127.0;\n c[0] = floor(ebias / 2.0);\n ebias -= c[0] * 2.0;\n c[1] += floor(ebias) * 128.0; \n\n //Unpack sign bit\n c[0] += 128.0 * step(0.0, -v);\n\n //Scale back to range\n return c / 255.0;\n}\n\n\n\nuniform float pickId;\nuniform vec3 clipBounds[2];\n\nvarying vec3 worldPosition;\nvarying float pixelArcLength;\nvarying vec4 fragColor;\n\nvoid main() {\n if(any(lessThan(worldPosition, clipBounds[0])) || any(greaterThan(worldPosition, clipBounds[1]))) {\n discard;\n }\n gl_FragColor = vec4(pickId/255.0, encode_float_1_0(pixelArcLength).xyz);\n}" - -var ATTRIBUTES = [ - {name: 'position', type: 'vec3'}, - {name: 'nextPosition', type: 'vec3'}, - {name: 'arcLength', type: 'float'}, - {name: 'lineWidth', type: 'float'}, - {name: 'color', type: 'vec4'} -] - -exports.createShader = function(gl) { - return createShader(gl, vertSrc, forwardFrag, null, ATTRIBUTES) -} - -exports.createPickShader = function(gl) { - return createShader(gl, vertSrc, pickFrag, null, ATTRIBUTES) -} - -},{"gl-shader":835}],214:[function(require,module,exports){ -'use strict' - -module.exports = createLinePlot - -var createBuffer = require('gl-buffer') -var createVAO = require('gl-vao') -var createTexture = require('gl-texture2d') -var unpackFloat = require('glsl-read-float') -var bsearch = require('binary-search-bounds') -var ndarray = require('ndarray') -var shaders = require('./lib/shaders') - -var createShader = shaders.createShader -var createPickShader = shaders.createPickShader - -var identity = [1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1] - -function distance (a, b) { - var s = 0.0 - for (var i = 0; i < 3; ++i) { - var d = a[i] - b[i] - s += d * d - } - return Math.sqrt(s) -} - -function filterClipBounds (bounds) { - var result = [[-1e6, -1e6, -1e6], [1e6, 1e6, 1e6]] - for (var i = 0; i < 3; ++i) { - result[0][i] = Math.max(bounds[0][i], result[0][i]) - result[1][i] = Math.min(bounds[1][i], result[1][i]) - } - return result -} - -function PickResult (tau, position, index, dataCoordinate) { - this.arcLength = tau - this.position = position - this.index = index - this.dataCoordinate = dataCoordinate -} - -function LinePlot (gl, shader, pickShader, buffer, vao, texture) { - this.gl = gl - this.shader = shader - this.pickShader = pickShader - this.buffer = buffer - this.vao = vao - this.clipBounds = [ - [ -Infinity, -Infinity, -Infinity ], - [ Infinity, Infinity, Infinity ]] - this.points = [] - this.arcLength = [] - this.vertexCount = 0 - this.bounds = [[0, 0, 0], [0, 0, 0]] - this.pickId = 0 - this.lineWidth = 1 - this.texture = texture - this.dashScale = 1 - this.opacity = 1 - this.dirty = true - this.pixelRatio = 1 -} - -var proto = LinePlot.prototype - -proto.isTransparent = function () { - return this.opacity < 1 -} - -proto.isOpaque = function () { - return this.opacity >= 1 -} - -proto.pickSlots = 1 - -proto.setPickBase = function (id) { - this.pickId = id -} - -proto.drawTransparent = proto.draw = function (camera) { - var gl = this.gl - var shader = this.shader - var vao = this.vao - shader.bind() - shader.uniforms = { - model: camera.model || identity, - view: camera.view || identity, - projection: camera.projection || identity, - clipBounds: filterClipBounds(this.clipBounds), - dashTexture: this.texture.bind(), - dashScale: this.dashScale / this.arcLength[this.arcLength.length - 1], - opacity: this.opacity, - screenShape: [gl.drawingBufferWidth, gl.drawingBufferHeight], - pixelRatio: this.pixelRatio - } - vao.bind() - vao.draw(gl.TRIANGLE_STRIP, this.vertexCount) -} - -proto.drawPick = function (camera) { - var gl = this.gl - var shader = this.pickShader - var vao = this.vao - shader.bind() - shader.uniforms = { - model: camera.model || identity, - view: camera.view || identity, - projection: camera.projection || identity, - pickId: this.pickId, - clipBounds: filterClipBounds(this.clipBounds), - screenShape: [gl.drawingBufferWidth, gl.drawingBufferHeight], - pixelRatio: this.pixelRatio - } - vao.bind() - vao.draw(gl.TRIANGLE_STRIP, this.vertexCount) -} - -proto.update = function (options) { - var i, j - - this.dirty = true - - var connectGaps = !!options.connectGaps - - if ('dashScale' in options) { - this.dashScale = options.dashScale - } - if ('opacity' in options) { - this.opacity = +options.opacity - } - - var positions = options.position || options.positions - if (!positions) { - return - } - - // Default color - var colors = options.color || options.colors || [0, 0, 0, 1] - - var lineWidth = options.lineWidth || 1 - - // Recalculate buffer data - var buffer = [] - var arcLengthArray = [] - var pointArray = [] - var arcLength = 0.0 - var vertexCount = 0 - var bounds = [ - [ Infinity, Infinity, Infinity ], - [ -Infinity, -Infinity, -Infinity ]] - var hadGap = false - - fill_loop: - for (i = 1; i < positions.length; ++i) { - var a = positions[i - 1] - var b = positions[i] - - arcLengthArray.push(arcLength) - pointArray.push(a.slice()) - - for (j = 0; j < 3; ++j) { - if (isNaN(a[j]) || isNaN(b[j]) || - !isFinite(a[j]) || !isFinite(b[j])) { - - if (!connectGaps && buffer.length > 0) { - for (var k = 0; k < 24; ++k) { - buffer.push(buffer[buffer.length - 12]) - } - vertexCount += 2 - hadGap = true - } - - continue fill_loop - } - bounds[0][j] = Math.min(bounds[0][j], a[j], b[j]) - bounds[1][j] = Math.max(bounds[1][j], a[j], b[j]) - } - - var acolor, bcolor - if (Array.isArray(colors[0])) { - acolor = colors[i - 1] - bcolor = colors[i] - } else { - acolor = bcolor = colors - } - if (acolor.length === 3) { - acolor = [acolor[0], acolor[1], acolor[2], 1] - } - if (bcolor.length === 3) { - bcolor = [bcolor[0], bcolor[1], bcolor[2], 1] - } - - var w0 - if (Array.isArray(lineWidth)) { - w0 = lineWidth[i - 1] - } else { - w0 = lineWidth - } - - var t0 = arcLength - arcLength += distance(a, b) - - if (hadGap) { - for (j = 0; j < 2; ++j) { - buffer.push( - a[0], a[1], a[2], b[0], b[1], b[2], t0, w0, acolor[0], acolor[1], acolor[2], acolor[3]) - } - vertexCount += 2 - hadGap = false - } - - buffer.push( - a[0], a[1], a[2], b[0], b[1], b[2], t0, w0, acolor[0], acolor[1], acolor[2], acolor[3], - a[0], a[1], a[2], b[0], b[1], b[2], t0, -w0, acolor[0], acolor[1], acolor[2], acolor[3], - b[0], b[1], b[2], a[0], a[1], a[2], arcLength, -w0, bcolor[0], bcolor[1], bcolor[2], bcolor[3], - b[0], b[1], b[2], a[0], a[1], a[2], arcLength, w0, bcolor[0], bcolor[1], bcolor[2], bcolor[3]) - - vertexCount += 4 - } - this.buffer.update(buffer) - - arcLengthArray.push(arcLength) - pointArray.push(positions[positions.length - 1].slice()) - - this.bounds = bounds - - this.vertexCount = vertexCount - - this.points = pointArray - this.arcLength = arcLengthArray - - if ('dashes' in options) { - var dashArray = options.dashes - - // Calculate prefix sum - var prefixSum = dashArray.slice() - prefixSum.unshift(0) - for (i = 1; i < prefixSum.length; ++i) { - prefixSum[i] = prefixSum[i - 1] + prefixSum[i] - } - - var dashTexture = ndarray(new Array(256 * 4), [256, 1, 4]) - for (i = 0; i < 256; ++i) { - for (j = 0; j < 4; ++j) { - dashTexture.set(i, 0, j, 0) - } - if (bsearch.le(prefixSum, prefixSum[prefixSum.length - 1] * i / 255.0) & 1) { - dashTexture.set(i, 0, 0, 0) - } else { - dashTexture.set(i, 0, 0, 255) - } - } - - this.texture.setPixels(dashTexture) - } -} - -proto.dispose = function () { - this.shader.dispose() - this.vao.dispose() - this.buffer.dispose() -} - -proto.pick = function (selection) { - if (!selection) { - return null - } - if (selection.id !== this.pickId) { - return null - } - var tau = unpackFloat( - selection.value[0], - selection.value[1], - selection.value[2], - 0) - var index = bsearch.le(this.arcLength, tau) - if (index < 0) { - return null - } - if (index === this.arcLength.length - 1) { - return new PickResult( - this.arcLength[this.arcLength.length - 1], - this.points[this.points.length - 1].slice(), - index) - } - var a = this.points[index] - var b = this.points[Math.min(index + 1, this.points.length - 1)] - var t = (tau - this.arcLength[index]) / (this.arcLength[index + 1] - this.arcLength[index]) - var ti = 1.0 - t - var x = [0, 0, 0] - for (var i = 0; i < 3; ++i) { - x[i] = ti * a[i] + t * b[i] - } - var dataIndex = Math.min((t < 0.5) ? index : (index + 1), this.points.length - 1) - return new PickResult( - tau, - x, - dataIndex, - this.points[dataIndex]) -} - -function createLinePlot (options) { - var gl = options.gl || (options.scene && options.scene.gl) - - var shader = createShader(gl) - shader.attributes.position.location = 0 - shader.attributes.nextPosition.location = 1 - shader.attributes.arcLength.location = 2 - shader.attributes.lineWidth.location = 3 - shader.attributes.color.location = 4 - - var pickShader = createPickShader(gl) - pickShader.attributes.position.location = 0 - pickShader.attributes.nextPosition.location = 1 - pickShader.attributes.arcLength.location = 2 - pickShader.attributes.lineWidth.location = 3 - pickShader.attributes.color.location = 4 - - var buffer = createBuffer(gl) - var vao = createVAO(gl, [ - { - 'buffer': buffer, - 'size': 3, - 'offset': 0, - 'stride': 48 - }, - { - 'buffer': buffer, - 'size': 3, - 'offset': 12, - 'stride': 48 - }, - { - 'buffer': buffer, - 'size': 1, - 'offset': 24, - 'stride': 48 - }, - { - 'buffer': buffer, - 'size': 1, - 'offset': 28, - 'stride': 48 - }, - { - 'buffer': buffer, - 'size': 4, - 'offset': 32, - 'stride': 48 - } - ]) - - // Create texture for dash pattern - var defaultTexture = ndarray(new Array(256 * 4), [256, 1, 4]) - for (var i = 0; i < 256 * 4; ++i) { - defaultTexture.data[i] = 255 - } - var texture = createTexture(gl, defaultTexture) - texture.wrap = gl.REPEAT - - var linePlot = new LinePlot(gl, shader, pickShader, buffer, vao, texture) - linePlot.update(options) - return linePlot -} - -},{"./lib/shaders":213,"binary-search-bounds":215,"gl-buffer":216,"gl-texture2d":223,"gl-vao":227,"glsl-read-float":228,"ndarray":1118}],215:[function(require,module,exports){ -arguments[4][118][0].apply(exports,arguments) -},{"dup":118}],216:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":219}],217:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],218:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],219:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":217,"buffer":33,"dup":187}],220:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],221:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],222:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":220,"buffer":33,"dup":187}],223:[function(require,module,exports){ -arguments[4][209][0].apply(exports,arguments) -},{"dup":209,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":222}],224:[function(require,module,exports){ -arguments[4][193][0].apply(exports,arguments) -},{"dup":193}],225:[function(require,module,exports){ -arguments[4][194][0].apply(exports,arguments) -},{"./do-bind.js":224,"dup":194}],226:[function(require,module,exports){ -arguments[4][195][0].apply(exports,arguments) -},{"./do-bind.js":224,"dup":195}],227:[function(require,module,exports){ -arguments[4][196][0].apply(exports,arguments) -},{"./lib/vao-emulated.js":225,"./lib/vao-native.js":226,"dup":196}],228:[function(require,module,exports){ -module.exports = decodeFloat - -var UINT8_VIEW = new Uint8Array(4) -var FLOAT_VIEW = new Float32Array(UINT8_VIEW.buffer) - -function decodeFloat(x, y, z, w) { - UINT8_VIEW[0] = w - UINT8_VIEW[1] = z - UINT8_VIEW[2] = y - UINT8_VIEW[3] = x - return FLOAT_VIEW[0] -} - -},{}],229:[function(require,module,exports){ -module.exports = clone; - -/** - * Creates a new mat4 initialized with values from an existing matrix - * - * @param {mat4} a matrix to clone - * @returns {mat4} a new 4x4 matrix - */ -function clone(a) { - var out = new Float32Array(16); - 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; -}; -},{}],230:[function(require,module,exports){ -module.exports = create; - -/** - * Creates a new identity mat4 - * - * @returns {mat4} a new 4x4 matrix - */ -function create() { - var out = new Float32Array(16); - 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; -}; -},{}],231:[function(require,module,exports){ -module.exports = determinant; - -/** - * Calculates the determinant of a mat4 - * - * @param {mat4} 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], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], - - b00 = a00 * a11 - a01 * a10, - b01 = a00 * a12 - a02 * a10, - b02 = a00 * a13 - a03 * a10, - b03 = a01 * a12 - a02 * a11, - b04 = a01 * a13 - a03 * a11, - b05 = a02 * a13 - a03 * a12, - b06 = a20 * a31 - a21 * a30, - b07 = a20 * a32 - a22 * a30, - b08 = a20 * a33 - a23 * a30, - b09 = a21 * a32 - a22 * a31, - b10 = a21 * a33 - a23 * a31, - b11 = a22 * a33 - a23 * a32; - - // Calculate the determinant - return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; -}; -},{}],232:[function(require,module,exports){ -module.exports = fromQuat; - -/** - * Creates a matrix from a quaternion rotation. - * - * @param {mat4} out mat4 receiving operation result - * @param {quat4} q Rotation quaternion - * @returns {mat4} out - */ -function fromQuat(out, q) { - var x = q[0], y = q[1], z = q[2], w = q[3], - x2 = x + x, - y2 = y + y, - z2 = z + z, - - xx = x * x2, - yx = y * x2, - yy = y * y2, - zx = z * x2, - zy = z * y2, - zz = z * z2, - wx = w * x2, - wy = w * y2, - wz = w * z2; - - out[0] = 1 - yy - zz; - out[1] = yx + wz; - out[2] = zx - wy; - out[3] = 0; - - out[4] = yx - wz; - out[5] = 1 - xx - zz; - out[6] = zy + wx; - out[7] = 0; - - out[8] = zx + wy; - out[9] = zy - wx; - out[10] = 1 - xx - yy; - out[11] = 0; - - out[12] = 0; - out[13] = 0; - out[14] = 0; - out[15] = 1; - - return out; -}; -},{}],233:[function(require,module,exports){ -module.exports = fromRotationTranslation; - -/** - * Creates a matrix from a quaternion rotation and vector translation - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.translate(dest, vec); - * var quatMat = mat4.create(); - * quat4.toMat4(quat, quatMat); - * mat4.multiply(dest, quatMat); - * - * @param {mat4} out mat4 receiving operation result - * @param {quat4} q Rotation quaternion - * @param {vec3} v Translation vector - * @returns {mat4} out - */ -function fromRotationTranslation(out, q, v) { - // Quaternion math - var x = q[0], y = q[1], z = q[2], w = q[3], - x2 = x + x, - y2 = y + y, - z2 = z + z, - - xx = x * x2, - xy = x * y2, - xz = x * z2, - yy = y * y2, - yz = y * z2, - zz = z * z2, - wx = w * x2, - wy = w * y2, - wz = w * z2; - - out[0] = 1 - (yy + zz); - out[1] = xy + wz; - out[2] = xz - wy; - out[3] = 0; - out[4] = xy - wz; - out[5] = 1 - (xx + zz); - out[6] = yz + wx; - out[7] = 0; - out[8] = xz + wy; - out[9] = yz - wx; - out[10] = 1 - (xx + yy); - out[11] = 0; - out[12] = v[0]; - out[13] = v[1]; - out[14] = v[2]; - out[15] = 1; - - return out; -}; -},{}],234:[function(require,module,exports){ -module.exports = identity; - -/** - * 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; -}; -},{}],235:[function(require,module,exports){ -module.exports = invert; - -/** - * Inverts a mat4 - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -function invert(out, a) { - var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], - - b00 = a00 * a11 - a01 * a10, - b01 = a00 * a12 - a02 * a10, - b02 = a00 * a13 - a03 * a10, - b03 = a01 * a12 - a02 * a11, - b04 = a01 * a13 - a03 * a11, - b05 = a02 * a13 - a03 * a12, - b06 = a20 * a31 - a21 * a30, - b07 = a20 * a32 - a22 * a30, - b08 = a20 * a33 - a23 * a30, - b09 = a21 * a32 - a22 * a31, - b10 = a21 * a33 - a23 * a31, - b11 = a22 * a33 - a23 * a32, - - // Calculate the determinant - 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; -}; -},{}],236:[function(require,module,exports){ -var identity = require('./identity'); - -module.exports = lookAt; - -/** - * Generates a look-at matrix with the given eye position, focal point, and up axis - * - * @param {mat4} out mat4 frustum matrix will be written into - * @param {vec3} eye Position of the viewer - * @param {vec3} center Point the viewer is looking at - * @param {vec3} up vec3 pointing up - * @returns {mat4} out - */ -function lookAt(out, eye, center, up) { - var x0, x1, x2, y0, y1, y2, z0, z1, z2, len, - eyex = eye[0], - eyey = eye[1], - eyez = eye[2], - upx = up[0], - upy = up[1], - upz = up[2], - centerx = center[0], - centery = center[1], - centerz = center[2]; - - if (Math.abs(eyex - centerx) < 0.000001 && - Math.abs(eyey - centery) < 0.000001 && - Math.abs(eyez - centerz) < 0.000001) { - return identity(out); - } - - z0 = eyex - centerx; - z1 = eyey - centery; - z2 = eyez - centerz; - - len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); - z0 *= len; - z1 *= len; - z2 *= len; - - x0 = upy * z2 - upz * z1; - x1 = upz * z0 - upx * z2; - x2 = upx * z1 - upy * z0; - len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); - if (!len) { - x0 = 0; - x1 = 0; - x2 = 0; - } else { - len = 1 / len; - x0 *= len; - x1 *= len; - x2 *= len; - } - - y0 = z1 * x2 - z2 * x1; - y1 = z2 * x0 - z0 * x2; - y2 = z0 * x1 - z1 * x0; - - len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); - if (!len) { - y0 = 0; - y1 = 0; - y2 = 0; - } else { - len = 1 / len; - y0 *= len; - y1 *= len; - y2 *= len; - } - - out[0] = x0; - out[1] = y0; - out[2] = z0; - out[3] = 0; - out[4] = x1; - out[5] = y1; - out[6] = z1; - out[7] = 0; - out[8] = x2; - out[9] = y2; - out[10] = z2; - out[11] = 0; - out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); - out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); - out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); - out[15] = 1; - - return out; -}; -},{"./identity":234}],237:[function(require,module,exports){ -module.exports = multiply; - -/** - * Multiplies two mat4's - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the first operand - * @param {mat4} b the second operand - * @returns {mat4} out - */ -function multiply(out, a, b) { - var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; - - // Cache only the current line of the second matrix - var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; - out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - - b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7]; - out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - - b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11]; - out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - - b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15]; - out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - return out; -}; -},{}],238:[function(require,module,exports){ -module.exports = perspective; - -/** - * Generates a perspective projection matrix with the given bounds - * - * @param {mat4} out mat4 frustum matrix will be written into - * @param {number} fovy Vertical field of view in radians - * @param {number} aspect Aspect ratio. typically viewport width/height - * @param {number} near Near bound of the frustum - * @param {number} far Far bound of the frustum - * @returns {mat4} out - */ -function perspective(out, fovy, aspect, near, far) { - var f = 1.0 / Math.tan(fovy / 2), - nf = 1 / (near - far); - out[0] = f / aspect; - out[1] = 0; - out[2] = 0; - out[3] = 0; - out[4] = 0; - out[5] = f; - out[6] = 0; - out[7] = 0; - out[8] = 0; - out[9] = 0; - out[10] = (far + near) * nf; - out[11] = -1; - out[12] = 0; - out[13] = 0; - out[14] = (2 * far * near) * nf; - out[15] = 0; - return out; -}; -},{}],239:[function(require,module,exports){ -module.exports = rotate; - -/** - * Rotates a mat4 by the given angle - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @param {vec3} axis the axis to rotate around - * @returns {mat4} out - */ -function rotate(out, a, rad, axis) { - var x = axis[0], y = axis[1], z = axis[2], - len = Math.sqrt(x * x + y * y + z * z), - s, c, t, - a00, a01, a02, a03, - a10, a11, a12, a13, - a20, a21, a22, a23, - b00, b01, b02, - b10, b11, b12, - b20, b21, b22; - - if (Math.abs(len) < 0.000001) { return null; } - - len = 1 / len; - x *= len; - y *= len; - z *= len; - - s = Math.sin(rad); - c = Math.cos(rad); - t = 1 - c; - - a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; - a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; - a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; - - // Construct the elements of the rotation matrix - b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s; - b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s; - b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c; - - // Perform rotation-specific matrix multiplication - out[0] = a00 * b00 + a10 * b01 + a20 * b02; - out[1] = a01 * b00 + a11 * b01 + a21 * b02; - out[2] = a02 * b00 + a12 * b01 + a22 * b02; - out[3] = a03 * b00 + a13 * b01 + a23 * b02; - out[4] = a00 * b10 + a10 * b11 + a20 * b12; - out[5] = a01 * b10 + a11 * b11 + a21 * b12; - out[6] = a02 * b10 + a12 * b11 + a22 * b12; - out[7] = a03 * b10 + a13 * b11 + a23 * b12; - out[8] = a00 * b20 + a10 * b21 + a20 * b22; - out[9] = a01 * b20 + a11 * b21 + a21 * b22; - out[10] = a02 * b20 + a12 * b21 + a22 * b22; - out[11] = a03 * b20 + a13 * b21 + a23 * b22; - - if (a !== out) { // If the source and destination differ, copy the unchanged last row - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - return out; -}; -},{}],240:[function(require,module,exports){ -module.exports = rotateX; - -/** - * Rotates a matrix by the given angle around the X axis - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -function rotateX(out, a, rad) { - var s = Math.sin(rad), - c = Math.cos(rad), - a10 = a[4], - a11 = a[5], - a12 = a[6], - a13 = a[7], - a20 = a[8], - a21 = a[9], - a22 = a[10], - a23 = a[11]; - - if (a !== out) { // If the source and destination differ, copy the unchanged rows - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - - // Perform axis-specific matrix multiplication - out[4] = a10 * c + a20 * s; - out[5] = a11 * c + a21 * s; - out[6] = a12 * c + a22 * s; - out[7] = a13 * c + a23 * s; - out[8] = a20 * c - a10 * s; - out[9] = a21 * c - a11 * s; - out[10] = a22 * c - a12 * s; - out[11] = a23 * c - a13 * s; - return out; -}; -},{}],241:[function(require,module,exports){ -module.exports = rotateY; - -/** - * Rotates a matrix by the given angle around the Y axis - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -function rotateY(out, a, rad) { - var s = Math.sin(rad), - c = Math.cos(rad), - a00 = a[0], - a01 = a[1], - a02 = a[2], - a03 = a[3], - a20 = a[8], - a21 = a[9], - a22 = a[10], - a23 = a[11]; - - if (a !== out) { // If the source and destination differ, copy the unchanged rows - out[4] = a[4]; - out[5] = a[5]; - out[6] = a[6]; - out[7] = a[7]; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - - // Perform axis-specific matrix multiplication - out[0] = a00 * c - a20 * s; - out[1] = a01 * c - a21 * s; - out[2] = a02 * c - a22 * s; - out[3] = a03 * c - a23 * s; - out[8] = a00 * s + a20 * c; - out[9] = a01 * s + a21 * c; - out[10] = a02 * s + a22 * c; - out[11] = a03 * s + a23 * c; - return out; -}; -},{}],242:[function(require,module,exports){ -module.exports = rotateZ; - -/** - * Rotates a matrix by the given angle around the Z axis - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -function rotateZ(out, a, rad) { - var s = Math.sin(rad), - c = Math.cos(rad), - a00 = a[0], - a01 = a[1], - a02 = a[2], - a03 = a[3], - a10 = a[4], - a11 = a[5], - a12 = a[6], - a13 = a[7]; - - if (a !== out) { // If the source and destination differ, copy the unchanged last row - 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]; - } - - // Perform axis-specific matrix multiplication - out[0] = a00 * c + a10 * s; - out[1] = a01 * c + a11 * s; - out[2] = a02 * c + a12 * s; - out[3] = a03 * c + a13 * s; - out[4] = a10 * c - a00 * s; - out[5] = a11 * c - a01 * s; - out[6] = a12 * c - a02 * s; - out[7] = a13 * c - a03 * s; - return out; -}; -},{}],243:[function(require,module,exports){ -module.exports = scale; - -/** - * Scales the mat4 by the dimensions in the given vec3 - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to scale - * @param {vec3} v the vec3 to scale the matrix by - * @returns {mat4} out - **/ -function scale(out, a, v) { - var x = v[0], y = v[1], z = v[2]; - - out[0] = a[0] * x; - out[1] = a[1] * x; - out[2] = a[2] * x; - out[3] = a[3] * x; - out[4] = a[4] * y; - out[5] = a[5] * y; - out[6] = a[6] * y; - out[7] = a[7] * y; - out[8] = a[8] * z; - out[9] = a[9] * z; - out[10] = a[10] * z; - out[11] = a[11] * z; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - return out; -}; -},{}],244:[function(require,module,exports){ -module.exports = translate; - -/** - * Translate a mat4 by the given vector - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to translate - * @param {vec3} v vector to translate by - * @returns {mat4} out - */ -function translate(out, a, v) { - var x = v[0], y = v[1], z = v[2], - a00, a01, a02, a03, - a10, a11, a12, a13, - a20, a21, a22, a23; - - if (a === out) { - out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; - out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; - out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; - out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; - } else { - a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; - a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; - a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; - - out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03; - out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13; - out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23; - - out[12] = a00 * x + a10 * y + a20 * z + a[12]; - out[13] = a01 * x + a11 * y + a21 * z + a[13]; - out[14] = a02 * x + a12 * y + a22 * z + a[14]; - out[15] = a03 * x + a13 * y + a23 * z + a[15]; - } - - return out; -}; -},{}],245:[function(require,module,exports){ -module.exports = transpose; - -/** - * Transpose the values of a mat4 - * - * @param {mat4} out the receiving matrix - * @param {mat4} 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], - a12 = a[6], a13 = a[7], - 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; -}; -},{}],246:[function(require,module,exports){ -'use strict' - -var barycentric = require('barycentric') -var closestPointToTriangle = require('polytope-closest-point/lib/closest_point_2d.js') - -module.exports = closestPointToPickLocation - -function xformMatrix(m, v) { - var out = [0,0,0,0] - for(var i=0; i<4; ++i) { - for(var j=0; j<4; ++j) { - out[j] += m[4*i + j] * v[i] - } - } - return out -} - -function projectVertex(v, model, view, projection, resolution) { - var p = xformMatrix(projection, - xformMatrix(view, - xformMatrix(model, [v[0], v[1], v[2], 1]))) - for(var i=0; i<3; ++i) { - p[i] /= p[3] - } - return [ 0.5 * resolution[0] * (1.0+p[0]), 0.5 * resolution[1] * (1.0-p[1]) ] -} - -function barycentricCoord(simplex, point) { - if(simplex.length === 2) { - var d0 = 0.0 - var d1 = 0.0 - for(var i=0; i<2; ++i) { - d0 += Math.pow(point[i] - simplex[0][i], 2) - d1 += Math.pow(point[i] - simplex[1][i], 2) - } - d0 = Math.sqrt(d0) - d1 = Math.sqrt(d1) - if(d0+d1 < 1e-6) { - return [1,0] - } - return [d1/(d0+d1),d0/(d1+d0)] - } else if(simplex.length === 3) { - var closestPoint = [0,0] - closestPointToTriangle(simplex[0], simplex[1], simplex[2], point, closestPoint) - return barycentric(simplex, closestPoint) - } - return [] -} - -function interpolate(simplex, weights) { - var result = [0,0,0] - for(var i=0; i 1.0001) { - return null - } - s += weights[i] - } - if(Math.abs(s - 1.0) > 0.001) { - return null - } - return [closestIndex, interpolate(simplex, weights), weights] -} -},{"barycentric":249,"polytope-closest-point/lib/closest_point_2d.js":267}],247:[function(require,module,exports){ - - -var triVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position, normal;\nattribute vec4 color;\nattribute vec2 uv;\n\nuniform mat4 model\n , view\n , projection;\nuniform vec3 eyePosition\n , lightPosition;\n\nvarying vec3 f_normal\n , f_lightDirection\n , f_eyeDirection\n , f_data;\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n vec4 m_position = model * vec4(position, 1.0);\n vec4 t_position = view * m_position;\n gl_Position = projection * t_position;\n f_color = color;\n f_normal = normal;\n f_data = position;\n f_eyeDirection = eyePosition - position;\n f_lightDirection = lightPosition - position;\n f_uv = uv;\n}" -var triFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nfloat beckmannDistribution_2_0(float x, float roughness) {\n float NdotH = max(x, 0.0001);\n float cos2Alpha = NdotH * NdotH;\n float tan2Alpha = (cos2Alpha - 1.0) / cos2Alpha;\n float roughness2 = roughness * roughness;\n float denom = 3.141592653589793 * roughness2 * cos2Alpha * cos2Alpha;\n return exp(tan2Alpha / roughness2) / denom;\n}\n\n\n\nfloat cookTorranceSpecular_1_1(\n vec3 lightDirection,\n vec3 viewDirection,\n vec3 surfaceNormal,\n float roughness,\n float fresnel) {\n\n float VdotN = max(dot(viewDirection, surfaceNormal), 0.0);\n float LdotN = max(dot(lightDirection, surfaceNormal), 0.0);\n\n //Half angle vector\n vec3 H = normalize(lightDirection + viewDirection);\n\n //Geometric term\n float NdotH = max(dot(surfaceNormal, H), 0.0);\n float VdotH = max(dot(viewDirection, H), 0.000001);\n float LdotH = max(dot(lightDirection, H), 0.000001);\n float G1 = (2.0 * NdotH * VdotN) / VdotH;\n float G2 = (2.0 * NdotH * LdotN) / LdotH;\n float G = min(1.0, min(G1, G2));\n \n //Distribution term\n float D = beckmannDistribution_2_0(NdotH, roughness);\n\n //Fresnel term\n float F = pow(1.0 - VdotN, fresnel);\n\n //Multiply terms and done\n return G * F * D / max(3.14159265 * VdotN, 0.000001);\n}\n\n\n\nuniform vec3 clipBounds[2];\nuniform float roughness\n , fresnel\n , kambient\n , kdiffuse\n , kspecular\n , opacity;\nuniform sampler2D texture;\n\nvarying vec3 f_normal\n , f_lightDirection\n , f_eyeDirection\n , f_data;\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n if(any(lessThan(f_data, clipBounds[0])) || \n any(greaterThan(f_data, clipBounds[1]))) {\n discard;\n }\n\n vec3 N = normalize(f_normal);\n vec3 L = normalize(f_lightDirection);\n vec3 V = normalize(f_eyeDirection);\n \n if(!gl_FrontFacing) {\n N = -N;\n }\n\n float specular = cookTorranceSpecular_1_1(L, V, N, roughness, fresnel);\n float diffuse = min(kambient + kdiffuse * max(dot(N, L), 0.0), 1.0);\n\n vec4 surfaceColor = f_color * texture2D(texture, f_uv);\n vec4 litColor = surfaceColor.a * vec4(diffuse * surfaceColor.rgb + kspecular * vec3(1,1,1) * specular, 1.0);\n\n gl_FragColor = litColor * opacity;\n}" -var edgeVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute vec4 color;\nattribute vec2 uv;\n\nuniform mat4 model, view, projection;\n\nvarying vec4 f_color;\nvarying vec3 f_data;\nvarying vec2 f_uv;\n\nvoid main() {\n gl_Position = projection * view * model * vec4(position, 1.0);\n f_color = color;\n f_data = position;\n f_uv = uv;\n}" -var edgeFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 clipBounds[2];\nuniform sampler2D texture;\nuniform float opacity;\n\nvarying vec4 f_color;\nvarying vec3 f_data;\nvarying vec2 f_uv;\n\nvoid main() {\n if(any(lessThan(f_data, clipBounds[0])) || \n any(greaterThan(f_data, clipBounds[1]))) {\n discard;\n }\n\n gl_FragColor = f_color * texture2D(texture, f_uv) * opacity;\n}" -var pointVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute vec4 color;\nattribute vec2 uv;\nattribute float pointSize;\n\nuniform mat4 model, view, projection;\nuniform vec3 clipBounds[2];\n\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n if(any(lessThan(position, clipBounds[0])) || \n any(greaterThan(position, clipBounds[1]))) {\n gl_Position = vec4(0,0,0,0);\n } else {\n gl_Position = projection * view * model * vec4(position, 1.0);\n }\n gl_PointSize = pointSize;\n f_color = color;\n f_uv = uv;\n}" -var pointFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform sampler2D texture;\nuniform float opacity;\n\nvarying vec4 f_color;\nvarying vec2 f_uv;\n\nvoid main() {\n vec2 pointR = gl_PointCoord.xy - vec2(0.5,0.5);\n if(dot(pointR, pointR) > 0.25) {\n discard;\n }\n gl_FragColor = f_color * texture2D(texture, f_uv) * opacity;\n}" -var pickVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute vec4 id;\n\nuniform mat4 model, view, projection;\n\nvarying vec3 f_position;\nvarying vec4 f_id;\n\nvoid main() {\n gl_Position = projection * view * model * vec4(position, 1.0);\n f_id = id;\n f_position = position;\n}" -var pickFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 clipBounds[2];\nuniform float pickId;\n\nvarying vec3 f_position;\nvarying vec4 f_id;\n\nvoid main() {\n if(any(lessThan(f_position, clipBounds[0])) || \n any(greaterThan(f_position, clipBounds[1]))) {\n discard;\n }\n gl_FragColor = vec4(pickId, f_id.xyz);\n}" -var pickPointVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\nattribute float pointSize;\nattribute vec4 id;\n\nuniform mat4 model, view, projection;\nuniform vec3 clipBounds[2];\n\nvarying vec3 f_position;\nvarying vec4 f_id;\n\nvoid main() {\n if(any(lessThan(position, clipBounds[0])) || \n any(greaterThan(position, clipBounds[1]))) {\n gl_Position = vec4(0,0,0,0);\n } else {\n gl_Position = projection * view * model * vec4(position, 1.0);\n gl_PointSize = pointSize;\n }\n f_id = id;\n f_position = position;\n}" -var contourVertSrc = "precision mediump float;\n#define GLSLIFY 1\n\nattribute vec3 position;\n\nuniform mat4 model, view, projection;\n\nvoid main() {\n gl_Position = projection * view * model * vec4(position, 1.0);\n}" -var contourFragSrc = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec3 contourColor;\n\nvoid main() {\n gl_FragColor = vec4(contourColor,1);\n}\n" - -exports.meshShader = { - vertex: triVertSrc, - fragment: triFragSrc, - attributes: [ - {name: 'position', type: 'vec3'}, - {name: 'normal', type: 'vec3'}, - {name: 'color', type: 'vec4'}, - {name: 'uv', type: 'vec2'} - ] -} -exports.wireShader = { - vertex: edgeVertSrc, - fragment: edgeFragSrc, - attributes: [ - {name: 'position', type: 'vec3'}, - {name: 'color', type: 'vec4'}, - {name: 'uv', type: 'vec2'} - ] -} -exports.pointShader = { - vertex: pointVertSrc, - fragment: pointFragSrc, - attributes: [ - {name: 'position', type: 'vec3'}, - {name: 'color', type: 'vec4'}, - {name: 'uv', type: 'vec2'}, - {name: 'pointSize', type: 'float'} - ] -} -exports.pickShader = { - vertex: pickVertSrc, - fragment: pickFragSrc, - attributes: [ - {name: 'position', type: 'vec3'}, - {name: 'id', type: 'vec4'} - ] -} -exports.pointPickShader = { - vertex: pickPointVertSrc, - fragment: pickFragSrc, - attributes: [ - {name: 'position', type: 'vec3'}, - {name: 'pointSize', type: 'float'}, - {name: 'id', type: 'vec4'} - ] -} -exports.contourShader = { - vertex: contourVertSrc, - fragment: contourFragSrc, - attributes: [ - {name: 'position', type: 'vec3'} - ] -} - -},{}],248:[function(require,module,exports){ -'use strict' - -var DEFAULT_VERTEX_NORMALS_EPSILON = 1e-6; // may be too large if triangles are very small -var DEFAULT_FACE_NORMALS_EPSILON = 1e-6; - -var createShader = require('gl-shader') -var createBuffer = require('gl-buffer') -var createVAO = require('gl-vao') -var createTexture = require('gl-texture2d') -var normals = require('normals') -var multiply = require('gl-mat4/multiply') -var invert = require('gl-mat4/invert') -var ndarray = require('ndarray') -var colormap = require('colormap') -var getContour = require('simplicial-complex-contour') -var pool = require('typedarray-pool') -var shaders = require('./lib/shaders') -var closestPoint = require('./lib/closest-point') - -var meshShader = shaders.meshShader -var wireShader = shaders.wireShader -var pointShader = shaders.pointShader -var pickShader = shaders.pickShader -var pointPickShader = shaders.pointPickShader -var contourShader = shaders.contourShader - -var identityMatrix = [ - 1,0,0,0, - 0,1,0,0, - 0,0,1,0, - 0,0,0,1] - -function SimplicialMesh(gl - , texture - , triShader - , lineShader - , pointShader - , pickShader - , pointPickShader - , contourShader - , trianglePositions - , triangleIds - , triangleColors - , triangleUVs - , triangleNormals - , triangleVAO - , edgePositions - , edgeIds - , edgeColors - , edgeUVs - , edgeVAO - , pointPositions - , pointIds - , pointColors - , pointUVs - , pointSizes - , pointVAO - , contourPositions - , contourVAO) { - - this.gl = gl - this.cells = [] - this.positions = [] - this.intensity = [] - this.texture = texture - this.dirty = true - - this.triShader = triShader - this.lineShader = lineShader - this.pointShader = pointShader - this.pickShader = pickShader - this.pointPickShader = pointPickShader - this.contourShader = contourShader - - this.trianglePositions = trianglePositions - this.triangleColors = triangleColors - this.triangleNormals = triangleNormals - this.triangleUVs = triangleUVs - this.triangleIds = triangleIds - this.triangleVAO = triangleVAO - this.triangleCount = 0 - - this.lineWidth = 1 - this.edgePositions = edgePositions - this.edgeColors = edgeColors - this.edgeUVs = edgeUVs - this.edgeIds = edgeIds - this.edgeVAO = edgeVAO - this.edgeCount = 0 - - this.pointPositions = pointPositions - this.pointColors = pointColors - this.pointUVs = pointUVs - this.pointSizes = pointSizes - this.pointIds = pointIds - this.pointVAO = pointVAO - this.pointCount = 0 - - this.contourLineWidth = 1 - this.contourPositions = contourPositions - this.contourVAO = contourVAO - this.contourCount = 0 - this.contourColor = [0,0,0] - this.contourEnable = true - - this.pickId = 1 - this.bounds = [ - [ Infinity, Infinity, Infinity], - [-Infinity,-Infinity,-Infinity] ] - this.clipBounds = [ - [-Infinity,-Infinity,-Infinity], - [ Infinity, Infinity, Infinity] ] - - this.lightPosition = [1e5, 1e5, 0] - this.ambientLight = 0.8 - this.diffuseLight = 0.8 - this.specularLight = 2.0 - this.roughness = 0.5 - this.fresnel = 1.5 - - this.opacity = 1.0 - - this._model = identityMatrix - this._view = identityMatrix - this._projection = identityMatrix - this._resolution = [1,1] -} - -var proto = SimplicialMesh.prototype - -proto.isOpaque = function() { - return this.opacity >= 1 -} - -proto.isTransparent = function() { - return this.opacity < 1 -} - -proto.pickSlots = 1 - -proto.setPickBase = function(id) { - this.pickId = id -} - -function genColormap(param) { - var colors = colormap({ - colormap: param - , nshades: 256 - , format: 'rgba' - }) - - var result = new Uint8Array(256*4) - for(var i=0; i<256; ++i) { - var c = colors[i] - for(var j=0; j<3; ++j) { - result[4*i+j] = c[j] - } - result[4*i+3] = c[3]*255 - } - - return ndarray(result, [256,256,4], [4,0,1]) -} - -function unpackIntensity(cells, numVerts, cellIntensity) { - var result = new Array(numVerts) - for(var i=0; i 0) { - var shader = this.triShader - shader.bind() - shader.uniforms = uniforms - - this.triangleVAO.bind() - gl.drawArrays(gl.TRIANGLES, 0, this.triangleCount*3) - this.triangleVAO.unbind() - } - - if(this.edgeCount > 0 && this.lineWidth > 0) { - var shader = this.lineShader - shader.bind() - shader.uniforms = uniforms - - this.edgeVAO.bind() - gl.lineWidth(this.lineWidth) - gl.drawArrays(gl.LINES, 0, this.edgeCount*2) - this.edgeVAO.unbind() - } - - if(this.pointCount > 0) { - var shader = this.pointShader - shader.bind() - shader.uniforms = uniforms - - this.pointVAO.bind() - gl.drawArrays(gl.POINTS, 0, this.pointCount) - this.pointVAO.unbind() - } - - if(this.contourEnable && this.contourCount > 0 && this.contourLineWidth > 0) { - var shader = this.contourShader - shader.bind() - shader.uniforms = uniforms - - this.contourVAO.bind() - gl.drawArrays(gl.LINES, 0, this.contourCount) - this.contourVAO.unbind() - } -} - -proto.drawPick = function(params) { - params = params || {} - - var gl = this.gl - - var model = params.model || identityMatrix - var view = params.view || identityMatrix - var projection = params.projection || identityMatrix - - var clipBounds = [[-1e6,-1e6,-1e6],[1e6,1e6,1e6]] - for(var i=0; i<3; ++i) { - clipBounds[0][i] = Math.max(clipBounds[0][i], this.clipBounds[0][i]) - clipBounds[1][i] = Math.min(clipBounds[1][i], this.clipBounds[1][i]) - } - - //Save camera parameters - this._model = [].slice.call(model) - this._view = [].slice.call(view) - this._projection = [].slice.call(projection) - this._resolution = [gl.drawingBufferWidth, gl.drawingBufferHeight] - - var uniforms = { - model: model, - view: view, - projection: projection, - clipBounds: clipBounds, - pickId: this.pickId / 255.0, - } - - var shader = this.pickShader - shader.bind() - shader.uniforms = uniforms - - if(this.triangleCount > 0) { - this.triangleVAO.bind() - gl.drawArrays(gl.TRIANGLES, 0, this.triangleCount*3) - this.triangleVAO.unbind() - } - - if(this.edgeCount > 0) { - this.edgeVAO.bind() - gl.lineWidth(this.lineWidth) - gl.drawArrays(gl.LINES, 0, this.edgeCount*2) - this.edgeVAO.unbind() - } - - if(this.pointCount > 0) { - var shader = this.pointPickShader - shader.bind() - shader.uniforms = uniforms - - this.pointVAO.bind() - gl.drawArrays(gl.POINTS, 0, this.pointCount) - this.pointVAO.unbind() - } -} - - -proto.pick = function(pickData) { - if(!pickData) { - return null - } - if(pickData.id !== this.pickId) { - return null - } - - var cellId = pickData.value[0] + 256*pickData.value[1] + 65536*pickData.value[2] - var cell = this.cells[cellId] - var positions = this.positions - - var simplex = new Array(cell.length) - for(var i=0; i nshades) { - throw new Error( - colormap+' map requires nshades to be at least size '+cmap.length + return interpolate( + input, + base, + parameters.stops[i - 1][0], + parameters.stops[i][0], + parameters.stops[i - 1][1], + parameters.stops[i][1] ); } +} - if (!Array.isArray(spec.alpha)) { +function evaluateIdentityFunction(parameters, input) { + return input; +} - if (typeof spec.alpha === 'number') { - alpha = [spec.alpha, spec.alpha]; - - } else { - alpha = [1, 1]; - } - - } else if (spec.alpha.length !== 2) { - alpha = [1, 1]; +function interpolate(input, base, inputLower, inputUpper, outputLower, outputUpper) { + if (typeof outputLower === 'function') { + return function() { + var evaluatedLower = outputLower.apply(undefined, arguments); + var evaluatedUpper = outputUpper.apply(undefined, arguments); + return interpolate(input, base, inputLower, inputUpper, evaluatedLower, evaluatedUpper); + }; + } else if (outputLower.length) { + return interpolateArray(input, base, inputLower, inputUpper, outputLower, outputUpper); } else { - alpha = clone(spec.alpha); + return interpolateNumber(input, base, inputLower, inputUpper, outputLower, outputUpper); + } +} + +function interpolateNumber(input, base, inputLower, inputUpper, outputLower, outputUpper) { + var difference = inputUpper - inputLower; + var progress = input - inputLower; + + var ratio; + if (base === 1) { + ratio = progress / difference; + } else { + ratio = (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1); } - /* - * map index points from 0->1 to 0 -> n-1 - */ - indicies = cmap.map(function(c) { - return Math.round(c.index * nshades); - }); + return (outputLower * (1 - ratio)) + (outputUpper * ratio); +} - /* - * Add alpha channel to the map - */ - if (alpha[0] < 0) alpha[0] = 0; - if (alpha[1] < 0) alpha[0] = 0; - if (alpha[0] > 1) alpha[0] = 1; - if (alpha[1] > 1) alpha[0] = 1; - - for (i = 0; i < indicies.length; ++i) { - index = cmap[i].index; - rgba = cmap[i].rgb; - - // if user supplies their own map use it - if (rgba.length === 4 && rgba[3] >= 0 && rgba[3] <= 1) continue; - rgba[3] = alpha[0] + (alpha[1] - alpha[0])*index; +function interpolateArray(input, base, inputLower, inputUpper, outputLower, outputUpper) { + var output = []; + for (var i = 0; i < outputLower.length; i++) { + output[i] = interpolateNumber(input, base, inputLower, inputUpper, outputLower[i], outputUpper[i]); } + return output; +} - /* - * map increasing linear values between indicies to - * linear steps in colorvalues - */ - for (i = 0; i < indicies.length-1; ++i) { - nsteps = indicies[i+1] - indicies[i]; - fromrgba = cmap[i].rgb; - torgba = cmap[i+1].rgb; - r = r.concat(at.linspace(fromrgba[0], torgba[0], nsteps ) ); - g = g.concat(at.linspace(fromrgba[1], torgba[1], nsteps ) ); - b = b.concat(at.linspace(fromrgba[2], torgba[2], nsteps ) ); - a = a.concat(at.linspace(fromrgba[3], torgba[3], nsteps ) ); - } +function isFunctionDefinition(value) { + return typeof value === 'object' && (value.stops || value.type === 'identity'); +} - r = r.map( Math.round ); - g = g.map( Math.round ); - b = b.map( Math.round ); - colors = at.zip(r, g, b, a); +module.exports.isFunctionDefinition = isFunctionDefinition; - if (format === 'hex') colors = colors.map( rgb2hex ); - if (format === 'rgbaString') colors = colors.map( rgbaStr ); - - return colors; +module.exports.interpolated = function(parameters) { + return createFunction(parameters, 'exponential'); }; - -function rgb2hex (rgba) { - var dig, hex = '#'; - for (var i = 0; i < 3; ++i) { - dig = rgba[i]; - dig = dig.toString(16); - hex += ('00' + dig).substr( dig.length ); - } - return hex; -} - -function rgbaStr (rgba) { - return 'rgba(' + rgba.join(',') + ')'; -} - -},{"./colorScales":257,"arraytools":161,"clone":259}],259:[function(require,module,exports){ -(function (Buffer){ -var clone = (function() { -'use strict'; - -/** - * Clones (copies) an Object using deep copying. - * - * This function supports circular references by default, but if you are certain - * there are no circular references in your object, you can save some CPU time - * by calling clone(obj, false). - * - * Caution: if `circular` is false and `parent` contains circular references, - * your program may enter an infinite loop and crash. - * - * @param `parent` - the object to be cloned - * @param `circular` - set to true if the object to be cloned may contain - * circular references. (optional - true by default) - * @param `depth` - set to a number if the object is only to be cloned to - * a particular depth. (optional - defaults to Infinity) - * @param `prototype` - sets the prototype to be used when cloning an object. - * (optional - defaults to parent prototype). -*/ -function clone(parent, circular, depth, prototype) { - var filter; - if (typeof circular === 'object') { - depth = circular.depth; - prototype = circular.prototype; - filter = circular.filter; - circular = circular.circular - } - // maintain two arrays for circular references, where corresponding parents - // and children have the same index - var allParents = []; - var allChildren = []; - - var useBuffer = typeof Buffer != 'undefined'; - - if (typeof circular == 'undefined') - circular = true; - - if (typeof depth == 'undefined') - depth = Infinity; - - // recurse this function so we don't reset allParents and allChildren - function _clone(parent, depth) { - // cloning null always returns null - if (parent === null) - return null; - - if (depth == 0) - return parent; - - var child; - var proto; - if (typeof parent != 'object') { - return parent; - } - - if (clone.__isArray(parent)) { - child = []; - } else if (clone.__isRegExp(parent)) { - child = new RegExp(parent.source, __getRegExpFlags(parent)); - if (parent.lastIndex) child.lastIndex = parent.lastIndex; - } else if (clone.__isDate(parent)) { - child = new Date(parent.getTime()); - } else if (useBuffer && Buffer.isBuffer(parent)) { - child = new Buffer(parent.length); - parent.copy(child); - return child; - } else { - if (typeof prototype == 'undefined') { - proto = Object.getPrototypeOf(parent); - child = Object.create(proto); - } - else { - child = Object.create(prototype); - proto = prototype; - } - } - - if (circular) { - var index = allParents.indexOf(parent); - - if (index != -1) { - return allChildren[index]; - } - allParents.push(parent); - allChildren.push(child); - } - - for (var i in parent) { - var attrs; - if (proto) { - attrs = Object.getOwnPropertyDescriptor(proto, i); - } - - if (attrs && attrs.set == null) { - continue; - } - child[i] = _clone(parent[i], depth - 1); - } - - return child; - } - - return _clone(parent, depth); -} - -/** - * Simple flat clone using prototype, accepts only objects, usefull for property - * override on FLAT configuration object (no nested props). - * - * USE WITH CAUTION! This may not behave as you wish if you do not know how this - * works. - */ -clone.clonePrototype = function clonePrototype(parent) { - if (parent === null) - return null; - - var c = function () {}; - c.prototype = parent; - return new c(); +module.exports['piecewise-constant'] = function(parameters) { + return createFunction(parameters, 'interval'); }; -// private utility functions +},{}],279:[function(require,module,exports){ -function __objToStr(o) { - return Object.prototype.toString.call(o); -}; -clone.__objToStr = __objToStr; - -function __isDate(o) { - return typeof o === 'object' && __objToStr(o) === '[object Date]'; -}; -clone.__isDate = __isDate; - -function __isArray(o) { - return typeof o === 'object' && __objToStr(o) === '[object Array]'; -}; -clone.__isArray = __isArray; - -function __isRegExp(o) { - return typeof o === 'object' && __objToStr(o) === '[object RegExp]'; -}; -clone.__isRegExp = __isRegExp; - -function __getRegExpFlags(re) { - var flags = ''; - if (re.global) flags += 'g'; - if (re.ignoreCase) flags += 'i'; - if (re.multiline) flags += 'm'; - return flags; -}; -clone.__getRegExpFlags = __getRegExpFlags; - -return clone; -})(); - -if (typeof module === 'object' && module.exports) { - module.exports = clone; -} - -}).call(this,require("buffer").Buffer) -},{"buffer":33}],260:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":275}],261:[function(require,module,exports){ -arguments[4][209][0].apply(exports,arguments) -},{"dup":209,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":275}],262:[function(require,module,exports){ -arguments[4][193][0].apply(exports,arguments) -},{"dup":193}],263:[function(require,module,exports){ -arguments[4][194][0].apply(exports,arguments) -},{"./do-bind.js":262,"dup":194}],264:[function(require,module,exports){ -arguments[4][195][0].apply(exports,arguments) -},{"./do-bind.js":262,"dup":195}],265:[function(require,module,exports){ -arguments[4][196][0].apply(exports,arguments) -},{"./lib/vao-emulated.js":263,"./lib/vao-native.js":264,"dup":196}],266:[function(require,module,exports){ -var DEFAULT_NORMALS_EPSILON = 1e-6; -var DEFAULT_FACE_EPSILON = 1e-6; - -//Estimate the vertex normals of a mesh -exports.vertexNormals = function(faces, positions, specifiedEpsilon) { - - var N = positions.length; - var normals = new Array(N); - var epsilon = specifiedEpsilon === void(0) ? DEFAULT_NORMALS_EPSILON : specifiedEpsilon; - - //Initialize normal array - for(var i=0; i epsilon) { - var norm = normals[c]; - var w = 1.0 / Math.sqrt(m01 * m21); - for(var k=0; k<3; ++k) { - var u = (k+1)%3; - var v = (k+2)%3; - norm[k] += w * (d21[u] * d01[v] - d21[v] * d01[u]); - } - } - } - } - - //Scale all normals to unit length - for(var i=0; i epsilon) { - var w = 1.0 / Math.sqrt(m); - for(var k=0; k<3; ++k) { - norm[k] *= w; - } - } else { - for(var k=0; k<3; ++k) { - norm[k] = 0.0; - } - } - } - - //Return the resulting set of patches - return normals; -} - -//Compute face normals of a mesh -exports.faceNormals = function(faces, positions, specifiedEpsilon) { - - var N = faces.length; - var normals = new Array(N); - var epsilon = specifiedEpsilon === void(0) ? DEFAULT_FACE_EPSILON : specifiedEpsilon; - - for(var i=0; i epsilon) { - l = 1.0 / Math.sqrt(l); - } else { - l = 0.0; - } - for(var j=0; j<3; ++j) { - n[j] *= l; - } - normals[i] = n; - } - return normals; -} - - - -},{}],267:[function(require,module,exports){ -//Optimized version for triangle closest point -// Based on Eberly's WildMagick codes -// http://www.geometrictools.com/LibMathematics/Distance/Distance.html -"use strict"; - -var diff = new Float64Array(4); -var edge0 = new Float64Array(4); -var edge1 = new Float64Array(4); - -function closestPoint2d(V0, V1, V2, point, result) { - //Reallocate buffers if necessary - if(diff.length < point.length) { - diff = new Float64Array(point.length); - edge0 = new Float64Array(point.length); - edge1 = new Float64Array(point.length); - } - //Compute edges - for(var i=0; i= a00) { - s = 1.0; - sqrDistance = a00 + 2.0*b0 + c; - } else { - s = -b0/a00; - sqrDistance = b0*s + c; - } - } else { - s = 0; - if (b1 >= 0) { - t = 0; - sqrDistance = c; - } else if (-b1 >= a11) { - t = 1; - sqrDistance = a11 + 2.0*b1 + c; - } else { - t = -b1/a11; - sqrDistance = b1*t + c; - } - } - } else { // region 3 - s = 0; - if (b1 >= 0) { - t = 0; - sqrDistance = c; - } else if (-b1 >= a11) { - t = 1; - sqrDistance = a11 + 2.0*b1 + c; - } else { - t = -b1/a11; - sqrDistance = b1*t + c; - } - } - } else if (t < 0) { // region 5 - t = 0; - if (b0 >= 0) { - s = 0; - sqrDistance = c; - } else if (-b0 >= a00) { - s = 1; - sqrDistance = a00 + 2.0*b0 + c; - } else { - s = -b0/a00; - sqrDistance = b0*s + c; - } - } else { // region 0 - // minimum at interior point - var invDet = 1.0 / det; - s *= invDet; - t *= invDet; - sqrDistance = s*(a00*s + a01*t + 2.0*b0) + t*(a01*s + a11*t + 2.0*b1) + c; - } - } else { - var tmp0, tmp1, numer, denom; - - if (s < 0) { // region 2 - tmp0 = a01 + b0; - tmp1 = a11 + b1; - if (tmp1 > tmp0) { - numer = tmp1 - tmp0; - denom = a00 - 2.0*a01 + a11; - if (numer >= denom) { - s = 1; - t = 0; - sqrDistance = a00 + 2.0*b0 + c; - } else { - s = numer/denom; - t = 1 - s; - sqrDistance = s*(a00*s + a01*t + 2.0*b0) + - t*(a01*s + a11*t + 2.0*b1) + c; - } - } else { - s = 0; - if (tmp1 <= 0) { - t = 1; - sqrDistance = a11 + 2.0*b1 + c; - } else if (b1 >= 0) { - t = 0; - sqrDistance = c; - } else { - t = -b1/a11; - sqrDistance = b1*t + c; - } - } - } else if (t < 0) { // region 6 - tmp0 = a01 + b1; - tmp1 = a00 + b0; - if (tmp1 > tmp0) { - numer = tmp1 - tmp0; - denom = a00 - 2.0*a01 + a11; - if (numer >= denom) { - t = 1; - s = 0; - sqrDistance = a11 + 2.0*b1 + c; - } else { - t = numer/denom; - s = 1 - t; - sqrDistance = s*(a00*s + a01*t + 2.0*b0) + - t*(a01*s + a11*t + 2.0*b1) + c; - } - } else { - t = 0; - if (tmp1 <= 0) { - s = 1; - sqrDistance = a00 + 2.0*b0 + c; - } else if (b0 >= 0) { - s = 0; - sqrDistance = c; - } else { - s = -b0/a00; - sqrDistance = b0*s + c; - } - } - } else { // region 1 - numer = a11 + b1 - a01 - b0; - if (numer <= 0) { - s = 0; - t = 1; - sqrDistance = a11 + 2.0*b1 + c; - } else { - denom = a00 - 2.0*a01 + a11; - if (numer >= denom) { - s = 1; - t = 0; - sqrDistance = a00 + 2.0*b0 + c; - } else { - s = numer/denom; - t = 1 - s; - sqrDistance = s*(a00*s + a01*t + 2.0*b0) + - t*(a01*s + a11*t + 2.0*b1) + c; - } - } - } - } - var u = 1.0 - s - t; - for(var i=0; i>1,v=E[2*m+1];', - 'if(v===b){return m}', - 'if(b 0) { - code.push(',') - } - code.push('[') - for(var j=0; j 0) { - code.push(',') - } - code.push('B(C,E,c[', f[0], '],c[', f[1], '])') - } - code.push(']') - } - code.push(');') - } - - for(var i=d+1; i>1; --i) { - if(i < d+1) { - code.push('else ') - } - code.push('if(l===', i, '){') - - //Generate mask - var maskStr = [] - for(var j=0; j 1) { - var scratch_shape = [] - for(var i=1; i 1) { - - //Copy data into scratch - code.push("dptr=0;sptr=ptr") - for(var i=order.length-1; i>=0; --i) { - var j = order[i] - if(j === 0) { - continue - } - code.push(["for(i",j,"=0;i",j,"left){", - "dptr=0", - "sptr=cptr-s0") - for(var i=1; ib){break __l}"].join("")) - for(var i=order.length-1; i>=1; --i) { - code.push( - "sptr+=e"+i, - "dptr+=f"+i, - "}") - } - - //Copy data back - code.push("dptr=cptr;sptr=cptr-s0") - for(var i=order.length-1; i>=0; --i) { - var j = order[i] - if(j === 0) { - continue - } - code.push(["for(i",j,"=0;i",j,"=0; --i) { - var j = order[i] - if(j === 0) { - continue - } - code.push(["for(i",j,"=0;i",j,"left)&&("+dataRead("cptr-s0")+">scratch)){", - dataWrite("cptr", dataRead("cptr-s0")), - "cptr-=s0", - "}", - dataWrite("cptr", "scratch")) - } - - //Close outer loop body - code.push("}") - if(order.length > 1 && allocator) { - code.push("free(scratch)") - } - code.push("} return " + funcName) - - //Compile and link function - if(allocator) { - var result = new Function("malloc", "free", code.join("\n")) - return result(allocator[0], allocator[1]) - } else { - var result = new Function(code.join("\n")) - return result() - } -} - -function createQuickSort(order, dtype, insertionSort) { - var code = [ "'use strict'" ] - var funcName = ["ndarrayQuickSort", order.join("d"), dtype].join("") - var funcArgs = ["left", "right", "data", "offset" ].concat(shapeArgs(order.length)) - var allocator = getMallocFree(dtype) - var labelCounter=0 - - code.push(["function ", funcName, "(", funcArgs.join(","), "){"].join("")) - - var vars = [ - "sixth=((right-left+1)/6)|0", - "index1=left+sixth", - "index5=right-sixth", - "index3=(left+right)>>1", - "index2=index3-sixth", - "index4=index3+sixth", - "el1=index1", - "el2=index2", - "el3=index3", - "el4=index4", - "el5=index5", - "less=left+1", - "great=right-1", - "pivots_are_equal=true", - "tmp", - "tmp0", - "x", - "y", - "z", - "k", - "ptr0", - "ptr1", - "ptr2", - "comp_pivot1=0", - "comp_pivot2=0", - "comp=0" - ] - - if(order.length > 1) { - var ele_size = [] - for(var i=1; i=0; --i) { - var j = order[i] - if(j === 0) { - continue - } - code.push(["for(i",j,"=0;i",j," 1) { - for(var i=0; i1) { - code.push("ptr_shift+=d"+j) - } else { - code.push("ptr0+=d"+j) - } - code.push("}") - } - } - - function lexicoLoop(label, ptrs, usePivot, body) { - if(ptrs.length === 1) { - code.push("ptr0="+toPointer(ptrs[0])) - } else { - for(var i=0; i 1) { - for(var i=0; i=1; --i) { - if(usePivot) { - code.push("pivot_ptr+=f"+i) - } - if(ptrs.length > 1) { - code.push("ptr_shift+=e"+i) - } else { - code.push("ptr0+=e"+i) - } - code.push("}") - } - } - - function cleanUp() { - if(order.length > 1 && allocator) { - code.push("free(pivot1)", "free(pivot2)") - } - } - - function compareSwap(a_id, b_id) { - var a = "el"+a_id - var b = "el"+b_id - if(order.length > 1) { - var lbl = "__l" + (++labelCounter) - lexicoLoop(lbl, [a, b], false, [ - "comp=",dataRead("ptr0"),"-",dataRead("ptr1"),"\n", - "if(comp>0){tmp0=", a, ";",a,"=",b,";", b,"=tmp0;break ", lbl,"}\n", - "if(comp<0){break ", lbl, "}" - ].join("")) - } else { - code.push(["if(", dataRead(toPointer(a)), ">", dataRead(toPointer(b)), "){tmp0=", a, ";",a,"=",b,";", b,"=tmp0}"].join("")) - } - } - - compareSwap(1, 2) - compareSwap(4, 5) - compareSwap(1, 3) - compareSwap(2, 3) - compareSwap(1, 4) - compareSwap(3, 4) - compareSwap(2, 5) - compareSwap(2, 3) - compareSwap(4, 5) - - if(order.length > 1) { - cacheLoop(["el1", "el2", "el3", "el4", "el5", "index1", "index3", "index5"], true, [ - "pivot1[pivot_ptr]=",dataRead("ptr1"),"\n", - "pivot2[pivot_ptr]=",dataRead("ptr3"),"\n", - "pivots_are_equal=pivots_are_equal&&(pivot1[pivot_ptr]===pivot2[pivot_ptr])\n", - "x=",dataRead("ptr0"),"\n", - "y=",dataRead("ptr2"),"\n", - "z=",dataRead("ptr4"),"\n", - dataWrite("ptr5", "x"),"\n", - dataWrite("ptr6", "y"),"\n", - dataWrite("ptr7", "z") - ].join("")) - } else { - code.push([ - "pivot1=", dataRead(toPointer("el2")), "\n", - "pivot2=", dataRead(toPointer("el4")), "\n", - "pivots_are_equal=pivot1===pivot2\n", - "x=", dataRead(toPointer("el1")), "\n", - "y=", dataRead(toPointer("el3")), "\n", - "z=", dataRead(toPointer("el5")), "\n", - dataWrite(toPointer("index1"), "x"), "\n", - dataWrite(toPointer("index3"), "y"), "\n", - dataWrite(toPointer("index5"), "z") - ].join("")) - } - - - function moveElement(dst, src) { - if(order.length > 1) { - cacheLoop([dst, src], false, - dataWrite("ptr0", dataRead("ptr1")) - ) - } else { - code.push(dataWrite(toPointer(dst), dataRead(toPointer(src)))) - } - } - - moveElement("index2", "left") - moveElement("index4", "right") - - function comparePivot(result, ptr, n) { - if(order.length > 1) { - var lbl = "__l" + (++labelCounter) - lexicoLoop(lbl, [ptr], true, [ - result,"=",dataRead("ptr0"),"-pivot",n,"[pivot_ptr]\n", - "if(",result,"!==0){break ", lbl, "}" - ].join("")) - } else { - code.push([result,"=", dataRead(toPointer(ptr)), "-pivot", n].join("")) - } - } - - function swapElements(a, b) { - if(order.length > 1) { - cacheLoop([a,b],false,[ - "tmp=",dataRead("ptr0"),"\n", - dataWrite("ptr0", dataRead("ptr1")),"\n", - dataWrite("ptr1", "tmp") - ].join("")) - } else { - code.push([ - "ptr0=",toPointer(a),"\n", - "ptr1=",toPointer(b),"\n", - "tmp=",dataRead("ptr0"),"\n", - dataWrite("ptr0", dataRead("ptr1")),"\n", - dataWrite("ptr1", "tmp") - ].join("")) - } - } - - function tripleSwap(k, less, great) { - if(order.length > 1) { - cacheLoop([k,less,great], false, [ - "tmp=",dataRead("ptr0"),"\n", - dataWrite("ptr0", dataRead("ptr1")),"\n", - dataWrite("ptr1", dataRead("ptr2")),"\n", - dataWrite("ptr2", "tmp") - ].join("")) - code.push("++"+less, "--"+great) - } else { - code.push([ - "ptr0=",toPointer(k),"\n", - "ptr1=",toPointer(less),"\n", - "ptr2=",toPointer(great),"\n", - "++",less,"\n", - "--",great,"\n", - "tmp=", dataRead("ptr0"), "\n", - dataWrite("ptr0", dataRead("ptr1")), "\n", - dataWrite("ptr1", dataRead("ptr2")), "\n", - dataWrite("ptr2", "tmp") - ].join("")) - } - } - - function swapAndDecrement(k, great) { - swapElements(k, great) - code.push("--"+great) - } - - code.push("if(pivots_are_equal){") - //Pivots are equal case - code.push("for(k=less;k<=great;++k){") - comparePivot("comp", "k", 1) - code.push("if(comp===0){continue}") - code.push("if(comp<0){") - code.push("if(k!==less){") - swapElements("k", "less") - code.push("}") - code.push("++less") - code.push("}else{") - code.push("while(true){") - comparePivot("comp", "great", 1) - code.push("if(comp>0){") - code.push("great--") - code.push("}else if(comp<0){") - tripleSwap("k", "less", "great") - code.push("break") - code.push("}else{") - swapAndDecrement("k", "great") - code.push("break") - code.push("}") - code.push("}") - code.push("}") - code.push("}") - code.push("}else{") - //Pivots not equal case - code.push("for(k=less;k<=great;++k){") - comparePivot("comp_pivot1", "k", 1) - code.push("if(comp_pivot1<0){") - code.push("if(k!==less){") - swapElements("k", "less") - code.push("}") - code.push("++less") - code.push("}else{") - comparePivot("comp_pivot2", "k", 2) - code.push("if(comp_pivot2>0){") - code.push("while(true){") - comparePivot("comp", "great", 2) - code.push("if(comp>0){") - code.push("if(--great1) { - cacheLoop([mem_dest, pivot_dest], true, [ - dataWrite("ptr0", dataRead("ptr1")), "\n", - dataWrite("ptr1", ["pivot",pivot,"[pivot_ptr]"].join("")) - ].join("")) - } else { - code.push( - dataWrite(toPointer(mem_dest), dataRead(toPointer(pivot_dest))), - dataWrite(toPointer(pivot_dest), "pivot"+pivot)) - } - } - - storePivot("left", "(less-1)", 1) - storePivot("right", "(great+1)", 2) - - //Recursive sort call - function doSort(left, right) { - code.push([ - "if((",right,"-",left,")<=",INSERTION_SORT_THRESHOLD,"){\n", - "insertionSort(", left, ",", right, ",data,offset,", shapeArgs(order.length).join(","), ")\n", - "}else{\n", - funcName, "(", left, ",", right, ",data,offset,", shapeArgs(order.length).join(","), ")\n", - "}" - ].join("")) - } - doSort("left", "(less-2)") - doSort("(great+2)", "right") - - //If pivots are equal, then early out - code.push("if(pivots_are_equal){") - cleanUp() - code.push("return") - code.push("}") - - function walkPointer(ptr, pivot, body) { - if(order.length > 1) { - code.push(["__l",++labelCounter,":while(true){"].join("")) - cacheLoop([ptr], true, [ - "if(", dataRead("ptr0"), "!==pivot", pivot, "[pivot_ptr]){break __l", labelCounter, "}" - ].join("")) - code.push(body, "}") - } else { - code.push(["while(", dataRead(toPointer(ptr)), "===pivot", pivot, "){", body, "}"].join("")) - } - } - - //Check bounds - code.push("if(lessindex5){") - - walkPointer("less", 1, "++less") - walkPointer("great", 2, "--great") - - code.push("for(k=less;k<=great;++k){") - comparePivot("comp_pivot1", "k", 1) - code.push("if(comp_pivot1===0){") - code.push("if(k!==less){") - swapElements("k", "less") - code.push("}") - code.push("++less") - code.push("}else{") - comparePivot("comp_pivot2", "k", 2) - code.push("if(comp_pivot2===0){") - code.push("while(true){") - comparePivot("comp", "great", 2) - code.push("if(comp===0){") - code.push("if(--great 1 && allocator) { - var compiled = new Function("insertionSort", "malloc", "free", code.join("\n")) - return compiled(insertionSort, allocator[0], allocator[1]) - } - var compiled = new Function("insertionSort", code.join("\n")) - return compiled(insertionSort) -} - -function compileSort(order, dtype) { - var code = ["'use strict'"] - var funcName = ["ndarraySortWrapper", order.join("d"), dtype].join("") - var funcArgs = [ "array" ] - - code.push(["function ", funcName, "(", funcArgs.join(","), "){"].join("")) - - //Unpack local variables from array - var vars = ["data=array.data,offset=array.offset|0,shape=array.shape,stride=array.stride"] - for(var i=0; i 0) { - vars.push(["d",j,"=s",j,"-d",p,"*n",p].join("")) - } else { - vars.push(["d",j,"=s",j].join("")) - } - p = j - } - var k = order.length-1-i - if(k !== 0) { - if(q > 0) { - vars.push(["e",k,"=s",k,"-e",q,"*n",q, - ",f",k,"=",scratch_stride[k],"-f",q,"*n",q].join("")) - } else { - vars.push(["e",k,"=s",k,",f",k,"=",scratch_stride[k]].join("")) - } - q = k - } - } - - //Declare local variables - code.push("var " + vars.join(",")) - - //Create arguments for subroutine - var sortArgs = ["0", "n0-1", "data", "offset"].concat(shapeArgs(order.length)) - - //Call main sorting routine - code.push([ - "if(n0<=",INSERTION_SORT_THRESHOLD,"){", - "insertionSort(", sortArgs.join(","), ")}else{", - "quickSort(", sortArgs.join(","), - ")}" - ].join("")) - - //Return - code.push("}return " + funcName) - - //Link everything together - var result = new Function("insertionSort", "quickSort", code.join("\n")) - var insertionSort = createInsertionSort(order, dtype) - var quickSort = createQuickSort(order, dtype, insertionSort) - return result(insertionSort, quickSort) -} - -module.exports = compileSort -},{"typedarray-pool":275}],272:[function(require,module,exports){ -"use strict" - -var compile = require("./lib/compile_sort.js") -var CACHE = {} - -function sort(array) { - var order = array.order - var dtype = array.dtype - var typeSig = [order, dtype ] - var typeName = typeSig.join(":") - var compiled = CACHE[typeName] - if(!compiled) { - CACHE[typeName] = compiled = compile(order, dtype) - } - compiled(array) - return array -} - -module.exports = sort -},{"./lib/compile_sort.js":271}],273:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],274:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],275:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":273,"buffer":33,"dup":187}],276:[function(require,module,exports){ -'use strict' - -module.exports = createBoxes - -var createBuffer = require('gl-buffer') -var createShader = require('gl-shader') - -var shaders = require('./shaders') - -function Boxes(plot, vbo, shader) { - this.plot = plot - this.vbo = vbo - this.shader = shader -} - -var proto = Boxes.prototype - -proto.bind = function() { - var shader = this.shader - this.vbo.bind() - this.shader.bind() - shader.attributes.coord.pointer() - shader.uniforms.screenBox = this.plot.screenBox -} - -proto.drawBox = (function() { - var lo = [0,0] - var hi = [0,0] - return function(loX, loY, hiX, hiY, color) { - var plot = this.plot - var shader = this.shader - var gl = plot.gl - - lo[0] = loX - lo[1] = loY - hi[0] = hiX - hi[1] = hiY - - shader.uniforms.lo = lo - shader.uniforms.hi = hi - shader.uniforms.color = color - - gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4) - } -}()) - -proto.dispose = function() { - this.vbo.dispose() - this.shader.dispose() -} - -function createBoxes(plot) { - var gl = plot.gl - var vbo = createBuffer(gl, [ - 0,0, - 0,1, - 1,0, - 1,1]) - var shader = createShader(gl, shaders.boxVert, shaders.lineFrag) - return new Boxes(plot, vbo, shader) -} - -},{"./shaders":279,"gl-buffer":282,"gl-shader":297}],277:[function(require,module,exports){ -'use strict' - -module.exports = createGrid - -var createBuffer = require('gl-buffer') -var createShader = require('gl-shader') -var bsearch = require('binary-search-bounds') -var shaders = require('./shaders') - -function Grid(plot, vbo, shader, tickShader) { - this.plot = plot - this.vbo = vbo - this.shader = shader - this.tickShader = tickShader - this.ticks = [[], []] -} - -function compareTickNum(a, b) { - return a - b -} - -var proto = Grid.prototype - -proto.draw = (function() { - - var DATA_SHIFT = [0,0] - var DATA_SCALE = [0,0] - var DATA_AXIS = [0,0] - - return function() { - var plot = this.plot - var vbo = this.vbo - var shader = this.shader - var ticks = this.ticks - var gl = plot.gl - var bounds = plot._tickBounds - var dataBox = plot.dataBox - var viewPixels = plot.viewBox - var lineWidth = plot.gridLineWidth - var gridColor = plot.gridLineColor - var gridEnable = plot.gridLineEnable - var pixelRatio = plot.pixelRatio - - for(var i=0; i<2; ++i) { - var lo = bounds[i] - var hi = bounds[i+2] - var boundScale = hi - lo - var dataCenter = 0.5 * (dataBox[i+2] + dataBox[i]) - var dataWidth = dataBox[i+2] - dataBox[i] - DATA_SCALE[i] = 2.0 * boundScale / dataWidth - DATA_SHIFT[i] = 2.0 * (lo - dataCenter) / dataWidth - } - - shader.bind() - vbo.bind() - shader.attributes.dataCoord.pointer() - shader.uniforms.dataShift = DATA_SHIFT - shader.uniforms.dataScale = DATA_SCALE - - var offset = 0 - for(var i=0; i<2; ++i) { - DATA_AXIS[0] = DATA_AXIS[1] = 0 - DATA_AXIS[i] = 1 - shader.uniforms.dataAxis = DATA_AXIS - shader.uniforms.lineWidth = lineWidth[i] / (viewPixels[i+2] - viewPixels[i]) * pixelRatio - shader.uniforms.color = gridColor[i] - - var size = ticks[i].length * 6 - if(gridEnable[i] && size) { - gl.drawArrays(gl.TRIANGLES, offset, size) - } - offset += size - } - } -})() - -proto.drawTickMarks = (function() { - var DATA_SHIFT = [0,0] - var DATA_SCALE = [0,0] - var X_AXIS = [1,0] - var Y_AXIS = [0,1] - var SCR_OFFSET = [0,0] - var TICK_SCALE = [0,0] - - return function() { - var plot = this.plot - var vbo = this.vbo - var shader = this.tickShader - var ticks = this.ticks - var gl = plot.gl - var bounds = plot._tickBounds - var dataBox = plot.dataBox - var viewBox = plot.viewBox - var pixelRatio = plot.pixelRatio - var screenBox = plot.screenBox - - var screenWidth = screenBox[2] - screenBox[0] - var screenHeight = screenBox[3] - screenBox[1] - var viewWidth = viewBox[2] - viewBox[0] - var viewHeight = viewBox[3] - viewBox[1] - - for(var i=0; i<2; ++i) { - var lo = bounds[i] - var hi = bounds[i+2] - var boundScale = hi - lo - var dataCenter = 0.5 * (dataBox[i+2] + dataBox[i]) - var dataWidth = (dataBox[i+2] - dataBox[i]) - DATA_SCALE[i] = 2.0 * boundScale / dataWidth - DATA_SHIFT[i] = 2.0 * (lo - dataCenter) / dataWidth - } - - DATA_SCALE[0] *= viewWidth / screenWidth - DATA_SHIFT[0] *= viewWidth / screenWidth - - DATA_SCALE[1] *= viewHeight / screenHeight - DATA_SHIFT[1] *= viewHeight / screenHeight - - shader.bind() - vbo.bind() - - shader.attributes.dataCoord.pointer() - - var uniforms = shader.uniforms - uniforms.dataShift = DATA_SHIFT - uniforms.dataScale = DATA_SCALE - - var tickMarkLength = plot.tickMarkLength - var tickMarkWidth = plot.tickMarkWidth - var tickMarkColor = plot.tickMarkColor - - var xTicksOffset = 0 - var yTicksOffset = ticks[0].length * 6 - - var xStart = Math.min(bsearch.ge(ticks[0], (dataBox[0] - bounds[0]) / (bounds[2] - bounds[0]), compareTickNum), ticks[0].length) - var xEnd = Math.min(bsearch.gt(ticks[0], (dataBox[2] - bounds[0]) / (bounds[2] - bounds[0]), compareTickNum), ticks[0].length) - var xOffset = xTicksOffset + 6 * xStart - var xCount = 6 * Math.max(0, xEnd - xStart) - - var yStart = Math.min(bsearch.ge(ticks[1], (dataBox[1] - bounds[1]) / (bounds[3] - bounds[1]), compareTickNum), ticks[1].length) - var yEnd = Math.min(bsearch.gt(ticks[1], (dataBox[3] - bounds[1]) / (bounds[3] - bounds[1]), compareTickNum), ticks[1].length) - var yOffset = yTicksOffset + 6 * yStart - var yCount = 6 * Math.max(0, yEnd - yStart) - - SCR_OFFSET[0] = 2.0 * (viewBox[0] - tickMarkLength[1]) / screenWidth - 1.0 - SCR_OFFSET[1] = (viewBox[3] + viewBox[1]) / screenHeight - 1.0 - TICK_SCALE[0] = tickMarkLength[1] * pixelRatio / screenWidth - TICK_SCALE[1] = tickMarkWidth[1] * pixelRatio / screenHeight - - if(yCount) { - uniforms.color = tickMarkColor[1] - uniforms.tickScale = TICK_SCALE - uniforms.dataAxis = Y_AXIS - uniforms.screenOffset = SCR_OFFSET - gl.drawArrays(gl.TRIANGLES, yOffset, yCount) - } - - SCR_OFFSET[0] = (viewBox[2] + viewBox[0]) / screenWidth - 1.0 - SCR_OFFSET[1] = 2.0 * (viewBox[1] - tickMarkLength[0]) / screenHeight - 1.0 - TICK_SCALE[0] = tickMarkWidth[0] * pixelRatio / screenWidth - TICK_SCALE[1] = tickMarkLength[0] * pixelRatio / screenHeight - - if(xCount) { - uniforms.color = tickMarkColor[0] - uniforms.tickScale = TICK_SCALE - uniforms.dataAxis = X_AXIS - uniforms.screenOffset = SCR_OFFSET - gl.drawArrays(gl.TRIANGLES, xOffset, xCount) - } - - SCR_OFFSET[0] = 2.0 * (viewBox[2] + tickMarkLength[3]) / screenWidth - 1.0 - SCR_OFFSET[1] = (viewBox[3] + viewBox[1]) / screenHeight - 1.0 - TICK_SCALE[0] = tickMarkLength[3] * pixelRatio / screenWidth - TICK_SCALE[1] = tickMarkWidth[3] * pixelRatio / screenHeight - - if(yCount) { - uniforms.color = tickMarkColor[3] - uniforms.tickScale = TICK_SCALE - uniforms.dataAxis = Y_AXIS - uniforms.screenOffset = SCR_OFFSET - gl.drawArrays(gl.TRIANGLES, yOffset, yCount) - } - - SCR_OFFSET[0] = (viewBox[2] + viewBox[0]) / screenWidth - 1.0 - SCR_OFFSET[1] = 2.0 * (viewBox[3] + tickMarkLength[2]) / screenHeight - 1.0 - TICK_SCALE[0] = tickMarkWidth[2] * pixelRatio / screenWidth - TICK_SCALE[1] = tickMarkLength[2] * pixelRatio / screenHeight - - if(xCount) { - uniforms.color = tickMarkColor[2] - uniforms.tickScale = TICK_SCALE - uniforms.dataAxis = X_AXIS - uniforms.screenOffset = SCR_OFFSET - gl.drawArrays(gl.TRIANGLES, xOffset, xCount) - } - } -})() - -proto.update = (function() { - var OFFSET_X = [1, 1, -1, -1, 1, -1] - var OFFSET_Y = [1, -1, 1, 1, -1, -1] - - return function(options) { - var ticks = options.ticks - var bounds = options.bounds - var data = new Float32Array(6 * 3 * (ticks[0].length + ticks[1].length)) - - var zeroLineEnable = this.plot.zeroLineEnable - - var ptr = 0 - var gridTicks = [[], []] - for(var dim=0; dim<2; ++dim) { - var localTicks = gridTicks[dim] - var axisTicks = ticks[dim] - var lo = bounds[dim] - var hi = bounds[dim+2] - for(var i=0; i tickOffset[start]) { - shader.uniforms.dataAxis = DATA_AXIS - shader.uniforms.screenOffset = SCREEN_OFFSET - shader.uniforms.color = textColor[axis] - shader.uniforms.angle = textAngle[axis] - gl.drawArrays( - gl.TRIANGLES, - tickOffset[start], - tickOffset[end] - tickOffset[start]) - } - } - if(labelEnable[axis] && labelCount) { - SCREEN_OFFSET[axis^1] -= screenScale * pixelRatio * labelPad[axis] - shader.uniforms.dataAxis = ZERO_2 - shader.uniforms.screenOffset = SCREEN_OFFSET - shader.uniforms.color = labelColor[axis] - shader.uniforms.angle = labelAngle[axis] - gl.drawArrays( - gl.TRIANGLES, - labelOffset, - labelCount) - } - - SCREEN_OFFSET[axis^1] = screenScale * viewBox[2+(axis^1)] - 1.0 - if(tickEnable[axis+2]) { - SCREEN_OFFSET[axis^1] += screenScale * pixelRatio * tickPad[axis+2] - if(start < end && tickOffset[end] > tickOffset[start]) { - shader.uniforms.dataAxis = DATA_AXIS - shader.uniforms.screenOffset = SCREEN_OFFSET - shader.uniforms.color = textColor[axis+2] - shader.uniforms.angle = textAngle[axis+2] - gl.drawArrays( - gl.TRIANGLES, - tickOffset[start], - tickOffset[end] - tickOffset[start]) - } - } - if(labelEnable[axis+2] && labelCount) { - SCREEN_OFFSET[axis^1] += screenScale * pixelRatio * labelPad[axis+2] - shader.uniforms.dataAxis = ZERO_2 - shader.uniforms.screenOffset = SCREEN_OFFSET - shader.uniforms.color = labelColor[axis+2] - shader.uniforms.angle = labelAngle[axis+2] - gl.drawArrays( - gl.TRIANGLES, - labelOffset, - labelCount) - } - - } -})() - -proto.drawTitle = (function() { - var DATA_AXIS = [0,0] - var SCREEN_OFFSET = [0,0] - - return function() { - var plot = this.plot - var shader = this.shader - var gl = plot.gl - var screenBox = plot.screenBox - var titleCenter = plot.titleCenter - var titleAngle = plot.titleAngle - var titleColor = plot.titleColor - var pixelRatio = plot.pixelRatio - - if(!this.titleCount) { - return - } - - for(var i=0; i<2; ++i) { - SCREEN_OFFSET[i] = 2.0 * (titleCenter[i]*pixelRatio - screenBox[i]) / - (screenBox[2+i] - screenBox[i]) - 1 - } - - shader.bind() - shader.uniforms.dataAxis = DATA_AXIS - shader.uniforms.screenOffset = SCREEN_OFFSET - shader.uniforms.angle = titleAngle - shader.uniforms.color = titleColor - - gl.drawArrays(gl.TRIANGLES, this.titleOffset, this.titleCount) - } -})() - -proto.bind = (function() { - var DATA_SHIFT = [0,0] - var DATA_SCALE = [0,0] - var TEXT_SCALE = [0,0] - - return function() { - var plot = this.plot - var shader = this.shader - var bounds = plot._tickBounds - var dataBox = plot.dataBox - var screenBox = plot.screenBox - var viewBox = plot.viewBox - - shader.bind() - - //Set up coordinate scaling uniforms - for(var i=0; i<2; ++i) { - - var lo = bounds[i] - var hi = bounds[i+2] - var boundScale = hi - lo - var dataCenter = 0.5 * (dataBox[i+2] + dataBox[i]) - var dataWidth = (dataBox[i+2] - dataBox[i]) - - var viewLo = viewBox[i] - var viewHi = viewBox[i+2] - var viewScale = viewHi - viewLo - var screenLo = screenBox[i] - var screenHi = screenBox[i+2] - var screenScale = screenHi - screenLo - - DATA_SCALE[i] = 2.0 * boundScale / dataWidth * viewScale / screenScale - DATA_SHIFT[i] = 2.0 * (lo - dataCenter) / dataWidth * viewScale / screenScale - } - - TEXT_SCALE[1] = 2.0 * plot.pixelRatio / (screenBox[3] - screenBox[1]) - TEXT_SCALE[0] = TEXT_SCALE[1] * (screenBox[3] - screenBox[1]) / (screenBox[2] - screenBox[0]) - - shader.uniforms.dataScale = DATA_SCALE - shader.uniforms.dataShift = DATA_SHIFT - shader.uniforms.textScale = TEXT_SCALE - - //Set attributes - this.vbo.bind() - shader.attributes.textCoordinate.pointer() - } -})() - -proto.update = function(options) { - var vertices = [] - var axesTicks = options.ticks - var bounds = options.bounds - var i, j, k, data, scale, dimension - - for(dimension=0; dimension<2; ++dimension) { - var offsets = [Math.floor(vertices.length/3)], tickX = [-Infinity] - - //Copy vertices over to buffer - var ticks = axesTicks[dimension] - for(i=0; i 1) { - ext.drawBuffersWEBGL(colorAttachmentArrays[numColors]) - } - - //Allocate depth/stencil buffers - var WEBGL_depth_texture = gl.getExtension('WEBGL_depth_texture') - if(WEBGL_depth_texture) { - if(useStencil) { - fbo.depth = initTexture(gl, width, height, - WEBGL_depth_texture.UNSIGNED_INT_24_8_WEBGL, - gl.DEPTH_STENCIL, - gl.DEPTH_STENCIL_ATTACHMENT) - } else if(useDepth) { - fbo.depth = initTexture(gl, width, height, - gl.UNSIGNED_SHORT, - gl.DEPTH_COMPONENT, - gl.DEPTH_ATTACHMENT) - } - } else { - if(useDepth && useStencil) { - fbo._depth_rb = initRenderBuffer(gl, width, height, gl.DEPTH_STENCIL, gl.DEPTH_STENCIL_ATTACHMENT) - } else if(useDepth) { - fbo._depth_rb = initRenderBuffer(gl, width, height, gl.DEPTH_COMPONENT16, gl.DEPTH_ATTACHMENT) - } else if(useStencil) { - fbo._depth_rb = initRenderBuffer(gl, width, height, gl.STENCIL_INDEX, gl.STENCIL_ATTACHMENT) - } - } - - //Check frame buffer state - var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER) - if(status !== gl.FRAMEBUFFER_COMPLETE) { - - //Release all partially allocated resources - fbo._destroyed = true - - //Release all resources - gl.bindFramebuffer(gl.FRAMEBUFFER, null) - gl.deleteFramebuffer(fbo.handle) - fbo.handle = null - if(fbo.depth) { - fbo.depth.dispose() - fbo.depth = null - } - if(fbo._depth_rb) { - gl.deleteRenderbuffer(fbo._depth_rb) - fbo._depth_rb = null - } - for(var i=0; i maxFBOSize || - h < 0 || h > maxFBOSize) { - throw new Error('gl-fbo: Can\'t resize FBO, invalid dimensions') - } - - //Update shape - fbo._shape[0] = w - fbo._shape[1] = h - - //Save framebuffer state - var state = saveFBOState(gl) - - //Resize framebuffer attachments - for(var i=0; i maxFBOSize || height < 0 || height > maxFBOSize) { - throw new Error('gl-fbo: Parameters are too large for FBO') - } - - //Handle each option type - options = options || {} - - //Figure out number of color buffers to use - var numColors = 1 - if('color' in options) { - numColors = Math.max(options.color|0, 0) - if(numColors < 0) { - throw new Error('gl-fbo: Must specify a nonnegative number of colors') - } - if(numColors > 1) { - //Check if multiple render targets supported - if(!WEBGL_draw_buffers) { - throw new Error('gl-fbo: Multiple draw buffer extension not supported') - } else if(numColors > gl.getParameter(WEBGL_draw_buffers.MAX_COLOR_ATTACHMENTS_WEBGL)) { - throw new Error('gl-fbo: Context does not support ' + numColors + ' draw buffers') - } - } - } - - //Determine whether to use floating point textures - var colorType = gl.UNSIGNED_BYTE - var OES_texture_float = gl.getExtension('OES_texture_float') - if(options.float && numColors > 0) { - if(!OES_texture_float) { - throw new Error('gl-fbo: Context does not support floating point textures') - } - colorType = gl.FLOAT - } else if(options.preferFloat && numColors > 0) { - if(OES_texture_float) { - colorType = gl.FLOAT - } - } - - //Check if we should use depth buffer - var useDepth = true - if('depth' in options) { - useDepth = !!options.depth - } - - //Check if we should use a stencil buffer - var useStencil = false - if('stencil' in options) { - useStencil = !!options.stencil - } - - return new Framebuffer( - gl, - width, - height, - colorType, - numColors, - useDepth, - useStencil, - WEBGL_draw_buffers) -} - -},{"gl-texture2d":293}],293:[function(require,module,exports){ -arguments[4][209][0].apply(exports,arguments) -},{"dup":209,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":295}],294:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],295:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":286,"buffer":33,"dup":187}],296:[function(require,module,exports){ -'use strict' - -module.exports = createSelectBuffer - -var createFBO = require('gl-fbo') -var pool = require('typedarray-pool') -var ndarray = require('ndarray') - -var nextPow2 = require('bit-twiddle').nextPow2 - -var selectRange = require('cwise/lib/wrapper')({"args":["array",{"offset":[0,0,1],"array":0},{"offset":[0,0,2],"array":0},{"offset":[0,0,3],"array":0},"scalar","scalar","index"],"pre":{"body":"{this_closestD2=1e8,this_closestX=-1,this_closestY=-1}","args":[],"thisVars":["this_closestD2","this_closestX","this_closestY"],"localVars":[]},"body":{"body":"{if(_inline_1_arg0_<255||_inline_1_arg1_<255||_inline_1_arg2_<255||_inline_1_arg3_<255){var _inline_1_l=_inline_1_arg4_-_inline_1_arg6_[0],_inline_1_a=_inline_1_arg5_-_inline_1_arg6_[1],_inline_1_f=_inline_1_l*_inline_1_l+_inline_1_a*_inline_1_a;_inline_1_f this.buffer.length) { - pool.free(this.buffer) - var buffer = this.buffer = pool.mallocUint8(nextPow2(r*c*4)) - for(var i=0; i -1 and 1 => 1\n // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap\n // y is 1 if the normal points up, and -1 if it points down\n mediump vec2 normal = mod(a_pos, 2.0);\n normal.y = sign(normal.y - 0.5);\n v_normal = normal;\n\n float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0);\n float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing;\n\n // Scale the extrusion vector down to a normal and then up by the line width\n // of this vertex.\n mediump vec2 dist = outset * a_extrude * scale;\n\n // Calculate the offset when drawing a line that is to the side of the actual line.\n // We do this by creating a vector that points towards the extrude, but rotate\n // it when we're drawing round end points (a_direction = -1 or 1) since their\n // extrude vector points in another direction.\n mediump float u = 0.5 * a_direction;\n mediump float t = 1.0 - abs(u);\n mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);\n\n // Remove the texture normal bit of the position before scaling it with the\n // model/view matrix.\n gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);\n\n // position of y on the screen\n float y = gl_Position.y / gl_Position.w;\n\n // how much features are squished in the y direction by the tilt\n float squish_scale = length(a_extrude) / length(u_antialiasingmatrix * a_extrude);\n\n // how much features are squished in all directions by the perspectiveness\n float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9));\n\n v_linewidth = vec2(outset, inset);\n v_gamma_scale = perspective_scale * squish_scale;\n}\n" + }, + linepattern: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_blur;\n\nuniform vec2 u_pattern_size_a;\nuniform vec2 u_pattern_size_b;\nuniform vec2 u_pattern_tl_a;\nuniform vec2 u_pattern_br_a;\nuniform vec2 u_pattern_tl_b;\nuniform vec2 u_pattern_br_b;\nuniform float u_fade;\nuniform float u_opacity;\n\nuniform sampler2D u_image;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying float v_linesofar;\nvarying float v_gamma_scale;\n\nvoid main() {\n // Calculate the distance of the pixel from the line in pixels.\n float dist = length(v_normal) * v_linewidth.s;\n\n // Calculate the antialiasing fade factor. This is either when fading in\n // the line in case of an offset line (v_linewidth.t) or when fading out\n // (v_linewidth.s)\n float blur = u_blur * v_gamma_scale;\n float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);\n\n float x_a = mod(v_linesofar / u_pattern_size_a.x, 1.0);\n float x_b = mod(v_linesofar / u_pattern_size_b.x, 1.0);\n float y_a = 0.5 + (v_normal.y * v_linewidth.s / u_pattern_size_a.y);\n float y_b = 0.5 + (v_normal.y * v_linewidth.s / u_pattern_size_b.y);\n vec2 pos_a = mix(u_pattern_tl_a, u_pattern_br_a, vec2(x_a, y_a));\n vec2 pos_b = mix(u_pattern_tl_b, u_pattern_br_b, vec2(x_b, y_b));\n\n vec4 color = mix(texture2D(u_image, pos_a), texture2D(u_image, pos_b), u_fade);\n\n alpha *= u_opacity;\n\n gl_FragColor = color * alpha;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n// floor(127 / 2) == 63.0\n// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is\n// stored in a byte (-128..127). we scale regular normals up to length 63, but\n// there are also \"special\" normals that have a bigger length (of up to 126 in\n// this case).\n// #define scale 63.0\n#define scale 0.015873016\n\n// We scale the distance before adding it to the buffers so that we can store\n// long distances for long segments. Use this value to unscale the distance.\n#define LINE_DISTANCE_SCALE 2.0\n\nattribute vec2 a_pos;\nattribute vec4 a_data;\n\nuniform mat4 u_matrix;\nuniform mediump float u_ratio;\nuniform mediump float u_linewidth;\nuniform mediump float u_gapwidth;\nuniform mediump float u_antialiasing;\nuniform mediump float u_extra;\nuniform mat2 u_antialiasingmatrix;\nuniform mediump float u_offset;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying float v_linesofar;\nvarying float v_gamma_scale;\n\nvoid main() {\n vec2 a_extrude = a_data.xy - 128.0;\n float a_direction = mod(a_data.z, 4.0) - 1.0;\n float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE;\n\n // We store the texture normals in the most insignificant bit\n // transform y so that 0 => -1 and 1 => 1\n // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap\n // y is 1 if the normal points up, and -1 if it points down\n mediump vec2 normal = mod(a_pos, 2.0);\n normal.y = sign(normal.y - 0.5);\n v_normal = normal;\n\n float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0);\n float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing;\n\n // Scale the extrusion vector down to a normal and then up by the line width\n // of this vertex.\n mediump vec2 dist = outset * a_extrude * scale;\n\n // Calculate the offset when drawing a line that is to the side of the actual line.\n // We do this by creating a vector that points towards the extrude, but rotate\n // it when we're drawing round end points (a_direction = -1 or 1) since their\n // extrude vector points in another direction.\n mediump float u = 0.5 * a_direction;\n mediump float t = 1.0 - abs(u);\n mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);\n\n // Remove the texture normal bit of the position before scaling it with the\n // model/view matrix.\n gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);\n v_linesofar = a_linesofar;\n\n // position of y on the screen\n float y = gl_Position.y / gl_Position.w;\n\n // how much features are squished in the y direction by the tilt\n float squish_scale = length(a_extrude) / length(u_antialiasingmatrix * a_extrude);\n\n // how much features are squished in all directions by the perspectiveness\n float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9));\n\n v_linewidth = vec2(outset, inset);\n v_gamma_scale = perspective_scale * squish_scale;\n}\n" + }, + linesdfpattern: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform lowp vec4 u_color;\nuniform lowp float u_opacity;\n\nuniform float u_blur;\nuniform sampler2D u_image;\nuniform float u_sdfgamma;\nuniform float u_mix;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying vec2 v_tex_a;\nvarying vec2 v_tex_b;\nvarying float v_gamma_scale;\n\nvoid main() {\n // Calculate the distance of the pixel from the line in pixels.\n float dist = length(v_normal) * v_linewidth.s;\n\n // Calculate the antialiasing fade factor. This is either when fading in\n // the line in case of an offset line (v_linewidth.t) or when fading out\n // (v_linewidth.s)\n float blur = u_blur * v_gamma_scale;\n float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);\n\n float sdfdist_a = texture2D(u_image, v_tex_a).a;\n float sdfdist_b = texture2D(u_image, v_tex_b).a;\n float sdfdist = mix(sdfdist_a, sdfdist_b, u_mix);\n alpha *= smoothstep(0.5 - u_sdfgamma, 0.5 + u_sdfgamma, sdfdist);\n\n gl_FragColor = u_color * (alpha * u_opacity);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n// floor(127 / 2) == 63.0\n// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is\n// stored in a byte (-128..127). we scale regular normals up to length 63, but\n// there are also \"special\" normals that have a bigger length (of up to 126 in\n// this case).\n// #define scale 63.0\n#define scale 0.015873016\n\n// We scale the distance before adding it to the buffers so that we can store\n// long distances for long segments. Use this value to unscale the distance.\n#define LINE_DISTANCE_SCALE 2.0\n\nattribute vec2 a_pos;\nattribute vec4 a_data;\n\nuniform mat4 u_matrix;\nuniform mediump float u_ratio;\nuniform mediump float u_linewidth;\nuniform mediump float u_gapwidth;\nuniform mediump float u_antialiasing;\nuniform vec2 u_patternscale_a;\nuniform float u_tex_y_a;\nuniform vec2 u_patternscale_b;\nuniform float u_tex_y_b;\nuniform float u_extra;\nuniform mat2 u_antialiasingmatrix;\nuniform mediump float u_offset;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying vec2 v_tex_a;\nvarying vec2 v_tex_b;\nvarying float v_gamma_scale;\n\nvoid main() {\n vec2 a_extrude = a_data.xy - 128.0;\n float a_direction = mod(a_data.z, 4.0) - 1.0;\n float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE;\n\n // We store the texture normals in the most insignificant bit\n // transform y so that 0 => -1 and 1 => 1\n // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap\n // y is 1 if the normal points up, and -1 if it points down\n mediump vec2 normal = mod(a_pos, 2.0);\n normal.y = sign(normal.y - 0.5);\n v_normal = normal;\n\n float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0);\n float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing;\n\n // Scale the extrusion vector down to a normal and then up by the line width\n // of this vertex.\n mediump vec2 dist = outset * a_extrude * scale;\n\n // Calculate the offset when drawing a line that is to the side of the actual line.\n // We do this by creating a vector that points towards the extrude, but rotate\n // it when we're drawing round end points (a_direction = -1 or 1) since their\n // extrude vector points in another direction.\n mediump float u = 0.5 * a_direction;\n mediump float t = 1.0 - abs(u);\n mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);\n\n // Remove the texture normal bit of the position before scaling it with the\n // model/view matrix.\n gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);\n\n v_tex_a = vec2(a_linesofar * u_patternscale_a.x, normal.y * u_patternscale_a.y + u_tex_y_a);\n v_tex_b = vec2(a_linesofar * u_patternscale_b.x, normal.y * u_patternscale_b.y + u_tex_y_b);\n\n // position of y on the screen\n float y = gl_Position.y / gl_Position.w;\n\n // how much features are squished in the y direction by the tilt\n float squish_scale = length(a_extrude) / length(u_antialiasingmatrix * a_extrude);\n\n // how much features are squished in all directions by the perspectiveness\n float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9));\n\n v_linewidth = vec2(outset, inset);\n v_gamma_scale = perspective_scale * squish_scale;\n}\n" + }, + outline: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n#pragma mapbox: define lowp vec4 outline_color\n#pragma mapbox: define lowp float opacity\n\nvarying vec2 v_pos;\n\nvoid main() {\n #pragma mapbox: initialize lowp vec4 outline_color\n #pragma mapbox: initialize lowp float opacity\n\n float dist = length(v_pos - gl_FragCoord.xy);\n float alpha = smoothstep(1.0, 0.0, dist);\n gl_FragColor = outline_color * (alpha * opacity);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\n\nuniform mat4 u_matrix;\nuniform vec2 u_world;\n\nvarying vec2 v_pos;\n\n#pragma mapbox: define lowp vec4 outline_color\n#pragma mapbox: define lowp float opacity\n\nvoid main() {\n #pragma mapbox: initialize lowp vec4 outline_color\n #pragma mapbox: initialize lowp float opacity\n\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world;\n}\n" + }, + outlinepattern: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_opacity;\nuniform vec2 u_pattern_tl_a;\nuniform vec2 u_pattern_br_a;\nuniform vec2 u_pattern_tl_b;\nuniform vec2 u_pattern_br_b;\nuniform float u_mix;\n\nuniform sampler2D u_image;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\nvarying vec2 v_pos;\n\nvoid main() {\n vec2 imagecoord = mod(v_pos_a, 1.0);\n vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord);\n vec4 color1 = texture2D(u_image, pos);\n\n vec2 imagecoord_b = mod(v_pos_b, 1.0);\n vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b);\n vec4 color2 = texture2D(u_image, pos2);\n\n // find distance to outline for alpha interpolation\n\n float dist = length(v_pos - gl_FragCoord.xy);\n float alpha = smoothstep(1.0, 0.0, dist);\n \n\n gl_FragColor = mix(color1, color2, u_mix) * alpha * u_opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform vec2 u_pattern_size_a;\nuniform vec2 u_pattern_size_b;\nuniform vec2 u_pixel_coord_upper;\nuniform vec2 u_pixel_coord_lower;\nuniform float u_scale_a;\nuniform float u_scale_b;\nuniform float u_tile_units_to_pixels;\n\nattribute vec2 a_pos;\n\nuniform mat4 u_matrix;\nuniform vec2 u_world;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\nvarying vec2 v_pos;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n vec2 scaled_size_a = u_scale_a * u_pattern_size_a;\n vec2 scaled_size_b = u_scale_b * u_pattern_size_b;\n\n // the correct offset needs to be calculated.\n //\n // The offset depends on how many pixels are between the world origin and\n // the edge of the tile:\n // vec2 offset = mod(pixel_coord, size)\n //\n // At high zoom levels there are a ton of pixels between the world origin\n // and the edge of the tile. The glsl spec only guarantees 16 bits of\n // precision for highp floats. We need more than that.\n //\n // The pixel_coord is passed in as two 16 bit values:\n // pixel_coord_upper = floor(pixel_coord / 2^16)\n // pixel_coord_lower = mod(pixel_coord, 2^16)\n //\n // The offset is calculated in a series of steps that should preserve this precision:\n vec2 offset_a = mod(mod(mod(u_pixel_coord_upper, scaled_size_a) * 256.0, scaled_size_a) * 256.0 + u_pixel_coord_lower, scaled_size_a);\n vec2 offset_b = mod(mod(mod(u_pixel_coord_upper, scaled_size_b) * 256.0, scaled_size_b) * 256.0 + u_pixel_coord_lower, scaled_size_b);\n\n v_pos_a = (u_tile_units_to_pixels * a_pos + offset_a) / scaled_size_a;\n v_pos_b = (u_tile_units_to_pixels * a_pos + offset_b) / scaled_size_b;\n\n v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world;\n}\n" + }, + pattern: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_opacity;\nuniform vec2 u_pattern_tl_a;\nuniform vec2 u_pattern_br_a;\nuniform vec2 u_pattern_tl_b;\nuniform vec2 u_pattern_br_b;\nuniform float u_mix;\n\nuniform sampler2D u_image;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\n\nvoid main() {\n\n vec2 imagecoord = mod(v_pos_a, 1.0);\n vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord);\n vec4 color1 = texture2D(u_image, pos);\n\n vec2 imagecoord_b = mod(v_pos_b, 1.0);\n vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b);\n vec4 color2 = texture2D(u_image, pos2);\n\n gl_FragColor = mix(color1, color2, u_mix) * u_opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform mat4 u_matrix;\nuniform vec2 u_pattern_size_a;\nuniform vec2 u_pattern_size_b;\nuniform vec2 u_pixel_coord_upper;\nuniform vec2 u_pixel_coord_lower;\nuniform float u_scale_a;\nuniform float u_scale_b;\nuniform float u_tile_units_to_pixels;\n\nattribute vec2 a_pos;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n vec2 scaled_size_a = u_scale_a * u_pattern_size_a;\n vec2 scaled_size_b = u_scale_b * u_pattern_size_b;\n\n // the correct offset needs to be calculated.\n //\n // The offset depends on how many pixels are between the world origin and\n // the edge of the tile:\n // vec2 offset = mod(pixel_coord, size)\n //\n // At high zoom levels there are a ton of pixels between the world origin\n // and the edge of the tile. The glsl spec only guarantees 16 bits of\n // precision for highp floats. We need more than that.\n //\n // The pixel_coord is passed in as two 16 bit values:\n // pixel_coord_upper = floor(pixel_coord / 2^16)\n // pixel_coord_lower = mod(pixel_coord, 2^16)\n //\n // The offset is calculated in a series of steps that should preserve this precision:\n vec2 offset_a = mod(mod(mod(u_pixel_coord_upper, scaled_size_a) * 256.0, scaled_size_a) * 256.0 + u_pixel_coord_lower, scaled_size_a);\n vec2 offset_b = mod(mod(mod(u_pixel_coord_upper, scaled_size_b) * 256.0, scaled_size_b) * 256.0 + u_pixel_coord_lower, scaled_size_b);\n\n v_pos_a = (u_tile_units_to_pixels * a_pos + offset_a) / scaled_size_a;\n v_pos_b = (u_tile_units_to_pixels * a_pos + offset_b) / scaled_size_b;\n}\n" + }, + raster: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_opacity0;\nuniform float u_opacity1;\nuniform sampler2D u_image0;\nuniform sampler2D u_image1;\nvarying vec2 v_pos0;\nvarying vec2 v_pos1;\n\nuniform float u_brightness_low;\nuniform float u_brightness_high;\n\nuniform float u_saturation_factor;\nuniform float u_contrast_factor;\nuniform vec3 u_spin_weights;\n\nvoid main() {\n\n // read and cross-fade colors from the main and parent tiles\n vec4 color0 = texture2D(u_image0, v_pos0);\n vec4 color1 = texture2D(u_image1, v_pos1);\n vec4 color = color0 * u_opacity0 + color1 * u_opacity1;\n vec3 rgb = color.rgb;\n\n // spin\n rgb = vec3(\n dot(rgb, u_spin_weights.xyz),\n dot(rgb, u_spin_weights.zxy),\n dot(rgb, u_spin_weights.yzx));\n\n // saturation\n float average = (color.r + color.g + color.b) / 3.0;\n rgb += (average - rgb) * u_saturation_factor;\n\n // contrast\n rgb = (rgb - 0.5) * u_contrast_factor + 0.5;\n\n // brightness\n vec3 u_high_vec = vec3(u_brightness_low, u_brightness_low, u_brightness_low);\n vec3 u_low_vec = vec3(u_brightness_high, u_brightness_high, u_brightness_high);\n\n gl_FragColor = vec4(mix(u_high_vec, u_low_vec, rgb), color.a);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform mat4 u_matrix;\nuniform vec2 u_tl_parent;\nuniform float u_scale_parent;\nuniform float u_buffer_scale;\n\nattribute vec2 a_pos;\nattribute vec2 a_texture_pos;\n\nvarying vec2 v_pos0;\nvarying vec2 v_pos1;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n v_pos0 = (((a_texture_pos / 32767.0) - 0.5) / u_buffer_scale ) + 0.5;\n v_pos1 = (v_pos0 * u_scale_parent) + u_tl_parent;\n}\n" + }, + icon: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform sampler2D u_texture;\nuniform sampler2D u_fadetexture;\nuniform lowp float u_opacity;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\n\nvoid main() {\n lowp float alpha = texture2D(u_fadetexture, v_fade_tex).a * u_opacity;\n gl_FragColor = texture2D(u_texture, v_tex) * alpha;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\nattribute vec2 a_offset;\nattribute vec2 a_texture_pos;\nattribute vec4 a_data;\n\n\n// matrix is for the vertex position.\nuniform mat4 u_matrix;\n\nuniform mediump float u_zoom;\nuniform bool u_rotate_with_map;\nuniform vec2 u_extrude_scale;\n\nuniform vec2 u_texsize;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\n\nvoid main() {\n vec2 a_tex = a_texture_pos.xy;\n mediump float a_labelminzoom = a_data[0];\n mediump vec2 a_zoom = a_data.pq;\n mediump float a_minzoom = a_zoom[0];\n mediump float a_maxzoom = a_zoom[1];\n\n // u_zoom is the current zoom level adjusted for the change in font size\n mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom));\n\n vec2 extrude = u_extrude_scale * (a_offset / 64.0);\n if (u_rotate_with_map) {\n gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1);\n gl_Position.z += z * gl_Position.w;\n } else {\n gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);\n }\n\n v_tex = a_tex / u_texsize;\n v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0);\n}\n" + }, + sdf: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform sampler2D u_texture;\nuniform sampler2D u_fadetexture;\nuniform lowp vec4 u_color;\nuniform lowp float u_opacity;\nuniform lowp float u_buffer;\nuniform lowp float u_gamma;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\nvarying float v_gamma_scale;\n\nvoid main() {\n lowp float dist = texture2D(u_texture, v_tex).a;\n lowp float fade_alpha = texture2D(u_fadetexture, v_fade_tex).a;\n lowp float gamma = u_gamma * v_gamma_scale;\n lowp float alpha = smoothstep(u_buffer - gamma, u_buffer + gamma, dist) * fade_alpha;\n\n gl_FragColor = u_color * (alpha * u_opacity);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nconst float PI = 3.141592653589793;\n\nattribute vec2 a_pos;\nattribute vec2 a_offset;\nattribute vec2 a_texture_pos;\nattribute vec4 a_data;\n\n\n// matrix is for the vertex position.\nuniform mat4 u_matrix;\n\nuniform mediump float u_zoom;\nuniform bool u_rotate_with_map;\nuniform bool u_pitch_with_map;\nuniform mediump float u_pitch;\nuniform mediump float u_bearing;\nuniform mediump float u_aspect_ratio;\nuniform vec2 u_extrude_scale;\n\nuniform vec2 u_texsize;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\nvarying float v_gamma_scale;\n\nvoid main() {\n vec2 a_tex = a_texture_pos.xy;\n mediump float a_labelminzoom = a_data[0];\n mediump vec2 a_zoom = a_data.pq;\n mediump float a_minzoom = a_zoom[0];\n mediump float a_maxzoom = a_zoom[1];\n\n // u_zoom is the current zoom level adjusted for the change in font size\n mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom));\n\n // pitch-alignment: map\n // rotation-alignment: map | viewport\n if (u_pitch_with_map) {\n lowp float angle = u_rotate_with_map ? (a_data[1] / 256.0 * 2.0 * PI) : u_bearing;\n lowp float asin = sin(angle);\n lowp float acos = cos(angle);\n mat2 RotationMatrix = mat2(acos, asin, -1.0 * asin, acos);\n vec2 offset = RotationMatrix * a_offset;\n vec2 extrude = u_extrude_scale * (offset / 64.0);\n gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1);\n gl_Position.z += z * gl_Position.w;\n // pitch-alignment: viewport\n // rotation-alignment: map\n } else if (u_rotate_with_map) {\n // foreshortening factor to apply on pitched maps\n // as a label goes from horizontal <=> vertical in angle\n // it goes from 0% foreshortening to up to around 70% foreshortening\n lowp float pitchfactor = 1.0 - cos(u_pitch * sin(u_pitch * 0.75));\n\n lowp float lineangle = a_data[1] / 256.0 * 2.0 * PI;\n\n // use the lineangle to position points a,b along the line\n // project the points and calculate the label angle in projected space\n // this calculation allows labels to be rendered unskewed on pitched maps\n vec4 a = u_matrix * vec4(a_pos, 0, 1);\n vec4 b = u_matrix * vec4(a_pos + vec2(cos(lineangle),sin(lineangle)), 0, 1);\n lowp float angle = atan((b[1]/b[3] - a[1]/a[3])/u_aspect_ratio, b[0]/b[3] - a[0]/a[3]);\n lowp float asin = sin(angle);\n lowp float acos = cos(angle);\n mat2 RotationMatrix = mat2(acos, -1.0 * asin, asin, acos);\n\n vec2 offset = RotationMatrix * (vec2((1.0-pitchfactor)+(pitchfactor*cos(angle*2.0)), 1.0) * a_offset);\n vec2 extrude = u_extrude_scale * (offset / 64.0);\n gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);\n gl_Position.z += z * gl_Position.w;\n // pitch-alignment: viewport\n // rotation-alignment: viewport\n } else {\n vec2 extrude = u_extrude_scale * (a_offset / 64.0);\n gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);\n }\n\n v_gamma_scale = (gl_Position.w - 0.5);\n\n v_tex = a_tex / u_texsize;\n v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0);\n}\n" + }, + collisionbox: { + fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_zoom;\nuniform float u_maxzoom;\n\nvarying float v_max_zoom;\nvarying float v_placement_zoom;\n\nvoid main() {\n\n float alpha = 0.5;\n\n gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0) * alpha;\n\n if (v_placement_zoom > u_zoom) {\n gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * alpha;\n }\n\n if (u_zoom >= v_max_zoom) {\n gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) * alpha * 0.25;\n }\n\n if (v_placement_zoom >= u_maxzoom) {\n gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0) * alpha * 0.2;\n }\n}\n", + vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\nattribute vec2 a_extrude;\nattribute vec2 a_data;\n\nuniform mat4 u_matrix;\nuniform float u_scale;\n\nvarying float v_max_zoom;\nvarying float v_placement_zoom;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos + a_extrude / u_scale, 0.0, 1.0);\n\n v_max_zoom = a_data.x;\n v_placement_zoom = a_data.y;\n}\n" } -}) +}; -proto.begin = function() { - var gl = this.gl - var shape = this.shape - if(!gl) { - return - } - - this.fbo.bind() - gl.clearColor(1,1,1,1) - gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) -} - -proto.end = function() { - var gl = this.gl - if(!gl) { - return - } - gl.bindFramebuffer(gl.FRAMEBUFFER, null) - if(!this._readTimeout) { - clearTimeout(this._readTimeout) - } - this._readTimeout = setTimeout(this._readCallback, 1) -} - -proto.query = function(x, y, radius) { - if(!this.gl) { - return null - } - - var shape = this.fbo.shape.slice() - - x = x|0 - y = y|0 - if(typeof radius !== 'number') { - radius = 1.0 - } - - var x0 = Math.min(Math.max(x - radius, 0), shape[0])|0 - var x1 = Math.min(Math.max(x + radius, 0), shape[0])|0 - var y0 = Math.min(Math.max(y - radius, 0), shape[1])|0 - var y1 = Math.min(Math.max(y + radius, 0), shape[1])|0 - - if(x1 <= x0 || y1 <= y0) { - return null - } - - var dims = [x1-x0,y1-y0] - var region = ndarray( - this.buffer, - [dims[0], dims[1], 4], - [4, shape[0]*4, 1], - 4*(x0 + shape[0]*y0)); - - var closest = selectRange(region.hi(dims[0],dims[1],1), radius, radius) - var dx = closest[0] - var dy = closest[1] - if(dx < 0 || Math.pow(this.radius, 2) < closest[2]) { - return null - } - - var c0 = region.get(dx, dy, 0) - var c1 = region.get(dx, dy, 1) - var c2 = region.get(dx, dy, 2) - var c3 = region.get(dx, dy, 3) - - return new SelectResult( - (dx + x0)|0, - (dy + y0)|0, - c0, - [c1, c2, c3], - Math.sqrt(closest[2])) -} - -proto.dispose = function() { - if(!this.gl) { - return - } - this.fbo.dispose() - pool.free(this.buffer) - this.gl = null - if(this._readTimeout) { - clearTimeout(this._readTimeout) - } -} - -function createSelectBuffer(gl, shape) { - var fbo = createFBO(gl, shape) - var buffer = pool.mallocUint8(shape[0]*shape[1]*4) - return new SelectBuffer(gl, fbo, buffer) -} - -},{"bit-twiddle":286,"cwise/lib/wrapper":287,"gl-fbo":292,"ndarray":1118,"typedarray-pool":295}],297:[function(require,module,exports){ -'use strict' - -var createUniformWrapper = require('./lib/create-uniforms') -var createAttributeWrapper = require('./lib/create-attributes') -var makeReflect = require('./lib/reflect') -var shaderCache = require('./lib/shader-cache') -var runtime = require('./lib/runtime-reflect') -var GLError = require("./lib/GLError") - -//Shader object -function Shader(gl) { - this.gl = gl - this.gl.lastAttribCount = 0 // fixme where else should we store info, safe but not nice on the gl object - - //Default initialize these to null - this._vref = - this._fref = - this._relink = - this.vertShader = - this.fragShader = - this.program = - this.attributes = - this.uniforms = - this.types = null -} - -var proto = Shader.prototype - -proto.bind = function() { - if(!this.program) { - this._relink() - } - - // ensuring that we have the right number of enabled vertex attributes - var i - var newAttribCount = this.gl.getProgramParameter(this.program, this.gl.ACTIVE_ATTRIBUTES) // more robust approach - //var newAttribCount = Object.keys(this.attributes).length // avoids the probably immaterial introspection slowdown - var oldAttribCount = this.gl.lastAttribCount - if(newAttribCount > oldAttribCount) { - for(i = oldAttribCount; i < newAttribCount; i++) { - this.gl.enableVertexAttribArray(i) - } - } else if(oldAttribCount > newAttribCount) { - for(i = newAttribCount; i < oldAttribCount; i++) { - this.gl.disableVertexAttribArray(i) - } - } - - this.gl.lastAttribCount = newAttribCount - - this.gl.useProgram(this.program) -} - -proto.dispose = function() { - - // disabling vertex attributes so new shader starts with zero - // and it's also useful if all shaders are disposed but the - // gl context is reused for subsequent replotting - var oldAttribCount = this.gl.lastAttribCount - for (var i = 0; i < oldAttribCount; i++) { - this.gl.disableVertexAttribArray(i) - } - this.gl.lastAttribCount = 0 - - if(this._fref) { - this._fref.dispose() - } - if(this._vref) { - this._vref.dispose() - } - this.attributes = - this.types = - this.vertShader = - this.fragShader = - this.program = - this._relink = - this._fref = - this._vref = null -} - -function compareAttributes(a, b) { - if(a.name < b.name) { - return -1 - } - return 1 -} - -//Update export hook for glslify-live -proto.update = function( - vertSource - , fragSource - , uniforms - , attributes) { - - //If only one object passed, assume glslify style output - if(!fragSource || arguments.length === 1) { - var obj = vertSource - vertSource = obj.vertex - fragSource = obj.fragment - uniforms = obj.uniforms - attributes = obj.attributes - } - - var wrapper = this - var gl = wrapper.gl - - //Compile vertex and fragment shaders - var pvref = wrapper._vref - wrapper._vref = shaderCache.shader(gl, gl.VERTEX_SHADER, vertSource) - if(pvref) { - pvref.dispose() - } - wrapper.vertShader = wrapper._vref.shader - var pfref = this._fref - wrapper._fref = shaderCache.shader(gl, gl.FRAGMENT_SHADER, fragSource) - if(pfref) { - pfref.dispose() - } - wrapper.fragShader = wrapper._fref.shader - - //If uniforms/attributes is not specified, use RT reflection - if(!uniforms || !attributes) { - - //Create initial test program - var testProgram = gl.createProgram() - gl.attachShader(testProgram, wrapper.fragShader) - gl.attachShader(testProgram, wrapper.vertShader) - gl.linkProgram(testProgram) - if(!gl.getProgramParameter(testProgram, gl.LINK_STATUS)) { - var errLog = gl.getProgramInfoLog(testProgram) - throw new GLError(errLog, 'Error linking program:' + errLog) - } - - //Load data from runtime - uniforms = uniforms || runtime.uniforms(gl, testProgram) - attributes = attributes || runtime.attributes(gl, testProgram) - - //Release test program - gl.deleteProgram(testProgram) - } - - //Sort attributes lexicographically - // overrides undefined WebGL behavior for attribute locations - attributes = attributes.slice() - attributes.sort(compareAttributes) - - //Convert attribute types, read out locations - var attributeUnpacked = [] - var attributeNames = [] - var attributeLocations = [] - var i - for(i=0; i= 0) { - var size = attr.type.charAt(attr.type.length-1)|0 - var locVector = new Array(size) - for(var j=0; j= 0) { - curLocation += 1 - } - attributeLocations[i] = curLocation - } - } - - //Rebuild program and recompute all uniform locations - var uniformLocations = new Array(uniforms.length) - function relink() { - wrapper.program = shaderCache.program( - gl - , wrapper._vref - , wrapper._fref - , attributeNames - , attributeLocations) - - for(var i=0; i= 0) { - var d = type.charCodeAt(type.length-1) - 48 - if(d < 2 || d > 4) { - throw new GLError('', 'Invalid data type for attribute ' + name + ': ' + type) - } - addVectorAttribute( - gl - , wrapper - , locs[0] - , locations - , d - , obj - , name) - } else if(type.indexOf('mat') >= 0) { - var d = type.charCodeAt(type.length-1) - 48 - if(d < 2 || d > 4) { - throw new GLError('', 'Invalid data type for attribute ' + name + ': ' + type) - } - addMatrixAttribute( - gl - , wrapper - , locs - , locations - , d - , obj - , name) - } else { - throw new GLError('', 'Unknown data type for attribute ' + name + ': ' + type) - } - break - } - } - return obj -} - -},{"./GLError":298}],300:[function(require,module,exports){ -'use strict' - -var coallesceUniforms = require('./reflect') -var GLError = require("./GLError") - -module.exports = createUniformWrapper - -//Binds a function and returns a value -function identity(x) { - var c = new Function('y', 'return function(){return y}') - return c(x) -} - -function makeVector(length, fill) { - var result = new Array(length) - for(var i=0; i 4) { - throw new GLError('', 'Invalid data type') - } - switch(type.charAt(0)) { - case 'b': - case 'i': - return 'gl.uniform' + d + 'iv(locations[' + index + '],obj' + path + ')' - case 'v': - return 'gl.uniform' + d + 'fv(locations[' + index + '],obj' + path + ')' - default: - throw new GLError('', 'Unrecognized data type for vector ' + name + ': ' + type) - } - } else if(type.indexOf('mat') === 0 && type.length === 4) { - var d = type.charCodeAt(type.length-1) - 48 - if(d < 2 || d > 4) { - throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + type) - } - return 'gl.uniformMatrix' + d + 'fv(locations[' + index + '],false,obj' + path + ')' - } else { - throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type) - } - break - } - } - - function enumerateIndices(prefix, type) { - if(typeof type !== 'object') { - return [ [prefix, type] ] - } - var indices = [] - for(var id in type) { - var prop = type[id] - var tprefix = prefix - if(parseInt(id) + '' === id) { - tprefix += '[' + id + ']' - } else { - tprefix += '.' + id - } - if(typeof prop === 'object') { - indices.push.apply(indices, enumerateIndices(tprefix, prop)) - } else { - indices.push([tprefix, prop]) - } - } - return indices - } - - function makeSetter(type) { - var code = [ 'return function updateProperty(obj){' ] - var indices = enumerateIndices('', type) - for(var i=0; i 4) { - throw new GLError('', 'Invalid data type') - } - if(type.charAt(0) === 'b') { - return makeVector(d, false) - } - return makeVector(d, 0) - } else if(type.indexOf('mat') === 0 && type.length === 4) { - var d = type.charCodeAt(type.length-1) - 48 - if(d < 2 || d > 4) { - throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + type) - } - return makeVector(d*d, 0) - } else { - throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type) - } - break - } - } - - function storeProperty(obj, prop, type) { - if(typeof type === 'object') { - var child = processObject(type) - Object.defineProperty(obj, prop, { - get: identity(child), - set: makeSetter(type), - enumerable: true, - configurable: false - }) - } else { - if(locations[type]) { - Object.defineProperty(obj, prop, { - get: makeGetter(type), - set: makeSetter(type), - enumerable: true, - configurable: false - }) - } else { - obj[prop] = defaultValue(uniforms[type].type) - } - } - } - - function processObject(obj) { - var result - if(Array.isArray(obj)) { - result = new Array(obj.length) - for(var i=0; i 1) { - if(!(x[0] in o)) { - o[x[0]] = [] - } - o = o[x[0]] - for(var k=1; k 1) { - for(var j=0; j - * - * Copyright (c) 2014-2015, Jon Schlinkert. - * Licensed under the MIT license. - */ +module.exports.util = "float evaluate_zoom_function_1(const vec4 values, const float t) {\n if (t < 1.0) {\n return mix(values[0], values[1], t);\n } else if (t < 2.0) {\n return mix(values[1], values[2], t - 1.0);\n } else {\n return mix(values[2], values[3], t - 2.0);\n }\n}\nvec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) {\n if (t < 1.0) {\n return mix(value0, value1, t);\n } else if (t < 2.0) {\n return mix(value1, value2, t - 1.0);\n } else {\n return mix(value2, value3, t - 2.0);\n }\n}\n"; +},{"path":463}],280:[function(require,module,exports){ 'use strict'; -var repeat = require('repeat-string'); +var format = require('util').format; -module.exports = function padLeft(str, num, ch) { - ch = typeof ch !== 'undefined' ? (ch + '') : ' '; - return repeat(ch, num) + str; -}; -},{"repeat-string":307}],307:[function(require,module,exports){ -/*! - * repeat-string - * - * Copyright (c) 2014-2015, Jon Schlinkert. - * Licensed under the MIT License. - */ +function ValidationError(key, value /*, message, ...*/) { + this.message = ( + (key ? key + ': ' : '') + + format.apply(format, Array.prototype.slice.call(arguments, 2)) + ); + if (value !== null && value !== undefined && value.__line__) { + this.line = value.__line__; + } +} + +module.exports = ValidationError; + +},{"util":1001}],281:[function(require,module,exports){ 'use strict'; -/** - * Results cache - */ - -var res = ''; -var cache; - -/** - * Expose `repeat` - */ - -module.exports = repeat; - -/** - * Repeat the given `string` the specified `number` - * of times. - * - * **Example:** - * - * ```js - * var repeat = require('repeat-string'); - * repeat('A', 5); - * //=> AAAAA - * ``` - * - * @param {String} `string` The string to repeat - * @param {Number} `number` The number of times to repeat the string - * @return {String} Repeated string - * @api public - */ - -function repeat(str, num) { - if (typeof str !== 'string') { - throw new TypeError('expected a string'); - } - - // cover common, quick use cases - if (num === 1) return str; - if (num === 2) return str + str; - - var max = str.length * num; - if (cache !== str || typeof cache === 'undefined') { - cache = str; - res = ''; - } else if (res.length >= max) { - return res.substr(0, max); - } - - while (max > res.length && num > 1) { - if (num & 1) { - res += str; - } - - num >>= 1; - str += str; - } - - res += str; - res = res.substr(0, max); - return res; -} - -},{}],308:[function(require,module,exports){ -module.exports = { - 0: 'NONE', - 1: 'ONE', - 2: 'LINE_LOOP', - 3: 'LINE_STRIP', - 4: 'TRIANGLES', - 5: 'TRIANGLE_STRIP', - 6: 'TRIANGLE_FAN', - 256: 'DEPTH_BUFFER_BIT', - 512: 'NEVER', - 513: 'LESS', - 514: 'EQUAL', - 515: 'LEQUAL', - 516: 'GREATER', - 517: 'NOTEQUAL', - 518: 'GEQUAL', - 519: 'ALWAYS', - 768: 'SRC_COLOR', - 769: 'ONE_MINUS_SRC_COLOR', - 770: 'SRC_ALPHA', - 771: 'ONE_MINUS_SRC_ALPHA', - 772: 'DST_ALPHA', - 773: 'ONE_MINUS_DST_ALPHA', - 774: 'DST_COLOR', - 775: 'ONE_MINUS_DST_COLOR', - 776: 'SRC_ALPHA_SATURATE', - 1024: 'STENCIL_BUFFER_BIT', - 1028: 'FRONT', - 1029: 'BACK', - 1032: 'FRONT_AND_BACK', - 1280: 'INVALID_ENUM', - 1281: 'INVALID_VALUE', - 1282: 'INVALID_OPERATION', - 1285: 'OUT_OF_MEMORY', - 1286: 'INVALID_FRAMEBUFFER_OPERATION', - 2304: 'CW', - 2305: 'CCW', - 2849: 'LINE_WIDTH', - 2884: 'CULL_FACE', - 2885: 'CULL_FACE_MODE', - 2886: 'FRONT_FACE', - 2928: 'DEPTH_RANGE', - 2929: 'DEPTH_TEST', - 2930: 'DEPTH_WRITEMASK', - 2931: 'DEPTH_CLEAR_VALUE', - 2932: 'DEPTH_FUNC', - 2960: 'STENCIL_TEST', - 2961: 'STENCIL_CLEAR_VALUE', - 2962: 'STENCIL_FUNC', - 2963: 'STENCIL_VALUE_MASK', - 2964: 'STENCIL_FAIL', - 2965: 'STENCIL_PASS_DEPTH_FAIL', - 2966: 'STENCIL_PASS_DEPTH_PASS', - 2967: 'STENCIL_REF', - 2968: 'STENCIL_WRITEMASK', - 2978: 'VIEWPORT', - 3024: 'DITHER', - 3042: 'BLEND', - 3088: 'SCISSOR_BOX', - 3089: 'SCISSOR_TEST', - 3106: 'COLOR_CLEAR_VALUE', - 3107: 'COLOR_WRITEMASK', - 3317: 'UNPACK_ALIGNMENT', - 3333: 'PACK_ALIGNMENT', - 3379: 'MAX_TEXTURE_SIZE', - 3386: 'MAX_VIEWPORT_DIMS', - 3408: 'SUBPIXEL_BITS', - 3410: 'RED_BITS', - 3411: 'GREEN_BITS', - 3412: 'BLUE_BITS', - 3413: 'ALPHA_BITS', - 3414: 'DEPTH_BITS', - 3415: 'STENCIL_BITS', - 3553: 'TEXTURE_2D', - 4352: 'DONT_CARE', - 4353: 'FASTEST', - 4354: 'NICEST', - 5120: 'BYTE', - 5121: 'UNSIGNED_BYTE', - 5122: 'SHORT', - 5123: 'UNSIGNED_SHORT', - 5124: 'INT', - 5125: 'UNSIGNED_INT', - 5126: 'FLOAT', - 5386: 'INVERT', - 5890: 'TEXTURE', - 6401: 'STENCIL_INDEX', - 6402: 'DEPTH_COMPONENT', - 6406: 'ALPHA', - 6407: 'RGB', - 6408: 'RGBA', - 6409: 'LUMINANCE', - 6410: 'LUMINANCE_ALPHA', - 7680: 'KEEP', - 7681: 'REPLACE', - 7682: 'INCR', - 7683: 'DECR', - 7936: 'VENDOR', - 7937: 'RENDERER', - 7938: 'VERSION', - 9728: 'NEAREST', - 9729: 'LINEAR', - 9984: 'NEAREST_MIPMAP_NEAREST', - 9985: 'LINEAR_MIPMAP_NEAREST', - 9986: 'NEAREST_MIPMAP_LINEAR', - 9987: 'LINEAR_MIPMAP_LINEAR', - 10240: 'TEXTURE_MAG_FILTER', - 10241: 'TEXTURE_MIN_FILTER', - 10242: 'TEXTURE_WRAP_S', - 10243: 'TEXTURE_WRAP_T', - 10497: 'REPEAT', - 10752: 'POLYGON_OFFSET_UNITS', - 16384: 'COLOR_BUFFER_BIT', - 32769: 'CONSTANT_COLOR', - 32770: 'ONE_MINUS_CONSTANT_COLOR', - 32771: 'CONSTANT_ALPHA', - 32772: 'ONE_MINUS_CONSTANT_ALPHA', - 32773: 'BLEND_COLOR', - 32774: 'FUNC_ADD', - 32777: 'BLEND_EQUATION_RGB', - 32778: 'FUNC_SUBTRACT', - 32779: 'FUNC_REVERSE_SUBTRACT', - 32819: 'UNSIGNED_SHORT_4_4_4_4', - 32820: 'UNSIGNED_SHORT_5_5_5_1', - 32823: 'POLYGON_OFFSET_FILL', - 32824: 'POLYGON_OFFSET_FACTOR', - 32854: 'RGBA4', - 32855: 'RGB5_A1', - 32873: 'TEXTURE_BINDING_2D', - 32926: 'SAMPLE_ALPHA_TO_COVERAGE', - 32928: 'SAMPLE_COVERAGE', - 32936: 'SAMPLE_BUFFERS', - 32937: 'SAMPLES', - 32938: 'SAMPLE_COVERAGE_VALUE', - 32939: 'SAMPLE_COVERAGE_INVERT', - 32968: 'BLEND_DST_RGB', - 32969: 'BLEND_SRC_RGB', - 32970: 'BLEND_DST_ALPHA', - 32971: 'BLEND_SRC_ALPHA', - 33071: 'CLAMP_TO_EDGE', - 33170: 'GENERATE_MIPMAP_HINT', - 33189: 'DEPTH_COMPONENT16', - 33306: 'DEPTH_STENCIL_ATTACHMENT', - 33635: 'UNSIGNED_SHORT_5_6_5', - 33648: 'MIRRORED_REPEAT', - 33901: 'ALIASED_POINT_SIZE_RANGE', - 33902: 'ALIASED_LINE_WIDTH_RANGE', - 33984: 'TEXTURE0', - 33985: 'TEXTURE1', - 33986: 'TEXTURE2', - 33987: 'TEXTURE3', - 33988: 'TEXTURE4', - 33989: 'TEXTURE5', - 33990: 'TEXTURE6', - 33991: 'TEXTURE7', - 33992: 'TEXTURE8', - 33993: 'TEXTURE9', - 33994: 'TEXTURE10', - 33995: 'TEXTURE11', - 33996: 'TEXTURE12', - 33997: 'TEXTURE13', - 33998: 'TEXTURE14', - 33999: 'TEXTURE15', - 34000: 'TEXTURE16', - 34001: 'TEXTURE17', - 34002: 'TEXTURE18', - 34003: 'TEXTURE19', - 34004: 'TEXTURE20', - 34005: 'TEXTURE21', - 34006: 'TEXTURE22', - 34007: 'TEXTURE23', - 34008: 'TEXTURE24', - 34009: 'TEXTURE25', - 34010: 'TEXTURE26', - 34011: 'TEXTURE27', - 34012: 'TEXTURE28', - 34013: 'TEXTURE29', - 34014: 'TEXTURE30', - 34015: 'TEXTURE31', - 34016: 'ACTIVE_TEXTURE', - 34024: 'MAX_RENDERBUFFER_SIZE', - 34041: 'DEPTH_STENCIL', - 34055: 'INCR_WRAP', - 34056: 'DECR_WRAP', - 34067: 'TEXTURE_CUBE_MAP', - 34068: 'TEXTURE_BINDING_CUBE_MAP', - 34069: 'TEXTURE_CUBE_MAP_POSITIVE_X', - 34070: 'TEXTURE_CUBE_MAP_NEGATIVE_X', - 34071: 'TEXTURE_CUBE_MAP_POSITIVE_Y', - 34072: 'TEXTURE_CUBE_MAP_NEGATIVE_Y', - 34073: 'TEXTURE_CUBE_MAP_POSITIVE_Z', - 34074: 'TEXTURE_CUBE_MAP_NEGATIVE_Z', - 34076: 'MAX_CUBE_MAP_TEXTURE_SIZE', - 34338: 'VERTEX_ATTRIB_ARRAY_ENABLED', - 34339: 'VERTEX_ATTRIB_ARRAY_SIZE', - 34340: 'VERTEX_ATTRIB_ARRAY_STRIDE', - 34341: 'VERTEX_ATTRIB_ARRAY_TYPE', - 34342: 'CURRENT_VERTEX_ATTRIB', - 34373: 'VERTEX_ATTRIB_ARRAY_POINTER', - 34466: 'NUM_COMPRESSED_TEXTURE_FORMATS', - 34467: 'COMPRESSED_TEXTURE_FORMATS', - 34660: 'BUFFER_SIZE', - 34661: 'BUFFER_USAGE', - 34816: 'STENCIL_BACK_FUNC', - 34817: 'STENCIL_BACK_FAIL', - 34818: 'STENCIL_BACK_PASS_DEPTH_FAIL', - 34819: 'STENCIL_BACK_PASS_DEPTH_PASS', - 34877: 'BLEND_EQUATION_ALPHA', - 34921: 'MAX_VERTEX_ATTRIBS', - 34922: 'VERTEX_ATTRIB_ARRAY_NORMALIZED', - 34930: 'MAX_TEXTURE_IMAGE_UNITS', - 34962: 'ARRAY_BUFFER', - 34963: 'ELEMENT_ARRAY_BUFFER', - 34964: 'ARRAY_BUFFER_BINDING', - 34965: 'ELEMENT_ARRAY_BUFFER_BINDING', - 34975: 'VERTEX_ATTRIB_ARRAY_BUFFER_BINDING', - 35040: 'STREAM_DRAW', - 35044: 'STATIC_DRAW', - 35048: 'DYNAMIC_DRAW', - 35632: 'FRAGMENT_SHADER', - 35633: 'VERTEX_SHADER', - 35660: 'MAX_VERTEX_TEXTURE_IMAGE_UNITS', - 35661: 'MAX_COMBINED_TEXTURE_IMAGE_UNITS', - 35663: 'SHADER_TYPE', - 35664: 'FLOAT_VEC2', - 35665: 'FLOAT_VEC3', - 35666: 'FLOAT_VEC4', - 35667: 'INT_VEC2', - 35668: 'INT_VEC3', - 35669: 'INT_VEC4', - 35670: 'BOOL', - 35671: 'BOOL_VEC2', - 35672: 'BOOL_VEC3', - 35673: 'BOOL_VEC4', - 35674: 'FLOAT_MAT2', - 35675: 'FLOAT_MAT3', - 35676: 'FLOAT_MAT4', - 35678: 'SAMPLER_2D', - 35680: 'SAMPLER_CUBE', - 35712: 'DELETE_STATUS', - 35713: 'COMPILE_STATUS', - 35714: 'LINK_STATUS', - 35715: 'VALIDATE_STATUS', - 35716: 'INFO_LOG_LENGTH', - 35717: 'ATTACHED_SHADERS', - 35718: 'ACTIVE_UNIFORMS', - 35719: 'ACTIVE_UNIFORM_MAX_LENGTH', - 35720: 'SHADER_SOURCE_LENGTH', - 35721: 'ACTIVE_ATTRIBUTES', - 35722: 'ACTIVE_ATTRIBUTE_MAX_LENGTH', - 35724: 'SHADING_LANGUAGE_VERSION', - 35725: 'CURRENT_PROGRAM', - 36003: 'STENCIL_BACK_REF', - 36004: 'STENCIL_BACK_VALUE_MASK', - 36005: 'STENCIL_BACK_WRITEMASK', - 36006: 'FRAMEBUFFER_BINDING', - 36007: 'RENDERBUFFER_BINDING', - 36048: 'FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE', - 36049: 'FRAMEBUFFER_ATTACHMENT_OBJECT_NAME', - 36050: 'FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL', - 36051: 'FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE', - 36053: 'FRAMEBUFFER_COMPLETE', - 36054: 'FRAMEBUFFER_INCOMPLETE_ATTACHMENT', - 36055: 'FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT', - 36057: 'FRAMEBUFFER_INCOMPLETE_DIMENSIONS', - 36061: 'FRAMEBUFFER_UNSUPPORTED', - 36064: 'COLOR_ATTACHMENT0', - 36096: 'DEPTH_ATTACHMENT', - 36128: 'STENCIL_ATTACHMENT', - 36160: 'FRAMEBUFFER', - 36161: 'RENDERBUFFER', - 36162: 'RENDERBUFFER_WIDTH', - 36163: 'RENDERBUFFER_HEIGHT', - 36164: 'RENDERBUFFER_INTERNAL_FORMAT', - 36168: 'STENCIL_INDEX8', - 36176: 'RENDERBUFFER_RED_SIZE', - 36177: 'RENDERBUFFER_GREEN_SIZE', - 36178: 'RENDERBUFFER_BLUE_SIZE', - 36179: 'RENDERBUFFER_ALPHA_SIZE', - 36180: 'RENDERBUFFER_DEPTH_SIZE', - 36181: 'RENDERBUFFER_STENCIL_SIZE', - 36194: 'RGB565', - 36336: 'LOW_FLOAT', - 36337: 'MEDIUM_FLOAT', - 36338: 'HIGH_FLOAT', - 36339: 'LOW_INT', - 36340: 'MEDIUM_INT', - 36341: 'HIGH_INT', - 36346: 'SHADER_COMPILER', - 36347: 'MAX_VERTEX_UNIFORM_VECTORS', - 36348: 'MAX_VARYING_VECTORS', - 36349: 'MAX_FRAGMENT_UNIFORM_VECTORS', - 37440: 'UNPACK_FLIP_Y_WEBGL', - 37441: 'UNPACK_PREMULTIPLY_ALPHA_WEBGL', - 37442: 'CONTEXT_LOST_WEBGL', - 37443: 'UNPACK_COLORSPACE_CONVERSION_WEBGL', - 37444: 'BROWSER_DEFAULT_WEBGL' -} - -},{}],309:[function(require,module,exports){ -var gl10 = require('./1.0/numbers') - -module.exports = function lookupConstant (number) { - return gl10[number] -} - -},{"./1.0/numbers":308}],310:[function(require,module,exports){ -var tokenize = require('glsl-tokenizer') -var atob = require('atob-lite') - -module.exports = getName - -function getName(src) { - var tokens = Array.isArray(src) - ? src - : tokenize(src) - - for (var i = 0; i < tokens.length; i++) { - var token = tokens[i] - if (token.type !== 'preprocessor') continue - var match = token.data.match(/\#define\s+SHADER_NAME(_B64)?\s+(.+)$/) - if (!match) continue - if (!match[2]) continue - - var b64 = match[1] - var name = match[2] - - return (b64 ? atob(name) : name).trim() - } -} - -},{"atob-lite":311,"glsl-tokenizer":318}],311:[function(require,module,exports){ -module.exports = function _atob(str) { - return atob(str) -} - -},{}],312:[function(require,module,exports){ -module.exports = tokenize - -var literals100 = require('./lib/literals') - , operators = require('./lib/operators') - , builtins100 = require('./lib/builtins') - , literals300es = require('./lib/literals-300es') - , builtins300es = require('./lib/builtins-300es') - -var NORMAL = 999 // <-- never emitted - , TOKEN = 9999 // <-- never emitted - , BLOCK_COMMENT = 0 - , LINE_COMMENT = 1 - , PREPROCESSOR = 2 - , OPERATOR = 3 - , INTEGER = 4 - , FLOAT = 5 - , IDENT = 6 - , BUILTIN = 7 - , KEYWORD = 8 - , WHITESPACE = 9 - , EOF = 10 - , HEX = 11 - -var map = [ - 'block-comment' - , 'line-comment' - , 'preprocessor' - , 'operator' - , 'integer' - , 'float' - , 'ident' - , 'builtin' - , 'keyword' - , 'whitespace' - , 'eof' - , 'integer' -] - -function tokenize(opt) { - var i = 0 - , total = 0 - , mode = NORMAL - , c - , last - , content = [] - , tokens = [] - , token_idx = 0 - , token_offs = 0 - , line = 1 - , col = 0 - , start = 0 - , isnum = false - , isoperator = false - , input = '' - , len - - opt = opt || {} - var allBuiltins = builtins100 - var allLiterals = literals100 - if (opt.version === '300 es') { - allBuiltins = builtins300es - allLiterals = literals300es - } - - return function(data) { - tokens = [] - if (data !== null) return write(data.replace ? data.replace(/\r\n/g, '\n') : data) - return end() - } - - function token(data) { - if (data.length) { - tokens.push({ - type: map[mode] - , data: data - , position: start - , line: line - , column: col - }) - } - } - - function write(chunk) { - i = 0 - input += chunk - len = input.length - - var last - - while(c = input[i], i < len) { - last = i - - switch(mode) { - case BLOCK_COMMENT: i = block_comment(); break - case LINE_COMMENT: i = line_comment(); break - case PREPROCESSOR: i = preprocessor(); break - case OPERATOR: i = operator(); break - case INTEGER: i = integer(); break - case HEX: i = hex(); break - case FLOAT: i = decimal(); break - case TOKEN: i = readtoken(); break - case WHITESPACE: i = whitespace(); break - case NORMAL: i = normal(); break - } - - if(last !== i) { - switch(input[last]) { - case '\n': col = 0; ++line; break - default: ++col; break - } - } - } - - total += i - input = input.slice(i) - return tokens - } - - function end(chunk) { - if(content.length) { - token(content.join('')) - } - - mode = EOF - token('(eof)') - return tokens - } - - function normal() { - content = content.length ? [] : content - - if(last === '/' && c === '*') { - start = total + i - 1 - mode = BLOCK_COMMENT - last = c - return i + 1 - } - - if(last === '/' && c === '/') { - start = total + i - 1 - mode = LINE_COMMENT - last = c - return i + 1 - } - - if(c === '#') { - mode = PREPROCESSOR - start = total + i - return i - } - - if(/\s/.test(c)) { - mode = WHITESPACE - start = total + i - return i - } - - isnum = /\d/.test(c) - isoperator = /[^\w_]/.test(c) - - start = total + i - mode = isnum ? INTEGER : isoperator ? OPERATOR : TOKEN - return i - } - - function whitespace() { - if(/[^\s]/g.test(c)) { - token(content.join('')) - mode = NORMAL - return i - } - content.push(c) - last = c - return i + 1 - } - - function preprocessor() { - if((c === '\r' || c === '\n') && last !== '\\') { - token(content.join('')) - mode = NORMAL - return i - } - content.push(c) - last = c - return i + 1 - } - - function line_comment() { - return preprocessor() - } - - function block_comment() { - if(c === '/' && last === '*') { - content.push(c) - token(content.join('')) - mode = NORMAL - return i + 1 - } - - content.push(c) - last = c - return i + 1 - } - - function operator() { - if(last === '.' && /\d/.test(c)) { - mode = FLOAT - return i - } - - if(last === '/' && c === '*') { - mode = BLOCK_COMMENT - return i - } - - if(last === '/' && c === '/') { - mode = LINE_COMMENT - return i - } - - if(c === '.' && content.length) { - while(determine_operator(content)); - - mode = FLOAT - return i - } - - if(c === ';' || c === ')' || c === '(') { - if(content.length) while(determine_operator(content)); - token(c) - mode = NORMAL - return i + 1 - } - - var is_composite_operator = content.length === 2 && c !== '=' - if(/[\w_\d\s]/.test(c) || is_composite_operator) { - while(determine_operator(content)); - mode = NORMAL - return i - } - - content.push(c) - last = c - return i + 1 - } - - function determine_operator(buf) { - var j = 0 - , idx - , res - - do { - idx = operators.indexOf(buf.slice(0, buf.length + j).join('')) - res = operators[idx] - - if(idx === -1) { - if(j-- + buf.length > 0) continue - res = buf.slice(0, 1).join('') - } - - token(res) - - start += res.length - content = content.slice(res.length) - return content.length - } while(1) - } - - function hex() { - if(/[^a-fA-F0-9]/.test(c)) { - token(content.join('')) - mode = NORMAL - return i - } - - content.push(c) - last = c - return i + 1 - } - - function integer() { - if(c === '.') { - content.push(c) - mode = FLOAT - last = c - return i + 1 - } - - if(/[eE]/.test(c)) { - content.push(c) - mode = FLOAT - last = c - return i + 1 - } - - if(c === 'x' && content.length === 1 && content[0] === '0') { - mode = HEX - content.push(c) - last = c - return i + 1 - } - - if(/[^\d]/.test(c)) { - token(content.join('')) - mode = NORMAL - return i - } - - content.push(c) - last = c - return i + 1 - } - - function decimal() { - if(c === 'f') { - content.push(c) - last = c - i += 1 - } - - if(/[eE]/.test(c)) { - content.push(c) - last = c - return i + 1 - } - - if (c === '-' && /[eE]/.test(last)) { - content.push(c) - last = c - return i + 1 - } - - if(/[^\d]/.test(c)) { - token(content.join('')) - mode = NORMAL - return i - } - - content.push(c) - last = c - return i + 1 - } - - function readtoken() { - if(/[^\d\w_]/.test(c)) { - var contentstr = content.join('') - if(allLiterals.indexOf(contentstr) > -1) { - mode = KEYWORD - } else if(allBuiltins.indexOf(contentstr) > -1) { - mode = BUILTIN - } else { - mode = IDENT - } - token(content.join('')) - mode = NORMAL - return i - } - content.push(c) - last = c - return i + 1 - } -} - -},{"./lib/builtins":314,"./lib/builtins-300es":313,"./lib/literals":316,"./lib/literals-300es":315,"./lib/operators":317}],313:[function(require,module,exports){ -// 300es builtins/reserved words that were previously valid in v100 -var v100 = require('./builtins') - -// The texture2D|Cube functions have been removed -// And the gl_ features are updated -v100 = v100.slice().filter(function (b) { - return !/^(gl\_|texture)/.test(b) -}) - -module.exports = v100.concat([ - // the updated gl_ constants - 'gl_VertexID' - , 'gl_InstanceID' - , 'gl_Position' - , 'gl_PointSize' - , 'gl_FragCoord' - , 'gl_FrontFacing' - , 'gl_FragDepth' - , 'gl_PointCoord' - , 'gl_MaxVertexAttribs' - , 'gl_MaxVertexUniformVectors' - , 'gl_MaxVertexOutputVectors' - , 'gl_MaxFragmentInputVectors' - , 'gl_MaxVertexTextureImageUnits' - , 'gl_MaxCombinedTextureImageUnits' - , 'gl_MaxTextureImageUnits' - , 'gl_MaxFragmentUniformVectors' - , 'gl_MaxDrawBuffers' - , 'gl_MinProgramTexelOffset' - , 'gl_MaxProgramTexelOffset' - , 'gl_DepthRangeParameters' - , 'gl_DepthRange' - - // other builtins - , 'trunc' - , 'round' - , 'roundEven' - , 'isnan' - , 'isinf' - , 'floatBitsToInt' - , 'floatBitsToUint' - , 'intBitsToFloat' - , 'uintBitsToFloat' - , 'packSnorm2x16' - , 'unpackSnorm2x16' - , 'packUnorm2x16' - , 'unpackUnorm2x16' - , 'packHalf2x16' - , 'unpackHalf2x16' - , 'outerProduct' - , 'transpose' - , 'determinant' - , 'inverse' - , 'texture' - , 'textureSize' - , 'textureProj' - , 'textureLod' - , 'textureOffset' - , 'texelFetch' - , 'texelFetchOffset' - , 'textureProjOffset' - , 'textureLodOffset' - , 'textureProjLod' - , 'textureProjLodOffset' - , 'textureGrad' - , 'textureGradOffset' - , 'textureProjGrad' - , 'textureProjGradOffset' -]) - -},{"./builtins":314}],314:[function(require,module,exports){ -module.exports = [ - // Keep this list sorted - 'abs' - , 'acos' - , 'all' - , 'any' - , 'asin' - , 'atan' - , 'ceil' - , 'clamp' - , 'cos' - , 'cross' - , 'dFdx' - , 'dFdy' - , 'degrees' - , 'distance' - , 'dot' - , 'equal' - , 'exp' - , 'exp2' - , 'faceforward' - , 'floor' - , 'fract' - , 'gl_BackColor' - , 'gl_BackLightModelProduct' - , 'gl_BackLightProduct' - , 'gl_BackMaterial' - , 'gl_BackSecondaryColor' - , 'gl_ClipPlane' - , 'gl_ClipVertex' - , 'gl_Color' - , 'gl_DepthRange' - , 'gl_DepthRangeParameters' - , 'gl_EyePlaneQ' - , 'gl_EyePlaneR' - , 'gl_EyePlaneS' - , 'gl_EyePlaneT' - , 'gl_Fog' - , 'gl_FogCoord' - , 'gl_FogFragCoord' - , 'gl_FogParameters' - , 'gl_FragColor' - , 'gl_FragCoord' - , 'gl_FragData' - , 'gl_FragDepth' - , 'gl_FragDepthEXT' - , 'gl_FrontColor' - , 'gl_FrontFacing' - , 'gl_FrontLightModelProduct' - , 'gl_FrontLightProduct' - , 'gl_FrontMaterial' - , 'gl_FrontSecondaryColor' - , 'gl_LightModel' - , 'gl_LightModelParameters' - , 'gl_LightModelProducts' - , 'gl_LightProducts' - , 'gl_LightSource' - , 'gl_LightSourceParameters' - , 'gl_MaterialParameters' - , 'gl_MaxClipPlanes' - , 'gl_MaxCombinedTextureImageUnits' - , 'gl_MaxDrawBuffers' - , 'gl_MaxFragmentUniformComponents' - , 'gl_MaxLights' - , 'gl_MaxTextureCoords' - , 'gl_MaxTextureImageUnits' - , 'gl_MaxTextureUnits' - , 'gl_MaxVaryingFloats' - , 'gl_MaxVertexAttribs' - , 'gl_MaxVertexTextureImageUnits' - , 'gl_MaxVertexUniformComponents' - , 'gl_ModelViewMatrix' - , 'gl_ModelViewMatrixInverse' - , 'gl_ModelViewMatrixInverseTranspose' - , 'gl_ModelViewMatrixTranspose' - , 'gl_ModelViewProjectionMatrix' - , 'gl_ModelViewProjectionMatrixInverse' - , 'gl_ModelViewProjectionMatrixInverseTranspose' - , 'gl_ModelViewProjectionMatrixTranspose' - , 'gl_MultiTexCoord0' - , 'gl_MultiTexCoord1' - , 'gl_MultiTexCoord2' - , 'gl_MultiTexCoord3' - , 'gl_MultiTexCoord4' - , 'gl_MultiTexCoord5' - , 'gl_MultiTexCoord6' - , 'gl_MultiTexCoord7' - , 'gl_Normal' - , 'gl_NormalMatrix' - , 'gl_NormalScale' - , 'gl_ObjectPlaneQ' - , 'gl_ObjectPlaneR' - , 'gl_ObjectPlaneS' - , 'gl_ObjectPlaneT' - , 'gl_Point' - , 'gl_PointCoord' - , 'gl_PointParameters' - , 'gl_PointSize' - , 'gl_Position' - , 'gl_ProjectionMatrix' - , 'gl_ProjectionMatrixInverse' - , 'gl_ProjectionMatrixInverseTranspose' - , 'gl_ProjectionMatrixTranspose' - , 'gl_SecondaryColor' - , 'gl_TexCoord' - , 'gl_TextureEnvColor' - , 'gl_TextureMatrix' - , 'gl_TextureMatrixInverse' - , 'gl_TextureMatrixInverseTranspose' - , 'gl_TextureMatrixTranspose' - , 'gl_Vertex' - , 'greaterThan' - , 'greaterThanEqual' - , 'inversesqrt' - , 'length' - , 'lessThan' - , 'lessThanEqual' - , 'log' - , 'log2' - , 'matrixCompMult' - , 'max' - , 'min' - , 'mix' - , 'mod' - , 'normalize' - , 'not' - , 'notEqual' - , 'pow' - , 'radians' - , 'reflect' - , 'refract' - , 'sign' - , 'sin' - , 'smoothstep' - , 'sqrt' - , 'step' - , 'tan' - , 'texture2D' - , 'texture2DLod' - , 'texture2DProj' - , 'texture2DProjLod' - , 'textureCube' - , 'textureCubeLod' - , 'texture2DLodEXT' - , 'texture2DProjLodEXT' - , 'textureCubeLodEXT' - , 'texture2DGradEXT' - , 'texture2DProjGradEXT' - , 'textureCubeGradEXT' -] - -},{}],315:[function(require,module,exports){ -var v100 = require('./literals') - -module.exports = v100.slice().concat([ - 'layout' - , 'centroid' - , 'smooth' - , 'case' - , 'mat2x2' - , 'mat2x3' - , 'mat2x4' - , 'mat3x2' - , 'mat3x3' - , 'mat3x4' - , 'mat4x2' - , 'mat4x3' - , 'mat4x4' - , 'uint' - , 'uvec2' - , 'uvec3' - , 'uvec4' - , 'samplerCubeShadow' - , 'sampler2DArray' - , 'sampler2DArrayShadow' - , 'isampler2D' - , 'isampler3D' - , 'isamplerCube' - , 'isampler2DArray' - , 'usampler2D' - , 'usampler3D' - , 'usamplerCube' - , 'usampler2DArray' - , 'coherent' - , 'restrict' - , 'readonly' - , 'writeonly' - , 'resource' - , 'atomic_uint' - , 'noperspective' - , 'patch' - , 'sample' - , 'subroutine' - , 'common' - , 'partition' - , 'active' - , 'filter' - , 'image1D' - , 'image2D' - , 'image3D' - , 'imageCube' - , 'iimage1D' - , 'iimage2D' - , 'iimage3D' - , 'iimageCube' - , 'uimage1D' - , 'uimage2D' - , 'uimage3D' - , 'uimageCube' - , 'image1DArray' - , 'image2DArray' - , 'iimage1DArray' - , 'iimage2DArray' - , 'uimage1DArray' - , 'uimage2DArray' - , 'image1DShadow' - , 'image2DShadow' - , 'image1DArrayShadow' - , 'image2DArrayShadow' - , 'imageBuffer' - , 'iimageBuffer' - , 'uimageBuffer' - , 'sampler1DArray' - , 'sampler1DArrayShadow' - , 'isampler1D' - , 'isampler1DArray' - , 'usampler1D' - , 'usampler1DArray' - , 'isampler2DRect' - , 'usampler2DRect' - , 'samplerBuffer' - , 'isamplerBuffer' - , 'usamplerBuffer' - , 'sampler2DMS' - , 'isampler2DMS' - , 'usampler2DMS' - , 'sampler2DMSArray' - , 'isampler2DMSArray' - , 'usampler2DMSArray' -]) - -},{"./literals":316}],316:[function(require,module,exports){ -module.exports = [ - // current - 'precision' - , 'highp' - , 'mediump' - , 'lowp' - , 'attribute' - , 'const' - , 'uniform' - , 'varying' - , 'break' - , 'continue' - , 'do' - , 'for' - , 'while' - , 'if' - , 'else' - , 'in' - , 'out' - , 'inout' - , 'float' - , 'int' - , 'void' - , 'bool' - , 'true' - , 'false' - , 'discard' - , 'return' - , 'mat2' - , 'mat3' - , 'mat4' - , 'vec2' - , 'vec3' - , 'vec4' - , 'ivec2' - , 'ivec3' - , 'ivec4' - , 'bvec2' - , 'bvec3' - , 'bvec4' - , 'sampler1D' - , 'sampler2D' - , 'sampler3D' - , 'samplerCube' - , 'sampler1DShadow' - , 'sampler2DShadow' - , 'struct' - - // future - , 'asm' - , 'class' - , 'union' - , 'enum' - , 'typedef' - , 'template' - , 'this' - , 'packed' - , 'goto' - , 'switch' - , 'default' - , 'inline' - , 'noinline' - , 'volatile' - , 'public' - , 'static' - , 'extern' - , 'external' - , 'interface' - , 'long' - , 'short' - , 'double' - , 'half' - , 'fixed' - , 'unsigned' - , 'input' - , 'output' - , 'hvec2' - , 'hvec3' - , 'hvec4' - , 'dvec2' - , 'dvec3' - , 'dvec4' - , 'fvec2' - , 'fvec3' - , 'fvec4' - , 'sampler2DRect' - , 'sampler3DRect' - , 'sampler2DRectShadow' - , 'sizeof' - , 'cast' - , 'namespace' - , 'using' -] - -},{}],317:[function(require,module,exports){ -module.exports = [ - '<<=' - , '>>=' - , '++' - , '--' - , '<<' - , '>>' - , '<=' - , '>=' - , '==' - , '!=' - , '&&' - , '||' - , '+=' - , '-=' - , '*=' - , '/=' - , '%=' - , '&=' - , '^^' - , '^=' - , '|=' - , '(' - , ')' - , '[' - , ']' - , '.' - , '!' - , '~' - , '*' - , '/' - , '%' - , '+' - , '-' - , '<' - , '>' - , '&' - , '^' - , '|' - , '?' - , ':' - , '=' - , ',' - , ';' - , '{' - , '}' -] - -},{}],318:[function(require,module,exports){ -var tokenize = require('./index') - -module.exports = tokenizeString - -function tokenizeString(str, opt) { - var generator = tokenize(opt) - var tokens = [] - - tokens = tokens.concat(generator(str)) - tokens = tokens.concat(generator(null)) - - return tokens -} - -},{"./index":312}],319:[function(require,module,exports){ -(function(window) { - var re = { - not_string: /[^s]/, - number: /[diefg]/, - json: /[j]/, - not_json: /[^j]/, - text: /^[^\x25]+/, - modulo: /^\x25{2}/, - placeholder: /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijosuxX])/, - key: /^([a-z_][a-z_\d]*)/i, - key_access: /^\.([a-z_][a-z_\d]*)/i, - index_access: /^\[(\d+)\]/, - sign: /^[\+\-]/ - } - - function sprintf() { - var key = arguments[0], cache = sprintf.cache - if (!(cache[key] && cache.hasOwnProperty(key))) { - cache[key] = sprintf.parse(key) - } - return sprintf.format.call(null, cache[key], arguments) - } - - sprintf.format = function(parse_tree, argv) { - var cursor = 1, tree_length = parse_tree.length, node_type = "", arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = "" - for (i = 0; i < tree_length; i++) { - node_type = get_type(parse_tree[i]) - if (node_type === "string") { - output[output.length] = parse_tree[i] - } - else if (node_type === "array") { - match = parse_tree[i] // convenience purposes only - if (match[2]) { // keyword argument - arg = argv[cursor] - for (k = 0; k < match[2].length; k++) { - if (!arg.hasOwnProperty(match[2][k])) { - throw new Error(sprintf("[sprintf] property '%s' does not exist", match[2][k])) - } - arg = arg[match[2][k]] - } - } - else if (match[1]) { // positional argument (explicit) - arg = argv[match[1]] - } - else { // positional argument (implicit) - arg = argv[cursor++] - } - - if (get_type(arg) == "function") { - arg = arg() - } - - if (re.not_string.test(match[8]) && re.not_json.test(match[8]) && (get_type(arg) != "number" && isNaN(arg))) { - throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg))) - } - - if (re.number.test(match[8])) { - is_positive = arg >= 0 - } - - switch (match[8]) { - case "b": - arg = arg.toString(2) - break - case "c": - arg = String.fromCharCode(arg) - break - case "d": - case "i": - arg = parseInt(arg, 10) - break - case "j": - arg = JSON.stringify(arg, null, match[6] ? parseInt(match[6]) : 0) - break - case "e": - arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential() - break - case "f": - arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg) - break - case "g": - arg = match[7] ? parseFloat(arg).toPrecision(match[7]) : parseFloat(arg) - break - case "o": - arg = arg.toString(8) - break - case "s": - arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg) - break - case "u": - arg = arg >>> 0 - break - case "x": - arg = arg.toString(16) - break - case "X": - arg = arg.toString(16).toUpperCase() - break - } - if (re.json.test(match[8])) { - output[output.length] = arg - } - else { - if (re.number.test(match[8]) && (!is_positive || match[3])) { - sign = is_positive ? "+" : "-" - arg = arg.toString().replace(re.sign, "") - } - else { - sign = "" - } - pad_character = match[4] ? match[4] === "0" ? "0" : match[4].charAt(1) : " " - pad_length = match[6] - (sign + arg).length - pad = match[6] ? (pad_length > 0 ? str_repeat(pad_character, pad_length) : "") : "" - output[output.length] = match[5] ? sign + arg + pad : (pad_character === "0" ? sign + pad + arg : pad + sign + arg) - } - } - } - return output.join("") - } - - sprintf.cache = {} - - sprintf.parse = function(fmt) { - var _fmt = fmt, match = [], parse_tree = [], arg_names = 0 - while (_fmt) { - if ((match = re.text.exec(_fmt)) !== null) { - parse_tree[parse_tree.length] = match[0] - } - else if ((match = re.modulo.exec(_fmt)) !== null) { - parse_tree[parse_tree.length] = "%" - } - else if ((match = re.placeholder.exec(_fmt)) !== null) { - if (match[2]) { - arg_names |= 1 - var field_list = [], replacement_field = match[2], field_match = [] - if ((field_match = re.key.exec(replacement_field)) !== null) { - field_list[field_list.length] = field_match[1] - while ((replacement_field = replacement_field.substring(field_match[0].length)) !== "") { - if ((field_match = re.key_access.exec(replacement_field)) !== null) { - field_list[field_list.length] = field_match[1] - } - else if ((field_match = re.index_access.exec(replacement_field)) !== null) { - field_list[field_list.length] = field_match[1] - } - else { - throw new SyntaxError("[sprintf] failed to parse named argument key") - } - } - } - else { - throw new SyntaxError("[sprintf] failed to parse named argument key") - } - match[2] = field_list - } - else { - arg_names |= 2 - } - if (arg_names === 3) { - throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported") - } - parse_tree[parse_tree.length] = match - } - else { - throw new SyntaxError("[sprintf] unexpected placeholder") - } - _fmt = _fmt.substring(match[0].length) - } - return parse_tree - } - - var vsprintf = function(fmt, argv, _argv) { - _argv = (argv || []).slice(0) - _argv.splice(0, 0, fmt) - return sprintf.apply(null, _argv) - } - - /** - * helpers - */ - function get_type(variable) { - return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase() - } - - function str_repeat(input, multiplier) { - return Array(multiplier + 1).join(input) - } - - /** - * export to either browser or node.js - */ - if (typeof exports !== "undefined") { - exports.sprintf = sprintf - exports.vsprintf = vsprintf - } - else { - window.sprintf = sprintf - window.vsprintf = vsprintf - - if (typeof define === "function" && define.amd) { - define(function() { - return { - sprintf: sprintf, - vsprintf: vsprintf - } - }) +module.exports = function (output) { + for (var i = 1; i < arguments.length; i++) { + var input = arguments[i]; + for (var k in input) { + output[k] = input[k]; } } -})(typeof window === "undefined" ? this : window); - -},{}],320:[function(require,module,exports){ -var hiddenStore = require('./hidden-store.js'); - -module.exports = createStore; - -function createStore() { - var key = {}; - - return function (obj) { - if ((typeof obj !== 'object' || obj === null) && - typeof obj !== 'function' - ) { - throw new Error('Weakmap-shim: Key must be object') - } - - var store = obj.valueOf(key); - return store && store.identity === key ? - store : hiddenStore(obj, key); - }; -} - -},{"./hidden-store.js":321}],321:[function(require,module,exports){ -module.exports = hiddenStore; - -function hiddenStore(obj, key) { - var store = { identity: key }; - var valueOf = obj.valueOf; - - Object.defineProperty(obj, "valueOf", { - value: function (value) { - return value !== key ? - valueOf.apply(this, arguments) : store; - }, - writable: true - }); - - return store; -} - -},{}],322:[function(require,module,exports){ -// Original - @Gozola. -// https://gist.github.com/Gozala/1269991 -// This is a reimplemented version (with a few bug fixes). - -var createStore = require('./create-store.js'); - -module.exports = weakMap; - -function weakMap() { - var privates = createStore(); - - return { - 'get': function (key, fallback) { - var store = privates(key) - return store.hasOwnProperty('value') ? - store.value : fallback - }, - 'set': function (key, value) { - privates(key).value = value; - return this; - }, - 'has': function(key) { - return 'value' in privates(key); - }, - 'delete': function (key) { - return delete privates(key).value; - } - } -} - -},{"./create-store.js":320}],323:[function(require,module,exports){ -"use strict" - -module.exports = createText - -var vectorizeText = require("./lib/vtext") -var defaultCanvas = null -var defaultContext = null - -if(typeof document !== 'undefined') { - defaultCanvas = document.createElement('canvas') - defaultCanvas.width = 8192 - defaultCanvas.height = 1024 - defaultContext = defaultCanvas.getContext("2d") -} - -function createText(str, options) { - if((typeof options !== "object") || (options === null)) { - options = {} - } - return vectorizeText( - str, - options.canvas || defaultCanvas, - options.context || defaultContext, - options) -} - -},{"./lib/vtext":324}],324:[function(require,module,exports){ -"use strict" - -module.exports = vectorizeText -module.exports.processPixels = processPixels - -var surfaceNets = require('surface-nets') -var ndarray = require('ndarray') -var simplify = require('simplify-planar-graph') -var cleanPSLG = require('clean-pslg') -var cdt2d = require('cdt2d') -var toPolygonCrappy = require('planar-graph-to-polyline') - -function transformPositions(positions, options, size) { - var align = options.textAlign || "start" - var baseline = options.textBaseline || "alphabetic" - - var lo = [1<<30, 1<<30] - var hi = [0,0] - var n = positions.length - for(var i=0; i 8192) { - throw new Error("vectorize-text: String too long (sorry, this will get fixed later)") - } - var height = 3 * size - if(canvas.height < height) { - canvas.height = height - } - - context.fillStyle = "#000" - context.fillRect(0, 0, canvas.width, canvas.height) - - context.fillStyle = "#fff" - context.fillText(str, size, 2*size) - - //Cut pixels from image - var pixelData = context.getImageData(0, 0, width, height) - var pixels = ndarray(pixelData.data, [height, width, 4]) - - return pixels.pick(-1,-1,0).transpose(1,0) -} - -function getContour(pixels, doSimplify) { - var contour = surfaceNets(pixels, 128) - if(doSimplify) { - return simplify(contour.cells, contour.positions, 0.25) - } - return { - edges: contour.cells, - positions: contour.positions - } -} - -function processPixelsImpl(pixels, options, size, simplify) { - //Extract contour - var contour = getContour(pixels, simplify) - - //Apply warp to positions - var positions = transformPositions(contour.positions, options, size) - var edges = contour.edges - var flip = "ccw" === options.orientation - - //Clean up the PSLG, resolve self intersections, etc. - cleanPSLG(positions, edges) - - //If triangulate flag passed, triangulate the result - if(options.polygons || options.polygon || options.polyline) { - var result = toPolygonCrappy(edges, positions) - var nresult = new Array(result.length) - for(var i=0; i 0) { - var b = stack.pop() - var a = stack.pop() - - //Find opposite pairs - var x = -1, y = -1 - var star = stars[a] - for(var i=1; i= 0) { - continue - } - - //Flip the edge - triangulation.flip(a, b) - - //Test flipping neighboring edges - testFlip(points, triangulation, stack, x, a, y) - testFlip(points, triangulation, stack, a, y, x) - testFlip(points, triangulation, stack, y, b, x) - testFlip(points, triangulation, stack, b, x, y) - } -} - -},{"binary-search-bounds":281,"robust-in-sphere":330}],327:[function(require,module,exports){ -'use strict' - -var bsearch = require('binary-search-bounds') - -module.exports = classifyFaces - -function FaceIndex(cells, neighbor, constraint, flags, active, next, boundary) { - this.cells = cells - this.neighbor = neighbor - this.flags = flags - this.constraint = constraint - this.active = active - this.next = next - this.boundary = boundary -} - -var proto = FaceIndex.prototype - -function compareCell(a, b) { - return a[0] - b[0] || - a[1] - b[1] || - a[2] - b[2] -} - -proto.locate = (function() { - var key = [0,0,0] - return function(a, b, c) { - var x = a, y = b, z = c - if(b < c) { - if(b < a) { - x = b - y = c - z = a - } - } else if(c < a) { - x = c - y = a - z = b - } - if(x < 0) { - return -1 - } - key[0] = x - key[1] = y - key[2] = z - return bsearch.eq(this.cells, key, compareCell) - } -})() - -function indexCells(triangulation, infinity) { - //First get cells and canonicalize - var cells = triangulation.cells() - var nc = cells.length - for(var i=0; i 0 || next.length > 0) { - while(active.length > 0) { - var t = active.pop() - if(flags[t] === -side) { - continue - } - flags[t] = side - var c = cells[t] - for(var j=0; j<3; ++j) { - var f = neighbor[3*t+j] - if(f >= 0 && flags[f] === 0) { - if(constraint[3*t+j]) { - next.push(f) - } else { - active.push(f) - flags[f] = side - } - } - } - } - - //Swap arrays and loop - var tmp = next - next = active - active = tmp - next.length = 0 - side = -side - } - - var result = filterCells(cells, flags, target) - if(infinity) { - return result.concat(index.boundary) - } - return result -} - -},{"binary-search-bounds":281}],328:[function(require,module,exports){ -'use strict' - -var bsearch = require('binary-search-bounds') -var orient = require('robust-orientation')[3] - -var EVENT_POINT = 0 -var EVENT_END = 1 -var EVENT_START = 2 - -module.exports = monotoneTriangulate - -//A partial convex hull fragment, made of two unimonotone polygons -function PartialHull(a, b, idx, lowerIds, upperIds) { - this.a = a - this.b = b - this.idx = idx - this.lowerIds = lowerIds - this.upperIds = upperIds -} - -//An event in the sweep line procedure -function Event(a, b, type, idx) { - this.a = a - this.b = b - this.type = type - this.idx = idx -} - -//This is used to compare events for the sweep line procedure -// Points are: -// 1. sorted lexicographically -// 2. sorted by type (point < end < start) -// 3. segments sorted by winding order -// 4. sorted by index -function compareEvent(a, b) { - var d = - (a.a[0] - b.a[0]) || - (a.a[1] - b.a[1]) || - (a.type - b.type) - if(d) { return d } - if(a.type !== EVENT_POINT) { - d = orient(a.a, a.b, b.b) - if(d) { return d } - } - return a.idx - b.idx -} - -function testPoint(hull, p) { - return orient(hull.a, hull.b, p) -} - -function addPoint(cells, hulls, points, p, idx) { - var lo = bsearch.lt(hulls, p, testPoint) - var hi = bsearch.gt(hulls, p, testPoint) - for(var i=lo; i 1 && orient( - points[lowerIds[m-2]], - points[lowerIds[m-1]], - p) > 0) { - cells.push( - [lowerIds[m-1], - lowerIds[m-2], - idx]) - m -= 1 - } - lowerIds.length = m - lowerIds.push(idx) - - //Insert p into upper hull - var upperIds = hull.upperIds - var m = upperIds.length - while(m > 1 && orient( - points[upperIds[m-2]], - points[upperIds[m-1]], - p) < 0) { - cells.push( - [upperIds[m-2], - upperIds[m-1], - idx]) - m -= 1 - } - upperIds.length = m - upperIds.push(idx) - } -} - -function findSplit(hull, edge) { - var d - if(hull.a[0] < edge.a[0]) { - d = orient(hull.a, hull.b, edge.a) - } else { - d = orient(edge.b, edge.a, hull.a) - } - if(d) { return d } - if(edge.b[0] < hull.b[0]) { - d = orient(hull.a, hull.b, edge.b) - } else { - d = orient(edge.b, edge.a, hull.b) - } - return d || hull.idx - edge.idx -} - -function splitHulls(hulls, points, event) { - var splitIdx = bsearch.le(hulls, event, findSplit) - var hull = hulls[splitIdx] - var upperIds = hull.upperIds - var x = upperIds[upperIds.length-1] - hull.upperIds = [x] - hulls.splice(splitIdx+1, 0, - new PartialHull(event.a, event.b, event.idx, [x], upperIds)) -} - - -function mergeHulls(hulls, points, event) { - //Swap pointers for merge search - var tmp = event.a - event.a = event.b - event.b = tmp - var mergeIdx = bsearch.eq(hulls, event, findSplit) - var upper = hulls[mergeIdx] - var lower = hulls[mergeIdx-1] - lower.upperIds = upper.upperIds - hulls.splice(mergeIdx, 1) -} - - -function monotoneTriangulate(points, edges) { - - var numPoints = points.length - var numEdges = edges.length - - var events = [] - - //Create point events - for(var i=0; i b[0]) { - events.push( - new Event(b, a, EVENT_START, i), - new Event(a, b, EVENT_END, i)) - } - } - - //Sort events - events.sort(compareEvent) - - //Initialize hull - var minX = events[0].a[0] - (1 + Math.abs(events[0].a[0])) * Math.pow(2, -52) - var hull = [ new PartialHull([minX, 1], [minX, 0], -1, [], [], [], []) ] - - //Process events in order - var cells = [] - for(var i=0, numEvents=events.length; i= 0 - } -})() - -proto.removeTriangle = function(i, j, k) { - var stars = this.stars - removePair(stars[i], j, k) - removePair(stars[j], k, i) - removePair(stars[k], i, j) -} - -proto.addTriangle = function(i, j, k) { - var stars = this.stars - stars[i].push(j, k) - stars[j].push(k, i) - stars[k].push(i, j) -} - -proto.opposite = function(j, i) { - var list = this.stars[i] - for(var k=1, n=list.length; k>1 - return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") - } -} - -function makeProduct(a, b) { - if(a.charAt(0) === "m") { - if(b.charAt(0) === "w") { - var toks = a.split("[") - return ["w", b.substr(1), "m", toks[0].substr(1)].join("") - } else { - return ["prod(", a, ",", b, ")"].join("") - } - } else { - return makeProduct(b, a) - } -} - -function sign(s) { - if(s & 1 !== 0) { - return "-" - } - return "" -} - -function determinant(m) { - if(m.length === 2) { - return [["diff(", makeProduct(m[0][0], m[1][1]), ",", makeProduct(m[1][0], m[0][1]), ")"].join("")] - } else { - var expr = [] - for(var i=0; i= nf)) { - a = ei - eptr += 1 - if(eptr < ne) { - ei = e[eptr] - ea = abs(ei) - } - } else { - a = fi - fptr += 1 - if(fptr < nf) { - fi = -f[fptr] - fa = abs(fi) - } - } - var x = a + b - var bv = x - a - var y = b - bv - var q0 = y - var q1 = x - var _x, _bv, _av, _br, _ar - while(eptr < ne && fptr < nf) { - if(ea < fa) { - a = ei - eptr += 1 - if(eptr < ne) { - ei = e[eptr] - ea = abs(ei) - } - } else { - a = fi - fptr += 1 - if(fptr < nf) { - fi = -f[fptr] - fa = abs(fi) - } - } - b = q0 - x = a + b - bv = x - a - y = b - bv - if(y) { - g[count++] = y - } - _x = q1 + x - _bv = _x - q1 - _av = _x - _bv - _br = x - _bv - _ar = q1 - _av - q0 = _ar + _br - q1 = _x - } - while(eptr < ne) { - a = ei - b = q0 - x = a + b - bv = x - a - y = b - bv - if(y) { - g[count++] = y - } - _x = q1 + x - _bv = _x - q1 - _av = _x - _bv - _br = x - _bv - _ar = q1 - _av - q0 = _ar + _br - q1 = _x - eptr += 1 - if(eptr < ne) { - ei = e[eptr] - } - } - while(fptr < nf) { - a = fi - b = q0 - x = a + b - bv = x - a - y = b - bv - if(y) { - g[count++] = y - } - _x = q1 + x - _bv = _x - q1 - _av = _x - _bv - _br = x - _bv - _ar = q1 - _av - q0 = _ar + _br - q1 = _x - fptr += 1 - if(fptr < nf) { - fi = -f[fptr] - } - } - if(q0) { - g[count++] = q0 - } - if(q1) { - g[count++] = q1 - } - if(!count) { - g[count++] = 0.0 - } - g.length = count - return g -} -},{}],334:[function(require,module,exports){ -arguments[4][152][0].apply(exports,arguments) -},{"dup":152}],335:[function(require,module,exports){ -arguments[4][153][0].apply(exports,arguments) -},{"dup":153}],336:[function(require,module,exports){ -'use strict' - -module.exports = cleanPSLG - -var UnionFind = require('union-find') -var boxIntersect = require('box-intersect') -var compareCell = require('compare-cell') -var segseg = require('robust-segment-intersect') -var rat = require('big-rat') -var ratCmp = require('big-rat/cmp') -var ratToFloat = require('big-rat/to-float') -var ratVec = require('rat-vec') -var nextafter = require('nextafter') - -var solveIntersection = require('./lib/rat-seg-intersect') - -//Bounds on a rational number when rounded to a float -function boundRat(r) { - var f = ratToFloat(r) - var cmp = ratCmp(rat(f), r) - if(cmp < 0) { - return [f, nextafter(f, Infinity)] - } else if(cmp > 0) { - return [nextafter(f, -Infinity), f] - } else { - return [f, f] - } -} - -//Convert a list of edges in a pslg to bounding boxes -function boundEdges(points, edges) { - var bounds = new Array(edges.length) - for(var i=0; i= floatPoints.length) { - return ratPoints[idx-floatPoints.length] - } - var p = floatPoints[idx] - return [ rat(p[0]), rat(p[1]) ] - } - junctions.sort(function(a, b) { - if(a[0] !== b[0]) { - return a[0] - b[0] - } - var u = getPoint(a[1]) - var v = getPoint(b[1]) - return ratCmp(u[0], v[0]) || ratCmp(u[1], v[1]) - }) - - //Split edges along junctions - for(var i=junctions.length-1; i>=0; --i) { - var junction = junctions[i] - var e = junction[0] - - var edge = edges[e] - var s = edge[0] - var t = edge[1] - - //Check if edge is not lexicographically sorted - var a = floatPoints[s] - var b = floatPoints[t] - if(((a[0] - b[0]) || (a[1] - b[1])) < 0) { - var tmp = s - s = t - t = tmp - } - - //Split leading edge - edge[0] = s - var last = edge[1] = junction[1] - - //If we are grouping edges by color, remember to track data - var color - if(useColor) { - color = edge[2] - } - - //Split other edges - while(i > 0 && junctions[i-1][0] === e) { - var junction = junctions[--i] - var next = junction[1] - if(useColor) { - edges.push([last, next, color]) - } else { - edges.push([last, next]) - } - last = next - } - - //Add final edge - if(useColor) { - edges.push([last, t, color]) - } else { - edges.push([last, t]) - } - } - - //Return constructed rational points - return ratPoints -} - -//Merge overlapping points -function dedupPoints(floatPoints, ratPoints, floatBounds) { - var numPoints = floatPoints.length + ratPoints.length - var uf = new UnionFind(numPoints) - - //Compute rational bounds - var bounds = floatBounds - for(var i=0; i b[2]) { - return 1 - } - return 0 -} - -//Remove duplicate edge labels -function dedupEdges(edges, labels, useColor) { - if(edges.length === 0) { - return - } - if(labels) { - for(var i=0; i 0 || tjunctions.length > 0) - } - - // More iterations necessary - return true -} - -//Main loop, runs PSLG clean up until completion -function cleanPSLG(points, edges, colors) { - var modified = false - - //If using colors, augment edges with color data - var prevEdges - if(colors) { - prevEdges = edges - var augEdges = new Array(edges.length) - for(var i=0; i 0) { - a = a.shln(shift) - } else if(shift < 0) { - b = b.shln(-shift) - } - return rationalize(a, b) -} - -},{"./div":340,"./is-rat":342,"./lib/is-bn":346,"./lib/num-to-bn":347,"./lib/rationalize":348,"./lib/str-to-bn":349}],342:[function(require,module,exports){ -'use strict' - -var isBN = require('./lib/is-bn') - -module.exports = isRat - -function isRat(x) { - return Array.isArray(x) && x.length === 2 && isBN(x[0]) && isBN(x[1]) -} - -},{"./lib/is-bn":346}],343:[function(require,module,exports){ -'use strict' - -var bn = require('bn.js') - -module.exports = sign - -function sign(x) { - return x.cmp(new bn(0)) -} - -},{"bn.js":352}],344:[function(require,module,exports){ -'use strict' - -module.exports = bn2num - -//TODO: Make this better -function bn2num(b) { - var l = b.length - var words = b.words - var out = 0 - if (l === 1) { - out = words[0] - } else if (l === 2) { - out = words[0] + (words[1] * 0x4000000) - } else { - var out = 0 - for (var i = 0; i < l; i++) { - var w = words[i] - out += w * Math.pow(0x4000000, i) - } - } - return b.sign ? -out : out -} - -},{}],345:[function(require,module,exports){ -'use strict' - -var db = require('double-bits') -var ctz = require('bit-twiddle').countTrailingZeros - -module.exports = ctzNumber - -//Counts the number of trailing zeros -function ctzNumber(x) { - var l = ctz(db.lo(x)) - if(l < 32) { - return l - } - var h = ctz(db.hi(x)) - if(h > 20) { - return 52 - } - return h + 32 -} - -},{"bit-twiddle":351,"double-bits":353}],346:[function(require,module,exports){ -'use strict' - -var BN = require('bn.js') - -module.exports = isBN - -//Test if x is a bignumber -//FIXME: obviously this is the wrong way to do it -function isBN(x) { - return x && typeof x === 'object' && Boolean(x.words) -} - -},{"bn.js":352}],347:[function(require,module,exports){ -'use strict' - -var BN = require('bn.js') -var db = require('double-bits') - -module.exports = num2bn - -function num2bn(x) { - var e = db.exponent(x) - if(e < 52) { - return new BN(x) - } else { - return (new BN(x * Math.pow(2, 52-e))).shln(e-52) - } -} - -},{"bn.js":352,"double-bits":353}],348:[function(require,module,exports){ -'use strict' - -var num2bn = require('./num-to-bn') -var sign = require('./bn-sign') - -module.exports = rationalize - -function rationalize(numer, denom) { - var snumer = sign(numer) - var sdenom = sign(denom) - if(snumer === 0) { - return [num2bn(0), num2bn(1)] - } - if(sdenom === 0) { - return [num2bn(0), num2bn(0)] - } - if(sdenom < 0) { - numer = numer.neg() - denom = denom.neg() - } - var d = numer.gcd(denom) - if(d.cmpn(1)) { - return [ numer.div(d), denom.div(d) ] - } - return [ numer, denom ] -} - -},{"./bn-sign":343,"./num-to-bn":347}],349:[function(require,module,exports){ -'use strict' - -var BN = require('bn.js') - -module.exports = str2BN - -function str2BN(x) { - return new BN(x) -} - -},{"bn.js":352}],350:[function(require,module,exports){ -'use strict' - -var rationalize = require('./lib/rationalize') - -module.exports = mul - -function mul(a, b) { - return rationalize(a[0].mul(b[0]), a[1].mul(b[1])) -} - -},{"./lib/rationalize":348}],351:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],352:[function(require,module,exports){ -(function (module, exports) { + return output; +}; +},{}],282:[function(require,module,exports){ 'use strict'; -// Utils - -function assert(val, msg) { - if (!val) - throw new Error(msg || 'Assertion failed'); -} - -// Could use `inherits` module, but don't want to move from single file -// architecture yet. -function inherits(ctor, superCtor) { - ctor.super_ = superCtor; - var TempCtor = function () {}; - TempCtor.prototype = superCtor.prototype; - ctor.prototype = new TempCtor(); - ctor.prototype.constructor = ctor; -} - -// BN - -function BN(number, base, endian) { - // May be `new BN(bn)` ? - if (number !== null && - typeof number === 'object' && - Array.isArray(number.words)) { - return number; - } - - this.sign = false; - this.words = null; - this.length = 0; - - // Reduction context - this.red = null; - - if (base === 'le' || base === 'be') { - endian = base; - base = 10; - } - - if (number !== null) - this._init(number || 0, base || 10, endian || 'be'); -} -if (typeof module === 'object') - module.exports = BN; -else - exports.BN = BN; - -BN.BN = BN; -BN.wordSize = 26; - -BN.prototype._init = function init(number, base, endian) { - if (typeof number === 'number') { - return this._initNumber(number, base, endian); - } else if (typeof number === 'object') { - return this._initArray(number, base, endian); - } - if (base === 'hex') - base = 16; - assert(base === (base | 0) && base >= 2 && base <= 36); - - number = number.toString().replace(/\s+/g, ''); - var start = 0; - if (number[0] === '-') - start++; - - if (base === 16) - this._parseHex(number, start); - else - this._parseBase(number, base, start); - - if (number[0] === '-') - this.sign = true; - - this.strip(); - - if (endian !== 'le') - return; - - this._initArray(this.toArray(), base, endian); -}; - -BN.prototype._initNumber = function _initNumber(number, base, endian) { - if (number < 0) { - this.sign = true; - number = -number; - } - if (number < 0x4000000) { - this.words = [ number & 0x3ffffff ]; - this.length = 1; - } else if (number < 0x10000000000000) { - this.words = [ - number & 0x3ffffff, - (number / 0x4000000) & 0x3ffffff - ]; - this.length = 2; - } else { - assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) - this.words = [ - number & 0x3ffffff, - (number / 0x4000000) & 0x3ffffff, - 1 - ]; - this.length = 3; - } - - if (endian !== 'le') - return; - - // Reverse the bytes - this._initArray(this.toArray(), base, endian); -}; - -BN.prototype._initArray = function _initArray(number, base, endian) { - // Perhaps a Uint8Array - assert(typeof number.length === 'number'); - if (number.length <= 0) { - this.words = [ 0 ]; - this.length = 1; - return this; - } - - this.length = Math.ceil(number.length / 3); - this.words = new Array(this.length); - for (var i = 0; i < this.length; i++) - this.words[i] = 0; - - var off = 0; - if (endian === 'be') { - for (var i = number.length - 1, j = 0; i >= 0; i -= 3) { - var w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); - this.words[j] |= (w << off) & 0x3ffffff; - this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - } else if (endian === 'le') { - for (var i = 0, j = 0; i < number.length; i += 3) { - var w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); - this.words[j] |= (w << off) & 0x3ffffff; - this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - } - return this.strip(); -}; - -function parseHex(str, start, end) { - var r = 0; - var len = Math.min(str.length, end); - for (var i = start; i < len; i++) { - var c = str.charCodeAt(i) - 48; - - r <<= 4; - - // 'a' - 'f' - if (c >= 49 && c <= 54) - r |= c - 49 + 0xa; - - // 'A' - 'F' - else if (c >= 17 && c <= 22) - r |= c - 17 + 0xa; - - // '0' - '9' - else - r |= c & 0xf; - } - return r; -} - -BN.prototype._parseHex = function _parseHex(number, start) { - // Create possibly bigger array to ensure that it fits the number - this.length = Math.ceil((number.length - start) / 6); - this.words = new Array(this.length); - for (var i = 0; i < this.length; i++) - this.words[i] = 0; - - // Scan 24-bit chunks and add them to the number - var off = 0; - for (var i = number.length - 6, j = 0; i >= start; i -= 6) { - var w = parseHex(number, i, i + 6); - this.words[j] |= (w << off) & 0x3ffffff; - this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; - off += 24; - if (off >= 26) { - off -= 26; - j++; - } - } - if (i + 6 !== start) { - var w = parseHex(number, start, i + 6); - this.words[j] |= (w << off) & 0x3ffffff; - this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; - } - this.strip(); -}; - -function parseBase(str, start, end, mul) { - var r = 0; - var len = Math.min(str.length, end); - for (var i = start; i < len; i++) { - var c = str.charCodeAt(i) - 48; - - r *= mul; - - // 'a' - if (c >= 49) - r += c - 49 + 0xa; - - // 'A' - else if (c >= 17) - r += c - 17 + 0xa; - - // '0' - '9' - else - r += c; - } - return r; -} - -BN.prototype._parseBase = function _parseBase(number, base, start) { - // Initialize as zero - this.words = [ 0 ]; - this.length = 1; - - // Find length of limb in base - for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) - limbLen++; - limbLen--; - limbPow = (limbPow / base) | 0; - - var total = number.length - start; - var mod = total % limbLen; - var end = Math.min(total, total - mod) + start; - - var word = 0; - for (var i = start; i < end; i += limbLen) { - word = parseBase(number, i, i + limbLen, base); - - this.imuln(limbPow); - if (this.words[0] + word < 0x4000000) - this.words[0] += word; - else - this._iaddn(word); - } - - if (mod !== 0) { - var pow = 1; - var word = parseBase(number, i, number.length, base); - - for (var i = 0; i < mod; i++) - pow *= base; - this.imuln(pow); - if (this.words[0] + word < 0x4000000) - this.words[0] += word; - else - this._iaddn(word); - } -}; - -BN.prototype.copy = function copy(dest) { - dest.words = new Array(this.length); - for (var i = 0; i < this.length; i++) - dest.words[i] = this.words[i]; - dest.length = this.length; - dest.sign = this.sign; - dest.red = this.red; -}; - -BN.prototype.clone = function clone() { - var r = new BN(null); - this.copy(r); - return r; -}; - -// Remove leading `0` from `this` -BN.prototype.strip = function strip() { - while (this.length > 1 && this.words[this.length - 1] === 0) - this.length--; - return this._normSign(); -}; - -BN.prototype._normSign = function _normSign() { - // -0 = 0 - if (this.length === 1 && this.words[0] === 0) - this.sign = false; - return this; -}; - -BN.prototype.inspect = function inspect() { - return (this.red ? ''; -}; - -/* - -var zeros = []; -var groupSizes = []; -var groupBases = []; - -var s = ''; -var i = -1; -while (++i < BN.wordSize) { - zeros[i] = s; - s += '0'; -} -groupSizes[0] = 0; -groupSizes[1] = 0; -groupBases[0] = 0; -groupBases[1] = 0; -var base = 2 - 1; -while (++base < 36 + 1) { - var groupSize = 0; - var groupBase = 1; - while (groupBase < (1 << BN.wordSize) / base) { - groupBase *= base; - groupSize += 1; - } - groupSizes[base] = groupSize; - groupBases[base] = groupBase; -} - -*/ - -var zeros = [ - '', - '0', - '00', - '000', - '0000', - '00000', - '000000', - '0000000', - '00000000', - '000000000', - '0000000000', - '00000000000', - '000000000000', - '0000000000000', - '00000000000000', - '000000000000000', - '0000000000000000', - '00000000000000000', - '000000000000000000', - '0000000000000000000', - '00000000000000000000', - '000000000000000000000', - '0000000000000000000000', - '00000000000000000000000', - '000000000000000000000000', - '0000000000000000000000000' -]; - -var groupSizes = [ - 0, 0, - 25, 16, 12, 11, 10, 9, 8, - 8, 7, 7, 7, 7, 6, 6, - 6, 6, 6, 6, 6, 5, 5, - 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5 -]; - -var groupBases = [ - 0, 0, - 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, - 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, - 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, - 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, - 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 -]; - -BN.prototype.toString = function toString(base, padding) { - base = base || 10; - if (base === 16 || base === 'hex') { - var out = ''; - var off = 0; - var padding = padding | 0 || 1; - var carry = 0; - for (var i = 0; i < this.length; i++) { - var w = this.words[i]; - var word = (((w << off) | carry) & 0xffffff).toString(16); - carry = (w >>> (24 - off)) & 0xffffff; - if (carry !== 0 || i !== this.length - 1) - out = zeros[6 - word.length] + word + out; - else - out = word + out; - off += 2; - if (off >= 26) { - off -= 26; - i--; - } - } - if (carry !== 0) - out = carry.toString(16) + out; - while (out.length % padding !== 0) - out = '0' + out; - if (this.sign) - out = '-' + out; - return out; - } else if (base === (base | 0) && base >= 2 && base <= 36) { - // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); - var groupSize = groupSizes[base]; - // var groupBase = Math.pow(base, groupSize); - var groupBase = groupBases[base]; - var out = ''; - var c = this.clone(); - c.sign = false; - while (c.cmpn(0) !== 0) { - var r = c.modn(groupBase).toString(base); - c = c.idivn(groupBase); - - if (c.cmpn(0) !== 0) - out = zeros[groupSize - r.length] + r + out; - else - out = r + out; - } - if (this.cmpn(0) === 0) - out = '0' + out; - if (this.sign) - out = '-' + out; - return out; - } else { - assert(false, 'Base should be between 2 and 36'); - } -}; - -BN.prototype.toJSON = function toJSON() { - return this.toString(16); -}; - -BN.prototype.toArray = function toArray(endian) { - this.strip(); - var res = new Array(this.byteLength()); - res[0] = 0; - - var q = this.clone(); - if (endian !== 'le') { - // Assume big-endian - for (var i = 0; q.cmpn(0) !== 0; i++) { - var b = q.andln(0xff); - q.ishrn(8); - - res[res.length - i - 1] = b; - } - } else { - // Assume little-endian - for (var i = 0; q.cmpn(0) !== 0; i++) { - var b = q.andln(0xff); - q.ishrn(8); - - res[i] = b; - } - } - - return res; -}; - -if (Math.clz32) { - BN.prototype._countBits = function _countBits(w) { - return 32 - Math.clz32(w); - }; -} else { - BN.prototype._countBits = function _countBits(w) { - var t = w; - var r = 0; - if (t >= 0x1000) { - r += 13; - t >>>= 13; - } - if (t >= 0x40) { - r += 7; - t >>>= 7; - } - if (t >= 0x8) { - r += 4; - t >>>= 4; - } - if (t >= 0x02) { - r += 2; - t >>>= 2; - } - return r + t; - }; -} - -BN.prototype._zeroBits = function _zeroBits(w) { - // Short-cut - if (w === 0) - return 26; - - var t = w; - var r = 0; - if ((t & 0x1fff) === 0) { - r += 13; - t >>>= 13; - } - if ((t & 0x7f) === 0) { - r += 7; - t >>>= 7; - } - if ((t & 0xf) === 0) { - r += 4; - t >>>= 4; - } - if ((t & 0x3) === 0) { - r += 2; - t >>>= 2; - } - if ((t & 0x1) === 0) - r++; - return r; -}; - -// Return number of used bits in a BN -BN.prototype.bitLength = function bitLength() { - var hi = 0; - var w = this.words[this.length - 1]; - var hi = this._countBits(w); - return (this.length - 1) * 26 + hi; -}; - -// Number of trailing zero bits -BN.prototype.zeroBits = function zeroBits() { - if (this.cmpn(0) === 0) - return 0; - - var r = 0; - for (var i = 0; i < this.length; i++) { - var b = this._zeroBits(this.words[i]); - r += b; - if (b !== 26) - break; - } - return r; -}; - -BN.prototype.byteLength = function byteLength() { - return Math.ceil(this.bitLength() / 8); -}; - -// Return negative clone of `this` -BN.prototype.neg = function neg() { - if (this.cmpn(0) === 0) - return this.clone(); - - var r = this.clone(); - r.sign = !this.sign; - return r; -}; - - -// Or `num` with `this` in-place -BN.prototype.ior = function ior(num) { - this.sign = this.sign || num.sign; - - while (this.length < num.length) - this.words[this.length++] = 0; - - for (var i = 0; i < num.length; i++) - this.words[i] = this.words[i] | num.words[i]; - - return this.strip(); -}; - - -// Or `num` with `this` -BN.prototype.or = function or(num) { - if (this.length > num.length) - return this.clone().ior(num); - else - return num.clone().ior(this); -}; - - -// And `num` with `this` in-place -BN.prototype.iand = function iand(num) { - this.sign = this.sign && num.sign; - - // b = min-length(num, this) - var b; - if (this.length > num.length) - b = num; - else - b = this; - - for (var i = 0; i < b.length; i++) - this.words[i] = this.words[i] & num.words[i]; - - this.length = b.length; - - return this.strip(); -}; - - -// And `num` with `this` -BN.prototype.and = function and(num) { - if (this.length > num.length) - return this.clone().iand(num); - else - return num.clone().iand(this); -}; - - -// Xor `num` with `this` in-place -BN.prototype.ixor = function ixor(num) { - this.sign = this.sign || num.sign; - - // a.length > b.length - var a; - var b; - if (this.length > num.length) { - a = this; - b = num; - } else { - a = num; - b = this; - } - - for (var i = 0; i < b.length; i++) - this.words[i] = a.words[i] ^ b.words[i]; - - if (this !== a) - for (; i < a.length; i++) - this.words[i] = a.words[i]; - - this.length = a.length; - - return this.strip(); -}; - - -// Xor `num` with `this` -BN.prototype.xor = function xor(num) { - if (this.length > num.length) - return this.clone().ixor(num); - else - return num.clone().ixor(this); -}; - - -// Set `bit` of `this` -BN.prototype.setn = function setn(bit, val) { - assert(typeof bit === 'number' && bit >= 0); - - var off = (bit / 26) | 0; - var wbit = bit % 26; - - while (this.length <= off) - this.words[this.length++] = 0; - - if (val) - this.words[off] = this.words[off] | (1 << wbit); - else - this.words[off] = this.words[off] & ~(1 << wbit); - - return this.strip(); -}; - - -// Add `num` to `this` in-place -BN.prototype.iadd = function iadd(num) { - // negative + positive - if (this.sign && !num.sign) { - this.sign = false; - var r = this.isub(num); - this.sign = !this.sign; - return this._normSign(); - - // positive + negative - } else if (!this.sign && num.sign) { - num.sign = false; - var r = this.isub(num); - num.sign = true; - return r._normSign(); - } - - // a.length > b.length - var a; - var b; - if (this.length > num.length) { - a = this; - b = num; - } else { - a = num; - b = this; - } - - var carry = 0; - for (var i = 0; i < b.length; i++) { - var r = a.words[i] + b.words[i] + carry; - this.words[i] = r & 0x3ffffff; - carry = r >>> 26; - } - for (; carry !== 0 && i < a.length; i++) { - var r = a.words[i] + carry; - this.words[i] = r & 0x3ffffff; - carry = r >>> 26; - } - - this.length = a.length; - if (carry !== 0) { - this.words[this.length] = carry; - this.length++; - // Copy the rest of the words - } else if (a !== this) { - for (; i < a.length; i++) - this.words[i] = a.words[i]; - } - - return this; -}; - -// Add `num` to `this` -BN.prototype.add = function add(num) { - if (num.sign && !this.sign) { - num.sign = false; - var res = this.sub(num); - num.sign = true; - return res; - } else if (!num.sign && this.sign) { - this.sign = false; - var res = num.sub(this); - this.sign = true; - return res; - } - - if (this.length > num.length) - return this.clone().iadd(num); - else - return num.clone().iadd(this); -}; - -// Subtract `num` from `this` in-place -BN.prototype.isub = function isub(num) { - // this - (-num) = this + num - if (num.sign) { - num.sign = false; - var r = this.iadd(num); - num.sign = true; - return r._normSign(); - - // -this - num = -(this + num) - } else if (this.sign) { - this.sign = false; - this.iadd(num); - this.sign = true; - return this._normSign(); - } - - // At this point both numbers are positive - var cmp = this.cmp(num); - - // Optimization - zeroify - if (cmp === 0) { - this.sign = false; - this.length = 1; - this.words[0] = 0; - return this; - } - - // a > b - var a; - var b; - if (cmp > 0) { - a = this; - b = num; - } else { - a = num; - b = this; - } - - var carry = 0; - for (var i = 0; i < b.length; i++) { - var r = a.words[i] - b.words[i] + carry; - carry = r >> 26; - this.words[i] = r & 0x3ffffff; - } - for (; carry !== 0 && i < a.length; i++) { - var r = a.words[i] + carry; - carry = r >> 26; - this.words[i] = r & 0x3ffffff; - } - - // Copy rest of the words - if (carry === 0 && i < a.length && a !== this) - for (; i < a.length; i++) - this.words[i] = a.words[i]; - this.length = Math.max(this.length, i); - - if (a !== this) - this.sign = true; - - return this.strip(); -}; - -// Subtract `num` from `this` -BN.prototype.sub = function sub(num) { - return this.clone().isub(num); -}; - -/* -// NOTE: This could be potentionally used to generate loop-less multiplications -function _genCombMulTo(alen, blen) { - var len = alen + blen - 1; - var src = [ - 'var a = this.words, b = num.words, o = out.words, c = 0, w, ' + - 'mask = 0x3ffffff, shift = 0x4000000;', - 'out.length = ' + len + ';' - ]; - for (var k = 0; k < len; k++) { - var minJ = Math.max(0, k - alen + 1); - var maxJ = Math.min(k, blen - 1); - - for (var j = minJ; j <= maxJ; j++) { - var i = k - j; - var mul = 'a[' + i + '] * b[' + j + ']'; - - if (j === minJ) { - src.push('w = ' + mul + ' + c;'); - src.push('c = (w / shift) | 0;'); - } else { - src.push('w += ' + mul + ';'); - src.push('c += (w / shift) | 0;'); - } - src.push('w &= mask;'); - } - src.push('o[' + k + '] = w;'); - } - src.push('if (c !== 0) {', - ' o[' + k + '] = c;', - ' out.length++;', - '}', - 'return out;'); - - return src.join('\n'); -} -*/ - -BN.prototype._smallMulTo = function _smallMulTo(num, out) { - out.sign = num.sign !== this.sign; - out.length = this.length + num.length; - - var carry = 0; - for (var k = 0; k < out.length - 1; k++) { - // Sum all words with the same `i + j = k` and accumulate `ncarry`, - // note that ncarry could be >= 0x3ffffff - var ncarry = carry >>> 26; - var rword = carry & 0x3ffffff; - var maxJ = Math.min(k, num.length - 1); - for (var j = Math.max(0, k - this.length + 1); j <= maxJ; j++) { - var i = k - j; - var a = this.words[i] | 0; - var b = num.words[j] | 0; - var r = a * b; - - var lo = r & 0x3ffffff; - ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; - lo = (lo + rword) | 0; - rword = lo & 0x3ffffff; - ncarry = (ncarry + (lo >>> 26)) | 0; - } - out.words[k] = rword; - carry = ncarry; - } - if (carry !== 0) { - out.words[k] = carry; - } else { - out.length--; - } - - return out.strip(); -}; - -BN.prototype._bigMulTo = function _bigMulTo(num, out) { - out.sign = num.sign !== this.sign; - out.length = this.length + num.length; - - var carry = 0; - var hncarry = 0; - for (var k = 0; k < out.length - 1; k++) { - // Sum all words with the same `i + j = k` and accumulate `ncarry`, - // note that ncarry could be >= 0x3ffffff - var ncarry = hncarry; - hncarry = 0; - var rword = carry & 0x3ffffff; - var maxJ = Math.min(k, num.length - 1); - for (var j = Math.max(0, k - this.length + 1); j <= maxJ; j++) { - var i = k - j; - var a = this.words[i] | 0; - var b = num.words[j] | 0; - var r = a * b; - - var lo = r & 0x3ffffff; - ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; - lo = (lo + rword) | 0; - rword = lo & 0x3ffffff; - ncarry = (ncarry + (lo >>> 26)) | 0; - - hncarry += ncarry >>> 26; - ncarry &= 0x3ffffff; - } - out.words[k] = rword; - carry = ncarry; - ncarry = hncarry; - } - if (carry !== 0) { - out.words[k] = carry; - } else { - out.length--; - } - - return out.strip(); -}; - -BN.prototype.mulTo = function mulTo(num, out) { - var res; - if (this.length + num.length < 63) - res = this._smallMulTo(num, out); - else - res = this._bigMulTo(num, out); - return res; -}; - -// Multiply `this` by `num` -BN.prototype.mul = function mul(num) { - var out = new BN(null); - out.words = new Array(this.length + num.length); - return this.mulTo(num, out); -}; - -// In-place Multiplication -BN.prototype.imul = function imul(num) { - if (this.cmpn(0) === 0 || num.cmpn(0) === 0) { - this.words[0] = 0; - this.length = 1; - return this; - } - - var tlen = this.length; - var nlen = num.length; - - this.sign = num.sign !== this.sign; - this.length = this.length + num.length; - this.words[this.length - 1] = 0; - - for (var k = this.length - 2; k >= 0; k--) { - // Sum all words with the same `i + j = k` and accumulate `carry`, - // note that carry could be >= 0x3ffffff - var carry = 0; - var rword = 0; - var maxJ = Math.min(k, nlen - 1); - for (var j = Math.max(0, k - tlen + 1); j <= maxJ; j++) { - var i = k - j; - var a = this.words[i]; - var b = num.words[j]; - var r = a * b; - - var lo = r & 0x3ffffff; - carry += (r / 0x4000000) | 0; - lo += rword; - rword = lo & 0x3ffffff; - carry += lo >>> 26; - } - this.words[k] = rword; - this.words[k + 1] += carry; - carry = 0; - } - - // Propagate overflows - var carry = 0; - for (var i = 1; i < this.length; i++) { - var w = this.words[i] + carry; - this.words[i] = w & 0x3ffffff; - carry = w >>> 26; - } - - return this.strip(); -}; - -BN.prototype.imuln = function imuln(num) { - assert(typeof num === 'number'); - - // Carry - var carry = 0; - for (var i = 0; i < this.length; i++) { - var w = this.words[i] * num; - var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); - carry >>= 26; - carry += (w / 0x4000000) | 0; - // NOTE: lo is 27bit maximum - carry += lo >>> 26; - this.words[i] = lo & 0x3ffffff; - } - - if (carry !== 0) { - this.words[i] = carry; - this.length++; - } - - return this; -}; - -BN.prototype.muln = function muln(num) { - return this.clone().imuln(num); -}; - -// `this` * `this` -BN.prototype.sqr = function sqr() { - return this.mul(this); -}; - -// `this` * `this` in-place -BN.prototype.isqr = function isqr() { - return this.mul(this); -}; - -// Shift-left in-place -BN.prototype.ishln = function ishln(bits) { - assert(typeof bits === 'number' && bits >= 0); - var r = bits % 26; - var s = (bits - r) / 26; - var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); - - if (r !== 0) { - var carry = 0; - for (var i = 0; i < this.length; i++) { - var newCarry = this.words[i] & carryMask; - var c = (this.words[i] - newCarry) << r; - this.words[i] = c | carry; - carry = newCarry >>> (26 - r); - } - if (carry) { - this.words[i] = carry; - this.length++; - } - } - - if (s !== 0) { - for (var i = this.length - 1; i >= 0; i--) - this.words[i + s] = this.words[i]; - for (var i = 0; i < s; i++) - this.words[i] = 0; - this.length += s; - } - - return this.strip(); -}; - -// Shift-right in-place -// NOTE: `hint` is a lowest bit before trailing zeroes -// NOTE: if `extended` is present - it will be filled with destroyed bits -BN.prototype.ishrn = function ishrn(bits, hint, extended) { - assert(typeof bits === 'number' && bits >= 0); - var h; - if (hint) - h = (hint - (hint % 26)) / 26; - else - h = 0; - - var r = bits % 26; - var s = Math.min((bits - r) / 26, this.length); - var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); - var maskedWords = extended; - - h -= s; - h = Math.max(0, h); - - // Extended mode, copy masked part - if (maskedWords) { - for (var i = 0; i < s; i++) - maskedWords.words[i] = this.words[i]; - maskedWords.length = s; - } - - if (s === 0) { - // No-op, we should not move anything at all - } else if (this.length > s) { - this.length -= s; - for (var i = 0; i < this.length; i++) - this.words[i] = this.words[i + s]; - } else { - this.words[0] = 0; - this.length = 1; - } - - var carry = 0; - for (var i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { - var word = this.words[i]; - this.words[i] = (carry << (26 - r)) | (word >>> r); - carry = word & mask; - } - - // Push carried bits as a mask - if (maskedWords && carry !== 0) - maskedWords.words[maskedWords.length++] = carry; - - if (this.length === 0) { - this.words[0] = 0; - this.length = 1; - } - - this.strip(); - - return this; -}; - -// Shift-left -BN.prototype.shln = function shln(bits) { - return this.clone().ishln(bits); -}; - -// Shift-right -BN.prototype.shrn = function shrn(bits) { - return this.clone().ishrn(bits); -}; - -// Test if n bit is set -BN.prototype.testn = function testn(bit) { - assert(typeof bit === 'number' && bit >= 0); - var r = bit % 26; - var s = (bit - r) / 26; - var q = 1 << r; - - // Fast case: bit is much higher than all existing words - if (this.length <= s) { - return false; - } - - // Check bit and return - var w = this.words[s]; - - return !!(w & q); -}; - -// Return only lowers bits of number (in-place) -BN.prototype.imaskn = function imaskn(bits) { - assert(typeof bits === 'number' && bits >= 0); - var r = bits % 26; - var s = (bits - r) / 26; - - assert(!this.sign, 'imaskn works only with positive numbers'); - - if (r !== 0) - s++; - this.length = Math.min(s, this.length); - - if (r !== 0) { - var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); - this.words[this.length - 1] &= mask; - } - - return this.strip(); -}; - -// Return only lowers bits of number -BN.prototype.maskn = function maskn(bits) { - return this.clone().imaskn(bits); -}; - -// Add plain number `num` to `this` -BN.prototype.iaddn = function iaddn(num) { - assert(typeof num === 'number'); - if (num < 0) - return this.isubn(-num); - - // Possible sign change - if (this.sign) { - if (this.length === 1 && this.words[0] < num) { - this.words[0] = num - this.words[0]; - this.sign = false; - return this; - } - - this.sign = false; - this.isubn(num); - this.sign = true; - return this; - } - - // Add without checks - return this._iaddn(num); -}; - -BN.prototype._iaddn = function _iaddn(num) { - this.words[0] += num; - - // Carry - for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { - this.words[i] -= 0x4000000; - if (i === this.length - 1) - this.words[i + 1] = 1; - else - this.words[i + 1]++; - } - this.length = Math.max(this.length, i + 1); - - return this; -}; - -// Subtract plain number `num` from `this` -BN.prototype.isubn = function isubn(num) { - assert(typeof num === 'number'); - if (num < 0) - return this.iaddn(-num); - - if (this.sign) { - this.sign = false; - this.iaddn(num); - this.sign = true; - return this; - } - - this.words[0] -= num; - - // Carry - for (var i = 0; i < this.length && this.words[i] < 0; i++) { - this.words[i] += 0x4000000; - this.words[i + 1] -= 1; - } - - return this.strip(); -}; - -BN.prototype.addn = function addn(num) { - return this.clone().iaddn(num); -}; - -BN.prototype.subn = function subn(num) { - return this.clone().isubn(num); -}; - -BN.prototype.iabs = function iabs() { - this.sign = false; - - return this; -}; - -BN.prototype.abs = function abs() { - return this.clone().iabs(); -}; - -BN.prototype._ishlnsubmul = function _ishlnsubmul(num, mul, shift) { - // Bigger storage is needed - var len = num.length + shift; - var i; - if (this.words.length < len) { - var t = new Array(len); - for (var i = 0; i < this.length; i++) - t[i] = this.words[i]; - this.words = t; - } else { - i = this.length; - } - - // Zeroify rest - this.length = Math.max(this.length, len); - for (; i < this.length; i++) - this.words[i] = 0; - - var carry = 0; - for (var i = 0; i < num.length; i++) { - var w = this.words[i + shift] + carry; - var right = num.words[i] * mul; - w -= right & 0x3ffffff; - carry = (w >> 26) - ((right / 0x4000000) | 0); - this.words[i + shift] = w & 0x3ffffff; - } - for (; i < this.length - shift; i++) { - var w = this.words[i + shift] + carry; - carry = w >> 26; - this.words[i + shift] = w & 0x3ffffff; - } - - if (carry === 0) - return this.strip(); - - // Subtraction overflow - assert(carry === -1); - carry = 0; - for (var i = 0; i < this.length; i++) { - var w = -this.words[i] + carry; - carry = w >> 26; - this.words[i] = w & 0x3ffffff; - } - this.sign = true; - - return this.strip(); -}; - -BN.prototype._wordDiv = function _wordDiv(num, mode) { - var shift = this.length - num.length; - - var a = this.clone(); - var b = num; - - // Normalize - var bhi = b.words[b.length - 1]; - var bhiBits = this._countBits(bhi); - shift = 26 - bhiBits; - if (shift !== 0) { - b = b.shln(shift); - a.ishln(shift); - bhi = b.words[b.length - 1]; - } - - // Initialize quotient - var m = a.length - b.length; - var q; - - if (mode !== 'mod') { - q = new BN(null); - q.length = m + 1; - q.words = new Array(q.length); - for (var i = 0; i < q.length; i++) - q.words[i] = 0; - } - - var diff = a.clone()._ishlnsubmul(b, 1, m); - if (!diff.sign) { - a = diff; - if (q) - q.words[m] = 1; - } - - for (var j = m - 1; j >= 0; j--) { - var qj = a.words[b.length + j] * 0x4000000 + a.words[b.length + j - 1]; - - // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max - // (0x7ffffff) - qj = Math.min((qj / bhi) | 0, 0x3ffffff); - - a._ishlnsubmul(b, qj, j); - while (a.sign) { - qj--; - a.sign = false; - a._ishlnsubmul(b, 1, j); - if (a.cmpn(0) !== 0) - a.sign = !a.sign; - } - if (q) - q.words[j] = qj; - } - if (q) - q.strip(); - a.strip(); - - // Denormalize - if (mode !== 'div' && shift !== 0) - a.ishrn(shift); - return { div: q ? q : null, mod: a }; -}; - -BN.prototype.divmod = function divmod(num, mode) { - assert(num.cmpn(0) !== 0); - - if (this.sign && !num.sign) { - var res = this.neg().divmod(num, mode); - var div; - var mod; - if (mode !== 'mod') - div = res.div.neg(); - if (mode !== 'div') - mod = res.mod.cmpn(0) === 0 ? res.mod : num.sub(res.mod); - return { - div: div, - mod: mod - }; - } else if (!this.sign && num.sign) { - var res = this.divmod(num.neg(), mode); - var div; - if (mode !== 'mod') - div = res.div.neg(); - return { div: div, mod: res.mod }; - } else if (this.sign && num.sign) { - return this.neg().divmod(num.neg(), mode); - } - - // Both numbers are positive at this point - - // Strip both numbers to approximate shift value - if (num.length > this.length || this.cmp(num) < 0) - return { div: new BN(0), mod: this }; - - // Very short reduction - if (num.length === 1) { - if (mode === 'div') - return { div: this.divn(num.words[0]), mod: null }; - else if (mode === 'mod') - return { div: null, mod: new BN(this.modn(num.words[0])) }; - return { - div: this.divn(num.words[0]), - mod: new BN(this.modn(num.words[0])) - }; - } - - return this._wordDiv(num, mode); -}; - -// Find `this` / `num` -BN.prototype.div = function div(num) { - return this.divmod(num, 'div').div; -}; - -// Find `this` % `num` -BN.prototype.mod = function mod(num) { - return this.divmod(num, 'mod').mod; -}; - -// Find Round(`this` / `num`) -BN.prototype.divRound = function divRound(num) { - var dm = this.divmod(num); - - // Fast case - exact division - if (dm.mod.cmpn(0) === 0) - return dm.div; - - var mod = dm.div.sign ? dm.mod.isub(num) : dm.mod; - - var half = num.shrn(1); - var r2 = num.andln(1); - var cmp = mod.cmp(half); - - // Round down - if (cmp < 0 || r2 === 1 && cmp === 0) - return dm.div; - - // Round up - return dm.div.sign ? dm.div.isubn(1) : dm.div.iaddn(1); -}; - -BN.prototype.modn = function modn(num) { - assert(num <= 0x3ffffff); - var p = (1 << 26) % num; - - var acc = 0; - for (var i = this.length - 1; i >= 0; i--) - acc = (p * acc + this.words[i]) % num; - - return acc; -}; - -// In-place division by number -BN.prototype.idivn = function idivn(num) { - assert(num <= 0x3ffffff); - - var carry = 0; - for (var i = this.length - 1; i >= 0; i--) { - var w = this.words[i] + carry * 0x4000000; - this.words[i] = (w / num) | 0; - carry = w % num; - } - - return this.strip(); -}; - -BN.prototype.divn = function divn(num) { - return this.clone().idivn(num); -}; - -BN.prototype.egcd = function egcd(p) { - assert(!p.sign); - assert(p.cmpn(0) !== 0); - - var x = this; - var y = p.clone(); - - if (x.sign) - x = x.mod(p); - else - x = x.clone(); - - // A * x + B * y = x - var A = new BN(1); - var B = new BN(0); - - // C * x + D * y = y - var C = new BN(0); - var D = new BN(1); - - var g = 0; - - while (x.isEven() && y.isEven()) { - x.ishrn(1); - y.ishrn(1); - ++g; - } - - var yp = y.clone(); - var xp = x.clone(); - - while (x.cmpn(0) !== 0) { - while (x.isEven()) { - x.ishrn(1); - if (A.isEven() && B.isEven()) { - A.ishrn(1); - B.ishrn(1); - } else { - A.iadd(yp).ishrn(1); - B.isub(xp).ishrn(1); - } - } - - while (y.isEven()) { - y.ishrn(1); - if (C.isEven() && D.isEven()) { - C.ishrn(1); - D.ishrn(1); - } else { - C.iadd(yp).ishrn(1); - D.isub(xp).ishrn(1); - } - } - - if (x.cmp(y) >= 0) { - x.isub(y); - A.isub(C); - B.isub(D); +module.exports = function getType(val) { + if (val instanceof Number) { + return 'number'; + } else if (val instanceof String) { + return 'string'; + } else if (val instanceof Boolean) { + return 'boolean'; + } else if (Array.isArray(val)) { + return 'array'; + } else if (val === null) { + return 'null'; } else { - y.isub(x); - C.isub(A); - D.isub(B); + return typeof val; } - } - - return { - a: C, - b: D, - gcd: y.ishln(g) - }; }; -// This is reduced incarnation of the binary EEA -// above, designated to invert members of the -// _prime_ fields F(p) at a maximal speed -BN.prototype._invmp = function _invmp(p) { - assert(!p.sign); - assert(p.cmpn(0) !== 0); - - var a = this; - var b = p.clone(); - - if (a.sign) - a = a.mod(p); - else - a = a.clone(); - - var x1 = new BN(1); - var x2 = new BN(0); - - var delta = b.clone(); - - while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { - while (a.isEven()) { - a.ishrn(1); - if (x1.isEven()) - x1.ishrn(1); - else - x1.iadd(delta).ishrn(1); - } - while (b.isEven()) { - b.ishrn(1); - if (x2.isEven()) - x2.ishrn(1); - else - x2.iadd(delta).ishrn(1); - } - if (a.cmp(b) >= 0) { - a.isub(b); - x1.isub(x2); - } else { - b.isub(a); - x2.isub(x1); - } - } - if (a.cmpn(1) === 0) - return x1; - else - return x2; -}; - -BN.prototype.gcd = function gcd(num) { - if (this.cmpn(0) === 0) - return num.clone(); - if (num.cmpn(0) === 0) - return this.clone(); - - var a = this.clone(); - var b = num.clone(); - a.sign = false; - b.sign = false; - - // Remove common factor of two - for (var shift = 0; a.isEven() && b.isEven(); shift++) { - a.ishrn(1); - b.ishrn(1); - } - - do { - while (a.isEven()) - a.ishrn(1); - while (b.isEven()) - b.ishrn(1); - - var r = a.cmp(b); - if (r < 0) { - // Swap `a` and `b` to make `a` always bigger than `b` - var t = a; - a = b; - b = t; - } else if (r === 0 || b.cmpn(1) === 0) { - break; - } - - a.isub(b); - } while (true); - - return b.ishln(shift); -}; - -// Invert number in the field F(num) -BN.prototype.invm = function invm(num) { - return this.egcd(num).a.mod(num); -}; - -BN.prototype.isEven = function isEven() { - return (this.words[0] & 1) === 0; -}; - -BN.prototype.isOdd = function isOdd() { - return (this.words[0] & 1) === 1; -}; - -// And first word and num -BN.prototype.andln = function andln(num) { - return this.words[0] & num; -}; - -// Increment at the bit position in-line -BN.prototype.bincn = function bincn(bit) { - assert(typeof bit === 'number'); - var r = bit % 26; - var s = (bit - r) / 26; - var q = 1 << r; - - // Fast case: bit is much higher than all existing words - if (this.length <= s) { - for (var i = this.length; i < s + 1; i++) - this.words[i] = 0; - this.words[s] |= q; - this.length = s + 1; - return this; - } - - // Add bit and propagate, if needed - var carry = q; - for (var i = s; carry !== 0 && i < this.length; i++) { - var w = this.words[i]; - w += carry; - carry = w >>> 26; - w &= 0x3ffffff; - this.words[i] = w; - } - if (carry !== 0) { - this.words[i] = carry; - this.length++; - } - return this; -}; - -BN.prototype.cmpn = function cmpn(num) { - var sign = num < 0; - if (sign) - num = -num; - - if (this.sign && !sign) - return -1; - else if (!this.sign && sign) - return 1; - - num &= 0x3ffffff; - this.strip(); - - var res; - if (this.length > 1) { - res = 1; - } else { - var w = this.words[0]; - res = w === num ? 0 : w < num ? -1 : 1; - } - if (this.sign) - res = -res; - return res; -}; - -// Compare two numbers and return: -// 1 - if `this` > `num` -// 0 - if `this` == `num` -// -1 - if `this` < `num` -BN.prototype.cmp = function cmp(num) { - if (this.sign && !num.sign) - return -1; - else if (!this.sign && num.sign) - return 1; - - var res = this.ucmp(num); - if (this.sign) - return -res; - else - return res; -}; - -// Unsigned comparison -BN.prototype.ucmp = function ucmp(num) { - // At this point both numbers have the same sign - if (this.length > num.length) - return 1; - else if (this.length < num.length) - return -1; - - var res = 0; - for (var i = this.length - 1; i >= 0; i--) { - var a = this.words[i]; - var b = num.words[i]; - - if (a === b) - continue; - if (a < b) - res = -1; - else if (a > b) - res = 1; - break; - } - return res; -}; - -// -// A reduce context, could be using montgomery or something better, depending -// on the `m` itself. -// -BN.red = function red(num) { - return new Red(num); -}; - -BN.prototype.toRed = function toRed(ctx) { - assert(!this.red, 'Already a number in reduction context'); - assert(!this.sign, 'red works only with positives'); - return ctx.convertTo(this)._forceRed(ctx); -}; - -BN.prototype.fromRed = function fromRed() { - assert(this.red, 'fromRed works only with numbers in reduction context'); - return this.red.convertFrom(this); -}; - -BN.prototype._forceRed = function _forceRed(ctx) { - this.red = ctx; - return this; -}; - -BN.prototype.forceRed = function forceRed(ctx) { - assert(!this.red, 'Already a number in reduction context'); - return this._forceRed(ctx); -}; - -BN.prototype.redAdd = function redAdd(num) { - assert(this.red, 'redAdd works only with red numbers'); - return this.red.add(this, num); -}; - -BN.prototype.redIAdd = function redIAdd(num) { - assert(this.red, 'redIAdd works only with red numbers'); - return this.red.iadd(this, num); -}; - -BN.prototype.redSub = function redSub(num) { - assert(this.red, 'redSub works only with red numbers'); - return this.red.sub(this, num); -}; - -BN.prototype.redISub = function redISub(num) { - assert(this.red, 'redISub works only with red numbers'); - return this.red.isub(this, num); -}; - -BN.prototype.redShl = function redShl(num) { - assert(this.red, 'redShl works only with red numbers'); - return this.red.shl(this, num); -}; - -BN.prototype.redMul = function redMul(num) { - assert(this.red, 'redMul works only with red numbers'); - this.red._verify2(this, num); - return this.red.mul(this, num); -}; - -BN.prototype.redIMul = function redIMul(num) { - assert(this.red, 'redMul works only with red numbers'); - this.red._verify2(this, num); - return this.red.imul(this, num); -}; - -BN.prototype.redSqr = function redSqr() { - assert(this.red, 'redSqr works only with red numbers'); - this.red._verify1(this); - return this.red.sqr(this); -}; - -BN.prototype.redISqr = function redISqr() { - assert(this.red, 'redISqr works only with red numbers'); - this.red._verify1(this); - return this.red.isqr(this); -}; - -// Square root over p -BN.prototype.redSqrt = function redSqrt() { - assert(this.red, 'redSqrt works only with red numbers'); - this.red._verify1(this); - return this.red.sqrt(this); -}; - -BN.prototype.redInvm = function redInvm() { - assert(this.red, 'redInvm works only with red numbers'); - this.red._verify1(this); - return this.red.invm(this); -}; - -// Return negative clone of `this` % `red modulo` -BN.prototype.redNeg = function redNeg() { - assert(this.red, 'redNeg works only with red numbers'); - this.red._verify1(this); - return this.red.neg(this); -}; - -BN.prototype.redPow = function redPow(num) { - assert(this.red && !num.red, 'redPow(normalNum)'); - this.red._verify1(this); - return this.red.pow(this, num); -}; - -// Prime numbers with efficient reduction -var primes = { - k256: null, - p224: null, - p192: null, - p25519: null -}; - -// Pseudo-Mersenne prime -function MPrime(name, p) { - // P = 2 ^ N - K - this.name = name; - this.p = new BN(p, 16); - this.n = this.p.bitLength(); - this.k = new BN(1).ishln(this.n).isub(this.p); - - this.tmp = this._tmp(); -} - -MPrime.prototype._tmp = function _tmp() { - var tmp = new BN(null); - tmp.words = new Array(Math.ceil(this.n / 13)); - return tmp; -}; - -MPrime.prototype.ireduce = function ireduce(num) { - // Assumes that `num` is less than `P^2` - // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) - var r = num; - var rlen; - - do { - this.split(r, this.tmp); - r = this.imulK(r); - r = r.iadd(this.tmp); - rlen = r.bitLength(); - } while (rlen > this.n); - - var cmp = rlen < this.n ? -1 : r.ucmp(this.p); - if (cmp === 0) { - r.words[0] = 0; - r.length = 1; - } else if (cmp > 0) { - r.isub(this.p); - } else { - r.strip(); - } - - return r; -}; - -MPrime.prototype.split = function split(input, out) { - input.ishrn(this.n, 0, out); -}; - -MPrime.prototype.imulK = function imulK(num) { - return num.imul(this.k); -}; - -function K256() { - MPrime.call( - this, - 'k256', - 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); -} -inherits(K256, MPrime); - -K256.prototype.split = function split(input, output) { - // 256 = 9 * 26 + 22 - var mask = 0x3fffff; - - var outLen = Math.min(input.length, 9); - for (var i = 0; i < outLen; i++) - output.words[i] = input.words[i]; - output.length = outLen; - - if (input.length <= 9) { - input.words[0] = 0; - input.length = 1; - return; - } - - // Shift by 9 limbs - var prev = input.words[9]; - output.words[output.length++] = prev & mask; - - for (var i = 10; i < input.length; i++) { - var next = input.words[i]; - input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22); - prev = next; - } - input.words[i - 10] = prev >>> 22; - input.length -= 9; -}; - -K256.prototype.imulK = function imulK(num) { - // K = 0x1000003d1 = [ 0x40, 0x3d1 ] - num.words[num.length] = 0; - num.words[num.length + 1] = 0; - num.length += 2; - - // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 - var hi; - var lo = 0; - for (var i = 0; i < num.length; i++) { - var w = num.words[i]; - hi = w * 0x40; - lo += w * 0x3d1; - hi += (lo / 0x4000000) | 0; - lo &= 0x3ffffff; - - num.words[i] = lo; - - lo = hi; - } - - // Fast length reduction - if (num.words[num.length - 1] === 0) { - num.length--; - if (num.words[num.length - 1] === 0) - num.length--; - } - return num; -}; - -function P224() { - MPrime.call( - this, - 'p224', - 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); -} -inherits(P224, MPrime); - -function P192() { - MPrime.call( - this, - 'p192', - 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); -} -inherits(P192, MPrime); - -function P25519() { - // 2 ^ 255 - 19 - MPrime.call( - this, - '25519', - '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); -} -inherits(P25519, MPrime); - -P25519.prototype.imulK = function imulK(num) { - // K = 0x13 - var carry = 0; - for (var i = 0; i < num.length; i++) { - var hi = num.words[i] * 0x13 + carry; - var lo = hi & 0x3ffffff; - hi >>>= 26; - - num.words[i] = lo; - carry = hi; - } - if (carry !== 0) - num.words[num.length++] = carry; - return num; -}; - -// Exported mostly for testing purposes, use plain name instead -BN._prime = function prime(name) { - // Cached version of prime - if (primes[name]) - return primes[name]; - - var prime; - if (name === 'k256') - prime = new K256(); - else if (name === 'p224') - prime = new P224(); - else if (name === 'p192') - prime = new P192(); - else if (name === 'p25519') - prime = new P25519(); - else - throw new Error('Unknown prime ' + name); - primes[name] = prime; - - return prime; -}; - -// -// Base reduction engine -// -function Red(m) { - if (typeof m === 'string') { - var prime = BN._prime(m); - this.m = prime.p; - this.prime = prime; - } else { - this.m = m; - this.prime = null; - } -} - -Red.prototype._verify1 = function _verify1(a) { - assert(!a.sign, 'red works only with positives'); - assert(a.red, 'red works only with red numbers'); -}; - -Red.prototype._verify2 = function _verify2(a, b) { - assert(!a.sign && !b.sign, 'red works only with positives'); - assert(a.red && a.red === b.red, - 'red works only with red numbers'); -}; - -Red.prototype.imod = function imod(a) { - if (this.prime) - return this.prime.ireduce(a)._forceRed(this); - return a.mod(this.m)._forceRed(this); -}; - -Red.prototype.neg = function neg(a) { - var r = a.clone(); - r.sign = !r.sign; - return r.iadd(this.m)._forceRed(this); -}; - -Red.prototype.add = function add(a, b) { - this._verify2(a, b); - - var res = a.add(b); - if (res.cmp(this.m) >= 0) - res.isub(this.m); - return res._forceRed(this); -}; - -Red.prototype.iadd = function iadd(a, b) { - this._verify2(a, b); - - var res = a.iadd(b); - if (res.cmp(this.m) >= 0) - res.isub(this.m); - return res; -}; - -Red.prototype.sub = function sub(a, b) { - this._verify2(a, b); - - var res = a.sub(b); - if (res.cmpn(0) < 0) - res.iadd(this.m); - return res._forceRed(this); -}; - -Red.prototype.isub = function isub(a, b) { - this._verify2(a, b); - - var res = a.isub(b); - if (res.cmpn(0) < 0) - res.iadd(this.m); - return res; -}; - -Red.prototype.shl = function shl(a, num) { - this._verify1(a); - return this.imod(a.shln(num)); -}; - -Red.prototype.imul = function imul(a, b) { - this._verify2(a, b); - return this.imod(a.imul(b)); -}; - -Red.prototype.mul = function mul(a, b) { - this._verify2(a, b); - return this.imod(a.mul(b)); -}; - -Red.prototype.isqr = function isqr(a) { - return this.imul(a, a); -}; - -Red.prototype.sqr = function sqr(a) { - return this.mul(a, a); -}; - -Red.prototype.sqrt = function sqrt(a) { - if (a.cmpn(0) === 0) - return a.clone(); - - var mod3 = this.m.andln(3); - assert(mod3 % 2 === 1); - - // Fast case - if (mod3 === 3) { - var pow = this.m.add(new BN(1)).ishrn(2); - var r = this.pow(a, pow); - return r; - } - - // Tonelli-Shanks algorithm (Totally unoptimized and slow) - // - // Find Q and S, that Q * 2 ^ S = (P - 1) - var q = this.m.subn(1); - var s = 0; - while (q.cmpn(0) !== 0 && q.andln(1) === 0) { - s++; - q.ishrn(1); - } - assert(q.cmpn(0) !== 0); - - var one = new BN(1).toRed(this); - var nOne = one.redNeg(); - - // Find quadratic non-residue - // NOTE: Max is such because of generalized Riemann hypothesis. - var lpow = this.m.subn(1).ishrn(1); - var z = this.m.bitLength(); - z = new BN(2 * z * z).toRed(this); - while (this.pow(z, lpow).cmp(nOne) !== 0) - z.redIAdd(nOne); - - var c = this.pow(z, q); - var r = this.pow(a, q.addn(1).ishrn(1)); - var t = this.pow(a, q); - var m = s; - while (t.cmp(one) !== 0) { - var tmp = t; - for (var i = 0; tmp.cmp(one) !== 0; i++) - tmp = tmp.redSqr(); - assert(i < m); - var b = this.pow(c, new BN(1).ishln(m - i - 1)); - - r = r.redMul(b); - c = b.redSqr(); - t = t.redMul(c); - m = i; - } - - return r; -}; - -Red.prototype.invm = function invm(a) { - var inv = a._invmp(this.m); - if (inv.sign) { - inv.sign = false; - return this.imod(inv).redNeg(); - } else { - return this.imod(inv); - } -}; - -Red.prototype.pow = function pow(a, num) { - var w = []; - - if (num.cmpn(0) === 0) - return new BN(1); - - var q = num.clone(); - - while (q.cmpn(0) !== 0) { - w.push(q.andln(1)); - q.ishrn(1); - } - - // Skip leading zeroes - var res = a; - for (var i = 0; i < w.length; i++, res = this.sqr(res)) - if (w[i] !== 0) - break; - - if (++i < w.length) { - for (var q = this.sqr(res); i < w.length; i++, q = this.sqr(q)) { - if (w[i] === 0) - continue; - res = this.mul(res, q); - } - } - - return res; -}; - -Red.prototype.convertTo = function convertTo(num) { - var r = num.mod(this.m); - if (r === num) - return r.clone(); - else - return r; -}; - -Red.prototype.convertFrom = function convertFrom(num) { - var res = num.clone(); - res.red = null; - return res; -}; - -// -// Montgomery method engine -// - -BN.mont = function mont(num) { - return new Mont(num); -}; - -function Mont(m) { - Red.call(this, m); - - this.shift = this.m.bitLength(); - if (this.shift % 26 !== 0) - this.shift += 26 - (this.shift % 26); - this.r = new BN(1).ishln(this.shift); - this.r2 = this.imod(this.r.sqr()); - this.rinv = this.r._invmp(this.m); - - this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); - this.minv.sign = true; - this.minv = this.minv.mod(this.r); -} -inherits(Mont, Red); - -Mont.prototype.convertTo = function convertTo(num) { - return this.imod(num.shln(this.shift)); -}; - -Mont.prototype.convertFrom = function convertFrom(num) { - var r = this.imod(num.mul(this.rinv)); - r.red = null; - return r; -}; - -Mont.prototype.imul = function imul(a, b) { - if (a.cmpn(0) === 0 || b.cmpn(0) === 0) { - a.words[0] = 0; - a.length = 1; - return a; - } - - var t = a.imul(b); - var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); - var u = t.isub(c).ishrn(this.shift); - var res = u; - if (u.cmp(this.m) >= 0) - res = u.isub(this.m); - else if (u.cmpn(0) < 0) - res = u.iadd(this.m); - - return res._forceRed(this); -}; - -Mont.prototype.mul = function mul(a, b) { - if (a.cmpn(0) === 0 || b.cmpn(0) === 0) - return new BN(0)._forceRed(this); - - var t = a.mul(b); - var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); - var u = t.isub(c).ishrn(this.shift); - var res = u; - if (u.cmp(this.m) >= 0) - res = u.isub(this.m); - else if (u.cmpn(0) < 0) - res = u.iadd(this.m); - - return res._forceRed(this); -}; - -Mont.prototype.invm = function invm(a) { - // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R - var res = this.imod(a._invmp(this.m).mul(this.r2)); - return res._forceRed(this); -}; - -})(typeof module === 'undefined' || module, this); - -},{}],353:[function(require,module,exports){ -(function (Buffer){ -var hasTypedArrays = false -if(typeof Float64Array !== "undefined") { - var DOUBLE_VIEW = new Float64Array(1) - , UINT_VIEW = new Uint32Array(DOUBLE_VIEW.buffer) - DOUBLE_VIEW[0] = 1.0 - hasTypedArrays = true - if(UINT_VIEW[1] === 0x3ff00000) { - //Use little endian - module.exports = function doubleBitsLE(n) { - DOUBLE_VIEW[0] = n - return [ UINT_VIEW[0], UINT_VIEW[1] ] - } - function toDoubleLE(lo, hi) { - UINT_VIEW[0] = lo - UINT_VIEW[1] = hi - return DOUBLE_VIEW[0] - } - module.exports.pack = toDoubleLE - function lowUintLE(n) { - DOUBLE_VIEW[0] = n - return UINT_VIEW[0] - } - module.exports.lo = lowUintLE - function highUintLE(n) { - DOUBLE_VIEW[0] = n - return UINT_VIEW[1] - } - module.exports.hi = highUintLE - } else if(UINT_VIEW[0] === 0x3ff00000) { - //Use big endian - module.exports = function doubleBitsBE(n) { - DOUBLE_VIEW[0] = n - return [ UINT_VIEW[1], UINT_VIEW[0] ] - } - function toDoubleBE(lo, hi) { - UINT_VIEW[1] = lo - UINT_VIEW[0] = hi - return DOUBLE_VIEW[0] - } - module.exports.pack = toDoubleBE - function lowUintBE(n) { - DOUBLE_VIEW[0] = n - return UINT_VIEW[1] - } - module.exports.lo = lowUintBE - function highUintBE(n) { - DOUBLE_VIEW[0] = n - return UINT_VIEW[0] - } - module.exports.hi = highUintBE - } else { - hasTypedArrays = false - } -} -if(!hasTypedArrays) { - var buffer = new Buffer(8) - module.exports = function doubleBits(n) { - buffer.writeDoubleLE(n, 0, true) - return [ buffer.readUInt32LE(0, true), buffer.readUInt32LE(4, true) ] - } - function toDouble(lo, hi) { - buffer.writeUInt32LE(lo, 0, true) - buffer.writeUInt32LE(hi, 4, true) - return buffer.readDoubleLE(0, true) - } - module.exports.pack = toDouble - function lowUint(n) { - buffer.writeDoubleLE(n, 0, true) - return buffer.readUInt32LE(0, true) - } - module.exports.lo = lowUint - function highUint(n) { - buffer.writeDoubleLE(n, 0, true) - return buffer.readUInt32LE(4, true) - } - module.exports.hi = highUint -} - -module.exports.sign = function(n) { - return module.exports.hi(n) >>> 31 -} - -module.exports.exponent = function(n) { - var b = module.exports.hi(n) - return ((b<<1) >>> 21) - 1023 -} - -module.exports.fraction = function(n) { - var lo = module.exports.lo(n) - var hi = module.exports.hi(n) - var b = hi & ((1<<20) - 1) - if(hi & 0x7ff00000) { - b += (1<<20) - } - return [lo, b] -} - -module.exports.denormalized = function(n) { - var hi = module.exports.hi(n) - return !(hi & 0x7ff00000) -} -}).call(this,require("buffer").Buffer) -},{"buffer":33}],354:[function(require,module,exports){ -'use strict' - -var bnsign = require('./lib/bn-sign') - -module.exports = sign - -function sign(x) { - return bnsign(x[0]) * bnsign(x[1]) -} - -},{"./lib/bn-sign":343}],355:[function(require,module,exports){ -'use strict' - -var rationalize = require('./lib/rationalize') - -module.exports = sub - -function sub(a, b) { - return rationalize(a[0].mul(b[1]).sub(a[1].mul(b[0])), a[1].mul(b[1])) -} - -},{"./lib/rationalize":348}],356:[function(require,module,exports){ -'use strict' - -var bn2num = require('./lib/bn-to-num') -var ctz = require('./lib/ctz') - -module.exports = roundRat - -//Round a rational to the closest float -function roundRat(f) { - var a = f[0] - var b = f[1] - if(a.cmpn(0) === 0) { - return 0 - } - var h = a.divmod(b) - var iv = h.div - var x = bn2num(iv) - var ir = h.mod - if(ir.cmpn(0) === 0) { - return x - } - if(x) { - var s = ctz(x) + 4 - var y = bn2num(ir.shln(s).divRound(b)) - - // flip the sign of y if x is negative - if (x<0) { - y = -y; - } - - return x + y * Math.pow(2, -s) - } else { - var ybits = b.bitLength() - ir.bitLength() + 53 - var y = bn2num(ir.shln(ybits).divRound(b)) - if(ybits < 1023) { - return y * Math.pow(2, -ybits) - } - y *= Math.pow(2, -1023) - return y * Math.pow(2, 1023-ybits) - } -} - -},{"./lib/bn-to-num":344,"./lib/ctz":345}],357:[function(require,module,exports){ -'use strict' - -module.exports = boxIntersectWrapper - -var pool = require('typedarray-pool') -var sweep = require('./lib/sweep') -var boxIntersectIter = require('./lib/intersect') - -function boxEmpty(d, box) { - for(var j=0; j>>1 - if(d <= 0) { - return - } - - var retval - - //Convert red boxes - var redList = pool.mallocDouble(2*d*n) - var redIds = pool.mallocInt32(n) - n = convertBoxes(red, d, redList, redIds) - - if(n > 0) { - if(d === 1 && full) { - //Special case: 1d complete - sweep.init(n) - retval = sweep.sweepComplete( - d, visit, - 0, n, redList, redIds, - 0, n, redList, redIds) - } else { - - //Convert blue boxes - var blueList = pool.mallocDouble(2*d*m) - var blueIds = pool.mallocInt32(m) - m = convertBoxes(blue, d, blueList, blueIds) - - if(m > 0) { - sweep.init(n+m) - - if(d === 1) { - //Special case: 1d bipartite - retval = sweep.sweepBipartite( - d, visit, - 0, n, redList, redIds, - 0, m, blueList, blueIds) - } else { - //General case: d>1 - retval = boxIntersectIter( - d, visit, full, - n, redList, redIds, - m, blueList, blueIds) - } - - pool.free(blueList) - pool.free(blueIds) - } - } - - pool.free(redList) - pool.free(redIds) - } - - return retval -} - - -var RESULT - -function appendItem(i,j) { - RESULT.push([i,j]) -} - -function intersectFullArray(x) { - RESULT = [] - boxIntersect(x, x, appendItem, true) - return RESULT -} - -function intersectBipartiteArray(x, y) { - RESULT = [] - boxIntersect(x, y, appendItem, false) - return RESULT -} - -//User-friendly wrapper, handle full input and no-visitor cases -function boxIntersectWrapper(arg0, arg1, arg2) { - var result - switch(arguments.length) { - case 1: - return intersectFullArray(arg0) - case 2: - if(typeof arg1 === 'function') { - return boxIntersect(arg0, arg0, arg1, true) - } else { - return intersectBipartiteArray(arg0, arg1) - } - case 3: - return boxIntersect(arg0, arg1, arg2, false) - default: - throw new Error('box-intersect: Invalid arguments') - } -} -},{"./lib/intersect":359,"./lib/sweep":363,"typedarray-pool":366}],358:[function(require,module,exports){ -'use strict' - -var DIMENSION = 'd' -var AXIS = 'ax' -var VISIT = 'vv' -var FLIP = 'fp' - -var ELEM_SIZE = 'es' - -var RED_START = 'rs' -var RED_END = 're' -var RED_BOXES = 'rb' -var RED_INDEX = 'ri' -var RED_PTR = 'rp' - -var BLUE_START = 'bs' -var BLUE_END = 'be' -var BLUE_BOXES = 'bb' -var BLUE_INDEX = 'bi' -var BLUE_PTR = 'bp' - -var RETVAL = 'rv' - -var INNER_LABEL = 'Q' - -var ARGS = [ - DIMENSION, - AXIS, - VISIT, - RED_START, - RED_END, - RED_BOXES, - RED_INDEX, - BLUE_START, - BLUE_END, - BLUE_BOXES, - BLUE_INDEX -] - -function generateBruteForce(redMajor, flip, full) { - var funcName = 'bruteForce' + - (redMajor ? 'Red' : 'Blue') + - (flip ? 'Flip' : '') + - (full ? 'Full' : '') - - var code = ['function ', funcName, '(', ARGS.join(), '){', - 'var ', ELEM_SIZE, '=2*', DIMENSION, ';'] - - var redLoop = - 'for(var i=' + RED_START + ',' + RED_PTR + '=' + ELEM_SIZE + '*' + RED_START + ';' + - 'i<' + RED_END +';' + - '++i,' + RED_PTR + '+=' + ELEM_SIZE + '){' + - 'var x0=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '],' + - 'x1=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '+' + DIMENSION + '],' + - 'xi=' + RED_INDEX + '[i];' - - var blueLoop = - 'for(var j=' + BLUE_START + ',' + BLUE_PTR + '=' + ELEM_SIZE + '*' + BLUE_START + ';' + - 'j<' + BLUE_END + ';' + - '++j,' + BLUE_PTR + '+=' + ELEM_SIZE + '){' + - 'var y0=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '],' + - (full ? 'y1=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '+' + DIMENSION + '],' : '') + - 'yi=' + BLUE_INDEX + '[j];' - - if(redMajor) { - code.push(redLoop, INNER_LABEL, ':', blueLoop) - } else { - code.push(blueLoop, INNER_LABEL, ':', redLoop) - } - - if(full) { - code.push('if(y1' + - BLUE_END + '-' + BLUE_START + '){') - - if(full) { - invoke(true, false) - code.push('}else{') - invoke(false, false) - } else { - code.push('if(' + FLIP + '){') - invoke(true, true) - code.push('}else{') - invoke(true, false) - code.push('}}else{if(' + FLIP + '){') - invoke(false, true) - code.push('}else{') - invoke(false, false) - code.push('}') - } - code.push('}}return ' + funcName) - - var codeStr = prefix.join('') + code.join('') - var proc = new Function(codeStr) - return proc() -} - - -exports.partial = bruteForcePlanner(false) -exports.full = bruteForcePlanner(true) -},{}],359:[function(require,module,exports){ -'use strict' - -module.exports = boxIntersectIter - -var pool = require('typedarray-pool') -var bits = require('bit-twiddle') -var bruteForce = require('./brute') -var bruteForcePartial = bruteForce.partial -var bruteForceFull = bruteForce.full -var sweep = require('./sweep') -var findMedian = require('./median') -var genPartition = require('./partition') - -//Twiddle parameters -var BRUTE_FORCE_CUTOFF = 128 //Cut off for brute force search -var SCAN_CUTOFF = (1<<22) //Cut off for two way scan -var SCAN_COMPLETE_CUTOFF = (1<<22) - -//Partition functions -var partitionInteriorContainsInterval = genPartition( - '!(lo>=p0)&&!(p1>=hi)', - ['p0', 'p1']) - -var partitionStartEqual = genPartition( - 'lo===p0', - ['p0']) - -var partitionStartLessThan = genPartition( - 'lo 0) { - top -= 1 - - var iptr = top * IFRAME_SIZE - var axis = BOX_ISTACK[iptr] - var redStart = BOX_ISTACK[iptr+1] - var redEnd = BOX_ISTACK[iptr+2] - var blueStart = BOX_ISTACK[iptr+3] - var blueEnd = BOX_ISTACK[iptr+4] - var state = BOX_ISTACK[iptr+5] - - var dptr = top * DFRAME_SIZE - var lo = BOX_DSTACK[dptr] - var hi = BOX_DSTACK[dptr+1] - - //Unpack state info - var flip = (state & 1) - var full = !!(state & 16) - - //Unpack indices - var red = xBoxes - var redIndex = xIndex - var blue = yBoxes - var blueIndex = yIndex - if(flip) { - red = yBoxes - redIndex = yIndex - blue = xBoxes - blueIndex = xIndex - } - - if(state & 2) { - redEnd = partitionStartLessThan( - d, axis, - redStart, redEnd, red, redIndex, - hi) - if(redStart >= redEnd) { - continue - } - } - if(state & 4) { - redStart = partitionEndLessThanEqual( - d, axis, - redStart, redEnd, red, redIndex, - lo) - if(redStart >= redEnd) { - continue - } - } - - var redCount = redEnd - redStart - var blueCount = blueEnd - blueStart - - if(full) { - if(d * redCount * (redCount + blueCount) < SCAN_COMPLETE_CUTOFF) { - retval = sweep.scanComplete( - d, axis, visit, - redStart, redEnd, red, redIndex, - blueStart, blueEnd, blue, blueIndex) - if(retval !== void 0) { - return retval - } - continue - } - } else { - if(d * Math.min(redCount, blueCount) < BRUTE_FORCE_CUTOFF) { - //If input small, then use brute force - retval = bruteForcePartial( - d, axis, visit, flip, - redStart, redEnd, red, redIndex, - blueStart, blueEnd, blue, blueIndex) - if(retval !== void 0) { - return retval - } - continue - } else if(d * redCount * blueCount < SCAN_CUTOFF) { - //If input medium sized, then use sweep and prune - retval = sweep.scanBipartite( - d, axis, visit, flip, - redStart, redEnd, red, redIndex, - blueStart, blueEnd, blue, blueIndex) - if(retval !== void 0) { - return retval - } - continue - } - } - - //First, find all red intervals whose interior contains (lo,hi) - var red0 = partitionInteriorContainsInterval( - d, axis, - redStart, redEnd, red, redIndex, - lo, hi) - - //Lower dimensional case - if(redStart < red0) { - - if(d * (red0 - redStart) < BRUTE_FORCE_CUTOFF) { - //Special case for small inputs: use brute force - retval = bruteForceFull( - d, axis+1, visit, - redStart, red0, red, redIndex, - blueStart, blueEnd, blue, blueIndex) - if(retval !== void 0) { - return retval - } - } else if(axis === d-2) { - if(flip) { - retval = sweep.sweepBipartite( - d, visit, - blueStart, blueEnd, blue, blueIndex, - redStart, red0, red, redIndex) - } else { - retval = sweep.sweepBipartite( - d, visit, - redStart, red0, red, redIndex, - blueStart, blueEnd, blue, blueIndex) - } - if(retval !== void 0) { - return retval - } - } else { - iterPush(top++, - axis+1, - redStart, red0, - blueStart, blueEnd, - flip, - -Infinity, Infinity) - iterPush(top++, - axis+1, - blueStart, blueEnd, - redStart, red0, - flip^1, - -Infinity, Infinity) - } - } - - //Divide and conquer phase - if(red0 < redEnd) { - - //Cut blue into 3 parts: - // - // Points < mid point - // Points = mid point - // Points > mid point - // - var blue0 = findMedian( - d, axis, - blueStart, blueEnd, blue, blueIndex) - var mid = blue[elemSize * blue0 + axis] - var blue1 = partitionStartEqual( - d, axis, - blue0, blueEnd, blue, blueIndex, - mid) - - //Right case - if(blue1 < blueEnd) { - iterPush(top++, - axis, - red0, redEnd, - blue1, blueEnd, - (flip|4) + (full ? 16 : 0), - mid, hi) - } - - //Left case - if(blueStart < blue0) { - iterPush(top++, - axis, - red0, redEnd, - blueStart, blue0, - (flip|2) + (full ? 16 : 0), - lo, mid) - } - - //Center case (the hard part) - if(blue0 + 1 === blue1) { - //Optimization: Range with exactly 1 point, use a brute force scan - if(full) { - retval = onePointFull( - d, axis, visit, - red0, redEnd, red, redIndex, - blue0, blue, blueIndex[blue0]) - } else { - retval = onePointPartial( - d, axis, visit, flip, - red0, redEnd, red, redIndex, - blue0, blue, blueIndex[blue0]) - } - if(retval !== void 0) { - return retval - } - } else if(blue0 < blue1) { - var red1 - if(full) { - //If full intersection, need to handle special case - red1 = partitionContainsPoint( - d, axis, - red0, redEnd, red, redIndex, - mid) - if(red0 < red1) { - var redX = partitionStartEqual( - d, axis, - red0, red1, red, redIndex, - mid) - if(axis === d-2) { - //Degenerate sweep intersection: - // [red0, redX] with [blue0, blue1] - if(red0 < redX) { - retval = sweep.sweepComplete( - d, visit, - red0, redX, red, redIndex, - blue0, blue1, blue, blueIndex) - if(retval !== void 0) { - return retval - } - } - - //Normal sweep intersection: - // [redX, red1] with [blue0, blue1] - if(redX < red1) { - retval = sweep.sweepBipartite( - d, visit, - redX, red1, red, redIndex, - blue0, blue1, blue, blueIndex) - if(retval !== void 0) { - return retval - } - } - } else { - if(red0 < redX) { - iterPush(top++, - axis+1, - red0, redX, - blue0, blue1, - 16, - -Infinity, Infinity) - } - if(redX < red1) { - iterPush(top++, - axis+1, - redX, red1, - blue0, blue1, - 0, - -Infinity, Infinity) - iterPush(top++, - axis+1, - blue0, blue1, - redX, red1, - 1, - -Infinity, Infinity) - } - } - } - } else { - if(flip) { - red1 = partitionContainsPointProper( - d, axis, - red0, redEnd, red, redIndex, - mid) - } else { - red1 = partitionContainsPoint( - d, axis, - red0, redEnd, red, redIndex, - mid) - } - if(red0 < red1) { - if(axis === d-2) { - if(flip) { - retval = sweep.sweepBipartite( - d, visit, - blue0, blue1, blue, blueIndex, - red0, red1, red, redIndex) - } else { - retval = sweep.sweepBipartite( - d, visit, - red0, red1, red, redIndex, - blue0, blue1, blue, blueIndex) - } - } else { - iterPush(top++, - axis+1, - red0, red1, - blue0, blue1, - flip, - -Infinity, Infinity) - iterPush(top++, - axis+1, - blue0, blue1, - red0, red1, - flip^1, - -Infinity, Infinity) - } - } - } - } - } - } -} -},{"./brute":358,"./median":360,"./partition":361,"./sweep":363,"bit-twiddle":364,"typedarray-pool":366}],360:[function(require,module,exports){ -'use strict' - -module.exports = findMedian - -var genPartition = require('./partition') - -var partitionStartLessThan = genPartition('lostart && boxes[ptr+axis] > x; - --j, ptr-=elemSize) { - //Swap - var aPtr = ptr - var bPtr = ptr+elemSize - for(var k=0; k>> 1) - var elemSize = 2*d - var pivot = mid - var value = boxes[elemSize*mid+axis] - - while(lo < hi) { - if(hi - lo < PARTITION_THRESHOLD) { - insertionSort(d, axis, lo, hi, boxes, ids) - value = boxes[elemSize*mid+axis] - break - } - - //Select pivot using median-of-3 - var count = hi - lo - var pivot0 = (Math.random()*count+lo)|0 - var value0 = boxes[elemSize*pivot0 + axis] - var pivot1 = (Math.random()*count+lo)|0 - var value1 = boxes[elemSize*pivot1 + axis] - var pivot2 = (Math.random()*count+lo)|0 - var value2 = boxes[elemSize*pivot2 + axis] - if(value0 <= value1) { - if(value2 >= value1) { - pivot = pivot1 - value = value1 - } else if(value0 >= value2) { - pivot = pivot0 - value = value0 - } else { - pivot = pivot2 - value = value2 - } - } else { - if(value1 >= value2) { - pivot = pivot1 - value = value1 - } else if(value2 >= value0) { - pivot = pivot0 - value = value0 - } else { - pivot = pivot2 - value = value2 - } - } - - //Swap pivot to end of array - var aPtr = elemSize * (hi-1) - var bPtr = elemSize * pivot - for(var i=0; i= 0) { - reads.push('lo=e[k+n]') - } - if(predicate.indexOf('hi') >= 0) { - reads.push('hi=e[k+o]') - } - fargs.push( - code.replace('_', reads.join()) - .replace('$', predicate)) - return Function.apply(void 0, fargs) -} -},{}],362:[function(require,module,exports){ +},{}],283:[function(require,module,exports){ 'use strict'; -//This code is extracted from ndarray-sort -//It is inlined here as a temporary workaround - -module.exports = wrapper; - -var INSERT_SORT_CUTOFF = 32 - -function wrapper(data, n0) { - if (n0 <= 4*INSERT_SORT_CUTOFF) { - insertionSort(0, n0 - 1, data); - } else { - quickSort(0, n0 - 1, data); - } -} - -function insertionSort(left, right, data) { - var ptr = 2*(left+1) - for(var i=left+1; i<=right; ++i) { - var a = data[ptr++] - var b = data[ptr++] - var j = i - var jptr = ptr-2 - while(j-- > left) { - var x = data[jptr-2] - var y = data[jptr-1] - if(x < a) { - break - } else if(x === a && y < b) { - break - } - data[jptr] = x - data[jptr+1] = y - jptr -= 2 - } - data[jptr] = a - data[jptr+1] = b - } -} - -function swap(i, j, data) { - i *= 2 - j *= 2 - var x = data[i] - var y = data[i+1] - data[i] = data[j] - data[i+1] = data[j+1] - data[j] = x - data[j+1] = y -} - -function move(i, j, data) { - i *= 2 - j *= 2 - data[i] = data[j] - data[i+1] = data[j+1] -} - -function rotate(i, j, k, data) { - i *= 2 - j *= 2 - k *= 2 - var x = data[i] - var y = data[i+1] - data[i] = data[j] - data[i+1] = data[j+1] - data[j] = data[k] - data[j+1] = data[k+1] - data[k] = x - data[k+1] = y -} - -function shufflePivot(i, j, px, py, data) { - i *= 2 - j *= 2 - data[i] = data[j] - data[j] = px - data[i+1] = data[j+1] - data[j+1] = py -} - -function compare(i, j, data) { - i *= 2 - j *= 2 - var x = data[i], - y = data[j] - if(x < y) { - return false - } else if(x === y) { - return data[i+1] > data[j+1] - } - return true -} - -function comparePivot(i, y, b, data) { - i *= 2 - var x = data[i] - if(x < y) { - return true - } else if(x === y) { - return data[i+1] < b - } - return false -} - -function quickSort(left, right, data) { - var sixth = (right - left + 1) / 6 | 0, - index1 = left + sixth, - index5 = right - sixth, - index3 = left + right >> 1, - index2 = index3 - sixth, - index4 = index3 + sixth, - el1 = index1, - el2 = index2, - el3 = index3, - el4 = index4, - el5 = index5, - less = left + 1, - great = right - 1, - tmp = 0 - if(compare(el1, el2, data)) { - tmp = el1 - el1 = el2 - el2 = tmp - } - if(compare(el4, el5, data)) { - tmp = el4 - el4 = el5 - el5 = tmp - } - if(compare(el1, el3, data)) { - tmp = el1 - el1 = el3 - el3 = tmp - } - if(compare(el2, el3, data)) { - tmp = el2 - el2 = el3 - el3 = tmp - } - if(compare(el1, el4, data)) { - tmp = el1 - el1 = el4 - el4 = tmp - } - if(compare(el3, el4, data)) { - tmp = el3 - el3 = el4 - el4 = tmp - } - if(compare(el2, el5, data)) { - tmp = el2 - el2 = el5 - el5 = tmp - } - if(compare(el2, el3, data)) { - tmp = el2 - el2 = el3 - el3 = tmp - } - if(compare(el4, el5, data)) { - tmp = el4 - el4 = el5 - el5 = tmp - } - - var pivot1X = data[2*el2] - var pivot1Y = data[2*el2+1] - var pivot2X = data[2*el4] - var pivot2Y = data[2*el4+1] - - var ptr0 = 2 * el1; - var ptr2 = 2 * el3; - var ptr4 = 2 * el5; - var ptr5 = 2 * index1; - var ptr6 = 2 * index3; - var ptr7 = 2 * index5; - for (var i1 = 0; i1 < 2; ++i1) { - var x = data[ptr0+i1]; - var y = data[ptr2+i1]; - var z = data[ptr4+i1]; - data[ptr5+i1] = x; - data[ptr6+i1] = y; - data[ptr7+i1] = z; - } - - move(index2, left, data) - move(index4, right, data) - for (var k = less; k <= great; ++k) { - if (comparePivot(k, pivot1X, pivot1Y, data)) { - if (k !== less) { - swap(k, less, data) - } - ++less; +// Turn jsonlint-lines-primitives objects into primitive objects +module.exports = function unbundle(value) { + if (value instanceof Number || value instanceof String || value instanceof Boolean) { + return value.valueOf(); } else { - if (!comparePivot(k, pivot2X, pivot2Y, data)) { - while (true) { - if (!comparePivot(great, pivot2X, pivot2Y, data)) { - if (--great < k) { - break; + return value; + } +}; + +},{}],284:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var getType = require('../util/get_type'); +var extend = require('../util/extend'); + +// Main recursive validation function. Tracks: +// +// - key: string representing location of validation in style tree. Used only +// for more informative error reporting. +// - value: current value from style being evaluated. May be anything from a +// high level object that needs to be descended into deeper or a simple +// scalar value. +// - valueSpec: current spec being evaluated. Tracks value. + +module.exports = function validate(options) { + + var validateFunction = require('./validate_function'); + var validateObject = require('./validate_object'); + var VALIDATORS = { + '*': function() { + return []; + }, + 'array': require('./validate_array'), + 'boolean': require('./validate_boolean'), + 'number': require('./validate_number'), + 'color': require('./validate_color'), + 'constants': require('./validate_constants'), + 'enum': require('./validate_enum'), + 'filter': require('./validate_filter'), + 'function': require('./validate_function'), + 'layer': require('./validate_layer'), + 'object': require('./validate_object'), + 'source': require('./validate_source'), + 'string': require('./validate_string') + }; + + var value = options.value; + var valueSpec = options.valueSpec; + var key = options.key; + var styleSpec = options.styleSpec; + var style = options.style; + + if (getType(value) === 'string' && value[0] === '@') { + if (styleSpec.$version > 7) { + return [new ValidationError(key, value, 'constants have been deprecated as of v8')]; + } + if (!(value in style.constants)) { + return [new ValidationError(key, value, 'constant "%s" not found', value)]; + } + options = extend({}, options, { value: style.constants[value] }); + } + + if (valueSpec.function && getType(value) === 'object') { + return validateFunction(options); + + } else if (valueSpec.type && VALIDATORS[valueSpec.type]) { + return VALIDATORS[valueSpec.type](options); + + } else { + return validateObject(extend({}, options, { + valueSpec: valueSpec.type ? styleSpec[valueSpec.type] : valueSpec + })); + } +}; + +},{"../error/validation_error":280,"../util/extend":281,"../util/get_type":282,"./validate_array":285,"./validate_boolean":286,"./validate_color":287,"./validate_constants":288,"./validate_enum":289,"./validate_filter":290,"./validate_function":291,"./validate_layer":293,"./validate_number":295,"./validate_object":296,"./validate_source":298,"./validate_string":299}],285:[function(require,module,exports){ +'use strict'; + +var getType = require('../util/get_type'); +var validate = require('./validate'); +var ValidationError = require('../error/validation_error'); + +module.exports = function validateArray(options) { + var array = options.value; + var arraySpec = options.valueSpec; + var style = options.style; + var styleSpec = options.styleSpec; + var key = options.key; + var validateArrayElement = options.arrayElementValidator || validate; + + if (getType(array) !== 'array') { + return [new ValidationError(key, array, 'array expected, %s found', getType(array))]; + } + + if (arraySpec.length && array.length !== arraySpec.length) { + return [new ValidationError(key, array, 'array length %d expected, length %d found', arraySpec.length, array.length)]; + } + + if (arraySpec['min-length'] && array.length < arraySpec['min-length']) { + return [new ValidationError(key, array, 'array length at least %d expected, length %d found', arraySpec['min-length'], array.length)]; + } + + var arrayElementSpec = { + "type": arraySpec.value + }; + + if (styleSpec.$version < 7) { + arrayElementSpec.function = arraySpec.function; + } + + if (getType(arraySpec.value) === 'object') { + arrayElementSpec = arraySpec.value; + } + + var errors = []; + for (var i = 0; i < array.length; i++) { + errors = errors.concat(validateArrayElement({ + array: array, + arrayIndex: i, + value: array[i], + valueSpec: arrayElementSpec, + style: style, + styleSpec: styleSpec, + key: key + '[' + i + ']' + })); + } + return errors; +}; + +},{"../error/validation_error":280,"../util/get_type":282,"./validate":284}],286:[function(require,module,exports){ +'use strict'; + +var getType = require('../util/get_type'); +var ValidationError = require('../error/validation_error'); + +module.exports = function validateBoolean(options) { + var value = options.value; + var key = options.key; + var type = getType(value); + + if (type !== 'boolean') { + return [new ValidationError(key, value, 'boolean expected, %s found', type)]; + } + + return []; +}; + +},{"../error/validation_error":280,"../util/get_type":282}],287:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var getType = require('../util/get_type'); +var parseCSSColor = require('csscolorparser').parseCSSColor; + +module.exports = function validateColor(options) { + var key = options.key; + var value = options.value; + var type = getType(value); + + if (type !== 'string') { + return [new ValidationError(key, value, 'color expected, %s found', type)]; + } + + if (parseCSSColor(value) === null) { + return [new ValidationError(key, value, 'color expected, "%s" found', value)]; + } + + return []; +}; + +},{"../error/validation_error":280,"../util/get_type":282,"csscolorparser":80}],288:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var getType = require('../util/get_type'); + +module.exports = function validateConstants(options) { + var key = options.key; + var constants = options.value; + var styleSpec = options.styleSpec; + + if (styleSpec.$version > 7) { + if (constants) { + return [new ValidationError(key, constants, 'constants have been deprecated as of v8')]; + } else { + return []; + } + } else { + var type = getType(constants); + if (type !== 'object') { + return [new ValidationError(key, constants, 'object expected, %s found', type)]; + } + + var errors = []; + for (var constantName in constants) { + if (constantName[0] !== '@') { + errors.push(new ValidationError(key + '.' + constantName, constants[constantName], 'constants must start with "@"')); } - continue; - } else { - if (comparePivot(great, pivot1X, pivot1Y, data)) { - rotate(k, less, great, data) - ++less; - --great; - } else { - swap(k, great, data) - --great; + } + return errors; + } + +}; + +},{"../error/validation_error":280,"../util/get_type":282}],289:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var unbundle = require('../util/unbundle_jsonlint'); + +module.exports = function validateEnum(options) { + var key = options.key; + var value = options.value; + var valueSpec = options.valueSpec; + var errors = []; + + if (valueSpec.values.indexOf(unbundle(value)) === -1) { + errors.push(new ValidationError(key, value, 'expected one of [%s], %s found', valueSpec.values.join(', '), value)); + } + return errors; +}; + +},{"../error/validation_error":280,"../util/unbundle_jsonlint":283}],290:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var validateEnum = require('./validate_enum'); +var getType = require('../util/get_type'); +var unbundle = require('../util/unbundle_jsonlint'); + +module.exports = function validateFilter(options) { + var value = options.value; + var key = options.key; + var styleSpec = options.styleSpec; + var type; + + var errors = []; + + if (getType(value) !== 'array') { + return [new ValidationError(key, value, 'array expected, %s found', getType(value))]; + } + + if (value.length < 1) { + return [new ValidationError(key, value, 'filter array must have at least 1 element')]; + } + + errors = errors.concat(validateEnum({ + key: key + '[0]', + value: value[0], + valueSpec: styleSpec.filter_operator, + style: options.style, + styleSpec: options.styleSpec + })); + + switch (unbundle(value[0])) { + case '<': + case '<=': + case '>': + case '>=': + if (value.length >= 2 && value[1] == '$type') { + errors.push(new ValidationError(key, value, '"$type" cannot be use with operator "%s"', value[0])); + } + /* falls through */ + case '==': + case '!=': + if (value.length != 3) { + errors.push(new ValidationError(key, value, 'filter array for operator "%s" must have 3 elements', value[0])); + } + /* falls through */ + case 'in': + case '!in': + if (value.length >= 2) { + type = getType(value[1]); + if (type !== 'string') { + errors.push(new ValidationError(key + '[1]', value[1], 'string expected, %s found', type)); + } else if (value[1][0] === '@') { + errors.push(new ValidationError(key + '[1]', value[1], 'filter key cannot be a constant')); + } + } + for (var i = 2; i < value.length; i++) { + type = getType(value[i]); + if (value[1] == '$type') { + errors = errors.concat(validateEnum({ + key: key + '[' + i + ']', + value: value[i], + valueSpec: styleSpec.geometry_type, + style: options.style, + styleSpec: options.styleSpec + })); + } else if (type === 'string' && value[i][0] === '@') { + errors.push(new ValidationError(key + '[' + i + ']', value[i], 'filter value cannot be a constant')); + } else if (type !== 'string' && type !== 'number' && type !== 'boolean') { + errors.push(new ValidationError(key + '[' + i + ']', value[i], 'string, number, or boolean expected, %s found', type)); + } } break; - } - } - } - } - } - shufflePivot(left, less-1, pivot1X, pivot1Y, data) - shufflePivot(right, great+1, pivot2X, pivot2Y, data) - if (less - 2 - left <= INSERT_SORT_CUTOFF) { - insertionSort(left, less - 2, data); - } else { - quickSort(left, less - 2, data); - } - if (right - (great + 2) <= INSERT_SORT_CUTOFF) { - insertionSort(great + 2, right, data); - } else { - quickSort(great + 2, right, data); - } - if (great - less <= INSERT_SORT_CUTOFF) { - insertionSort(less, great, data); - } else { - quickSort(less, great, data); - } -} -},{}],363:[function(require,module,exports){ -'use strict' -module.exports = { - init: sqInit, - sweepBipartite: sweepBipartite, - sweepComplete: sweepComplete, - scanBipartite: scanBipartite, - scanComplete: scanComplete -} - -var pool = require('typedarray-pool') -var bits = require('bit-twiddle') -var isort = require('./sort') - -//Flag for blue -var BLUE_FLAG = (1<<28) - -//1D sweep event queue stuff (use pool to save space) -var INIT_CAPACITY = 1024 -var RED_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) -var RED_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) -var BLUE_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) -var BLUE_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) -var COMMON_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) -var COMMON_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) -var SWEEP_EVENTS = pool.mallocDouble(INIT_CAPACITY * 8) - -//Reserves memory for the 1D sweep data structures -function sqInit(count) { - var rcount = bits.nextPow2(count) - if(RED_SWEEP_QUEUE.length < rcount) { - pool.free(RED_SWEEP_QUEUE) - RED_SWEEP_QUEUE = pool.mallocInt32(rcount) - } - if(RED_SWEEP_INDEX.length < rcount) { - pool.free(RED_SWEEP_INDEX) - RED_SWEEP_INDEX = pool.mallocInt32(rcount) - } - if(BLUE_SWEEP_QUEUE.length < rcount) { - pool.free(BLUE_SWEEP_QUEUE) - BLUE_SWEEP_QUEUE = pool.mallocInt32(rcount) - } - if(BLUE_SWEEP_INDEX.length < rcount) { - pool.free(BLUE_SWEEP_INDEX) - BLUE_SWEEP_INDEX = pool.mallocInt32(rcount) - } - if(COMMON_SWEEP_QUEUE.length < rcount) { - pool.free(COMMON_SWEEP_QUEUE) - COMMON_SWEEP_QUEUE = pool.mallocInt32(rcount) - } - if(COMMON_SWEEP_INDEX.length < rcount) { - pool.free(COMMON_SWEEP_INDEX) - COMMON_SWEEP_INDEX = pool.mallocInt32(rcount) - } - var eventLength = 8 * rcount - if(SWEEP_EVENTS.length < eventLength) { - pool.free(SWEEP_EVENTS) - SWEEP_EVENTS = pool.mallocDouble(eventLength) - } -} - -//Remove an item from the active queue in O(1) -function sqPop(queue, index, count, item) { - var idx = index[item] - var top = queue[count-1] - queue[idx] = top - index[top] = idx -} - -//Insert an item into the active queue in O(1) -function sqPush(queue, index, count, item) { - queue[count] = item - index[item] = count -} - -//Recursion base case: use 1D sweep algorithm -function sweepBipartite( - d, visit, - redStart, redEnd, red, redIndex, - blueStart, blueEnd, blue, blueIndex) { - - //store events as pairs [coordinate, idx] - // - // red create: -(idx+1) - // red destroy: idx - // blue create: -(idx+BLUE_FLAG) - // blue destroy: idx+BLUE_FLAG - // - var ptr = 0 - var elemSize = 2*d - var istart = d-1 - var iend = elemSize-1 - - for(var i=redStart; iright - var n = ptr >>> 1 - isort(SWEEP_EVENTS, n) - - var redActive = 0 - var blueActive = 0 - for(var i=0; i= BLUE_FLAG) { - //blue destroy event - e = (e-BLUE_FLAG)|0 - sqPop(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive--, e) - } else if(e >= 0) { - //red destroy event - sqPop(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive--, e) - } else if(e <= -BLUE_FLAG) { - //blue create event - e = (-e-BLUE_FLAG)|0 - for(var j=0; jright - var n = ptr >>> 1 - isort(SWEEP_EVENTS, n) - - var redActive = 0 - var blueActive = 0 - var commonActive = 0 - for(var i=0; i>1) === (SWEEP_EVENTS[2*i+3]>>1)) { - color = 2 - i += 1 - } - - if(e < 0) { - //Create event - var id = -(e>>1) - 1 - - //Intersect with common - for(var j=0; j>1) - 1 - if(color === 0) { - //Red - sqPop(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive--, id) - } else if(color === 1) { - //Blue - sqPop(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive--, id) - } else if(color === 2) { - //Both - sqPop(COMMON_SWEEP_QUEUE, COMMON_SWEEP_INDEX, commonActive--, id) - } - } - } -} - -//Sweep and prune/scanline algorithm: -// Scan along axis, detect intersections -// Brute force all boxes along axis -function scanBipartite( - d, axis, visit, flip, - redStart, redEnd, red, redIndex, - blueStart, blueEnd, blue, blueIndex) { - - var ptr = 0 - var elemSize = 2*d - var istart = axis - var iend = axis+d - - var redShift = 1 - var blueShift = 1 - if(flip) { - blueShift = BLUE_FLAG - } else { - redShift = BLUE_FLAG - } - - for(var i=redStart; iright - var n = ptr >>> 1 - isort(SWEEP_EVENTS, n) - - var redActive = 0 - for(var i=0; i= BLUE_FLAG) { - isRed = !flip - idx -= BLUE_FLAG - } else { - isRed = !!flip - idx -= 1 - } - if(isRed) { - sqPush(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive++, idx) - } else { - var blueId = blueIndex[idx] - var bluePtr = elemSize * idx - - var b0 = blue[bluePtr+axis+1] - var b1 = blue[bluePtr+axis+1+d] - -red_loop: - for(var j=0; jright - var n = ptr >>> 1 - isort(SWEEP_EVENTS, n) - - var redActive = 0 - for(var i=0; i= BLUE_FLAG) { - RED_SWEEP_QUEUE[redActive++] = idx - BLUE_FLAG - } else { - idx -= 1 - var blueId = blueIndex[idx] - var bluePtr = elemSize * idx - - var b0 = blue[bluePtr+axis+1] - var b1 = blue[bluePtr+axis+1+d] - -red_loop: - for(var j=0; j=0; --j) { - if(RED_SWEEP_QUEUE[j] === idx) { - for(var k=j+1; k>>0 - -module.exports = nextafter - -function nextafter(x, y) { - if(isNaN(x) || isNaN(y)) { - return NaN - } - if(x === y) { - return x - } - if(x === 0) { - if(y < 0) { - return -SMALLEST_DENORM - } else { - return SMALLEST_DENORM - } - } - var hi = doubleBits.hi(x) - var lo = doubleBits.lo(x) - if((y > x) === (x > 0)) { - if(lo === UINT_MAX) { - hi += 1 - lo = 0 - } else { - lo += 1 - } - } else { - if(lo === 0) { - lo = UINT_MAX - hi -= 1 - } else { - lo -= 1 - } - } - return doubleBits.pack(lo, hi) -} -},{"double-bits":369}],369:[function(require,module,exports){ -arguments[4][353][0].apply(exports,arguments) -},{"buffer":33,"dup":353}],370:[function(require,module,exports){ -'use strict' - -var bnadd = require('big-rat/add') - -module.exports = add - -function add(a, b) { - var n = a.length - var r = new Array(n) - for(var i=0; i 0 && y0 > 0) || (x0 < 0 && y0 < 0)) { - return false - } - - var x1 = orient(b0, a0, a1) - var y1 = orient(b1, a0, a1) - if((x1 > 0 && y1 > 0) || (x1 < 0 && y1 < 0)) { - return false - } - - //Check for degenerate collinear case - if(x0 === 0 && y0 === 0 && x1 === 0 && y1 === 0) { - return checkCollinear(a0, a1, b0, b1) - } - - return true -} -},{"robust-orientation":1127}],375:[function(require,module,exports){ -arguments[4][169][0].apply(exports,arguments) -},{"dup":169}],376:[function(require,module,exports){ -'use strict' - -module.exports = trimLeaves - -var e2a = require('edges-to-adjacency-list') - -function trimLeaves(edges, positions) { - var adj = e2a(edges, positions.length) - var live = new Array(positions.length) - var nbhd = new Array(positions.length) - - var dead = [] - for(var i=0; i 0) { - var v = dead.pop() - live[v] = false - var n = adj[v] - for(var i=0; i 0) { - nextCell = adj[i][b][0] - nextDir = i - break - } - } - nextVertex = nextCell[nextDir^1] - - for(var dir=0; dir<2; ++dir) { - var nbhd = adj[dir][b] - for(var k=0; k 0) { - nextCell = e - nextVertex = p - nextDir = dir - } - } - } - if(noCut) { - return nextVertex - } - if(nextCell) { - cut(nextCell, nextDir) - } - return nextVertex - } - - function extractCycle(v, dir) { - var e0 = adj[dir][v][0] - var cycle = [v] - cut(e0, dir) - var u = e0[dir^1] - var d0 = dir - while(true) { - while(u !== v) { - cycle.push(u) - u = next(cycle[cycle.length-2], u, false) - } - if(adj[0][v].length + adj[1][v].length === 0) { - break - } - var a = cycle[cycle.length-1] - var b = v - var c = cycle[1] - var d = next(a, b, true) - if(compareAngle(positions[a], positions[b], positions[c], positions[d]) < 0) { - break - } - cycle.push(v) - u = next(a, b) - } - return cycle - } - - function shouldGlue(pcycle, ncycle) { - return (ncycle[1] === ncycle[ncycle.length-1]) - } - - for(var i=0; i 0) { - var ni = adj[0][i].length - var ncycle = extractCycle(i,j) - if(shouldGlue(pcycle, ncycle)) { - //Glue together trivial cycles - pcycle.push.apply(pcycle, ncycle) - } else { - if(pcycle.length > 0) { - cycles.push(pcycle) - } - pcycle = ncycle - } - } - if(pcycle.length > 0) { - cycles.push(pcycle) - } - } - } - - //Combine paths and loops together - return cycles -} -},{"compare-angle":379}],379:[function(require,module,exports){ -"use strict" - -module.exports = compareAngle - -var orient = require("robust-orientation") -var sgn = require("signum") -var twoSum = require("two-sum") -var robustProduct = require("robust-product") -var robustSum = require("robust-sum") - -function testInterior(a, b, c) { - var x0 = twoSum(a[0], -b[0]) - var y0 = twoSum(a[1], -b[1]) - var x1 = twoSum(c[0], -b[0]) - var y1 = twoSum(c[1], -b[1]) - - var d = robustSum( - robustProduct(x0, x1), - robustProduct(y0, y1)) - - return d[d.length-1] >= 0 -} - -function compareAngle(a, b, c, d) { - var bcd = orient(b, c, d) - if(bcd === 0) { - //Handle degenerate cases - var sabc = sgn(orient(a, b, c)) - var sabd = sgn(orient(a, b, d)) - if(sabc === sabd) { - if(sabc === 0) { - var ic = testInterior(a, b, c) - var id = testInterior(a, b, d) - if(ic === id) { - return 0 - } else if(ic) { - return 1 - } else { - return -1 - } - } - return 0 - } else if(sabd === 0) { - if(sabc > 0) { - return -1 - } else if(testInterior(a, b, d)) { - return -1 - } else { - return 1 - } - } else if(sabc === 0) { - if(sabd > 0) { - return 1 - } else if(testInterior(a, b, c)) { - return 1 - } else { - return -1 - } - } - return sgn(sabd - sabc) - } - var abc = orient(a, b, c) - if(abc > 0) { - if(bcd > 0 && orient(a, b, d) > 0) { - return 1 - } - return -1 - } else if(abc < 0) { - if(bcd > 0 || orient(a, b, d) > 0) { - return 1 - } - return -1 - } else { - var abd = orient(a, b, d) - if(abd > 0) { - return 1 - } else { - if(testInterior(a, b, c)) { - return 1 - } else { - return -1 - } - } - } -} -},{"robust-orientation":1127,"robust-product":381,"robust-sum":390,"signum":382,"two-sum":383}],380:[function(require,module,exports){ -arguments[4][151][0].apply(exports,arguments) -},{"dup":151,"two-product":391,"two-sum":383}],381:[function(require,module,exports){ -"use strict" - -var robustSum = require("robust-sum") -var robustScale = require("robust-scale") - -module.exports = robustProduct - -function robustProduct(a, b) { - if(a.length === 1) { - return robustScale(b, a[0]) - } - if(b.length === 1) { - return robustScale(a, b[0]) - } - if(a.length === 0 || b.length === 0) { - return [0] - } - var r = [0] - if(a.length < b.length) { - for(var i=0; i 0) { return 1 } - return 0.0 -} -},{}],383:[function(require,module,exports){ -arguments[4][150][0].apply(exports,arguments) -},{"dup":150}],384:[function(require,module,exports){ -arguments[4][118][0].apply(exports,arguments) -},{"dup":118}],385:[function(require,module,exports){ -"use strict" - -var bounds = require("binary-search-bounds") - -var NOT_FOUND = 0 -var SUCCESS = 1 -var EMPTY = 2 - -module.exports = createWrapper - -function IntervalTreeNode(mid, left, right, leftPoints, rightPoints) { - this.mid = mid - this.left = left - this.right = right - this.leftPoints = leftPoints - this.rightPoints = rightPoints - this.count = (left ? left.count : 0) + (right ? right.count : 0) + leftPoints.length -} - -var proto = IntervalTreeNode.prototype - -function copy(a, b) { - a.mid = b.mid - a.left = b.left - a.right = b.right - a.leftPoints = b.leftPoints - a.rightPoints = b.rightPoints - a.count = b.count -} - -function rebuild(node, intervals) { - var ntree = createIntervalTree(intervals) - node.mid = ntree.mid - node.left = ntree.left - node.right = ntree.right - node.leftPoints = ntree.leftPoints - node.rightPoints = ntree.rightPoints - node.count = ntree.count -} - -function rebuildWithInterval(node, interval) { - var intervals = node.intervals([]) - intervals.push(interval) - rebuild(node, intervals) -} - -function rebuildWithoutInterval(node, interval) { - var intervals = node.intervals([]) - var idx = intervals.indexOf(interval) - if(idx < 0) { - return NOT_FOUND - } - intervals.splice(idx, 1) - rebuild(node, intervals) - return SUCCESS -} - -proto.intervals = function(result) { - result.push.apply(result, this.leftPoints) - if(this.left) { - this.left.intervals(result) - } - if(this.right) { - this.right.intervals(result) - } - return result -} - -proto.insert = function(interval) { - var weight = this.count - this.leftPoints.length - this.count += 1 - if(interval[1] < this.mid) { - if(this.left) { - if(4*(this.left.count+1) > 3*(weight+1)) { - rebuildWithInterval(this, interval) - } else { - this.left.insert(interval) - } - } else { - this.left = createIntervalTree([interval]) - } - } else if(interval[0] > this.mid) { - if(this.right) { - if(4*(this.right.count+1) > 3*(weight+1)) { - rebuildWithInterval(this, interval) - } else { - this.right.insert(interval) - } - } else { - this.right = createIntervalTree([interval]) - } - } else { - var l = bounds.ge(this.leftPoints, interval, compareBegin) - var r = bounds.ge(this.rightPoints, interval, compareEnd) - this.leftPoints.splice(l, 0, interval) - this.rightPoints.splice(r, 0, interval) - } -} - -proto.remove = function(interval) { - var weight = this.count - this.leftPoints - if(interval[1] < this.mid) { - if(!this.left) { - return NOT_FOUND - } - var rw = this.right ? this.right.count : 0 - if(4 * rw > 3 * (weight-1)) { - return rebuildWithoutInterval(this, interval) - } - var r = this.left.remove(interval) - if(r === EMPTY) { - this.left = null - this.count -= 1 - return SUCCESS - } else if(r === SUCCESS) { - this.count -= 1 - } - return r - } else if(interval[0] > this.mid) { - if(!this.right) { - return NOT_FOUND - } - var lw = this.left ? this.left.count : 0 - if(4 * lw > 3 * (weight-1)) { - return rebuildWithoutInterval(this, interval) - } - var r = this.right.remove(interval) - if(r === EMPTY) { - this.right = null - this.count -= 1 - return SUCCESS - } else if(r === SUCCESS) { - this.count -= 1 - } - return r - } else { - if(this.count === 1) { - if(this.leftPoints[0] === interval) { - return EMPTY - } else { - return NOT_FOUND - } - } - if(this.leftPoints.length === 1 && this.leftPoints[0] === interval) { - if(this.left && this.right) { - var p = this - var n = this.left - while(n.right) { - p = n - n = n.right - } - if(p === this) { - n.right = this.right - } else { - var l = this.left - var r = this.right - p.count -= n.count - p.right = n.left - n.left = l - n.right = r - } - copy(this, n) - this.count = (this.left?this.left.count:0) + (this.right?this.right.count:0) + this.leftPoints.length - } else if(this.left) { - copy(this, this.left) - } else { - copy(this, this.right) - } - return SUCCESS - } - for(var l = bounds.ge(this.leftPoints, interval, compareBegin); l=0 && arr[i][1] >= lo; --i) { - var r = cb(arr[i]) - if(r) { return r } - } -} - -function reportRange(arr, cb) { - for(var i=0; i this.mid) { - if(this.right) { - var r = this.right.queryPoint(x, cb) - if(r) { return r } - } - return reportRightRange(this.rightPoints, x, cb) - } else { - return reportRange(this.leftPoints, cb) - } -} - -proto.queryInterval = function(lo, hi, cb) { - if(lo < this.mid && this.left) { - var r = this.left.queryInterval(lo, hi, cb) - if(r) { return r } - } - if(hi > this.mid && this.right) { - var r = this.right.queryInterval(lo, hi, cb) - if(r) { return r } - } - if(hi < this.mid) { - return reportLeftRange(this.leftPoints, hi, cb) - } else if(lo > this.mid) { - return reportRightRange(this.rightPoints, lo, cb) - } else { - return reportRange(this.leftPoints, cb) - } -} - -function compareNumbers(a, b) { - return a - b -} - -function compareBegin(a, b) { - var d = a[0] - b[0] - if(d) { return d } - return a[1] - b[1] -} - -function compareEnd(a, b) { - var d = a[1] - b[1] - if(d) { return d } - return a[0] - b[0] -} - -function createIntervalTree(intervals) { - if(intervals.length === 0) { - return null - } - var pts = [] - for(var i=0; i>1] - - var leftIntervals = [] - var rightIntervals = [] - var centerIntervals = [] - for(var i=0; i b[1][0]) { - bl = b[1] - br = b[0] - } else { - var alo = Math.min(a[0][1], a[1][1]) - var ahi = Math.max(a[0][1], a[1][1]) - var blo = Math.min(b[0][1], b[1][1]) - var bhi = Math.max(b[0][1], b[1][1]) - if(ahi < blo) { - return ahi - blo - } - if(alo > bhi) { - return alo - bhi - } - return ahi - bhi - } - var al, ar - if(a[0][1] < a[1][1]) { - al = a[0] - ar = a[1] - } else { - al = a[1] - ar = a[0] - } - var d = orient(br, bl, al) - if(d) { - return d - } - d = orient(br, bl, ar) - if(d) { - return d - } - return ar - br -} - -function orderSegments(b, a) { - var al, ar - if(a[0][0] < a[1][0]) { - al = a[0] - ar = a[1] - } else if(a[0][0] > a[1][0]) { - al = a[1] - ar = a[0] - } else { - return horizontalOrder(a, b) - } - var bl, br - if(b[0][0] < b[1][0]) { - bl = b[0] - br = b[1] - } else if(b[0][0] > b[1][0]) { - bl = b[1] - br = b[0] - } else { - return -horizontalOrder(b, a) - } - var d1 = orient(al, ar, br) - var d2 = orient(al, ar, bl) - if(d1 < 0) { - if(d2 <= 0) { - return d1 - } - } else if(d1 > 0) { - if(d2 >= 0) { - return d1 - } - } else if(d2) { - return d2 - } - d1 = orient(br, bl, ar) - d2 = orient(br, bl, al) - if(d1 < 0) { - if(d2 <= 0) { - return d1 - } - } else if(d1 > 0) { - if(d2 >= 0) { - return d1 - } - } else if(d2) { - return d2 - } - return ar[0] - br[0] -} -},{"robust-orientation":1127}],387:[function(require,module,exports){ -"use strict" - -module.exports = createRBTree - -var RED = 0 -var BLACK = 1 - -function RBNode(color, key, value, left, right, count) { - this._color = color - this.key = key - this.value = value - this.left = left - this.right = right - this._count = count -} - -function cloneNode(node) { - return new RBNode(node._color, node.key, node.value, node.left, node.right, node._count) -} - -function repaint(color, node) { - return new RBNode(color, node.key, node.value, node.left, node.right, node._count) -} - -function recount(node) { - node._count = 1 + (node.left ? node.left._count : 0) + (node.right ? node.right._count : 0) -} - -function RedBlackTree(compare, root) { - this._compare = compare - this.root = root -} - -var proto = RedBlackTree.prototype - -Object.defineProperty(proto, "keys", { - get: function() { - var result = [] - this.forEach(function(k,v) { - result.push(k) - }) - return result - } -}) - -Object.defineProperty(proto, "values", { - get: function() { - var result = [] - this.forEach(function(k,v) { - result.push(v) - }) - return result - } -}) - -//Returns the number of nodes in the tree -Object.defineProperty(proto, "length", { - get: function() { - if(this.root) { - return this.root._count - } - return 0 - } -}) - -//Insert a new item into the tree -proto.insert = function(key, value) { - var cmp = this._compare - //Find point to insert new node at - var n = this.root - var n_stack = [] - var d_stack = [] - while(n) { - var d = cmp(key, n.key) - n_stack.push(n) - d_stack.push(d) - if(d <= 0) { - n = n.left - } else { - n = n.right - } - } - //Rebuild path to leaf node - n_stack.push(new RBNode(RED, key, value, null, null, 1)) - for(var s=n_stack.length-2; s>=0; --s) { - var n = n_stack[s] - if(d_stack[s] <= 0) { - n_stack[s] = new RBNode(n._color, n.key, n.value, n_stack[s+1], n.right, n._count+1) - } else { - n_stack[s] = new RBNode(n._color, n.key, n.value, n.left, n_stack[s+1], n._count+1) - } - } - //Rebalance tree using rotations - //console.log("start insert", key, d_stack) - for(var s=n_stack.length-1; s>1; --s) { - var p = n_stack[s-1] - var n = n_stack[s] - if(p._color === BLACK || n._color === BLACK) { - break - } - var pp = n_stack[s-2] - if(pp.left === p) { - if(p.left === n) { - var y = pp.right - if(y && y._color === RED) { - //console.log("LLr") - p._color = BLACK - pp.right = repaint(BLACK, y) - pp._color = RED - s -= 1 - } else { - //console.log("LLb") - pp._color = RED - pp.left = p.right - p._color = BLACK - p.right = pp - n_stack[s-2] = p - n_stack[s-1] = n - recount(pp) - recount(p) - if(s >= 3) { - var ppp = n_stack[s-3] - if(ppp.left === pp) { - ppp.left = p - } else { - ppp.right = p - } - } - break - } - } else { - var y = pp.right - if(y && y._color === RED) { - //console.log("LRr") - p._color = BLACK - pp.right = repaint(BLACK, y) - pp._color = RED - s -= 1 - } else { - //console.log("LRb") - p.right = n.left - pp._color = RED - pp.left = n.right - n._color = BLACK - n.left = p - n.right = pp - n_stack[s-2] = n - n_stack[s-1] = p - recount(pp) - recount(p) - recount(n) - if(s >= 3) { - var ppp = n_stack[s-3] - if(ppp.left === pp) { - ppp.left = n - } else { - ppp.right = n - } - } - break - } - } - } else { - if(p.right === n) { - var y = pp.left - if(y && y._color === RED) { - //console.log("RRr", y.key) - p._color = BLACK - pp.left = repaint(BLACK, y) - pp._color = RED - s -= 1 - } else { - //console.log("RRb") - pp._color = RED - pp.right = p.left - p._color = BLACK - p.left = pp - n_stack[s-2] = p - n_stack[s-1] = n - recount(pp) - recount(p) - if(s >= 3) { - var ppp = n_stack[s-3] - if(ppp.right === pp) { - ppp.right = p - } else { - ppp.left = p - } - } - break - } - } else { - var y = pp.left - if(y && y._color === RED) { - //console.log("RLr") - p._color = BLACK - pp.left = repaint(BLACK, y) - pp._color = RED - s -= 1 - } else { - //console.log("RLb") - p.left = n.right - pp._color = RED - pp.right = n.left - n._color = BLACK - n.right = p - n.left = pp - n_stack[s-2] = n - n_stack[s-1] = p - recount(pp) - recount(p) - recount(n) - if(s >= 3) { - var ppp = n_stack[s-3] - if(ppp.right === pp) { - ppp.right = n - } else { - ppp.left = n - } - } - break - } - } - } - } - //Return new tree - n_stack[0]._color = BLACK - return new RedBlackTree(cmp, n_stack[0]) -} - - -//Visit all nodes inorder -function doVisitFull(visit, node) { - if(node.left) { - var v = doVisitFull(visit, node.left) - if(v) { return v } - } - var v = visit(node.key, node.value) - if(v) { return v } - if(node.right) { - return doVisitFull(visit, node.right) - } -} - -//Visit half nodes in order -function doVisitHalf(lo, compare, visit, node) { - var l = compare(lo, node.key) - if(l <= 0) { - if(node.left) { - var v = doVisitHalf(lo, compare, visit, node.left) - if(v) { return v } - } - var v = visit(node.key, node.value) - if(v) { return v } - } - if(node.right) { - return doVisitHalf(lo, compare, visit, node.right) - } -} - -//Visit all nodes within a range -function doVisit(lo, hi, compare, visit, node) { - var l = compare(lo, node.key) - var h = compare(hi, node.key) - var v - if(l <= 0) { - if(node.left) { - v = doVisit(lo, hi, compare, visit, node.left) - if(v) { return v } - } - if(h > 0) { - v = visit(node.key, node.value) - if(v) { return v } - } - } - if(h > 0 && node.right) { - return doVisit(lo, hi, compare, visit, node.right) - } -} - - -proto.forEach = function rbTreeForEach(visit, lo, hi) { - if(!this.root) { - return - } - switch(arguments.length) { - case 1: - return doVisitFull(visit, this.root) - break - - case 2: - return doVisitHalf(lo, this._compare, visit, this.root) - break - - case 3: - if(this._compare(lo, hi) >= 0) { - return - } - return doVisit(lo, hi, this._compare, visit, this.root) - break - } -} - -//First item in list -Object.defineProperty(proto, "begin", { - get: function() { - var stack = [] - var n = this.root - while(n) { - stack.push(n) - n = n.left - } - return new RedBlackTreeIterator(this, stack) - } -}) - -//Last item in list -Object.defineProperty(proto, "end", { - get: function() { - var stack = [] - var n = this.root - while(n) { - stack.push(n) - n = n.right - } - return new RedBlackTreeIterator(this, stack) - } -}) - -//Find the ith item in the tree -proto.at = function(idx) { - if(idx < 0) { - return new RedBlackTreeIterator(this, []) - } - var n = this.root - var stack = [] - while(true) { - stack.push(n) - if(n.left) { - if(idx < n.left._count) { - n = n.left - continue - } - idx -= n.left._count - } - if(!idx) { - return new RedBlackTreeIterator(this, stack) - } - idx -= 1 - if(n.right) { - if(idx >= n.right._count) { - break - } - n = n.right - } else { - break - } - } - return new RedBlackTreeIterator(this, []) -} - -proto.ge = function(key) { - var cmp = this._compare - var n = this.root - var stack = [] - var last_ptr = 0 - while(n) { - var d = cmp(key, n.key) - stack.push(n) - if(d <= 0) { - last_ptr = stack.length - } - if(d <= 0) { - n = n.left - } else { - n = n.right - } - } - stack.length = last_ptr - return new RedBlackTreeIterator(this, stack) -} - -proto.gt = function(key) { - var cmp = this._compare - var n = this.root - var stack = [] - var last_ptr = 0 - while(n) { - var d = cmp(key, n.key) - stack.push(n) - if(d < 0) { - last_ptr = stack.length - } - if(d < 0) { - n = n.left - } else { - n = n.right - } - } - stack.length = last_ptr - return new RedBlackTreeIterator(this, stack) -} - -proto.lt = function(key) { - var cmp = this._compare - var n = this.root - var stack = [] - var last_ptr = 0 - while(n) { - var d = cmp(key, n.key) - stack.push(n) - if(d > 0) { - last_ptr = stack.length - } - if(d <= 0) { - n = n.left - } else { - n = n.right - } - } - stack.length = last_ptr - return new RedBlackTreeIterator(this, stack) -} - -proto.le = function(key) { - var cmp = this._compare - var n = this.root - var stack = [] - var last_ptr = 0 - while(n) { - var d = cmp(key, n.key) - stack.push(n) - if(d >= 0) { - last_ptr = stack.length - } - if(d < 0) { - n = n.left - } else { - n = n.right - } - } - stack.length = last_ptr - return new RedBlackTreeIterator(this, stack) -} - -//Finds the item with key if it exists -proto.find = function(key) { - var cmp = this._compare - var n = this.root - var stack = [] - while(n) { - var d = cmp(key, n.key) - stack.push(n) - if(d === 0) { - return new RedBlackTreeIterator(this, stack) - } - if(d <= 0) { - n = n.left - } else { - n = n.right - } - } - return new RedBlackTreeIterator(this, []) -} - -//Removes item with key from tree -proto.remove = function(key) { - var iter = this.find(key) - if(iter) { - return iter.remove() - } - return this -} - -//Returns the item at `key` -proto.get = function(key) { - var cmp = this._compare - var n = this.root - while(n) { - var d = cmp(key, n.key) - if(d === 0) { - return n.value - } - if(d <= 0) { - n = n.left - } else { - n = n.right - } - } - return -} - -//Iterator for red black tree -function RedBlackTreeIterator(tree, stack) { - this.tree = tree - this._stack = stack -} - -var iproto = RedBlackTreeIterator.prototype - -//Test if iterator is valid -Object.defineProperty(iproto, "valid", { - get: function() { - return this._stack.length > 0 - } -}) - -//Node of the iterator -Object.defineProperty(iproto, "node", { - get: function() { - if(this._stack.length > 0) { - return this._stack[this._stack.length-1] - } - return null - }, - enumerable: true -}) - -//Makes a copy of an iterator -iproto.clone = function() { - return new RedBlackTreeIterator(this.tree, this._stack.slice()) -} - -//Swaps two nodes -function swapNode(n, v) { - n.key = v.key - n.value = v.value - n.left = v.left - n.right = v.right - n._color = v._color - n._count = v._count -} - -//Fix up a double black node in a tree -function fixDoubleBlack(stack) { - var n, p, s, z - for(var i=stack.length-1; i>=0; --i) { - n = stack[i] - if(i === 0) { - n._color = BLACK - return - } - //console.log("visit node:", n.key, i, stack[i].key, stack[i-1].key) - p = stack[i-1] - if(p.left === n) { - //console.log("left child") - s = p.right - if(s.right && s.right._color === RED) { - //console.log("case 1: right sibling child red") - s = p.right = cloneNode(s) - z = s.right = cloneNode(s.right) - p.right = s.left - s.left = p - s.right = z - s._color = p._color - n._color = BLACK - p._color = BLACK - z._color = BLACK - recount(p) - recount(s) - if(i > 1) { - var pp = stack[i-2] - if(pp.left === p) { - pp.left = s - } else { - pp.right = s - } - } - stack[i-1] = s - return - } else if(s.left && s.left._color === RED) { - //console.log("case 1: left sibling child red") - s = p.right = cloneNode(s) - z = s.left = cloneNode(s.left) - p.right = z.left - s.left = z.right - z.left = p - z.right = s - z._color = p._color - p._color = BLACK - s._color = BLACK - n._color = BLACK - recount(p) - recount(s) - recount(z) - if(i > 1) { - var pp = stack[i-2] - if(pp.left === p) { - pp.left = z - } else { - pp.right = z - } - } - stack[i-1] = z - return - } - if(s._color === BLACK) { - if(p._color === RED) { - //console.log("case 2: black sibling, red parent", p.right.value) - p._color = BLACK - p.right = repaint(RED, s) - return - } else { - //console.log("case 2: black sibling, black parent", p.right.value) - p.right = repaint(RED, s) - continue - } - } else { - //console.log("case 3: red sibling") - s = cloneNode(s) - p.right = s.left - s.left = p - s._color = p._color - p._color = RED - recount(p) - recount(s) - if(i > 1) { - var pp = stack[i-2] - if(pp.left === p) { - pp.left = s - } else { - pp.right = s - } - } - stack[i-1] = s - stack[i] = p - if(i+1 < stack.length) { - stack[i+1] = n - } else { - stack.push(n) - } - i = i+2 - } - } else { - //console.log("right child") - s = p.left - if(s.left && s.left._color === RED) { - //console.log("case 1: left sibling child red", p.value, p._color) - s = p.left = cloneNode(s) - z = s.left = cloneNode(s.left) - p.left = s.right - s.right = p - s.left = z - s._color = p._color - n._color = BLACK - p._color = BLACK - z._color = BLACK - recount(p) - recount(s) - if(i > 1) { - var pp = stack[i-2] - if(pp.right === p) { - pp.right = s - } else { - pp.left = s - } - } - stack[i-1] = s - return - } else if(s.right && s.right._color === RED) { - //console.log("case 1: right sibling child red") - s = p.left = cloneNode(s) - z = s.right = cloneNode(s.right) - p.left = z.right - s.right = z.left - z.right = p - z.left = s - z._color = p._color - p._color = BLACK - s._color = BLACK - n._color = BLACK - recount(p) - recount(s) - recount(z) - if(i > 1) { - var pp = stack[i-2] - if(pp.right === p) { - pp.right = z - } else { - pp.left = z - } - } - stack[i-1] = z - return - } - if(s._color === BLACK) { - if(p._color === RED) { - //console.log("case 2: black sibling, red parent") - p._color = BLACK - p.left = repaint(RED, s) - return - } else { - //console.log("case 2: black sibling, black parent") - p.left = repaint(RED, s) - continue - } - } else { - //console.log("case 3: red sibling") - s = cloneNode(s) - p.left = s.right - s.right = p - s._color = p._color - p._color = RED - recount(p) - recount(s) - if(i > 1) { - var pp = stack[i-2] - if(pp.right === p) { - pp.right = s - } else { - pp.left = s - } - } - stack[i-1] = s - stack[i] = p - if(i+1 < stack.length) { - stack[i+1] = n - } else { - stack.push(n) - } - i = i+2 - } - } - } -} - -//Removes item at iterator from tree -iproto.remove = function() { - var stack = this._stack - if(stack.length === 0) { - return this.tree - } - //First copy path to node - var cstack = new Array(stack.length) - var n = stack[stack.length-1] - cstack[cstack.length-1] = new RBNode(n._color, n.key, n.value, n.left, n.right, n._count) - for(var i=stack.length-2; i>=0; --i) { - var n = stack[i] - if(n.left === stack[i+1]) { - cstack[i] = new RBNode(n._color, n.key, n.value, cstack[i+1], n.right, n._count) - } else { - cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) - } - } - - //Get node - n = cstack[cstack.length-1] - //console.log("start remove: ", n.value) - - //If not leaf, then swap with previous node - if(n.left && n.right) { - //console.log("moving to leaf") - - //First walk to previous leaf - var split = cstack.length - n = n.left - while(n.right) { - cstack.push(n) - n = n.right - } - //Copy path to leaf - var v = cstack[split-1] - cstack.push(new RBNode(n._color, v.key, v.value, n.left, n.right, n._count)) - cstack[split-1].key = n.key - cstack[split-1].value = n.value - - //Fix up stack - for(var i=cstack.length-2; i>=split; --i) { - n = cstack[i] - cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) - } - cstack[split-1].left = cstack[split] - } - //console.log("stack=", cstack.map(function(v) { return v.value })) - - //Remove leaf node - n = cstack[cstack.length-1] - if(n._color === RED) { - //Easy case: removing red leaf - //console.log("RED leaf") - var p = cstack[cstack.length-2] - if(p.left === n) { - p.left = null - } else if(p.right === n) { - p.right = null - } - cstack.pop() - for(var i=0; i 0) { - return this._stack[this._stack.length-1].key - } - return - }, - enumerable: true -}) - -//Returns value -Object.defineProperty(iproto, "value", { - get: function() { - if(this._stack.length > 0) { - return this._stack[this._stack.length-1].value - } - return - }, - enumerable: true -}) - - -//Returns the position of this iterator in the sorted list -Object.defineProperty(iproto, "index", { - get: function() { - var idx = 0 - var stack = this._stack - if(stack.length === 0) { - var r = this.tree.root - if(r) { - return r._count - } - return 0 - } else if(stack[stack.length-1].left) { - idx = stack[stack.length-1].left._count - } - for(var s=stack.length-2; s>=0; --s) { - if(stack[s+1] === stack[s].right) { - ++idx - if(stack[s].left) { - idx += stack[s].left._count - } - } - } - return idx - }, - enumerable: true -}) - -//Advances iterator to next element in list -iproto.next = function() { - var stack = this._stack - if(stack.length === 0) { - return - } - var n = stack[stack.length-1] - if(n.right) { - n = n.right - while(n) { - stack.push(n) - n = n.left - } - } else { - stack.pop() - while(stack.length > 0 && stack[stack.length-1].right === n) { - n = stack[stack.length-1] - stack.pop() - } - } -} - -//Checks if iterator is at end of tree -Object.defineProperty(iproto, "hasNext", { - get: function() { - var stack = this._stack - if(stack.length === 0) { - return false - } - if(stack[stack.length-1].right) { - return true - } - for(var s=stack.length-1; s>0; --s) { - if(stack[s-1].left === stack[s]) { - return true - } - } - return false - } -}) - -//Update value -iproto.update = function(value) { - var stack = this._stack - if(stack.length === 0) { - throw new Error("Can't update empty node!") - } - var cstack = new Array(stack.length) - var n = stack[stack.length-1] - cstack[cstack.length-1] = new RBNode(n._color, n.key, value, n.left, n.right, n._count) - for(var i=stack.length-2; i>=0; --i) { - n = stack[i] - if(n.left === stack[i+1]) { - cstack[i] = new RBNode(n._color, n.key, n.value, cstack[i+1], n.right, n._count) - } else { - cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) - } - } - return new RedBlackTree(this.tree._compare, cstack[0]) -} - -//Moves iterator backward one element -iproto.prev = function() { - var stack = this._stack - if(stack.length === 0) { - return - } - var n = stack[stack.length-1] - if(n.left) { - n = n.left - while(n) { - stack.push(n) - n = n.right - } - } else { - stack.pop() - while(stack.length > 0 && stack[stack.length-1].left === n) { - n = stack[stack.length-1] - stack.pop() - } - } -} - -//Checks if iterator is at start of tree -Object.defineProperty(iproto, "hasPrev", { - get: function() { - var stack = this._stack - if(stack.length === 0) { - return false - } - if(stack[stack.length-1].left) { - return true - } - for(var s=stack.length-1; s>0; --s) { - if(stack[s-1].right === stack[s]) { - return true - } - } - return false - } -}) - -//Default comparison function -function defaultCompare(a, b) { - if(a < b) { - return -1 - } - if(a > b) { - return 1 - } - return 0 -} - -//Build a tree -function createRBTree(compare) { - return new RedBlackTree(compare || defaultCompare, null) -} -},{}],388:[function(require,module,exports){ -"use strict" - -module.exports = createSlabDecomposition - -var bounds = require("binary-search-bounds") -var createRBTree = require("functional-red-black-tree") -var orient = require("robust-orientation") -var orderSegments = require("./lib/order-segments") - -function SlabDecomposition(slabs, coordinates, horizontal) { - this.slabs = slabs - this.coordinates = coordinates - this.horizontal = horizontal -} - -var proto = SlabDecomposition.prototype - -function compareHorizontal(e, y) { - return e.y - y -} - -function searchBucket(root, p) { - var lastNode = null - while(root) { - var seg = root.key - var l, r - if(seg[0][0] < seg[1][0]) { - l = seg[0] - r = seg[1] - } else { - l = seg[1] - r = seg[0] - } - var o = orient(l, r, p) - if(o < 0) { - root = root.left - } else if(o > 0) { - if(p[0] !== seg[1][0]) { - lastNode = root - root = root.right - } else { - var val = searchBucket(root.right, p) - if(val) { - return val - } - root = root.left - } - } else { - if(p[0] !== seg[1][0]) { - return root - } else { - var val = searchBucket(root.right, p) - if(val) { - return val - } - root = root.left - } - } - } - return lastNode -} - -proto.castUp = function(p) { - var bucket = bounds.le(this.coordinates, p[0]) - if(bucket < 0) { - return -1 - } - var root = this.slabs[bucket] - var hitNode = searchBucket(this.slabs[bucket], p) - var lastHit = -1 - if(hitNode) { - lastHit = hitNode.value - } - //Edge case: need to handle horizontal segments (sucks) - if(this.coordinates[bucket] === p[0]) { - var lastSegment = null - if(hitNode) { - lastSegment = hitNode.key - } - if(bucket > 0) { - var otherHitNode = searchBucket(this.slabs[bucket-1], p) - if(otherHitNode) { - if(lastSegment) { - if(orderSegments(otherHitNode.key, lastSegment) > 0) { - lastSegment = otherHitNode.key - lastHit = otherHitNode.value - } - } else { - lastHit = otherHitNode.value - lastSegment = otherHitNode.key - } - } - } - var horiz = this.horizontal[bucket] - if(horiz.length > 0) { - var hbucket = bounds.ge(horiz, p[1], compareHorizontal) - if(hbucket < horiz.length) { - var e = horiz[hbucket] - if(p[1] === e.y) { - if(e.closed) { - return e.index - } else { - while(hbucket < horiz.length-1 && horiz[hbucket+1].y === p[1]) { - hbucket = hbucket+1 - e = horiz[hbucket] - if(e.closed) { - return e.index - } - } - if(e.y === p[1] && !e.start) { - hbucket = hbucket+1 - if(hbucket >= horiz.length) { - return lastHit - } - e = horiz[hbucket] - } - } - } - //Check if e is above/below last segment - if(e.start) { - if(lastSegment) { - var o = orient(lastSegment[0], lastSegment[1], [p[0], e.y]) - if(lastSegment[0][0] > lastSegment[1][0]) { - o = -o - } - if(o > 0) { - lastHit = e.index - } - } else { - lastHit = e.index - } - } else if(e.y !== p[1]) { - lastHit = e.index - } - } - } - } - return lastHit -} - -function IntervalSegment(y, index, start, closed) { - this.y = y - this.index = index - this.start = start - this.closed = closed -} - -function Event(x, segment, create, index) { - this.x = x - this.segment = segment - this.create = create - this.index = index -} - - -function createSlabDecomposition(segments) { - var numSegments = segments.length - var numEvents = 2 * numSegments - var events = new Array(numEvents) - for(var i=0; i 0 && coordinates[bucket] === p[0]) { - root = slabs[bucket-1] - } else { - return 1 - } - } - var lastOrientation = 1 - while(root) { - var s = root.key - var o = orient(p, s[0], s[1]) - if(s[0][0] < s[1][0]) { - if(o < 0) { - root = root.left - } else if(o > 0) { - lastOrientation = -1 - root = root.right - } else { - return 0 - } - } else { - if(o > 0) { - root = root.left - } else if(o < 0) { - lastOrientation = 1 - root = root.right - } else { - return 0 - } - } - } - return lastOrientation - } -} - -function classifyEmpty(p) { - return 1 -} - -function createClassifyVertical(testVertical) { - return function classify(p) { - if(testVertical(p[0], p[1])) { - return 0 - } - return 1 - } -} - -function createClassifyPointDegen(testVertical, testNormal) { - return function classify(p) { - if(testVertical(p[0], p[1])) { - return 0 - } - return testNormal(p) - } -} - -function preprocessPolygon(loops) { - //Compute number of loops - var numLoops = loops.length - - //Unpack segments - var segments = [] - var vsegments = [] - var ptr = 0 - for(var i=0; i 0 - } - - //Extract all clockwise faces - faces = faces.filter(ccw) - - //Detect which loops are contained in one another to handle parent-of relation - var numFaces = faces.length - var parent = new Array(numFaces) - var containment = new Array(numFaces) - for(var i=0; i 0) { - var top = toVisit.pop() - var nbhd = fadj[top] - uniq(nbhd, function(a,b) { - return a-b - }) - var nnbhr = nbhd.length - var p = parity[top] - var polyline - if(p === 0) { - var c = faces[top] - polyline = [c] - } - for(var i=0; i= 0) { - continue - } - parity[f] = p^1 - toVisit.push(f) - if(p === 0) { - var c = faces[f] - if(!sharedBoundary(c)) { - c.reverse() - polyline.push(c) - } - } - } - if(p === 0) { - result.push(polyline) - } - } - - return result -} -},{"./lib/trim-leaves":376,"edges-to-adjacency-list":377,"planar-dual":378,"point-in-big-polygon":389,"robust-sum":390,"two-product":391,"uniq":392}],394:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],395:[function(require,module,exports){ -"use strict"; "use restrict"; - -module.exports = UnionFind; - -function UnionFind(count) { - this.roots = new Array(count); - this.ranks = new Array(count); - - for(var i=0; i> 1 - } - return (i >> 1) - 1 - } - - //Bubble element i down the heap - function heapDown(i) { - var w = heapWeight(i) - while(true) { - var tw = w - var left = 2*i + 1 - var right = 2*(i + 1) - var next = i - if(left < heapCount) { - var lw = heapWeight(left) - if(lw < tw) { - next = left - tw = lw - } - } - if(right < heapCount) { - var rw = heapWeight(right) - if(rw < tw) { - next = right - } - } - if(next === i) { - return i - } - heapSwap(i, next) - i = next - } - } - - //Bubbles element i up the heap - function heapUp(i) { - var w = heapWeight(i) - while(i > 0) { - var parent = heapParent(i) - if(parent >= 0) { - var pw = heapWeight(parent) - if(w < pw) { - heapSwap(i, parent) - i = parent - continue - } - } - return i - } - } - - //Pop minimum element - function heapPop() { - if(heapCount > 0) { - var head = heap[0] - heapSwap(0, heapCount-1) - heapCount -= 1 - heapDown(0) - return head - } - return -1 - } - - //Update heap item i - function heapUpdate(i, w) { - var a = heap[i] - if(weights[a] === w) { - return i - } - weights[a] = -Infinity - heapUp(i) - heapPop() - weights[a] = w - heapCount += 1 - return heapUp(heapCount-1) - } - - //Kills a vertex (assume vertex already removed from heap) - function kill(i) { - if(dead[i]) { - return - } - //Kill vertex - dead[i] = true - //Fixup topology - var s = inv[i] - var t = outv[i] - if(inv[t] >= 0) { - inv[t] = s - } - if(outv[s] >= 0) { - outv[s] = t - } - - //Update weights on s and t - if(index[s] >= 0) { - heapUpdate(index[s], computeWeight(s)) - } - if(index[t] >= 0) { - heapUpdate(index[t], computeWeight(t)) - } - } - - //Initialize weights and heap - var heap = [] - var index = new Array(n) - for(var i=0; i>1; i>=0; --i) { - heapDown(i) - } - - //Kill vertices - while(true) { - var hmin = heapPop() - if((hmin < 0) || (weights[hmin] > minArea)) { - break - } - kill(hmin) - } - - //Build collapsed vertex table - var npositions = [] - for(var i=0; i= 0 && tout >= 0 && tin !== tout) { - var cin = index[tin] - var cout = index[tout] - if(cin !== cout) { - ncells.push([ cin, cout ]) - } - } - }) - - //Normalize result - sc.unique(sc.normalize(ncells)) - - //Return final list of cells - return { - positions: npositions, - edges: ncells - } -} -},{"robust-orientation":1127,"simplicial-complex":396}],398:[function(require,module,exports){ -"use strict" - -var pool = require("typedarray-pool") - -module.exports = createSurfaceExtractor - -//Helper macros -function array(i) { - return "a" + i -} -function data(i) { - return "d" + i -} -function cube(i,bitmask) { - return "c" + i + "_" + bitmask -} -function shape(i) { - return "s" + i -} -function stride(i,j) { - return "t" + i + "_" + j -} -function offset(i) { - return "o" + i -} -function scalar(i) { - return "x" + i -} -function pointer(i) { - return "p" + i -} -function delta(i,bitmask) { - return "d" + i + "_" + bitmask -} -function index(i) { - return "i" + i -} -function step(i,j) { - return "u" + i + "_" + j -} -function pcube(bitmask) { - return "b" + bitmask -} -function qcube(bitmask) { - return "y" + bitmask -} -function pdelta(bitmask) { - return "e" + bitmask -} -function vert(i) { - return "v" + i -} -var VERTEX_IDS = "V" -var PHASES = "P" -var VERTEX_COUNT = "N" -var POOL_SIZE = "Q" -var POINTER = "X" -var TEMPORARY = "T" - -function permBitmask(dimension, mask, order) { - var r = 0 - for(var i=0; i 0) { - stepVal.push(stride(i, order[j-1]) + "*" + shape(order[j-1]) ) - } - vars.push(step(i,order[j]) + "=(" + stepVal.join("-") + ")|0") - } - } - //Create index variables - for(var i=0; i=0; --i) { - sizeVariable.push(shape(order[i])) - } - //Previous phases and vertex_ids - vars.push(POOL_SIZE + "=(" + sizeVariable.join("*") + ")|0", - PHASES + "=mallocUint32(" + POOL_SIZE + ")", - VERTEX_IDS + "=mallocUint32(" + POOL_SIZE + ")", - POINTER + "=0") - //Create cube variables for phases - vars.push(pcube(0) + "=0") - for(var j=1; j<(1<=0; --i) { - forLoopBegin(i, 0) - } - var phaseFuncArgs = [] - for(var i=0; i0; k=(k-1)&subset) { - faceArgs.push(VERTEX_IDS + "[" + POINTER + "+" + pdelta(k) + "]") - } - faceArgs.push(vert(0)) - for(var k=0; k0){", - index(order[i]), "=1;") - createLoop(i-1, mask|(1< 0") - } - if(typeof args.vertex !== "function") { - error("Must specify vertex creation function") - } - if(typeof args.cell !== "function") { - error("Must specify cell creation function") - } - if(typeof args.phase !== "function") { - error("Must specify phase function") - } - var getters = args.getters || [] - var typesig = new Array(arrays) - for(var i=0; i= 0) { - typesig[i] = true - } else { - typesig[i] = false - } - } - return compileSurfaceProcedure( - args.vertex, - args.cell, - args.phase, - scalars, - order, - typesig) -} -},{"typedarray-pool":401}],399:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],400:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],401:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":399,"buffer":33,"dup":187}],402:[function(require,module,exports){ -// transliterated from the python snippet here: -// http://en.wikipedia.org/wiki/Lanczos_approximation - -var g = 7; -var p = [ - 0.99999999999980993, - 676.5203681218851, - -1259.1392167224028, - 771.32342877765313, - -176.61502916214059, - 12.507343278686905, - -0.13857109526572012, - 9.9843695780195716e-6, - 1.5056327351493116e-7 -]; - -var g_ln = 607/128; -var p_ln = [ - 0.99999999999999709182, - 57.156235665862923517, - -59.597960355475491248, - 14.136097974741747174, - -0.49191381609762019978, - 0.33994649984811888699e-4, - 0.46523628927048575665e-4, - -0.98374475304879564677e-4, - 0.15808870322491248884e-3, - -0.21026444172410488319e-3, - 0.21743961811521264320e-3, - -0.16431810653676389022e-3, - 0.84418223983852743293e-4, - -0.26190838401581408670e-4, - 0.36899182659531622704e-5 -]; - -// Spouge approximation (suitable for large arguments) -function lngamma(z) { - - if(z < 0) return Number('0/0'); - var x = p_ln[0]; - for(var i = p_ln.length - 1; i > 0; --i) x += p_ln[i] / (z + i); - var t = z + g_ln + 0.5; - return .5*Math.log(2*Math.PI)+(z+.5)*Math.log(t)-t+Math.log(x)-Math.log(z); -} - -module.exports = function gamma (z) { - if (z < 0.5) { - return Math.PI / (Math.sin(Math.PI * z) * gamma(1 - z)); - } - else if(z > 100) return Math.exp(lngamma(z)); - else { - z -= 1; - var x = p[0]; - for (var i = 1; i < g + 2; i++) { - x += p[i] / (z + i); - } - var t = z + g + 0.5; - - return Math.sqrt(2 * Math.PI) - * Math.pow(t, z + 0.5) - * Math.exp(-t) - * x - ; - } + return errors; }; -module.exports.log = lngamma; +},{"../error/validation_error":280,"../util/get_type":282,"../util/unbundle_jsonlint":283,"./validate_enum":289}],291:[function(require,module,exports){ +'use strict'; -},{}],403:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],404:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],405:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":403,"buffer":33,"dup":187}],406:[function(require,module,exports){ -"use strict" +var ValidationError = require('../error/validation_error'); +var getType = require('../util/get_type'); +var validate = require('./validate'); +var validateObject = require('./validate_object'); +var validateArray = require('./validate_array'); +var validateNumber = require('./validate_number'); -module.exports = permutationSign +module.exports = function validateFunction(options) { + var functionValueSpec = options.valueSpec; + var stopKeyType; -var BRUTE_FORCE_CUTOFF = 32 + var isPropertyFunction = options.value.property !== undefined || stopKeyType === 'object'; + var isZoomFunction = options.value.property === undefined || stopKeyType === 'object'; -var pool = require("typedarray-pool") + var errors = validateObject({ + key: options.key, + value: options.value, + valueSpec: options.styleSpec.function, + style: options.style, + styleSpec: options.styleSpec, + objectElementValidators: { stops: validateFunctionStops } + }); -function permutationSign(p) { - var n = p.length - if(n < BRUTE_FORCE_CUTOFF) { - //Use quadratic algorithm for small n - var sgn = 1 - for(var i=0; i= 8) { + if (isPropertyFunction && !options.valueSpec['property-function']) { + errors.push(new ValidationError(options.key, options.value, 'property functions not supported')); + } else if (isZoomFunction && !options.valueSpec['zoom-function']) { + errors.push(new ValidationError(options.key, options.value, 'zoom functions not supported')); + } } - return sgn - } else { - //Otherwise use linear time algorithm - var visited = pool.mallocUint8(n) - for(var i=0; i0; --i) { - t = pinv[i] - s = p[i] - p[i] = p[t] - p[t] = s - pinv[i] = pinv[s] - pinv[s] = t - r = (r + s) * i - } - pool.freeUint32(pinv) - pool.freeUint32(p) - return r -} + if (getType(value) !== 'array') { + return [new ValidationError(key, value, 'array expected, %s found', getType(value))]; + } -function unrank(n, r, p) { - switch(n) { - case 0: - if(p) { return p } - return [] - case 1: - if(p) { - p[0] = 0 - return p - } else { - return [0] - } - case 2: - if(p) { - if(r) { - p[0] = 0 - p[1] = 1 + if (value.length !== 2) { + return [new ValidationError(key, value, 'array length %d expected, length %d found', 2, value.length)]; + } + + var type = getType(value[0]); + if (!stopKeyType) stopKeyType = type; + if (type !== stopKeyType) { + return [new ValidationError(key, value, '%s stop key type must match previous stop key type %s', type, stopKeyType)]; + } + + if (type === 'object') { + if (value[0].zoom === undefined) { + return [new ValidationError(key, value, 'object stop key must have zoom')]; + } + if (value[0].value === undefined) { + return [new ValidationError(key, value, 'object stop key must have value')]; + } + errors = errors.concat(validateObject({ + key: key + '[0]', + value: value[0], + valueSpec: { zoom: {} }, + style: options.style, + styleSpec: options.styleSpec, + objectElementValidators: { zoom: validateNumber, value: validateValue } + })); } else { - p[0] = 1 - p[1] = 0 + errors = errors.concat((isZoomFunction ? validateNumber : validateValue)({ + key: key + '[0]', + value: value[0], + valueSpec: {}, + style: options.style, + styleSpec: options.styleSpec + })); } - return p - } else { - return r ? [0,1] : [1,0] - } - default: - break - } - p = p || new Array(n) - var s, t, i, nf=1 - p[0] = 0 - for(i=1; i0; --i) { - s = (r / nf)|0 - r = (r - s * nf)|0 - nf = (nf / i)|0 - t = p[i]|0 - p[i] = p[s]|0 - p[s] = t|0 - } - return p -} -exports.rank = rank -exports.unrank = unrank + errors = errors.concat(validate({ + key: key + '[1]', + value: value[1], + valueSpec: functionValueSpec, + style: options.style, + styleSpec: options.styleSpec + })); -},{"invert-permutation":408,"typedarray-pool":411}],408:[function(require,module,exports){ -"use strict" - -function invertPermutation(pi, result) { - result = result || new Array(pi.length) - for(var i=0; i= 0) !== (_inline_1_db >= 0)) {\n _inline_1_arg2_.push(_inline_1_arg4_[0] + 0.5 + 0.5 * (_inline_1_da + _inline_1_db) / (_inline_1_da - _inline_1_db))\n }\n }", - "args": [{ - "name": "_inline_1_arg0_", - "lvalue": false, - "rvalue": true, - "count": 1 - }, { - "name": "_inline_1_arg1_", - "lvalue": false, - "rvalue": true, - "count": 1 - }, { - "name": "_inline_1_arg2_", - "lvalue": false, - "rvalue": true, - "count": 1 - }, { - "name": "_inline_1_arg3_", - "lvalue": false, - "rvalue": true, - "count": 2 - }, { - "name": "_inline_1_arg4_", - "lvalue": false, - "rvalue": true, - "count": 1 - }], - "thisVars": [], - "localVars": ["_inline_1_da", "_inline_1_db"] - }, - funcName: 'zeroCrossings' -}) - -},{"cwise-compiler":414}],414:[function(require,module,exports){ -arguments[4][73][0].apply(exports,arguments) -},{"./lib/thunk.js":416,"dup":73}],415:[function(require,module,exports){ -arguments[4][74][0].apply(exports,arguments) -},{"dup":74,"uniq":417}],416:[function(require,module,exports){ -arguments[4][75][0].apply(exports,arguments) -},{"./compile.js":415,"dup":75}],417:[function(require,module,exports){ -arguments[4][76][0].apply(exports,arguments) -},{"dup":76}],418:[function(require,module,exports){ -"use strict" - -module.exports = findZeroCrossings - -var core = require("./lib/zc-core") - -function findZeroCrossings(array, level) { - var cross = [] - level = +level || 0.0 - core(array.hi(array.shape[0]-1), cross, level) - return cross -} -},{"./lib/zc-core":413}],419:[function(require,module,exports){ -"use strict" - -module.exports = surfaceNets - -var generateContourExtractor = require("ndarray-extract-contour") -var triangulateCube = require("triangulate-hypercube") -var zeroCrossings = require("zero-crossings") - -function buildSurfaceNets(order, dtype) { - var dimension = order.length - var code = ["'use strict';"] - var funcName = "surfaceNets" + order.join("_") + "d" + dtype - - //Contour extraction function - code.push( - "var contour=genContour({", - "order:[", order.join(), "],", - "scalarArguments: 3,", - "phase:function phaseFunc(p,a,b,c) { return (p > c)|0 },") - if(dtype === "generic") { - code.push("getters:[0],") - } - - //Generate vertex function - var cubeArgs = [] - var extraArgs = [] - for(var i=0; i>>7){") - } - for(var i=0; i<1<<(1< 128) { - if((i%128)===0) { - if(extraFuncs.length > 0) { - currentFunc.push("}}") - } - var efName = "vExtra" + extraFuncs.length - code.push("case ", (i>>>7), ":", efName, "(m&0x7f,", extraArgs.join(), ");break;") - currentFunc = [ - "function ", efName, "(m,", extraArgs.join(), "){switch(m){" - ] - extraFuncs.push(currentFunc) - } - } - currentFunc.push("case ", (i&0x7f), ":") - var crossings = new Array(dimension) - var denoms = new Array(dimension) - var crossingCount = new Array(dimension) - var bias = new Array(dimension) - var totalCrossings = 0 - for(var j=0; j j) { - continue - } - if(!(i&(1< 0) { - cStr = "+" + crossingCount[k] + "*c" - } - var weight = 0.5 * (crossings[k].length / totalCrossings) - var shift = 0.5 + 0.5 * (bias[k] / totalCrossings) - vertexStr.push("d" + k + "-" + shift + "-" + weight + "*(" + crossings[k].join("+") + cStr + ")/(" + denoms[k].join("+") + ")") - - } - } - currentFunc.push("a.push([", vertexStr.join(), "]);", - "break;") - } - code.push("}},") - if(extraFuncs.length > 0) { - currentFunc.push("}}") - } - - //Create face function - var faceArgs = [] - for(var i=0; i<(1<<(dimension-1)); ++i) { - faceArgs.push("v" + i) - } - faceArgs.push("c0", "c1", "p0", "p1", "a", "b", "c") - code.push("cell:function cellFunc(", faceArgs.join(), "){") - - var facets = triangulateCube(dimension-1) - code.push("if(p0){b.push(", - facets.map(function(f) { - return "[" + f.map(function(v) { - return "v" + v - }) + "]" - }).join(), ")}else{b.push(", - facets.map(function(f) { - var e = f.slice() - e.reverse() - return "[" + e.map(function(v) { - return "v" + v - }) + "]" - }).join(), - ")}}});function ", funcName, "(array,level){var verts=[],cells=[];contour(array,verts,cells,level);return {positions:verts,cells:cells};} return ", funcName, ";") - - for(var i=0; i0) { - shapeX += 0.02 - } - } - - var data = new Float32Array(bufferSize) - var ptr = 0 - var xOffset = -0.5 * shapeX - for(var i=0; i= 0)) { - continue - } - - var zeroIntercept = screenBox[i] - - dataBox[i] * (screenBox[i+2] - screenBox[i]) / (dataBox[i+2] - dataBox[i]) - - if(i === 0) { - line.drawLine( - zeroIntercept, screenBox[1], zeroIntercept, screenBox[3], - zeroLineWidth[i], - zeroLineColor[i]) - } else { - line.drawLine( - screenBox[0], zeroIntercept, screenBox[2], zeroIntercept, - zeroLineWidth[i], - zeroLineColor[i]) - } - } - } - - //Draw traces - for(var i=0; i=0; --i) { - this.objects[i].dispose() - } - this.objects.length = 0 - for(var i=this.overlays.length-1; i>=0; --i) { - this.overlays[i].dispose() - } - this.overlays.length = 0 - - this.gl = null -} - -proto.addObject = function(object) { - if(this.objects.indexOf(object) < 0) { - this.objects.push(object) - this.setDirty() - } -} - -proto.removeObject = function(object) { - var objects = this.objects - for(var i=0; i Math.abs(dy)) { - view.rotate(t, 0, 0, -dx * flipX * Math.PI * camera.rotateSpeed / window.innerWidth) - } else { - var kzoom = camera.zoomSpeed * flipY * dy / window.innerHeight * (t - view.lastT()) / 100.0 - view.pan(t, 0, 0, distance * (Math.exp(kzoom) - 1)) - } - }, true) - - return camera -} -},{"3d-view":142,"mouse-change":1091,"mouse-wheel":1095,"right-now":1121}],424:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":427}],425:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],426:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],427:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":425,"buffer":33,"dup":187}],428:[function(require,module,exports){ -arguments[4][193][0].apply(exports,arguments) -},{"dup":193}],429:[function(require,module,exports){ -arguments[4][194][0].apply(exports,arguments) -},{"./do-bind.js":428,"dup":194}],430:[function(require,module,exports){ -arguments[4][195][0].apply(exports,arguments) -},{"./do-bind.js":428,"dup":195}],431:[function(require,module,exports){ -arguments[4][196][0].apply(exports,arguments) -},{"./lib/vao-emulated.js":429,"./lib/vao-native.js":430,"dup":196}],432:[function(require,module,exports){ -// Copyright (C) 2011 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/** - * @fileoverview Install a leaky WeakMap emulation on platforms that - * don't provide a built-in one. - * - *

Assumes that an ES5 platform where, if {@code WeakMap} is - * already present, then it conforms to the anticipated ES6 - * specification. To run this file on an ES5 or almost ES5 - * implementation where the {@code WeakMap} specification does not - * quite conform, run repairES5.js first. - * - *

Even though WeakMapModule is not global, the linter thinks it - * is, which is why it is in the overrides list below. - * - *

NOTE: Before using this WeakMap emulation in a non-SES - * environment, see the note below about hiddenRecord. - * - * @author Mark S. Miller - * @requires crypto, ArrayBuffer, Uint8Array, navigator, console - * @overrides WeakMap, ses, Proxy - * @overrides WeakMapModule - */ - -/** - * This {@code WeakMap} emulation is observably equivalent to the - * ES-Harmony WeakMap, but with leakier garbage collection properties. - * - *

As with true WeakMaps, in this emulation, a key does not - * retain maps indexed by that key and (crucially) a map does not - * retain the keys it indexes. A map by itself also does not retain - * the values associated with that map. - * - *

However, the values associated with a key in some map are - * retained so long as that key is retained and those associations are - * not overridden. For example, when used to support membranes, all - * values exported from a given membrane will live for the lifetime - * they would have had in the absence of an interposed membrane. Even - * when the membrane is revoked, all objects that would have been - * reachable in the absence of revocation will still be reachable, as - * far as the GC can tell, even though they will no longer be relevant - * to ongoing computation. - * - *

The API implemented here is approximately the API as implemented - * in FF6.0a1 and agreed to by MarkM, Andreas Gal, and Dave Herman, - * rather than the offially approved proposal page. TODO(erights): - * upgrade the ecmascript WeakMap proposal page to explain this API - * change and present to EcmaScript committee for their approval. - * - *

The first difference between the emulation here and that in - * FF6.0a1 is the presence of non enumerable {@code get___, has___, - * set___, and delete___} methods on WeakMap instances to represent - * what would be the hidden internal properties of a primitive - * implementation. Whereas the FF6.0a1 WeakMap.prototype methods - * require their {@code this} to be a genuine WeakMap instance (i.e., - * an object of {@code [[Class]]} "WeakMap}), since there is nothing - * unforgeable about the pseudo-internal method names used here, - * nothing prevents these emulated prototype methods from being - * applied to non-WeakMaps with pseudo-internal methods of the same - * names. - * - *

Another difference is that our emulated {@code - * WeakMap.prototype} is not itself a WeakMap. A problem with the - * current FF6.0a1 API is that WeakMap.prototype is itself a WeakMap - * providing ambient mutability and an ambient communications - * channel. Thus, if a WeakMap is already present and has this - * problem, repairES5.js wraps it in a safe wrappper in order to - * prevent access to this channel. (See - * PATCH_MUTABLE_FROZEN_WEAKMAP_PROTO in repairES5.js). - */ - -/** - * If this is a full secureable ES5 platform and the ES-Harmony {@code WeakMap} is - * absent, install an approximate emulation. - * - *

If WeakMap is present but cannot store some objects, use our approximate - * emulation as a wrapper. - * - *

If this is almost a secureable ES5 platform, then WeakMap.js - * should be run after repairES5.js. - * - *

See {@code WeakMap} for documentation of the garbage collection - * properties of this WeakMap emulation. - */ -(function WeakMapModule() { - "use strict"; - - if (typeof ses !== 'undefined' && ses.ok && !ses.ok()) { - // already too broken, so give up - return; - } - - /** - * In some cases (current Firefox), we must make a choice betweeen a - * WeakMap which is capable of using all varieties of host objects as - * keys and one which is capable of safely using proxies as keys. See - * comments below about HostWeakMap and DoubleWeakMap for details. - * - * This function (which is a global, not exposed to guests) marks a - * WeakMap as permitted to do what is necessary to index all host - * objects, at the cost of making it unsafe for proxies. - * - * Do not apply this function to anything which is not a genuine - * fresh WeakMap. - */ - function weakMapPermitHostObjects(map) { - // identity of function used as a secret -- good enough and cheap - if (map.permitHostObjects___) { - map.permitHostObjects___(weakMapPermitHostObjects); - } - } - if (typeof ses !== 'undefined') { - ses.weakMapPermitHostObjects = weakMapPermitHostObjects; - } - - // IE 11 has no Proxy but has a broken WeakMap such that we need to patch - // it using DoubleWeakMap; this flag tells DoubleWeakMap so. - var doubleWeakMapCheckSilentFailure = false; - - // Check if there is already a good-enough WeakMap implementation, and if so - // exit without replacing it. - if (typeof WeakMap === 'function') { - var HostWeakMap = WeakMap; - // There is a WeakMap -- is it good enough? - if (typeof navigator !== 'undefined' && - /Firefox/.test(navigator.userAgent)) { - // We're now *assuming not*, because as of this writing (2013-05-06) - // Firefox's WeakMaps have a miscellany of objects they won't accept, and - // we don't want to make an exhaustive list, and testing for just one - // will be a problem if that one is fixed alone (as they did for Event). - - // If there is a platform that we *can* reliably test on, here's how to - // do it: - // var problematic = ... ; - // var testHostMap = new HostWeakMap(); - // try { - // testHostMap.set(problematic, 1); // Firefox 20 will throw here - // if (testHostMap.get(problematic) === 1) { - // return; - // } - // } catch (e) {} - - } else { - // IE 11 bug: WeakMaps silently fail to store frozen objects. - var testMap = new HostWeakMap(); - var testObject = Object.freeze({}); - testMap.set(testObject, 1); - if (testMap.get(testObject) !== 1) { - doubleWeakMapCheckSilentFailure = true; - // Fall through to installing our WeakMap. - } else { - module.exports = WeakMap; - return; - } - } - } - - var hop = Object.prototype.hasOwnProperty; - var gopn = Object.getOwnPropertyNames; - var defProp = Object.defineProperty; - var isExtensible = Object.isExtensible; - - /** - * Security depends on HIDDEN_NAME being both unguessable and - * undiscoverable by untrusted code. - * - *

Given the known weaknesses of Math.random() on existing - * browsers, it does not generate unguessability we can be confident - * of. - * - *

It is the monkey patching logic in this file that is intended - * to ensure undiscoverability. The basic idea is that there are - * three fundamental means of discovering properties of an object: - * The for/in loop, Object.keys(), and Object.getOwnPropertyNames(), - * as well as some proposed ES6 extensions that appear on our - * whitelist. The first two only discover enumerable properties, and - * we only use HIDDEN_NAME to name a non-enumerable property, so the - * only remaining threat should be getOwnPropertyNames and some - * proposed ES6 extensions that appear on our whitelist. We monkey - * patch them to remove HIDDEN_NAME from the list of properties they - * returns. - * - *

TODO(erights): On a platform with built-in Proxies, proxies - * could be used to trap and thereby discover the HIDDEN_NAME, so we - * need to monkey patch Proxy.create, Proxy.createFunction, etc, in - * order to wrap the provided handler with the real handler which - * filters out all traps using HIDDEN_NAME. - * - *

TODO(erights): Revisit Mike Stay's suggestion that we use an - * encapsulated function at a not-necessarily-secret name, which - * uses the Stiegler shared-state rights amplification pattern to - * reveal the associated value only to the WeakMap in which this key - * is associated with that value. Since only the key retains the - * function, the function can also remember the key without causing - * leakage of the key, so this doesn't violate our general gc - * goals. In addition, because the name need not be a guarded - * secret, we could efficiently handle cross-frame frozen keys. - */ - var HIDDEN_NAME_PREFIX = 'weakmap:'; - var HIDDEN_NAME = HIDDEN_NAME_PREFIX + 'ident:' + Math.random() + '___'; - - if (typeof crypto !== 'undefined' && - typeof crypto.getRandomValues === 'function' && - typeof ArrayBuffer === 'function' && - typeof Uint8Array === 'function') { - var ab = new ArrayBuffer(25); - var u8s = new Uint8Array(ab); - crypto.getRandomValues(u8s); - HIDDEN_NAME = HIDDEN_NAME_PREFIX + 'rand:' + - Array.prototype.map.call(u8s, function(u8) { - return (u8 % 36).toString(36); - }).join('') + '___'; - } - - function isNotHiddenName(name) { - return !( - name.substr(0, HIDDEN_NAME_PREFIX.length) == HIDDEN_NAME_PREFIX && - name.substr(name.length - 3) === '___'); - } - - /** - * Monkey patch getOwnPropertyNames to avoid revealing the - * HIDDEN_NAME. - * - *

The ES5.1 spec requires each name to appear only once, but as - * of this writing, this requirement is controversial for ES6, so we - * made this code robust against this case. If the resulting extra - * search turns out to be expensive, we can probably relax this once - * ES6 is adequately supported on all major browsers, iff no browser - * versions we support at that time have relaxed this constraint - * without providing built-in ES6 WeakMaps. - */ - defProp(Object, 'getOwnPropertyNames', { - value: function fakeGetOwnPropertyNames(obj) { - return gopn(obj).filter(isNotHiddenName); - } - }); - - /** - * getPropertyNames is not in ES5 but it is proposed for ES6 and - * does appear in our whitelist, so we need to clean it too. - */ - if ('getPropertyNames' in Object) { - var originalGetPropertyNames = Object.getPropertyNames; - defProp(Object, 'getPropertyNames', { - value: function fakeGetPropertyNames(obj) { - return originalGetPropertyNames(obj).filter(isNotHiddenName); - } - }); - } - - /** - *

To treat objects as identity-keys with reasonable efficiency - * on ES5 by itself (i.e., without any object-keyed collections), we - * need to add a hidden property to such key objects when we - * can. This raises several issues: - *

    - *
  • Arranging to add this property to objects before we lose the - * chance, and - *
  • Hiding the existence of this new property from most - * JavaScript code. - *
  • Preventing certification theft, where one object is - * created falsely claiming to be the key of an association - * actually keyed by another object. - *
  • Preventing value theft, where untrusted code with - * access to a key object but not a weak map nevertheless - * obtains access to the value associated with that key in that - * weak map. - *
- * We do so by - *
    - *
  • Making the name of the hidden property unguessable, so "[]" - * indexing, which we cannot intercept, cannot be used to access - * a property without knowing the name. - *
  • Making the hidden property non-enumerable, so we need not - * worry about for-in loops or {@code Object.keys}, - *
  • monkey patching those reflective methods that would - * prevent extensions, to add this hidden property first, - *
  • monkey patching those methods that would reveal this - * hidden property. - *
- * Unfortunately, because of same-origin iframes, we cannot reliably - * add this hidden property before an object becomes - * non-extensible. Instead, if we encounter a non-extensible object - * without a hidden record that we can detect (whether or not it has - * a hidden record stored under a name secret to us), then we just - * use the key object itself to represent its identity in a brute - * force leaky map stored in the weak map, losing all the advantages - * of weakness for these. - */ - function getHiddenRecord(key) { - if (key !== Object(key)) { - throw new TypeError('Not an object: ' + key); - } - var hiddenRecord = key[HIDDEN_NAME]; - if (hiddenRecord && hiddenRecord.key === key) { return hiddenRecord; } - if (!isExtensible(key)) { - // Weak map must brute force, as explained in doc-comment above. - return void 0; - } - - // The hiddenRecord and the key point directly at each other, via - // the "key" and HIDDEN_NAME properties respectively. The key - // field is for quickly verifying that this hidden record is an - // own property, not a hidden record from up the prototype chain. - // - // NOTE: Because this WeakMap emulation is meant only for systems like - // SES where Object.prototype is frozen without any numeric - // properties, it is ok to use an object literal for the hiddenRecord. - // This has two advantages: - // * It is much faster in a performance critical place - // * It avoids relying on Object.create(null), which had been - // problematic on Chrome 28.0.1480.0. See - // https://code.google.com/p/google-caja/issues/detail?id=1687 - hiddenRecord = { key: key }; - - // When using this WeakMap emulation on platforms where - // Object.prototype might not be frozen and Object.create(null) is - // reliable, use the following two commented out lines instead. - // hiddenRecord = Object.create(null); - // hiddenRecord.key = key; - - // Please contact us if you need this to work on platforms where - // Object.prototype might not be frozen and - // Object.create(null) might not be reliable. - - try { - defProp(key, HIDDEN_NAME, { - value: hiddenRecord, - writable: false, - enumerable: false, - configurable: false - }); - return hiddenRecord; - } catch (error) { - // Under some circumstances, isExtensible seems to misreport whether - // the HIDDEN_NAME can be defined. - // The circumstances have not been isolated, but at least affect - // Node.js v0.10.26 on TravisCI / Linux, but not the same version of - // Node.js on OS X. - return void 0; - } - } - - /** - * Monkey patch operations that would make their argument - * non-extensible. - * - *

The monkey patched versions throw a TypeError if their - * argument is not an object, so it should only be done to functions - * that should throw a TypeError anyway if their argument is not an - * object. - */ - (function(){ - var oldFreeze = Object.freeze; - defProp(Object, 'freeze', { - value: function identifyingFreeze(obj) { - getHiddenRecord(obj); - return oldFreeze(obj); - } - }); - var oldSeal = Object.seal; - defProp(Object, 'seal', { - value: function identifyingSeal(obj) { - getHiddenRecord(obj); - return oldSeal(obj); - } - }); - var oldPreventExtensions = Object.preventExtensions; - defProp(Object, 'preventExtensions', { - value: function identifyingPreventExtensions(obj) { - getHiddenRecord(obj); - return oldPreventExtensions(obj); - } - }); - })(); - - function constFunc(func) { - func.prototype = null; - return Object.freeze(func); - } - - var calledAsFunctionWarningDone = false; - function calledAsFunctionWarning() { - // Future ES6 WeakMap is currently (2013-09-10) expected to reject WeakMap() - // but we used to permit it and do it ourselves, so warn only. - if (!calledAsFunctionWarningDone && typeof console !== 'undefined') { - calledAsFunctionWarningDone = true; - console.warn('WeakMap should be invoked as new WeakMap(), not ' + - 'WeakMap(). This will be an error in the future.'); - } - } - - var nextId = 0; - - var OurWeakMap = function() { - if (!(this instanceof OurWeakMap)) { // approximate test for new ...() - calledAsFunctionWarning(); - } - - // We are currently (12/25/2012) never encountering any prematurely - // non-extensible keys. - var keys = []; // brute force for prematurely non-extensible keys. - var values = []; // brute force for corresponding values. - var id = nextId++; - - function get___(key, opt_default) { - var index; - var hiddenRecord = getHiddenRecord(key); - if (hiddenRecord) { - return id in hiddenRecord ? hiddenRecord[id] : opt_default; - } else { - index = keys.indexOf(key); - return index >= 0 ? values[index] : opt_default; - } - } - - function has___(key) { - var hiddenRecord = getHiddenRecord(key); - if (hiddenRecord) { - return id in hiddenRecord; - } else { - return keys.indexOf(key) >= 0; - } - } - - function set___(key, value) { - var index; - var hiddenRecord = getHiddenRecord(key); - if (hiddenRecord) { - hiddenRecord[id] = value; - } else { - index = keys.indexOf(key); - if (index >= 0) { - values[index] = value; - } else { - // Since some browsers preemptively terminate slow turns but - // then continue computing with presumably corrupted heap - // state, we here defensively get keys.length first and then - // use it to update both the values and keys arrays, keeping - // them in sync. - index = keys.length; - values[index] = value; - // If we crash here, values will be one longer than keys. - keys[index] = key; - } - } - return this; - } - - function delete___(key) { - var hiddenRecord = getHiddenRecord(key); - var index, lastIndex; - if (hiddenRecord) { - return id in hiddenRecord && delete hiddenRecord[id]; - } else { - index = keys.indexOf(key); - if (index < 0) { - return false; - } - // Since some browsers preemptively terminate slow turns but - // then continue computing with potentially corrupted heap - // state, we here defensively get keys.length first and then use - // it to update both the keys and the values array, keeping - // them in sync. We update the two with an order of assignments, - // such that any prefix of these assignments will preserve the - // key/value correspondence, either before or after the delete. - // Note that this needs to work correctly when index === lastIndex. - lastIndex = keys.length - 1; - keys[index] = void 0; - // If we crash here, there's a void 0 in the keys array, but - // no operation will cause a "keys.indexOf(void 0)", since - // getHiddenRecord(void 0) will always throw an error first. - values[index] = values[lastIndex]; - // If we crash here, values[index] cannot be found here, - // because keys[index] is void 0. - keys[index] = keys[lastIndex]; - // If index === lastIndex and we crash here, then keys[index] - // is still void 0, since the aliasing killed the previous key. - keys.length = lastIndex; - // If we crash here, keys will be one shorter than values. - values.length = lastIndex; - return true; - } - } - - return Object.create(OurWeakMap.prototype, { - get___: { value: constFunc(get___) }, - has___: { value: constFunc(has___) }, - set___: { value: constFunc(set___) }, - delete___: { value: constFunc(delete___) } - }); - }; - - OurWeakMap.prototype = Object.create(Object.prototype, { - get: { - /** - * Return the value most recently associated with key, or - * opt_default if none. - */ - value: function get(key, opt_default) { - return this.get___(key, opt_default); - }, - writable: true, - configurable: true - }, - - has: { - /** - * Is there a value associated with key in this WeakMap? - */ - value: function has(key) { - return this.has___(key); - }, - writable: true, - configurable: true - }, - - set: { - /** - * Associate value with key in this WeakMap, overwriting any - * previous association if present. - */ - value: function set(key, value) { - return this.set___(key, value); - }, - writable: true, - configurable: true - }, - - 'delete': { - /** - * Remove any association for key in this WeakMap, returning - * whether there was one. - * - *

Note that the boolean return here does not work like the - * {@code delete} operator. The {@code delete} operator returns - * whether the deletion succeeds at bringing about a state in - * which the deleted property is absent. The {@code delete} - * operator therefore returns true if the property was already - * absent, whereas this {@code delete} method returns false if - * the association was already absent. - */ - value: function remove(key) { - return this.delete___(key); - }, - writable: true, - configurable: true - } - }); - - if (typeof HostWeakMap === 'function') { - (function() { - // If we got here, then the platform has a WeakMap but we are concerned - // that it may refuse to store some key types. Therefore, make a map - // implementation which makes use of both as possible. - - // In this mode we are always using double maps, so we are not proxy-safe. - // This combination does not occur in any known browser, but we had best - // be safe. - if (doubleWeakMapCheckSilentFailure && typeof Proxy !== 'undefined') { - Proxy = undefined; - } - - function DoubleWeakMap() { - if (!(this instanceof OurWeakMap)) { // approximate test for new ...() - calledAsFunctionWarning(); } - // Preferable, truly weak map. - var hmap = new HostWeakMap(); + return errors; + } - // Our hidden-property-based pseudo-weak-map. Lazily initialized in the - // 'set' implementation; thus we can avoid performing extra lookups if - // we know all entries actually stored are entered in 'hmap'. - var omap = undefined; - - // Hidden-property maps are not compatible with proxies because proxies - // can observe the hidden name and either accidentally expose it or fail - // to allow the hidden property to be set. Therefore, we do not allow - // arbitrary WeakMaps to switch to using hidden properties, but only - // those which need the ability, and unprivileged code is not allowed - // to set the flag. - // - // (Except in doubleWeakMapCheckSilentFailure mode in which case we - // disable proxies.) - var enableSwitching = false; - - function dget(key, opt_default) { - if (omap) { - return hmap.has(key) ? hmap.get(key) - : omap.get___(key, opt_default); - } else { - return hmap.get(key, opt_default); - } + function validateValue(options) { + var errors = []; + var type = getType(options.value); + if (type !== 'number' && type !== 'string' && type !== 'array') { + errors.push(new ValidationError(options.key, options.value, 'property value must be a number, string or array')); } + return errors; + } - function dhas(key) { - return hmap.has(key) || (omap ? omap.has___(key) : false); - } +}; - var dset; - if (doubleWeakMapCheckSilentFailure) { - dset = function(key, value) { - hmap.set(key, value); - if (!hmap.has(key)) { - if (!omap) { omap = new OurWeakMap(); } - omap.set(key, value); +},{"../error/validation_error":280,"../util/get_type":282,"./validate":284,"./validate_array":285,"./validate_number":295,"./validate_object":296}],292:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var validateString = require('./validate_string'); + +module.exports = function(options) { + var value = options.value; + var key = options.key; + + var errors = validateString(options); + if (errors.length) return errors; + + if (value.indexOf('{fontstack}') === -1) { + errors.push(new ValidationError(key, value, '"glyphs" url must include a "{fontstack}" token')); + } + + if (value.indexOf('{range}') === -1) { + errors.push(new ValidationError(key, value, '"glyphs" url must include a "{range}" token')); + } + + return errors; +}; + +},{"../error/validation_error":280,"./validate_string":299}],293:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var unbundle = require('../util/unbundle_jsonlint'); +var validateObject = require('./validate_object'); +var validateFilter = require('./validate_filter'); +var validatePaintProperty = require('./validate_paint_property'); +var validateLayoutProperty = require('./validate_layout_property'); +var extend = require('../util/extend'); + +module.exports = function validateLayer(options) { + var errors = []; + + var layer = options.value; + var key = options.key; + var style = options.style; + var styleSpec = options.styleSpec; + + if (!layer.type && !layer.ref) { + errors.push(new ValidationError(key, layer, 'either "type" or "ref" is required')); + } + var type = unbundle(layer.type); + var ref = unbundle(layer.ref); + + if (layer.id) { + for (var i = 0; i < options.arrayIndex; i++) { + var otherLayer = style.layers[i]; + if (unbundle(otherLayer.id) === unbundle(layer.id)) { + errors.push(new ValidationError(key, layer.id, 'duplicate layer id "%s", previously used at line %d', layer.id, otherLayer.id.__line__)); } - return this; - }; - } else { - dset = function(key, value) { - if (enableSwitching) { - try { - hmap.set(key, value); - } catch (e) { - if (!omap) { omap = new OurWeakMap(); } - omap.set___(key, value); - } - } else { - hmap.set(key, value); - } - return this; - }; } + } - function ddelete(key) { - var result = !!hmap['delete'](key); - if (omap) { return omap.delete___(key) || result; } - return result; - } - - return Object.create(OurWeakMap.prototype, { - get___: { value: constFunc(dget) }, - has___: { value: constFunc(dhas) }, - set___: { value: constFunc(dset) }, - delete___: { value: constFunc(ddelete) }, - permitHostObjects___: { value: constFunc(function(token) { - if (token === weakMapPermitHostObjects) { - enableSwitching = true; - } else { - throw new Error('bogus call to permitHostObjects___'); + if ('ref' in layer) { + ['type', 'source', 'source-layer', 'filter', 'layout'].forEach(function (p) { + if (p in layer) { + errors.push(new ValidationError(key, layer[p], '"%s" is prohibited for ref layers', p)); } - })} }); - } - DoubleWeakMap.prototype = OurWeakMap.prototype; - module.exports = DoubleWeakMap; - // define .constructor to hide OurWeakMap ctor - Object.defineProperty(WeakMap.prototype, 'constructor', { - value: WeakMap, - enumerable: false, // as default .constructor is - configurable: true, - writable: true - }); - })(); - } else { - // There is no host WeakMap, so we must use the emulation. + var parent; - // Emulated WeakMaps are incompatible with native proxies (because proxies - // can observe the hidden name), so we must disable Proxy usage (in - // ArrayLike and Domado, currently). - if (typeof Proxy !== 'undefined') { - Proxy = undefined; - } + style.layers.forEach(function(layer) { + if (layer.id == ref) parent = layer; + }); - module.exports = OurWeakMap; - } -})(); - -},{}],433:[function(require,module,exports){ -'use strict' - -var weakMap = typeof WeakMap === 'undefined' ? require('weak-map') : WeakMap -var createBuffer = require('gl-buffer') -var createVAO = require('gl-vao') - -var TriangleCache = new weakMap() - -function createABigTriangle(gl) { - - var triangleVAO = TriangleCache.get(gl) - var handle = triangleVAO && (triangleVAO._triangleBuffer.handle || triangleVAO._triangleBuffer.buffer) - if(!handle || !gl.isBuffer(handle)) { - var buf = createBuffer(gl, new Float32Array([-1, -1, -1, 4, 4, -1])) - triangleVAO = createVAO(gl, [ - { buffer: buf, - type: gl.FLOAT, - size: 2 - } - ]) - triangleVAO._triangleBuffer = buf - TriangleCache.set(gl, triangleVAO) - } - triangleVAO.bind() - gl.drawArrays(gl.TRIANGLES, 0, 3) - triangleVAO.unbind() -} - -module.exports = createABigTriangle - -},{"gl-buffer":424,"gl-vao":431,"weak-map":432}],434:[function(require,module,exports){ -'use strict' - -module.exports = createAxes - -var createText = require('./lib/text.js') -var createLines = require('./lib/lines.js') -var createBackground = require('./lib/background.js') -var getCubeProperties = require('./lib/cube.js') -var Ticks = require('./lib/ticks.js') - -var identity = new Float32Array([ - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1]) - -function copyVec3(a, b) { - a[0] = b[0] - a[1] = b[1] - a[2] = b[2] - return a -} - -function Axes(gl) { - this.gl = gl - - this.pixelRatio = 1 - - this.bounds = [ [-10, -10, -10], - [ 10, 10, 10] ] - this.ticks = [ [], [], [] ] - this.autoTicks = true - this.tickSpacing = [ 1, 1, 1 ] - - this.tickEnable = [ true, true, true ] - this.tickFont = [ 'sans-serif', 'sans-serif', 'sans-serif' ] - this.tickSize = [ 12, 12, 12 ] - this.tickAngle = [ 0, 0, 0 ] - this.tickColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] - this.tickPad = [ 10, 10, 10 ] - - this.lastCubeProps = { - cubeEdges: [0,0,0], - axis: [0,0,0] - } - - this.labels = [ 'x', 'y', 'z' ] - this.labelEnable = [ true, true, true ] - this.labelFont = 'sans-serif' - this.labelSize = [ 20, 20, 20 ] - this.labelAngle = [ 0, 0, 0 ] - this.labelColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] - this.labelPad = [ 10, 10, 10 ] - - this.lineEnable = [ true, true, true ] - this.lineMirror = [ false, false, false ] - this.lineWidth = [ 1, 1, 1 ] - this.lineColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] - - this.lineTickEnable = [ true, true, true ] - this.lineTickMirror = [ false, false, false ] - this.lineTickLength = [ 0, 0, 0 ] - this.lineTickWidth = [ 1, 1, 1 ] - this.lineTickColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] - - this.gridEnable = [ true, true, true ] - this.gridWidth = [ 1, 1, 1 ] - this.gridColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] - - this.zeroEnable = [ true, true, true ] - this.zeroLineColor = [ [0,0,0,1], [0,0,0,1], [0,0,0,1] ] - this.zeroLineWidth = [ 2, 2, 2 ] - - this.backgroundEnable = [ false, false, false ] - this.backgroundColor = [ [0.8, 0.8, 0.8, 0.5], - [0.8, 0.8, 0.8, 0.5], - [0.8, 0.8, 0.8, 0.5] ] - - this._firstInit = true - this._text = null - this._lines = null - this._background = createBackground(gl) -} - -var proto = Axes.prototype - -proto.update = function(options) { - options = options || {} - - //Option parsing helper functions - function parseOption(nest, cons, name) { - if(name in options) { - var opt = options[name] - var prev = this[name] - var next - if(nest ? (Array.isArray(opt) && Array.isArray(opt[0])) : - Array.isArray(opt) ) { - this[name] = next = [ cons(opt[0]), cons(opt[1]), cons(opt[2]) ] - } else { - this[name] = next = [ cons(opt), cons(opt), cons(opt) ] - } - for(var i=0; i<3; ++i) { - if(next[i] !== prev[i]) { - return true - } - } - } - return false - } - - var NUMBER = parseOption.bind(this, false, Number) - var BOOLEAN = parseOption.bind(this, false, Boolean) - var STRING = parseOption.bind(this, false, String) - var COLOR = parseOption.bind(this, true, function(v) { - if(Array.isArray(v)) { - if(v.length === 3) { - return [ +v[0], +v[1], +v[2], 1.0 ] - } else if(v.length === 4) { - return [ +v[0], +v[1], +v[2], +v[3] ] - } - } - return [ 0, 0, 0, 1 ] - }) - - //Tick marks and bounds - var nextTicks - var ticksUpdate = false - var boundsChanged = false - if('bounds' in options) { - var bounds = options.bounds -i_loop: - for(var i=0; i<2; ++i) { - for(var j=0; j<3; ++j) { - if(bounds[i][j] !== this.bounds[i][j]) { - boundsChanged = true - } - this.bounds[i][j] = bounds[i][j] - } - } - } - if('ticks' in options) { - nextTicks = options.ticks - ticksUpdate = true - this.autoTicks = false - for(var i=0; i<3; ++i) { - this.tickSpacing[i] = 0.0 - } - } else if(NUMBER('tickSpacing')) { - this.autoTicks = true - boundsChanged = true - } - - if(this._firstInit) { - if(!('ticks' in options || 'tickSpacing' in options)) { - this.autoTicks = true - } - - //Force tick recomputation on first update - boundsChanged = true - ticksUpdate = true - this._firstInit = false - } - - if(boundsChanged && this.autoTicks) { - nextTicks = Ticks.create(this.bounds, this.tickSpacing) - ticksUpdate = true - } - - //Compare next ticks to previous ticks, only update if needed - if(ticksUpdate) { - for(var i=0; i<3; ++i) { - nextTicks[i].sort(function(a,b) { - return a.x-b.x - }) - } - if(Ticks.equal(nextTicks, this.ticks)) { - ticksUpdate = false - } else { - this.ticks = nextTicks - } - } - - //Parse tick properties - BOOLEAN('tickEnable') - if(STRING('tickFont')) { - ticksUpdate = true //If font changes, must rebuild vbo - } - NUMBER('tickSize') - NUMBER('tickAngle') - NUMBER('tickPad') - COLOR('tickColor') - - //Axis labels - var labelUpdate = STRING('labels') - if(STRING('labelFont')) { - labelUpdate = true - } - BOOLEAN('labelEnable') - NUMBER('labelSize') - NUMBER('labelPad') - COLOR('labelColor') - - //Axis lines - BOOLEAN('lineEnable') - BOOLEAN('lineMirror') - NUMBER('lineWidth') - COLOR('lineColor') - - //Axis line ticks - BOOLEAN('lineTickEnable') - BOOLEAN('lineTickMirror') - NUMBER('lineTickLength') - NUMBER('lineTickWidth') - COLOR('lineTickColor') - - //Grid lines - BOOLEAN('gridEnable') - NUMBER('gridWidth') - COLOR('gridColor') - - //Zero line - BOOLEAN('zeroEnable') - COLOR('zeroLineColor') - NUMBER('zeroLineWidth') - - //Background - BOOLEAN('backgroundEnable') - COLOR('backgroundColor') - - //Update text if necessary - if(!this._text) { - this._text = createText( - this.gl, - this.bounds, - this.labels, - this.labelFont, - this.ticks, - this.tickFont) - } else if(this._text && (labelUpdate || ticksUpdate)) { - this._text.update( - this.bounds, - this.labels, - this.labelFont, - this.ticks, - this.tickFont) - } - - //Update lines if necessary - if(this._lines && ticksUpdate) { - this._lines.dispose() - this._lines = null - } - if(!this._lines) { - this._lines = createLines(this.gl, this.bounds, this.ticks) - } -} - -function OffsetInfo() { - this.primalOffset = [0,0,0] - this.primalMinor = [0,0,0] - this.mirrorOffset = [0,0,0] - this.mirrorMinor = [0,0,0] -} - -var LINE_OFFSET = [ new OffsetInfo(), new OffsetInfo(), new OffsetInfo() ] - -function computeLineOffset(result, i, bounds, cubeEdges, cubeAxis) { - var primalOffset = result.primalOffset - var primalMinor = result.primalMinor - var dualOffset = result.mirrorOffset - var dualMinor = result.mirrorMinor - var e = cubeEdges[i] - - //Calculate offsets - for(var j=0; j<3; ++j) { - if(i === j) { - continue - } - var a = primalOffset, - b = dualOffset, - c = primalMinor, - d = dualMinor - if(e & (1< 0) { - c[j] = -1 - d[j] = 0 - } else { - c[j] = 0 - d[j] = +1 - } - } -} - -var CUBE_ENABLE = [0,0,0] -var DEFAULT_PARAMS = { - model: identity, - view: identity, - projection: identity -} - -proto.isOpaque = function() { - return true -} - -proto.isTransparent = function() { - return false -} - -proto.drawTransparent = function(params) {} - - -var PRIMAL_MINOR = [0,0,0] -var MIRROR_MINOR = [0,0,0] -var PRIMAL_OFFSET = [0,0,0] - -proto.draw = function(params) { - params = params || DEFAULT_PARAMS - - var gl = this.gl - - //Geometry for camera and axes - var model = params.model || identity - var view = params.view || identity - var projection = params.projection || identity - var bounds = this.bounds - - //Unpack axis info - var cubeParams = getCubeProperties(model, view, projection, bounds) - var cubeEdges = cubeParams.cubeEdges - var cubeAxis = cubeParams.axis - - var cx = view[12] - var cy = view[13] - var cz = view[14] - var cw = view[15] - - var pixelScaleF = this.pixelRatio * (projection[3]*cx + projection[7]*cy + projection[11]*cz + projection[15]*cw) / gl.drawingBufferHeight - - for(var i=0; i<3; ++i) { - this.lastCubeProps.cubeEdges[i] = cubeEdges[i] - this.lastCubeProps.axis[i] = cubeAxis[i] - } - - //Compute axis info - var lineOffset = LINE_OFFSET - for(var i=0; i<3; ++i) { - computeLineOffset( - LINE_OFFSET[i], - i, - this.bounds, - cubeEdges, - cubeAxis) - } - - //Set up state parameters - var gl = this.gl - - //Draw background first - var cubeEnable = CUBE_ENABLE - for(var i=0; i<3; ++i) { - if(this.backgroundEnable[i]) { - cubeEnable[i] = cubeAxis[i] - } else { - cubeEnable[i] = 0 - } - } - - this._background.draw( - model, - view, - projection, - bounds, - cubeEnable, - this.backgroundColor) - - //Draw lines - this._lines.bind( - model, - view, - projection, - this) - - //First draw grid lines and zero lines - for(var i=0; i<3; ++i) { - var x = [0,0,0] - if(cubeAxis[i] > 0) { - x[i] = bounds[1][i] - } else { - x[i] = bounds[0][i] - } - - //Draw grid lines - for(var j=0; j<2; ++j) { - var u = (i + 1 + j) % 3 - var v = (i + 1 + (j^1)) % 3 - if(this.gridEnable[u]) { - this._lines.drawGrid(u, v, this.bounds, x, this.gridColor[u], this.gridWidth[u]*this.pixelRatio) - } - } - - //Draw zero lines (need to do this AFTER all grid lines are drawn) - for(var j=0; j<2; ++j) { - var u = (i + 1 + j) % 3 - var v = (i + 1 + (j^1)) % 3 - if(this.zeroEnable[v]) { - //Check if zero line in bounds - if(bounds[0][v] <= 0 && bounds[1][v] >= 0) { - this._lines.drawZero(u, v, this.bounds, x, this.zeroLineColor[v], this.zeroLineWidth[v]*this.pixelRatio) - } - } - } - } - - //Then draw axis lines and tick marks - for(var i=0; i<3; ++i) { - - //Draw axis lines - if(this.lineEnable[i]) { - this._lines.drawAxisLine(i, this.bounds, lineOffset[i].primalOffset, this.lineColor[i], this.lineWidth[i]*this.pixelRatio) - } - if(this.lineMirror[i]) { - this._lines.drawAxisLine(i, this.bounds, lineOffset[i].mirrorOffset, this.lineColor[i], this.lineWidth[i]*this.pixelRatio) - } - - //Compute minor axes - var primalMinor = copyVec3(PRIMAL_MINOR, lineOffset[i].primalMinor) - var mirrorMinor = copyVec3(MIRROR_MINOR, lineOffset[i].mirrorMinor) - var tickLength = this.lineTickLength - var op = 0 - for(var j=0; j<3; ++j) { - var scaleFactor = pixelScaleF / model[5*j] - primalMinor[j] *= tickLength[j] * scaleFactor - mirrorMinor[j] *= tickLength[j] * scaleFactor - } - - //Draw axis line ticks - if(this.lineTickEnable[i]) { - this._lines.drawAxisTicks(i, lineOffset[i].primalOffset, primalMinor, this.lineTickColor[i], this.lineTickWidth[i]*this.pixelRatio) - } - if(this.lineTickMirror[i]) { - this._lines.drawAxisTicks(i, lineOffset[i].mirrorOffset, mirrorMinor, this.lineTickColor[i], this.lineTickWidth[i]*this.pixelRatio) - } - } - - //Draw text sprites - this._text.bind( - model, - view, - projection, - this.pixelRatio) - - for(var i=0; i<3; ++i) { - - var minor = lineOffset[i].primalMinor - var offset = copyVec3(PRIMAL_OFFSET, lineOffset[i].primalOffset) - - for(var j=0; j<3; ++j) { - if(this.lineTickEnable[i]) { - offset[j] += pixelScaleF * minor[j] * Math.max(this.lineTickLength[j], 0) / model[5*j] - } - } - - //Draw tick text - if(this.tickEnable[i]) { - - //Add tick padding - for(var j=0; j<3; ++j) { - offset[j] += pixelScaleF * minor[j] * this.tickPad[j] / model[5*j] - } - - //Draw axis - this._text.drawTicks( - i, - this.tickSize[i], - this.tickAngle[i], - offset, - this.tickColor[i]) - } - - //Draw labels - if(this.labelEnable[i]) { - - //Add label padding - for(var j=0; j<3; ++j) { - offset[j] += pixelScaleF * minor[j] * this.labelPad[j] / model[5*j] - } - offset[i] += 0.5 * (bounds[0][i] + bounds[1][i]) - - //Draw axis - this._text.drawLabel( - i, - this.labelSize[i], - this.labelAngle[i], - offset, - this.labelColor[i]) - } - } -} - -proto.dispose = function() { - this._text.dispose() - this._lines.dispose() - this._background.dispose() - this._lines = null - this._text = null - this._background = null - this.gl = null -} - -function createAxes(gl, options) { - var axes = new Axes(gl) - axes.update(options) - return axes -} - -},{"./lib/background.js":435,"./lib/cube.js":436,"./lib/lines.js":437,"./lib/text.js":439,"./lib/ticks.js":440}],435:[function(require,module,exports){ -'use strict' - -module.exports = createBackgroundCube - -var createBuffer = require('gl-buffer') -var createVAO = require('gl-vao') -var createShader = require('./shaders').bg - -function BackgroundCube(gl, buffer, vao, shader) { - this.gl = gl - this.buffer = buffer - this.vao = vao - this.shader = shader -} - -var proto = BackgroundCube.prototype - -proto.draw = function(model, view, projection, bounds, enable, colors) { - var needsBG = false - for(var i=0; i<3; ++i) { - needsBG = needsBG || enable[i] - } - if(!needsBG) { - return - } - - var gl = this.gl - - gl.enable(gl.POLYGON_OFFSET_FILL) - gl.polygonOffset(1, 2) - - this.shader.bind() - this.shader.uniforms = { - model: model, - view: view, - projection: projection, - bounds: bounds, - enable: enable, - colors: colors - } - this.vao.bind() - this.vao.draw(this.gl.TRIANGLES, 36) - - gl.disable(gl.POLYGON_OFFSET_FILL) -} - -proto.dispose = function() { - this.vao.dispose() - this.buffer.dispose() - this.shader.dispose() -} - -function createBackgroundCube(gl) { - //Create cube vertices - var vertices = [] - var indices = [] - var ptr = 0 - for(var d=0; d<3; ++d) { - var u = (d+1) % 3 - var v = (d+2) % 3 - var x = [0,0,0] - var c = [0,0,0] - for(var s=-1; s<=1; s+=2) { - indices.push(ptr, ptr+2, ptr+1, - ptr+1, ptr+2, ptr+3) - x[d] = s - c[d] = s - for(var i=-1; i<=1; i+=2) { - x[u] = i - for(var j=-1; j<=1; j+=2) { - x[v] = j - vertices.push(x[0], x[1], x[2], - c[0], c[1], c[2]) - ptr += 1 - } - } - //Swap u and v - var tt = u - u = v - v = tt - } - } - - //Allocate buffer and vertex array - var buffer = createBuffer(gl, new Float32Array(vertices)) - var elements = createBuffer(gl, new Uint16Array(indices), gl.ELEMENT_ARRAY_BUFFER) - var vao = createVAO(gl, [ - { - buffer: buffer, - type: gl.FLOAT, - size: 3, - offset: 0, - stride: 24 - }, - { - buffer: buffer, - type: gl.FLOAT, - size: 3, - offset: 12, - stride: 24 - } - ], elements) - - //Create shader object - var shader = createShader(gl) - shader.attributes.position.location = 0 - shader.attributes.normal.location = 1 - - return new BackgroundCube(gl, buffer, vao, shader) -} - -},{"./shaders":438,"gl-buffer":444,"gl-vao":449}],436:[function(require,module,exports){ -"use strict" - -module.exports = getCubeEdges - -var bits = require('bit-twiddle') -var multiply = require('gl-mat4/multiply') -var invert = require('gl-mat4/invert') -var splitPoly = require('split-polygon') -var orient = require('robust-orientation') - -var mvp = new Array(16) -var imvp = new Array(16) -var pCubeVerts = new Array(8) -var cubeVerts = new Array(8) -var x = new Array(3) -var zero3 = [0,0,0] - -;(function() { - for(var i=0; i<8; ++i) { - pCubeVerts[i] =[1,1,1,1] - cubeVerts[i] = [1,1,1] - } -})() - - -function transformHg(result, x, mat) { - for(var i=0; i<4; ++i) { - result[i] = mat[12+i] - for(var j=0; j<3; ++j) { - result[i] += x[j]*mat[4*j+i] - } - } -} - -var FRUSTUM_PLANES = [ - [ 0, 0, 1, 0, 0], - [ 0, 0,-1, 1, 0], - [ 0,-1, 0, 1, 0], - [ 0, 1, 0, 1, 0], - [-1, 0, 0, 1, 0], - [ 1, 0, 0, 1, 0] -] - -function polygonArea(p) { - for(var i=0; i o0) { - closest |= 1< o0) { - closest |= 1< cubeVerts[i][1]) { - bottom = i - } - } - - //Find left/right neighbors of bottom vertex - var left = -1 - for(var i=0; i<3; ++i) { - var idx = bottom ^ (1< cubeVerts[right][0]) { - right = idx - } - } - - //Determine edge axis coordinates - var cubeEdges = CUBE_EDGES - cubeEdges[0] = cubeEdges[1] = cubeEdges[2] = 0 - cubeEdges[bits.log2(left^bottom)] = bottom&left - cubeEdges[bits.log2(bottom^right)] = bottom&right - var top = right ^ 7 - if(top === closest || top === farthest) { - top = left ^ 7 - cubeEdges[bits.log2(right^top)] = top&right - } else { - cubeEdges[bits.log2(left^top)] = top&left - } - - //Determine visible faces - var axis = CUBE_AXIS - var cutCorner = closest - for(var d=0; d<3; ++d) { - if(cutCorner & (1<=0; --j) { - var p = positions[c[j]] - data.push(scale*p[0], -scale*p[1], t) - } - } - } - - //Generate sprites for all 3 axes, store data in texture atlases - var tickOffset = [0,0,0] - var tickCount = [0,0,0] - var labelOffset = [0,0,0] - var labelCount = [0,0,0] - for(var d=0; d<3; ++d) { - - //Generate label - labelOffset[d] = (data.length/VERTEX_SIZE)|0 - addItem(0.5*(bounds[0][d]+bounds[1][d]), labels[d], labelFont) - labelCount[d] = ((data.length/VERTEX_SIZE)|0) - labelOffset[d] - - //Generate sprites for tick marks - tickOffset[d] = (data.length/VERTEX_SIZE)|0 - for(var i=0; i= 0) { - sigFigs = stepStr.length - u - 1 - } - var shift = Math.pow(10, sigFigs) - var x = Math.round(spacing * i * shift) - var xstr = x + "" - if(xstr.indexOf("e") >= 0) { - return xstr - } - var xi = x / shift, xf = x % shift - if(x < 0) { - xi = -Math.ceil(xi)|0 - xf = (-xf)|0 - } else { - xi = Math.floor(xi)|0 - xf = xf|0 - } - var xis = "" + xi - if(x < 0) { - xis = "-" + xis - } - if(sigFigs) { - var xs = "" + xf - while(xs.length < sigFigs) { - xs = "0" + xs - } - return xis + "." + xs - } else { - return xis - } -} - -function defaultTicks(bounds, tickSpacing) { - var array = [] - for(var d=0; d<3; ++d) { - var ticks = [] - var m = 0.5*(bounds[0][d]+bounds[1][d]) - for(var t=0; t*tickSpacing[d]<=bounds[1][d]; ++t) { - ticks.push({x: t*tickSpacing[d], text: prettyPrint(tickSpacing[d], t)}) - } - for(var t=-1; t*tickSpacing[d]>=bounds[0][d]; --t) { - ticks.push({x: t*tickSpacing[d], text: prettyPrint(tickSpacing[d], t)}) - } - array.push(ticks) - } - return array -} - -function ticksEqual(ticksA, ticksB) { - for(var i=0; i<3; ++i) { - if(ticksA[i].length !== ticksB[i].length) { - return false - } - for(var j=0; j 1.0) { - t = 1.0 - } - var ti = 1.0 - t - var n = a.length - var r = new Array(n) - for(var i=0; i 0) || (a > 0 && b < 0)) { - var p = lerpW(s, b, t, a) - pos.push(p) - neg.push(p.slice()) - } - if(b < 0) { - neg.push(t.slice()) - } else if(b > 0) { - pos.push(t.slice()) - } else { - pos.push(t.slice()) - neg.push(t.slice()) - } - a = b - } - return { positive: pos, negative: neg } -} - -function positive(points, plane) { - var pos = [] - var a = planeT(points[points.length-1], plane) - for(var s=points[points.length-1], t=points[0], i=0; i 0) || (a > 0 && b < 0)) { - pos.push(lerpW(s, b, t, a)) - } - if(b >= 0) { - pos.push(t.slice()) - } - a = b - } - return pos -} - -function negative(points, plane) { - var neg = [] - var a = planeT(points[points.length-1], plane) - for(var s=points[points.length-1], t=points[0], i=0; i 0) || (a > 0 && b < 0)) { - neg.push(lerpW(s, b, t, a)) - } - if(b <= 0) { - neg.push(t.slice()) - } - a = b - } - return neg -} -},{"robust-dot-product":452,"robust-sum":454}],452:[function(require,module,exports){ -"use strict" - -var twoProduct = require("two-product") -var robustSum = require("robust-sum") - -module.exports = robustDotProduct - -function robustDotProduct(a, b) { - var r = twoProduct(a[0], b[0]) - for(var i=1; i 0) { - var base = Math.round(Math.pow(10, y)) - return Math.ceil(x/base) * base - } - return Math.ceil(x) -} - -function defaultBool(x) { - if(typeof x === 'boolean') { - return x - } - return true -} - -function createScene(options) { - options = options || {} - - var stopped = false - - var pixelRatio = options.pixelRatio || parseFloat(window.devicePixelRatio) - - var canvas = options.canvas - if(!canvas) { - canvas = document.createElement('canvas') - if(options.container) { - var container = options.container - container.appendChild(canvas) - } else { - document.body.appendChild(canvas) - } - } - - var gl = options.gl - if(!gl) { - gl = getContext(canvas, - options.glOptions || { - premultipliedAlpha: true, - antialias: true - }) - } - if(!gl) { - throw new Error('webgl not supported') - } - - //Initial bounds - var bounds = options.bounds || [[-10,-10,-10], [10,10,10]] - - //Create selection - var selection = new MouseSelect() - - //Accumulation buffer - var accumBuffer = createFBO(gl, - [gl.drawingBufferWidth, gl.drawingBufferHeight], { - preferFloat: true - }) - - var accumShader = createShader(gl) - - //Create a camera - var cameraOptions = options.camera || { - eye: [2,0,0], - center: [0,0,0], - up: [0,1,0], - zoomMin: 0.1, - zoomMax: 100, - mode: 'turntable' - } - - //Create axes - var axesOptions = options.axes || {} - var axes = createAxes(gl, axesOptions) - axes.enable = !axesOptions.disable - - //Create spikes - var spikeOptions = options.spikes || {} - var spikes = createSpikes(gl, spikeOptions) - - //Object list is empty initially - var objects = [] - var pickBufferIds = [] - var pickBufferCount = [] - var pickBuffers = [] - - //Dirty flag, skip redraw if scene static - var dirty = true - var pickDirty = true - - var projection = new Array(16) - var model = new Array(16) - - var cameraParams = { - view: null, - projection: projection, - model: model - } - - var pickDirty = true - - var viewShape = [ gl.drawingBufferWidth, gl.drawingBufferHeight ] - - //Create scene object - var scene = { - gl: gl, - contextLost: false, - pixelRatio: options.pixelRatio || parseFloat(window.devicePixelRatio), - canvas: canvas, - selection: selection, - camera: createCamera(canvas, cameraOptions), - axes: axes, - axesPixels: null, - spikes: spikes, - bounds: bounds, - objects: objects, - shape: viewShape, - aspect: options.aspectRatio || [1,1,1], - pickRadius: options.pickRadius || 10, - zNear: options.zNear || 0.01, - zFar: options.zFar || 1000, - fovy: options.fovy || Math.PI/4, - clearColor: options.clearColor || [0,0,0,0], - autoResize: defaultBool(options.autoResize), - autoBounds: defaultBool(options.autoBounds), - autoScale: !!options.autoScale, - autoCenter: defaultBool(options.autoCenter), - clipToBounds: defaultBool(options.clipToBounds), - snapToData: !!options.snapToData, - onselect: options.onselect || null, - onrender: options.onrender || null, - onclick: options.onclick || null, - cameraParams: cameraParams, - oncontextloss: null, - mouseListener: null - } - - var pickShape = [ (gl.drawingBufferWidth/scene.pixelRatio)|0, (gl.drawingBufferHeight/scene.pixelRatio)|0 ] - - function resizeListener() { - if(stopped) { - return - } - if(!scene.autoResize) { - return - } - var parent = canvas.parentNode - var width = 1 - var height = 1 - if(parent && parent !== document.body) { - width = parent.clientWidth - height = parent.clientHeight - } else { - width = window.innerWidth - height = window.innerHeight - } - var nextWidth = Math.ceil(width * scene.pixelRatio)|0 - var nextHeight = Math.ceil(height * scene.pixelRatio)|0 - if(nextWidth !== canvas.width || nextHeight !== canvas.height) { - canvas.width = nextWidth - canvas.height = nextHeight - var style = canvas.style - style.position = style.position || 'absolute' - style.left = '0px' - style.top = '0px' - style.width = width + 'px' - style.height = height + 'px' - dirty = true - } - } - if(scene.autoResize) { - resizeListener() - } - window.addEventListener('resize', resizeListener) - - function reallocPickIds() { - var numObjs = objects.length - var numPick = pickBuffers.length - for(var i=0; i 0 && pickBufferCount[numPick-1] === 0) { - pickBufferCount.pop() - pickBuffers.pop().dispose() - } - } - - scene.update = function(options) { - if(stopped) { - return - } - options = options || {} - dirty = true - pickDirty = true - } - - scene.add = function(obj) { - if(stopped) { - return - } - obj.axes = axes - objects.push(obj) - pickBufferIds.push(-1) - dirty = true - pickDirty = true - reallocPickIds() - } - - scene.remove = function(obj) { - if(stopped) { - return - } - var idx = objects.indexOf(obj) - if(idx < 0) { - return - } - objects.splice(idx, 1) - pickBufferIds.pop() - dirty = true - pickDirty = true - reallocPickIds() - } - - scene.dispose = function() { - if(stopped) { - return - } - - stopped = true - - window.removeEventListener('resize', resizeListener) - canvas.removeEventListener('webglcontextlost', checkContextLoss) - scene.mouseListener.enabled = false - - if(scene.contextLost) { - return - } - - //Destroy objects - axes.dispose() - spikes.dispose() - for(var i=0; i selection.distance) { - continue - } - for(var j=0; j>> 1 - var dataStraightThrough = options.positions instanceof Float32Array - var idStraightThrough = options.idToIndex instanceof Int32Array && options.idToIndex.length >= pointCount // permit larger to help reuse - - var data = options.positions - var packed = dataStraightThrough ? data : pool.mallocFloat32(data.length) - var packedId = idStraightThrough ? options.idToIndex : pool.mallocInt32(pointCount) - - if(!dataStraightThrough) { - packed.set(data) - } - - if(!idStraightThrough) { - packed.set(data) - for(i = 0; i < pointCount; i++) { - packedId[i] = i - } - } - - this.points = data - - this.offsetBuffer.update(packed) - this.pickBuffer.update(packedId) - - if(!dataStraightThrough) { - pool.free(packed) - } - - if(!idStraightThrough) { - pool.free(packedId) - } - - this.pointCount = pointCount - this.pickOffset = 0 -} - -function count(points, dataBox) { - var visiblePointCountEstimate = 0 - var length = points.length >>> 1 - var i - for(i = 0; i < length; i++) { - var x = points[i * 2] - var y = points[i * 2 + 1] - if(x >= dataBox[0] && x <= dataBox[2] && y >= dataBox[1] && y <= dataBox[3]) - visiblePointCountEstimate++ - } - return visiblePointCountEstimate -} - -proto.unifiedDraw = (function() { - var MATRIX = [1, 0, 0, - 0, 1, 0, - 0, 0, 1] - var PICK_VEC4 = [0, 0, 0, 0] -return function(pickOffset) { - - var pick = pickOffset !== void(0) - - var shader = pick ? this.pickShader : this.shader - var gl = this.plot.gl - var dataBox = this.plot.dataBox - - if(this.pointCount === 0) { - return pickOffset - } - - var dataX = dataBox[2] - dataBox[0] - var dataY = dataBox[3] - dataBox[1] - - var visiblePointCountEstimate = count(this.points, dataBox) - var basicPointSize = this.plot.pickPixelRatio * Math.max(Math.min(this.sizeMinCap, this.sizeMin), Math.min(this.sizeMax, this.sizeMax / Math.pow(visiblePointCountEstimate, 0.33333))) - - MATRIX[0] = 2.0 / dataX - MATRIX[4] = 2.0 / dataY - MATRIX[6] = -2.0 * dataBox[0] / dataX - 1.0 - MATRIX[7] = -2.0 * dataBox[1] / dataY - 1.0 - - this.offsetBuffer.bind() - - shader.bind() - shader.attributes.position.pointer() - shader.uniforms.matrix = MATRIX - shader.uniforms.color = this.color - shader.uniforms.borderColor = this.borderColor - shader.uniforms.pointCloud = basicPointSize < 5 - shader.uniforms.pointSize = basicPointSize - shader.uniforms.centerFraction = Math.min(1, Math.max(0, Math.sqrt(1 - this.areaRatio))) - - if(pick) { - - PICK_VEC4[0] = ( pickOffset & 0xff) - PICK_VEC4[1] = ((pickOffset >> 8) & 0xff) - PICK_VEC4[2] = ((pickOffset >> 16) & 0xff) - PICK_VEC4[3] = ((pickOffset >> 24) & 0xff) - - this.pickBuffer.bind() - shader.attributes.pickId.pointer(gl.UNSIGNED_BYTE) - shader.uniforms.pickOffset = PICK_VEC4 - this.pickOffset = pickOffset - } - - // Worth switching these off, but we can't make assumptions about other - // renderers, so let's restore it after each draw - var blend = gl.getParameter(gl.BLEND) - var dither = gl.getParameter(gl.DITHER) - - if(blend && !this.blend) - gl.disable(gl.BLEND) - if(dither) - gl.disable(gl.DITHER) - - gl.drawArrays(gl.POINTS, 0, this.pointCount) - - if(blend && !this.blend) - gl.enable(gl.BLEND) - if(dither) - gl.enable(gl.DITHER) - - return pickOffset + this.pointCount -} -})() - -proto.draw = proto.unifiedDraw -proto.drawPick = proto.unifiedDraw - -proto.pick = function(x, y, value) { - var pickOffset = this.pickOffset - var pointCount = this.pointCount - if(value < pickOffset || value >= pickOffset + pointCount) { - return null - } - var pointId = value - pickOffset - var points = this.points - return { - object: this, - pointId: pointId, - dataCoord: [points[2 * pointId], points[2 * pointId + 1] ] - } -} - -function createPointcloud2D(plot, options) { - var gl = plot.gl - var buffer = createBuffer(gl) - var pickBuffer = createBuffer(gl) - var shader = createShader(gl, SHADERS.pointVertex, SHADERS.pointFragment) - var pickShader = createShader(gl, SHADERS.pickVertex, SHADERS.pickFragment) - - var result = new Pointcloud2D(plot, buffer, pickBuffer, shader, pickShader) - result.update(options) - - //Register with plot - plot.addObject(result) - - return result -} - -},{"./lib/shader":570,"gl-buffer":571,"gl-shader":572,"typedarray-pool":600}],602:[function(require,module,exports){ -'use strict' - - - -module.exports = { - vertex: "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi, positionLo;\nattribute vec2 offset;\nattribute vec4 color;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale;\n\nvarying vec4 fragColor;\n\n\nvec4 computePosition_1_0(vec2 posHi, vec2 posLo, vec2 scHi, vec2 scLo, vec2 trHi, vec2 trLo, vec2 screenScale, vec2 screenOffset) {\n return vec4((posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo\n + screenScale * screenOffset, 0, 1);\n}\n\nvoid main() {\n fragColor = color;\n\n gl_Position = computePosition_1_0(\n positionHi, positionLo,\n scaleHi, scaleLo,\n translateHi, translateLo,\n pixelScale, offset);\n}\n", - fragment: "precision lowp float;\n#define GLSLIFY 1\nvarying vec4 fragColor;\nvoid main() {\n gl_FragColor = vec4(fragColor.rgb * fragColor.a, fragColor.a);\n}\n", - pickVertex: "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi, positionLo;\nattribute vec2 offset;\nattribute vec4 id;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale;\nuniform vec4 pickOffset;\n\nvarying vec4 fragColor;\n\n\nvec4 computePosition_1_0(vec2 posHi, vec2 posLo, vec2 scHi, vec2 scLo, vec2 trHi, vec2 trLo, vec2 screenScale, vec2 screenOffset) {\n return vec4((posHi + trHi) * scHi\n + (posLo + trLo) * scHi\n + (posHi + trHi) * scLo\n + (posLo + trLo) * scLo\n + screenScale * screenOffset, 0, 1);\n}\n\nvoid main() {\n vec4 fragId = id + pickOffset;\n\n fragId.y += floor(fragId.x / 256.0);\n fragId.x -= floor(fragId.x / 256.0) * 256.0;\n\n fragId.z += floor(fragId.y / 256.0);\n fragId.y -= floor(fragId.y / 256.0) * 256.0;\n\n fragId.w += floor(fragId.z / 256.0);\n fragId.z -= floor(fragId.z / 256.0) * 256.0;\n\n fragColor = fragId / 255.0;\n\n gl_Position = computePosition_1_0(\n positionHi, positionLo,\n scaleHi, scaleLo,\n translateHi, translateLo,\n pixelScale, offset);\n}\n", - pickFragment: "precision lowp float;\n#define GLSLIFY 1\nvarying vec4 fragColor;\nvoid main() {\n gl_FragColor = fragColor;\n}\n" -} - -},{}],603:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":633}],604:[function(require,module,exports){ -arguments[4][297][0].apply(exports,arguments) -},{"./lib/GLError":605,"./lib/create-attributes":606,"./lib/create-uniforms":607,"./lib/reflect":608,"./lib/runtime-reflect":609,"./lib/shader-cache":610,"dup":297}],605:[function(require,module,exports){ -arguments[4][298][0].apply(exports,arguments) -},{"dup":298}],606:[function(require,module,exports){ -arguments[4][299][0].apply(exports,arguments) -},{"./GLError":605,"dup":299}],607:[function(require,module,exports){ -arguments[4][300][0].apply(exports,arguments) -},{"./GLError":605,"./reflect":608,"dup":300}],608:[function(require,module,exports){ -arguments[4][301][0].apply(exports,arguments) -},{"dup":301}],609:[function(require,module,exports){ -arguments[4][302][0].apply(exports,arguments) -},{"dup":302}],610:[function(require,module,exports){ -arguments[4][303][0].apply(exports,arguments) -},{"./GLError":605,"dup":303,"gl-format-compiler-error":611,"weakmap-shim":629}],611:[function(require,module,exports){ -arguments[4][304][0].apply(exports,arguments) -},{"add-line-numbers":612,"dup":304,"gl-constants/lookup":616,"glsl-shader-name":617,"sprintf-js":626}],612:[function(require,module,exports){ -arguments[4][305][0].apply(exports,arguments) -},{"dup":305,"pad-left":613}],613:[function(require,module,exports){ -arguments[4][306][0].apply(exports,arguments) -},{"dup":306,"repeat-string":614}],614:[function(require,module,exports){ -arguments[4][307][0].apply(exports,arguments) -},{"dup":307}],615:[function(require,module,exports){ -arguments[4][308][0].apply(exports,arguments) -},{"dup":308}],616:[function(require,module,exports){ -arguments[4][309][0].apply(exports,arguments) -},{"./1.0/numbers":615,"dup":309}],617:[function(require,module,exports){ -arguments[4][310][0].apply(exports,arguments) -},{"atob-lite":618,"dup":310,"glsl-tokenizer":625}],618:[function(require,module,exports){ -arguments[4][311][0].apply(exports,arguments) -},{"dup":311}],619:[function(require,module,exports){ -arguments[4][312][0].apply(exports,arguments) -},{"./lib/builtins":621,"./lib/builtins-300es":620,"./lib/literals":623,"./lib/literals-300es":622,"./lib/operators":624,"dup":312}],620:[function(require,module,exports){ -arguments[4][313][0].apply(exports,arguments) -},{"./builtins":621,"dup":313}],621:[function(require,module,exports){ -arguments[4][314][0].apply(exports,arguments) -},{"dup":314}],622:[function(require,module,exports){ -arguments[4][315][0].apply(exports,arguments) -},{"./literals":623,"dup":315}],623:[function(require,module,exports){ -arguments[4][316][0].apply(exports,arguments) -},{"dup":316}],624:[function(require,module,exports){ -arguments[4][317][0].apply(exports,arguments) -},{"dup":317}],625:[function(require,module,exports){ -arguments[4][318][0].apply(exports,arguments) -},{"./index":619,"dup":318}],626:[function(require,module,exports){ -arguments[4][319][0].apply(exports,arguments) -},{"dup":319}],627:[function(require,module,exports){ -arguments[4][320][0].apply(exports,arguments) -},{"./hidden-store.js":628,"dup":320}],628:[function(require,module,exports){ -arguments[4][321][0].apply(exports,arguments) -},{"dup":321}],629:[function(require,module,exports){ -arguments[4][322][0].apply(exports,arguments) -},{"./create-store.js":627,"dup":322}],630:[function(require,module,exports){ -arguments[4][420][0].apply(exports,arguments) -},{"_process":41,"dup":420,"vectorize-text":634}],631:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],632:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],633:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":631,"buffer":33,"dup":187}],634:[function(require,module,exports){ -arguments[4][323][0].apply(exports,arguments) -},{"./lib/vtext":635,"dup":323}],635:[function(require,module,exports){ -arguments[4][324][0].apply(exports,arguments) -},{"cdt2d":636,"clean-pslg":648,"dup":324,"ndarray":1118,"planar-graph-to-polyline":703,"simplify-planar-graph":707,"surface-nets":720}],636:[function(require,module,exports){ -arguments[4][325][0].apply(exports,arguments) -},{"./lib/delaunay":637,"./lib/filter":638,"./lib/monotone":639,"./lib/triangulation":640,"dup":325}],637:[function(require,module,exports){ -arguments[4][326][0].apply(exports,arguments) -},{"binary-search-bounds":641,"dup":326,"robust-in-sphere":642}],638:[function(require,module,exports){ -arguments[4][327][0].apply(exports,arguments) -},{"binary-search-bounds":641,"dup":327}],639:[function(require,module,exports){ -arguments[4][328][0].apply(exports,arguments) -},{"binary-search-bounds":641,"dup":328,"robust-orientation":1127}],640:[function(require,module,exports){ -arguments[4][329][0].apply(exports,arguments) -},{"binary-search-bounds":641,"dup":329}],641:[function(require,module,exports){ -arguments[4][200][0].apply(exports,arguments) -},{"dup":200}],642:[function(require,module,exports){ -arguments[4][330][0].apply(exports,arguments) -},{"dup":330,"robust-scale":644,"robust-subtract":645,"robust-sum":646,"two-product":647}],643:[function(require,module,exports){ -arguments[4][150][0].apply(exports,arguments) -},{"dup":150}],644:[function(require,module,exports){ -arguments[4][151][0].apply(exports,arguments) -},{"dup":151,"two-product":647,"two-sum":643}],645:[function(require,module,exports){ -arguments[4][333][0].apply(exports,arguments) -},{"dup":333}],646:[function(require,module,exports){ -arguments[4][152][0].apply(exports,arguments) -},{"dup":152}],647:[function(require,module,exports){ -arguments[4][153][0].apply(exports,arguments) -},{"dup":153}],648:[function(require,module,exports){ -arguments[4][336][0].apply(exports,arguments) -},{"./lib/rat-seg-intersect":649,"big-rat":653,"big-rat/cmp":651,"big-rat/to-float":668,"box-intersect":669,"compare-cell":677,"dup":336,"nextafter":678,"rat-vec":681,"robust-segment-intersect":684,"union-find":685}],649:[function(require,module,exports){ -arguments[4][337][0].apply(exports,arguments) -},{"big-rat/div":652,"big-rat/mul":662,"big-rat/sign":666,"big-rat/sub":667,"big-rat/to-float":668,"dup":337,"rat-vec/add":680,"rat-vec/muls":682,"rat-vec/sub":683}],650:[function(require,module,exports){ -arguments[4][338][0].apply(exports,arguments) -},{"./lib/rationalize":660,"dup":338}],651:[function(require,module,exports){ -arguments[4][339][0].apply(exports,arguments) -},{"dup":339}],652:[function(require,module,exports){ -arguments[4][340][0].apply(exports,arguments) -},{"./lib/rationalize":660,"dup":340}],653:[function(require,module,exports){ -arguments[4][341][0].apply(exports,arguments) -},{"./div":652,"./is-rat":654,"./lib/is-bn":658,"./lib/num-to-bn":659,"./lib/rationalize":660,"./lib/str-to-bn":661,"dup":341}],654:[function(require,module,exports){ -arguments[4][342][0].apply(exports,arguments) -},{"./lib/is-bn":658,"dup":342}],655:[function(require,module,exports){ -arguments[4][343][0].apply(exports,arguments) -},{"bn.js":664,"dup":343}],656:[function(require,module,exports){ -arguments[4][344][0].apply(exports,arguments) -},{"dup":344}],657:[function(require,module,exports){ -arguments[4][345][0].apply(exports,arguments) -},{"bit-twiddle":663,"double-bits":665,"dup":345}],658:[function(require,module,exports){ -arguments[4][346][0].apply(exports,arguments) -},{"bn.js":664,"dup":346}],659:[function(require,module,exports){ -arguments[4][347][0].apply(exports,arguments) -},{"bn.js":664,"double-bits":665,"dup":347}],660:[function(require,module,exports){ -arguments[4][348][0].apply(exports,arguments) -},{"./bn-sign":655,"./num-to-bn":659,"dup":348}],661:[function(require,module,exports){ -arguments[4][349][0].apply(exports,arguments) -},{"bn.js":664,"dup":349}],662:[function(require,module,exports){ -arguments[4][350][0].apply(exports,arguments) -},{"./lib/rationalize":660,"dup":350}],663:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],664:[function(require,module,exports){ -arguments[4][352][0].apply(exports,arguments) -},{"dup":352}],665:[function(require,module,exports){ -arguments[4][353][0].apply(exports,arguments) -},{"buffer":33,"dup":353}],666:[function(require,module,exports){ -arguments[4][354][0].apply(exports,arguments) -},{"./lib/bn-sign":655,"dup":354}],667:[function(require,module,exports){ -arguments[4][355][0].apply(exports,arguments) -},{"./lib/rationalize":660,"dup":355}],668:[function(require,module,exports){ -arguments[4][356][0].apply(exports,arguments) -},{"./lib/bn-to-num":656,"./lib/ctz":657,"dup":356}],669:[function(require,module,exports){ -arguments[4][357][0].apply(exports,arguments) -},{"./lib/intersect":671,"./lib/sweep":675,"dup":357,"typedarray-pool":633}],670:[function(require,module,exports){ -arguments[4][358][0].apply(exports,arguments) -},{"dup":358}],671:[function(require,module,exports){ -arguments[4][359][0].apply(exports,arguments) -},{"./brute":670,"./median":672,"./partition":673,"./sweep":675,"bit-twiddle":676,"dup":359,"typedarray-pool":633}],672:[function(require,module,exports){ -arguments[4][360][0].apply(exports,arguments) -},{"./partition":673,"dup":360}],673:[function(require,module,exports){ -arguments[4][361][0].apply(exports,arguments) -},{"dup":361}],674:[function(require,module,exports){ -arguments[4][362][0].apply(exports,arguments) -},{"dup":362}],675:[function(require,module,exports){ -arguments[4][363][0].apply(exports,arguments) -},{"./sort":674,"bit-twiddle":676,"dup":363,"typedarray-pool":633}],676:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],677:[function(require,module,exports){ -arguments[4][158][0].apply(exports,arguments) -},{"dup":158}],678:[function(require,module,exports){ -arguments[4][368][0].apply(exports,arguments) -},{"double-bits":679,"dup":368}],679:[function(require,module,exports){ -arguments[4][353][0].apply(exports,arguments) -},{"buffer":33,"dup":353}],680:[function(require,module,exports){ -arguments[4][370][0].apply(exports,arguments) -},{"big-rat/add":650,"dup":370}],681:[function(require,module,exports){ -arguments[4][371][0].apply(exports,arguments) -},{"big-rat":653,"dup":371}],682:[function(require,module,exports){ -arguments[4][372][0].apply(exports,arguments) -},{"big-rat":653,"big-rat/mul":662,"dup":372}],683:[function(require,module,exports){ -arguments[4][373][0].apply(exports,arguments) -},{"big-rat/sub":667,"dup":373}],684:[function(require,module,exports){ -arguments[4][374][0].apply(exports,arguments) -},{"dup":374,"robust-orientation":1127}],685:[function(require,module,exports){ -arguments[4][169][0].apply(exports,arguments) -},{"dup":169}],686:[function(require,module,exports){ -arguments[4][376][0].apply(exports,arguments) -},{"dup":376,"edges-to-adjacency-list":687}],687:[function(require,module,exports){ -arguments[4][377][0].apply(exports,arguments) -},{"dup":377,"uniq":702}],688:[function(require,module,exports){ -arguments[4][378][0].apply(exports,arguments) -},{"compare-angle":689,"dup":378}],689:[function(require,module,exports){ -arguments[4][379][0].apply(exports,arguments) -},{"dup":379,"robust-orientation":1127,"robust-product":691,"robust-sum":700,"signum":692,"two-sum":693}],690:[function(require,module,exports){ -arguments[4][151][0].apply(exports,arguments) -},{"dup":151,"two-product":701,"two-sum":693}],691:[function(require,module,exports){ -arguments[4][381][0].apply(exports,arguments) -},{"dup":381,"robust-scale":690,"robust-sum":700}],692:[function(require,module,exports){ -arguments[4][382][0].apply(exports,arguments) -},{"dup":382}],693:[function(require,module,exports){ -arguments[4][150][0].apply(exports,arguments) -},{"dup":150}],694:[function(require,module,exports){ -arguments[4][118][0].apply(exports,arguments) -},{"dup":118}],695:[function(require,module,exports){ -arguments[4][385][0].apply(exports,arguments) -},{"binary-search-bounds":694,"dup":385}],696:[function(require,module,exports){ -arguments[4][386][0].apply(exports,arguments) -},{"dup":386,"robust-orientation":1127}],697:[function(require,module,exports){ -arguments[4][387][0].apply(exports,arguments) -},{"dup":387}],698:[function(require,module,exports){ -arguments[4][388][0].apply(exports,arguments) -},{"./lib/order-segments":696,"binary-search-bounds":694,"dup":388,"functional-red-black-tree":697,"robust-orientation":1127}],699:[function(require,module,exports){ -arguments[4][389][0].apply(exports,arguments) -},{"binary-search-bounds":694,"dup":389,"interval-tree-1d":695,"robust-orientation":1127,"slab-decomposition":698}],700:[function(require,module,exports){ -arguments[4][152][0].apply(exports,arguments) -},{"dup":152}],701:[function(require,module,exports){ -arguments[4][153][0].apply(exports,arguments) -},{"dup":153}],702:[function(require,module,exports){ -arguments[4][76][0].apply(exports,arguments) -},{"dup":76}],703:[function(require,module,exports){ -arguments[4][393][0].apply(exports,arguments) -},{"./lib/trim-leaves":686,"dup":393,"edges-to-adjacency-list":687,"planar-dual":688,"point-in-big-polygon":699,"robust-sum":700,"two-product":701,"uniq":702}],704:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],705:[function(require,module,exports){ -arguments[4][395][0].apply(exports,arguments) -},{"dup":395}],706:[function(require,module,exports){ -arguments[4][170][0].apply(exports,arguments) -},{"bit-twiddle":704,"dup":170,"union-find":705}],707:[function(require,module,exports){ -arguments[4][397][0].apply(exports,arguments) -},{"dup":397,"robust-orientation":1127,"simplicial-complex":706}],708:[function(require,module,exports){ -arguments[4][398][0].apply(exports,arguments) -},{"dup":398,"typedarray-pool":633}],709:[function(require,module,exports){ -arguments[4][402][0].apply(exports,arguments) -},{"dup":402}],710:[function(require,module,exports){ -arguments[4][406][0].apply(exports,arguments) -},{"dup":406,"typedarray-pool":633}],711:[function(require,module,exports){ -arguments[4][407][0].apply(exports,arguments) -},{"dup":407,"invert-permutation":712,"typedarray-pool":633}],712:[function(require,module,exports){ -arguments[4][408][0].apply(exports,arguments) -},{"dup":408}],713:[function(require,module,exports){ -arguments[4][412][0].apply(exports,arguments) -},{"dup":412,"gamma":709,"permutation-parity":710,"permutation-rank":711}],714:[function(require,module,exports){ -arguments[4][413][0].apply(exports,arguments) -},{"cwise-compiler":715,"dup":413}],715:[function(require,module,exports){ -arguments[4][73][0].apply(exports,arguments) -},{"./lib/thunk.js":717,"dup":73}],716:[function(require,module,exports){ -arguments[4][74][0].apply(exports,arguments) -},{"dup":74,"uniq":718}],717:[function(require,module,exports){ -arguments[4][75][0].apply(exports,arguments) -},{"./compile.js":716,"dup":75}],718:[function(require,module,exports){ -arguments[4][76][0].apply(exports,arguments) -},{"dup":76}],719:[function(require,module,exports){ -arguments[4][418][0].apply(exports,arguments) -},{"./lib/zc-core":714,"dup":418}],720:[function(require,module,exports){ -arguments[4][419][0].apply(exports,arguments) -},{"dup":419,"ndarray-extract-contour":708,"triangulate-hypercube":713,"zero-crossings":719}],721:[function(require,module,exports){ -'use strict' - -module.exports = createFancyScatter2D - -var createShader = require('gl-shader') -var createBuffer = require('gl-buffer') -var textCache = require('text-cache') -var pool = require('typedarray-pool') -var vectorizeText = require('vectorize-text') -var shaders = require('./lib/shaders') - -var BOUNDARIES = {} - -function getBoundary(glyph) { - if(glyph in BOUNDARIES) { - return BOUNDARIES[glyph] - } - - var polys = vectorizeText(glyph, { - polygons: true, - font: 'sans-serif', - textAlign: 'left', - textBaseline: 'alphabetic' - }) - - var coords = [] - var normals = [] - - polys.forEach(function(loops) { - loops.forEach(function(loop) { - for(var i=0; i < loop.length; ++i) { - var a = loop[(i + loop.length - 1) % loop.length] - var b = loop[i] - var c = loop[(i + 1) % loop.length] - var d = loop[(i + 2) % loop.length] - - var dx = b[0] - a[0] - var dy = b[1] - a[1] - var dl = Math.sqrt(dx * dx + dy * dy) - dx /= dl - dy /= dl - - coords.push(a[0], a[1] + 1.4) - normals.push(dy, -dx) - coords.push(a[0], a[1] + 1.4) - normals.push(-dy, dx) - coords.push(b[0], b[1] + 1.4) - normals.push(-dy, dx) - - coords.push(b[0], b[1] + 1.4) - normals.push(-dy, dx) - coords.push(a[0], a[1] + 1.4) - normals.push(dy, -dx) - coords.push(b[0], b[1] + 1.4) - normals.push(dy, -dx) - - var ex = d[0] - c[0] - var ey = d[1] - c[1] - var el = Math.sqrt(ex * ex + ey * ey) - ex /= el - ey /= el - - coords.push(b[0], b[1] + 1.4) - normals.push(dy, -dx) - coords.push(b[0], b[1] + 1.4) - normals.push(-dy, dx) - coords.push(c[0], c[1] + 1.4) - normals.push(-ey, ex) - - coords.push(c[0], c[1] + 1.4) - normals.push(-ey, ex) - coords.push(b[0], b[1] + 1.4) - normals.push(ey, -ex) - coords.push(c[0], c[1] + 1.4) - normals.push(ey, -ex) - } - }) - }) - - var bounds = [Infinity, Infinity, -Infinity, -Infinity] - for(var i = 0; i < coords.length; i += 2) { - for(var j = 0; j < 2; ++j) { - bounds[j] = Math.min(bounds[j], coords[i + j]) - bounds[2 + j] = Math.max(bounds[2 + j], coords[i + j]) - } - } - - return BOUNDARIES[glyph] = { - coords: coords, - normals: normals, - bounds: bounds - } -} - -function GLScatterFancy( - plot, - shader, - pickShader, - positionHiBuffer, - positionLoBuffer, - offsetBuffer, - colorBuffer, - idBuffer) { - this.plot = plot - this.shader = shader - this.pickShader = pickShader - this.posHiBuffer = positionHiBuffer - this.posLoBuffer = positionLoBuffer - this.offsetBuffer = offsetBuffer - this.colorBuffer = colorBuffer - this.idBuffer = idBuffer - this.bounds = [Infinity, Infinity, -Infinity, -Infinity] - this.numPoints = 0 - this.numVertices = 0 - this.pickOffset = 0 - this.points = null -} - -var proto = GLScatterFancy.prototype - -;(function() { - var SCALE_HI = new Float32Array([0, 0]) - var SCALE_LO = new Float32Array([0, 0]) - var TRANSLATE_HI = new Float32Array([0, 0]) - var TRANSLATE_LO = new Float32Array([0, 0]) - - var PIXEL_SCALE = [0, 0] - - function calcScales() { - var plot = this.plot - var bounds = this.bounds - var viewBox = plot.viewBox - var dataBox = plot.dataBox - var pixelRatio = plot.pixelRatio - - var boundX = bounds[2] - bounds[0] - var boundY = bounds[3] - bounds[1] - var dataX = dataBox[2] - dataBox[0] - var dataY = dataBox[3] - dataBox[1] - - var scaleX = 2 * boundX / dataX - var scaleY = 2 * boundY / dataY - var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX - var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY - - SCALE_HI[0] = scaleX - SCALE_LO[0] = scaleX - SCALE_HI[0] - SCALE_HI[1] = scaleY - SCALE_LO[1] = scaleY - SCALE_HI[1] - - TRANSLATE_HI[0] = translateX - TRANSLATE_LO[0] = translateX - TRANSLATE_HI[0] - TRANSLATE_HI[1] = translateY - TRANSLATE_LO[1] = translateY - TRANSLATE_HI[1] - - var screenX = viewBox[2] - viewBox[0] - var screenY = viewBox[3] - viewBox[1] - - PIXEL_SCALE[0] = 2 * pixelRatio / screenX - PIXEL_SCALE[1] = 2 * pixelRatio / screenY - } - - var PICK_OFFSET = [0, 0, 0, 0] - - proto.drawPick = function(offset) { - - var pick = offset !== undefined - var plot = this.plot - - var numVertices = this.numVertices - - if(!numVertices) { - return offset - } - - calcScales.call(this) - - var gl = plot.gl - var shader = pick ? this.pickShader : this.shader - - shader.bind() - - if(pick) { - - this.pickOffset = offset - - for (var i = 0; i < 4; ++i) { - PICK_OFFSET[i] = (offset >> (i * 8)) & 0xff - } - - shader.uniforms.pickOffset = PICK_OFFSET - - this.idBuffer.bind() - shader.attributes.id.pointer(gl.UNSIGNED_BYTE, false) - - } else { - - this.colorBuffer.bind() - shader.attributes.color.pointer(gl.UNSIGNED_BYTE, true) - - } - - this.posHiBuffer.bind() - shader.attributes.positionHi.pointer() - - this.posLoBuffer.bind() - shader.attributes.positionLo.pointer() - - this.offsetBuffer.bind() - shader.attributes.offset.pointer() - - shader.uniforms.pixelScale = PIXEL_SCALE - shader.uniforms.scaleHi = SCALE_HI - shader.uniforms.scaleLo = SCALE_LO - shader.uniforms.translateHi = TRANSLATE_HI - shader.uniforms.translateLo = TRANSLATE_LO - - gl.drawArrays(gl.TRIANGLES, 0, numVertices) - - if(pick) return offset + this.numPoints - } -})() - -proto.draw = proto.drawPick - -proto.pick = function(x, y, value) { - var pickOffset = this.pickOffset - var pointCount = this.numPoints - if(value < pickOffset || value >= pickOffset + pointCount) { - return null - } - var pointId = value - pickOffset - var points = this.points - return { - object: this, - pointId: pointId, - dataCoord: [points[2 * pointId], points[2 * pointId + 1]] - } -} - -proto.update = function(options) { - options = options || {} - - var positions = options.positions || [] - var colors = options.colors || [] - var glyphs = options.glyphs || [] - var sizes = options.sizes || [] - var borderWidths = options.borderWidths || [] - var borderColors = options.borderColors || [] - var i, j - - this.points = positions - - var bounds = this.bounds = [Infinity, Infinity, -Infinity, -Infinity] - var numVertices = 0 - - var glyphMeshes = [] - var glyphBoundaries = [] - var glyph, border - - for(i = 0; i < glyphs.length; ++i) { - glyph = textCache('sans-serif', glyphs[i]) - border = getBoundary(glyphs[i]) - glyphMeshes.push(glyph) - glyphBoundaries.push(border) - numVertices += (glyph.data.length + border.coords.length) >> 1 - for(j = 0; j < 2; ++j) { - bounds[j] = Math.min(bounds[j], positions[2 * i + j]) - bounds[2 + j] = Math.max(bounds[2 + j], positions[2 * i + j]) - } - } - - if(bounds[0] === bounds[2]) { - bounds[2] += 1 - } - if(bounds[3] === bounds[1]) { - bounds[3] += 1 - } - - var sx = 1 / (bounds[2] - bounds[0]) - var sy = 1 / (bounds[3] - bounds[1]) - var tx = bounds[0] - var ty = bounds[1] - - var v_position = pool.mallocFloat64(2 * numVertices) - var v_posHi = pool.mallocFloat32(2 * numVertices) - var v_posLo = pool.mallocFloat32(2 * numVertices) - var v_offset = pool.mallocFloat32(2 * numVertices) - var v_color = pool.mallocUint8(4 * numVertices) - var v_ids = pool.mallocUint32(numVertices) - var ptr = 0 - - for(i = 0; i < glyphs.length; ++i) { - glyph = glyphMeshes[i] - border = glyphBoundaries[i] - var x = sx * (positions[2 * i] - tx) - var y = sy * (positions[2 * i + 1] - ty) - var s = sizes[i] - var r = colors[4 * i] * 255 - var g = colors[4 * i + 1] * 255 - var b = colors[4 * i + 2] * 255 - var a = colors[4 * i + 3] * 255 - - var gx = 0.5 * (border.bounds[0] + border.bounds[2]) - var gy = 0.5 * (border.bounds[1] + border.bounds[3]) - - for(j = 0; j < glyph.data.length; j += 2) { - v_position[2 * ptr] = x - v_position[2 * ptr + 1] = y - v_offset[2 * ptr] = -s * (glyph.data[j] - gx) - v_offset[2 * ptr + 1] = -s * (glyph.data[j + 1] - gy) - v_color[4 * ptr] = r - v_color[4 * ptr + 1] = g - v_color[4 * ptr + 2] = b - v_color[4 * ptr + 3] = a - v_ids[ptr] = i - - ptr += 1 - } - - var w = borderWidths[i] - r = borderColors[4 * i] * 255 - g = borderColors[4 * i + 1] * 255 - b = borderColors[4 * i + 2] * 255 - a = borderColors[4 * i + 3] * 255 - - for(j = 0; j < border.coords.length; j += 2) { - v_position[2 * ptr] = x - v_position[2 * ptr + 1] = y - v_offset[2 * ptr] = - (s * (border.coords[j] - gx) + w * border.normals[j]) - v_offset[2 * ptr + 1] = - (s * (border.coords[j + 1] - gy) + w * border.normals[j + 1]) - v_color[4 * ptr] = r - v_color[4 * ptr + 1] = g - v_color[4 * ptr + 2] = b - v_color[4 * ptr + 3] = a - v_ids[ptr] = i - - ptr += 1 - } - } - - this.numPoints = glyphs.length - this.numVertices = numVertices - - v_posHi.set(v_position) - for(i = 0; i < v_position.length; i++) - v_posLo[i] = v_position[i] - v_posHi[i] - - this.posHiBuffer.update(v_posHi) - this.posLoBuffer.update(v_posLo) - this.offsetBuffer.update(v_offset) - this.colorBuffer.update(v_color) - this.idBuffer.update(v_ids) - - pool.free(v_position) - pool.free(v_posHi) - pool.free(v_posLo) - pool.free(v_offset) - pool.free(v_color) - pool.free(v_ids) -} - -proto.dispose = function() { - this.shader.dispose() - this.pickShader.dispose() - this.posHiBuffer.dispose() - this.posLoBuffer.dispose() - this.offsetBuffer.dispose() - this.colorBuffer.dispose() - this.idBuffer.dispose() - this.plot.removeObject(this) -} - -function createFancyScatter2D(plot, options) { - var gl = plot.gl - - var shader = createShader(gl, shaders.vertex, shaders.fragment) - var pickShader = createShader(gl, shaders.pickVertex, shaders.pickFragment) - - var positionHiBuffer = createBuffer(gl) - var positionLoBuffer = createBuffer(gl) - var offsetBuffer = createBuffer(gl) - var colorBuffer = createBuffer(gl) - var idBuffer = createBuffer(gl) - - var scatter = new GLScatterFancy( - plot, - shader, - pickShader, - positionHiBuffer, - positionLoBuffer, - offsetBuffer, - colorBuffer, - idBuffer) - - scatter.update(options) - - plot.addObject(scatter) - - return scatter -} -},{"./lib/shaders":602,"gl-buffer":603,"gl-shader":604,"text-cache":630,"typedarray-pool":633,"vectorize-text":634}],722:[function(require,module,exports){ - - -exports.pointVertex = "precision highp float;\n#define GLSLIFY 1\n\nattribute vec2 positionHi, positionLo;\nattribute float weight;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo;\nuniform float pointSize, useWeight;\n\nvarying float fragWeight;\n\n\nvec4 pfx_1_0(vec2 scaleHi, vec2 scaleLo, vec2 translateHi, vec2 translateLo, vec2 positionHi, vec2 positionLo) {\n return vec4((positionHi + translateHi) * scaleHi\n + (positionLo + translateLo) * scaleHi\n + (positionHi + translateHi) * scaleLo\n + (positionLo + translateLo) * scaleLo, 0.0, 1.0);\n}\n\nvoid main() {\n gl_Position = pfx_1_0(scaleHi, scaleLo, translateHi, translateLo, positionHi, positionLo);\n gl_PointSize = pointSize;\n fragWeight = mix(1.0, weight, useWeight);\n}" -exports.pointFragment = "precision mediump float;\n#define GLSLIFY 1\n\nuniform vec4 color, borderColor;\nuniform float centerFraction;\n\nvarying float fragWeight;\n\nfloat smoothStep(float x, float y) {\n return 1.0 / (1.0 + exp(50.0*(x - y)));\n}\n\nvoid main() {\n float radius = length(2.0*gl_PointCoord.xy-1.0);\n if(radius > 1.0) {\n discard;\n }\n vec4 baseColor = mix(borderColor, color, smoothStep(radius, centerFraction));\n float alpha = 1.0 - pow(1.0 - baseColor.a, fragWeight);\n gl_FragColor = vec4(baseColor.rgb * alpha, alpha);\n}" -exports.pickVertex = "precision highp float;\n#define GLSLIFY 1\n\nvec4 pfx_1_0(vec2 scaleHi, vec2 scaleLo, vec2 translateHi, vec2 translateLo, vec2 positionHi, vec2 positionLo) {\n return vec4((positionHi + translateHi) * scaleHi\n + (positionLo + translateLo) * scaleHi\n + (positionHi + translateHi) * scaleLo\n + (positionLo + translateLo) * scaleLo, 0.0, 1.0);\n}\n\nattribute vec2 positionHi, positionLo;\nattribute vec4 pickId;\n\nuniform vec2 scaleHi, scaleLo, translateHi, translateLo;\nuniform float pointSize;\nuniform vec4 pickOffset;\n\nvarying vec4 fragId;\n\nvoid main() {\n\n vec4 id = pickId + pickOffset;\n id.y += floor(id.x / 256.0);\n id.x -= floor(id.x / 256.0) * 256.0;\n\n id.z += floor(id.y / 256.0);\n id.y -= floor(id.y / 256.0) * 256.0;\n\n id.w += floor(id.z / 256.0);\n id.z -= floor(id.z / 256.0) * 256.0;\n\n gl_Position = pfx_1_0(scaleHi, scaleLo, translateHi, translateLo, positionHi, positionLo);\n gl_PointSize = pointSize;\n fragId = id;\n}" -exports.pickFragment = "precision mediump float;\n#define GLSLIFY 1\n\nvarying vec4 fragId;\n\nvoid main() {\n float radius = length(2.0 * gl_PointCoord.xy - 1.0);\n if(radius > 1.0) {\n discard;\n }\n gl_FragColor = fragId / 255.0;\n}" -},{}],723:[function(require,module,exports){ -arguments[4][200][0].apply(exports,arguments) -},{"dup":200}],724:[function(require,module,exports){ -arguments[4][184][0].apply(exports,arguments) -},{"dup":184,"ndarray":1118,"ndarray-ops":1113,"typedarray-pool":729}],725:[function(require,module,exports){ -'use strict' - -module.exports = sortLevels - -var INSERT_SORT_CUTOFF = 32 - -function sortLevels(data_levels, data_points, data_ids, data_weights, n0) { - if (n0 <= 4*INSERT_SORT_CUTOFF) { - insertionSort(0, n0 - 1, data_levels, data_points, data_ids, data_weights) - } else { - quickSort(0, n0 - 1, data_levels, data_points, data_ids, data_weights) - } -} - -function insertionSort(left, right, data_levels, data_points, data_ids, data_weights) { - for(var i=left+1; i<=right; ++i) { - var a_level = data_levels[i] - var a_x = data_points[2*i] - var a_y = data_points[2*i+1] - var a_id = data_ids[i] - var a_weight = data_weights[i] - - var j = i - while(j > left) { - var b_level = data_levels[j-1] - var b_x = data_points[2*(j-1)] - if(((b_level - a_level) || (a_x - b_x)) >= 0) { - break - } - data_levels[j] = b_level - data_points[2*j] = b_x - data_points[2*j+1] = data_points[2*j-1] - data_ids[j] = data_ids[j-1] - data_weights[j] = data_weights[j-1] - j -= 1 - } - - data_levels[j] = a_level - data_points[2*j] = a_x - data_points[2*j+1] = a_y - data_ids[j] = a_id - data_weights[j] = a_weight - } -} - -function swap(i, j, data_levels, data_points, data_ids, data_weights) { - var a_level = data_levels[i] - var a_x = data_points[2*i] - var a_y = data_points[2*i+1] - var a_id = data_ids[i] - var a_weight = data_weights[i] - - data_levels[i] = data_levels[j] - data_points[2*i] = data_points[2*j] - data_points[2*i+1] = data_points[2*j+1] - data_ids[i] = data_ids[j] - data_weights[i] = data_weights[j] - - data_levels[j] = a_level - data_points[2*j] = a_x - data_points[2*j+1] = a_y - data_ids[j] = a_id - data_weights[j] = a_weight -} - -function move(i, j, data_levels, data_points, data_ids, data_weights) { - data_levels[i] = data_levels[j] - data_points[2*i] = data_points[2*j] - data_points[2*i+1] = data_points[2*j+1] - data_ids[i] = data_ids[j] - data_weights[i] = data_weights[j] -} - -function rotate(i, j, k, data_levels, data_points, data_ids, data_weights) { - var a_level = data_levels[i] - var a_x = data_points[2*i] - var a_y = data_points[2*i+1] - var a_id = data_ids[i] - var a_weight = data_weights[i] - - data_levels[i] = data_levels[j] - data_points[2*i] = data_points[2*j] - data_points[2*i+1] = data_points[2*j+1] - data_ids[i] = data_ids[j] - data_weights[i] = data_weights[j] - - data_levels[j] = data_levels[k] - data_points[2*j] = data_points[2*k] - data_points[2*j+1] = data_points[2*k+1] - data_ids[j] = data_ids[k] - data_weights[j] = data_weights[k] - - data_levels[k] = a_level - data_points[2*k] = a_x - data_points[2*k+1] = a_y - data_ids[k] = a_id - data_weights[k] = a_weight -} - -function shufflePivot( - i, j, - a_level, a_x, a_y, a_id, a_weight, - data_levels, data_points, data_ids, data_weights) { - - data_levels[i] = data_levels[j] - data_points[2*i] = data_points[2*j] - data_points[2*i+1] = data_points[2*j+1] - data_ids[i] = data_ids[j] - data_weights[i] = data_weights[j] - - data_levels[j] = a_level - data_points[2*j] = a_x - data_points[2*j+1] = a_y - data_ids[j] = a_id - data_weights[j] = a_weight -} - -function compare(i, j, data_levels, data_points, data_ids) { - return ((data_levels[i] - data_levels[j]) || - (data_points[2*j] - data_points[2*i]) || - (data_ids[i] - data_ids[j])) < 0 -} - -function comparePivot(i, level, x, y, id, data_levels, data_points, data_ids) { - return ((level - data_levels[i]) || - (data_points[2*i] - x) || - (id - data_ids[i])) < 0 -} - -function quickSort(left, right, data_levels, data_points, data_ids, data_weights) { - var sixth = (right - left + 1) / 6 | 0, - index1 = left + sixth, - index5 = right - sixth, - index3 = left + right >> 1, - index2 = index3 - sixth, - index4 = index3 + sixth, - el1 = index1, - el2 = index2, - el3 = index3, - el4 = index4, - el5 = index5, - less = left + 1, - great = right - 1, - tmp = 0 - if(compare(el1, el2, data_levels, data_points, data_ids, data_weights)) { - tmp = el1 - el1 = el2 - el2 = tmp - } - if(compare(el4, el5, data_levels, data_points, data_ids, data_weights)) { - tmp = el4 - el4 = el5 - el5 = tmp - } - if(compare(el1, el3, data_levels, data_points, data_ids, data_weights)) { - tmp = el1 - el1 = el3 - el3 = tmp - } - if(compare(el2, el3, data_levels, data_points, data_ids, data_weights)) { - tmp = el2 - el2 = el3 - el3 = tmp - } - if(compare(el1, el4, data_levels, data_points, data_ids, data_weights)) { - tmp = el1 - el1 = el4 - el4 = tmp - } - if(compare(el3, el4, data_levels, data_points, data_ids, data_weights)) { - tmp = el3 - el3 = el4 - el4 = tmp - } - if(compare(el2, el5, data_levels, data_points, data_ids, data_weights)) { - tmp = el2 - el2 = el5 - el5 = tmp - } - if(compare(el2, el3, data_levels, data_points, data_ids, data_weights)) { - tmp = el2 - el2 = el3 - el3 = tmp - } - if(compare(el4, el5, data_levels, data_points, data_ids, data_weights)) { - tmp = el4 - el4 = el5 - el5 = tmp - } - - var pivot1_level = data_levels[el2] - var pivot1_x = data_points[2*el2] - var pivot1_y = data_points[2*el2+1] - var pivot1_id = data_ids[el2] - var pivot1_weight = data_weights[el2] - - var pivot2_level = data_levels[el4] - var pivot2_x = data_points[2*el4] - var pivot2_y = data_points[2*el4+1] - var pivot2_id = data_ids[el4] - var pivot2_weight = data_weights[el4] - - var ptr0 = el1 - var ptr2 = el3 - var ptr4 = el5 - var ptr5 = index1 - var ptr6 = index3 - var ptr7 = index5 - - var level_x = data_levels[ptr0] - var level_y = data_levels[ptr2] - var level_z = data_levels[ptr4] - data_levels[ptr5] = level_x - data_levels[ptr6] = level_y - data_levels[ptr7] = level_z - - for (var i1 = 0; i1 < 2; ++i1) { - var x = data_points[2*ptr0+i1] - var y = data_points[2*ptr2+i1] - var z = data_points[2*ptr4+i1] - data_points[2*ptr5+i1] = x - data_points[2*ptr6+i1] = y - data_points[2*ptr7+i1] = z - } - - var id_x = data_ids[ptr0] - var id_y = data_ids[ptr2] - var id_z = data_ids[ptr4] - data_ids[ptr5] = id_x - data_ids[ptr6] = id_y - data_ids[ptr7] = id_z - - var weight_x = data_weights[ptr0] - var weight_y = data_weights[ptr2] - var weight_z = data_weights[ptr4] - data_weights[ptr5] = weight_x - data_weights[ptr6] = weight_y - data_weights[ptr7] = weight_z - - move(index2, left, data_levels, data_points, data_ids, data_weights) - move(index4, right, data_levels, data_points, data_ids, data_weights) - for (var k = less; k <= great; ++k) { - if (comparePivot(k, - pivot1_level, pivot1_x, pivot1_y, pivot1_id, - data_levels, data_points, data_ids)) { - if (k !== less) { - swap(k, less, data_levels, data_points, data_ids, data_weights) - } - ++less; - } else { - if (!comparePivot(k, - pivot2_level, pivot2_x, pivot2_y, pivot2_id, - data_levels, data_points, data_ids)) { - while (true) { - if (!comparePivot(great, - pivot2_level, pivot2_x, pivot2_y, pivot2_id, - data_levels, data_points, data_ids)) { - if (--great < k) { - break; + if (options.layerType === 'symbol') { + if (propertyKey === 'icon-image' && style && !style.sprite) { + errors.push(new ValidationError(key, value, 'use of "icon-image" requires a style "sprite" property')); + } else if (propertyKey === 'text-field' && style && !style.glyphs) { + errors.push(new ValidationError(key, value, 'use of "text-field" requires a style "glyphs" property')); } - continue; - } else { - if (comparePivot(great, - pivot1_level, pivot1_x, pivot1_y, pivot1_id, - data_levels, data_points, data_ids)) { - rotate(k, less, great, data_levels, data_points, data_ids, data_weights) - ++less; - --great; - } else { - swap(k, great, data_levels, data_points, data_ids, data_weights) - --great; + } + + return errors.concat(validate({ + key: options.key, + value: value, + valueSpec: options.valueSpec || layerSpec[propertyKey], + style: style, + styleSpec: styleSpec + })); + + } else { + return [new ValidationError(key, value, 'unknown property "%s"', propertyKey)]; + } + +}; + +},{"../error/validation_error":280,"./validate":284}],295:[function(require,module,exports){ +'use strict'; + +var getType = require('../util/get_type'); +var ValidationError = require('../error/validation_error'); + +module.exports = function validateNumber(options) { + var key = options.key; + var value = options.value; + var valueSpec = options.valueSpec; + var type = getType(value); + + if (type !== 'number') { + return [new ValidationError(key, value, 'number expected, %s found', type)]; + } + + if ('minimum' in valueSpec && value < valueSpec.minimum) { + return [new ValidationError(key, value, '%s is less than the minimum value %s', value, valueSpec.minimum)]; + } + + if ('maximum' in valueSpec && value > valueSpec.maximum) { + return [new ValidationError(key, value, '%s is greater than the maximum value %s', value, valueSpec.maximum)]; + } + + return []; +}; + +},{"../error/validation_error":280,"../util/get_type":282}],296:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var getType = require('../util/get_type'); +var validate = require('./validate'); + +module.exports = function validateObject(options) { + var key = options.key; + var object = options.value; + var valueSpec = options.valueSpec; + var objectElementValidators = options.objectElementValidators || {}; + var style = options.style; + var styleSpec = options.styleSpec; + var errors = []; + + var type = getType(object); + if (type !== 'object') { + return [new ValidationError(key, object, 'object expected, %s found', type)]; + } + + for (var objectKey in object) { + var valueSpecKey = objectKey.split('.')[0]; // treat 'paint.*' as 'paint' + var objectElementSpec = valueSpec && (valueSpec[valueSpecKey] || valueSpec['*']); + var objectElementValidator = objectElementValidators[valueSpecKey] || objectElementValidators['*']; + + if (objectElementSpec || objectElementValidator) { + errors = errors.concat((objectElementValidator || validate)({ + key: (key ? key + '.' : key) + objectKey, + value: object[objectKey], + valueSpec: objectElementSpec, + style: style, + styleSpec: styleSpec, + object: object, + objectKey: objectKey + })); + + // tolerate root-level extra keys & arbitrary layer properties + // TODO remove this layer-specific logic + } else if (key !== '' && key.split('.').length !== 1) { + errors.push(new ValidationError(key, object[objectKey], 'unknown property "%s"', objectKey)); + } + } + + for (valueSpecKey in valueSpec) { + if (valueSpec[valueSpecKey].required && valueSpec[valueSpecKey]['default'] === undefined && object[valueSpecKey] === undefined) { + errors.push(new ValidationError(key, object, 'missing required property "%s"', valueSpecKey)); + } + } + + return errors; +}; + +},{"../error/validation_error":280,"../util/get_type":282,"./validate":284}],297:[function(require,module,exports){ +'use strict'; + +var validate = require('./validate'); +var ValidationError = require('../error/validation_error'); + +module.exports = function validatePaintProperty(options) { + var key = options.key; + var style = options.style; + var styleSpec = options.styleSpec; + var value = options.value; + var propertyKey = options.objectKey; + var layerSpec = styleSpec['paint_' + options.layerType]; + + var transitionMatch = propertyKey.match(/^(.*)-transition$/); + + if (transitionMatch && layerSpec[transitionMatch[1]] && layerSpec[transitionMatch[1]].transition) { + return validate({ + key: key, + value: value, + valueSpec: styleSpec.transition, + style: style, + styleSpec: styleSpec + }); + + } else if (options.valueSpec || layerSpec[propertyKey]) { + return validate({ + key: options.key, + value: value, + valueSpec: options.valueSpec || layerSpec[propertyKey], + style: style, + styleSpec: styleSpec + }); + + } else { + return [new ValidationError(key, value, 'unknown property "%s"', propertyKey)]; + } + +}; + +},{"../error/validation_error":280,"./validate":284}],298:[function(require,module,exports){ +'use strict'; + +var ValidationError = require('../error/validation_error'); +var unbundle = require('../util/unbundle_jsonlint'); +var validateObject = require('./validate_object'); +var validateEnum = require('./validate_enum'); + +module.exports = function validateSource(options) { + var value = options.value; + var key = options.key; + var styleSpec = options.styleSpec; + var style = options.style; + + if (!value.type) { + return [new ValidationError(key, value, '"type" is required')]; + } + + var type = unbundle(value.type); + switch (type) { + case 'vector': + case 'raster': + var errors = []; + errors = errors.concat(validateObject({ + key: key, + value: value, + valueSpec: styleSpec.source_tile, + style: options.style, + styleSpec: styleSpec + })); + if ('url' in value) { + for (var prop in value) { + if (['type', 'url', 'tileSize'].indexOf(prop) < 0) { + errors.push(new ValidationError(key + '.' + prop, value[prop], 'a source with a "url" property may not include a "%s" property', prop)); + } + } } - break; - } - } - } - } - } - shufflePivot(left, less-1, pivot1_level, pivot1_x, pivot1_y, pivot1_id, pivot1_weight, data_levels, data_points, data_ids, data_weights) - shufflePivot(right, great+1, pivot2_level, pivot2_x, pivot2_y, pivot2_id, pivot2_weight, data_levels, data_points, data_ids, data_weights) - if (less - 2 - left <= INSERT_SORT_CUTOFF) { - insertionSort(left, less - 2, data_levels, data_points, data_ids, data_weights) - } else { - quickSort(left, less - 2, data_levels, data_points, data_ids, data_weights) - } - if (right - (great + 2) <= INSERT_SORT_CUTOFF) { - insertionSort(great + 2, right, data_levels, data_points, data_ids, data_weights) - } else { - quickSort(great + 2, right, data_levels, data_points, data_ids, data_weights) - } - if (great - less <= INSERT_SORT_CUTOFF) { - insertionSort(less, great, data_levels, data_points, data_ids, data_weights) - } else { - quickSort(less, great, data_levels, data_points, data_ids, data_weights) - } -} - -},{}],726:[function(require,module,exports){ -'use strict' - -var pool = require('typedarray-pool') - -var sortLevels = require('./lib/sort') - -module.exports = snapPoints - -function partition(points, ids, start, end, lox, loy, hix, hiy) { - var mid = start - for(var i=start; i>> 1 - if(n < 1) { - return [] - } - - var lox = Infinity, loy = Infinity - var hix = -Infinity, hiy = -Infinity - for(var i=0; i= Math.max(0.9 * count, 32)) { - var mid = (end + start)>>>1 - snapRec(nx, ny, diam_2, offset, mid, level+1) - offset = mid - } - snapRec(nx, ny, diam_2, offset, nextOffset, level+1) - offset = nextOffset - } - } - } - snapRec(lox, loy, diam, 0, n, 0) - sortLevels(levels, points, ids, weights, n) - - var lod = [] - var lastLevel = 0 - var prevOffset = n - for(var ptr=n-1; ptr>=0; --ptr) { - points[2*ptr] = (points[2*ptr] - lox) * scaleX - points[2*ptr+1] = (points[2*ptr+1] - loy) * scaleY - - var level = levels[ptr] - if(level === lastLevel) { - continue - } - - lod.push(new SnapInterval( - diam * Math.pow(0.5, level), - ptr+1, - prevOffset - (ptr+1) - )) - prevOffset = ptr+1 - - lastLevel = level - } - - lod.push(new SnapInterval(diam * Math.pow(0.5, level+1), 0, prevOffset)) - pool.free(levels) - - return lod -} - -},{"./lib/sort":725,"typedarray-pool":729}],727:[function(require,module,exports){ -arguments[4][168][0].apply(exports,arguments) -},{"dup":168}],728:[function(require,module,exports){ -arguments[4][147][0].apply(exports,arguments) -},{"dup":147}],729:[function(require,module,exports){ -arguments[4][187][0].apply(exports,arguments) -},{"bit-twiddle":727,"buffer":33,"dup":187}],730:[function(require,module,exports){ -'use strict' - -var createShader = require('gl-shader') -var createBuffer = require('gl-buffer') -var search = require('binary-search-bounds') -var snapPoints = require('snap-points-2d') -var pool = require('typedarray-pool') -var SHADERS = require('./lib/shader') - -module.exports = createScatter2D - -function Scatter2D(plot, positionBufferHi, positionBufferLo, pickBuffer, weightBuffer, shader, pickShader) { - this.plot = plot - this.positionBufferHi = positionBufferHi - this.positionBufferLo = positionBufferLo - this.pickBuffer = pickBuffer - this.weightBuffer = weightBuffer - this.shader = shader - this.pickShader = pickShader - this.scales = [] - this.size = 12.0 - this.borderSize = 1.0 - this.pointCount = 0 - this.color = [1, 0, 0, 1] - this.borderColor = [0, 0, 0, 1] - this.bounds = [Infinity, Infinity, -Infinity, -Infinity] - this.pickOffset = 0 - this.points = null - this.xCoords = null -} - -var proto = Scatter2D.prototype -var scaleHi = new Float32Array(2) -var scaleLo = new Float32Array(2) -var translateHi = new Float32Array(2) -var translateLo = new Float32Array(2) -var PICK_VEC4 = [0, 0, 0, 0] - -proto.dispose = function() { - this.shader.dispose() - this.pickShader.dispose() - this.positionBufferHi.dispose() - this.positionBufferLo.dispose() - this.pickBuffer.dispose() - if(this.xCoords) pool.free(this.xCoords) - this.plot.removeObject(this) -} - -proto.update = function(options) { - options = options || {} - - function dflt(opt, value) { - return opt in options ? options[opt] : value - } - - this.size = dflt('size', 12) - this.color = dflt('color', [1, 0, 0, 1]).slice() - this.borderSize = dflt('borderSize', 1) - this.borderColor = dflt('borderColor', [0, 0, 0, 1]).slice() - - if(this.xCoords) pool.free(this.xCoords) - - this.points = options.positions - var pointCount = this.points.length >>> 1 - var packedId = pool.mallocInt32(pointCount) - var packedW = pool.mallocFloat32(2 * pointCount) - var packed = pool.mallocFloat64(2 * pointCount) - packed.set(this.points) - this.scales = snapPoints(packed, packedId, packedW, this.bounds) - - var xCoords = pool.mallocFloat64(pointCount) - var packedHi = pool.mallocFloat32(2 * pointCount) - var packedLo = pool.mallocFloat32(2 * pointCount) - packedHi.set(packed) - for(var i = 0, j = 0; i < pointCount; i++, j += 2) { - packedLo[j] = packed[j] - packedHi[j] - packedLo[j + 1] = packed[j + 1] - packedHi[j + 1] - xCoords[i] = packed[j] - } - this.positionBufferHi.update(packedHi) - this.positionBufferLo.update(packedLo) - this.pickBuffer.update(packedId) - this.weightBuffer.update(packedW) - - pool.free(packedId) - pool.free(packed) - pool.free(packedHi) - pool.free(packedLo) - pool.free(packedW) - - this.xCoords = xCoords - this.pointCount = pointCount - this.pickOffset = 0 -} - -proto.draw = function(pickOffset) { - - var pick = pickOffset !== void(0) - - var plot = this.plot - var shader = pick ? this.pickShader : this.shader - var scales = this.scales - var positionBufferHi = this.positionBufferHi - var positionBufferLo = this.positionBufferLo - var pickBuffer = this.pickBuffer - var bounds = this.bounds - var size = this.size - var borderSize = this.borderSize - var gl = plot.gl - var pixelRatio = pick ? plot.pickPixelRatio : plot.pixelRatio - var viewBox = plot.viewBox - var dataBox = plot.dataBox - - if(this.pointCount === 0) - return pickOffset - - var boundX = bounds[2] - bounds[0] - var boundY = bounds[3] - bounds[1] - var dataX = dataBox[2] - dataBox[0] - var dataY = dataBox[3] - dataBox[1] - var screenX = (viewBox[2] - viewBox[0]) * pixelRatio / plot.pixelRatio - var screenY = (viewBox[3] - viewBox[1]) * pixelRatio / plot.pixelRatio - - var pixelSize = Math.min(dataX / screenX, dataY / screenY) - - var scaleX = 2 * boundX / dataX - var scaleY = 2 * boundY / dataY - - scaleHi[0] = scaleX - scaleHi[1] = scaleY - - scaleLo[0] = scaleX - scaleHi[0] - scaleLo[1] = scaleY - scaleHi[1] - - var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX - var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY - - translateHi[0] = translateX - translateHi[1] = translateY - - translateLo[0] = translateX - translateHi[0] - translateLo[1] = translateY - translateHi[1] - - shader.bind() - shader.uniforms.scaleHi = scaleHi - shader.uniforms.scaleLo = scaleLo - shader.uniforms.translateHi = translateHi - shader.uniforms.translateLo = translateLo - shader.uniforms.color = this.color - shader.uniforms.borderColor = this.borderColor - shader.uniforms.pointSize = pixelRatio * (size + borderSize) - shader.uniforms.centerFraction = this.borderSize === 0 ? 2 : size / (size + borderSize + 1.25) - - positionBufferHi.bind() - shader.attributes.positionHi.pointer() - - positionBufferLo.bind() - shader.attributes.positionLo.pointer() - - if(pick) { - - this.pickOffset = pickOffset - PICK_VEC4[0] = ( pickOffset & 0xff) - PICK_VEC4[1] = ((pickOffset >> 8) & 0xff) - PICK_VEC4[2] = ((pickOffset >> 16) & 0xff) - PICK_VEC4[3] = ((pickOffset >> 24) & 0xff) - shader.uniforms.pickOffset = PICK_VEC4 - - pickBuffer.bind() - shader.attributes.pickId.pointer(gl.UNSIGNED_BYTE) - - } else { - - shader.uniforms.useWeight = 1 - this.weightBuffer.bind() - shader.attributes.weight.pointer() - - } - - var xCoords = this.xCoords - var xStart = (dataBox[0] - bounds[0] - pixelSize * size * pixelRatio) / boundX - var xEnd = (dataBox[2] - bounds[0] + pixelSize * size * pixelRatio) / boundX - - var firstLevel = true - - for(var scaleNum = scales.length - 1; scaleNum >= 0; scaleNum--) { - var lod = scales[scaleNum] - if(lod.pixelSize < pixelSize && scaleNum > 1) - continue - - var intervalStart = lod.offset - var intervalEnd = lod.count + intervalStart - - var startOffset = search.ge(xCoords, xStart, intervalStart, intervalEnd - 1) - var endOffset = search.lt(xCoords, xEnd, startOffset, intervalEnd - 1) + 1 - - if(endOffset > startOffset) - gl.drawArrays(gl.POINTS, startOffset, endOffset - startOffset) - - if(!pick && firstLevel) { - firstLevel = false - shader.uniforms.useWeight = 0 - } - } - - return pickOffset + this.pointCount -} - -proto.drawPick = proto.draw - -proto.pick = function(x, y, value) { - var pointId = value - this.pickOffset - return pointId < 0 || pointId >= this.pointCount - ? null : { - object: this, - pointId: pointId, - dataCoord: [ this.points[2 * pointId], this.points[2 * pointId + 1] ] - } -} - -function createScatter2D(plot, options) { - var gl = plot.gl - var positionBufferHi = createBuffer(gl) - var positionBufferLo = createBuffer(gl) - var pickBuffer = createBuffer(gl) - var weightBuffer = createBuffer(gl) - var shader = createShader(gl, SHADERS.pointVertex, SHADERS.pointFragment) - var pickShader = createShader(gl, SHADERS.pickVertex, SHADERS.pickFragment) - - var result = new Scatter2D(plot, positionBufferHi, positionBufferLo, pickBuffer, weightBuffer, shader, pickShader) - result.update(options) - - plot.addObject(result) // register with plot - - return result -} -},{"./lib/shader":722,"binary-search-bounds":723,"gl-buffer":724,"gl-shader":835,"snap-points-2d":726,"typedarray-pool":729}],731:[function(require,module,exports){ -"use strict" - -var vectorizeText = require("vectorize-text") - -module.exports = getGlyph - -var GLYPH_CACHE = {} - -function getGlyph(symbol, font) { - var fontCache = GLYPH_CACHE[font] - if(!fontCache) { - fontCache = GLYPH_CACHE[font] = {} - } - if(symbol in fontCache) { - return fontCache[symbol] - } - - //Get line and triangle meshes for glyph - var lineSymbol = vectorizeText(symbol, { - textAlign: "center", - textBaseline: "middle", - lineHeight: 1.0, - font: font - }) - var triSymbol = vectorizeText(symbol, { - triangles: true, - textAlign: "center", - textBaseline: "middle", - lineHeight: 1.0, - font: font - }) - - //Calculate bounding box - var bounds = [[Infinity,Infinity], [-Infinity,-Infinity]] - for(var i=0; i= 1) { - return true - } - for(var i=0; i<3; ++i) { - if(this.axesProject[i] && this.projectOpacity[i] >= 1) { - return true - } - } - return false -} - -var VIEW_SHAPE = [0,0] -var U_VEC = [0,0,0] -var V_VEC = [0,0,0] -var MU_VEC = [0,0,0,1] -var MV_VEC = [0,0,0,1] -var SCRATCH_MATRIX = IDENTITY.slice() -var SCRATCH_VEC = [0,0,0] -var CLIP_BOUNDS = [[0,0,0], [0,0,0]] - -function zeroVec(a) { - a[0] = a[1] = a[2] = 0 - return a -} - -function augment(hg, af) { - hg[0] = af[0] - hg[1] = af[1] - hg[2] = af[2] - hg[3] = 1 - return hg -} - -function setComponent(out, v, i, x) { - out[0] = v[0] - out[1] = v[1] - out[2] = v[2] - out[i] = x - return out -} - -function getClipBounds(bounds) { - var result = CLIP_BOUNDS - for(var i=0; i<2; ++i) { - for(var j=0; j<3; ++j) { - result[i][j] = Math.max(Math.min(bounds[i][j], 1e8), -1e8) - } - } - return result -} - -function drawProject(shader, points, camera, transparent, forceDraw) { - var axesProject = points.axesProject - - var gl = points.gl - var uniforms = shader.uniforms - var model = camera.model || IDENTITY - var view = camera.view || IDENTITY - var projection = camera.projection || IDENTITY - var bounds = points.axesBounds - var clipBounds = getClipBounds(points.clipBounds) - - var cubeAxis - if(points.axes) { - cubeAxis = points.axes.lastCubeProps.axis - } else { - cubeAxis = [1,1,1] - } - - VIEW_SHAPE[0] = 2.0/gl.drawingBufferWidth - VIEW_SHAPE[1] = 2.0/gl.drawingBufferHeight - - shader.bind() - uniforms.view = view - uniforms.projection = projection - uniforms.screenSize = VIEW_SHAPE - uniforms.highlightId = points.highlightId - uniforms.highlightScale = points.highlightScale - uniforms.clipBounds = clipBounds - uniforms.pickGroup = points.pickId / 255.0 - uniforms.pixelRatio = points.pixelRatio - - for(var i=0; i<3; ++i) { - if(!axesProject[i]) { - continue - } - if((points.projectOpacity[i] < 1) !== transparent) { - continue - } - - uniforms.scale = points.projectScale[i] - uniforms.opacity = points.projectOpacity[i] - - //Project model matrix - var pmodel = SCRATCH_MATRIX - for(var j=0; j<16; ++j) { - pmodel[j] = 0 - } - for(var j=0; j<4; ++j) { - pmodel[5*j] = 1 - } - pmodel[5*i] = 0 - if(cubeAxis[i] < 0) { - pmodel[12+i] = bounds[0][i] - } else { - pmodel[12+i] = bounds[1][i] - } - mat4mult(pmodel, model, pmodel) - uniforms.model = pmodel - - //Compute initial axes - var u = (i+1)%3 - var v = (i+2)%3 - var du = zeroVec(U_VEC) - var dv = zeroVec(V_VEC) - du[u] = 1 - dv[v] = 1 - - //Align orientation relative to viewer - var mdu = project(projection, view, model, augment(MU_VEC, du)) - var mdv = project(projection, view, model, augment(MV_VEC, dv)) - if(Math.abs(mdu[1]) > Math.abs(mdv[1])) { - var tmp = mdu - mdu = mdv - mdv = tmp - tmp = du - du = dv - dv = tmp - var t = u - u = v - v = t - } - if(mdu[0] < 0) { - du[u] = -1 - } - if(mdv[1] > 0) { - dv[v] = -1 - } - var su = 0.0 - var sv = 0.0 - for(var j=0; j<4; ++j) { - su += Math.pow(model[4*u+j], 2) - sv += Math.pow(model[4*v+j], 2) - } - du[u] /= Math.sqrt(su) - dv[v] /= Math.sqrt(sv) - uniforms.axes[0] = du - uniforms.axes[1] = dv - - //Update fragment clip bounds - uniforms.fragClipBounds[0] = setComponent(SCRATCH_VEC, clipBounds[0], i, -1e8) - uniforms.fragClipBounds[1] = setComponent(SCRATCH_VEC, clipBounds[1], i, 1e8) - - //Draw interior - points.vao.draw(gl.TRIANGLES, points.vertexCount) - - //Draw edges - if(points.lineWidth > 0) { - gl.lineWidth(points.lineWidth) - points.vao.draw(gl.LINES, points.lineVertexCount, points.vertexCount) - } - } -} - - -var NEG_INFINITY3 = [-1e8, -1e8, -1e8] -var POS_INFINITY3 = [1e8, 1e8, 1e8] -var CLIP_GROUP = [NEG_INFINITY3, POS_INFINITY3] - -function drawFull(shader, pshader, points, camera, transparent, forceDraw) { - var gl = points.gl - - points.vao.bind() - - if(transparent === (points.opacity < 1) || forceDraw) { - shader.bind() - var uniforms = shader.uniforms - - uniforms.model = camera.model || IDENTITY - uniforms.view = camera.view || IDENTITY - uniforms.projection = camera.projection || IDENTITY - - VIEW_SHAPE[0] = 2.0/gl.drawingBufferWidth - VIEW_SHAPE[1] = 2.0/gl.drawingBufferHeight - uniforms.screenSize = VIEW_SHAPE - - uniforms.highlightId = points.highlightId - uniforms.highlightScale = points.highlightScale - - uniforms.fragClipBounds = CLIP_GROUP - uniforms.clipBounds = points.axes.bounds - - uniforms.opacity = points.opacity - uniforms.pickGroup = points.pickId / 255.0 - - uniforms.pixelRatio = points.pixelRatio - - //Draw interior - points.vao.draw(gl.TRIANGLES, points.vertexCount) - - //Draw edges - if(points.lineWidth > 0) { - gl.lineWidth(points.lineWidth) - points.vao.draw(gl.LINES, points.lineVertexCount, points.vertexCount) - } - } - - drawProject(pshader, points, camera, transparent, forceDraw) - - points.vao.unbind() -} - -proto.draw = function(camera) { - var shader = this.useOrtho ? this.orthoShader : this.shader - drawFull(shader, this.projectShader, this, camera, false, false) -} - -proto.drawTransparent = function(camera) { - var shader = this.useOrtho ? this.orthoShader : this.shader - drawFull(shader, this.projectShader, this, camera, true, false) -} - -proto.drawPick = function(camera) { - var shader = this.useOrtho ? this.pickOrthoShader : this.pickPerspectiveShader - drawFull(shader, this.pickProjectShader, this, camera, false, true) -} - -proto.pick = function(selected) { - if(!selected) { - return null - } - if(selected.id !== this.pickId) { - return null - } - var x = selected.value[2] + (selected.value[1]<<8) + (selected.value[0]<<16) - if(x >= this.pointCount || x < 0) { - return null - } - - //Unpack result - var coord = this.points[x] - var result = this._selectResult - result.index = x - for(var i=0; i<3; ++i) { - result.position[i] = result.dataCoordinate[i] = coord[i] - } - return result -} - -proto.highlight = function(selection) { - if(!selection) { - this.highlightId = [1,1,1,1] - } else { - var pointId = selection.index - var a0 = pointId &0xff - var a1 = (pointId>>8) &0xff - var a2 = (pointId>>16)&0xff - this.highlightId = [a0/255.0, a1/255.0, a2/255.0, 0] - } -} - -proto.update = function(options) { - - options = options || {} - - if('perspective' in options) { - this.useOrtho = !options.perspective - } - if('orthographic' in options) { - this.useOrtho = !!options.orthographic - } - if('lineWidth' in options) { - this.lineWidth = options.lineWidth - } - if('project' in options) { - if(Array.isArray(options.project)) { - this.axesProject = options.project - } else { - var v = !!options.project - this.axesProject = [v,v,v] - } - } - if('projectScale' in options) { - if(Array.isArray(options.projectScale)) { - this.projectScale = options.projectScale.slice() - } else { - var s = +options.projectScale - this.projectScale = [s,s,s] - } - } - if('projectOpacity' in options) { - if(Array.isArray(options.projectOpacity)) { - this.projectOpacity = options.projectOpacity.slice() - } else { - var s = +options.projectOpacity - this.projectOpacity = [s,s,s] - } - } - if('opacity' in options) { - this.opacity = options.opacity - } - - //Set dirty flag - this.dirty = true - - //Create new buffers - var points = options.position - if(!points) { - return - } - - //Text font - var font = options.font || 'normal' - var alignment = options.alignment || [0,0] - - //Bounds - var lowerBound = [ Infinity, Infinity, Infinity] - var upperBound = [-Infinity,-Infinity,-Infinity] - - //Unpack options - var glyphs = options.glyph - var colors = options.color - var sizes = options.size - var angles = options.angle - var lineColors = options.lineColor - - //Picking geometry - var pickCounter = 0 - - //First do pass to compute buffer sizes - var triVertexCount = 0 - var lineVertexCount = 0 - - //Count number of points and buffer size - var numPoints = points.length - -count_loop: - for(var i=0; i 0) { - textOffset[0] = -alignment[0] * (1+glyphBounds[0][0]) - } - - //Write out inner marker - var cells = glyphMesh.cells - var verts = glyphMesh.positions - - for(var j=0; j 0) { - - //Draw border - var w = lineWidth * pixelRatio - boxes.drawBox(loX-w, loY-w, hiX+w, loY+w, borderColor) - boxes.drawBox(loX-w, hiY-w, hiX+w, hiY+w, borderColor) - boxes.drawBox(loX-w, loY-w, loX+w, hiY+w, borderColor) - boxes.drawBox(hiX-w, loY-w, hiX+w, hiY+w, borderColor) - } -} - -proto.update = function(options) { - options = options || {} - - this.innerFill = !!options.innerFill - this.outerFill = !!options.outerFill - this.innerColor = (options.innerColor || [0,0,0,0.5]).slice() - this.outerColor = (options.outerColor || [0,0,0,0.5]).slice() - this.borderColor = (options.borderColor || [0,0,0,1]).slice() - this.borderWidth = options.borderWidth || 0 - this.selectBox = (options.selectBox || this.selectBox).slice() -} - -proto.dispose = function() { - this.boxBuffer.dispose() - this.boxShader.dispose() - this.plot.removeOverlay(this) -} - -function createSelectBox(plot, options) { - var gl = plot.gl - var buffer = createBuffer(gl, [ - 0, 0, - 0, 1, - 1, 0, - 1, 1 ]) - var shader = createShader(gl, SHADERS.boxVertex, SHADERS.boxFragment) - var selectBox = new SelectBox(plot, buffer, shader) - selectBox.update(options) - plot.addOverlay(selectBox) - return selectBox -} - -},{"./lib/shaders":829,"gl-buffer":830,"gl-shader":835}],835:[function(require,module,exports){ -'use strict' - -var createUniformWrapper = require('./lib/create-uniforms') -var createAttributeWrapper = require('./lib/create-attributes') -var makeReflect = require('./lib/reflect') -var shaderCache = require('./lib/shader-cache') -var runtime = require('./lib/runtime-reflect') -var GLError = require("./lib/GLError") - -//Shader object -function Shader(gl) { - this.gl = gl - - //Default initialize these to null - this._vref = - this._fref = - this._relink = - this.vertShader = - this.fragShader = - this.program = - this.attributes = - this.uniforms = - this.types = null -} - -var proto = Shader.prototype - -proto.bind = function() { - if(!this.program) { - this._relink() - } - this.gl.useProgram(this.program) -} - -proto.dispose = function() { - if(this._fref) { - this._fref.dispose() - } - if(this._vref) { - this._vref.dispose() - } - this.attributes = - this.types = - this.vertShader = - this.fragShader = - this.program = - this._relink = - this._fref = - this._vref = null -} - -function compareAttributes(a, b) { - if(a.name < b.name) { - return -1 - } - return 1 -} - -//Update export hook for glslify-live -proto.update = function( - vertSource - , fragSource - , uniforms - , attributes) { - - //If only one object passed, assume glslify style output - if(!fragSource || arguments.length === 1) { - var obj = vertSource - vertSource = obj.vertex - fragSource = obj.fragment - uniforms = obj.uniforms - attributes = obj.attributes - } - - var wrapper = this - var gl = wrapper.gl - - //Compile vertex and fragment shaders - var pvref = wrapper._vref - wrapper._vref = shaderCache.shader(gl, gl.VERTEX_SHADER, vertSource) - if(pvref) { - pvref.dispose() - } - wrapper.vertShader = wrapper._vref.shader - var pfref = this._fref - wrapper._fref = shaderCache.shader(gl, gl.FRAGMENT_SHADER, fragSource) - if(pfref) { - pfref.dispose() - } - wrapper.fragShader = wrapper._fref.shader - - //If uniforms/attributes is not specified, use RT reflection - if(!uniforms || !attributes) { - - //Create initial test program - var testProgram = gl.createProgram() - gl.attachShader(testProgram, wrapper.fragShader) - gl.attachShader(testProgram, wrapper.vertShader) - gl.linkProgram(testProgram) - if(!gl.getProgramParameter(testProgram, gl.LINK_STATUS)) { - var errLog = gl.getProgramInfoLog(testProgram) - throw new GLError(errLog, 'Error linking program:' + errLog) - } - - //Load data from runtime - uniforms = uniforms || runtime.uniforms(gl, testProgram) - attributes = attributes || runtime.attributes(gl, testProgram) - - //Release test program - gl.deleteProgram(testProgram) - } - - //Sort attributes lexicographically - // overrides undefined WebGL behavior for attribute locations - attributes = attributes.slice() - attributes.sort(compareAttributes) - - //Convert attribute types, read out locations - var attributeUnpacked = [] - var attributeNames = [] - var attributeLocations = [] - for(var i=0; i= 0) { - var size = attr.type.charAt(attr.type.length-1)|0 - var locVector = new Array(size) - for(var j=0; j= 0) { - curLocation += 1 - } - attributeLocations[i] = curLocation - } - } - - //Rebuild program and recompute all uniform locations - var uniformLocations = new Array(uniforms.length) - function relink() { - wrapper.program = shaderCache.program( - gl - , wrapper._vref - , wrapper._fref - , attributeNames - , attributeLocations) - - for(var i=0; i= 0) { - pickStr.push('0') - } else if(facet.indexOf(-(i+1)) >= 0) { - pickStr.push('s['+i+']-1') - } else { - pickStr.push('-1') - loStr.push('1') - hiStr.push('s['+i+']-2') - } - } - var boundStr = '.lo(' + loStr.join() + ').hi(' + hiStr.join() + ')' - if(loStr.length === 0) { - boundStr = '' - } - - if(cod > 0) { - code.push('if(1') - for(var i=0; i= 0 || facet.indexOf(-(i+1)) >= 0) { - continue - } - code.push('&&s[', i, ']>2') - } - code.push('){grad', cod, '(src.pick(', pickStr.join(), ')', boundStr) - for(var i=0; i= 0 || facet.indexOf(-(i+1)) >= 0) { - continue - } - code.push(',dst.pick(', pickStr.join(), ',', i, ')', boundStr) - } - code.push(');') - } - - for(var i=0; i1){dst.set(', - pickStr.join(), ',', bnd, ',0.5*(src.get(', - cPickStr.join(), ')-src.get(', - dPickStr.join(), ')))}else{dst.set(', - pickStr.join(), ',', bnd, ',0)};') - } else { - code.push('if(s[', bnd, ']>1){diff(', outStr, - ',src.pick(', cPickStr.join(), ')', boundStr, - ',src.pick(', dPickStr.join(), ')', boundStr, - ');}else{zero(', outStr, ');};') - } - break - - case 'mirror': - if(cod === 0) { - code.push('dst.set(', pickStr.join(), ',', bnd, ',0);') - } else { - code.push('zero(', outStr, ');') - } - break - - case 'wrap': - var aPickStr = pickStr.slice() - var bPickStr = pickStr.slice() - if(facet[i] < 0) { - aPickStr[bnd] = 's[' + bnd + ']-2' - bPickStr[bnd] = '0' - - } else { - aPickStr[bnd] = 's[' + bnd + ']-1' - bPickStr[bnd] = '1' - } - if(cod === 0) { - code.push('if(s[', bnd, ']>2){dst.set(', - pickStr.join(), ',', bnd, ',0.5*(src.get(', - aPickStr.join(), ')-src.get(', - bPickStr.join(), ')))}else{dst.set(', - pickStr.join(), ',', bnd, ',0)};') - } else { - code.push('if(s[', bnd, ']>2){diff(', outStr, - ',src.pick(', aPickStr.join(), ')', boundStr, - ',src.pick(', bPickStr.join(), ')', boundStr, - ');}else{zero(', outStr, ');};') - } - break + return errors; + + case 'geojson': + return validateObject({ + key: key, + value: value, + valueSpec: styleSpec.source_geojson, + style: style, + styleSpec: styleSpec + }); + + case 'video': + return validateObject({ + key: key, + value: value, + valueSpec: styleSpec.source_video, + style: style, + styleSpec: styleSpec + }); + + case 'image': + return validateObject({ + key: key, + value: value, + valueSpec: styleSpec.source_image, + style: style, + styleSpec: styleSpec + }); default: - throw new Error('ndarray-gradient: Invalid boundary condition') - } + return validateEnum({ + key: key + '.type', + value: value.type, + valueSpec: {values: ['vector', 'raster', 'geojson', 'video', 'image']}, + style: style, + styleSpec: styleSpec + }); + } +}; + +},{"../error/validation_error":280,"../util/unbundle_jsonlint":283,"./validate_enum":289,"./validate_object":296}],299:[function(require,module,exports){ +'use strict'; + +var getType = require('../util/get_type'); +var ValidationError = require('../error/validation_error'); + +module.exports = function validateString(options) { + var value = options.value; + var key = options.key; + var type = getType(value); + + if (type !== 'string') { + return [new ValidationError(key, value, 'string expected, %s found', type)]; } - if(cod > 0) { - code.push('};') - } - } + return []; +}; - //Enumerate ridges, facets, etc. of hypercube - for(var i=0; i<(1<} + * @example + * var validate = require('mapbox-gl-style-spec/lib/validate_style.min'); + * var errors = validate(style); + */ +function validateStyleMin(style, styleSpec) { + styleSpec = styleSpec || latestStyleSpec; + + var errors = []; + + errors = errors.concat(validate({ + key: '', + value: style, + valueSpec: styleSpec.$root, + styleSpec: styleSpec, + style: style, + objectElementValidators: { + glyphs: validateGlyphsURL } - } - handleBoundary(sfaces) + })); + + if (styleSpec.$version > 7 && style.constants) { + errors = errors.concat(validateConstants({ + key: 'constants', + value: style.constants, + style: style, + styleSpec: styleSpec + })); } - } - code.push('return dst;};return gradient') - - //Compile and link routine, save cached procedure - var linkNames = [ 'diff', 'zero' ] - var linkArgs = [ centralDiff, zeroOut ] - for(var i=1; i<=d; ++i) { - linkNames.push('grad' + i) - linkArgs.push(generateTemplate(i)) - } - linkNames.push(code.join('')) - - var link = Function.apply(void 0, linkNames) - var proc = link.apply(void 0, linkArgs) - TEMPLATE_CACHE[token] = proc - return proc + return sortErrors(errors); } -function gradient(out, inp, bc) { - if(Array.isArray(bc)) { - if(bc.length !== inp.dimension) { - throw new Error('ndarray-gradient: invalid boundary conditions') - } - } else if(typeof bc === 'string') { - bc = dup(inp.dimension, bc) - } else { - bc = dup(inp.dimension, 'clamp') - } - if(out.dimension !== inp.dimension + 1) { - throw new Error('ndarray-gradient: output dimension must be +1 input dimension') - } - if(out.shape[inp.dimension] !== inp.dimension) { - throw new Error('ndarray-gradient: output shape must match input shape') - } - for(var i=0; i= 1) { - return true - } - for (var i = 0; i < 3; ++i) { - if (this._contourCounts[i].length > 0 || this._dynamicCounts[i] > 0) { - return true - } - } - return false -} - -proto.pickSlots = 1 - -proto.setPickBase = function (id) { - this.pickId = id -} - -var ZERO_VEC = [0, 0, 0] - -var PROJECT_DATA = { - showSurface: false, - showContour: false, - projections: [IDENTITY.slice(), IDENTITY.slice(), IDENTITY.slice()], - clipBounds: [ - [[0, 0, 0], [0, 0, 0]], - [[0, 0, 0], [0, 0, 0]], - [[0, 0, 0], [0, 0, 0]]] -} - -function computeProjectionData (camera, obj) { - var i, j, k - - // Compute cube properties - var cubeAxis = (obj.axes && obj.axes.lastCubeProps.axis) || ZERO_VEC - - var showSurface = obj.showSurface - var showContour = obj.showContour - - for (i = 0; i < 3; ++i) { - showSurface = showSurface || obj.surfaceProject[i] - for (j = 0; j < 3; ++j) { - showContour = showContour || obj.contourProject[i][j] - } - } - - for (i = 0; i < 3; ++i) { - // Construct projection onto axis - var axisSquish = PROJECT_DATA.projections[i] - for (j = 0; j < 16; ++j) { - axisSquish[j] = 0 - } - for (j = 0; j < 4; ++j) { - axisSquish[5 * j] = 1 - } - axisSquish[5 * i] = 0 - axisSquish[12 + i] = obj.axesBounds[+(cubeAxis[i] > 0)][i] - multiply(axisSquish, camera.model, axisSquish) - - var nclipBounds = PROJECT_DATA.clipBounds[i] - for (k = 0; k < 2; ++k) { - for (j = 0; j < 3; ++j) { - nclipBounds[k][j] = camera.clipBounds[k][j] - } - } - nclipBounds[0][i] = -1e8 - nclipBounds[1][i] = 1e8 - } - - PROJECT_DATA.showSurface = showSurface - PROJECT_DATA.showContour = showContour - - return PROJECT_DATA -} - -var UNIFORMS = { - model: IDENTITY, - view: IDENTITY, - projection: IDENTITY, - inverseModel: IDENTITY.slice(), - lowerBound: [0, 0, 0], - upperBound: [0, 0, 0], - colorMap: 0, - clipBounds: [[0, 0, 0], [0, 0, 0]], - height: 0.0, - contourTint: 0, - contourColor: [0, 0, 0, 1], - permutation: [1, 0, 0, 0, 1, 0, 0, 0, 1], - zOffset: -1e-4, - kambient: 1, - kdiffuse: 1, - kspecular: 1, - lightPosition: [1000, 1000, 1000], - eyePosition: [0, 0, 0], - roughness: 1, - fresnel: 1, - opacity: 1, - vertexColor: 0 -} - -var MATRIX_INVERSE = IDENTITY.slice() -var DEFAULT_PERM = [1, 0, 0, 0, 1, 0, 0, 0, 1] - -function drawCore (params, transparent) { - params = params || {} - var gl = this.gl - - gl.disable(gl.CULL_FACE) - - this._colorMap.bind(0) - - var uniforms = UNIFORMS - uniforms.model = params.model || IDENTITY - uniforms.view = params.view || IDENTITY - uniforms.projection = params.projection || IDENTITY - uniforms.lowerBound = [this.bounds[0][0], this.bounds[0][1], this.colorBounds[0] || this.bounds[0][2]] - uniforms.upperBound = [this.bounds[1][0], this.bounds[1][1], this.colorBounds[1] || this.bounds[1][2]] - uniforms.contourColor = this.contourColor[0] - - uniforms.inverseModel = invert(uniforms.inverseModel, uniforms.model) - - for (var i = 0; i < 2; ++i) { - var clipClamped = uniforms.clipBounds[i] - for (var j = 0; j < 3; ++j) { - clipClamped[j] = Math.min(Math.max(this.clipBounds[i][j], -1e8), 1e8) - } - } - - uniforms.kambient = this.ambientLight - uniforms.kdiffuse = this.diffuseLight - uniforms.kspecular = this.specularLight - - uniforms.roughness = this.roughness - uniforms.fresnel = this.fresnel - uniforms.opacity = this.opacity - - uniforms.height = 0.0 - uniforms.permutation = DEFAULT_PERM - - uniforms.vertexColor = this.vertexColor - - // Compute camera matrix inverse - var invCameraMatrix = MATRIX_INVERSE - multiply(invCameraMatrix, uniforms.view, uniforms.model) - multiply(invCameraMatrix, uniforms.projection, invCameraMatrix) - invert(invCameraMatrix, invCameraMatrix) - - for (i = 0; i < 3; ++i) { - uniforms.eyePosition[i] = invCameraMatrix[12 + i] / invCameraMatrix[15] - } - - var w = invCameraMatrix[15] - for (i = 0; i < 3; ++i) { - w += this.lightPosition[i] * invCameraMatrix[4 * i + 3] - } - for (i = 0; i < 3; ++i) { - var s = invCameraMatrix[12 + i] - for (j = 0; j < 3; ++j) { - s += invCameraMatrix[4 * j + i] * this.lightPosition[j] - } - uniforms.lightPosition[i] = s / w - } - - var projectData = computeProjectionData(uniforms, this) - - if (projectData.showSurface && (transparent === (this.opacity < 1))) { - // Set up uniforms - this._shader.bind() - this._shader.uniforms = uniforms - - // Draw it - this._vao.bind() - - if (this.showSurface && this._vertexCount) { - this._vao.draw(gl.TRIANGLES, this._vertexCount) - } - - // Draw projections of surface - for (i = 0; i < 3; ++i) { - if (!this.surfaceProject[i] || !this.vertexCount) { - continue - } - this._shader.uniforms.model = projectData.projections[i] - this._shader.uniforms.clipBounds = projectData.clipBounds[i] - this._vao.draw(gl.TRIANGLES, this._vertexCount) - } - - this._vao.unbind() - } - - if (projectData.showContour && !transparent) { - var shader = this._contourShader - - // Don't apply lighting to contours - uniforms.kambient = 1.0 - uniforms.kdiffuse = 0.0 - uniforms.kspecular = 0.0 - uniforms.opacity = 1.0 - - shader.bind() - shader.uniforms = uniforms - - // Draw contour lines - var vao = this._contourVAO - vao.bind() - - // Draw contour levels - for (i = 0; i < 3; ++i) { - shader.uniforms.permutation = PERMUTATIONS[i] - gl.lineWidth(this.contourWidth[i]) - - for (j = 0; j < this.contourLevels[i].length; ++j) { - if (!this._contourCounts[i][j]) { - continue - } - if (j === this.highlightLevel[i]) { - shader.uniforms.contourColor = this.highlightColor[i] - shader.uniforms.contourTint = this.highlightTint[i] - } else if (j === 0 || (j - 1) === this.highlightLevel[i]) { - shader.uniforms.contourColor = this.contourColor[i] - shader.uniforms.contourTint = this.contourTint[i] - } - shader.uniforms.height = this.contourLevels[i][j] - vao.draw(gl.LINES, this._contourCounts[i][j], this._contourOffsets[i][j]) - } - } - - // Draw projections of surface - for (i = 0; i < 3; ++i) { - shader.uniforms.model = projectData.projections[i] - shader.uniforms.clipBounds = projectData.clipBounds[i] - for (j = 0; j < 3; ++j) { - if (!this.contourProject[i][j]) { - continue - } - shader.uniforms.permutation = PERMUTATIONS[j] - gl.lineWidth(this.contourWidth[j]) - for (var k = 0; k < this.contourLevels[j].length; ++k) { - if (k === this.highlightLevel[j]) { - shader.uniforms.contourColor = this.highlightColor[j] - shader.uniforms.contourTint = this.highlightTint[j] - } else if (k === 0 || (k - 1) === this.highlightLevel[j]) { - shader.uniforms.contourColor = this.contourColor[j] - shader.uniforms.contourTint = this.contourTint[j] - } - shader.uniforms.height = this.contourLevels[j][k] - vao.draw(gl.LINES, this._contourCounts[j][k], this._contourOffsets[j][k]) - } - } - } - - // Draw dynamic contours - vao = this._dynamicVAO - vao.bind() - - // Draw contour levels - for (i = 0; i < 3; ++i) { - if (this._dynamicCounts[i] === 0) { - continue - } - - shader.uniforms.model = uniforms.model - shader.uniforms.clipBounds = uniforms.clipBounds - shader.uniforms.permutation = PERMUTATIONS[i] - gl.lineWidth(this.dynamicWidth[i]) - - shader.uniforms.contourColor = this.dynamicColor[i] - shader.uniforms.contourTint = this.dynamicTint[i] - shader.uniforms.height = this.dynamicLevel[i] - vao.draw(gl.LINES, this._dynamicCounts[i], this._dynamicOffsets[i]) - - for (j = 0; j < 3; ++j) { - if (!this.contourProject[j][i]) { - continue - } - - shader.uniforms.model = projectData.projections[j] - shader.uniforms.clipBounds = projectData.clipBounds[j] - vao.draw(gl.LINES, this._dynamicCounts[i], this._dynamicOffsets[i]) - } - } - - vao.unbind() - } -} - -proto.draw = function (params) { - return drawCore.call(this, params, false) -} - -proto.drawTransparent = function (params) { - return drawCore.call(this, params, true) -} - -var PICK_UNIFORMS = { - model: IDENTITY, - view: IDENTITY, - projection: IDENTITY, - inverseModel: IDENTITY, - clipBounds: [[0, 0, 0], [0, 0, 0]], - height: 0.0, - shape: [0, 0], - pickId: 0, - lowerBound: [0, 0, 0], - upperBound: [0, 0, 0], - zOffset: 0.0, - permutation: [1, 0, 0, 0, 1, 0, 0, 0, 1], - lightPosition: [0, 0, 0], - eyePosition: [0, 0, 0] -} - -proto.drawPick = function (params) { - params = params || {} - var gl = this.gl - gl.disable(gl.CULL_FACE) - - var uniforms = PICK_UNIFORMS - uniforms.model = params.model || IDENTITY - uniforms.view = params.view || IDENTITY - uniforms.projection = params.projection || IDENTITY - uniforms.shape = this._field[2].shape - uniforms.pickId = this.pickId / 255.0 - uniforms.lowerBound = this.bounds[0] - uniforms.upperBound = this.bounds[1] - uniforms.permutation = DEFAULT_PERM - - for (var i = 0; i < 2; ++i) { - var clipClamped = uniforms.clipBounds[i] - for (var j = 0; j < 3; ++j) { - clipClamped[j] = Math.min(Math.max(this.clipBounds[i][j], -1e8), 1e8) - } - } - - var projectData = computeProjectionData(uniforms, this) - - if (projectData.showSurface) { - // Set up uniforms - this._pickShader.bind() - this._pickShader.uniforms = uniforms - - // Draw it - this._vao.bind() - this._vao.draw(gl.TRIANGLES, this._vertexCount) - - // Draw projections of surface - for (i = 0; i < 3; ++i) { - if (!this.surfaceProject[i]) { - continue - } - this._pickShader.uniforms.model = projectData.projections[i] - this._pickShader.uniforms.clipBounds = projectData.clipBounds[i] - this._vao.draw(gl.TRIANGLES, this._vertexCount) - } - - this._vao.unbind() - } - - if (projectData.showContour) { - var shader = this._contourPickShader - - shader.bind() - shader.uniforms = uniforms - - var vao = this._contourVAO - vao.bind() - - for (j = 0; j < 3; ++j) { - gl.lineWidth(this.contourWidth[j]) - shader.uniforms.permutation = PERMUTATIONS[j] - for (i = 0; i < this.contourLevels[j].length; ++i) { - if (this._contourCounts[j][i]) { - shader.uniforms.height = this.contourLevels[j][i] - vao.draw(gl.LINES, this._contourCounts[j][i], this._contourOffsets[j][i]) - } - } - } - - // Draw projections of surface - for (i = 0; i < 3; ++i) { - shader.uniforms.model = projectData.projections[i] - shader.uniforms.clipBounds = projectData.clipBounds[i] - - for (j = 0; j < 3; ++j) { - if (!this.contourProject[i][j]) { - continue - } - - shader.uniforms.permutation = PERMUTATIONS[j] - gl.lineWidth(this.contourWidth[j]) - for (var k = 0; k < this.contourLevels[j].length; ++k) { - if (this._contourCounts[j][k]) { - shader.uniforms.height = this.contourLevels[j][k] - vao.draw(gl.LINES, this._contourCounts[j][k], this._contourOffsets[j][k]) - } - } - } - } - - vao.unbind() - } -} - -proto.pick = function (selection) { - if (!selection) { - return null - } - - if (selection.id !== this.pickId) { - return null - } - - var shape = this._field[2].shape - - var result = this._pickResult - - // Compute uv coordinate - var x = shape[0] * (selection.value[0] + (selection.value[2] >> 4) / 16.0) / 255.0 - var ix = Math.floor(x) - var fx = x - ix - - var y = shape[1] * (selection.value[1] + (selection.value[2] & 15) / 16.0) / 255.0 - var iy = Math.floor(y) - var fy = y - iy - - ix += 1 - iy += 1 - - // Compute xyz coordinate - var pos = result.position - pos[0] = pos[1] = pos[2] = 0 - for (var dx = 0; dx < 2; ++dx) { - var s = dx ? fx : 1.0 - fx - for (var dy = 0; dy < 2; ++dy) { - var t = dy ? fy : 1.0 - fy - - var r = ix + dx - var c = iy + dy - var w = s * t - - for (var i = 0; i < 3; ++i) { - pos[i] += this._field[i].get(r, c) * w - } - } - } - - // Find closest level - var levelIndex = this._pickResult.level - for (var j = 0; j < 3; ++j) { - levelIndex[j] = bsearch.le(this.contourLevels[j], pos[j]) - if (levelIndex[j] < 0) { - if (this.contourLevels[j].length > 0) { - levelIndex[j] = 0 - } - } else if (levelIndex[j] < this.contourLevels[j].length - 1) { - var a = this.contourLevels[j][levelIndex[j]] - var b = this.contourLevels[j][levelIndex[j] + 1] - if (Math.abs(a - pos[j]) > Math.abs(b - pos[j])) { - levelIndex[j] += 1 - } - } - } - - result.index[0] = fx < 0.5 ? ix : (ix + 1) - result.index[1] = fy < 0.5 ? iy : (iy + 1) - - result.uv[0] = x / shape[0] - result.uv[1] = y / shape[1] - - for (i = 0; i < 3; ++i) { - result.dataCoordinate[i] = this._field[i].get(result.index[0], result.index[1]) - } - - return result -} - -function padField (nfield, field) { - var shape = field.shape.slice() - var nshape = nfield.shape.slice() - - // Center - ops.assign(nfield.lo(1, 1).hi(shape[0], shape[1]), field) - - // Edges - ops.assign(nfield.lo(1).hi(shape[0], 1), - field.hi(shape[0], 1)) - ops.assign(nfield.lo(1, nshape[1] - 1).hi(shape[0], 1), - field.lo(0, shape[1] - 1).hi(shape[0], 1)) - ops.assign(nfield.lo(0, 1).hi(1, shape[1]), - field.hi(1)) - ops.assign(nfield.lo(nshape[0] - 1, 1).hi(1, shape[1]), - field.lo(shape[0] - 1)) - // Corners - nfield.set(0, 0, field.get(0, 0)) - nfield.set(0, nshape[1] - 1, field.get(0, shape[1] - 1)) - nfield.set(nshape[0] - 1, 0, field.get(shape[0] - 1, 0)) - nfield.set(nshape[0] - 1, nshape[1] - 1, field.get(shape[0] - 1, shape[1] - 1)) -} - -function handleArray (param, ctor) { - if (Array.isArray(param)) { - return [ ctor(param[0]), ctor(param[1]), ctor(param[2]) ] - } - return [ ctor(param), ctor(param), ctor(param) ] -} - -function toColor (x) { - if (Array.isArray(x)) { - if (x.length === 3) { - return [x[0], x[1], x[2], 1] - } - return [x[0], x[1], x[2], x[3]] - } - return [0, 0, 0, 1] -} - -function handleColor (param) { - if (Array.isArray(param)) { - if (Array.isArray(param)) { - return [ - toColor(param[0]), - toColor(param[1]), - toColor(param[2]) ] - } else { - var c = toColor(param) - return [ - c.slice(), - c.slice(), - c.slice() ] - } - } -} - -proto.update = function (params) { - params = params || {} - - this.dirty = true - - if ('contourWidth' in params) { - this.contourWidth = handleArray(params.contourWidth, Number) - } - if ('showContour' in params) { - this.showContour = handleArray(params.showContour, Boolean) - } - if ('showSurface' in params) { - this.showSurface = !!params.showSurface - } - if ('contourTint' in params) { - this.contourTint = handleArray(params.contourTint, Boolean) - } - if ('contourColor' in params) { - this.contourColor = handleColor(params.contourColor) - } - if ('contourProject' in params) { - this.contourProject = handleArray(params.contourProject, function (x) { - return handleArray(x, Boolean) - }) - } - if ('surfaceProject' in params) { - this.surfaceProject = params.surfaceProject - } - if ('dynamicColor' in params) { - this.dynamicColor = handleColor(params.dynamicColor) - } - if ('dynamicTint' in params) { - this.dynamicTint = handleArray(params.dynamicTint, Number) - } - if ('dynamicWidth' in params) { - this.dynamicWidth = handleArray(params.dynamicWidth, Number) - } - if ('opacity' in params) { - this.opacity = params.opacity - } - if ('colorBounds' in params) { - this.colorBounds = params.colorBounds - } - if ('vertexColor' in params) { - this.vertexColor = params.vertexColor ? 1 : 0; - } - - var field = params.field || (params.coords && params.coords[2]) || null - var levelsChanged = false - - if (!field) { - if (this._field[2].shape[0] || this._field[2].shape[2]) { - field = this._field[2].lo(1, 1).hi(this._field[2].shape[0] - 2, this._field[2].shape[1] - 2) - } else { - field = this._field[2].hi(0, 0) - } - } - - // Update field - if ('field' in params || 'coords' in params) { - var fsize = (field.shape[0] + 2) * (field.shape[1] + 2) - - // Resize if necessary - if (fsize > this._field[2].data.length) { - pool.freeFloat(this._field[2].data) - this._field[2].data = pool.mallocFloat(bits.nextPow2(fsize)) - } - - // Pad field - this._field[2] = ndarray(this._field[2].data, [field.shape[0] + 2, field.shape[1] + 2]) - padField(this._field[2], field) - - // Save shape of field - this.shape = field.shape.slice() - var shape = this.shape - - // Resize coordinate fields if necessary - for (var i = 0; i < 2; ++i) { - if (this._field[2].size > this._field[i].data.length) { - pool.freeFloat(this._field[i].data) - this._field[i].data = pool.mallocFloat(this._field[2].size) - } - this._field[i] = ndarray(this._field[i].data, [shape[0] + 2, shape[1] + 2]) - } - - // Generate x/y coordinates - if (params.coords) { - var coords = params.coords - if (!Array.isArray(coords) || coords.length !== 3) { - throw new Error('gl-surface: invalid coordinates for x/y') - } - for (i = 0; i < 2; ++i) { - var coord = coords[i] - for (j = 0; j < 2; ++j) { - if (coord.shape[j] !== shape[j]) { - throw new Error('gl-surface: coords have incorrect shape') - } - } - padField(this._field[i], coord) - } - } else if (params.ticks) { - var ticks = params.ticks - if (!Array.isArray(ticks) || ticks.length !== 2) { - throw new Error('gl-surface: invalid ticks') - } - for (i = 0; i < 2; ++i) { - var tick = ticks[i] - if (Array.isArray(tick) || tick.length) { - tick = ndarray(tick) - } - if (tick.shape[0] !== shape[i]) { - throw new Error('gl-surface: invalid tick length') - } - // Make a copy view of the tick array - var tick2 = ndarray(tick.data, shape) - tick2.stride[i] = tick.stride[0] - tick2.stride[i ^ 1] = 0 - - // Fill in field array - padField(this._field[i], tick2) - } - } else { - for (i = 0; i < 2; ++i) { - var offset = [0, 0] - offset[i] = 1 - this._field[i] = ndarray(this._field[i].data, [shape[0] + 2, shape[1] + 2], offset, 0) - } - this._field[0].set(0, 0, 0) - for (var j = 0; j < shape[0]; ++j) { - this._field[0].set(j + 1, 0, j) - } - this._field[0].set(shape[0] + 1, 0, shape[0] - 1) - this._field[1].set(0, 0, 0) - for (j = 0; j < shape[1]; ++j) { - this._field[1].set(0, j + 1, j) - } - this._field[1].set(0, shape[1] + 1, shape[1] - 1) - } - - // Save shape - var fields = this._field - - // Compute surface normals - var dfields = ndarray(pool.mallocFloat(fields[2].size * 3 * 2), [3, shape[0] + 2, shape[1] + 2, 2]) - for (i = 0; i < 3; ++i) { - gradient(dfields.pick(i), fields[i], 'mirror') - } - var normals = ndarray(pool.mallocFloat(fields[2].size * 3), [shape[0] + 2, shape[1] + 2, 3]) - for (i = 0; i < shape[0] + 2; ++i) { - for (j = 0; j < shape[1] + 2; ++j) { - var dxdu = dfields.get(0, i, j, 0) - var dxdv = dfields.get(0, i, j, 1) - var dydu = dfields.get(1, i, j, 0) - var dydv = dfields.get(1, i, j, 1) - var dzdu = dfields.get(2, i, j, 0) - var dzdv = dfields.get(2, i, j, 1) - - var nx = dydu * dzdv - dydv * dzdu - var ny = dzdu * dxdv - dzdv * dxdu - var nz = dxdu * dydv - dxdv * dydu - - var nl = Math.sqrt(nx * nx + ny * ny + nz * nz) - if (nl < 1e-8) { - nl = Math.max(Math.abs(nx), Math.abs(ny), Math.abs(nz)) - if (nl < 1e-8) { - nz = 1.0 - ny = nx = 0.0 - nl = 1.0 - } else { - nl = 1.0 / nl - } - } else { - nl = 1.0 / Math.sqrt(nl) - } - - normals.set(i, j, 0, nx * nl) - normals.set(i, j, 1, ny * nl) - normals.set(i, j, 2, nz * nl) - } - } - pool.free(dfields.data) - - // Initialize surface - var lo = [ Infinity, Infinity, Infinity ] - var hi = [ -Infinity, -Infinity, -Infinity ] - var lo_intensity = Infinity - var hi_intensity = -Infinity - var count = (shape[0] - 1) * (shape[1] - 1) * 6 - var tverts = pool.mallocFloat(bits.nextPow2(10 * count)) - var tptr = 0 - var vertexCount = 0 - for (i = 0; i < shape[0] - 1; ++i) { - j_loop: - for (j = 0; j < shape[1] - 1; ++j) { - // Test for NaNs - for (var dx = 0; dx < 2; ++dx) { - for (var dy = 0; dy < 2; ++dy) { - for (var k = 0; k < 3; ++k) { - var f = this._field[k].get(1 + i + dx, 1 + j + dy) - if (isNaN(f) || !isFinite(f)) { - continue j_loop - } - } - } - } - for (k = 0; k < 6; ++k) { - var r = i + QUAD[k][0] - var c = j + QUAD[k][1] - - var tx = this._field[0].get(r + 1, c + 1) - var ty = this._field[1].get(r + 1, c + 1) - f = this._field[2].get(r + 1, c + 1) - var vf = f - nx = normals.get(r + 1, c + 1, 0) - ny = normals.get(r + 1, c + 1, 1) - nz = normals.get(r + 1, c + 1, 2) - - if (params.intensity) { - vf = params.intensity.get(r, c) - } - - tverts[tptr++] = r - tverts[tptr++] = c - tverts[tptr++] = tx - tverts[tptr++] = ty - tverts[tptr++] = f - tverts[tptr++] = 0 - tverts[tptr++] = vf - tverts[tptr++] = nx - tverts[tptr++] = ny - tverts[tptr++] = nz - - lo[0] = Math.min(lo[0], tx) - lo[1] = Math.min(lo[1], ty) - lo[2] = Math.min(lo[2], f) - lo_intensity = Math.min(lo_intensity, vf) - - hi[0] = Math.max(hi[0], tx) - hi[1] = Math.max(hi[1], ty) - hi[2] = Math.max(hi[2], f) - hi_intensity = Math.max(hi_intensity, vf) - - vertexCount += 1 - } - } - } - - if (params.intensityBounds) { - lo_intensity = +params.intensityBounds[0] - hi_intensity = +params.intensityBounds[1] - } - - // Scale all vertex intensities - for (i = 6; i < tptr; i += 10) { - tverts[i] = (tverts[i] - lo_intensity) / (hi_intensity - lo_intensity) - } - - this._vertexCount = vertexCount - this._coordinateBuffer.update(tverts.subarray(0, tptr)) - pool.freeFloat(tverts) - pool.free(normals.data) - - // Update bounds - this.bounds = [lo, hi] - - // Save intensity - this.intensity = params.intensity || this._field[2] - - if(this.intensityBounds[0] !== lo_intensity || this.intensityBounds[1] !== hi_intensity) { - levelsChanged = true - } - - // Save intensity bound - this.intensityBounds = [lo_intensity, hi_intensity] - } - - // Update level crossings - if ('levels' in params) { - var levels = params.levels - if (!Array.isArray(levels[0])) { - levels = [ [], [], levels ] - } else { - levels = levels.slice() - } - for (i = 0; i < 3; ++i) { - levels[i] = levels[i].slice() - levels.sort(function (a, b) { - return a - b - }) - } - change_test: - for (i = 0; i < 3; ++i) { - if (levels[i].length !== this.contourLevels[i].length) { - levelsChanged = true - break - } - for (j = 0; j < levels[i].length; ++j) { - if (levels[i][j] !== this.contourLevels[i][j]) { - levelsChanged = true - break change_test - } - } - } - this.contourLevels = levels - } - - if (levelsChanged) { - fields = this._field - shape = this.shape - - // Update contour lines - var contourVerts = [] - - for (var dim = 0; dim < 3; ++dim) { - levels = this.contourLevels[dim] - var levelOffsets = [] - var levelCounts = [] - - var parts = [0, 0, 0] - - for (i = 0; i < levels.length; ++i) { - var graph = surfaceNets(this._field[dim], levels[i]) - levelOffsets.push((contourVerts.length / 5) | 0) - vertexCount = 0 - - edge_loop: - for (j = 0; j < graph.cells.length; ++j) { - var e = graph.cells[j] - for (k = 0; k < 2; ++k) { - var p = graph.positions[e[k]] - - var x = p[0] - var ix = Math.floor(x) | 0 - var fx = x - ix - - var y = p[1] - var iy = Math.floor(y) | 0 - var fy = y - iy - - var hole = false - dd_loop: - for (var dd = 0; dd < 3; ++dd) { - parts[dd] = 0.0 - var iu = (dim + dd + 1) % 3 - for (dx = 0; dx < 2; ++dx) { - var s = dx ? fx : 1.0 - fx - r = Math.min(Math.max(ix + dx, 0), shape[0]) | 0 - for (dy = 0; dy < 2; ++dy) { - var t = dy ? fy : 1.0 - fy - c = Math.min(Math.max(iy + dy, 0), shape[1]) | 0 - - if (dd < 2) { - f = this._field[iu].get(r, c) - } else { - f = (this.intensity.get(r, c) - this.intensityBounds[0]) / (this.intensityBounds[1] - this.intensityBounds[0]) - } - if (!isFinite(f) || isNaN(f)) { - hole = true - break dd_loop - } - - var w = s * t - parts[dd] += w * f - } - } - } - - if (!hole) { - contourVerts.push(parts[0], parts[1], p[0], p[1], parts[2]) - vertexCount += 1 - } else { - if (k > 0) { - // If we already added first edge, pop off verts - for (var l = 0; l < 5; ++l) { - contourVerts.pop() - } - vertexCount -= 1 - } - continue edge_loop - } - } - } - levelCounts.push(vertexCount) - } - - // Store results - this._contourOffsets[dim] = levelOffsets - this._contourCounts[dim] = levelCounts - } - - var floatBuffer = pool.mallocFloat(contourVerts.length) - for (i = 0; i < contourVerts.length; ++i) { - floatBuffer[i] = contourVerts[i] - } - this._contourBuffer.update(floatBuffer) - pool.freeFloat(floatBuffer) - } - - if (params.colormap) { - this._colorMap.setPixels(genColormap(params.colormap)) - } -} - -proto.dispose = function () { - this._shader.dispose() - this._vao.dispose() - this._coordinateBuffer.dispose() - this._colorMap.dispose() - this._contourBuffer.dispose() - this._contourVAO.dispose() - this._contourShader.dispose() - this._contourPickShader.dispose() - this._dynamicBuffer.dispose() - this._dynamicVAO.dispose() - for (var i = 0; i < 3; ++i) { - pool.freeFloat(this._field[i].data) - } -} - -proto.highlight = function (selection) { - if (!selection) { - this._dynamicCounts = [0, 0, 0] - this.dyanamicLevel = [NaN, NaN, NaN] - this.highlightLevel = [-1, -1, -1] - return - } - - for (var i = 0; i < 3; ++i) { - if (this.enableHighlight[i]) { - this.highlightLevel[i] = selection.level[i] - } else { - this.highlightLevel[i] = -1 - } - } - - var levels - if (this.snapToData) { - levels = selection.dataCoordinate - } else { - levels = selection.position - } - if ((!this.enableDynamic[0] || levels[0] === this.dynamicLevel[0]) && - (!this.enableDynamic[1] || levels[1] === this.dynamicLevel[1]) && - (!this.enableDynamic[2] || levels[2] === this.dynamicLevel[2])) { - return - } - - var vertexCount = 0 - var shape = this.shape - var scratchBuffer = pool.mallocFloat(12 * shape[0] * shape[1]) - - for (var d = 0; d < 3; ++d) { - if (!this.enableDynamic[d]) { - this.dynamicLevel[d] = NaN - this._dynamicCounts[d] = 0 - continue - } - - this.dynamicLevel[d] = levels[d] - - var u = (d + 1) % 3 - var v = (d + 2) % 3 - - var f = this._field[d] - var g = this._field[u] - var h = this._field[v] - var intensity = this.intensity - - var graph = surfaceNets(f, levels[d]) - var edges = graph.cells - var positions = graph.positions - - this._dynamicOffsets[d] = vertexCount - - for (i = 0; i < edges.length; ++i) { - var e = edges[i] - for (var j = 0; j < 2; ++j) { - var p = positions[e[j]] - - var x = +p[0] - var ix = x | 0 - var jx = Math.min(ix + 1, shape[0]) | 0 - var fx = x - ix - var hx = 1.0 - fx - - var y = +p[1] - var iy = y | 0 - var jy = Math.min(iy + 1, shape[1]) | 0 - var fy = y - iy - var hy = 1.0 - fy - - var w00 = hx * hy - var w01 = hx * fy - var w10 = fx * hy - var w11 = fx * fy - - var cu = w00 * g.get(ix, iy) + - w01 * g.get(ix, jy) + - w10 * g.get(jx, iy) + - w11 * g.get(jx, jy) - - var cv = w00 * h.get(ix, iy) + - w01 * h.get(ix, jy) + - w10 * h.get(jx, iy) + - w11 * h.get(jx, jy) - - if (isNaN(cu) || isNaN(cv)) { - if (j) { - vertexCount -= 1 - } - break - } - - scratchBuffer[2 * vertexCount + 0] = cu - scratchBuffer[2 * vertexCount + 1] = cv - - vertexCount += 1 - } - } - - this._dynamicCounts[d] = vertexCount - this._dynamicOffsets[d] - } - - this._dynamicBuffer.update(scratchBuffer.subarray(0, 2 * vertexCount)) - pool.freeFloat(scratchBuffer) -} - -function createSurfacePlot (params) { - var gl = params.gl - var shader = createShader(gl) - var pickShader = createPickShader(gl) - var contourShader = createContourShader(gl) - var contourPickShader = createPickContourShader(gl) - - var coordinateBuffer = createBuffer(gl) - var vao = createVAO(gl, [ - { buffer: coordinateBuffer, - size: 4, - stride: SURFACE_VERTEX_SIZE, - offset: 0 - }, - { buffer: coordinateBuffer, - size: 3, - stride: SURFACE_VERTEX_SIZE, - offset: 16 - }, - { - buffer: coordinateBuffer, - size: 3, - stride: SURFACE_VERTEX_SIZE, - offset: 28 - } - ]) - - var contourBuffer = createBuffer(gl) - var contourVAO = createVAO(gl, [ - { - buffer: contourBuffer, - size: 4, - stride: 20, - offset: 0 - }, - { - buffer: contourBuffer, - size: 1, - stride: 20, - offset: 16 - } - ]) - - var dynamicBuffer = createBuffer(gl) - var dynamicVAO = createVAO(gl, [ - { - buffer: dynamicBuffer, - size: 2, - type: gl.FLOAT - }]) - - var cmap = createTexture(gl, 1, N_COLORS, gl.RGBA, gl.UNSIGNED_BYTE) - cmap.minFilter = gl.LINEAR - cmap.magFilter = gl.LINEAR - - var surface = new SurfacePlot( - gl, - [0, 0], - [[0, 0, 0], [0, 0, 0]], - shader, - pickShader, - coordinateBuffer, - vao, - cmap, - contourShader, - contourPickShader, - contourBuffer, - contourVAO, - dynamicBuffer, - dynamicVAO - ) - - var nparams = { - levels: [[], [], []] - } - for (var id in params) { - nparams[id] = params[id] - } - nparams.colormap = nparams.colormap || 'jet' - - surface.update(nparams) - - return surface -} +validateStyleMin.source = wrapCleanErrors(require('./validate/validate_source')); +validateStyleMin.layer = wrapCleanErrors(require('./validate/validate_layer')); +validateStyleMin.filter = wrapCleanErrors(require('./validate/validate_filter')); +validateStyleMin.paintProperty = wrapCleanErrors(require('./validate/validate_paint_property')); +validateStyleMin.layoutProperty = wrapCleanErrors(require('./validate/validate_layout_property')); -},{"./lib/shaders":862,"binary-search-bounds":863,"bit-twiddle":864,"colormap":866,"gl-buffer":869,"gl-mat4/invert":235,"gl-mat4/multiply":237,"gl-texture2d":870,"gl-vao":874,"ndarray":1118,"ndarray-gradient":875,"ndarray-ops":1113,"ndarray-pack":880,"surface-nets":898,"typedarray-pool":899}],901:[function(require,module,exports){ +function sortErrors(errors) { + return [].concat(errors).sort(function (a, b) { + return a.line - b.line; + }); +} + +function wrapCleanErrors(inner) { + return function() { + return sortErrors(inner.apply(this, arguments)); + }; +} + +module.exports = validateStyleMin; + +},{"../reference/latest.min":301,"./validate/validate":284,"./validate/validate_constants":288,"./validate/validate_filter":290,"./validate/validate_glyphs_url":292,"./validate/validate_layer":293,"./validate/validate_layout_property":294,"./validate/validate_paint_property":297,"./validate/validate_source":298}],301:[function(require,module,exports){ +module.exports = require('./v8.min.json'); + +},{"./v8.min.json":302}],302:[function(require,module,exports){ +module.exports={"$version":8,"$root":{"version":{"required":true,"type":"enum","values":[8]},"name":{"type":"string"},"metadata":{"type":"*"},"center":{"type":"array","value":"number"},"zoom":{"type":"number"},"bearing":{"type":"number","default":0,"period":360,"units":"degrees"},"pitch":{"type":"number","default":0,"units":"degrees"},"sources":{"required":true,"type":"sources"},"sprite":{"type":"string"},"glyphs":{"type":"string"},"transition":{"type":"transition"},"layers":{"required":true,"type":"array","value":"layer"}},"sources":{"*":{"type":"source"}},"source":["source_tile","source_geojson","source_video","source_image"],"source_tile":{"type":{"required":true,"type":"enum","values":["vector","raster"]},"url":{"type":"string"},"tiles":{"type":"array","value":"string"},"minzoom":{"type":"number","default":0},"maxzoom":{"type":"number","default":22},"tileSize":{"type":"number","default":512,"units":"pixels"},"*":{"type":"*"}},"source_geojson":{"type":{"required":true,"type":"enum","values":["geojson"]},"data":{"type":"*"},"maxzoom":{"type":"number","default":14},"buffer":{"type":"number","default":64},"tolerance":{"type":"number","default":3},"cluster":{"type":"boolean","default":false},"clusterRadius":{"type":"number","default":400},"clusterMaxZoom":{"type":"number"}},"source_video":{"type":{"required":true,"type":"enum","values":["video"]},"urls":{"required":true,"type":"array","value":"string"},"coordinates":{"required":true,"type":"array","length":4,"value":{"type":"array","length":2,"value":"number"}}},"source_image":{"type":{"required":true,"type":"enum","values":["image"]},"url":{"required":true,"type":"string"},"coordinates":{"required":true,"type":"array","length":4,"value":{"type":"array","length":2,"value":"number"}}},"layer":{"id":{"type":"string","required":true},"type":{"type":"enum","values":["fill","line","symbol","circle","raster","background"]},"metadata":{"type":"*"},"ref":{"type":"string"},"source":{"type":"string"},"source-layer":{"type":"string"},"minzoom":{"type":"number","minimum":0,"maximum":22},"maxzoom":{"type":"number","minimum":0,"maximum":22},"interactive":{"type":"boolean","default":false},"filter":{"type":"filter"},"layout":{"type":"layout"},"paint":{"type":"paint"},"paint.*":{"type":"paint"}},"layout":["layout_fill","layout_line","layout_circle","layout_symbol","layout_raster","layout_background"],"layout_background":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_fill":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_circle":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_line":{"line-cap":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["butt","round","square"],"default":"butt"},"line-join":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["bevel","round","miter"],"default":"miter"},"line-miter-limit":{"type":"number","default":2,"function":"interpolated","zoom-function":true,"property-function":true,"requires":[{"line-join":"miter"}]},"line-round-limit":{"type":"number","default":1.05,"function":"interpolated","zoom-function":true,"property-function":true,"requires":[{"line-join":"round"}]},"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_symbol":{"symbol-placement":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["point","line"],"default":"point"},"symbol-spacing":{"type":"number","default":250,"minimum":1,"function":"interpolated","zoom-function":true,"property-function":true,"units":"pixels","requires":[{"symbol-placement":"line"}]},"symbol-avoid-edges":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false},"icon-allow-overlap":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image"]},"icon-ignore-placement":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image"]},"icon-optional":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image","text-field"]},"icon-rotation-alignment":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"viewport","requires":["icon-image"]},"icon-size":{"type":"number","default":1,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"requires":["icon-image"]},"icon-text-fit":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":false,"values":["none","both","width","height"],"default":"none","requires":["icon-image","text-field"]},"icon-text-fit-padding":{"type":"array","value":"number","length":4,"default":[0,0,0,0],"units":"pixels","function":"interpolated","zoom-function":true,"property-function":true,"requires":["icon-image","icon-text-fit","text-field"]},"icon-image":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"tokens":true},"icon-rotate":{"type":"number","default":0,"period":360,"function":"interpolated","zoom-function":true,"property-function":true,"units":"degrees","requires":["icon-image"]},"icon-padding":{"type":"number","default":2,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"units":"pixels","requires":["icon-image"]},"icon-keep-upright":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image",{"icon-rotation-alignment":"map"},{"symbol-placement":"line"}]},"icon-offset":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"requires":["icon-image"]},"text-pitch-alignment":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"requires":["text-field"]},"text-rotation-alignment":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"viewport","requires":["text-field"]},"text-field":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":"","tokens":true},"text-font":{"type":"array","value":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":["Open Sans Regular","Arial Unicode MS Regular"],"requires":["text-field"]},"text-size":{"type":"number","default":16,"minimum":0,"units":"pixels","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-max-width":{"type":"number","default":10,"minimum":0,"units":"em","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-line-height":{"type":"number","default":1.2,"units":"em","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-letter-spacing":{"type":"number","default":0,"units":"em","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-justify":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["left","center","right"],"default":"center","requires":["text-field"]},"text-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["center","left","right","top","bottom","top-left","top-right","bottom-left","bottom-right"],"default":"center","requires":["text-field"]},"text-max-angle":{"type":"number","default":45,"units":"degrees","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field",{"symbol-placement":"line"}]},"text-rotate":{"type":"number","default":0,"period":360,"units":"degrees","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-padding":{"type":"number","default":2,"minimum":0,"units":"pixels","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-keep-upright":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":true,"requires":["text-field",{"text-rotation-alignment":"map"},{"symbol-placement":"line"}]},"text-transform":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["none","uppercase","lowercase"],"default":"none","requires":["text-field"]},"text-offset":{"type":"array","value":"number","units":"ems","function":"interpolated","zoom-function":true,"property-function":true,"length":2,"default":[0,0],"requires":["text-field"]},"text-allow-overlap":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["text-field"]},"text-ignore-placement":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["text-field"]},"text-optional":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["text-field","icon-image"]},"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_raster":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"filter":{"type":"array","value":"*"},"filter_operator":{"type":"enum","values":["==","!=",">",">=","<","<=","in","!in","all","any","none","has","!has"]},"geometry_type":{"type":"enum","values":["Point","LineString","Polygon"]},"color_operation":{"type":"enum","values":["lighten","saturate","spin","fade","mix"]},"function":{"stops":{"type":"array","required":true,"value":"function_stop"},"base":{"type":"number","default":1,"minimum":0},"property":{"type":"string","default":"$zoom"},"type":{"type":"enum","values":["exponential","interval","categorical"],"default":"exponential"}},"function_stop":{"type":"array","minimum":0,"maximum":22,"value":["number","color"],"length":2},"paint":["paint_fill","paint_line","paint_circle","paint_symbol","paint_raster","paint_background"],"paint_fill":{"fill-antialias":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":true},"fill-opacity":{"type":"number","function":"interpolated","zoom-function":true,"property-function":true,"default":1,"minimum":0,"maximum":1,"transition":true},"fill-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":[{"!":"fill-pattern"}]},"fill-outline-color":{"type":"color","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":[{"!":"fill-pattern"},{"fill-antialias":true}]},"fill-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"fill-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["fill-translate"]},"fill-pattern":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"transition":true}},"paint_line":{"line-opacity":{"type":"number","function":"interpolated","zoom-function":true,"property-function":true,"default":1,"minimum":0,"maximum":1,"transition":true},"line-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":[{"!":"line-pattern"}]},"line-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["line-translate"]},"line-width":{"type":"number","default":1,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-gap-width":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-offset":{"type":"number","default":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-blur":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-dasharray":{"type":"array","value":"number","function":"piecewise-constant","zoom-function":true,"property-function":true,"minimum":0,"transition":true,"units":"line widths","requires":[{"!":"line-pattern"}]},"line-pattern":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"transition":true}},"paint_circle":{"circle-radius":{"type":"number","default":5,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"circle-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true},"circle-blur":{"type":"number","default":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true},"circle-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true},"circle-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"circle-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["circle-translate"]},"circle-pitch-scale":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map"}},"paint_symbol":{"icon-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["icon-image"]},"icon-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["icon-image"]},"icon-halo-color":{"type":"color","default":"rgba(0, 0, 0, 0)","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["icon-image"]},"icon-halo-width":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["icon-image"]},"icon-halo-blur":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["icon-image"]},"icon-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["icon-image"]},"icon-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["icon-image","icon-translate"]},"text-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["text-field"]},"text-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["text-field"]},"text-halo-color":{"type":"color","default":"rgba(0, 0, 0, 0)","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["text-field"]},"text-halo-width":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["text-field"]},"text-halo-blur":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["text-field"]},"text-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["text-field"]},"text-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["text-field","text-translate"]}},"paint_raster":{"raster-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true},"raster-hue-rotate":{"type":"number","default":0,"period":360,"function":"interpolated","zoom-function":true,"transition":true,"units":"degrees"},"raster-brightness-min":{"type":"number","function":"interpolated","zoom-function":true,"default":0,"minimum":0,"maximum":1,"transition":true},"raster-brightness-max":{"type":"number","function":"interpolated","zoom-function":true,"default":1,"minimum":0,"maximum":1,"transition":true},"raster-saturation":{"type":"number","default":0,"minimum":-1,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true},"raster-contrast":{"type":"number","default":0,"minimum":-1,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true},"raster-fade-duration":{"type":"number","default":300,"minimum":0,"function":"interpolated","zoom-function":true,"transition":true,"units":"milliseconds"}},"paint_background":{"background-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"transition":true,"requires":[{"!":"background-pattern"}]},"background-pattern":{"type":"string","function":"piecewise-constant","zoom-function":true,"transition":true},"background-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true}},"transition":{"duration":{"type":"number","default":300,"minimum":0,"units":"milliseconds"},"delay":{"type":"number","default":0,"minimum":0,"units":"milliseconds"}}} +},{}],303:[function(require,module,exports){ +'use strict'; + +if (typeof module !== 'undefined' && module.exports) { + module.exports = isSupported; +} else if (window) { + window.mapboxgl = window.mapboxgl || {}; + window.mapboxgl.supported = isSupported; +} + +/** + * Test whether the current browser supports Mapbox GL JS + * @param {Object} options + * @param {boolean} [options.failIfMajorPerformanceCaveat=false] Return `false` + * if the performance of Mapbox GL JS would be dramatically worse than + * expected (i.e. a software renderer is would be used) + * @return {boolean} + */ +function isSupported(options) { + return !!( + isBrowser() && + isArraySupported() && + isFunctionSupported() && + isObjectSupported() && + isJSONSupported() && + isWorkerSupported() && + isUint8ClampedArraySupported() && + isWebGLSupportedCached(options && options.failIfMajorPerformanceCaveat) + ); +} + +function isBrowser() { + return typeof window !== 'undefined' && typeof document !== 'undefined'; +} + +function isArraySupported() { + return ( + Array.prototype && + Array.prototype.every && + Array.prototype.filter && + Array.prototype.forEach && + Array.prototype.indexOf && + Array.prototype.lastIndexOf && + Array.prototype.map && + Array.prototype.some && + Array.prototype.reduce && + Array.prototype.reduceRight && + Array.isArray + ); +} + +function isFunctionSupported() { + return Function.prototype && Function.prototype.bind; +} + +function isObjectSupported() { + return ( + Object.keys && + Object.create && + Object.getPrototypeOf && + Object.getOwnPropertyNames && + Object.isSealed && + Object.isFrozen && + Object.isExtensible && + Object.getOwnPropertyDescriptor && + Object.defineProperty && + Object.defineProperties && + Object.seal && + Object.freeze && + Object.preventExtensions + ); +} + +function isJSONSupported() { + return 'JSON' in window && 'parse' in JSON && 'stringify' in JSON; +} + +function isWorkerSupported() { + return 'Worker' in window; +} + +// IE11 only supports `Uint8ClampedArray` as of version +// [KB2929437](https://support.microsoft.com/en-us/kb/2929437) +function isUint8ClampedArraySupported() { + return 'Uint8ClampedArray' in window; +} + +var isWebGLSupportedCache = {}; +function isWebGLSupportedCached(failIfMajorPerformanceCaveat) { + + if (isWebGLSupportedCache[failIfMajorPerformanceCaveat] === undefined) { + isWebGLSupportedCache[failIfMajorPerformanceCaveat] = isWebGLSupported(failIfMajorPerformanceCaveat); + } + + return isWebGLSupportedCache[failIfMajorPerformanceCaveat]; +} + +isSupported.webGLContextAttributes = { + antialias: false, + alpha: true, + stencil: true, + depth: true +}; + +function isWebGLSupported(failIfMajorPerformanceCaveat) { + + var canvas = document.createElement('canvas'); + + var attributes = Object.create(isSupported.webGLContextAttributes); + attributes.failIfMajorPerformanceCaveat = failIfMajorPerformanceCaveat; + + if (canvas.probablySupportsContext) { + return ( + canvas.probablySupportsContext('webgl', attributes) || + canvas.probablySupportsContext('experimental-webgl', attributes) + ); + + } else if (canvas.supportsContext) { + return ( + canvas.supportsContext('webgl', attributes) || + canvas.supportsContext('experimental-webgl', attributes) + ); + + } else { + return ( + canvas.getContext('webgl', attributes) || + canvas.getContext('experimental-webgl', attributes) + ); + } +} + +},{}],304:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -74350,7 +67168,7 @@ ArrayGroup.prototype.getTransferables = function(transferables) { } }; -},{"../util/util":1015}],902:[function(require,module,exports){ +},{"../util/util":418}],305:[function(require,module,exports){ 'use strict'; var featureFilter = require('feature-filter'); @@ -74868,7 +67686,7 @@ function createGetUniform(attribute, stopOffset) { }; } -},{"../util/struct_array":1013,"../util/util":1015,"./array_group":901,"./bucket/circle_bucket":903,"./bucket/fill_bucket":904,"./bucket/line_bucket":905,"./bucket/symbol_bucket":906,"./buffer_group":908,"assert":18,"feature-filter":1019}],903:[function(require,module,exports){ +},{"../util/struct_array":416,"../util/util":418,"./array_group":304,"./bucket/circle_bucket":306,"./bucket/fill_bucket":307,"./bucket/line_bucket":308,"./bucket/symbol_bucket":309,"./buffer_group":311,"assert":9,"feature-filter":98}],306:[function(require,module,exports){ 'use strict'; var Bucket = require('../bucket'); @@ -74990,7 +67808,7 @@ CircleBucket.prototype.addFeature = function(feature) { this.populatePaintArrays('circle', globalProperties, feature.properties, startGroup, startIndex); }; -},{"../../util/util":1015,"../bucket":902,"../load_geometry":910}],904:[function(require,module,exports){ +},{"../../util/util":418,"../bucket":305,"../load_geometry":313}],307:[function(require,module,exports){ 'use strict'; var Bucket = require('../bucket'); @@ -75101,7 +67919,7 @@ FillBucket.prototype.addPolygon = function(polygon) { } }; -},{"../../util/classify_rings":1003,"../../util/util":1015,"../bucket":902,"../load_geometry":910,"earcut":1018}],905:[function(require,module,exports){ +},{"../../util/classify_rings":406,"../../util/util":418,"../bucket":305,"../load_geometry":313,"earcut":91}],308:[function(require,module,exports){ 'use strict'; var Bucket = require('../bucket'); @@ -75528,7 +68346,7 @@ LineBucket.prototype.addPieSliceVertex = function(currentVertex, distance, extru } }; -},{"../../util/util":1015,"../bucket":902,"../load_geometry":910}],906:[function(require,module,exports){ +},{"../../util/util":418,"../bucket":305,"../load_geometry":313}],309:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -76174,7 +68992,7 @@ SymbolBucket.prototype.addSymbolQuad = function(symbolQuad) { symbolQuad.minScale); }; -},{"../../symbol/anchor":964,"../../symbol/clip_line":966,"../../symbol/collision_feature":968,"../../symbol/get_anchors":970,"../../symbol/mergelines":973,"../../symbol/quads":974,"../../symbol/resolve_text":975,"../../symbol/shaping":976,"../../util/token":1014,"../../util/util":1015,"../bucket":902,"../load_geometry":910,"point-geometry":1071}],907:[function(require,module,exports){ +},{"../../symbol/anchor":367,"../../symbol/clip_line":369,"../../symbol/collision_feature":371,"../../symbol/get_anchors":373,"../../symbol/mergelines":376,"../../symbol/quads":377,"../../symbol/resolve_text":378,"../../symbol/shaping":379,"../../util/token":417,"../../util/util":418,"../bucket":305,"../load_geometry":313,"point-geometry":919}],310:[function(require,module,exports){ 'use strict'; module.exports = Buffer; @@ -76275,7 +69093,7 @@ Buffer.BufferType = { ELEMENT: 'ELEMENT_ARRAY_BUFFER' }; -},{}],908:[function(require,module,exports){ +},{}],311:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -76330,7 +69148,7 @@ BufferGroup.prototype.destroy = function(gl) { } }; -},{"../render/vertex_array_object":930,"../util/util":1015,"./buffer":907}],909:[function(require,module,exports){ +},{"../render/vertex_array_object":333,"../util/util":418,"./buffer":310}],312:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -76629,7 +69447,7 @@ function offsetLine(rings, offset) { return newRings; } -},{"../util/dictionary_coder":1005,"../util/intersection_tests":1010,"../util/struct_array":1013,"../util/util":1015,"../util/vectortile_to_geojson":1016,"./bucket":902,"./load_geometry":910,"feature-filter":1019,"grid-index":1041,"pbf":1069,"point-geometry":1071,"vector-tile":1081}],910:[function(require,module,exports){ +},{"../util/dictionary_coder":408,"../util/intersection_tests":413,"../util/struct_array":416,"../util/util":418,"../util/vectortile_to_geojson":419,"./bucket":305,"./load_geometry":313,"feature-filter":98,"grid-index":259,"pbf":465,"point-geometry":919,"vector-tile":1002}],313:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -76685,7 +69503,7 @@ module.exports = function loadGeometry(feature, bits) { return geometry; }; -},{"../util/util":1015,"./bucket":902,"assert":18}],911:[function(require,module,exports){ +},{"../util/util":418,"./bucket":305,"assert":9}],314:[function(require,module,exports){ 'use strict'; module.exports = Coordinate; @@ -76764,7 +69582,7 @@ Coordinate.prototype = { } }; -},{}],912:[function(require,module,exports){ +},{}],315:[function(require,module,exports){ 'use strict'; module.exports = LngLat; @@ -76856,7 +69674,7 @@ LngLat.convert = function (input) { return input; }; -},{"../util/util":1015}],913:[function(require,module,exports){ +},{"../util/util":418}],316:[function(require,module,exports){ 'use strict'; module.exports = LngLatBounds; @@ -77048,7 +69866,7 @@ LngLatBounds.convert = function (input) { return new LngLatBounds(input); }; -},{"./lng_lat":912}],914:[function(require,module,exports){ +},{"./lng_lat":315}],317:[function(require,module,exports){ 'use strict'; var LngLat = require('./lng_lat'), @@ -77522,7 +70340,7 @@ Transform.prototype = { } }; -},{"../data/bucket":902,"../source/tile_coord":942,"../util/interpolate":1009,"../util/util":1015,"./coordinate":911,"./lng_lat":912,"gl-matrix":1031,"point-geometry":1071}],915:[function(require,module,exports){ +},{"../data/bucket":305,"../source/tile_coord":345,"../util/interpolate":412,"../util/util":418,"./coordinate":314,"./lng_lat":315,"gl-matrix":168,"point-geometry":919}],318:[function(require,module,exports){ 'use strict'; // Font data From Hershey Simplex Font @@ -77655,7 +70473,7 @@ module.exports = function textVertices(text, left, baseline, scale) { return strokes; }; -},{}],916:[function(require,module,exports){ +},{}],319:[function(require,module,exports){ 'use strict'; // jshint -W079 @@ -77722,7 +70540,7 @@ Object.defineProperty(mapboxgl, 'accessToken', { * mapboxgl.supported() // = true */ -},{"../package.json":1090,"./geo/lng_lat":912,"./geo/lng_lat_bounds":913,"./style/style":951,"./ui/control/attribution":982,"./ui/control/control":983,"./ui/control/geolocate":984,"./ui/control/navigation":985,"./ui/map":994,"./ui/marker":995,"./ui/popup":996,"./util/ajax":998,"./util/browser":999,"./util/config":1004,"./util/evented":1007,"./util/util":1015,"point-geometry":1071}],917:[function(require,module,exports){ +},{"../package.json":420,"./geo/lng_lat":315,"./geo/lng_lat_bounds":316,"./style/style":354,"./ui/control/attribution":385,"./ui/control/control":386,"./ui/control/geolocate":387,"./ui/control/navigation":388,"./ui/map":397,"./ui/marker":398,"./ui/popup":399,"./util/ajax":401,"./util/browser":402,"./util/config":407,"./util/evented":410,"./util/util":418,"point-geometry":919}],320:[function(require,module,exports){ 'use strict'; var assert = require('assert'); @@ -77742,7 +70560,7 @@ module.exports = function(uniforms) { return pragmas; }; -},{"assert":18}],918:[function(require,module,exports){ +},{"assert":9}],321:[function(require,module,exports){ 'use strict'; var pixelsToTileUnits = require('../source/pixels_to_tile_units'); @@ -77835,7 +70653,7 @@ function drawBackground(painter, source, layer) { gl.stencilFunc(gl.EQUAL, 0x80, 0x80); } -},{"../source/pixels_to_tile_units":936,"./create_uniform_pragmas":917}],919:[function(require,module,exports){ +},{"../source/pixels_to_tile_units":339,"./create_uniform_pragmas":320}],322:[function(require,module,exports){ 'use strict'; var browser = require('../util/browser'); @@ -77900,7 +70718,7 @@ function drawCircles(painter, source, layer, coords) { } } -},{"../util/browser":999}],920:[function(require,module,exports){ +},{"../util/browser":402}],323:[function(require,module,exports){ 'use strict'; module.exports = drawCollisionDebug; @@ -77935,7 +70753,7 @@ function drawCollisionDebug(painter, source, layer, coords) { } } -},{}],921:[function(require,module,exports){ +},{}],324:[function(require,module,exports){ 'use strict'; var textVertices = require('../lib/debugtext'); @@ -77996,7 +70814,7 @@ function drawDebugTile(painter, source, coord) { gl.drawArrays(gl.LINES, 0, debugTextBuffer.length); } -},{"../data/bucket":902,"../data/buffer":907,"../lib/debugtext":915,"../util/browser":999,"./vertex_array_object":930,"gl-matrix":1031}],922:[function(require,module,exports){ +},{"../data/bucket":305,"../data/buffer":310,"../lib/debugtext":318,"../util/browser":402,"./vertex_array_object":333,"gl-matrix":168}],325:[function(require,module,exports){ 'use strict'; var pixelsToTileUnits = require('../source/pixels_to_tile_units'); @@ -78193,7 +71011,7 @@ function setPattern(image, opacity, tile, coord, painter, program) { painter.spriteAtlas.bind(gl, true); } -},{"../source/pixels_to_tile_units":936}],923:[function(require,module,exports){ +},{"../source/pixels_to_tile_units":339}],326:[function(require,module,exports){ 'use strict'; var browser = require('../util/browser'); @@ -78358,7 +71176,7 @@ module.exports = function drawLine(painter, source, layer, coords) { }; -},{"../source/pixels_to_tile_units":936,"../util/browser":999,"gl-matrix":1031}],924:[function(require,module,exports){ +},{"../source/pixels_to_tile_units":339,"../util/browser":402,"gl-matrix":168}],327:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -78504,7 +71322,7 @@ function getOpacities(tile, parentTile, layer, transform) { return opacity; } -},{"../util/struct_array":1013,"../util/util":1015}],925:[function(require,module,exports){ +},{"../util/struct_array":416,"../util/util":418}],328:[function(require,module,exports){ 'use strict'; var browser = require('../util/browser'); @@ -78720,7 +71538,7 @@ function drawSymbol(painter, layer, posMatrix, tile, bucket, bufferGroups, isTex } } -},{"../source/pixels_to_tile_units":936,"../util/browser":999,"./draw_collision_debug":920}],926:[function(require,module,exports){ +},{"../source/pixels_to_tile_units":339,"../util/browser":402,"./draw_collision_debug":323}],329:[function(require,module,exports){ 'use strict'; module.exports = FrameHistory; @@ -78792,7 +71610,7 @@ FrameHistory.prototype.bind = function(gl) { } }; -},{}],927:[function(require,module,exports){ +},{}],330:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -78939,7 +71757,7 @@ LineAtlas.prototype.bind = function(gl) { } }; -},{"../util/util":1015}],928:[function(require,module,exports){ +},{"../util/util":418}],331:[function(require,module,exports){ 'use strict'; var browser = require('../util/browser'); @@ -79283,7 +72101,7 @@ Painter.prototype.showOverdrawInspector = function(enabled) { } }; -},{"../data/bucket":902,"../data/buffer":907,"../source/pixels_to_tile_units":936,"../source/source_cache":940,"../util/browser":999,"../util/struct_array":1013,"../util/util":1015,"./create_uniform_pragmas":917,"./draw_background":918,"./draw_circle":919,"./draw_debug":921,"./draw_fill":922,"./draw_line":923,"./draw_raster":924,"./draw_symbol":925,"./frame_history":926,"./painter/use_program":929,"./vertex_array_object":930,"gl-matrix":1031}],929:[function(require,module,exports){ +},{"../data/bucket":305,"../data/buffer":310,"../source/pixels_to_tile_units":339,"../source/source_cache":343,"../util/browser":402,"../util/struct_array":416,"../util/util":418,"./create_uniform_pragmas":320,"./draw_background":321,"./draw_circle":322,"./draw_debug":324,"./draw_fill":325,"./draw_line":326,"./draw_raster":327,"./draw_symbol":328,"./frame_history":329,"./painter/use_program":332,"./vertex_array_object":333,"gl-matrix":168}],332:[function(require,module,exports){ 'use strict'; var assert = require('assert'); @@ -79380,7 +72198,7 @@ function applyPragmas(source, pragmas) { }); } -},{"../../util/util":1015,"assert":18,"mapbox-gl-shaders":1043}],930:[function(require,module,exports){ +},{"../../util/util":418,"assert":9,"mapbox-gl-shaders":279}],333:[function(require,module,exports){ 'use strict'; var assert = require('assert'); @@ -79478,7 +72296,7 @@ VertexArrayObject.prototype.destroy = function(gl) { } }; -},{"assert":18}],931:[function(require,module,exports){ +},{"assert":9}],334:[function(require,module,exports){ 'use strict'; var Evented = require('../util/evented'); @@ -79702,7 +72520,7 @@ GeoJSONSource.prototype = util.inherit(Evented, /** @lends GeoJSONSource.prototy } }); -},{"../data/bucket":902,"../util/evented":1007,"../util/util":1015,"resolve-url":1073}],932:[function(require,module,exports){ +},{"../data/bucket":305,"../util/evented":410,"../util/util":418,"resolve-url":947}],335:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -79840,7 +72658,7 @@ GeoJSONWorkerSource.prototype = util.inherit(VectorTileWorkerSource, /** @lends } }); -},{"../util/ajax":998,"../util/util":1015,"./geojson_wrapper":933,"./vector_tile_worker_source":944,"geojson-rewind":1020,"geojson-vt":1026,"supercluster":1075,"vt-pbf":1085}],933:[function(require,module,exports){ +},{"../util/ajax":401,"../util/util":418,"./geojson_wrapper":336,"./vector_tile_worker_source":347,"geojson-rewind":103,"geojson-vt":107,"supercluster":980,"vt-pbf":1008}],336:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -79916,7 +72734,7 @@ FeatureWrapper.prototype.bbox = function() { FeatureWrapper.prototype.toGeoJSON = VectorTileFeature.prototype.toGeoJSON; -},{"../data/bucket":902,"point-geometry":1071,"vector-tile":1081}],934:[function(require,module,exports){ +},{"../data/bucket":305,"point-geometry":919,"vector-tile":1002}],337:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -80095,7 +72913,7 @@ ImageSource.prototype = util.inherit(Evented, /** @lends ImageSource.prototype * } }); -},{"../data/bucket":902,"../data/buffer":907,"../geo/lng_lat":912,"../render/draw_raster":924,"../render/vertex_array_object":930,"../util/ajax":998,"../util/evented":1007,"../util/util":1015,"./tile_coord":942,"point-geometry":1071}],935:[function(require,module,exports){ +},{"../data/bucket":305,"../data/buffer":310,"../geo/lng_lat":315,"../render/draw_raster":327,"../render/vertex_array_object":333,"../util/ajax":401,"../util/evented":410,"../util/util":418,"./tile_coord":345,"point-geometry":919}],338:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); var ajax = require('../util/ajax'); @@ -80126,7 +72944,7 @@ module.exports = function(options, callback) { }; -},{"../util/ajax":998,"../util/browser":999,"../util/mapbox":1012,"../util/util":1015}],936:[function(require,module,exports){ +},{"../util/ajax":401,"../util/browser":402,"../util/mapbox":415,"../util/util":418}],339:[function(require,module,exports){ 'use strict'; var Bucket = require('../data/bucket'); @@ -80151,7 +72969,7 @@ module.exports = function(tile, pixelValue, z) { }; -},{"../data/bucket":902}],937:[function(require,module,exports){ +},{"../data/bucket":305}],340:[function(require,module,exports){ 'use strict'; var TileCoord = require('./tile_coord'); @@ -80222,7 +73040,7 @@ function mergeRenderedFeatureLayers(tiles) { } -},{"./tile_coord":942}],938:[function(require,module,exports){ +},{"./tile_coord":345}],341:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -80319,7 +73137,7 @@ RasterTileSource.prototype = util.inherit(Evented, { } }); -},{"../util/ajax":998,"../util/evented":1007,"../util/mapbox":1012,"../util/util":1015,"./load_tilejson":935}],939:[function(require,module,exports){ +},{"../util/ajax":401,"../util/evented":410,"../util/mapbox":415,"../util/util":418,"./load_tilejson":338}],342:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -80490,7 +73308,7 @@ exports.setType = function (name, type) { * @instance */ -},{"../source/geojson_source":931,"../source/image_source":934,"../source/raster_tile_source":938,"../source/vector_tile_source":943,"../source/video_source":945,"../util/util":1015}],940:[function(require,module,exports){ +},{"../source/geojson_source":334,"../source/image_source":337,"../source/raster_tile_source":341,"../source/vector_tile_source":346,"../source/video_source":348,"../util/util":418}],343:[function(require,module,exports){ 'use strict'; var Source = require('./source'); @@ -81028,7 +73846,7 @@ function compareKeyZoom(a, b) { return (a % 32) - (b % 32); } -},{"../data/bucket":902,"../geo/coordinate":911,"../util/evented":1007,"../util/lru_cache":1011,"../util/util":1015,"./source":939,"./tile":941,"./tile_coord":942}],941:[function(require,module,exports){ +},{"../data/bucket":305,"../geo/coordinate":314,"../util/evented":410,"../util/lru_cache":414,"../util/util":418,"./source":342,"./tile":344,"./tile_coord":345}],344:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -81230,7 +74048,7 @@ function unserializeBuckets(input, style) { return output; } -},{"../data/bucket":902,"../data/feature_index":909,"../symbol/collision_box":967,"../symbol/collision_tile":969,"../symbol/symbol_instances":978,"../symbol/symbol_quads":979,"../util/util":1015,"../util/vectortile_to_geojson":1016,"feature-filter":1019,"pbf":1069,"vector-tile":1081}],942:[function(require,module,exports){ +},{"../data/bucket":305,"../data/feature_index":312,"../symbol/collision_box":370,"../symbol/collision_tile":372,"../symbol/symbol_instances":381,"../symbol/symbol_quads":382,"../util/util":418,"../util/vectortile_to_geojson":419,"feature-filter":98,"pbf":465,"vector-tile":1002}],345:[function(require,module,exports){ 'use strict'; var assert = require('assert'); @@ -81424,7 +74242,7 @@ TileCoord.cover = function(z, bounds, actualZ) { }); }; -},{"../geo/coordinate":911,"assert":18,"whoots-js":1089}],943:[function(require,module,exports){ +},{"../geo/coordinate":314,"assert":9,"whoots-js":1018}],346:[function(require,module,exports){ 'use strict'; var Evented = require('../util/evented'); @@ -81531,7 +74349,7 @@ VectorTileSource.prototype = util.inherit(Evented, { } }); -},{"../util/evented":1007,"../util/mapbox":1012,"../util/util":1015,"./load_tilejson":935}],944:[function(require,module,exports){ +},{"../util/evented":410,"../util/mapbox":415,"../util/util":418,"./load_tilejson":338}],347:[function(require,module,exports){ 'use strict'; var ajax = require('../util/ajax'); var vt = require('vector-tile'); @@ -81679,7 +74497,7 @@ VectorTileWorkerSource.prototype = { } }; -},{"../util/ajax":998,"./worker_tile":947,"pbf":1069,"vector-tile":1081}],945:[function(require,module,exports){ +},{"../util/ajax":401,"./worker_tile":350,"pbf":465,"vector-tile":1002}],348:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -81883,7 +74701,7 @@ VideoSource.prototype = util.inherit(Evented, /** @lends VideoSource.prototype * } }); -},{"../data/bucket":902,"../data/buffer":907,"../geo/lng_lat":912,"../render/draw_raster":924,"../render/vertex_array_object":930,"../util/ajax":998,"../util/evented":1007,"../util/util":1015,"./tile_coord":942,"point-geometry":1071}],946:[function(require,module,exports){ +},{"../data/bucket":305,"../data/buffer":310,"../geo/lng_lat":315,"../render/draw_raster":327,"../render/vertex_array_object":333,"../util/ajax":401,"../util/evented":410,"../util/util":418,"./tile_coord":345,"point-geometry":919}],349:[function(require,module,exports){ 'use strict'; var Actor = require('../util/actor'); @@ -82047,7 +74865,7 @@ function createLayerFamilies(layers) { return families; } -},{"../style/style_layer":954,"../util/actor":997,"../util/util":1015,"./geojson_worker_source":932,"./vector_tile_worker_source":944}],947:[function(require,module,exports){ +},{"../style/style_layer":357,"../util/actor":400,"../util/util":418,"./geojson_worker_source":335,"./vector_tile_worker_source":347}],350:[function(require,module,exports){ 'use strict'; var FeatureIndex = require('../data/feature_index'); @@ -82320,7 +75138,7 @@ function getLayerId(layer) { return layer.id; } -},{"../data/bucket":902,"../data/feature_index":909,"../symbol/collision_box":967,"../symbol/collision_tile":969,"../symbol/symbol_instances":978,"../symbol/symbol_quads":979,"../util/dictionary_coder":1005,"../util/util":1015}],948:[function(require,module,exports){ +},{"../data/bucket":305,"../data/feature_index":312,"../symbol/collision_box":370,"../symbol/collision_tile":372,"../symbol/symbol_instances":381,"../symbol/symbol_quads":382,"../util/dictionary_coder":408,"../util/util":418}],351:[function(require,module,exports){ 'use strict'; module.exports = AnimationLoop; @@ -82352,7 +75170,7 @@ AnimationLoop.prototype.cancel = function(n) { }); }; -},{}],949:[function(require,module,exports){ +},{}],352:[function(require,module,exports){ 'use strict'; var Evented = require('../util/evented'); @@ -82433,7 +75251,7 @@ ImageSprite.prototype.getSpritePosition = function(name) { return new SpritePosition(); }; -},{"../util/ajax":998,"../util/browser":999,"../util/evented":1007,"../util/mapbox":1012}],950:[function(require,module,exports){ +},{"../util/ajax":401,"../util/browser":402,"../util/evented":410,"../util/mapbox":415}],353:[function(require,module,exports){ 'use strict'; var parseColorString = require('csscolorparser').parseCSSColor; @@ -82475,7 +75293,7 @@ module.exports = function parseColor(input) { } }; -},{"../util/util":1015,"./style_function":953,"csscolorparser":1017}],951:[function(require,module,exports){ +},{"../util/util":418,"./style_function":356,"csscolorparser":80}],354:[function(require,module,exports){ 'use strict'; var Evented = require('../util/evented'); @@ -83256,7 +76074,7 @@ Style.prototype = util.inherit(Evented, { }); -},{"../render/line_atlas":927,"../source/query_features":937,"../source/source":939,"../source/source_cache":940,"../symbol/glyph_source":972,"../symbol/sprite_atlas":977,"../util/ajax":998,"../util/browser":999,"../util/dispatcher":1006,"../util/evented":1007,"../util/mapbox":1012,"../util/util":1015,"./animation_loop":948,"./image_sprite":949,"./style_function":953,"./style_layer":954,"./style_spec":961,"./validate_style":963}],952:[function(require,module,exports){ +},{"../render/line_atlas":330,"../source/query_features":340,"../source/source":342,"../source/source_cache":343,"../symbol/glyph_source":375,"../symbol/sprite_atlas":380,"../util/ajax":401,"../util/browser":402,"../util/dispatcher":409,"../util/evented":410,"../util/mapbox":415,"../util/util":418,"./animation_loop":351,"./image_sprite":352,"./style_function":356,"./style_layer":357,"./style_spec":364,"./validate_style":366}],355:[function(require,module,exports){ 'use strict'; var MapboxGLFunction = require('./style_function'); @@ -83340,7 +76158,7 @@ function transitioned(calculate) { }; } -},{"../util/util":1015,"./parse_color":950,"./style_function":953}],953:[function(require,module,exports){ +},{"../util/util":418,"./parse_color":353,"./style_function":356}],356:[function(require,module,exports){ 'use strict'; var MapboxGLFunction = require('mapbox-gl-function'); @@ -83367,7 +76185,7 @@ exports['piecewise-constant'] = function(parameters) { exports.isFunctionDefinition = MapboxGLFunction.isFunctionDefinition; -},{"mapbox-gl-function":1042}],954:[function(require,module,exports){ +},{"mapbox-gl-function":278}],357:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -83714,7 +76532,7 @@ function getDeclarationValue(declaration) { return declaration.value; } -},{"../util/evented":1007,"../util/util":1015,"./parse_color":950,"./style_declaration":952,"./style_layer/background_style_layer":955,"./style_layer/circle_style_layer":956,"./style_layer/fill_style_layer":957,"./style_layer/line_style_layer":958,"./style_layer/raster_style_layer":959,"./style_layer/symbol_style_layer":960,"./style_spec":961,"./style_transition":962,"./validate_style":963}],955:[function(require,module,exports){ +},{"../util/evented":410,"../util/util":418,"./parse_color":353,"./style_declaration":355,"./style_layer/background_style_layer":358,"./style_layer/circle_style_layer":359,"./style_layer/fill_style_layer":360,"./style_layer/line_style_layer":361,"./style_layer/raster_style_layer":362,"./style_layer/symbol_style_layer":363,"./style_spec":364,"./style_transition":365,"./validate_style":366}],358:[function(require,module,exports){ 'use strict'; var util = require('../../util/util'); @@ -83728,7 +76546,7 @@ module.exports = BackgroundStyleLayer; BackgroundStyleLayer.prototype = util.inherit(StyleLayer, {}); -},{"../../util/util":1015,"../style_layer":954}],956:[function(require,module,exports){ +},{"../../util/util":418,"../style_layer":357}],359:[function(require,module,exports){ 'use strict'; var util = require('../../util/util'); @@ -83742,7 +76560,7 @@ module.exports = CircleStyleLayer; CircleStyleLayer.prototype = util.inherit(StyleLayer, {}); -},{"../../util/util":1015,"../style_layer":954}],957:[function(require,module,exports){ +},{"../../util/util":418,"../style_layer":357}],360:[function(require,module,exports){ 'use strict'; var util = require('../../util/util'); @@ -83798,7 +76616,7 @@ FillStyleLayer.prototype = util.inherit(StyleLayer, { module.exports = FillStyleLayer; -},{"../../util/util":1015,"../style_layer":954}],958:[function(require,module,exports){ +},{"../../util/util":418,"../style_layer":357}],361:[function(require,module,exports){ 'use strict'; var util = require('../../util/util'); @@ -83832,7 +76650,7 @@ LineStyleLayer.prototype = util.inherit(StyleLayer, { } }); -},{"../../util/util":1015,"../style_layer":954}],959:[function(require,module,exports){ +},{"../../util/util":418,"../style_layer":357}],362:[function(require,module,exports){ 'use strict'; var util = require('../../util/util'); @@ -83846,7 +76664,7 @@ module.exports = RasterStyleLayer; RasterStyleLayer.prototype = util.inherit(StyleLayer, {}); -},{"../../util/util":1015,"../style_layer":954}],960:[function(require,module,exports){ +},{"../../util/util":418,"../style_layer":357}],363:[function(require,module,exports){ 'use strict'; var util = require('../../util/util'); @@ -83889,12 +76707,12 @@ SymbolStyleLayer.prototype = util.inherit(StyleLayer, { }); -},{"../../util/util":1015,"../style_layer":954}],961:[function(require,module,exports){ +},{"../../util/util":418,"../style_layer":357}],364:[function(require,module,exports){ 'use strict'; module.exports = require('mapbox-gl-style-spec/reference/latest.min'); -},{"mapbox-gl-style-spec/reference/latest.min":1065}],962:[function(require,module,exports){ +},{"mapbox-gl-style-spec/reference/latest.min":301}],365:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -83979,7 +76797,7 @@ function interpZoomTransitioned(from, to, t) { } } -},{"../util/interpolate":1009,"../util/util":1015}],963:[function(require,module,exports){ +},{"../util/interpolate":412,"../util/util":418}],366:[function(require,module,exports){ 'use strict'; module.exports = require('mapbox-gl-style-spec/lib/validate_style.min'); @@ -84003,7 +76821,7 @@ module.exports.throwErrors = function throwErrors(emitter, errors) { } }; -},{"mapbox-gl-style-spec/lib/validate_style.min":1064}],964:[function(require,module,exports){ +},{"mapbox-gl-style-spec/lib/validate_style.min":300}],367:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -84026,7 +76844,7 @@ Anchor.prototype.clone = function() { return new Anchor(this.x, this.y, this.angle, this.segment); }; -},{"point-geometry":1071}],965:[function(require,module,exports){ +},{"point-geometry":919}],368:[function(require,module,exports){ 'use strict'; module.exports = checkMaxAngle; @@ -84106,7 +76924,7 @@ function checkMaxAngle(line, anchor, labelLength, windowSize, maxAngle) { return true; } -},{}],966:[function(require,module,exports){ +},{}],369:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -84180,7 +76998,7 @@ function clipLine(lines, x1, y1, x2, y2) { return clippedLines; } -},{"point-geometry":1071}],967:[function(require,module,exports){ +},{"point-geometry":919}],370:[function(require,module,exports){ 'use strict'; var StructArrayType = require('../util/struct_array'); @@ -84261,7 +77079,7 @@ util.extendAll(CollisionBoxArray.prototype.StructType.prototype, { } }); -},{"../util/struct_array":1013,"../util/util":1015,"point-geometry":1071}],968:[function(require,module,exports){ +},{"../util/struct_array":416,"../util/util":418,"point-geometry":919}],371:[function(require,module,exports){ 'use strict'; module.exports = CollisionFeature; @@ -84395,7 +77213,7 @@ CollisionFeature.prototype._addLineCollisionBoxes = function(collisionBoxArray, return bboxes; }; -},{}],969:[function(require,module,exports){ +},{}],372:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -84696,7 +77514,7 @@ CollisionTile.prototype.insertCollisionFeature = function(collisionFeature, minP } }; -},{"../data/bucket":902,"grid-index":1041,"point-geometry":1071}],970:[function(require,module,exports){ +},{"../data/bucket":305,"grid-index":259,"point-geometry":919}],373:[function(require,module,exports){ 'use strict'; var interpolate = require('../util/interpolate'); @@ -84800,7 +77618,7 @@ function resample(line, offset, spacing, angleWindowSize, maxAngle, labelLength, return anchors; } -},{"../symbol/anchor":964,"../util/interpolate":1009,"./check_max_angle":965}],971:[function(require,module,exports){ +},{"../symbol/anchor":367,"../util/interpolate":412,"./check_max_angle":368}],374:[function(require,module,exports){ 'use strict'; var ShelfPack = require('shelf-pack'); @@ -84969,7 +77787,7 @@ GlyphAtlas.prototype.updateTexture = function(gl) { } }; -},{"../util/util":1015,"shelf-pack":1074}],972:[function(require,module,exports){ +},{"../util/util":418,"shelf-pack":964}],375:[function(require,module,exports){ 'use strict'; var normalizeURL = require('../util/mapbox').normalizeGlyphsURL; @@ -85110,7 +77928,7 @@ function glyphUrl(fontstack, range, url, subdomains) { .replace('{range}', range); } -},{"../symbol/glyph_atlas":971,"../util/ajax":998,"../util/glyphs":1008,"../util/mapbox":1012,"pbf":1069}],973:[function(require,module,exports){ +},{"../symbol/glyph_atlas":374,"../util/ajax":401,"../util/glyphs":411,"../util/mapbox":415,"pbf":465}],376:[function(require,module,exports){ 'use strict'; module.exports = function (features, textFeatures, geometries) { @@ -85201,7 +78019,7 @@ module.exports = function (features, textFeatures, geometries) { }; }; -},{}],974:[function(require,module,exports){ +},{}],377:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -85484,7 +78302,7 @@ function getSegmentGlyphs(glyphs, anchor, offset, line, segment, forward) { return placementScale; } -},{"point-geometry":1071}],975:[function(require,module,exports){ +},{"point-geometry":919}],378:[function(require,module,exports){ 'use strict'; var resolveTokens = require('../util/token'); @@ -85527,7 +78345,7 @@ function resolveText(features, layoutProperties, codepoints) { return textFeatures; } -},{"../util/token":1014}],976:[function(require,module,exports){ +},{"../util/token":417}],379:[function(require,module,exports){ 'use strict'; module.exports = { @@ -85708,7 +78526,7 @@ function PositionedIcon(image, top, bottom, left, right) { this.right = right; } -},{}],977:[function(require,module,exports){ +},{}],380:[function(require,module,exports){ 'use strict'; var ShelfPack = require('shelf-pack'); @@ -85941,7 +78759,7 @@ function AtlasImage(rect, width, height, sdf, pixelRatio) { this.pixelRatio = pixelRatio; } -},{"../util/browser":999,"../util/util":1015,"shelf-pack":1074}],978:[function(require,module,exports){ +},{"../util/browser":402,"../util/util":418,"shelf-pack":964}],381:[function(require,module,exports){ 'use strict'; var StructArrayType = require('../util/struct_array'); @@ -85986,7 +78804,7 @@ util.extendAll(SymbolInstancesArray.prototype.StructType.prototype, { -},{"../util/struct_array":1013,"../util/util":1015,"point-geometry":1071}],979:[function(require,module,exports){ +},{"../util/struct_array":416,"../util/util":418,"point-geometry":919}],382:[function(require,module,exports){ 'use strict'; var StructArrayType = require('../util/struct_array'); @@ -86060,7 +78878,7 @@ util.extendAll(SymbolQuadsArray.prototype.StructType.prototype, { }); -},{"../util/struct_array":1013,"../util/util":1015,"./quads":974,"point-geometry":1071}],980:[function(require,module,exports){ +},{"../util/struct_array":416,"../util/util":418,"./quads":377,"point-geometry":919}],383:[function(require,module,exports){ 'use strict'; var DOM = require('../util/dom'); @@ -86235,7 +79053,7 @@ module.exports = function bindHandlers(map, options) { * property. */ -},{"../util/dom":1001,"./handler/box_zoom":986,"./handler/dblclick_zoom":987,"./handler/drag_pan":988,"./handler/drag_rotate":989,"./handler/keyboard":990,"./handler/scroll_zoom":991,"./handler/touch_zoom_rotate":992,"point-geometry":1071}],981:[function(require,module,exports){ +},{"../util/dom":404,"./handler/box_zoom":389,"./handler/dblclick_zoom":390,"./handler/drag_pan":391,"./handler/drag_rotate":392,"./handler/keyboard":393,"./handler/scroll_zoom":394,"./handler/touch_zoom_rotate":395,"point-geometry":919}],384:[function(require,module,exports){ 'use strict'; var util = require('../util/util'); @@ -87022,7 +79840,7 @@ util.extend(Camera.prototype, /** @lends Map.prototype */{ * @property {MapEventData} data */ -},{"../geo/lng_lat":912,"../geo/lng_lat_bounds":913,"../util/browser":999,"../util/interpolate":1009,"../util/util":1015,"point-geometry":1071}],982:[function(require,module,exports){ +},{"../geo/lng_lat":315,"../geo/lng_lat_bounds":316,"../util/browser":402,"../util/interpolate":412,"../util/util":418,"point-geometry":919}],385:[function(require,module,exports){ 'use strict'; var Control = require('./control'); @@ -87105,7 +79923,7 @@ Attribution.prototype = util.inherit(Control, { } }); -},{"../../util/dom":1001,"../../util/util":1015,"./control":983}],983:[function(require,module,exports){ +},{"../../util/dom":404,"../../util/util":418,"./control":386}],386:[function(require,module,exports){ 'use strict'; var util = require('../../util/util'); @@ -87160,7 +79978,7 @@ Control.prototype = { util.extend(Control.prototype, Evented); -},{"../../util/evented":1007,"../../util/util":1015}],984:[function(require,module,exports){ +},{"../../util/evented":410,"../../util/util":418}],387:[function(require,module,exports){ 'use strict'; var Control = require('./control'); @@ -87262,7 +80080,7 @@ Geolocate.prototype = util.inherit(Control, { * */ -},{"../../util/browser":999,"../../util/dom":1001,"../../util/util":1015,"./control":983}],985:[function(require,module,exports){ +},{"../../util/browser":402,"../../util/dom":404,"../../util/util":418,"./control":386}],388:[function(require,module,exports){ 'use strict'; var Control = require('./control'); @@ -87383,7 +80201,7 @@ function copyMouseEvent(e) { }); } -},{"../../util/dom":1001,"../../util/util":1015,"./control":983}],986:[function(require,module,exports){ +},{"../../util/dom":404,"../../util/util":418,"./control":386}],389:[function(require,module,exports){ 'use strict'; var DOM = require('../../util/dom'), @@ -87570,7 +80388,7 @@ BoxZoomHandler.prototype = { * @property {MapBoxZoomEvent} data */ -},{"../../geo/lng_lat_bounds":913,"../../util/dom":1001,"../../util/util":1015}],987:[function(require,module,exports){ +},{"../../geo/lng_lat_bounds":316,"../../util/dom":404,"../../util/util":418}],390:[function(require,module,exports){ 'use strict'; module.exports = DoubleClickZoomHandler; @@ -87633,7 +80451,7 @@ DoubleClickZoomHandler.prototype = { } }; -},{}],988:[function(require,module,exports){ +},{}],391:[function(require,module,exports){ 'use strict'; var DOM = require('../../util/dom'), @@ -87869,7 +80687,7 @@ DragPanHandler.prototype = { * @property {MapMouseEvent | MapTouchEvent} data */ -},{"../../util/dom":1001,"../../util/util":1015}],989:[function(require,module,exports){ +},{"../../util/dom":404,"../../util/util":418}],392:[function(require,module,exports){ 'use strict'; var DOM = require('../../util/dom'), @@ -88123,7 +80941,7 @@ DragRotateHandler.prototype = { * @property {MapMouseEvent | MapTouchEvent} data */ -},{"../../util/dom":1001,"../../util/util":1015,"point-geometry":1071}],990:[function(require,module,exports){ +},{"../../util/dom":404,"../../util/util":418,"point-geometry":919}],393:[function(require,module,exports){ 'use strict'; module.exports = KeyboardHandler; @@ -88255,7 +81073,7 @@ KeyboardHandler.prototype = { } }; -},{}],991:[function(require,module,exports){ +},{}],394:[function(require,module,exports){ 'use strict'; var DOM = require('../../util/dom'), @@ -88438,7 +81256,7 @@ ScrollZoomHandler.prototype = { * @property {MapMouseEvent | MapTouchEvent} data */ -},{"../../util/browser":999,"../../util/dom":1001,"../../util/util":1015}],992:[function(require,module,exports){ +},{"../../util/browser":402,"../../util/dom":404,"../../util/util":418}],395:[function(require,module,exports){ 'use strict'; var DOM = require('../../util/dom'), @@ -88653,7 +81471,7 @@ TouchZoomRotateHandler.prototype = { } }; -},{"../../util/dom":1001,"../../util/util":1015}],993:[function(require,module,exports){ +},{"../../util/dom":404,"../../util/util":418}],396:[function(require,module,exports){ 'use strict'; /* @@ -88728,7 +81546,7 @@ Hash.prototype = { } }; -},{"../util/util":1015}],994:[function(require,module,exports){ +},{"../util/util":418}],397:[function(require,module,exports){ 'use strict'; var Canvas = require('../util/canvas'); @@ -90168,7 +82986,7 @@ function removeNode(node) { * @property {{error: {message: string}}} data */ -},{"../geo/lng_lat":912,"../geo/lng_lat_bounds":913,"../geo/transform":914,"../render/painter":928,"../style/animation_loop":948,"../style/style":951,"../util/browser":999,"../util/canvas":1000,"../util/dom":1001,"../util/evented":1007,"../util/util":1015,"./bind_handlers":980,"./camera":981,"./control/attribution":982,"./hash":993,"point-geometry":1071}],995:[function(require,module,exports){ +},{"../geo/lng_lat":315,"../geo/lng_lat_bounds":316,"../geo/transform":317,"../render/painter":331,"../style/animation_loop":351,"../style/style":354,"../util/browser":402,"../util/canvas":403,"../util/dom":404,"../util/evented":410,"../util/util":418,"./bind_handlers":383,"./camera":384,"./control/attribution":385,"./hash":396,"point-geometry":919}],398:[function(require,module,exports){ /* eslint-disable */ 'use strict'; @@ -90263,7 +83081,7 @@ Marker.prototype = { } }; -},{"../geo/lng_lat":912,"../util/dom":1001,"point-geometry":1071}],996:[function(require,module,exports){ +},{"../geo/lng_lat":315,"../util/dom":404,"point-geometry":919}],399:[function(require,module,exports){ 'use strict'; module.exports = Popup; @@ -90517,7 +83335,7 @@ Popup.prototype = util.inherit(Evented, /** @lends Popup.prototype */{ } }); -},{"../geo/lng_lat":912,"../util/dom":1001,"../util/evented":1007,"../util/util":1015}],997:[function(require,module,exports){ +},{"../geo/lng_lat":315,"../util/dom":404,"../util/evented":410,"../util/util":418}],400:[function(require,module,exports){ 'use strict'; module.exports = Actor; @@ -90589,7 +83407,7 @@ Actor.prototype.postMessage = function(message, transferList) { this.target.postMessage(message, transferList); }; -},{}],998:[function(require,module,exports){ +},{}],401:[function(require,module,exports){ 'use strict'; exports.getJSON = function(url, callback) { @@ -90679,7 +83497,7 @@ exports.getVideo = function(urls, callback) { return video; }; -},{}],999:[function(require,module,exports){ +},{}],402:[function(require,module,exports){ 'use strict'; /** @@ -90774,7 +83592,7 @@ webpImgTest.src = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAQAAAAfQ// exports.supportsGeolocation = !!navigator.geolocation; -},{"mapbox-gl-supported":1067}],1000:[function(require,module,exports){ +},{"mapbox-gl-supported":303}],403:[function(require,module,exports){ 'use strict'; var util = require('../util'); @@ -90818,7 +83636,7 @@ Canvas.prototype.getElement = function() { return this.canvas; }; -},{"../util":1015,"mapbox-gl-supported":1067}],1001:[function(require,module,exports){ +},{"../util":418,"mapbox-gl-supported":303}],404:[function(require,module,exports){ 'use strict'; var Point = require('point-geometry'); @@ -90893,7 +83711,7 @@ exports.touchPos = function (el, e) { return points; }; -},{"point-geometry":1071}],1002:[function(require,module,exports){ +},{"point-geometry":919}],405:[function(require,module,exports){ 'use strict'; var WebWorkify = require('webworkify'); @@ -90901,7 +83719,7 @@ module.exports = function () { return new WebWorkify(require('../../source/worker')); }; -},{"../../source/worker":946,"webworkify":1088}],1003:[function(require,module,exports){ +},{"../../source/worker":349,"webworkify":1016}],406:[function(require,module,exports){ 'use strict'; var quickselect = require('quickselect'); @@ -90961,7 +83779,7 @@ function calculateSignedArea(ring) { return sum; } -},{"quickselect":1072}],1004:[function(require,module,exports){ +},{"quickselect":929}],407:[function(require,module,exports){ 'use strict'; module.exports = { @@ -90969,7 +83787,7 @@ module.exports = { REQUIRE_ACCESS_TOKEN: true }; -},{}],1005:[function(require,module,exports){ +},{}],408:[function(require,module,exports){ 'use strict'; var assert = require('assert'); @@ -90996,7 +83814,7 @@ DictionaryCoder.prototype.decode = function(n) { return this._numberToString[n]; }; -},{"assert":18}],1006:[function(require,module,exports){ +},{"assert":9}],409:[function(require,module,exports){ 'use strict'; var util = require('./util'); @@ -91071,7 +83889,7 @@ Dispatcher.prototype = { } }; -},{"./actor":997,"./util":1015,"./web_worker":1002}],1007:[function(require,module,exports){ +},{"./actor":400,"./util":418,"./web_worker":405}],410:[function(require,module,exports){ 'use strict'; var util = require('./util'); @@ -91194,7 +84012,7 @@ var Evented = { module.exports = Evented; -},{"./util":1015}],1008:[function(require,module,exports){ +},{"./util":418}],411:[function(require,module,exports){ 'use strict'; module.exports = Glyphs; @@ -91229,7 +84047,7 @@ function readGlyph(tag, glyph, pbf) { else if (tag === 7) glyph.advance = pbf.readVarint(); } -},{}],1009:[function(require,module,exports){ +},{}],412:[function(require,module,exports){ 'use strict'; module.exports = interpolate; @@ -91270,7 +84088,7 @@ interpolate.array = function(from, to, t) { }); }; -},{}],1010:[function(require,module,exports){ +},{}],413:[function(require,module,exports){ 'use strict'; module.exports = { @@ -91436,7 +84254,7 @@ function polygonContainsPoint(ring, p) { return c; } -},{}],1011:[function(require,module,exports){ +},{}],414:[function(require,module,exports){ 'use strict'; module.exports = LRUCache; @@ -91561,7 +84379,7 @@ LRUCache.prototype.setMaxSize = function(max) { return this; }; -},{}],1012:[function(require,module,exports){ +},{}],415:[function(require,module,exports){ 'use strict'; var config = require('./config'); @@ -91689,7 +84507,7 @@ function replaceTempAccessToken(query) { } } -},{"./browser":999,"./config":1004,"./util":1015,"url":64}],1013:[function(require,module,exports){ +},{"./browser":402,"./config":407,"./util":418,"url":996}],416:[function(require,module,exports){ 'use strict'; // Note: all "sizes" are measured in bytes @@ -92037,7 +84855,7 @@ StructArray.prototype.toArray = function(startIndex, endIndex) { return array; }; -},{"assert":18}],1014:[function(require,module,exports){ +},{"assert":9}],417:[function(require,module,exports){ 'use strict'; module.exports = resolveTokens; @@ -92056,7 +84874,7 @@ function resolveTokens(properties, text) { }); } -},{}],1015:[function(require,module,exports){ +},{}],418:[function(require,module,exports){ 'use strict'; var UnitBezier = require('unitbezier'); @@ -92515,7 +85333,7 @@ exports.warnOnce = function(message) { } }; -},{"../geo/coordinate":911,"unitbezier":1080}],1016:[function(require,module,exports){ +},{"../geo/coordinate":314,"unitbezier":995}],419:[function(require,module,exports){ 'use strict'; module.exports = Feature; @@ -92560,12299 +85378,63 @@ Feature.prototype = { } }; -},{}],1017:[function(require,module,exports){ -// (c) Dean McNamee , 2012. -// -// https://github.com/deanm/css-color-parser-js -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. - -// http://www.w3.org/TR/css3-color/ -var kCSSColorTable = { - "transparent": [0,0,0,0], "aliceblue": [240,248,255,1], - "antiquewhite": [250,235,215,1], "aqua": [0,255,255,1], - "aquamarine": [127,255,212,1], "azure": [240,255,255,1], - "beige": [245,245,220,1], "bisque": [255,228,196,1], - "black": [0,0,0,1], "blanchedalmond": [255,235,205,1], - "blue": [0,0,255,1], "blueviolet": [138,43,226,1], - "brown": [165,42,42,1], "burlywood": [222,184,135,1], - "cadetblue": [95,158,160,1], "chartreuse": [127,255,0,1], - "chocolate": [210,105,30,1], "coral": [255,127,80,1], - "cornflowerblue": [100,149,237,1], "cornsilk": [255,248,220,1], - "crimson": [220,20,60,1], "cyan": [0,255,255,1], - "darkblue": [0,0,139,1], "darkcyan": [0,139,139,1], - "darkgoldenrod": [184,134,11,1], "darkgray": [169,169,169,1], - "darkgreen": [0,100,0,1], "darkgrey": [169,169,169,1], - "darkkhaki": [189,183,107,1], "darkmagenta": [139,0,139,1], - "darkolivegreen": [85,107,47,1], "darkorange": [255,140,0,1], - "darkorchid": [153,50,204,1], "darkred": [139,0,0,1], - "darksalmon": [233,150,122,1], "darkseagreen": [143,188,143,1], - "darkslateblue": [72,61,139,1], "darkslategray": [47,79,79,1], - "darkslategrey": [47,79,79,1], "darkturquoise": [0,206,209,1], - "darkviolet": [148,0,211,1], "deeppink": [255,20,147,1], - "deepskyblue": [0,191,255,1], "dimgray": [105,105,105,1], - "dimgrey": [105,105,105,1], "dodgerblue": [30,144,255,1], - "firebrick": [178,34,34,1], "floralwhite": [255,250,240,1], - "forestgreen": [34,139,34,1], "fuchsia": [255,0,255,1], - "gainsboro": [220,220,220,1], "ghostwhite": [248,248,255,1], - "gold": [255,215,0,1], "goldenrod": [218,165,32,1], - "gray": [128,128,128,1], "green": [0,128,0,1], - "greenyellow": [173,255,47,1], "grey": [128,128,128,1], - "honeydew": [240,255,240,1], "hotpink": [255,105,180,1], - "indianred": [205,92,92,1], "indigo": [75,0,130,1], - "ivory": [255,255,240,1], "khaki": [240,230,140,1], - "lavender": [230,230,250,1], "lavenderblush": [255,240,245,1], - "lawngreen": [124,252,0,1], "lemonchiffon": [255,250,205,1], - "lightblue": [173,216,230,1], "lightcoral": [240,128,128,1], - "lightcyan": [224,255,255,1], "lightgoldenrodyellow": [250,250,210,1], - "lightgray": [211,211,211,1], "lightgreen": [144,238,144,1], - "lightgrey": [211,211,211,1], "lightpink": [255,182,193,1], - "lightsalmon": [255,160,122,1], "lightseagreen": [32,178,170,1], - "lightskyblue": [135,206,250,1], "lightslategray": [119,136,153,1], - "lightslategrey": [119,136,153,1], "lightsteelblue": [176,196,222,1], - "lightyellow": [255,255,224,1], "lime": [0,255,0,1], - "limegreen": [50,205,50,1], "linen": [250,240,230,1], - "magenta": [255,0,255,1], "maroon": [128,0,0,1], - "mediumaquamarine": [102,205,170,1], "mediumblue": [0,0,205,1], - "mediumorchid": [186,85,211,1], "mediumpurple": [147,112,219,1], - "mediumseagreen": [60,179,113,1], "mediumslateblue": [123,104,238,1], - "mediumspringgreen": [0,250,154,1], "mediumturquoise": [72,209,204,1], - "mediumvioletred": [199,21,133,1], "midnightblue": [25,25,112,1], - "mintcream": [245,255,250,1], "mistyrose": [255,228,225,1], - "moccasin": [255,228,181,1], "navajowhite": [255,222,173,1], - "navy": [0,0,128,1], "oldlace": [253,245,230,1], - "olive": [128,128,0,1], "olivedrab": [107,142,35,1], - "orange": [255,165,0,1], "orangered": [255,69,0,1], - "orchid": [218,112,214,1], "palegoldenrod": [238,232,170,1], - "palegreen": [152,251,152,1], "paleturquoise": [175,238,238,1], - "palevioletred": [219,112,147,1], "papayawhip": [255,239,213,1], - "peachpuff": [255,218,185,1], "peru": [205,133,63,1], - "pink": [255,192,203,1], "plum": [221,160,221,1], - "powderblue": [176,224,230,1], "purple": [128,0,128,1], - "rebeccapurple": [102,51,153,1], - "red": [255,0,0,1], "rosybrown": [188,143,143,1], - "royalblue": [65,105,225,1], "saddlebrown": [139,69,19,1], - "salmon": [250,128,114,1], "sandybrown": [244,164,96,1], - "seagreen": [46,139,87,1], "seashell": [255,245,238,1], - "sienna": [160,82,45,1], "silver": [192,192,192,1], - "skyblue": [135,206,235,1], "slateblue": [106,90,205,1], - "slategray": [112,128,144,1], "slategrey": [112,128,144,1], - "snow": [255,250,250,1], "springgreen": [0,255,127,1], - "steelblue": [70,130,180,1], "tan": [210,180,140,1], - "teal": [0,128,128,1], "thistle": [216,191,216,1], - "tomato": [255,99,71,1], "turquoise": [64,224,208,1], - "violet": [238,130,238,1], "wheat": [245,222,179,1], - "white": [255,255,255,1], "whitesmoke": [245,245,245,1], - "yellow": [255,255,0,1], "yellowgreen": [154,205,50,1]} - -function clamp_css_byte(i) { // Clamp to integer 0 .. 255. - i = Math.round(i); // Seems to be what Chrome does (vs truncation). - return i < 0 ? 0 : i > 255 ? 255 : i; -} - -function clamp_css_float(f) { // Clamp to float 0.0 .. 1.0. - return f < 0 ? 0 : f > 1 ? 1 : f; -} - -function parse_css_int(str) { // int or percentage. - if (str[str.length - 1] === '%') - return clamp_css_byte(parseFloat(str) / 100 * 255); - return clamp_css_byte(parseInt(str)); -} - -function parse_css_float(str) { // float or percentage. - if (str[str.length - 1] === '%') - return clamp_css_float(parseFloat(str) / 100); - return clamp_css_float(parseFloat(str)); -} - -function css_hue_to_rgb(m1, m2, h) { - if (h < 0) h += 1; - else if (h > 1) h -= 1; - - if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; - if (h * 2 < 1) return m2; - if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6; - return m1; -} - -function parseCSSColor(css_str) { - // Remove all whitespace, not compliant, but should just be more accepting. - var str = css_str.replace(/ /g, '').toLowerCase(); - - // Color keywords (and transparent) lookup. - if (str in kCSSColorTable) return kCSSColorTable[str].slice(); // dup. - - // #abc and #abc123 syntax. - if (str[0] === '#') { - if (str.length === 4) { - var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. - if (!(iv >= 0 && iv <= 0xfff)) return null; // Covers NaN. - return [((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8), - (iv & 0xf0) | ((iv & 0xf0) >> 4), - (iv & 0xf) | ((iv & 0xf) << 4), - 1]; - } else if (str.length === 7) { - var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. - if (!(iv >= 0 && iv <= 0xffffff)) return null; // Covers NaN. - return [(iv & 0xff0000) >> 16, - (iv & 0xff00) >> 8, - iv & 0xff, - 1]; - } - - return null; - } - - var op = str.indexOf('('), ep = str.indexOf(')'); - if (op !== -1 && ep + 1 === str.length) { - var fname = str.substr(0, op); - var params = str.substr(op+1, ep-(op+1)).split(','); - var alpha = 1; // To allow case fallthrough. - switch (fname) { - case 'rgba': - if (params.length !== 4) return null; - alpha = parse_css_float(params.pop()); - // Fall through. - case 'rgb': - if (params.length !== 3) return null; - return [parse_css_int(params[0]), - parse_css_int(params[1]), - parse_css_int(params[2]), - alpha]; - case 'hsla': - if (params.length !== 4) return null; - alpha = parse_css_float(params.pop()); - // Fall through. - case 'hsl': - if (params.length !== 3) return null; - var h = (((parseFloat(params[0]) % 360) + 360) % 360) / 360; // 0 .. 1 - // NOTE(deanm): According to the CSS spec s/l should only be - // percentages, but we don't bother and let float or percentage. - var s = parse_css_float(params[1]); - var l = parse_css_float(params[2]); - var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s; - var m1 = l * 2 - m2; - return [clamp_css_byte(css_hue_to_rgb(m1, m2, h+1/3) * 255), - clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255), - clamp_css_byte(css_hue_to_rgb(m1, m2, h-1/3) * 255), - alpha]; - default: - return null; - } - } - - return null; -} - -try { exports.parseCSSColor = parseCSSColor } catch(e) { } - -},{}],1018:[function(require,module,exports){ -'use strict'; - -module.exports = earcut; - -function earcut(data, holeIndices, dim) { - - dim = dim || 2; - - var hasHoles = holeIndices && holeIndices.length, - outerLen = hasHoles ? holeIndices[0] * dim : data.length, - outerNode = linkedList(data, 0, outerLen, dim, true), - triangles = []; - - if (!outerNode) return triangles; - - var minX, minY, maxX, maxY, x, y, size; - - if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim); - - // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox - if (data.length > 80 * dim) { - minX = maxX = data[0]; - minY = maxY = data[1]; - - for (var i = dim; i < outerLen; i += dim) { - x = data[i]; - y = data[i + 1]; - if (x < minX) minX = x; - if (y < minY) minY = y; - if (x > maxX) maxX = x; - if (y > maxY) maxY = y; - } - - // minX, minY and size are later used to transform coords into integers for z-order calculation - size = Math.max(maxX - minX, maxY - minY); - } - - earcutLinked(outerNode, triangles, dim, minX, minY, size); - - return triangles; -} - -// create a circular doubly linked list from polygon points in the specified winding order -function linkedList(data, start, end, dim, clockwise) { - var i, last; - - if (clockwise === (signedArea(data, start, end, dim) > 0)) { - for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last); - } else { - for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last); - } - - if (last && equals(last, last.next)) { - removeNode(last); - last = last.next; - } - - return last; -} - -// eliminate colinear or duplicate points -function filterPoints(start, end) { - if (!start) return start; - if (!end) end = start; - - var p = start, - again; - do { - again = false; - - if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) { - removeNode(p); - p = end = p.prev; - if (p === p.next) return null; - again = true; - - } else { - p = p.next; - } - } while (again || p !== end); - - return end; -} - -// main ear slicing loop which triangulates a polygon (given as a linked list) -function earcutLinked(ear, triangles, dim, minX, minY, size, pass) { - if (!ear) return; - - // interlink polygon nodes in z-order - if (!pass && size) indexCurve(ear, minX, minY, size); - - var stop = ear, - prev, next; - - // iterate through ears, slicing them one by one - while (ear.prev !== ear.next) { - prev = ear.prev; - next = ear.next; - - if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) { - // cut off the triangle - triangles.push(prev.i / dim); - triangles.push(ear.i / dim); - triangles.push(next.i / dim); - - removeNode(ear); - - // skipping the next vertice leads to less sliver triangles - ear = next.next; - stop = next.next; - - continue; - } - - ear = next; - - // if we looped through the whole remaining polygon and can't find any more ears - if (ear === stop) { - // try filtering points and slicing again - if (!pass) { - earcutLinked(filterPoints(ear), triangles, dim, minX, minY, size, 1); - - // if this didn't work, try curing all small self-intersections locally - } else if (pass === 1) { - ear = cureLocalIntersections(ear, triangles, dim); - earcutLinked(ear, triangles, dim, minX, minY, size, 2); - - // as a last resort, try splitting the remaining polygon into two - } else if (pass === 2) { - splitEarcut(ear, triangles, dim, minX, minY, size); - } - - break; - } - } -} - -// check whether a polygon node forms a valid ear with adjacent nodes -function isEar(ear) { - var a = ear.prev, - b = ear, - c = ear.next; - - if (area(a, b, c) >= 0) return false; // reflex, can't be an ear - - // now make sure we don't have other points inside the potential ear - var p = ear.next.next; - - while (p !== ear.prev) { - if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && - area(p.prev, p, p.next) >= 0) return false; - p = p.next; - } - - return true; -} - -function isEarHashed(ear, minX, minY, size) { - var a = ear.prev, - b = ear, - c = ear.next; - - if (area(a, b, c) >= 0) return false; // reflex, can't be an ear - - // triangle bbox; min & max are calculated like this for speed - var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x), - minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y), - maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x), - maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y); - - // z-order range for the current triangle bbox; - var minZ = zOrder(minTX, minTY, minX, minY, size), - maxZ = zOrder(maxTX, maxTY, minX, minY, size); - - // first look for points inside the triangle in increasing z-order - var p = ear.nextZ; - - while (p && p.z <= maxZ) { - if (p !== ear.prev && p !== ear.next && - pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && - area(p.prev, p, p.next) >= 0) return false; - p = p.nextZ; - } - - // then look for points in decreasing z-order - p = ear.prevZ; - - while (p && p.z >= minZ) { - if (p !== ear.prev && p !== ear.next && - pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && - area(p.prev, p, p.next) >= 0) return false; - p = p.prevZ; - } - - return true; -} - -// go through all polygon nodes and cure small local self-intersections -function cureLocalIntersections(start, triangles, dim) { - var p = start; - do { - var a = p.prev, - b = p.next.next; - - if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) { - - triangles.push(a.i / dim); - triangles.push(p.i / dim); - triangles.push(b.i / dim); - - // remove two nodes involved - removeNode(p); - removeNode(p.next); - - p = start = b; - } - p = p.next; - } while (p !== start); - - return p; -} - -// try splitting polygon into two and triangulate them independently -function splitEarcut(start, triangles, dim, minX, minY, size) { - // look for a valid diagonal that divides the polygon into two - var a = start; - do { - var b = a.next.next; - while (b !== a.prev) { - if (a.i !== b.i && isValidDiagonal(a, b)) { - // split the polygon in two by the diagonal - var c = splitPolygon(a, b); - - // filter colinear points around the cuts - a = filterPoints(a, a.next); - c = filterPoints(c, c.next); - - // run earcut on each half - earcutLinked(a, triangles, dim, minX, minY, size); - earcutLinked(c, triangles, dim, minX, minY, size); - return; - } - b = b.next; - } - a = a.next; - } while (a !== start); -} - -// link every hole into the outer loop, producing a single-ring polygon without holes -function eliminateHoles(data, holeIndices, outerNode, dim) { - var queue = [], - i, len, start, end, list; - - for (i = 0, len = holeIndices.length; i < len; i++) { - start = holeIndices[i] * dim; - end = i < len - 1 ? holeIndices[i + 1] * dim : data.length; - list = linkedList(data, start, end, dim, false); - if (list === list.next) list.steiner = true; - queue.push(getLeftmost(list)); - } - - queue.sort(compareX); - - // process holes from left to right - for (i = 0; i < queue.length; i++) { - eliminateHole(queue[i], outerNode); - outerNode = filterPoints(outerNode, outerNode.next); - } - - return outerNode; -} - -function compareX(a, b) { - return a.x - b.x; -} - -// find a bridge between vertices that connects hole with an outer ring and and link it -function eliminateHole(hole, outerNode) { - outerNode = findHoleBridge(hole, outerNode); - if (outerNode) { - var b = splitPolygon(outerNode, hole); - filterPoints(b, b.next); - } -} - -// David Eberly's algorithm for finding a bridge between hole and outer polygon -function findHoleBridge(hole, outerNode) { - var p = outerNode, - hx = hole.x, - hy = hole.y, - qx = -Infinity, - m; - - // find a segment intersected by a ray from the hole's leftmost point to the left; - // segment's endpoint with lesser x will be potential connection point - do { - if (hy <= p.y && hy >= p.next.y) { - var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y); - if (x <= hx && x > qx) { - qx = x; - if (x === hx) { - if (hy === p.y) return p; - if (hy === p.next.y) return p.next; - } - m = p.x < p.next.x ? p : p.next; - } - } - p = p.next; - } while (p !== outerNode); - - if (!m) return null; - - if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint - - // look for points inside the triangle of hole point, segment intersection and endpoint; - // if there are no points found, we have a valid connection; - // otherwise choose the point of the minimum angle with the ray as connection point - - var stop = m, - mx = m.x, - my = m.y, - tanMin = Infinity, - tan; - - p = m.next; - - while (p !== stop) { - if (hx >= p.x && p.x >= mx && - pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) { - - tan = Math.abs(hy - p.y) / (hx - p.x); // tangential - - if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) { - m = p; - tanMin = tan; - } - } - - p = p.next; - } - - return m; -} - -// interlink polygon nodes in z-order -function indexCurve(start, minX, minY, size) { - var p = start; - do { - if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, size); - p.prevZ = p.prev; - p.nextZ = p.next; - p = p.next; - } while (p !== start); - - p.prevZ.nextZ = null; - p.prevZ = null; - - sortLinked(p); -} - -// Simon Tatham's linked list merge sort algorithm -// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html -function sortLinked(list) { - var i, p, q, e, tail, numMerges, pSize, qSize, - inSize = 1; - - do { - p = list; - list = null; - tail = null; - numMerges = 0; - - while (p) { - numMerges++; - q = p; - pSize = 0; - for (i = 0; i < inSize; i++) { - pSize++; - q = q.nextZ; - if (!q) break; - } - - qSize = inSize; - - while (pSize > 0 || (qSize > 0 && q)) { - - if (pSize === 0) { - e = q; - q = q.nextZ; - qSize--; - } else if (qSize === 0 || !q) { - e = p; - p = p.nextZ; - pSize--; - } else if (p.z <= q.z) { - e = p; - p = p.nextZ; - pSize--; - } else { - e = q; - q = q.nextZ; - qSize--; - } - - if (tail) tail.nextZ = e; - else list = e; - - e.prevZ = tail; - tail = e; - } - - p = q; - } - - tail.nextZ = null; - inSize *= 2; - - } while (numMerges > 1); - - return list; -} - -// z-order of a point given coords and size of the data bounding box -function zOrder(x, y, minX, minY, size) { - // coords are transformed into non-negative 15-bit integer range - x = 32767 * (x - minX) / size; - y = 32767 * (y - minY) / size; - - x = (x | (x << 8)) & 0x00FF00FF; - x = (x | (x << 4)) & 0x0F0F0F0F; - x = (x | (x << 2)) & 0x33333333; - x = (x | (x << 1)) & 0x55555555; - - y = (y | (y << 8)) & 0x00FF00FF; - y = (y | (y << 4)) & 0x0F0F0F0F; - y = (y | (y << 2)) & 0x33333333; - y = (y | (y << 1)) & 0x55555555; - - return x | (y << 1); -} - -// find the leftmost node of a polygon ring -function getLeftmost(start) { - var p = start, - leftmost = start; - do { - if (p.x < leftmost.x) leftmost = p; - p = p.next; - } while (p !== start); - - return leftmost; -} - -// check if a point lies within a convex triangle -function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) { - return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 && - (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 && - (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0; -} - -// check if a diagonal between two polygon nodes is valid (lies in polygon interior) -function isValidDiagonal(a, b) { - return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && - locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b); -} - -// signed area of a triangle -function area(p, q, r) { - return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); -} - -// check if two points are equal -function equals(p1, p2) { - return p1.x === p2.x && p1.y === p2.y; -} - -// check if two segments intersect -function intersects(p1, q1, p2, q2) { - if ((equals(p1, q1) && equals(p2, q2)) || - (equals(p1, q2) && equals(p2, q1))) return true; - return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 && - area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0; -} - -// check if a polygon diagonal intersects any polygon segments -function intersectsPolygon(a, b) { - var p = a; - do { - if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && - intersects(p, p.next, a, b)) return true; - p = p.next; - } while (p !== a); - - return false; -} - -// check if a polygon diagonal is locally inside the polygon -function locallyInside(a, b) { - return area(a.prev, a, a.next) < 0 ? - area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 : - area(a, b, a.prev) < 0 || area(a, a.next, b) < 0; -} - -// check if the middle point of a polygon diagonal is inside the polygon -function middleInside(a, b) { - var p = a, - inside = false, - px = (a.x + b.x) / 2, - py = (a.y + b.y) / 2; - do { - if (((p.y > py) !== (p.next.y > py)) && (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x)) - inside = !inside; - p = p.next; - } while (p !== a); - - return inside; -} - -// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two; -// if one belongs to the outer ring and another to a hole, it merges it into a single ring -function splitPolygon(a, b) { - var a2 = new Node(a.i, a.x, a.y), - b2 = new Node(b.i, b.x, b.y), - an = a.next, - bp = b.prev; - - a.next = b; - b.prev = a; - - a2.next = an; - an.prev = a2; - - b2.next = a2; - a2.prev = b2; - - bp.next = b2; - b2.prev = bp; - - return b2; -} - -// create a node and optionally link it with previous one (in a circular doubly linked list) -function insertNode(i, x, y, last) { - var p = new Node(i, x, y); - - if (!last) { - p.prev = p; - p.next = p; - - } else { - p.next = last.next; - p.prev = last; - last.next.prev = p; - last.next = p; - } - return p; -} - -function removeNode(p) { - p.next.prev = p.prev; - p.prev.next = p.next; - - if (p.prevZ) p.prevZ.nextZ = p.nextZ; - if (p.nextZ) p.nextZ.prevZ = p.prevZ; -} - -function Node(i, x, y) { - // vertice index in coordinates array - this.i = i; - - // vertex coordinates - this.x = x; - this.y = y; - - // previous and next vertice nodes in a polygon ring - this.prev = null; - this.next = null; - - // z-order curve value - this.z = null; - - // previous and next nodes in z-order - this.prevZ = null; - this.nextZ = null; - - // indicates whether this is a steiner point - this.steiner = false; -} - -// return a percentage difference between the polygon area and its triangulation area; -// used to verify correctness of triangulation -earcut.deviation = function (data, holeIndices, dim, triangles) { - var hasHoles = holeIndices && holeIndices.length; - var outerLen = hasHoles ? holeIndices[0] * dim : data.length; - - var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim)); - if (hasHoles) { - for (var i = 0, len = holeIndices.length; i < len; i++) { - var start = holeIndices[i] * dim; - var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length; - polygonArea -= Math.abs(signedArea(data, start, end, dim)); - } - } - - var trianglesArea = 0; - for (i = 0; i < triangles.length; i += 3) { - var a = triangles[i] * dim; - var b = triangles[i + 1] * dim; - var c = triangles[i + 2] * dim; - trianglesArea += Math.abs( - (data[a] - data[c]) * (data[b + 1] - data[a + 1]) - - (data[a] - data[b]) * (data[c + 1] - data[a + 1])); - } - - return polygonArea === 0 && trianglesArea === 0 ? 0 : - Math.abs((trianglesArea - polygonArea) / polygonArea); -}; - -function signedArea(data, start, end, dim) { - var sum = 0; - for (var i = start, j = end - dim; i < end; i += dim) { - sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]); - j = i; - } - return sum; -} - -// turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts -earcut.flatten = function (data) { - var dim = data[0][0].length, - result = {vertices: [], holes: [], dimensions: dim}, - holeIndex = 0; - - for (var i = 0; i < data.length; i++) { - for (var j = 0; j < data[i].length; j++) { - for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]); - } - if (i > 0) { - holeIndex += data[i - 1].length; - result.holes.push(holeIndex); - } - } - return result; -}; - -},{}],1019:[function(require,module,exports){ -'use strict'; - -module.exports = createFilter; - -var types = ['Unknown', 'Point', 'LineString', 'Polygon']; - -/** - * Given a filter expressed as nested arrays, return a new function - * that evaluates whether a given feature (with a .properties or .tags property) - * passes its test. - * - * @param {Array} filter mapbox gl filter - * @returns {Function} filter-evaluating function - */ -function createFilter(filter) { - return new Function('f', 'var p = (f && f.properties || {}); return ' + compile(filter)); -} - -function compile(filter) { - if (!filter) return 'true'; - var op = filter[0]; - if (filter.length <= 1) return op === 'any' ? 'false' : 'true'; - var str = - op === '==' ? compileComparisonOp(filter[1], filter[2], '===', false) : - op === '!=' ? compileComparisonOp(filter[1], filter[2], '!==', false) : - op === '<' || - op === '>' || - op === '<=' || - op === '>=' ? compileComparisonOp(filter[1], filter[2], op, true) : - op === 'any' ? compileLogicalOp(filter.slice(1), '||') : - op === 'all' ? compileLogicalOp(filter.slice(1), '&&') : - op === 'none' ? compileNegation(compileLogicalOp(filter.slice(1), '||')) : - op === 'in' ? compileInOp(filter[1], filter.slice(2)) : - op === '!in' ? compileNegation(compileInOp(filter[1], filter.slice(2))) : - op === 'has' ? compileHasOp(filter[1]) : - op === '!has' ? compileNegation(compileHasOp([filter[1]])) : - 'true'; - return '(' + str + ')'; -} - -function compilePropertyReference(property) { - return property === '$type' ? 'f.type' : - property === '$id' ? 'f.id' : - 'p[' + JSON.stringify(property) + ']'; -} - -function compileComparisonOp(property, value, op, checkType) { - var left = compilePropertyReference(property); - var right = property === '$type' ? types.indexOf(value) : JSON.stringify(value); - return (checkType ? 'typeof ' + left + '=== typeof ' + right + '&&' : '') + left + op + right; -} - -function compileLogicalOp(expressions, op) { - return expressions.map(compile).join(op); -} - -function compileInOp(property, values) { - if (property === '$type') values = values.map(function(value) { return types.indexOf(value); }); - var left = JSON.stringify(values.sort(compare)); - var right = compilePropertyReference(property); - - if (values.length <= 200) return left + '.indexOf(' + right + ') !== -1'; - - return 'function(v, a, i, j) {' + - 'while (i <= j) { var m = (i + j) >> 1;' + - ' if (a[m] === v) return true; if (a[m] > v) j = m - 1; else i = m + 1;' + - '}' + - 'return false; }(' + right + ', ' + left + ',0,' + (values.length - 1) + ')'; -} - -function compileHasOp(property) { - return JSON.stringify(property) + ' in p'; -} - -function compileNegation(expression) { - return '!(' + expression + ')'; -} - -// Comparison function to sort numbers and strings -function compare(a, b) { - return a < b ? -1 : a > b ? 1 : 0; -} - -},{}],1020:[function(require,module,exports){ -var geojsonArea = require('geojson-area'); - -module.exports = rewind; - -function rewind(gj, outer) { - switch ((gj && gj.type) || null) { - case 'FeatureCollection': - gj.features = gj.features.map(curryOuter(rewind, outer)); - return gj; - case 'Feature': - gj.geometry = rewind(gj.geometry, outer); - return gj; - case 'Polygon': - case 'MultiPolygon': - return correct(gj, outer); - default: - return gj; - } -} - -function curryOuter(a, b) { - return function(_) { return a(_, b); }; -} - -function correct(_, outer) { - if (_.type === 'Polygon') { - _.coordinates = correctRings(_.coordinates, outer); - } else if (_.type === 'MultiPolygon') { - _.coordinates = _.coordinates.map(curryOuter(correctRings, outer)); - } - return _; -} - -function correctRings(_, outer) { - outer = !!outer; - _[0] = wind(_[0], !outer); - for (var i = 1; i < _.length; i++) { - _[i] = wind(_[i], outer); - } - return _; -} - -function wind(_, dir) { - return cw(_) === dir ? _ : _.reverse(); -} - -function cw(_) { - return geojsonArea.ring(_) >= 0; -} - -},{"geojson-area":1021}],1021:[function(require,module,exports){ -var wgs84 = require('wgs84'); - -module.exports.geometry = geometry; -module.exports.ring = ringArea; - -function geometry(_) { - if (_.type === 'Polygon') return polygonArea(_.coordinates); - else if (_.type === 'MultiPolygon') { - var area = 0; - for (var i = 0; i < _.coordinates.length; i++) { - area += polygonArea(_.coordinates[i]); - } - return area; - } else { - return null; - } -} - -function polygonArea(coords) { - var area = 0; - if (coords && coords.length > 0) { - area += Math.abs(ringArea(coords[0])); - for (var i = 1; i < coords.length; i++) { - area -= Math.abs(ringArea(coords[i])); - } - } - return area; -} - -/** - * Calculate the approximate area of the polygon were it projected onto - * the earth. Note that this area will be positive if ring is oriented - * clockwise, otherwise it will be negative. - * - * Reference: - * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for - * Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion - * Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409 - * - * Returns: - * {float} The approximate signed geodesic area of the polygon in square - * meters. - */ - -function ringArea(coords) { - var area = 0; - - if (coords.length > 2) { - var p1, p2; - for (var i = 0; i < coords.length - 1; i++) { - p1 = coords[i]; - p2 = coords[i + 1]; - area += rad(p2[0] - p1[0]) * (2 + Math.sin(rad(p1[1])) + Math.sin(rad(p2[1]))); - } - - area = area * wgs84.RADIUS * wgs84.RADIUS / 2; - } - - return area; -} - -function rad(_) { - return _ * Math.PI / 180; -} - -},{"wgs84":1022}],1022:[function(require,module,exports){ -module.exports.RADIUS = 6378137; -module.exports.FLATTENING = 1/298.257223563; -module.exports.POLAR_RADIUS = 6356752.3142; - -},{}],1023:[function(require,module,exports){ -'use strict'; - -module.exports = clip; - -var createFeature = require('./feature'); - -/* clip features between two axis-parallel lines: - * | | - * ___|___ | / - * / | \____|____/ - * | | - */ - -function clip(features, scale, k1, k2, axis, intersect, minAll, maxAll) { - - k1 /= scale; - k2 /= scale; - - if (minAll >= k1 && maxAll <= k2) return features; // trivial accept - else if (minAll > k2 || maxAll < k1) return null; // trivial reject - - var clipped = []; - - for (var i = 0; i < features.length; i++) { - - var feature = features[i], - geometry = feature.geometry, - type = feature.type, - min, max; - - min = feature.min[axis]; - max = feature.max[axis]; - - if (min >= k1 && max <= k2) { // trivial accept - clipped.push(feature); - continue; - } else if (min > k2 || max < k1) continue; // trivial reject - - var slices = type === 1 ? - clipPoints(geometry, k1, k2, axis) : - clipGeometry(geometry, k1, k2, axis, intersect, type === 3); - - if (slices.length) { - // if a feature got clipped, it will likely get clipped on the next zoom level as well, - // so there's no need to recalculate bboxes - clipped.push(createFeature(feature.tags, type, slices, feature.id)); - } - } - - return clipped.length ? clipped : null; -} - -function clipPoints(geometry, k1, k2, axis) { - var slice = []; - - for (var i = 0; i < geometry.length; i++) { - var a = geometry[i], - ak = a[axis]; - - if (ak >= k1 && ak <= k2) slice.push(a); - } - return slice; -} - -function clipGeometry(geometry, k1, k2, axis, intersect, closed) { - - var slices = []; - - for (var i = 0; i < geometry.length; i++) { - - var ak = 0, - bk = 0, - b = null, - points = geometry[i], - area = points.area, - dist = points.dist, - outer = points.outer, - len = points.length, - a, j, last; - - var slice = []; - - for (j = 0; j < len - 1; j++) { - a = b || points[j]; - b = points[j + 1]; - ak = bk || a[axis]; - bk = b[axis]; - - if (ak < k1) { - - if ((bk > k2)) { // ---|-----|--> - slice.push(intersect(a, b, k1), intersect(a, b, k2)); - if (!closed) slice = newSlice(slices, slice, area, dist, outer); - - } else if (bk >= k1) slice.push(intersect(a, b, k1)); // ---|--> | - - } else if (ak > k2) { - - if ((bk < k1)) { // <--|-----|--- - slice.push(intersect(a, b, k2), intersect(a, b, k1)); - if (!closed) slice = newSlice(slices, slice, area, dist, outer); - - } else if (bk <= k2) slice.push(intersect(a, b, k2)); // | <--|--- - - } else { - - slice.push(a); - - if (bk < k1) { // <--|--- | - slice.push(intersect(a, b, k1)); - if (!closed) slice = newSlice(slices, slice, area, dist, outer); - - } else if (bk > k2) { // | ---|--> - slice.push(intersect(a, b, k2)); - if (!closed) slice = newSlice(slices, slice, area, dist, outer); - } - // | --> | - } - } - - // add the last point - a = points[len - 1]; - ak = a[axis]; - if (ak >= k1 && ak <= k2) slice.push(a); - - // close the polygon if its endpoints are not the same after clipping - - last = slice[slice.length - 1]; - if (closed && last && (slice[0][0] !== last[0] || slice[0][1] !== last[1])) slice.push(slice[0]); - - // add the final slice - newSlice(slices, slice, area, dist, outer); - } - - return slices; -} - -function newSlice(slices, slice, area, dist, outer) { - if (slice.length) { - // we don't recalculate the area/length of the unclipped geometry because the case where it goes - // below the visibility threshold as a result of clipping is rare, so we avoid doing unnecessary work - slice.area = area; - slice.dist = dist; - if (outer !== undefined) slice.outer = outer; - - slices.push(slice); - } - return []; -} - -},{"./feature":1025}],1024:[function(require,module,exports){ -'use strict'; - -module.exports = convert; - -var simplify = require('./simplify'); -var createFeature = require('./feature'); - -// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data - -function convert(data, tolerance) { - var features = []; - - if (data.type === 'FeatureCollection') { - for (var i = 0; i < data.features.length; i++) { - convertFeature(features, data.features[i], tolerance); - } - } else if (data.type === 'Feature') { - convertFeature(features, data, tolerance); - - } else { - // single geometry or a geometry collection - convertFeature(features, {geometry: data}, tolerance); - } - return features; -} - -function convertFeature(features, feature, tolerance) { - if (feature.geometry === null) { - // ignore features with null geometry - return; - } - - var geom = feature.geometry, - type = geom.type, - coords = geom.coordinates, - tags = feature.properties, - id = feature.id, - i, j, rings, projectedRing; - - if (type === 'Point') { - features.push(createFeature(tags, 1, [projectPoint(coords)], id)); - - } else if (type === 'MultiPoint') { - features.push(createFeature(tags, 1, project(coords), id)); - - } else if (type === 'LineString') { - features.push(createFeature(tags, 2, [project(coords, tolerance)], id)); - - } else if (type === 'MultiLineString' || type === 'Polygon') { - rings = []; - for (i = 0; i < coords.length; i++) { - projectedRing = project(coords[i], tolerance); - if (type === 'Polygon') projectedRing.outer = (i === 0); - rings.push(projectedRing); - } - features.push(createFeature(tags, type === 'Polygon' ? 3 : 2, rings, id)); - - } else if (type === 'MultiPolygon') { - rings = []; - for (i = 0; i < coords.length; i++) { - for (j = 0; j < coords[i].length; j++) { - projectedRing = project(coords[i][j], tolerance); - projectedRing.outer = (j === 0); - rings.push(projectedRing); - } - } - features.push(createFeature(tags, 3, rings, id)); - - } else if (type === 'GeometryCollection') { - for (i = 0; i < geom.geometries.length; i++) { - convertFeature(features, { - geometry: geom.geometries[i], - properties: tags - }, tolerance); - } - - } else { - throw new Error('Input data is not a valid GeoJSON object.'); - } -} - -function project(lonlats, tolerance) { - var projected = []; - for (var i = 0; i < lonlats.length; i++) { - projected.push(projectPoint(lonlats[i])); - } - if (tolerance) { - simplify(projected, tolerance); - calcSize(projected); - } - return projected; -} - -function projectPoint(p) { - var sin = Math.sin(p[1] * Math.PI / 180), - x = (p[0] / 360 + 0.5), - y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI); - - y = y < 0 ? 0 : - y > 1 ? 1 : y; - - return [x, y, 0]; -} - -// calculate area and length of the poly -function calcSize(points) { - var area = 0, - dist = 0; - - for (var i = 0, a, b; i < points.length - 1; i++) { - a = b || points[i]; - b = points[i + 1]; - - area += a[0] * b[1] - b[0] * a[1]; - - // use Manhattan distance instead of Euclidian one to avoid expensive square root computation - dist += Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1]); - } - points.area = Math.abs(area / 2); - points.dist = dist; -} - -},{"./feature":1025,"./simplify":1027}],1025:[function(require,module,exports){ -'use strict'; - -module.exports = createFeature; - -function createFeature(tags, type, geom, id) { - var feature = { - id: id || null, - type: type, - geometry: geom, - tags: tags || null, - min: [Infinity, Infinity], // initial bbox values - max: [-Infinity, -Infinity] - }; - calcBBox(feature); - return feature; -} - -// calculate the feature bounding box for faster clipping later -function calcBBox(feature) { - var geometry = feature.geometry, - min = feature.min, - max = feature.max; - - if (feature.type === 1) { - calcRingBBox(min, max, geometry); - } else { - for (var i = 0; i < geometry.length; i++) { - calcRingBBox(min, max, geometry[i]); - } - } - - return feature; -} - -function calcRingBBox(min, max, points) { - for (var i = 0, p; i < points.length; i++) { - p = points[i]; - min[0] = Math.min(p[0], min[0]); - max[0] = Math.max(p[0], max[0]); - min[1] = Math.min(p[1], min[1]); - max[1] = Math.max(p[1], max[1]); - } -} - -},{}],1026:[function(require,module,exports){ -'use strict'; - -module.exports = geojsonvt; - -var convert = require('./convert'), // GeoJSON conversion and preprocessing - transform = require('./transform'), // coordinate transformation - clip = require('./clip'), // stripe clipping algorithm - wrap = require('./wrap'), // date line processing - createTile = require('./tile'); // final simplified tile generation - - -function geojsonvt(data, options) { - return new GeoJSONVT(data, options); -} - -function GeoJSONVT(data, options) { - options = this.options = extend(Object.create(this.options), options); - - var debug = options.debug; - - if (debug) console.time('preprocess data'); - - var z2 = 1 << options.maxZoom, // 2^z - features = convert(data, options.tolerance / (z2 * options.extent)); - - this.tiles = {}; - this.tileCoords = []; - - if (debug) { - console.timeEnd('preprocess data'); - console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints); - console.time('generate tiles'); - this.stats = {}; - this.total = 0; - } - - features = wrap(features, options.buffer / options.extent, intersectX); - - // start slicing from the top tile down - if (features.length) this.splitTile(features, 0, 0, 0); - - if (debug) { - if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints); - console.timeEnd('generate tiles'); - console.log('tiles generated:', this.total, JSON.stringify(this.stats)); - } -} - -GeoJSONVT.prototype.options = { - maxZoom: 14, // max zoom to preserve detail on - indexMaxZoom: 5, // max zoom in the tile index - indexMaxPoints: 100000, // max number of points per tile in the tile index - solidChildren: false, // whether to tile solid square tiles further - tolerance: 3, // simplification tolerance (higher means simpler) - extent: 4096, // tile extent - buffer: 64, // tile buffer on each side - debug: 0 // logging level (0, 1 or 2) -}; - -GeoJSONVT.prototype.splitTile = function (features, z, x, y, cz, cx, cy) { - - var stack = [features, z, x, y], - options = this.options, - debug = options.debug, - solid = null; - - // avoid recursion by using a processing queue - while (stack.length) { - y = stack.pop(); - x = stack.pop(); - z = stack.pop(); - features = stack.pop(); - - var z2 = 1 << z, - id = toID(z, x, y), - tile = this.tiles[id], - tileTolerance = z === options.maxZoom ? 0 : options.tolerance / (z2 * options.extent); - - if (!tile) { - if (debug > 1) console.time('creation'); - - tile = this.tiles[id] = createTile(features, z2, x, y, tileTolerance, z === options.maxZoom); - this.tileCoords.push({z: z, x: x, y: y}); - - if (debug) { - if (debug > 1) { - console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)', - z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified); - console.timeEnd('creation'); - } - var key = 'z' + z; - this.stats[key] = (this.stats[key] || 0) + 1; - this.total++; - } - } - - // save reference to original geometry in tile so that we can drill down later if we stop now - tile.source = features; - - // if it's the first-pass tiling - if (!cz) { - // stop tiling if we reached max zoom, or if the tile is too simple - if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue; - - // if a drilldown to a specific tile - } else { - // stop tiling if we reached base zoom or our target tile zoom - if (z === options.maxZoom || z === cz) continue; - - // stop tiling if it's not an ancestor of the target tile - var m = 1 << (cz - z); - if (x !== Math.floor(cx / m) || y !== Math.floor(cy / m)) continue; - } - - // stop tiling if the tile is solid clipped square - if (!options.solidChildren && isClippedSquare(tile, options.extent, options.buffer)) { - if (cz) solid = z; // and remember the zoom if we're drilling down - continue; - } - - // if we slice further down, no need to keep source geometry - tile.source = null; - - if (debug > 1) console.time('clipping'); - - // values we'll use for clipping - var k1 = 0.5 * options.buffer / options.extent, - k2 = 0.5 - k1, - k3 = 0.5 + k1, - k4 = 1 + k1, - tl, bl, tr, br, left, right; - - tl = bl = tr = br = null; - - left = clip(features, z2, x - k1, x + k3, 0, intersectX, tile.min[0], tile.max[0]); - right = clip(features, z2, x + k2, x + k4, 0, intersectX, tile.min[0], tile.max[0]); - - if (left) { - tl = clip(left, z2, y - k1, y + k3, 1, intersectY, tile.min[1], tile.max[1]); - bl = clip(left, z2, y + k2, y + k4, 1, intersectY, tile.min[1], tile.max[1]); - } - - if (right) { - tr = clip(right, z2, y - k1, y + k3, 1, intersectY, tile.min[1], tile.max[1]); - br = clip(right, z2, y + k2, y + k4, 1, intersectY, tile.min[1], tile.max[1]); - } - - if (debug > 1) console.timeEnd('clipping'); - - if (features.length) { - stack.push(tl || [], z + 1, x * 2, y * 2); - stack.push(bl || [], z + 1, x * 2, y * 2 + 1); - stack.push(tr || [], z + 1, x * 2 + 1, y * 2); - stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1); - } - } - - return solid; -}; - -GeoJSONVT.prototype.getTile = function (z, x, y) { - var options = this.options, - extent = options.extent, - debug = options.debug; - - var z2 = 1 << z; - x = ((x % z2) + z2) % z2; // wrap tile x coordinate - - var id = toID(z, x, y); - if (this.tiles[id]) return transform.tile(this.tiles[id], extent); - - if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y); - - var z0 = z, - x0 = x, - y0 = y, - parent; - - while (!parent && z0 > 0) { - z0--; - x0 = Math.floor(x0 / 2); - y0 = Math.floor(y0 / 2); - parent = this.tiles[toID(z0, x0, y0)]; - } - - if (!parent || !parent.source) return null; - - // if we found a parent tile containing the original geometry, we can drill down from it - if (debug > 1) console.log('found parent tile z%d-%d-%d', z0, x0, y0); - - // it parent tile is a solid clipped square, return it instead since it's identical - if (isClippedSquare(parent, extent, options.buffer)) return transform.tile(parent, extent); - - if (debug > 1) console.time('drilling down'); - var solid = this.splitTile(parent.source, z0, x0, y0, z, x, y); - if (debug > 1) console.timeEnd('drilling down'); - - // one of the parent tiles was a solid clipped square - if (solid !== null) { - var m = 1 << (z - solid); - id = toID(solid, Math.floor(x / m), Math.floor(y / m)); - } - - return this.tiles[id] ? transform.tile(this.tiles[id], extent) : null; -}; - -function toID(z, x, y) { - return (((1 << z) * y + x) * 32) + z; -} - -function intersectX(a, b, x) { - return [x, (x - a[0]) * (b[1] - a[1]) / (b[0] - a[0]) + a[1], 1]; -} -function intersectY(a, b, y) { - return [(y - a[1]) * (b[0] - a[0]) / (b[1] - a[1]) + a[0], y, 1]; -} - -function extend(dest, src) { - for (var i in src) dest[i] = src[i]; - return dest; -} - -// checks whether a tile is a whole-area fill after clipping; if it is, there's no sense slicing it further -function isClippedSquare(tile, extent, buffer) { - - var features = tile.source; - if (features.length !== 1) return false; - - var feature = features[0]; - if (feature.type !== 3 || feature.geometry.length > 1) return false; - - var len = feature.geometry[0].length; - if (len !== 5) return false; - - for (var i = 0; i < len; i++) { - var p = transform.point(feature.geometry[0][i], extent, tile.z2, tile.x, tile.y); - if ((p[0] !== -buffer && p[0] !== extent + buffer) || - (p[1] !== -buffer && p[1] !== extent + buffer)) return false; - } - - return true; -} - -},{"./clip":1023,"./convert":1024,"./tile":1028,"./transform":1029,"./wrap":1030}],1027:[function(require,module,exports){ -'use strict'; - -module.exports = simplify; - -// calculate simplification data using optimized Douglas-Peucker algorithm - -function simplify(points, tolerance) { - - var sqTolerance = tolerance * tolerance, - len = points.length, - first = 0, - last = len - 1, - stack = [], - i, maxSqDist, sqDist, index; - - // always retain the endpoints (1 is the max value) - points[first][2] = 1; - points[last][2] = 1; - - // avoid recursion by using a stack - while (last) { - - maxSqDist = 0; - - for (i = first + 1; i < last; i++) { - sqDist = getSqSegDist(points[i], points[first], points[last]); - - if (sqDist > maxSqDist) { - index = i; - maxSqDist = sqDist; - } - } - - if (maxSqDist > sqTolerance) { - points[index][2] = maxSqDist; // save the point importance in squared pixels as a z coordinate - stack.push(first); - stack.push(index); - first = index; - - } else { - last = stack.pop(); - first = stack.pop(); - } - } -} - -// square distance from a point to a segment -function getSqSegDist(p, a, b) { - - var x = a[0], y = a[1], - bx = b[0], by = b[1], - px = p[0], py = p[1], - dx = bx - x, - dy = by - y; - - if (dx !== 0 || dy !== 0) { - - var t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy); - - if (t > 1) { - x = bx; - y = by; - - } else if (t > 0) { - x += dx * t; - y += dy * t; - } - } - - dx = px - x; - dy = py - y; - - return dx * dx + dy * dy; -} - -},{}],1028:[function(require,module,exports){ -'use strict'; - -module.exports = createTile; - -function createTile(features, z2, tx, ty, tolerance, noSimplify) { - var tile = { - features: [], - numPoints: 0, - numSimplified: 0, - numFeatures: 0, - source: null, - x: tx, - y: ty, - z2: z2, - transformed: false, - min: [2, 1], - max: [-1, 0] - }; - for (var i = 0; i < features.length; i++) { - tile.numFeatures++; - addFeature(tile, features[i], tolerance, noSimplify); - - var min = features[i].min, - max = features[i].max; - - if (min[0] < tile.min[0]) tile.min[0] = min[0]; - if (min[1] < tile.min[1]) tile.min[1] = min[1]; - if (max[0] > tile.max[0]) tile.max[0] = max[0]; - if (max[1] > tile.max[1]) tile.max[1] = max[1]; - } - return tile; -} - -function addFeature(tile, feature, tolerance, noSimplify) { - - var geom = feature.geometry, - type = feature.type, - simplified = [], - sqTolerance = tolerance * tolerance, - i, j, ring, p; - - if (type === 1) { - for (i = 0; i < geom.length; i++) { - simplified.push(geom[i]); - tile.numPoints++; - tile.numSimplified++; - } - - } else { - - // simplify and transform projected coordinates for tile geometry - for (i = 0; i < geom.length; i++) { - ring = geom[i]; - - // filter out tiny polylines & polygons - if (!noSimplify && ((type === 2 && ring.dist < tolerance) || - (type === 3 && ring.area < sqTolerance))) { - tile.numPoints += ring.length; - continue; - } - - var simplifiedRing = []; - - for (j = 0; j < ring.length; j++) { - p = ring[j]; - // keep points with importance > tolerance - if (noSimplify || p[2] > sqTolerance) { - simplifiedRing.push(p); - tile.numSimplified++; - } - tile.numPoints++; - } - - if (type === 3) rewind(simplifiedRing, ring.outer); - - simplified.push(simplifiedRing); - } - } - - if (simplified.length) { - var tileFeature = { - geometry: simplified, - type: type, - tags: feature.tags || null - }; - if (feature.id !== null) { - tileFeature.id = feature.id; - } - tile.features.push(tileFeature); - } -} - -function rewind(ring, clockwise) { - var area = signedArea(ring); - if (area < 0 === clockwise) ring.reverse(); -} - -function signedArea(ring) { - var sum = 0; - for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) { - p1 = ring[i]; - p2 = ring[j]; - sum += (p2[0] - p1[0]) * (p1[1] + p2[1]); - } - return sum; -} - -},{}],1029:[function(require,module,exports){ -'use strict'; - -exports.tile = transformTile; -exports.point = transformPoint; - -// Transforms the coordinates of each feature in the given tile from -// mercator-projected space into (extent x extent) tile space. -function transformTile(tile, extent) { - if (tile.transformed) return tile; - - var z2 = tile.z2, - tx = tile.x, - ty = tile.y, - i, j, k; - - for (i = 0; i < tile.features.length; i++) { - var feature = tile.features[i], - geom = feature.geometry, - type = feature.type; - - if (type === 1) { - for (j = 0; j < geom.length; j++) geom[j] = transformPoint(geom[j], extent, z2, tx, ty); - - } else { - for (j = 0; j < geom.length; j++) { - var ring = geom[j]; - for (k = 0; k < ring.length; k++) ring[k] = transformPoint(ring[k], extent, z2, tx, ty); - } - } - } - - tile.transformed = true; - - return tile; -} - -function transformPoint(p, extent, z2, tx, ty) { - var x = Math.round(extent * (p[0] * z2 - tx)), - y = Math.round(extent * (p[1] * z2 - ty)); - return [x, y]; -} - -},{}],1030:[function(require,module,exports){ -'use strict'; - -var clip = require('./clip'); -var createFeature = require('./feature'); - -module.exports = wrap; - -function wrap(features, buffer, intersectX) { - var merged = features, - left = clip(features, 1, -1 - buffer, buffer, 0, intersectX, -1, 2), // left world copy - right = clip(features, 1, 1 - buffer, 2 + buffer, 0, intersectX, -1, 2); // right world copy - - if (left || right) { - merged = clip(features, 1, -buffer, 1 + buffer, 0, intersectX, -1, 2) || []; // center world copy - - if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center - if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center - } - - return merged; -} - -function shiftFeatureCoords(features, offset) { - var newFeatures = []; - - for (var i = 0; i < features.length; i++) { - var feature = features[i], - type = feature.type; - - var newGeometry; - - if (type === 1) { - newGeometry = shiftCoords(feature.geometry, offset); - } else { - newGeometry = []; - for (var j = 0; j < feature.geometry.length; j++) { - newGeometry.push(shiftCoords(feature.geometry[j], offset)); - } - } - - newFeatures.push(createFeature(feature.tags, type, newGeometry, feature.id)); - } - - return newFeatures; -} - -function shiftCoords(points, offset) { - var newPoints = []; - newPoints.area = points.area; - newPoints.dist = points.dist; - - for (var i = 0; i < points.length; i++) { - newPoints.push([points[i][0] + offset, points[i][1], points[i][2]]); - } - return newPoints; -} - -},{"./clip":1023,"./feature":1025}],1031:[function(require,module,exports){ -/** - * @fileoverview gl-matrix - High performance matrix and vector operations - * @author Brandon Jones - * @author Colin MacKenzie IV - * @version 2.3.2 - */ - -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ -// END HEADER - -exports.glMatrix = require("./gl-matrix/common.js"); -exports.mat2 = require("./gl-matrix/mat2.js"); -exports.mat2d = require("./gl-matrix/mat2d.js"); -exports.mat3 = require("./gl-matrix/mat3.js"); -exports.mat4 = require("./gl-matrix/mat4.js"); -exports.quat = require("./gl-matrix/quat.js"); -exports.vec2 = require("./gl-matrix/vec2.js"); -exports.vec3 = require("./gl-matrix/vec3.js"); -exports.vec4 = require("./gl-matrix/vec4.js"); -},{"./gl-matrix/common.js":1032,"./gl-matrix/mat2.js":1033,"./gl-matrix/mat2d.js":1034,"./gl-matrix/mat3.js":1035,"./gl-matrix/mat4.js":1036,"./gl-matrix/quat.js":1037,"./gl-matrix/vec2.js":1038,"./gl-matrix/vec3.js":1039,"./gl-matrix/vec4.js":1040}],1032:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -/** - * @class Common utilities - * @name glMatrix - */ -var glMatrix = {}; - -// Configuration Constants -glMatrix.EPSILON = 0.000001; -glMatrix.ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array; -glMatrix.RANDOM = Math.random; -glMatrix.ENABLE_SIMD = false; - -// Capability detection -glMatrix.SIMD_AVAILABLE = (glMatrix.ARRAY_TYPE === Float32Array) && ('SIMD' in this); -glMatrix.USE_SIMD = glMatrix.ENABLE_SIMD && glMatrix.SIMD_AVAILABLE; - -/** - * Sets the type of array used when creating new vectors and matrices - * - * @param {Type} type Array type, such as Float32Array or Array - */ -glMatrix.setMatrixArrayType = function(type) { - glMatrix.ARRAY_TYPE = type; -} - -var degree = Math.PI / 180; - -/** -* Convert Degree To Radian -* -* @param {Number} Angle in Degrees -*/ -glMatrix.toRadian = function(a){ - return a * degree; -} - -/** - * Tests whether or not the arguments have approximately the same value, within an absolute - * or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less - * than or equal to 1.0, and a relative tolerance is used for larger values) - * - * @param {Number} a The first number to test. - * @param {Number} b The second number to test. - * @returns {Boolean} True if the numbers are approximately equal, false otherwise. - */ -glMatrix.equals = function(a, b) { - return Math.abs(a - b) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a), Math.abs(b)); -} - -module.exports = glMatrix; - -},{}],1033:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); - -/** - * @class 2x2 Matrix - * @name mat2 - */ -var mat2 = {}; - -/** - * Creates a new identity mat2 - * - * @returns {mat2} a new 2x2 matrix - */ -mat2.create = function() { - var out = new glMatrix.ARRAY_TYPE(4); - out[0] = 1; - out[1] = 0; - out[2] = 0; - out[3] = 1; - return out; -}; - -/** - * Creates a new mat2 initialized with values from an existing matrix - * - * @param {mat2} a matrix to clone - * @returns {mat2} a new 2x2 matrix - */ -mat2.clone = function(a) { - var out = new glMatrix.ARRAY_TYPE(4); - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - return out; -}; - -/** - * Copy the values from one mat2 to another - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the source matrix - * @returns {mat2} out - */ -mat2.copy = function(out, a) { - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - return out; -}; - -/** - * Set a mat2 to the identity matrix - * - * @param {mat2} out the receiving matrix - * @returns {mat2} out - */ -mat2.identity = function(out) { - out[0] = 1; - out[1] = 0; - out[2] = 0; - out[3] = 1; - return out; -}; - -/** - * Create a new mat2 with the given values - * - * @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} m10 Component in column 1, row 0 position (index 2) - * @param {Number} m11 Component in column 1, row 1 position (index 3) - * @returns {mat2} out A new 2x2 matrix - */ -mat2.fromValues = function(m00, m01, m10, m11) { - var out = new glMatrix.ARRAY_TYPE(4); - out[0] = m00; - out[1] = m01; - out[2] = m10; - out[3] = m11; - return out; -}; - -/** - * Set the components of a mat2 to the given values - * - * @param {mat2} 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} m10 Component in column 1, row 0 position (index 2) - * @param {Number} m11 Component in column 1, row 1 position (index 3) - * @returns {mat2} out - */ -mat2.set = function(out, m00, m01, m10, m11) { - out[0] = m00; - out[1] = m01; - out[2] = m10; - out[3] = m11; - return out; -}; - - -/** - * Transpose the values of a mat2 - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the source matrix - * @returns {mat2} out - */ -mat2.transpose = function(out, a) { - // If we are transposing ourselves we can skip a few steps but have to cache some values - if (out === a) { - var a1 = a[1]; - out[1] = a[2]; - out[2] = a1; - } else { - out[0] = a[0]; - out[1] = a[2]; - out[2] = a[1]; - out[3] = a[3]; - } - - return out; -}; - -/** - * Inverts a mat2 - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the source matrix - * @returns {mat2} out - */ -mat2.invert = function(out, a) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], - - // Calculate the determinant - det = a0 * a3 - a2 * a1; - - if (!det) { - return null; - } - det = 1.0 / det; - - out[0] = a3 * det; - out[1] = -a1 * det; - out[2] = -a2 * det; - out[3] = a0 * det; - - return out; -}; - -/** - * Calculates the adjugate of a mat2 - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the source matrix - * @returns {mat2} out - */ -mat2.adjoint = function(out, a) { - // Caching this value is nessecary if out == a - var a0 = a[0]; - out[0] = a[3]; - out[1] = -a[1]; - out[2] = -a[2]; - out[3] = a0; - - return out; -}; - -/** - * Calculates the determinant of a mat2 - * - * @param {mat2} a the source matrix - * @returns {Number} determinant of a - */ -mat2.determinant = function (a) { - return a[0] * a[3] - a[2] * a[1]; -}; - -/** - * Multiplies two mat2's - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the first operand - * @param {mat2} b the second operand - * @returns {mat2} out - */ -mat2.multiply = function (out, a, b) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; - var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; - out[0] = a0 * b0 + a2 * b1; - out[1] = a1 * b0 + a3 * b1; - out[2] = a0 * b2 + a2 * b3; - out[3] = a1 * b2 + a3 * b3; - return out; -}; - -/** - * Alias for {@link mat2.multiply} - * @function - */ -mat2.mul = mat2.multiply; - -/** - * Rotates a mat2 by the given angle - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat2} out - */ -mat2.rotate = function (out, a, rad) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], - s = Math.sin(rad), - c = Math.cos(rad); - out[0] = a0 * c + a2 * s; - out[1] = a1 * c + a3 * s; - out[2] = a0 * -s + a2 * c; - out[3] = a1 * -s + a3 * c; - return out; -}; - -/** - * Scales the mat2 by the dimensions in the given vec2 - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the matrix to rotate - * @param {vec2} v the vec2 to scale the matrix by - * @returns {mat2} out - **/ -mat2.scale = function(out, a, v) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], - v0 = v[0], v1 = v[1]; - out[0] = a0 * v0; - out[1] = a1 * v0; - out[2] = a2 * v1; - out[3] = a3 * v1; - return out; -}; - -/** - * Creates a matrix from a given angle - * This is equivalent to (but much faster than): - * - * mat2.identity(dest); - * mat2.rotate(dest, dest, rad); - * - * @param {mat2} out mat2 receiving operation result - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat2} out - */ -mat2.fromRotation = function(out, rad) { - var s = Math.sin(rad), - c = Math.cos(rad); - out[0] = c; - out[1] = s; - out[2] = -s; - out[3] = c; - return out; -} - -/** - * Creates a matrix from a vector scaling - * This is equivalent to (but much faster than): - * - * mat2.identity(dest); - * mat2.scale(dest, dest, vec); - * - * @param {mat2} out mat2 receiving operation result - * @param {vec2} v Scaling vector - * @returns {mat2} out - */ -mat2.fromScaling = function(out, v) { - out[0] = v[0]; - out[1] = 0; - out[2] = 0; - out[3] = v[1]; - return out; -} - -/** - * Returns a string representation of a mat2 - * - * @param {mat2} mat matrix to represent as a string - * @returns {String} string representation of the matrix - */ -mat2.str = function (a) { - return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; -}; - -/** - * Returns Frobenius norm of a mat2 - * - * @param {mat2} a the matrix to calculate Frobenius norm of - * @returns {Number} Frobenius norm - */ -mat2.frob = function (a) { - return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2))) -}; - -/** - * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix - * @param {mat2} L the lower triangular matrix - * @param {mat2} D the diagonal matrix - * @param {mat2} U the upper triangular matrix - * @param {mat2} a the input matrix to factorize - */ - -mat2.LDU = function (L, D, U, a) { - L[2] = a[2]/a[0]; - U[0] = a[0]; - U[1] = a[1]; - U[3] = a[3] - L[2] * U[1]; - return [L, D, U]; -}; - -/** - * Adds two mat2's - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the first operand - * @param {mat2} b the second operand - * @returns {mat2} out - */ -mat2.add = function(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]; - return out; -}; - -/** - * Subtracts matrix b from matrix a - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the first operand - * @param {mat2} b the second operand - * @returns {mat2} out - */ -mat2.subtract = function(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]; - return out; -}; - -/** - * Alias for {@link mat2.subtract} - * @function - */ -mat2.sub = mat2.subtract; - -/** - * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) - * - * @param {mat2} a The first matrix. - * @param {mat2} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat2.exactEquals = function (a, b) { - return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; -}; - -/** - * Returns whether or not the matrices have approximately the same elements in the same position. - * - * @param {mat2} a The first matrix. - * @param {mat2} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat2.equals = function (a, b) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; - var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; - return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && - Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && - Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3))); -}; - -/** - * Multiply each element of the matrix by a scalar. - * - * @param {mat2} out the receiving matrix - * @param {mat2} a the matrix to scale - * @param {Number} b amount to scale the matrix's elements by - * @returns {mat2} out - */ -mat2.multiplyScalar = function(out, a, b) { - out[0] = a[0] * b; - out[1] = a[1] * b; - out[2] = a[2] * b; - out[3] = a[3] * b; - return out; -}; - -/** - * Adds two mat2's after multiplying each element of the second operand by a scalar value. - * - * @param {mat2} out the receiving vector - * @param {mat2} a the first operand - * @param {mat2} b the second operand - * @param {Number} scale the amount to scale b's elements by before adding - * @returns {mat2} out - */ -mat2.multiplyScalarAndAdd = function(out, a, b, scale) { - out[0] = a[0] + (b[0] * scale); - out[1] = a[1] + (b[1] * scale); - out[2] = a[2] + (b[2] * scale); - out[3] = a[3] + (b[3] * scale); - return out; -}; - -module.exports = mat2; - -},{"./common.js":1032}],1034:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); - -/** - * @class 2x3 Matrix - * @name mat2d - * - * @description - * A mat2d contains six elements defined as: - *

- * [a, c, tx,
- *  b, d, ty]
- * 
- * This is a short form for the 3x3 matrix: - *
- * [a, c, tx,
- *  b, d, ty,
- *  0, 0, 1]
- * 
- * The last row is ignored so the array is shorter and operations are faster. - */ -var mat2d = {}; - -/** - * Creates a new identity mat2d - * - * @returns {mat2d} a new 2x3 matrix - */ -mat2d.create = function() { - var out = new glMatrix.ARRAY_TYPE(6); - out[0] = 1; - out[1] = 0; - out[2] = 0; - out[3] = 1; - out[4] = 0; - out[5] = 0; - return out; -}; - -/** - * Creates a new mat2d initialized with values from an existing matrix - * - * @param {mat2d} a matrix to clone - * @returns {mat2d} a new 2x3 matrix - */ -mat2d.clone = function(a) { - var out = new glMatrix.ARRAY_TYPE(6); - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - out[4] = a[4]; - out[5] = a[5]; - return out; -}; - -/** - * Copy the values from one mat2d to another - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the source matrix - * @returns {mat2d} out - */ -mat2d.copy = function(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]; - return out; -}; - -/** - * Set a mat2d to the identity matrix - * - * @param {mat2d} out the receiving matrix - * @returns {mat2d} out - */ -mat2d.identity = function(out) { - out[0] = 1; - out[1] = 0; - out[2] = 0; - out[3] = 1; - out[4] = 0; - out[5] = 0; - return out; -}; - -/** - * Create a new mat2d with the given values - * - * @param {Number} a Component A (index 0) - * @param {Number} b Component B (index 1) - * @param {Number} c Component C (index 2) - * @param {Number} d Component D (index 3) - * @param {Number} tx Component TX (index 4) - * @param {Number} ty Component TY (index 5) - * @returns {mat2d} A new mat2d - */ -mat2d.fromValues = function(a, b, c, d, tx, ty) { - var out = new glMatrix.ARRAY_TYPE(6); - out[0] = a; - out[1] = b; - out[2] = c; - out[3] = d; - out[4] = tx; - out[5] = ty; - return out; -}; - -/** - * Set the components of a mat2d to the given values - * - * @param {mat2d} out the receiving matrix - * @param {Number} a Component A (index 0) - * @param {Number} b Component B (index 1) - * @param {Number} c Component C (index 2) - * @param {Number} d Component D (index 3) - * @param {Number} tx Component TX (index 4) - * @param {Number} ty Component TY (index 5) - * @returns {mat2d} out - */ -mat2d.set = function(out, a, b, c, d, tx, ty) { - out[0] = a; - out[1] = b; - out[2] = c; - out[3] = d; - out[4] = tx; - out[5] = ty; - return out; -}; - -/** - * Inverts a mat2d - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the source matrix - * @returns {mat2d} out - */ -mat2d.invert = function(out, a) { - var aa = a[0], ab = a[1], ac = a[2], ad = a[3], - atx = a[4], aty = a[5]; - - var det = aa * ad - ab * ac; - if(!det){ - return null; - } - det = 1.0 / det; - - out[0] = ad * det; - out[1] = -ab * det; - out[2] = -ac * det; - out[3] = aa * det; - out[4] = (ac * aty - ad * atx) * det; - out[5] = (ab * atx - aa * aty) * det; - return out; -}; - -/** - * Calculates the determinant of a mat2d - * - * @param {mat2d} a the source matrix - * @returns {Number} determinant of a - */ -mat2d.determinant = function (a) { - return a[0] * a[3] - a[1] * a[2]; -}; - -/** - * Multiplies two mat2d's - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the first operand - * @param {mat2d} b the second operand - * @returns {mat2d} out - */ -mat2d.multiply = function (out, a, b) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], - b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; - out[0] = a0 * b0 + a2 * b1; - out[1] = a1 * b0 + a3 * b1; - out[2] = a0 * b2 + a2 * b3; - out[3] = a1 * b2 + a3 * b3; - out[4] = a0 * b4 + a2 * b5 + a4; - out[5] = a1 * b4 + a3 * b5 + a5; - return out; -}; - -/** - * Alias for {@link mat2d.multiply} - * @function - */ -mat2d.mul = mat2d.multiply; - -/** - * Rotates a mat2d by the given angle - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat2d} out - */ -mat2d.rotate = function (out, a, rad) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], - s = Math.sin(rad), - c = Math.cos(rad); - out[0] = a0 * c + a2 * s; - out[1] = a1 * c + a3 * s; - out[2] = a0 * -s + a2 * c; - out[3] = a1 * -s + a3 * c; - out[4] = a4; - out[5] = a5; - return out; -}; - -/** - * Scales the mat2d by the dimensions in the given vec2 - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the matrix to translate - * @param {vec2} v the vec2 to scale the matrix by - * @returns {mat2d} out - **/ -mat2d.scale = function(out, a, v) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], - v0 = v[0], v1 = v[1]; - out[0] = a0 * v0; - out[1] = a1 * v0; - out[2] = a2 * v1; - out[3] = a3 * v1; - out[4] = a4; - out[5] = a5; - return out; -}; - -/** - * Translates the mat2d by the dimensions in the given vec2 - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the matrix to translate - * @param {vec2} v the vec2 to translate the matrix by - * @returns {mat2d} out - **/ -mat2d.translate = function(out, a, v) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], - v0 = v[0], v1 = v[1]; - out[0] = a0; - out[1] = a1; - out[2] = a2; - out[3] = a3; - out[4] = a0 * v0 + a2 * v1 + a4; - out[5] = a1 * v0 + a3 * v1 + a5; - return out; -}; - -/** - * Creates a matrix from a given angle - * This is equivalent to (but much faster than): - * - * mat2d.identity(dest); - * mat2d.rotate(dest, dest, rad); - * - * @param {mat2d} out mat2d receiving operation result - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat2d} out - */ -mat2d.fromRotation = function(out, rad) { - var s = Math.sin(rad), c = Math.cos(rad); - out[0] = c; - out[1] = s; - out[2] = -s; - out[3] = c; - out[4] = 0; - out[5] = 0; - return out; -} - -/** - * Creates a matrix from a vector scaling - * This is equivalent to (but much faster than): - * - * mat2d.identity(dest); - * mat2d.scale(dest, dest, vec); - * - * @param {mat2d} out mat2d receiving operation result - * @param {vec2} v Scaling vector - * @returns {mat2d} out - */ -mat2d.fromScaling = function(out, v) { - out[0] = v[0]; - out[1] = 0; - out[2] = 0; - out[3] = v[1]; - out[4] = 0; - out[5] = 0; - return out; -} - -/** - * Creates a matrix from a vector translation - * This is equivalent to (but much faster than): - * - * mat2d.identity(dest); - * mat2d.translate(dest, dest, vec); - * - * @param {mat2d} out mat2d receiving operation result - * @param {vec2} v Translation vector - * @returns {mat2d} out - */ -mat2d.fromTranslation = function(out, v) { - out[0] = 1; - out[1] = 0; - out[2] = 0; - out[3] = 1; - out[4] = v[0]; - out[5] = v[1]; - return out; -} - -/** - * Returns a string representation of a mat2d - * - * @param {mat2d} a matrix to represent as a string - * @returns {String} string representation of the matrix - */ -mat2d.str = function (a) { - return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + - a[3] + ', ' + a[4] + ', ' + a[5] + ')'; -}; - -/** - * Returns Frobenius norm of a mat2d - * - * @param {mat2d} a the matrix to calculate Frobenius norm of - * @returns {Number} Frobenius norm - */ -mat2d.frob = function (a) { - return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + 1)) -}; - -/** - * Adds two mat2d's - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the first operand - * @param {mat2d} b the second operand - * @returns {mat2d} out - */ -mat2d.add = function(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]; - return out; -}; - -/** - * Subtracts matrix b from matrix a - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the first operand - * @param {mat2d} b the second operand - * @returns {mat2d} out - */ -mat2d.subtract = function(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]; - return out; -}; - -/** - * Alias for {@link mat2d.subtract} - * @function - */ -mat2d.sub = mat2d.subtract; - -/** - * Multiply each element of the matrix by a scalar. - * - * @param {mat2d} out the receiving matrix - * @param {mat2d} a the matrix to scale - * @param {Number} b amount to scale the matrix's elements by - * @returns {mat2d} out - */ -mat2d.multiplyScalar = function(out, a, b) { - out[0] = a[0] * b; - out[1] = a[1] * b; - out[2] = a[2] * b; - out[3] = a[3] * b; - out[4] = a[4] * b; - out[5] = a[5] * b; - return out; -}; - -/** - * Adds two mat2d's after multiplying each element of the second operand by a scalar value. - * - * @param {mat2d} out the receiving vector - * @param {mat2d} a the first operand - * @param {mat2d} b the second operand - * @param {Number} scale the amount to scale b's elements by before adding - * @returns {mat2d} out - */ -mat2d.multiplyScalarAndAdd = function(out, a, b, scale) { - out[0] = a[0] + (b[0] * scale); - out[1] = a[1] + (b[1] * scale); - out[2] = a[2] + (b[2] * scale); - out[3] = a[3] + (b[3] * scale); - out[4] = a[4] + (b[4] * scale); - out[5] = a[5] + (b[5] * scale); - return out; -}; - -/** - * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) - * - * @param {mat2d} a The first matrix. - * @param {mat2d} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat2d.exactEquals = function (a, b) { - return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5]; -}; - -/** - * Returns whether or not the matrices have approximately the same elements in the same position. - * - * @param {mat2d} a The first matrix. - * @param {mat2d} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat2d.equals = function (a, b) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; - var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; - return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && - Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && - Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && - Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && - Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5))); -}; - -module.exports = mat2d; - -},{"./common.js":1032}],1035:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); - -/** - * @class 3x3 Matrix - * @name mat3 - */ -var mat3 = {}; - -/** - * Creates a new identity mat3 - * - * @returns {mat3} a new 3x3 matrix - */ -mat3.create = function() { - var out = new glMatrix.ARRAY_TYPE(9); - 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; -}; - -/** - * Copies the upper-left 3x3 values into the given mat3. - * - * @param {mat3} out the receiving 3x3 matrix - * @param {mat4} a the source 4x4 matrix - * @returns {mat3} out - */ -mat3.fromMat4 = function(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; -}; - -/** - * Creates a new mat3 initialized with values from an existing matrix - * - * @param {mat3} a matrix to clone - * @returns {mat3} a new 3x3 matrix - */ -mat3.clone = function(a) { - var out = new glMatrix.ARRAY_TYPE(9); - 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; -}; - -/** - * Copy the values from one mat3 to another - * - * @param {mat3} out the receiving matrix - * @param {mat3} a the source matrix - * @returns {mat3} out - */ -mat3.copy = function(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; -}; - -/** - * Create a new mat3 with the given values - * - * @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} A new mat3 - */ -mat3.fromValues = function(m00, m01, m02, m10, m11, m12, m20, m21, m22) { - var out = new glMatrix.ARRAY_TYPE(9); - 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 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 - */ -mat3.set = function(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 - */ -mat3.identity = function(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 {mat3} a the source matrix - * @returns {mat3} out - */ -mat3.transpose = function(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 {mat3} a the source matrix - * @returns {mat3} out - */ -mat3.invert = function(out, a) { - 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], - - b01 = a22 * a11 - a12 * a21, - b11 = -a22 * a10 + a12 * a20, - b21 = a21 * a10 - a11 * a20, - - // Calculate the determinant - 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 {mat3} a the source matrix - * @returns {mat3} out - */ -mat3.adjoint = function(out, a) { - 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]; - - 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 {mat3} a the source matrix - * @returns {Number} determinant of a - */ -mat3.determinant = function (a) { - 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]; - - 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 {mat3} a the first operand - * @param {mat3} b the second operand - * @returns {mat3} out - */ -mat3.multiply = function (out, a, b) { - 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], - - b00 = b[0], b01 = b[1], b02 = b[2], - b10 = b[3], b11 = b[4], b12 = b[5], - 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; -}; - -/** - * Alias for {@link mat3.multiply} - * @function - */ -mat3.mul = mat3.multiply; - -/** - * Translate a mat3 by the given vector - * - * @param {mat3} out the receiving matrix - * @param {mat3} a the matrix to translate - * @param {vec2} v vector to translate by - * @returns {mat3} out - */ -mat3.translate = function(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 {mat3} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat3} out - */ -mat3.rotate = function (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 {mat3} a the matrix to rotate - * @param {vec2} v the vec2 to scale the matrix by - * @returns {mat3} out - **/ -mat3.scale = function(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 {vec2} v Translation vector - * @returns {mat3} out - */ -mat3.fromTranslation = function(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 - */ -mat3.fromRotation = function(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 {vec2} v Scaling vector - * @returns {mat3} out - */ -mat3.fromScaling = function(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; -} - -/** - * Copies the values from a mat2d into a mat3 - * - * @param {mat3} out the receiving matrix - * @param {mat2d} a the matrix to copy - * @returns {mat3} out - **/ -mat3.fromMat2d = function(out, a) { - out[0] = a[0]; - out[1] = a[1]; - out[2] = 0; - - out[3] = a[2]; - out[4] = a[3]; - out[5] = 0; - - out[6] = a[4]; - out[7] = a[5]; - out[8] = 1; - return out; -}; - -/** -* Calculates a 3x3 matrix from the given quaternion -* -* @param {mat3} out mat3 receiving operation result -* @param {quat} q Quaternion to create matrix from -* -* @returns {mat3} out -*/ -mat3.fromQuat = function (out, q) { - var x = q[0], y = q[1], z = q[2], w = q[3], - x2 = x + x, - y2 = y + y, - z2 = z + z, - - xx = x * x2, - yx = y * x2, - yy = y * y2, - zx = z * x2, - zy = z * y2, - zz = z * z2, - wx = w * x2, - wy = w * y2, - 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 {mat4} a Mat4 to derive the normal matrix from -* -* @returns {mat3} out -*/ -mat3.normalFromMat4 = function (out, a) { - var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], - - b00 = a00 * a11 - a01 * a10, - b01 = a00 * a12 - a02 * a10, - b02 = a00 * a13 - a03 * a10, - b03 = a01 * a12 - a02 * a11, - b04 = a01 * a13 - a03 * a11, - b05 = a02 * a13 - a03 * a12, - b06 = a20 * a31 - a21 * a30, - b07 = a20 * a32 - a22 * a30, - b08 = a20 * a33 - a23 * a30, - b09 = a21 * a32 - a22 * a31, - b10 = a21 * a33 - a23 * a31, - b11 = a22 * a33 - a23 * a32, - - // Calculate the determinant - 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 a string representation of a mat3 - * - * @param {mat3} mat matrix to represent as a string - * @returns {String} string representation of the matrix - */ -mat3.str = function (a) { - return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + - a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + - a[6] + ', ' + a[7] + ', ' + a[8] + ')'; -}; - -/** - * Returns Frobenius norm of a mat3 - * - * @param {mat3} a the matrix to calculate Frobenius norm of - * @returns {Number} Frobenius norm - */ -mat3.frob = function (a) { - return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2))) -}; - -/** - * Adds two mat3's - * - * @param {mat3} out the receiving matrix - * @param {mat3} a the first operand - * @param {mat3} b the second operand - * @returns {mat3} out - */ -mat3.add = function(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 {mat3} a the first operand - * @param {mat3} b the second operand - * @returns {mat3} out - */ -mat3.subtract = function(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; -}; - -/** - * Alias for {@link mat3.subtract} - * @function - */ -mat3.sub = mat3.subtract; - -/** - * Multiply each element of the matrix by a scalar. - * - * @param {mat3} out the receiving matrix - * @param {mat3} a the matrix to scale - * @param {Number} b amount to scale the matrix's elements by - * @returns {mat3} out - */ -mat3.multiplyScalar = function(out, a, b) { - out[0] = a[0] * b; - out[1] = a[1] * b; - out[2] = a[2] * b; - out[3] = a[3] * b; - out[4] = a[4] * b; - out[5] = a[5] * b; - out[6] = a[6] * b; - out[7] = a[7] * b; - out[8] = a[8] * b; - return out; -}; - -/** - * Adds two mat3's after multiplying each element of the second operand by a scalar value. - * - * @param {mat3} out the receiving vector - * @param {mat3} a the first operand - * @param {mat3} b the second operand - * @param {Number} scale the amount to scale b's elements by before adding - * @returns {mat3} out - */ -mat3.multiplyScalarAndAdd = function(out, a, b, scale) { - out[0] = a[0] + (b[0] * scale); - out[1] = a[1] + (b[1] * scale); - out[2] = a[2] + (b[2] * scale); - out[3] = a[3] + (b[3] * scale); - out[4] = a[4] + (b[4] * scale); - out[5] = a[5] + (b[5] * scale); - out[6] = a[6] + (b[6] * scale); - out[7] = a[7] + (b[7] * scale); - out[8] = a[8] + (b[8] * scale); - return out; -}; - -/* - * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) - * - * @param {mat3} a The first matrix. - * @param {mat3} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat3.exactEquals = function (a, b) { - return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && - a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && - a[6] === b[6] && a[7] === b[7] && a[8] === b[8]; -}; - -/** - * Returns whether or not the matrices have approximately the same elements in the same position. - * - * @param {mat3} a The first matrix. - * @param {mat3} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat3.equals = function (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 = a[6], b7 = b[7], b8 = b[8]; - return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && - Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && - Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && - Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && - Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5)) && - Math.abs(a6 - b6) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a6), Math.abs(b6)) && - Math.abs(a7 - b7) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a7), Math.abs(b7)) && - Math.abs(a8 - b8) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a8), Math.abs(b8))); -}; - - -module.exports = mat3; - -},{"./common.js":1032}],1036:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); - -/** - * @class 4x4 Matrix - * @name mat4 - */ -var mat4 = { - scalar: {}, - SIMD: {}, -}; - -/** - * Creates a new identity mat4 - * - * @returns {mat4} a new 4x4 matrix - */ -mat4.create = function() { - var out = new glMatrix.ARRAY_TYPE(16); - 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; -}; - -/** - * Creates a new mat4 initialized with values from an existing matrix - * - * @param {mat4} a matrix to clone - * @returns {mat4} a new 4x4 matrix - */ -mat4.clone = function(a) { - var out = new glMatrix.ARRAY_TYPE(16); - 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; -}; - -/** - * Copy the values from one mat4 to another - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.copy = function(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; -}; - -/** - * Create a new mat4 with the given values - * - * @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} A new mat4 - */ -mat4.fromValues = function(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { - var out = new glMatrix.ARRAY_TYPE(16); - 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 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 - */ -mat4.set = function(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 - */ -mat4.identity = function(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 not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.scalar.transpose = function(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], - a12 = a[6], a13 = a[7], - 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; -}; - -/** - * Transpose the values of a mat4 using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.SIMD.transpose = function(out, a) { - var a0, a1, a2, a3, - tmp01, tmp23, - out0, out1, out2, out3; - - a0 = SIMD.Float32x4.load(a, 0); - a1 = SIMD.Float32x4.load(a, 4); - a2 = SIMD.Float32x4.load(a, 8); - a3 = SIMD.Float32x4.load(a, 12); - - tmp01 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); - tmp23 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); - out0 = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6); - out1 = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7); - SIMD.Float32x4.store(out, 0, out0); - SIMD.Float32x4.store(out, 4, out1); - - tmp01 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); - tmp23 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); - out2 = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6); - out3 = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7); - SIMD.Float32x4.store(out, 8, out2); - SIMD.Float32x4.store(out, 12, out3); - - return out; -}; - -/** - * Transpse a mat4 using SIMD if available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.transpose = glMatrix.USE_SIMD ? mat4.SIMD.transpose : mat4.scalar.transpose; - -/** - * Inverts a mat4 not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.scalar.invert = function(out, a) { - var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], - - b00 = a00 * a11 - a01 * a10, - b01 = a00 * a12 - a02 * a10, - b02 = a00 * a13 - a03 * a10, - b03 = a01 * a12 - a02 * a11, - b04 = a01 * a13 - a03 * a11, - b05 = a02 * a13 - a03 * a12, - b06 = a20 * a31 - a21 * a30, - b07 = a20 * a32 - a22 * a30, - b08 = a20 * a33 - a23 * a30, - b09 = a21 * a32 - a22 * a31, - b10 = a21 * a33 - a23 * a31, - b11 = a22 * a33 - a23 * a32, - - // Calculate the determinant - 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; -}; - -/** - * Inverts a mat4 using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.SIMD.invert = function(out, a) { - var row0, row1, row2, row3, - tmp1, - minor0, minor1, minor2, minor3, - det, - a0 = SIMD.Float32x4.load(a, 0), - a1 = SIMD.Float32x4.load(a, 4), - a2 = SIMD.Float32x4.load(a, 8), - a3 = SIMD.Float32x4.load(a, 12); - - // Compute matrix adjugate - tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); - row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); - row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6); - row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7); - tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); - row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); - row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6); - row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7); - - tmp1 = SIMD.Float32x4.mul(row2, row3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor0 = SIMD.Float32x4.mul(row1, tmp1); - minor1 = SIMD.Float32x4.mul(row0, tmp1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0); - minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1); - minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1); - - tmp1 = SIMD.Float32x4.mul(row1, row2); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0); - minor3 = SIMD.Float32x4.mul(row0, tmp1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1)); - minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3); - minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1); - - tmp1 = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - row2 = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0); - minor2 = SIMD.Float32x4.mul(row0, tmp1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1)); - minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2); - minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1); - - tmp1 = SIMD.Float32x4.mul(row0, row1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2); - minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2); - minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1)); - - tmp1 = SIMD.Float32x4.mul(row0, row3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1)); - minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1); - minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1)); - - tmp1 = SIMD.Float32x4.mul(row0, row2); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1); - minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1)); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1)); - minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3); - - // Compute matrix determinant - det = SIMD.Float32x4.mul(row0, minor0); - det = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 2, 3, 0, 1), det); - det = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 1, 0, 3, 2), det); - tmp1 = SIMD.Float32x4.reciprocalApproximation(det); - det = SIMD.Float32x4.sub( - SIMD.Float32x4.add(tmp1, tmp1), - SIMD.Float32x4.mul(det, SIMD.Float32x4.mul(tmp1, tmp1))); - det = SIMD.Float32x4.swizzle(det, 0, 0, 0, 0); - if (!det) { - return null; - } - - // Compute matrix inverse - SIMD.Float32x4.store(out, 0, SIMD.Float32x4.mul(det, minor0)); - SIMD.Float32x4.store(out, 4, SIMD.Float32x4.mul(det, minor1)); - SIMD.Float32x4.store(out, 8, SIMD.Float32x4.mul(det, minor2)); - SIMD.Float32x4.store(out, 12, SIMD.Float32x4.mul(det, minor3)); - return out; -} - -/** - * Inverts a mat4 using SIMD if available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.invert = glMatrix.USE_SIMD ? mat4.SIMD.invert : mat4.scalar.invert; - -/** - * Calculates the adjugate of a mat4 not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.scalar.adjoint = function(out, a) { - var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - 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 adjugate of a mat4 using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ -mat4.SIMD.adjoint = function(out, a) { - var a0, a1, a2, a3; - var row0, row1, row2, row3; - var tmp1; - var minor0, minor1, minor2, minor3; - - var a0 = SIMD.Float32x4.load(a, 0); - var a1 = SIMD.Float32x4.load(a, 4); - var a2 = SIMD.Float32x4.load(a, 8); - var a3 = SIMD.Float32x4.load(a, 12); - - // Transpose the source matrix. Sort of. Not a true transpose operation - tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); - row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); - row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6); - row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7); - - tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); - row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); - row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6); - row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7); - - tmp1 = SIMD.Float32x4.mul(row2, row3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor0 = SIMD.Float32x4.mul(row1, tmp1); - minor1 = SIMD.Float32x4.mul(row0, tmp1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0); - minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1); - minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1); - - tmp1 = SIMD.Float32x4.mul(row1, row2); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0); - minor3 = SIMD.Float32x4.mul(row0, tmp1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1)); - minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3); - minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1); - - tmp1 = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - row2 = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0); - minor2 = SIMD.Float32x4.mul(row0, tmp1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1)); - minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2); - minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1); - - tmp1 = SIMD.Float32x4.mul(row0, row1); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2); - minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2); - minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1)); - - tmp1 = SIMD.Float32x4.mul(row0, row3); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1)); - minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1); - minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1)); - - tmp1 = SIMD.Float32x4.mul(row0, row2); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); - minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1); - minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1)); - tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); - minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1)); - minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3); - - SIMD.Float32x4.store(out, 0, minor0); - SIMD.Float32x4.store(out, 4, minor1); - SIMD.Float32x4.store(out, 8, minor2); - SIMD.Float32x4.store(out, 12, minor3); - return out; -}; - -/** - * Calculates the adjugate of a mat4 using SIMD if available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the source matrix - * @returns {mat4} out - */ - mat4.adjoint = glMatrix.USE_SIMD ? mat4.SIMD.adjoint : mat4.scalar.adjoint; - -/** - * Calculates the determinant of a mat4 - * - * @param {mat4} a the source matrix - * @returns {Number} determinant of a - */ -mat4.determinant = function (a) { - var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], - - b00 = a00 * a11 - a01 * a10, - b01 = a00 * a12 - a02 * a10, - b02 = a00 * a13 - a03 * a10, - b03 = a01 * a12 - a02 * a11, - b04 = a01 * a13 - a03 * a11, - b05 = a02 * a13 - a03 * a12, - b06 = a20 * a31 - a21 * a30, - b07 = a20 * a32 - a22 * a30, - b08 = a20 * a33 - a23 * a30, - b09 = a21 * a32 - a22 * a31, - b10 = a21 * a33 - a23 * a31, - b11 = a22 * a33 - a23 * a32; - - // Calculate the determinant - return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; -}; - -/** - * Multiplies two mat4's explicitly using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the first operand, must be a Float32Array - * @param {mat4} b the second operand, must be a Float32Array - * @returns {mat4} out - */ -mat4.SIMD.multiply = function (out, a, b) { - var a0 = SIMD.Float32x4.load(a, 0); - var a1 = SIMD.Float32x4.load(a, 4); - var a2 = SIMD.Float32x4.load(a, 8); - var a3 = SIMD.Float32x4.load(a, 12); - - var b0 = SIMD.Float32x4.load(b, 0); - var out0 = SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 0, 0, 0, 0), a0), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 1, 1, 1, 1), a1), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 2, 2, 2, 2), a2), - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 3, 3, 3, 3), a3)))); - SIMD.Float32x4.store(out, 0, out0); - - var b1 = SIMD.Float32x4.load(b, 4); - var out1 = SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 0, 0, 0, 0), a0), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 1, 1, 1, 1), a1), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 2, 2, 2, 2), a2), - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 3, 3, 3, 3), a3)))); - SIMD.Float32x4.store(out, 4, out1); - - var b2 = SIMD.Float32x4.load(b, 8); - var out2 = SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 0, 0, 0, 0), a0), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 1, 1, 1, 1), a1), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 2, 2, 2, 2), a2), - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 3, 3, 3, 3), a3)))); - SIMD.Float32x4.store(out, 8, out2); - - var b3 = SIMD.Float32x4.load(b, 12); - var out3 = SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 0, 0, 0, 0), a0), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 1, 1, 1, 1), a1), - SIMD.Float32x4.add( - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 2, 2, 2, 2), a2), - SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 3, 3, 3, 3), a3)))); - SIMD.Float32x4.store(out, 12, out3); - - return out; -}; - -/** - * Multiplies two mat4's explicitly not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the first operand - * @param {mat4} b the second operand - * @returns {mat4} out - */ -mat4.scalar.multiply = function (out, a, b) { - var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], - a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], - a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], - a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; - - // Cache only the current line of the second matrix - var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; - out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - - b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7]; - out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - - b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11]; - out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - - b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15]; - out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33; - return out; -}; - -/** - * Multiplies two mat4's using SIMD if available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the first operand - * @param {mat4} b the second operand - * @returns {mat4} out - */ -mat4.multiply = glMatrix.USE_SIMD ? mat4.SIMD.multiply : mat4.scalar.multiply; - -/** - * Alias for {@link mat4.multiply} - * @function - */ -mat4.mul = mat4.multiply; - -/** - * Translate a mat4 by the given vector not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to translate - * @param {vec3} v vector to translate by - * @returns {mat4} out - */ -mat4.scalar.translate = function (out, a, v) { - var x = v[0], y = v[1], z = v[2], - a00, a01, a02, a03, - a10, a11, a12, a13, - a20, a21, a22, a23; - - if (a === out) { - out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; - out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; - out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; - out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; - } else { - a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; - a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; - a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; - - out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03; - out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13; - out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23; - - out[12] = a00 * x + a10 * y + a20 * z + a[12]; - out[13] = a01 * x + a11 * y + a21 * z + a[13]; - out[14] = a02 * x + a12 * y + a22 * z + a[14]; - out[15] = a03 * x + a13 * y + a23 * z + a[15]; - } - - return out; -}; - -/** - * Translates a mat4 by the given vector using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to translate - * @param {vec3} v vector to translate by - * @returns {mat4} out - */ -mat4.SIMD.translate = function (out, a, v) { - var a0 = SIMD.Float32x4.load(a, 0), - a1 = SIMD.Float32x4.load(a, 4), - a2 = SIMD.Float32x4.load(a, 8), - a3 = SIMD.Float32x4.load(a, 12), - vec = SIMD.Float32x4(v[0], v[1], v[2] , 0); - - if (a !== out) { - 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]; - } - - a0 = SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0)); - a1 = SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1)); - a2 = SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2)); - - var t0 = SIMD.Float32x4.add(a0, SIMD.Float32x4.add(a1, SIMD.Float32x4.add(a2, a3))); - SIMD.Float32x4.store(out, 12, t0); - - return out; -}; - -/** - * Translates a mat4 by the given vector using SIMD if available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to translate - * @param {vec3} v vector to translate by - * @returns {mat4} out - */ -mat4.translate = glMatrix.USE_SIMD ? mat4.SIMD.translate : mat4.scalar.translate; - -/** - * Scales the mat4 by the dimensions in the given vec3 not using vectorization - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to scale - * @param {vec3} v the vec3 to scale the matrix by - * @returns {mat4} out - **/ -mat4.scalar.scale = function(out, a, v) { - var x = v[0], y = v[1], z = v[2]; - - out[0] = a[0] * x; - out[1] = a[1] * x; - out[2] = a[2] * x; - out[3] = a[3] * x; - out[4] = a[4] * y; - out[5] = a[5] * y; - out[6] = a[6] * y; - out[7] = a[7] * y; - out[8] = a[8] * z; - out[9] = a[9] * z; - out[10] = a[10] * z; - out[11] = a[11] * z; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - return out; -}; - -/** - * Scales the mat4 by the dimensions in the given vec3 using vectorization - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to scale - * @param {vec3} v the vec3 to scale the matrix by - * @returns {mat4} out - **/ -mat4.SIMD.scale = function(out, a, v) { - var a0, a1, a2; - var vec = SIMD.Float32x4(v[0], v[1], v[2], 0); - - a0 = SIMD.Float32x4.load(a, 0); - SIMD.Float32x4.store( - out, 0, SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0))); - - a1 = SIMD.Float32x4.load(a, 4); - SIMD.Float32x4.store( - out, 4, SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1))); - - a2 = SIMD.Float32x4.load(a, 8); - SIMD.Float32x4.store( - out, 8, SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2))); - - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - return out; -}; - -/** - * Scales the mat4 by the dimensions in the given vec3 using SIMD if available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to scale - * @param {vec3} v the vec3 to scale the matrix by - * @returns {mat4} out - */ -mat4.scale = glMatrix.USE_SIMD ? mat4.SIMD.scale : mat4.scalar.scale; - -/** - * Rotates a mat4 by the given angle around the given axis - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @param {vec3} axis the axis to rotate around - * @returns {mat4} out - */ -mat4.rotate = function (out, a, rad, axis) { - var x = axis[0], y = axis[1], z = axis[2], - len = Math.sqrt(x * x + y * y + z * z), - s, c, t, - a00, a01, a02, a03, - a10, a11, a12, a13, - a20, a21, a22, a23, - b00, b01, b02, - b10, b11, b12, - b20, b21, b22; - - if (Math.abs(len) < glMatrix.EPSILON) { return null; } - - len = 1 / len; - x *= len; - y *= len; - z *= len; - - s = Math.sin(rad); - c = Math.cos(rad); - t = 1 - c; - - a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; - a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; - a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; - - // Construct the elements of the rotation matrix - b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s; - b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s; - b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c; - - // Perform rotation-specific matrix multiplication - out[0] = a00 * b00 + a10 * b01 + a20 * b02; - out[1] = a01 * b00 + a11 * b01 + a21 * b02; - out[2] = a02 * b00 + a12 * b01 + a22 * b02; - out[3] = a03 * b00 + a13 * b01 + a23 * b02; - out[4] = a00 * b10 + a10 * b11 + a20 * b12; - out[5] = a01 * b10 + a11 * b11 + a21 * b12; - out[6] = a02 * b10 + a12 * b11 + a22 * b12; - out[7] = a03 * b10 + a13 * b11 + a23 * b12; - out[8] = a00 * b20 + a10 * b21 + a20 * b22; - out[9] = a01 * b20 + a11 * b21 + a21 * b22; - out[10] = a02 * b20 + a12 * b21 + a22 * b22; - out[11] = a03 * b20 + a13 * b21 + a23 * b22; - - if (a !== out) { // If the source and destination differ, copy the unchanged last row - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - return out; -}; - -/** - * Rotates a matrix by the given angle around the X axis not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.scalar.rotateX = function (out, a, rad) { - var s = Math.sin(rad), - c = Math.cos(rad), - a10 = a[4], - a11 = a[5], - a12 = a[6], - a13 = a[7], - a20 = a[8], - a21 = a[9], - a22 = a[10], - a23 = a[11]; - - if (a !== out) { // If the source and destination differ, copy the unchanged rows - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - - // Perform axis-specific matrix multiplication - out[4] = a10 * c + a20 * s; - out[5] = a11 * c + a21 * s; - out[6] = a12 * c + a22 * s; - out[7] = a13 * c + a23 * s; - out[8] = a20 * c - a10 * s; - out[9] = a21 * c - a11 * s; - out[10] = a22 * c - a12 * s; - out[11] = a23 * c - a13 * s; - return out; -}; - -/** - * Rotates a matrix by the given angle around the X axis using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.SIMD.rotateX = function (out, a, rad) { - var s = SIMD.Float32x4.splat(Math.sin(rad)), - c = SIMD.Float32x4.splat(Math.cos(rad)); - - if (a !== out) { // If the source and destination differ, copy the unchanged rows - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - - // Perform axis-specific matrix multiplication - var a_1 = SIMD.Float32x4.load(a, 4); - var a_2 = SIMD.Float32x4.load(a, 8); - SIMD.Float32x4.store(out, 4, - SIMD.Float32x4.add(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_2, s))); - SIMD.Float32x4.store(out, 8, - SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_2, c), SIMD.Float32x4.mul(a_1, s))); - return out; -}; - -/** - * Rotates a matrix by the given angle around the X axis using SIMD if availabe and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.rotateX = glMatrix.USE_SIMD ? mat4.SIMD.rotateX : mat4.scalar.rotateX; - -/** - * Rotates a matrix by the given angle around the Y axis not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.scalar.rotateY = function (out, a, rad) { - var s = Math.sin(rad), - c = Math.cos(rad), - a00 = a[0], - a01 = a[1], - a02 = a[2], - a03 = a[3], - a20 = a[8], - a21 = a[9], - a22 = a[10], - a23 = a[11]; - - if (a !== out) { // If the source and destination differ, copy the unchanged rows - out[4] = a[4]; - out[5] = a[5]; - out[6] = a[6]; - out[7] = a[7]; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - - // Perform axis-specific matrix multiplication - out[0] = a00 * c - a20 * s; - out[1] = a01 * c - a21 * s; - out[2] = a02 * c - a22 * s; - out[3] = a03 * c - a23 * s; - out[8] = a00 * s + a20 * c; - out[9] = a01 * s + a21 * c; - out[10] = a02 * s + a22 * c; - out[11] = a03 * s + a23 * c; - return out; -}; - -/** - * Rotates a matrix by the given angle around the Y axis using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.SIMD.rotateY = function (out, a, rad) { - var s = SIMD.Float32x4.splat(Math.sin(rad)), - c = SIMD.Float32x4.splat(Math.cos(rad)); - - if (a !== out) { // If the source and destination differ, copy the unchanged rows - out[4] = a[4]; - out[5] = a[5]; - out[6] = a[6]; - out[7] = a[7]; - out[12] = a[12]; - out[13] = a[13]; - out[14] = a[14]; - out[15] = a[15]; - } - - // Perform axis-specific matrix multiplication - var a_0 = SIMD.Float32x4.load(a, 0); - var a_2 = SIMD.Float32x4.load(a, 8); - SIMD.Float32x4.store(out, 0, - SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_2, s))); - SIMD.Float32x4.store(out, 8, - SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, s), SIMD.Float32x4.mul(a_2, c))); - return out; -}; - -/** - * Rotates a matrix by the given angle around the Y axis if SIMD available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ - mat4.rotateY = glMatrix.USE_SIMD ? mat4.SIMD.rotateY : mat4.scalar.rotateY; - -/** - * Rotates a matrix by the given angle around the Z axis not using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.scalar.rotateZ = function (out, a, rad) { - var s = Math.sin(rad), - c = Math.cos(rad), - a00 = a[0], - a01 = a[1], - a02 = a[2], - a03 = a[3], - a10 = a[4], - a11 = a[5], - a12 = a[6], - a13 = a[7]; - - if (a !== out) { // If the source and destination differ, copy the unchanged last row - 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]; - } - - // Perform axis-specific matrix multiplication - out[0] = a00 * c + a10 * s; - out[1] = a01 * c + a11 * s; - out[2] = a02 * c + a12 * s; - out[3] = a03 * c + a13 * s; - out[4] = a10 * c - a00 * s; - out[5] = a11 * c - a01 * s; - out[6] = a12 * c - a02 * s; - out[7] = a13 * c - a03 * s; - return out; -}; - -/** - * Rotates a matrix by the given angle around the Z axis using SIMD - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.SIMD.rotateZ = function (out, a, rad) { - var s = SIMD.Float32x4.splat(Math.sin(rad)), - c = SIMD.Float32x4.splat(Math.cos(rad)); - - if (a !== out) { // If the source and destination differ, copy the unchanged last row - 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]; - } - - // Perform axis-specific matrix multiplication - var a_0 = SIMD.Float32x4.load(a, 0); - var a_1 = SIMD.Float32x4.load(a, 4); - SIMD.Float32x4.store(out, 0, - SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_1, s))); - SIMD.Float32x4.store(out, 4, - SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_0, s))); - return out; -}; - -/** - * Rotates a matrix by the given angle around the Z axis if SIMD available and enabled - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to rotate - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ - mat4.rotateZ = glMatrix.USE_SIMD ? mat4.SIMD.rotateZ : mat4.scalar.rotateZ; - -/** - * Creates a matrix from a vector translation - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.translate(dest, dest, vec); - * - * @param {mat4} out mat4 receiving operation result - * @param {vec3} v Translation vector - * @returns {mat4} out - */ -mat4.fromTranslation = function(out, v) { - 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] = v[0]; - out[13] = v[1]; - out[14] = v[2]; - out[15] = 1; - return out; -} - -/** - * Creates a matrix from a vector scaling - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.scale(dest, dest, vec); - * - * @param {mat4} out mat4 receiving operation result - * @param {vec3} v Scaling vector - * @returns {mat4} out - */ -mat4.fromScaling = function(out, v) { - out[0] = v[0]; - out[1] = 0; - out[2] = 0; - out[3] = 0; - out[4] = 0; - out[5] = v[1]; - out[6] = 0; - out[7] = 0; - out[8] = 0; - out[9] = 0; - out[10] = v[2]; - out[11] = 0; - out[12] = 0; - out[13] = 0; - out[14] = 0; - out[15] = 1; - return out; -} - -/** - * Creates a matrix from a given angle around a given axis - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.rotate(dest, dest, rad, axis); - * - * @param {mat4} out mat4 receiving operation result - * @param {Number} rad the angle to rotate the matrix by - * @param {vec3} axis the axis to rotate around - * @returns {mat4} out - */ -mat4.fromRotation = function(out, rad, axis) { - var x = axis[0], y = axis[1], z = axis[2], - len = Math.sqrt(x * x + y * y + z * z), - s, c, t; - - if (Math.abs(len) < glMatrix.EPSILON) { return null; } - - len = 1 / len; - x *= len; - y *= len; - z *= len; - - s = Math.sin(rad); - c = Math.cos(rad); - t = 1 - c; - - // Perform rotation-specific matrix multiplication - out[0] = x * x * t + c; - out[1] = y * x * t + z * s; - out[2] = z * x * t - y * s; - out[3] = 0; - out[4] = x * y * t - z * s; - out[5] = y * y * t + c; - out[6] = z * y * t + x * s; - out[7] = 0; - out[8] = x * z * t + y * s; - out[9] = y * z * t - x * s; - out[10] = z * z * t + c; - out[11] = 0; - out[12] = 0; - out[13] = 0; - out[14] = 0; - out[15] = 1; - return out; -} - -/** - * Creates a matrix from the given angle around the X axis - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.rotateX(dest, dest, rad); - * - * @param {mat4} out mat4 receiving operation result - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.fromXRotation = function(out, rad) { - var s = Math.sin(rad), - c = Math.cos(rad); - - // Perform axis-specific matrix multiplication - out[0] = 1; - out[1] = 0; - out[2] = 0; - out[3] = 0; - out[4] = 0; - out[5] = c; - out[6] = s; - out[7] = 0; - out[8] = 0; - out[9] = -s; - out[10] = c; - out[11] = 0; - out[12] = 0; - out[13] = 0; - out[14] = 0; - out[15] = 1; - return out; -} - -/** - * Creates a matrix from the given angle around the Y axis - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.rotateY(dest, dest, rad); - * - * @param {mat4} out mat4 receiving operation result - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.fromYRotation = function(out, rad) { - var s = Math.sin(rad), - c = Math.cos(rad); - - // Perform axis-specific matrix multiplication - out[0] = c; - out[1] = 0; - out[2] = -s; - out[3] = 0; - out[4] = 0; - out[5] = 1; - out[6] = 0; - out[7] = 0; - out[8] = s; - out[9] = 0; - out[10] = c; - out[11] = 0; - out[12] = 0; - out[13] = 0; - out[14] = 0; - out[15] = 1; - return out; -} - -/** - * Creates a matrix from the given angle around the Z axis - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.rotateZ(dest, dest, rad); - * - * @param {mat4} out mat4 receiving operation result - * @param {Number} rad the angle to rotate the matrix by - * @returns {mat4} out - */ -mat4.fromZRotation = function(out, rad) { - var s = Math.sin(rad), - c = Math.cos(rad); - - // Perform axis-specific matrix multiplication - out[0] = c; - out[1] = s; - out[2] = 0; - out[3] = 0; - out[4] = -s; - out[5] = c; - 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; -} - -/** - * Creates a matrix from a quaternion rotation and vector translation - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.translate(dest, vec); - * var quatMat = mat4.create(); - * quat4.toMat4(quat, quatMat); - * mat4.multiply(dest, quatMat); - * - * @param {mat4} out mat4 receiving operation result - * @param {quat4} q Rotation quaternion - * @param {vec3} v Translation vector - * @returns {mat4} out - */ -mat4.fromRotationTranslation = function (out, q, v) { - // Quaternion math - var x = q[0], y = q[1], z = q[2], w = q[3], - x2 = x + x, - y2 = y + y, - z2 = z + z, - - xx = x * x2, - xy = x * y2, - xz = x * z2, - yy = y * y2, - yz = y * z2, - zz = z * z2, - wx = w * x2, - wy = w * y2, - wz = w * z2; - - out[0] = 1 - (yy + zz); - out[1] = xy + wz; - out[2] = xz - wy; - out[3] = 0; - out[4] = xy - wz; - out[5] = 1 - (xx + zz); - out[6] = yz + wx; - out[7] = 0; - out[8] = xz + wy; - out[9] = yz - wx; - out[10] = 1 - (xx + yy); - out[11] = 0; - out[12] = v[0]; - out[13] = v[1]; - out[14] = v[2]; - out[15] = 1; - - return out; -}; - -/** - * Returns the translation vector component of a transformation - * matrix. If a matrix is built with fromRotationTranslation, - * the returned vector will be the same as the translation vector - * originally supplied. - * @param {vec3} out Vector to receive translation component - * @param {mat4} mat Matrix to be decomposed (input) - * @return {vec3} out - */ -mat4.getTranslation = function (out, mat) { - out[0] = mat[12]; - out[1] = mat[13]; - out[2] = mat[14]; - - return out; -}; - -/** - * Returns a quaternion representing the rotational component - * of a transformation matrix. If a matrix is built with - * fromRotationTranslation, the returned quaternion will be the - * same as the quaternion originally supplied. - * @param {quat} out Quaternion to receive the rotation component - * @param {mat4} mat Matrix to be decomposed (input) - * @return {quat} out - */ -mat4.getRotation = function (out, mat) { - // Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm - var trace = mat[0] + mat[5] + mat[10]; - var S = 0; - - if (trace > 0) { - S = Math.sqrt(trace + 1.0) * 2; - out[3] = 0.25 * S; - out[0] = (mat[6] - mat[9]) / S; - out[1] = (mat[8] - mat[2]) / S; - out[2] = (mat[1] - mat[4]) / S; - } else if ((mat[0] > mat[5])&(mat[0] > mat[10])) { - S = Math.sqrt(1.0 + mat[0] - mat[5] - mat[10]) * 2; - out[3] = (mat[6] - mat[9]) / S; - out[0] = 0.25 * S; - out[1] = (mat[1] + mat[4]) / S; - out[2] = (mat[8] + mat[2]) / S; - } else if (mat[5] > mat[10]) { - S = Math.sqrt(1.0 + mat[5] - mat[0] - mat[10]) * 2; - out[3] = (mat[8] - mat[2]) / S; - out[0] = (mat[1] + mat[4]) / S; - out[1] = 0.25 * S; - out[2] = (mat[6] + mat[9]) / S; - } else { - S = Math.sqrt(1.0 + mat[10] - mat[0] - mat[5]) * 2; - out[3] = (mat[1] - mat[4]) / S; - out[0] = (mat[8] + mat[2]) / S; - out[1] = (mat[6] + mat[9]) / S; - out[2] = 0.25 * S; - } - - return out; -}; - -/** - * Creates a matrix from a quaternion rotation, vector translation and vector scale - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.translate(dest, vec); - * var quatMat = mat4.create(); - * quat4.toMat4(quat, quatMat); - * mat4.multiply(dest, quatMat); - * mat4.scale(dest, scale) - * - * @param {mat4} out mat4 receiving operation result - * @param {quat4} q Rotation quaternion - * @param {vec3} v Translation vector - * @param {vec3} s Scaling vector - * @returns {mat4} out - */ -mat4.fromRotationTranslationScale = function (out, q, v, s) { - // Quaternion math - var x = q[0], y = q[1], z = q[2], w = q[3], - x2 = x + x, - y2 = y + y, - z2 = z + z, - - xx = x * x2, - xy = x * y2, - xz = x * z2, - yy = y * y2, - yz = y * z2, - zz = z * z2, - wx = w * x2, - wy = w * y2, - wz = w * z2, - sx = s[0], - sy = s[1], - sz = s[2]; - - out[0] = (1 - (yy + zz)) * sx; - out[1] = (xy + wz) * sx; - out[2] = (xz - wy) * sx; - out[3] = 0; - out[4] = (xy - wz) * sy; - out[5] = (1 - (xx + zz)) * sy; - out[6] = (yz + wx) * sy; - out[7] = 0; - out[8] = (xz + wy) * sz; - out[9] = (yz - wx) * sz; - out[10] = (1 - (xx + yy)) * sz; - out[11] = 0; - out[12] = v[0]; - out[13] = v[1]; - out[14] = v[2]; - out[15] = 1; - - return out; -}; - -/** - * Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin - * This is equivalent to (but much faster than): - * - * mat4.identity(dest); - * mat4.translate(dest, vec); - * mat4.translate(dest, origin); - * var quatMat = mat4.create(); - * quat4.toMat4(quat, quatMat); - * mat4.multiply(dest, quatMat); - * mat4.scale(dest, scale) - * mat4.translate(dest, negativeOrigin); - * - * @param {mat4} out mat4 receiving operation result - * @param {quat4} q Rotation quaternion - * @param {vec3} v Translation vector - * @param {vec3} s Scaling vector - * @param {vec3} o The origin vector around which to scale and rotate - * @returns {mat4} out - */ -mat4.fromRotationTranslationScaleOrigin = function (out, q, v, s, o) { - // Quaternion math - var x = q[0], y = q[1], z = q[2], w = q[3], - x2 = x + x, - y2 = y + y, - z2 = z + z, - - xx = x * x2, - xy = x * y2, - xz = x * z2, - yy = y * y2, - yz = y * z2, - zz = z * z2, - wx = w * x2, - wy = w * y2, - wz = w * z2, - - sx = s[0], - sy = s[1], - sz = s[2], - - ox = o[0], - oy = o[1], - oz = o[2]; - - out[0] = (1 - (yy + zz)) * sx; - out[1] = (xy + wz) * sx; - out[2] = (xz - wy) * sx; - out[3] = 0; - out[4] = (xy - wz) * sy; - out[5] = (1 - (xx + zz)) * sy; - out[6] = (yz + wx) * sy; - out[7] = 0; - out[8] = (xz + wy) * sz; - out[9] = (yz - wx) * sz; - out[10] = (1 - (xx + yy)) * sz; - out[11] = 0; - out[12] = v[0] + ox - (out[0] * ox + out[4] * oy + out[8] * oz); - out[13] = v[1] + oy - (out[1] * ox + out[5] * oy + out[9] * oz); - out[14] = v[2] + oz - (out[2] * ox + out[6] * oy + out[10] * oz); - out[15] = 1; - - return out; -}; - -/** - * Calculates a 4x4 matrix from the given quaternion - * - * @param {mat4} out mat4 receiving operation result - * @param {quat} q Quaternion to create matrix from - * - * @returns {mat4} out - */ -mat4.fromQuat = function (out, q) { - var x = q[0], y = q[1], z = q[2], w = q[3], - x2 = x + x, - y2 = y + y, - z2 = z + z, - - xx = x * x2, - yx = y * x2, - yy = y * y2, - zx = z * x2, - zy = z * y2, - zz = z * z2, - wx = w * x2, - wy = w * y2, - wz = w * z2; - - out[0] = 1 - yy - zz; - out[1] = yx + wz; - out[2] = zx - wy; - out[3] = 0; - - out[4] = yx - wz; - out[5] = 1 - xx - zz; - out[6] = zy + wx; - out[7] = 0; - - out[8] = zx + wy; - out[9] = zy - wx; - out[10] = 1 - xx - yy; - out[11] = 0; - - out[12] = 0; - out[13] = 0; - out[14] = 0; - out[15] = 1; - - return out; -}; - -/** - * Generates a frustum matrix with the given bounds - * - * @param {mat4} out mat4 frustum matrix will be written into - * @param {Number} left Left bound of the frustum - * @param {Number} right Right bound of the frustum - * @param {Number} bottom Bottom bound of the frustum - * @param {Number} top Top bound of the frustum - * @param {Number} near Near bound of the frustum - * @param {Number} far Far bound of the frustum - * @returns {mat4} out - */ -mat4.frustum = function (out, left, right, bottom, top, near, far) { - var rl = 1 / (right - left), - tb = 1 / (top - bottom), - nf = 1 / (near - far); - out[0] = (near * 2) * rl; - out[1] = 0; - out[2] = 0; - out[3] = 0; - out[4] = 0; - out[5] = (near * 2) * tb; - out[6] = 0; - out[7] = 0; - out[8] = (right + left) * rl; - out[9] = (top + bottom) * tb; - out[10] = (far + near) * nf; - out[11] = -1; - out[12] = 0; - out[13] = 0; - out[14] = (far * near * 2) * nf; - out[15] = 0; - return out; -}; - -/** - * Generates a perspective projection matrix with the given bounds - * - * @param {mat4} out mat4 frustum matrix will be written into - * @param {number} fovy Vertical field of view in radians - * @param {number} aspect Aspect ratio. typically viewport width/height - * @param {number} near Near bound of the frustum - * @param {number} far Far bound of the frustum - * @returns {mat4} out - */ -mat4.perspective = function (out, fovy, aspect, near, far) { - var f = 1.0 / Math.tan(fovy / 2), - nf = 1 / (near - far); - out[0] = f / aspect; - out[1] = 0; - out[2] = 0; - out[3] = 0; - out[4] = 0; - out[5] = f; - out[6] = 0; - out[7] = 0; - out[8] = 0; - out[9] = 0; - out[10] = (far + near) * nf; - out[11] = -1; - out[12] = 0; - out[13] = 0; - out[14] = (2 * far * near) * nf; - out[15] = 0; - return out; -}; - -/** - * Generates a perspective projection matrix with the given field of view. - * This is primarily useful for generating projection matrices to be used - * with the still experiemental WebVR API. - * - * @param {mat4} out mat4 frustum matrix will be written into - * @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees - * @param {number} near Near bound of the frustum - * @param {number} far Far bound of the frustum - * @returns {mat4} out - */ -mat4.perspectiveFromFieldOfView = function (out, fov, near, far) { - var upTan = Math.tan(fov.upDegrees * Math.PI/180.0), - downTan = Math.tan(fov.downDegrees * Math.PI/180.0), - leftTan = Math.tan(fov.leftDegrees * Math.PI/180.0), - rightTan = Math.tan(fov.rightDegrees * Math.PI/180.0), - xScale = 2.0 / (leftTan + rightTan), - yScale = 2.0 / (upTan + downTan); - - out[0] = xScale; - out[1] = 0.0; - out[2] = 0.0; - out[3] = 0.0; - out[4] = 0.0; - out[5] = yScale; - out[6] = 0.0; - out[7] = 0.0; - out[8] = -((leftTan - rightTan) * xScale * 0.5); - out[9] = ((upTan - downTan) * yScale * 0.5); - out[10] = far / (near - far); - out[11] = -1.0; - out[12] = 0.0; - out[13] = 0.0; - out[14] = (far * near) / (near - far); - out[15] = 0.0; - return out; -} - -/** - * Generates a orthogonal projection matrix with the given bounds - * - * @param {mat4} out mat4 frustum matrix will be written into - * @param {number} left Left bound of the frustum - * @param {number} right Right bound of the frustum - * @param {number} bottom Bottom bound of the frustum - * @param {number} top Top bound of the frustum - * @param {number} near Near bound of the frustum - * @param {number} far Far bound of the frustum - * @returns {mat4} out - */ -mat4.ortho = function (out, left, right, bottom, top, near, far) { - var lr = 1 / (left - right), - bt = 1 / (bottom - top), - nf = 1 / (near - far); - out[0] = -2 * lr; - out[1] = 0; - out[2] = 0; - out[3] = 0; - out[4] = 0; - out[5] = -2 * bt; - out[6] = 0; - out[7] = 0; - out[8] = 0; - out[9] = 0; - out[10] = 2 * nf; - out[11] = 0; - out[12] = (left + right) * lr; - out[13] = (top + bottom) * bt; - out[14] = (far + near) * nf; - out[15] = 1; - return out; -}; - -/** - * Generates a look-at matrix with the given eye position, focal point, and up axis - * - * @param {mat4} out mat4 frustum matrix will be written into - * @param {vec3} eye Position of the viewer - * @param {vec3} center Point the viewer is looking at - * @param {vec3} up vec3 pointing up - * @returns {mat4} out - */ -mat4.lookAt = function (out, eye, center, up) { - var x0, x1, x2, y0, y1, y2, z0, z1, z2, len, - eyex = eye[0], - eyey = eye[1], - eyez = eye[2], - upx = up[0], - upy = up[1], - upz = up[2], - centerx = center[0], - centery = center[1], - centerz = center[2]; - - if (Math.abs(eyex - centerx) < glMatrix.EPSILON && - Math.abs(eyey - centery) < glMatrix.EPSILON && - Math.abs(eyez - centerz) < glMatrix.EPSILON) { - return mat4.identity(out); - } - - z0 = eyex - centerx; - z1 = eyey - centery; - z2 = eyez - centerz; - - len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); - z0 *= len; - z1 *= len; - z2 *= len; - - x0 = upy * z2 - upz * z1; - x1 = upz * z0 - upx * z2; - x2 = upx * z1 - upy * z0; - len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); - if (!len) { - x0 = 0; - x1 = 0; - x2 = 0; - } else { - len = 1 / len; - x0 *= len; - x1 *= len; - x2 *= len; - } - - y0 = z1 * x2 - z2 * x1; - y1 = z2 * x0 - z0 * x2; - y2 = z0 * x1 - z1 * x0; - - len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); - if (!len) { - y0 = 0; - y1 = 0; - y2 = 0; - } else { - len = 1 / len; - y0 *= len; - y1 *= len; - y2 *= len; - } - - out[0] = x0; - out[1] = y0; - out[2] = z0; - out[3] = 0; - out[4] = x1; - out[5] = y1; - out[6] = z1; - out[7] = 0; - out[8] = x2; - out[9] = y2; - out[10] = z2; - out[11] = 0; - out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); - out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); - out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); - out[15] = 1; - - return out; -}; - -/** - * Returns a string representation of a mat4 - * - * @param {mat4} mat matrix to represent as a string - * @returns {String} string representation of the matrix - */ -mat4.str = function (a) { - return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + - a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' + - a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' + - a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')'; -}; - -/** - * Returns Frobenius norm of a mat4 - * - * @param {mat4} a the matrix to calculate Frobenius norm of - * @returns {Number} Frobenius norm - */ -mat4.frob = function (a) { - return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2) + Math.pow(a[9], 2) + Math.pow(a[10], 2) + Math.pow(a[11], 2) + Math.pow(a[12], 2) + Math.pow(a[13], 2) + Math.pow(a[14], 2) + Math.pow(a[15], 2) )) -}; - -/** - * Adds two mat4's - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the first operand - * @param {mat4} b the second operand - * @returns {mat4} out - */ -mat4.add = function(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]; - out[9] = a[9] + b[9]; - out[10] = a[10] + b[10]; - out[11] = a[11] + b[11]; - out[12] = a[12] + b[12]; - out[13] = a[13] + b[13]; - out[14] = a[14] + b[14]; - out[15] = a[15] + b[15]; - return out; -}; - -/** - * Subtracts matrix b from matrix a - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the first operand - * @param {mat4} b the second operand - * @returns {mat4} out - */ -mat4.subtract = function(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]; - out[9] = a[9] - b[9]; - out[10] = a[10] - b[10]; - out[11] = a[11] - b[11]; - out[12] = a[12] - b[12]; - out[13] = a[13] - b[13]; - out[14] = a[14] - b[14]; - out[15] = a[15] - b[15]; - return out; -}; - -/** - * Alias for {@link mat4.subtract} - * @function - */ -mat4.sub = mat4.subtract; - -/** - * Multiply each element of the matrix by a scalar. - * - * @param {mat4} out the receiving matrix - * @param {mat4} a the matrix to scale - * @param {Number} b amount to scale the matrix's elements by - * @returns {mat4} out - */ -mat4.multiplyScalar = function(out, a, b) { - out[0] = a[0] * b; - out[1] = a[1] * b; - out[2] = a[2] * b; - out[3] = a[3] * b; - out[4] = a[4] * b; - out[5] = a[5] * b; - out[6] = a[6] * b; - out[7] = a[7] * b; - out[8] = a[8] * b; - out[9] = a[9] * b; - out[10] = a[10] * b; - out[11] = a[11] * b; - out[12] = a[12] * b; - out[13] = a[13] * b; - out[14] = a[14] * b; - out[15] = a[15] * b; - return out; -}; - -/** - * Adds two mat4's after multiplying each element of the second operand by a scalar value. - * - * @param {mat4} out the receiving vector - * @param {mat4} a the first operand - * @param {mat4} b the second operand - * @param {Number} scale the amount to scale b's elements by before adding - * @returns {mat4} out - */ -mat4.multiplyScalarAndAdd = function(out, a, b, scale) { - out[0] = a[0] + (b[0] * scale); - out[1] = a[1] + (b[1] * scale); - out[2] = a[2] + (b[2] * scale); - out[3] = a[3] + (b[3] * scale); - out[4] = a[4] + (b[4] * scale); - out[5] = a[5] + (b[5] * scale); - out[6] = a[6] + (b[6] * scale); - out[7] = a[7] + (b[7] * scale); - out[8] = a[8] + (b[8] * scale); - out[9] = a[9] + (b[9] * scale); - out[10] = a[10] + (b[10] * scale); - out[11] = a[11] + (b[11] * scale); - out[12] = a[12] + (b[12] * scale); - out[13] = a[13] + (b[13] * scale); - out[14] = a[14] + (b[14] * scale); - out[15] = a[15] + (b[15] * scale); - return out; -}; - -/** - * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) - * - * @param {mat4} a The first matrix. - * @param {mat4} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat4.exactEquals = function (a, b) { - return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && - a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && - a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && - a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15]; -}; - -/** - * Returns whether or not the matrices have approximately the same elements in the same position. - * - * @param {mat4} a The first matrix. - * @param {mat4} b The second matrix. - * @returns {Boolean} True if the matrices are equal, false otherwise. - */ -mat4.equals = function (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], a9 = a[9], a10 = a[10], a11 = a[11], - a12 = a[12], a13 = a[13], a14 = a[14], a15 = a[15]; - - 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], b9 = b[9], b10 = b[10], b11 = b[11], - b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15]; - - return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && - Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && - Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && - Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && - Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5)) && - Math.abs(a6 - b6) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a6), Math.abs(b6)) && - Math.abs(a7 - b7) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a7), Math.abs(b7)) && - Math.abs(a8 - b8) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a8), Math.abs(b8)) && - Math.abs(a9 - b9) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a9), Math.abs(b9)) && - Math.abs(a10 - b10) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a10), Math.abs(b10)) && - Math.abs(a11 - b11) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a11), Math.abs(b11)) && - Math.abs(a12 - b12) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a12), Math.abs(b12)) && - Math.abs(a13 - b13) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a13), Math.abs(b13)) && - Math.abs(a14 - b14) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a14), Math.abs(b14)) && - Math.abs(a15 - b15) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a15), Math.abs(b15))); -}; - - - -module.exports = mat4; - -},{"./common.js":1032}],1037:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); -var mat3 = require("./mat3.js"); -var vec3 = require("./vec3.js"); -var vec4 = require("./vec4.js"); - -/** - * @class Quaternion - * @name quat - */ -var quat = {}; - -/** - * Creates a new identity quat - * - * @returns {quat} a new quaternion - */ -quat.create = function() { - var out = new glMatrix.ARRAY_TYPE(4); - out[0] = 0; - out[1] = 0; - out[2] = 0; - out[3] = 1; - return out; -}; - -/** - * Sets a quaternion to represent the shortest rotation from one - * vector to another. - * - * Both vectors are assumed to be unit length. - * - * @param {quat} out the receiving quaternion. - * @param {vec3} a the initial vector - * @param {vec3} b the destination vector - * @returns {quat} out - */ -quat.rotationTo = (function() { - var tmpvec3 = vec3.create(); - var xUnitVec3 = vec3.fromValues(1,0,0); - var yUnitVec3 = vec3.fromValues(0,1,0); - - return function(out, a, b) { - var dot = vec3.dot(a, b); - if (dot < -0.999999) { - vec3.cross(tmpvec3, xUnitVec3, a); - if (vec3.length(tmpvec3) < 0.000001) - vec3.cross(tmpvec3, yUnitVec3, a); - vec3.normalize(tmpvec3, tmpvec3); - quat.setAxisAngle(out, tmpvec3, Math.PI); - return out; - } else if (dot > 0.999999) { - out[0] = 0; - out[1] = 0; - out[2] = 0; - out[3] = 1; - return out; - } else { - vec3.cross(tmpvec3, a, b); - out[0] = tmpvec3[0]; - out[1] = tmpvec3[1]; - out[2] = tmpvec3[2]; - out[3] = 1 + dot; - return quat.normalize(out, out); - } - }; -})(); - -/** - * Sets the specified quaternion with values corresponding to the given - * axes. Each axis is a vec3 and is expected to be unit length and - * perpendicular to all other specified axes. - * - * @param {vec3} view the vector representing the viewing direction - * @param {vec3} right the vector representing the local "right" direction - * @param {vec3} up the vector representing the local "up" direction - * @returns {quat} out - */ -quat.setAxes = (function() { - var matr = mat3.create(); - - return function(out, view, right, up) { - matr[0] = right[0]; - matr[3] = right[1]; - matr[6] = right[2]; - - matr[1] = up[0]; - matr[4] = up[1]; - matr[7] = up[2]; - - matr[2] = -view[0]; - matr[5] = -view[1]; - matr[8] = -view[2]; - - return quat.normalize(out, quat.fromMat3(out, matr)); - }; -})(); - -/** - * Creates a new quat initialized with values from an existing quaternion - * - * @param {quat} a quaternion to clone - * @returns {quat} a new quaternion - * @function - */ -quat.clone = vec4.clone; - -/** - * Creates a new quat initialized with the given values - * - * @param {Number} x X component - * @param {Number} y Y component - * @param {Number} z Z component - * @param {Number} w W component - * @returns {quat} a new quaternion - * @function - */ -quat.fromValues = vec4.fromValues; - -/** - * Copy the values from one quat to another - * - * @param {quat} out the receiving quaternion - * @param {quat} a the source quaternion - * @returns {quat} out - * @function - */ -quat.copy = vec4.copy; - -/** - * Set the components of a quat to the given values - * - * @param {quat} out the receiving quaternion - * @param {Number} x X component - * @param {Number} y Y component - * @param {Number} z Z component - * @param {Number} w W component - * @returns {quat} out - * @function - */ -quat.set = vec4.set; - -/** - * Set a quat to the identity quaternion - * - * @param {quat} out the receiving quaternion - * @returns {quat} out - */ -quat.identity = function(out) { - out[0] = 0; - out[1] = 0; - out[2] = 0; - out[3] = 1; - return out; -}; - -/** - * Sets a quat from the given angle and rotation axis, - * then returns it. - * - * @param {quat} out the receiving quaternion - * @param {vec3} axis the axis around which to rotate - * @param {Number} rad the angle in radians - * @returns {quat} out - **/ -quat.setAxisAngle = function(out, axis, rad) { - rad = rad * 0.5; - var s = Math.sin(rad); - out[0] = s * axis[0]; - out[1] = s * axis[1]; - out[2] = s * axis[2]; - out[3] = Math.cos(rad); - return out; -}; - -/** - * Gets the rotation axis and angle for a given - * quaternion. If a quaternion is created with - * setAxisAngle, this method will return the same - * values as providied in the original parameter list - * OR functionally equivalent values. - * Example: The quaternion formed by axis [0, 0, 1] and - * angle -90 is the same as the quaternion formed by - * [0, 0, 1] and 270. This method favors the latter. - * @param {vec3} out_axis Vector receiving the axis of rotation - * @param {quat} q Quaternion to be decomposed - * @return {Number} Angle, in radians, of the rotation - */ -quat.getAxisAngle = function(out_axis, q) { - var rad = Math.acos(q[3]) * 2.0; - var s = Math.sin(rad / 2.0); - if (s != 0.0) { - out_axis[0] = q[0] / s; - out_axis[1] = q[1] / s; - out_axis[2] = q[2] / s; - } else { - // If s is zero, return any axis (no rotation - axis does not matter) - out_axis[0] = 1; - out_axis[1] = 0; - out_axis[2] = 0; - } - return rad; -}; - -/** - * Adds two quat's - * - * @param {quat} out the receiving quaternion - * @param {quat} a the first operand - * @param {quat} b the second operand - * @returns {quat} out - * @function - */ -quat.add = vec4.add; - -/** - * Multiplies two quat's - * - * @param {quat} out the receiving quaternion - * @param {quat} a the first operand - * @param {quat} b the second operand - * @returns {quat} out - */ -quat.multiply = function(out, a, b) { - var ax = a[0], ay = a[1], az = a[2], aw = a[3], - bx = b[0], by = b[1], bz = b[2], bw = b[3]; - - out[0] = ax * bw + aw * bx + ay * bz - az * by; - out[1] = ay * bw + aw * by + az * bx - ax * bz; - out[2] = az * bw + aw * bz + ax * by - ay * bx; - out[3] = aw * bw - ax * bx - ay * by - az * bz; - return out; -}; - -/** - * Alias for {@link quat.multiply} - * @function - */ -quat.mul = quat.multiply; - -/** - * Scales a quat by a scalar number - * - * @param {quat} out the receiving vector - * @param {quat} a the vector to scale - * @param {Number} b amount to scale the vector by - * @returns {quat} out - * @function - */ -quat.scale = vec4.scale; - -/** - * Rotates a quaternion by the given angle about the X axis - * - * @param {quat} out quat receiving operation result - * @param {quat} a quat to rotate - * @param {number} rad angle (in radians) to rotate - * @returns {quat} out - */ -quat.rotateX = function (out, a, rad) { - rad *= 0.5; - - var ax = a[0], ay = a[1], az = a[2], aw = a[3], - bx = Math.sin(rad), bw = Math.cos(rad); - - out[0] = ax * bw + aw * bx; - out[1] = ay * bw + az * bx; - out[2] = az * bw - ay * bx; - out[3] = aw * bw - ax * bx; - return out; -}; - -/** - * Rotates a quaternion by the given angle about the Y axis - * - * @param {quat} out quat receiving operation result - * @param {quat} a quat to rotate - * @param {number} rad angle (in radians) to rotate - * @returns {quat} out - */ -quat.rotateY = function (out, a, rad) { - rad *= 0.5; - - var ax = a[0], ay = a[1], az = a[2], aw = a[3], - by = Math.sin(rad), bw = Math.cos(rad); - - out[0] = ax * bw - az * by; - out[1] = ay * bw + aw * by; - out[2] = az * bw + ax * by; - out[3] = aw * bw - ay * by; - return out; -}; - -/** - * Rotates a quaternion by the given angle about the Z axis - * - * @param {quat} out quat receiving operation result - * @param {quat} a quat to rotate - * @param {number} rad angle (in radians) to rotate - * @returns {quat} out - */ -quat.rotateZ = function (out, a, rad) { - rad *= 0.5; - - var ax = a[0], ay = a[1], az = a[2], aw = a[3], - bz = Math.sin(rad), bw = Math.cos(rad); - - out[0] = ax * bw + ay * bz; - out[1] = ay * bw - ax * bz; - out[2] = az * bw + aw * bz; - out[3] = aw * bw - az * bz; - return out; -}; - -/** - * Calculates the W component of a quat from the X, Y, and Z components. - * Assumes that quaternion is 1 unit in length. - * Any existing W component will be ignored. - * - * @param {quat} out the receiving quaternion - * @param {quat} a quat to calculate W component of - * @returns {quat} out - */ -quat.calculateW = function (out, a) { - var x = a[0], y = a[1], z = a[2]; - - out[0] = x; - out[1] = y; - out[2] = z; - out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); - return out; -}; - -/** - * Calculates the dot product of two quat's - * - * @param {quat} a the first operand - * @param {quat} b the second operand - * @returns {Number} dot product of a and b - * @function - */ -quat.dot = vec4.dot; - -/** - * Performs a linear interpolation between two quat's - * - * @param {quat} out the receiving quaternion - * @param {quat} a the first operand - * @param {quat} b the second operand - * @param {Number} t interpolation amount between the two inputs - * @returns {quat} out - * @function - */ -quat.lerp = vec4.lerp; - -/** - * Performs a spherical linear interpolation between two quat - * - * @param {quat} out the receiving quaternion - * @param {quat} a the first operand - * @param {quat} b the second operand - * @param {Number} t interpolation amount between the two inputs - * @returns {quat} out - */ -quat.slerp = function (out, a, b, t) { - // benchmarks: - // http://jsperf.com/quaternion-slerp-implementations - - var ax = a[0], ay = a[1], az = a[2], aw = a[3], - bx = b[0], by = b[1], bz = b[2], bw = b[3]; - - var omega, cosom, sinom, scale0, scale1; - - // calc cosine - cosom = ax * bx + ay * by + az * bz + aw * bw; - // adjust signs (if necessary) - if ( cosom < 0.0 ) { - cosom = -cosom; - bx = - bx; - by = - by; - bz = - bz; - bw = - bw; - } - // calculate coefficients - if ( (1.0 - cosom) > 0.000001 ) { - // standard case (slerp) - omega = Math.acos(cosom); - sinom = Math.sin(omega); - scale0 = Math.sin((1.0 - t) * omega) / sinom; - scale1 = Math.sin(t * omega) / sinom; - } else { - // "from" and "to" quaternions are very close - // ... so we can do a linear interpolation - scale0 = 1.0 - t; - scale1 = t; - } - // calculate final values - out[0] = scale0 * ax + scale1 * bx; - out[1] = scale0 * ay + scale1 * by; - out[2] = scale0 * az + scale1 * bz; - out[3] = scale0 * aw + scale1 * bw; - - return out; -}; - -/** - * Performs a spherical linear interpolation with two control points - * - * @param {quat} out the receiving quaternion - * @param {quat} a the first operand - * @param {quat} b the second operand - * @param {quat} c the third operand - * @param {quat} d the fourth operand - * @param {Number} t interpolation amount - * @returns {quat} out - */ -quat.sqlerp = (function () { - var temp1 = quat.create(); - var temp2 = quat.create(); - - return function (out, a, b, c, d, t) { - quat.slerp(temp1, a, d, t); - quat.slerp(temp2, b, c, t); - quat.slerp(out, temp1, temp2, 2 * t * (1 - t)); - - return out; - }; -}()); - -/** - * Calculates the inverse of a quat - * - * @param {quat} out the receiving quaternion - * @param {quat} a quat to calculate inverse of - * @returns {quat} out - */ -quat.invert = function(out, a) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], - dot = a0*a0 + a1*a1 + a2*a2 + a3*a3, - invDot = dot ? 1.0/dot : 0; - - // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 - - out[0] = -a0*invDot; - out[1] = -a1*invDot; - out[2] = -a2*invDot; - out[3] = a3*invDot; - return out; -}; - -/** - * Calculates the conjugate of a quat - * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. - * - * @param {quat} out the receiving quaternion - * @param {quat} a quat to calculate conjugate of - * @returns {quat} out - */ -quat.conjugate = function (out, a) { - out[0] = -a[0]; - out[1] = -a[1]; - out[2] = -a[2]; - out[3] = a[3]; - return out; -}; - -/** - * Calculates the length of a quat - * - * @param {quat} a vector to calculate length of - * @returns {Number} length of a - * @function - */ -quat.length = vec4.length; - -/** - * Alias for {@link quat.length} - * @function - */ -quat.len = quat.length; - -/** - * Calculates the squared length of a quat - * - * @param {quat} a vector to calculate squared length of - * @returns {Number} squared length of a - * @function - */ -quat.squaredLength = vec4.squaredLength; - -/** - * Alias for {@link quat.squaredLength} - * @function - */ -quat.sqrLen = quat.squaredLength; - -/** - * Normalize a quat - * - * @param {quat} out the receiving quaternion - * @param {quat} a quaternion to normalize - * @returns {quat} out - * @function - */ -quat.normalize = vec4.normalize; - -/** - * Creates a quaternion from the given 3x3 rotation matrix. - * - * NOTE: The resultant quaternion is not normalized, so you should be sure - * to renormalize the quaternion yourself where necessary. - * - * @param {quat} out the receiving quaternion - * @param {mat3} m rotation matrix - * @returns {quat} out - * @function - */ -quat.fromMat3 = function(out, m) { - // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes - // article "Quaternion Calculus and Fast Animation". - var fTrace = m[0] + m[4] + m[8]; - var fRoot; - - if ( fTrace > 0.0 ) { - // |w| > 1/2, may as well choose w > 1/2 - fRoot = Math.sqrt(fTrace + 1.0); // 2w - out[3] = 0.5 * fRoot; - fRoot = 0.5/fRoot; // 1/(4w) - out[0] = (m[5]-m[7])*fRoot; - out[1] = (m[6]-m[2])*fRoot; - out[2] = (m[1]-m[3])*fRoot; - } else { - // |w| <= 1/2 - var i = 0; - if ( m[4] > m[0] ) - i = 1; - if ( m[8] > m[i*3+i] ) - i = 2; - var j = (i+1)%3; - var k = (i+2)%3; - - fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0); - out[i] = 0.5 * fRoot; - fRoot = 0.5 / fRoot; - out[3] = (m[j*3+k] - m[k*3+j]) * fRoot; - out[j] = (m[j*3+i] + m[i*3+j]) * fRoot; - out[k] = (m[k*3+i] + m[i*3+k]) * fRoot; - } - - return out; -}; - -/** - * Returns a string representation of a quatenion - * - * @param {quat} vec vector to represent as a string - * @returns {String} string representation of the vector - */ -quat.str = function (a) { - return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; -}; - -/** - * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===) - * - * @param {quat} a The first quaternion. - * @param {quat} b The second quaternion. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -quat.exactEquals = vec4.exactEquals; - -/** - * Returns whether or not the quaternions have approximately the same elements in the same position. - * - * @param {quat} a The first vector. - * @param {quat} b The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -quat.equals = vec4.equals; - -module.exports = quat; - -},{"./common.js":1032,"./mat3.js":1035,"./vec3.js":1039,"./vec4.js":1040}],1038:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); - -/** - * @class 2 Dimensional Vector - * @name vec2 - */ -var vec2 = {}; - -/** - * Creates a new, empty vec2 - * - * @returns {vec2} a new 2D vector - */ -vec2.create = function() { - var out = new glMatrix.ARRAY_TYPE(2); - out[0] = 0; - out[1] = 0; - return out; -}; - -/** - * Creates a new vec2 initialized with values from an existing vector - * - * @param {vec2} a vector to clone - * @returns {vec2} a new 2D vector - */ -vec2.clone = function(a) { - var out = new glMatrix.ARRAY_TYPE(2); - out[0] = a[0]; - out[1] = a[1]; - return out; -}; - -/** - * Creates a new vec2 initialized with the given values - * - * @param {Number} x X component - * @param {Number} y Y component - * @returns {vec2} a new 2D vector - */ -vec2.fromValues = function(x, y) { - var out = new glMatrix.ARRAY_TYPE(2); - out[0] = x; - out[1] = y; - return out; -}; - -/** - * Copy the values from one vec2 to another - * - * @param {vec2} out the receiving vector - * @param {vec2} a the source vector - * @returns {vec2} out - */ -vec2.copy = function(out, a) { - out[0] = a[0]; - out[1] = a[1]; - return out; -}; - -/** - * Set the components of a vec2 to the given values - * - * @param {vec2} out the receiving vector - * @param {Number} x X component - * @param {Number} y Y component - * @returns {vec2} out - */ -vec2.set = function(out, x, y) { - out[0] = x; - out[1] = y; - return out; -}; - -/** - * Adds two vec2's - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {vec2} out - */ -vec2.add = function(out, a, b) { - out[0] = a[0] + b[0]; - out[1] = a[1] + b[1]; - return out; -}; - -/** - * Subtracts vector b from vector a - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {vec2} out - */ -vec2.subtract = function(out, a, b) { - out[0] = a[0] - b[0]; - out[1] = a[1] - b[1]; - return out; -}; - -/** - * Alias for {@link vec2.subtract} - * @function - */ -vec2.sub = vec2.subtract; - -/** - * Multiplies two vec2's - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {vec2} out - */ -vec2.multiply = function(out, a, b) { - out[0] = a[0] * b[0]; - out[1] = a[1] * b[1]; - return out; -}; - -/** - * Alias for {@link vec2.multiply} - * @function - */ -vec2.mul = vec2.multiply; - -/** - * Divides two vec2's - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {vec2} out - */ -vec2.divide = function(out, a, b) { - out[0] = a[0] / b[0]; - out[1] = a[1] / b[1]; - return out; -}; - -/** - * Alias for {@link vec2.divide} - * @function - */ -vec2.div = vec2.divide; - -/** - * Math.ceil the components of a vec2 - * - * @param {vec2} out the receiving vector - * @param {vec2} a vector to ceil - * @returns {vec2} out - */ -vec2.ceil = function (out, a) { - out[0] = Math.ceil(a[0]); - out[1] = Math.ceil(a[1]); - return out; -}; - -/** - * Math.floor the components of a vec2 - * - * @param {vec2} out the receiving vector - * @param {vec2} a vector to floor - * @returns {vec2} out - */ -vec2.floor = function (out, a) { - out[0] = Math.floor(a[0]); - out[1] = Math.floor(a[1]); - return out; -}; - -/** - * Returns the minimum of two vec2's - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {vec2} out - */ -vec2.min = function(out, a, b) { - out[0] = Math.min(a[0], b[0]); - out[1] = Math.min(a[1], b[1]); - return out; -}; - -/** - * Returns the maximum of two vec2's - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {vec2} out - */ -vec2.max = function(out, a, b) { - out[0] = Math.max(a[0], b[0]); - out[1] = Math.max(a[1], b[1]); - return out; -}; - -/** - * Math.round the components of a vec2 - * - * @param {vec2} out the receiving vector - * @param {vec2} a vector to round - * @returns {vec2} out - */ -vec2.round = function (out, a) { - out[0] = Math.round(a[0]); - out[1] = Math.round(a[1]); - return out; -}; - -/** - * Scales a vec2 by a scalar number - * - * @param {vec2} out the receiving vector - * @param {vec2} a the vector to scale - * @param {Number} b amount to scale the vector by - * @returns {vec2} out - */ -vec2.scale = function(out, a, b) { - out[0] = a[0] * b; - out[1] = a[1] * b; - return out; -}; - -/** - * Adds two vec2's after scaling the second operand by a scalar value - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @param {Number} scale the amount to scale b by before adding - * @returns {vec2} out - */ -vec2.scaleAndAdd = function(out, a, b, scale) { - out[0] = a[0] + (b[0] * scale); - out[1] = a[1] + (b[1] * scale); - return out; -}; - -/** - * Calculates the euclidian distance between two vec2's - * - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {Number} distance between a and b - */ -vec2.distance = function(a, b) { - var x = b[0] - a[0], - y = b[1] - a[1]; - return Math.sqrt(x*x + y*y); -}; - -/** - * Alias for {@link vec2.distance} - * @function - */ -vec2.dist = vec2.distance; - -/** - * Calculates the squared euclidian distance between two vec2's - * - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {Number} squared distance between a and b - */ -vec2.squaredDistance = function(a, b) { - var x = b[0] - a[0], - y = b[1] - a[1]; - return x*x + y*y; -}; - -/** - * Alias for {@link vec2.squaredDistance} - * @function - */ -vec2.sqrDist = vec2.squaredDistance; - -/** - * Calculates the length of a vec2 - * - * @param {vec2} a vector to calculate length of - * @returns {Number} length of a - */ -vec2.length = function (a) { - var x = a[0], - y = a[1]; - return Math.sqrt(x*x + y*y); -}; - -/** - * Alias for {@link vec2.length} - * @function - */ -vec2.len = vec2.length; - -/** - * Calculates the squared length of a vec2 - * - * @param {vec2} a vector to calculate squared length of - * @returns {Number} squared length of a - */ -vec2.squaredLength = function (a) { - var x = a[0], - y = a[1]; - return x*x + y*y; -}; - -/** - * Alias for {@link vec2.squaredLength} - * @function - */ -vec2.sqrLen = vec2.squaredLength; - -/** - * Negates the components of a vec2 - * - * @param {vec2} out the receiving vector - * @param {vec2} a vector to negate - * @returns {vec2} out - */ -vec2.negate = function(out, a) { - out[0] = -a[0]; - out[1] = -a[1]; - return out; -}; - -/** - * Returns the inverse of the components of a vec2 - * - * @param {vec2} out the receiving vector - * @param {vec2} a vector to invert - * @returns {vec2} out - */ -vec2.inverse = function(out, a) { - out[0] = 1.0 / a[0]; - out[1] = 1.0 / a[1]; - return out; -}; - -/** - * Normalize a vec2 - * - * @param {vec2} out the receiving vector - * @param {vec2} a vector to normalize - * @returns {vec2} out - */ -vec2.normalize = function(out, a) { - var x = a[0], - y = a[1]; - var len = x*x + y*y; - if (len > 0) { - //TODO: evaluate use of glm_invsqrt here? - len = 1 / Math.sqrt(len); - out[0] = a[0] * len; - out[1] = a[1] * len; - } - return out; -}; - -/** - * Calculates the dot product of two vec2's - * - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {Number} dot product of a and b - */ -vec2.dot = function (a, b) { - return a[0] * b[0] + a[1] * b[1]; -}; - -/** - * Computes the cross product of two vec2's - * Note that the cross product must by definition produce a 3D vector - * - * @param {vec3} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @returns {vec3} out - */ -vec2.cross = function(out, a, b) { - var z = a[0] * b[1] - a[1] * b[0]; - out[0] = out[1] = 0; - out[2] = z; - return out; -}; - -/** - * Performs a linear interpolation between two vec2's - * - * @param {vec2} out the receiving vector - * @param {vec2} a the first operand - * @param {vec2} b the second operand - * @param {Number} t interpolation amount between the two inputs - * @returns {vec2} out - */ -vec2.lerp = function (out, a, b, t) { - var ax = a[0], - ay = a[1]; - out[0] = ax + t * (b[0] - ax); - out[1] = ay + t * (b[1] - ay); - return out; -}; - -/** - * Generates a random vector with the given scale - * - * @param {vec2} out the receiving vector - * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned - * @returns {vec2} out - */ -vec2.random = function (out, scale) { - scale = scale || 1.0; - var r = glMatrix.RANDOM() * 2.0 * Math.PI; - out[0] = Math.cos(r) * scale; - out[1] = Math.sin(r) * scale; - return out; -}; - -/** - * Transforms the vec2 with a mat2 - * - * @param {vec2} out the receiving vector - * @param {vec2} a the vector to transform - * @param {mat2} m matrix to transform with - * @returns {vec2} out - */ -vec2.transformMat2 = function(out, a, m) { - var x = a[0], - y = a[1]; - out[0] = m[0] * x + m[2] * y; - out[1] = m[1] * x + m[3] * y; - return out; -}; - -/** - * Transforms the vec2 with a mat2d - * - * @param {vec2} out the receiving vector - * @param {vec2} a the vector to transform - * @param {mat2d} m matrix to transform with - * @returns {vec2} out - */ -vec2.transformMat2d = function(out, a, m) { - var x = a[0], - y = a[1]; - out[0] = m[0] * x + m[2] * y + m[4]; - out[1] = m[1] * x + m[3] * y + m[5]; - return out; -}; - -/** - * Transforms the vec2 with a mat3 - * 3rd vector component is implicitly '1' - * - * @param {vec2} out the receiving vector - * @param {vec2} a the vector to transform - * @param {mat3} m matrix to transform with - * @returns {vec2} out - */ -vec2.transformMat3 = function(out, a, m) { - var x = a[0], - y = a[1]; - out[0] = m[0] * x + m[3] * y + m[6]; - out[1] = m[1] * x + m[4] * y + m[7]; - return out; -}; - -/** - * Transforms the vec2 with a mat4 - * 3rd vector component is implicitly '0' - * 4th vector component is implicitly '1' - * - * @param {vec2} out the receiving vector - * @param {vec2} a the vector to transform - * @param {mat4} m matrix to transform with - * @returns {vec2} out - */ -vec2.transformMat4 = function(out, a, m) { - var x = a[0], - y = a[1]; - out[0] = m[0] * x + m[4] * y + m[12]; - out[1] = m[1] * x + m[5] * y + m[13]; - return out; -}; - -/** - * Perform some operation over an array of vec2s. - * - * @param {Array} a the array of vectors to iterate over - * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed - * @param {Number} offset Number of elements to skip at the beginning of the array - * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array - * @param {Function} fn Function to call for each vector in the array - * @param {Object} [arg] additional argument to pass to fn - * @returns {Array} a - * @function - */ -vec2.forEach = (function() { - var vec = vec2.create(); - - return function(a, stride, offset, count, fn, arg) { - var i, l; - if(!stride) { - stride = 2; - } - - if(!offset) { - offset = 0; - } - - if(count) { - l = Math.min((count * stride) + offset, a.length); - } else { - l = a.length; - } - - for(i = offset; i < l; i += stride) { - vec[0] = a[i]; vec[1] = a[i+1]; - fn(vec, vec, arg); - a[i] = vec[0]; a[i+1] = vec[1]; - } - - return a; - }; -})(); - -/** - * Returns a string representation of a vector - * - * @param {vec2} vec vector to represent as a string - * @returns {String} string representation of the vector - */ -vec2.str = function (a) { - return 'vec2(' + a[0] + ', ' + a[1] + ')'; -}; - -/** - * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===) - * - * @param {vec2} a The first vector. - * @param {vec2} b The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -vec2.exactEquals = function (a, b) { - return a[0] === b[0] && a[1] === b[1]; -}; - -/** - * Returns whether or not the vectors have approximately the same elements in the same position. - * - * @param {vec2} a The first vector. - * @param {vec2} b The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -vec2.equals = function (a, b) { - var a0 = a[0], a1 = a[1]; - var b0 = b[0], b1 = b[1]; - return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1))); -}; - -module.exports = vec2; - -},{"./common.js":1032}],1039:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); - -/** - * @class 3 Dimensional Vector - * @name vec3 - */ -var vec3 = {}; - -/** - * Creates a new, empty vec3 - * - * @returns {vec3} a new 3D vector - */ -vec3.create = function() { - var out = new glMatrix.ARRAY_TYPE(3); - out[0] = 0; - out[1] = 0; - out[2] = 0; - return out; -}; - -/** - * Creates a new vec3 initialized with values from an existing vector - * - * @param {vec3} a vector to clone - * @returns {vec3} a new 3D vector - */ -vec3.clone = function(a) { - var out = new glMatrix.ARRAY_TYPE(3); - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - return out; -}; - -/** - * Creates a new vec3 initialized with the given values - * - * @param {Number} x X component - * @param {Number} y Y component - * @param {Number} z Z component - * @returns {vec3} a new 3D vector - */ -vec3.fromValues = function(x, y, z) { - var out = new glMatrix.ARRAY_TYPE(3); - out[0] = x; - out[1] = y; - out[2] = z; - return out; -}; - -/** - * Copy the values from one vec3 to another - * - * @param {vec3} out the receiving vector - * @param {vec3} a the source vector - * @returns {vec3} out - */ -vec3.copy = function(out, a) { - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - return out; -}; - -/** - * Set the components of a vec3 to the given values - * - * @param {vec3} out the receiving vector - * @param {Number} x X component - * @param {Number} y Y component - * @param {Number} z Z component - * @returns {vec3} out - */ -vec3.set = function(out, x, y, z) { - out[0] = x; - out[1] = y; - out[2] = z; - return out; -}; - -/** - * Adds two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -vec3.add = function(out, a, b) { - out[0] = a[0] + b[0]; - out[1] = a[1] + b[1]; - out[2] = a[2] + b[2]; - return out; -}; - -/** - * Subtracts vector b from vector a - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -vec3.subtract = function(out, a, b) { - out[0] = a[0] - b[0]; - out[1] = a[1] - b[1]; - out[2] = a[2] - b[2]; - return out; -}; - -/** - * Alias for {@link vec3.subtract} - * @function - */ -vec3.sub = vec3.subtract; - -/** - * Multiplies two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -vec3.multiply = function(out, a, b) { - out[0] = a[0] * b[0]; - out[1] = a[1] * b[1]; - out[2] = a[2] * b[2]; - return out; -}; - -/** - * Alias for {@link vec3.multiply} - * @function - */ -vec3.mul = vec3.multiply; - -/** - * Divides two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -vec3.divide = function(out, a, b) { - out[0] = a[0] / b[0]; - out[1] = a[1] / b[1]; - out[2] = a[2] / b[2]; - return out; -}; - -/** - * Alias for {@link vec3.divide} - * @function - */ -vec3.div = vec3.divide; - -/** - * Math.ceil the components of a vec3 - * - * @param {vec3} out the receiving vector - * @param {vec3} a vector to ceil - * @returns {vec3} out - */ -vec3.ceil = function (out, a) { - out[0] = Math.ceil(a[0]); - out[1] = Math.ceil(a[1]); - out[2] = Math.ceil(a[2]); - return out; -}; - -/** - * Math.floor the components of a vec3 - * - * @param {vec3} out the receiving vector - * @param {vec3} a vector to floor - * @returns {vec3} out - */ -vec3.floor = function (out, a) { - out[0] = Math.floor(a[0]); - out[1] = Math.floor(a[1]); - out[2] = Math.floor(a[2]); - return out; -}; - -/** - * Returns the minimum of two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -vec3.min = function(out, a, b) { - out[0] = Math.min(a[0], b[0]); - out[1] = Math.min(a[1], b[1]); - out[2] = Math.min(a[2], b[2]); - return out; -}; - -/** - * Returns the maximum of two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -vec3.max = function(out, a, b) { - out[0] = Math.max(a[0], b[0]); - out[1] = Math.max(a[1], b[1]); - out[2] = Math.max(a[2], b[2]); - return out; -}; - -/** - * Math.round the components of a vec3 - * - * @param {vec3} out the receiving vector - * @param {vec3} a vector to round - * @returns {vec3} out - */ -vec3.round = function (out, a) { - out[0] = Math.round(a[0]); - out[1] = Math.round(a[1]); - out[2] = Math.round(a[2]); - return out; -}; - -/** - * Scales a vec3 by a scalar number - * - * @param {vec3} out the receiving vector - * @param {vec3} a the vector to scale - * @param {Number} b amount to scale the vector by - * @returns {vec3} out - */ -vec3.scale = function(out, a, b) { - out[0] = a[0] * b; - out[1] = a[1] * b; - out[2] = a[2] * b; - return out; -}; - -/** - * Adds two vec3's after scaling the second operand by a scalar value - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @param {Number} scale the amount to scale b by before adding - * @returns {vec3} out - */ -vec3.scaleAndAdd = function(out, a, b, scale) { - out[0] = a[0] + (b[0] * scale); - out[1] = a[1] + (b[1] * scale); - out[2] = a[2] + (b[2] * scale); - return out; -}; - -/** - * Calculates the euclidian distance between two vec3's - * - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {Number} distance between a and b - */ -vec3.distance = function(a, b) { - var x = b[0] - a[0], - y = b[1] - a[1], - z = b[2] - a[2]; - return Math.sqrt(x*x + y*y + z*z); -}; - -/** - * Alias for {@link vec3.distance} - * @function - */ -vec3.dist = vec3.distance; - -/** - * Calculates the squared euclidian distance between two vec3's - * - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {Number} squared distance between a and b - */ -vec3.squaredDistance = function(a, b) { - var x = b[0] - a[0], - y = b[1] - a[1], - z = b[2] - a[2]; - return x*x + y*y + z*z; -}; - -/** - * Alias for {@link vec3.squaredDistance} - * @function - */ -vec3.sqrDist = vec3.squaredDistance; - -/** - * Calculates the length of a vec3 - * - * @param {vec3} a vector to calculate length of - * @returns {Number} length of a - */ -vec3.length = function (a) { - var x = a[0], - y = a[1], - z = a[2]; - return Math.sqrt(x*x + y*y + z*z); -}; - -/** - * Alias for {@link vec3.length} - * @function - */ -vec3.len = vec3.length; - -/** - * Calculates the squared length of a vec3 - * - * @param {vec3} a vector to calculate squared length of - * @returns {Number} squared length of a - */ -vec3.squaredLength = function (a) { - var x = a[0], - y = a[1], - z = a[2]; - return x*x + y*y + z*z; -}; - -/** - * Alias for {@link vec3.squaredLength} - * @function - */ -vec3.sqrLen = vec3.squaredLength; - -/** - * Negates the components of a vec3 - * - * @param {vec3} out the receiving vector - * @param {vec3} a vector to negate - * @returns {vec3} out - */ -vec3.negate = function(out, a) { - out[0] = -a[0]; - out[1] = -a[1]; - out[2] = -a[2]; - return out; -}; - -/** - * Returns the inverse of the components of a vec3 - * - * @param {vec3} out the receiving vector - * @param {vec3} a vector to invert - * @returns {vec3} out - */ -vec3.inverse = function(out, a) { - out[0] = 1.0 / a[0]; - out[1] = 1.0 / a[1]; - out[2] = 1.0 / a[2]; - return out; -}; - -/** - * Normalize a vec3 - * - * @param {vec3} out the receiving vector - * @param {vec3} a vector to normalize - * @returns {vec3} out - */ -vec3.normalize = function(out, a) { - var x = a[0], - y = a[1], - z = a[2]; - var len = x*x + y*y + z*z; - if (len > 0) { - //TODO: evaluate use of glm_invsqrt here? - len = 1 / Math.sqrt(len); - out[0] = a[0] * len; - out[1] = a[1] * len; - out[2] = a[2] * len; - } - return out; -}; - -/** - * Calculates the dot product of two vec3's - * - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {Number} dot product of a and b - */ -vec3.dot = function (a, b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; -}; - -/** - * Computes the cross product of two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @returns {vec3} out - */ -vec3.cross = function(out, a, b) { - var ax = a[0], ay = a[1], az = a[2], - bx = b[0], by = b[1], bz = b[2]; - - out[0] = ay * bz - az * by; - out[1] = az * bx - ax * bz; - out[2] = ax * by - ay * bx; - return out; -}; - -/** - * Performs a linear interpolation between two vec3's - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @param {Number} t interpolation amount between the two inputs - * @returns {vec3} out - */ -vec3.lerp = function (out, a, b, t) { - var ax = a[0], - ay = a[1], - az = a[2]; - out[0] = ax + t * (b[0] - ax); - out[1] = ay + t * (b[1] - ay); - out[2] = az + t * (b[2] - az); - return out; -}; - -/** - * Performs a hermite interpolation with two control points - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @param {vec3} c the third operand - * @param {vec3} d the fourth operand - * @param {Number} t interpolation amount between the two inputs - * @returns {vec3} out - */ -vec3.hermite = function (out, a, b, c, d, t) { - var factorTimes2 = t * t, - factor1 = factorTimes2 * (2 * t - 3) + 1, - factor2 = factorTimes2 * (t - 2) + t, - factor3 = factorTimes2 * (t - 1), - factor4 = factorTimes2 * (3 - 2 * t); - - out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; - out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; - out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; - - return out; -}; - -/** - * Performs a bezier interpolation with two control points - * - * @param {vec3} out the receiving vector - * @param {vec3} a the first operand - * @param {vec3} b the second operand - * @param {vec3} c the third operand - * @param {vec3} d the fourth operand - * @param {Number} t interpolation amount between the two inputs - * @returns {vec3} out - */ -vec3.bezier = function (out, a, b, c, d, t) { - var inverseFactor = 1 - t, - inverseFactorTimesTwo = inverseFactor * inverseFactor, - factorTimes2 = t * t, - factor1 = inverseFactorTimesTwo * inverseFactor, - factor2 = 3 * t * inverseFactorTimesTwo, - factor3 = 3 * factorTimes2 * inverseFactor, - factor4 = factorTimes2 * t; - - out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; - out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; - out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; - - return out; -}; - -/** - * Generates a random vector with the given scale - * - * @param {vec3} out the receiving vector - * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned - * @returns {vec3} out - */ -vec3.random = function (out, scale) { - scale = scale || 1.0; - - var r = glMatrix.RANDOM() * 2.0 * Math.PI; - var z = (glMatrix.RANDOM() * 2.0) - 1.0; - var zScale = Math.sqrt(1.0-z*z) * scale; - - out[0] = Math.cos(r) * zScale; - out[1] = Math.sin(r) * zScale; - out[2] = z * scale; - return out; -}; - -/** - * Transforms the vec3 with a mat4. - * 4th vector component is implicitly '1' - * - * @param {vec3} out the receiving vector - * @param {vec3} a the vector to transform - * @param {mat4} m matrix to transform with - * @returns {vec3} out - */ -vec3.transformMat4 = function(out, a, m) { - var x = a[0], y = a[1], z = a[2], - w = m[3] * x + m[7] * y + m[11] * z + m[15]; - w = w || 1.0; - out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; - out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; - out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w; - return out; -}; - -/** - * Transforms the vec3 with a mat3. - * - * @param {vec3} out the receiving vector - * @param {vec3} a the vector to transform - * @param {mat4} m the 3x3 matrix to transform with - * @returns {vec3} out - */ -vec3.transformMat3 = function(out, a, m) { - var x = a[0], y = a[1], z = a[2]; - out[0] = x * m[0] + y * m[3] + z * m[6]; - out[1] = x * m[1] + y * m[4] + z * m[7]; - out[2] = x * m[2] + y * m[5] + z * m[8]; - return out; -}; - -/** - * Transforms the vec3 with a quat - * - * @param {vec3} out the receiving vector - * @param {vec3} a the vector to transform - * @param {quat} q quaternion to transform with - * @returns {vec3} out - */ -vec3.transformQuat = function(out, a, q) { - // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations - - var x = a[0], y = a[1], z = a[2], - qx = q[0], qy = q[1], qz = q[2], qw = q[3], - - // calculate quat * vec - ix = qw * x + qy * z - qz * y, - iy = qw * y + qz * x - qx * z, - iz = qw * z + qx * y - qy * x, - iw = -qx * x - qy * y - qz * z; - - // calculate result * inverse quat - out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; - out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; - out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; - return out; -}; - -/** - * Rotate a 3D vector around the x-axis - * @param {vec3} out The receiving vec3 - * @param {vec3} a The vec3 point to rotate - * @param {vec3} b The origin of the rotation - * @param {Number} c The angle of rotation - * @returns {vec3} out - */ -vec3.rotateX = function(out, a, b, c){ - var p = [], r=[]; - //Translate point to the origin - p[0] = a[0] - b[0]; - p[1] = a[1] - b[1]; - p[2] = a[2] - b[2]; - - //perform rotation - r[0] = p[0]; - r[1] = p[1]*Math.cos(c) - p[2]*Math.sin(c); - r[2] = p[1]*Math.sin(c) + p[2]*Math.cos(c); - - //translate to correct position - out[0] = r[0] + b[0]; - out[1] = r[1] + b[1]; - out[2] = r[2] + b[2]; - - return out; -}; - -/** - * Rotate a 3D vector around the y-axis - * @param {vec3} out The receiving vec3 - * @param {vec3} a The vec3 point to rotate - * @param {vec3} b The origin of the rotation - * @param {Number} c The angle of rotation - * @returns {vec3} out - */ -vec3.rotateY = function(out, a, b, c){ - var p = [], r=[]; - //Translate point to the origin - p[0] = a[0] - b[0]; - p[1] = a[1] - b[1]; - p[2] = a[2] - b[2]; - - //perform rotation - r[0] = p[2]*Math.sin(c) + p[0]*Math.cos(c); - r[1] = p[1]; - r[2] = p[2]*Math.cos(c) - p[0]*Math.sin(c); - - //translate to correct position - out[0] = r[0] + b[0]; - out[1] = r[1] + b[1]; - out[2] = r[2] + b[2]; - - return out; -}; - -/** - * Rotate a 3D vector around the z-axis - * @param {vec3} out The receiving vec3 - * @param {vec3} a The vec3 point to rotate - * @param {vec3} b The origin of the rotation - * @param {Number} c The angle of rotation - * @returns {vec3} out - */ -vec3.rotateZ = function(out, a, b, c){ - var p = [], r=[]; - //Translate point to the origin - p[0] = a[0] - b[0]; - p[1] = a[1] - b[1]; - p[2] = a[2] - b[2]; - - //perform rotation - r[0] = p[0]*Math.cos(c) - p[1]*Math.sin(c); - r[1] = p[0]*Math.sin(c) + p[1]*Math.cos(c); - r[2] = p[2]; - - //translate to correct position - out[0] = r[0] + b[0]; - out[1] = r[1] + b[1]; - out[2] = r[2] + b[2]; - - return out; -}; - -/** - * Perform some operation over an array of vec3s. - * - * @param {Array} a the array of vectors to iterate over - * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed - * @param {Number} offset Number of elements to skip at the beginning of the array - * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array - * @param {Function} fn Function to call for each vector in the array - * @param {Object} [arg] additional argument to pass to fn - * @returns {Array} a - * @function - */ -vec3.forEach = (function() { - var vec = vec3.create(); - - return function(a, stride, offset, count, fn, arg) { - var i, l; - if(!stride) { - stride = 3; - } - - if(!offset) { - offset = 0; - } - - if(count) { - l = Math.min((count * stride) + offset, a.length); - } else { - l = a.length; - } - - for(i = offset; i < l; i += stride) { - vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; - fn(vec, vec, arg); - a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; - } - - return a; - }; -})(); - -/** - * Get the angle between two 3D vectors - * @param {vec3} a The first operand - * @param {vec3} b The second operand - * @returns {Number} The angle in radians - */ -vec3.angle = function(a, b) { - - var tempA = vec3.fromValues(a[0], a[1], a[2]); - var tempB = vec3.fromValues(b[0], b[1], b[2]); - - vec3.normalize(tempA, tempA); - vec3.normalize(tempB, tempB); - - var cosine = vec3.dot(tempA, tempB); - - if(cosine > 1.0){ - return 0; - } else { - return Math.acos(cosine); - } -}; - -/** - * Returns a string representation of a vector - * - * @param {vec3} vec vector to represent as a string - * @returns {String} string representation of the vector - */ -vec3.str = function (a) { - return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')'; -}; - -/** - * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) - * - * @param {vec3} a The first vector. - * @param {vec3} b The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -vec3.exactEquals = function (a, b) { - return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; -}; - -/** - * Returns whether or not the vectors have approximately the same elements in the same position. - * - * @param {vec3} a The first vector. - * @param {vec3} b The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -vec3.equals = function (a, b) { - var a0 = a[0], a1 = a[1], a2 = a[2]; - var b0 = b[0], b1 = b[1], b2 = b[2]; - return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && - Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2))); -}; - -module.exports = vec3; - -},{"./common.js":1032}],1040:[function(require,module,exports){ -/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. */ - -var glMatrix = require("./common.js"); - -/** - * @class 4 Dimensional Vector - * @name vec4 - */ -var vec4 = {}; - -/** - * Creates a new, empty vec4 - * - * @returns {vec4} a new 4D vector - */ -vec4.create = function() { - var out = new glMatrix.ARRAY_TYPE(4); - out[0] = 0; - out[1] = 0; - out[2] = 0; - out[3] = 0; - return out; -}; - -/** - * Creates a new vec4 initialized with values from an existing vector - * - * @param {vec4} a vector to clone - * @returns {vec4} a new 4D vector - */ -vec4.clone = function(a) { - var out = new glMatrix.ARRAY_TYPE(4); - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - return out; -}; - -/** - * Creates a new vec4 initialized with the given values - * - * @param {Number} x X component - * @param {Number} y Y component - * @param {Number} z Z component - * @param {Number} w W component - * @returns {vec4} a new 4D vector - */ -vec4.fromValues = function(x, y, z, w) { - var out = new glMatrix.ARRAY_TYPE(4); - out[0] = x; - out[1] = y; - out[2] = z; - out[3] = w; - return out; -}; - -/** - * Copy the values from one vec4 to another - * - * @param {vec4} out the receiving vector - * @param {vec4} a the source vector - * @returns {vec4} out - */ -vec4.copy = function(out, a) { - out[0] = a[0]; - out[1] = a[1]; - out[2] = a[2]; - out[3] = a[3]; - return out; -}; - -/** - * Set the components of a vec4 to the given values - * - * @param {vec4} out the receiving vector - * @param {Number} x X component - * @param {Number} y Y component - * @param {Number} z Z component - * @param {Number} w W component - * @returns {vec4} out - */ -vec4.set = function(out, x, y, z, w) { - out[0] = x; - out[1] = y; - out[2] = z; - out[3] = w; - return out; -}; - -/** - * Adds two vec4's - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {vec4} out - */ -vec4.add = function(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]; - return out; -}; - -/** - * Subtracts vector b from vector a - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {vec4} out - */ -vec4.subtract = function(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]; - return out; -}; - -/** - * Alias for {@link vec4.subtract} - * @function - */ -vec4.sub = vec4.subtract; - -/** - * Multiplies two vec4's - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {vec4} out - */ -vec4.multiply = function(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]; - return out; -}; - -/** - * Alias for {@link vec4.multiply} - * @function - */ -vec4.mul = vec4.multiply; - -/** - * Divides two vec4's - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {vec4} out - */ -vec4.divide = function(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]; - return out; -}; - -/** - * Alias for {@link vec4.divide} - * @function - */ -vec4.div = vec4.divide; - -/** - * Math.ceil the components of a vec4 - * - * @param {vec4} out the receiving vector - * @param {vec4} a vector to ceil - * @returns {vec4} out - */ -vec4.ceil = function (out, a) { - out[0] = Math.ceil(a[0]); - out[1] = Math.ceil(a[1]); - out[2] = Math.ceil(a[2]); - out[3] = Math.ceil(a[3]); - return out; -}; - -/** - * Math.floor the components of a vec4 - * - * @param {vec4} out the receiving vector - * @param {vec4} a vector to floor - * @returns {vec4} out - */ -vec4.floor = function (out, a) { - out[0] = Math.floor(a[0]); - out[1] = Math.floor(a[1]); - out[2] = Math.floor(a[2]); - out[3] = Math.floor(a[3]); - return out; -}; - -/** - * Returns the minimum of two vec4's - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {vec4} out - */ -vec4.min = function(out, a, b) { - out[0] = Math.min(a[0], b[0]); - out[1] = Math.min(a[1], b[1]); - out[2] = Math.min(a[2], b[2]); - out[3] = Math.min(a[3], b[3]); - return out; -}; - -/** - * Returns the maximum of two vec4's - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {vec4} out - */ -vec4.max = function(out, a, b) { - out[0] = Math.max(a[0], b[0]); - out[1] = Math.max(a[1], b[1]); - out[2] = Math.max(a[2], b[2]); - out[3] = Math.max(a[3], b[3]); - return out; -}; - -/** - * Math.round the components of a vec4 - * - * @param {vec4} out the receiving vector - * @param {vec4} a vector to round - * @returns {vec4} out - */ -vec4.round = function (out, a) { - out[0] = Math.round(a[0]); - out[1] = Math.round(a[1]); - out[2] = Math.round(a[2]); - out[3] = Math.round(a[3]); - return out; -}; - -/** - * Scales a vec4 by a scalar number - * - * @param {vec4} out the receiving vector - * @param {vec4} a the vector to scale - * @param {Number} b amount to scale the vector by - * @returns {vec4} out - */ -vec4.scale = function(out, a, b) { - out[0] = a[0] * b; - out[1] = a[1] * b; - out[2] = a[2] * b; - out[3] = a[3] * b; - return out; -}; - -/** - * Adds two vec4's after scaling the second operand by a scalar value - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @param {Number} scale the amount to scale b by before adding - * @returns {vec4} out - */ -vec4.scaleAndAdd = function(out, a, b, scale) { - out[0] = a[0] + (b[0] * scale); - out[1] = a[1] + (b[1] * scale); - out[2] = a[2] + (b[2] * scale); - out[3] = a[3] + (b[3] * scale); - return out; -}; - -/** - * Calculates the euclidian distance between two vec4's - * - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {Number} distance between a and b - */ -vec4.distance = function(a, b) { - var x = b[0] - a[0], - y = b[1] - a[1], - z = b[2] - a[2], - w = b[3] - a[3]; - return Math.sqrt(x*x + y*y + z*z + w*w); -}; - -/** - * Alias for {@link vec4.distance} - * @function - */ -vec4.dist = vec4.distance; - -/** - * Calculates the squared euclidian distance between two vec4's - * - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {Number} squared distance between a and b - */ -vec4.squaredDistance = function(a, b) { - var x = b[0] - a[0], - y = b[1] - a[1], - z = b[2] - a[2], - w = b[3] - a[3]; - return x*x + y*y + z*z + w*w; -}; - -/** - * Alias for {@link vec4.squaredDistance} - * @function - */ -vec4.sqrDist = vec4.squaredDistance; - -/** - * Calculates the length of a vec4 - * - * @param {vec4} a vector to calculate length of - * @returns {Number} length of a - */ -vec4.length = function (a) { - var x = a[0], - y = a[1], - z = a[2], - w = a[3]; - return Math.sqrt(x*x + y*y + z*z + w*w); -}; - -/** - * Alias for {@link vec4.length} - * @function - */ -vec4.len = vec4.length; - -/** - * Calculates the squared length of a vec4 - * - * @param {vec4} a vector to calculate squared length of - * @returns {Number} squared length of a - */ -vec4.squaredLength = function (a) { - var x = a[0], - y = a[1], - z = a[2], - w = a[3]; - return x*x + y*y + z*z + w*w; -}; - -/** - * Alias for {@link vec4.squaredLength} - * @function - */ -vec4.sqrLen = vec4.squaredLength; - -/** - * Negates the components of a vec4 - * - * @param {vec4} out the receiving vector - * @param {vec4} a vector to negate - * @returns {vec4} out - */ -vec4.negate = function(out, a) { - out[0] = -a[0]; - out[1] = -a[1]; - out[2] = -a[2]; - out[3] = -a[3]; - return out; -}; - -/** - * Returns the inverse of the components of a vec4 - * - * @param {vec4} out the receiving vector - * @param {vec4} a vector to invert - * @returns {vec4} out - */ -vec4.inverse = function(out, a) { - out[0] = 1.0 / a[0]; - out[1] = 1.0 / a[1]; - out[2] = 1.0 / a[2]; - out[3] = 1.0 / a[3]; - return out; -}; - -/** - * Normalize a vec4 - * - * @param {vec4} out the receiving vector - * @param {vec4} a vector to normalize - * @returns {vec4} out - */ -vec4.normalize = function(out, a) { - var x = a[0], - y = a[1], - z = a[2], - w = a[3]; - var len = x*x + y*y + z*z + w*w; - if (len > 0) { - len = 1 / Math.sqrt(len); - out[0] = x * len; - out[1] = y * len; - out[2] = z * len; - out[3] = w * len; - } - return out; -}; - -/** - * Calculates the dot product of two vec4's - * - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @returns {Number} dot product of a and b - */ -vec4.dot = function (a, b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; -}; - -/** - * Performs a linear interpolation between two vec4's - * - * @param {vec4} out the receiving vector - * @param {vec4} a the first operand - * @param {vec4} b the second operand - * @param {Number} t interpolation amount between the two inputs - * @returns {vec4} out - */ -vec4.lerp = function (out, a, b, t) { - var ax = a[0], - ay = a[1], - az = a[2], - aw = a[3]; - out[0] = ax + t * (b[0] - ax); - out[1] = ay + t * (b[1] - ay); - out[2] = az + t * (b[2] - az); - out[3] = aw + t * (b[3] - aw); - return out; -}; - -/** - * Generates a random vector with the given scale - * - * @param {vec4} out the receiving vector - * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned - * @returns {vec4} out - */ -vec4.random = function (out, scale) { - scale = scale || 1.0; - - //TODO: This is a pretty awful way of doing this. Find something better. - out[0] = glMatrix.RANDOM(); - out[1] = glMatrix.RANDOM(); - out[2] = glMatrix.RANDOM(); - out[3] = glMatrix.RANDOM(); - vec4.normalize(out, out); - vec4.scale(out, out, scale); - return out; -}; - -/** - * Transforms the vec4 with a mat4. - * - * @param {vec4} out the receiving vector - * @param {vec4} a the vector to transform - * @param {mat4} m matrix to transform with - * @returns {vec4} out - */ -vec4.transformMat4 = function(out, a, m) { - var x = a[0], y = a[1], z = a[2], w = a[3]; - out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; - out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; - out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; - out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; - return out; -}; - -/** - * Transforms the vec4 with a quat - * - * @param {vec4} out the receiving vector - * @param {vec4} a the vector to transform - * @param {quat} q quaternion to transform with - * @returns {vec4} out - */ -vec4.transformQuat = function(out, a, q) { - var x = a[0], y = a[1], z = a[2], - qx = q[0], qy = q[1], qz = q[2], qw = q[3], - - // calculate quat * vec - ix = qw * x + qy * z - qz * y, - iy = qw * y + qz * x - qx * z, - iz = qw * z + qx * y - qy * x, - iw = -qx * x - qy * y - qz * z; - - // calculate result * inverse quat - out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; - out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; - out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; - out[3] = a[3]; - return out; -}; - -/** - * Perform some operation over an array of vec4s. - * - * @param {Array} a the array of vectors to iterate over - * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed - * @param {Number} offset Number of elements to skip at the beginning of the array - * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array - * @param {Function} fn Function to call for each vector in the array - * @param {Object} [arg] additional argument to pass to fn - * @returns {Array} a - * @function - */ -vec4.forEach = (function() { - var vec = vec4.create(); - - return function(a, stride, offset, count, fn, arg) { - var i, l; - if(!stride) { - stride = 4; - } - - if(!offset) { - offset = 0; - } - - if(count) { - l = Math.min((count * stride) + offset, a.length); - } else { - l = a.length; - } - - for(i = offset; i < l; i += stride) { - vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; vec[3] = a[i+3]; - fn(vec, vec, arg); - a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; a[i+3] = vec[3]; - } - - return a; - }; -})(); - -/** - * Returns a string representation of a vector - * - * @param {vec4} vec vector to represent as a string - * @returns {String} string representation of the vector - */ -vec4.str = function (a) { - return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; -}; - -/** - * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) - * - * @param {vec4} a The first vector. - * @param {vec4} b The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -vec4.exactEquals = function (a, b) { - return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; -}; - -/** - * Returns whether or not the vectors have approximately the same elements in the same position. - * - * @param {vec4} a The first vector. - * @param {vec4} b The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ -vec4.equals = function (a, b) { - var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; - var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; - return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && - Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && - Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3))); -}; - -module.exports = vec4; - -},{"./common.js":1032}],1041:[function(require,module,exports){ -'use strict'; - -module.exports = GridIndex; - -var NUM_PARAMS = 3; - -function GridIndex(extent, n, padding) { - var cells = this.cells = []; - - if (extent instanceof ArrayBuffer) { - this.arrayBuffer = extent; - var array = new Int32Array(this.arrayBuffer); - extent = array[0]; - n = array[1]; - padding = array[2]; - - this.d = n + 2 * padding; - for (var k = 0; k < this.d * this.d; k++) { - var start = array[NUM_PARAMS + k]; - var end = array[NUM_PARAMS + k + 1]; - cells.push(start === end ? - null : - array.subarray(start, end)); - } - var keysOffset = array[NUM_PARAMS + cells.length]; - var bboxesOffset = array[NUM_PARAMS + cells.length + 1]; - this.keys = array.subarray(keysOffset, bboxesOffset); - this.bboxes = array.subarray(bboxesOffset); - - this.insert = this._insertReadonly; - - } else { - this.d = n + 2 * padding; - for (var i = 0; i < this.d * this.d; i++) { - cells.push([]); - } - this.keys = []; - this.bboxes = []; - } - - this.n = n; - this.extent = extent; - this.padding = padding; - this.scale = n / extent; - this.uid = 0; - - var p = (padding / n) * extent; - this.min = -p; - this.max = extent + p; -} - - -GridIndex.prototype.insert = function(key, x1, y1, x2, y2) { - this._forEachCell(x1, y1, x2, y2, this._insertCell, this.uid++); - this.keys.push(key); - this.bboxes.push(x1); - this.bboxes.push(y1); - this.bboxes.push(x2); - this.bboxes.push(y2); -}; - -GridIndex.prototype._insertReadonly = function() { - throw 'Cannot insert into a GridIndex created from an ArrayBuffer.'; -}; - -GridIndex.prototype._insertCell = function(x1, y1, x2, y2, cellIndex, uid) { - this.cells[cellIndex].push(uid); -}; - -GridIndex.prototype.query = function(x1, y1, x2, y2) { - var min = this.min; - var max = this.max; - if (x1 <= min && y1 <= min && max <= x2 && max <= y2) { - // We use `Array#slice` because `this.keys` may be a `Int32Array` and - // some browsers (Safari and IE) do not support `TypedArray#slice` - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice#Browser_compatibility - return Array.prototype.slice.call(this.keys); - - } else { - var result = []; - var seenUids = {}; - this._forEachCell(x1, y1, x2, y2, this._queryCell, result, seenUids); - return result; - } -}; - -GridIndex.prototype._queryCell = function(x1, y1, x2, y2, cellIndex, result, seenUids) { - var cell = this.cells[cellIndex]; - if (cell !== null) { - var keys = this.keys; - var bboxes = this.bboxes; - for (var u = 0; u < cell.length; u++) { - var uid = cell[u]; - if (seenUids[uid] === undefined) { - var offset = uid * 4; - if ((x1 <= bboxes[offset + 2]) && - (y1 <= bboxes[offset + 3]) && - (x2 >= bboxes[offset + 0]) && - (y2 >= bboxes[offset + 1])) { - seenUids[uid] = true; - result.push(keys[uid]); - } else { - seenUids[uid] = false; - } - } - } - } -}; - -GridIndex.prototype._forEachCell = function(x1, y1, x2, y2, fn, arg1, arg2) { - var cx1 = this._convertToCellCoord(x1); - var cy1 = this._convertToCellCoord(y1); - var cx2 = this._convertToCellCoord(x2); - var cy2 = this._convertToCellCoord(y2); - for (var x = cx1; x <= cx2; x++) { - for (var y = cy1; y <= cy2; y++) { - var cellIndex = this.d * y + x; - if (fn.call(this, x1, y1, x2, y2, cellIndex, arg1, arg2)) return; - } - } -}; - -GridIndex.prototype._convertToCellCoord = function(x) { - return Math.max(0, Math.min(this.d - 1, Math.floor(x * this.scale) + this.padding)); -}; - -GridIndex.prototype.toArrayBuffer = function() { - if (this.arrayBuffer) return this.arrayBuffer; - - var cells = this.cells; - - var metadataLength = NUM_PARAMS + this.cells.length + 1 + 1; - var totalCellLength = 0; - for (var i = 0; i < this.cells.length; i++) { - totalCellLength += this.cells[i].length; - } - - var array = new Int32Array(metadataLength + totalCellLength + this.keys.length + this.bboxes.length); - array[0] = this.extent; - array[1] = this.n; - array[2] = this.padding; - - var offset = metadataLength; - for (var k = 0; k < cells.length; k++) { - var cell = cells[k]; - array[NUM_PARAMS + k] = offset; - array.set(cell, offset); - offset += cell.length; - } - - array[NUM_PARAMS + cells.length] = offset; - array.set(this.keys, offset); - offset += this.keys.length; - - array[NUM_PARAMS + cells.length + 1] = offset; - array.set(this.bboxes, offset); - offset += this.bboxes.length; - - return array.buffer; -}; - -},{}],1042:[function(require,module,exports){ -'use strict'; - -function createFunction(parameters, defaultType) { - var fun; - - if (!isFunctionDefinition(parameters)) { - fun = function() { return parameters; }; - fun.isFeatureConstant = true; - fun.isZoomConstant = true; - - } else { - var zoomAndFeatureDependent = parameters.stops && typeof parameters.stops[0][0] === 'object'; - var featureDependent = zoomAndFeatureDependent || parameters.property !== undefined; - var zoomDependent = zoomAndFeatureDependent || !featureDependent; - var type = parameters.type || defaultType || 'exponential'; - - var innerFun; - if (type === 'exponential') { - innerFun = evaluateExponentialFunction; - } else if (type === 'interval') { - innerFun = evaluateIntervalFunction; - } else if (type === 'categorical') { - innerFun = evaluateCategoricalFunction; - } else if (type === 'identity') { - innerFun = evaluateIdentityFunction; - } else { - throw new Error('Unknown function type "' + type + '"'); - } - - if (zoomAndFeatureDependent) { - var featureFunctions = {}; - var featureFunctionStops = []; - for (var s = 0; s < parameters.stops.length; s++) { - var stop = parameters.stops[s]; - if (featureFunctions[stop[0].zoom] === undefined) { - featureFunctions[stop[0].zoom] = { - zoom: stop[0].zoom, - type: parameters.type, - property: parameters.property, - stops: [] - }; - } - featureFunctions[stop[0].zoom].stops.push([stop[0].value, stop[1]]); - } - - for (var z in featureFunctions) { - featureFunctionStops.push([featureFunctions[z].zoom, createFunction(featureFunctions[z])]); - } - fun = function(zoom, feature) { - return evaluateExponentialFunction({ stops: featureFunctionStops, base: parameters.base }, zoom)(zoom, feature); - }; - fun.isFeatureConstant = false; - fun.isZoomConstant = false; - - } else if (zoomDependent) { - fun = function(zoom) { - return innerFun(parameters, zoom); - }; - fun.isFeatureConstant = true; - fun.isZoomConstant = false; - } else { - fun = function(zoom, feature) { - return innerFun(parameters, feature[parameters.property]); - }; - fun.isFeatureConstant = false; - fun.isZoomConstant = true; - } - } - - return fun; -} - -function evaluateCategoricalFunction(parameters, input) { - for (var i = 0; i < parameters.stops.length; i++) { - if (input === parameters.stops[i][0]) { - return parameters.stops[i][1]; - } - } - return parameters.stops[0][1]; -} - -function evaluateIntervalFunction(parameters, input) { - for (var i = 0; i < parameters.stops.length; i++) { - if (input < parameters.stops[i][0]) break; - } - return parameters.stops[Math.max(i - 1, 0)][1]; -} - -function evaluateExponentialFunction(parameters, input) { - var base = parameters.base !== undefined ? parameters.base : 1; - - var i = 0; - while (true) { - if (i >= parameters.stops.length) break; - else if (input <= parameters.stops[i][0]) break; - else i++; - } - - if (i === 0) { - return parameters.stops[i][1]; - - } else if (i === parameters.stops.length) { - return parameters.stops[i - 1][1]; - - } else { - return interpolate( - input, - base, - parameters.stops[i - 1][0], - parameters.stops[i][0], - parameters.stops[i - 1][1], - parameters.stops[i][1] - ); - } -} - -function evaluateIdentityFunction(parameters, input) { - return input; -} - - -function interpolate(input, base, inputLower, inputUpper, outputLower, outputUpper) { - if (typeof outputLower === 'function') { - return function() { - var evaluatedLower = outputLower.apply(undefined, arguments); - var evaluatedUpper = outputUpper.apply(undefined, arguments); - return interpolate(input, base, inputLower, inputUpper, evaluatedLower, evaluatedUpper); - }; - } else if (outputLower.length) { - return interpolateArray(input, base, inputLower, inputUpper, outputLower, outputUpper); - } else { - return interpolateNumber(input, base, inputLower, inputUpper, outputLower, outputUpper); - } -} - -function interpolateNumber(input, base, inputLower, inputUpper, outputLower, outputUpper) { - var difference = inputUpper - inputLower; - var progress = input - inputLower; - - var ratio; - if (base === 1) { - ratio = progress / difference; - } else { - ratio = (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1); - } - - return (outputLower * (1 - ratio)) + (outputUpper * ratio); -} - -function interpolateArray(input, base, inputLower, inputUpper, outputLower, outputUpper) { - var output = []; - for (var i = 0; i < outputLower.length; i++) { - output[i] = interpolateNumber(input, base, inputLower, inputUpper, outputLower[i], outputUpper[i]); - } - return output; -} - -function isFunctionDefinition(value) { - return typeof value === 'object' && (value.stops || value.type === 'identity'); -} - - -module.exports.isFunctionDefinition = isFunctionDefinition; - -module.exports.interpolated = function(parameters) { - return createFunction(parameters, 'exponential'); -}; - -module.exports['piecewise-constant'] = function(parameters) { - return createFunction(parameters, 'interval'); -}; - -},{}],1043:[function(require,module,exports){ - -var path = require('path'); - -// readFileSync calls must be written out long-form for brfs. -module.exports = { - debug: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform lowp vec4 u_color;\n\nvoid main() {\n gl_FragColor = u_color;\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\n\nuniform mat4 u_matrix;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, step(32767.0, a_pos.x), 1);\n}\n" - }, - fill: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n#pragma mapbox: define lowp vec4 color\n#pragma mapbox: define lowp float opacity\n\nvoid main() {\n #pragma mapbox: initialize lowp vec4 color\n #pragma mapbox: initialize lowp float opacity\n\n gl_FragColor = color * opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\n\nuniform mat4 u_matrix;\n\n#pragma mapbox: define lowp vec4 color\n#pragma mapbox: define lowp float opacity\n\nvoid main() {\n #pragma mapbox: initialize lowp vec4 color\n #pragma mapbox: initialize lowp float opacity\n\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n}\n" - }, - circle: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n#pragma mapbox: define lowp vec4 color\n#pragma mapbox: define lowp float blur\n#pragma mapbox: define lowp float opacity\n\nvarying vec2 v_extrude;\nvarying lowp float v_antialiasblur;\n\nvoid main() {\n #pragma mapbox: initialize lowp vec4 color\n #pragma mapbox: initialize lowp float blur\n #pragma mapbox: initialize lowp float opacity\n\n float t = smoothstep(1.0 - max(blur, v_antialiasblur), 1.0, length(v_extrude));\n gl_FragColor = color * (1.0 - t) * opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform mat4 u_matrix;\nuniform bool u_scale_with_map;\nuniform vec2 u_extrude_scale;\nuniform float u_devicepixelratio;\n\nattribute vec2 a_pos;\n\n#pragma mapbox: define lowp vec4 color\n#pragma mapbox: define mediump float radius\n#pragma mapbox: define lowp float blur\n#pragma mapbox: define lowp float opacity\n\nvarying vec2 v_extrude;\nvarying lowp float v_antialiasblur;\n\nvoid main(void) {\n #pragma mapbox: initialize lowp vec4 color\n #pragma mapbox: initialize mediump float radius\n #pragma mapbox: initialize lowp float blur\n #pragma mapbox: initialize lowp float opacity\n\n // unencode the extrusion vector that we snuck into the a_pos vector\n v_extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);\n\n vec2 extrude = v_extrude * radius * u_extrude_scale;\n // multiply a_pos by 0.5, since we had it * 2 in order to sneak\n // in extrusion data\n gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0, 1);\n\n if (u_scale_with_map) {\n gl_Position.xy += extrude;\n } else {\n gl_Position.xy += extrude * gl_Position.w;\n }\n\n // This is a minimum blur distance that serves as a faux-antialiasing for\n // the circle. since blur is a ratio of the circle's size and the intent is\n // to keep the blur at roughly 1px, the two are inversely related.\n v_antialiasblur = 1.0 / u_devicepixelratio / radius;\n}\n" - }, - line: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform lowp vec4 u_color;\nuniform lowp float u_opacity;\nuniform float u_blur;\n\nvarying vec2 v_linewidth;\nvarying vec2 v_normal;\nvarying float v_gamma_scale;\n\nvoid main() {\n // Calculate the distance of the pixel from the line in pixels.\n float dist = length(v_normal) * v_linewidth.s;\n\n // Calculate the antialiasing fade factor. This is either when fading in\n // the line in case of an offset line (v_linewidth.t) or when fading out\n // (v_linewidth.s)\n float blur = u_blur * v_gamma_scale;\n float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);\n\n gl_FragColor = u_color * (alpha * u_opacity);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n// floor(127 / 2) == 63.0\n// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is\n// stored in a byte (-128..127). we scale regular normals up to length 63, but\n// there are also \"special\" normals that have a bigger length (of up to 126 in\n// this case).\n// #define scale 63.0\n#define scale 0.015873016\n\nattribute vec2 a_pos;\nattribute vec4 a_data;\n\nuniform mat4 u_matrix;\nuniform mediump float u_ratio;\nuniform mediump float u_linewidth;\nuniform mediump float u_gapwidth;\nuniform mediump float u_antialiasing;\nuniform mediump float u_extra;\nuniform mat2 u_antialiasingmatrix;\nuniform mediump float u_offset;\nuniform mediump float u_blur;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying float v_gamma_scale;\n\nvoid main() {\n vec2 a_extrude = a_data.xy - 128.0;\n float a_direction = mod(a_data.z, 4.0) - 1.0;\n\n // We store the texture normals in the most insignificant bit\n // transform y so that 0 => -1 and 1 => 1\n // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap\n // y is 1 if the normal points up, and -1 if it points down\n mediump vec2 normal = mod(a_pos, 2.0);\n normal.y = sign(normal.y - 0.5);\n v_normal = normal;\n\n float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0);\n float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing;\n\n // Scale the extrusion vector down to a normal and then up by the line width\n // of this vertex.\n mediump vec2 dist = outset * a_extrude * scale;\n\n // Calculate the offset when drawing a line that is to the side of the actual line.\n // We do this by creating a vector that points towards the extrude, but rotate\n // it when we're drawing round end points (a_direction = -1 or 1) since their\n // extrude vector points in another direction.\n mediump float u = 0.5 * a_direction;\n mediump float t = 1.0 - abs(u);\n mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);\n\n // Remove the texture normal bit of the position before scaling it with the\n // model/view matrix.\n gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);\n\n // position of y on the screen\n float y = gl_Position.y / gl_Position.w;\n\n // how much features are squished in the y direction by the tilt\n float squish_scale = length(a_extrude) / length(u_antialiasingmatrix * a_extrude);\n\n // how much features are squished in all directions by the perspectiveness\n float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9));\n\n v_linewidth = vec2(outset, inset);\n v_gamma_scale = perspective_scale * squish_scale;\n}\n" - }, - linepattern: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_blur;\n\nuniform vec2 u_pattern_size_a;\nuniform vec2 u_pattern_size_b;\nuniform vec2 u_pattern_tl_a;\nuniform vec2 u_pattern_br_a;\nuniform vec2 u_pattern_tl_b;\nuniform vec2 u_pattern_br_b;\nuniform float u_fade;\nuniform float u_opacity;\n\nuniform sampler2D u_image;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying float v_linesofar;\nvarying float v_gamma_scale;\n\nvoid main() {\n // Calculate the distance of the pixel from the line in pixels.\n float dist = length(v_normal) * v_linewidth.s;\n\n // Calculate the antialiasing fade factor. This is either when fading in\n // the line in case of an offset line (v_linewidth.t) or when fading out\n // (v_linewidth.s)\n float blur = u_blur * v_gamma_scale;\n float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);\n\n float x_a = mod(v_linesofar / u_pattern_size_a.x, 1.0);\n float x_b = mod(v_linesofar / u_pattern_size_b.x, 1.0);\n float y_a = 0.5 + (v_normal.y * v_linewidth.s / u_pattern_size_a.y);\n float y_b = 0.5 + (v_normal.y * v_linewidth.s / u_pattern_size_b.y);\n vec2 pos_a = mix(u_pattern_tl_a, u_pattern_br_a, vec2(x_a, y_a));\n vec2 pos_b = mix(u_pattern_tl_b, u_pattern_br_b, vec2(x_b, y_b));\n\n vec4 color = mix(texture2D(u_image, pos_a), texture2D(u_image, pos_b), u_fade);\n\n alpha *= u_opacity;\n\n gl_FragColor = color * alpha;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n// floor(127 / 2) == 63.0\n// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is\n// stored in a byte (-128..127). we scale regular normals up to length 63, but\n// there are also \"special\" normals that have a bigger length (of up to 126 in\n// this case).\n// #define scale 63.0\n#define scale 0.015873016\n\n// We scale the distance before adding it to the buffers so that we can store\n// long distances for long segments. Use this value to unscale the distance.\n#define LINE_DISTANCE_SCALE 2.0\n\nattribute vec2 a_pos;\nattribute vec4 a_data;\n\nuniform mat4 u_matrix;\nuniform mediump float u_ratio;\nuniform mediump float u_linewidth;\nuniform mediump float u_gapwidth;\nuniform mediump float u_antialiasing;\nuniform mediump float u_extra;\nuniform mat2 u_antialiasingmatrix;\nuniform mediump float u_offset;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying float v_linesofar;\nvarying float v_gamma_scale;\n\nvoid main() {\n vec2 a_extrude = a_data.xy - 128.0;\n float a_direction = mod(a_data.z, 4.0) - 1.0;\n float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE;\n\n // We store the texture normals in the most insignificant bit\n // transform y so that 0 => -1 and 1 => 1\n // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap\n // y is 1 if the normal points up, and -1 if it points down\n mediump vec2 normal = mod(a_pos, 2.0);\n normal.y = sign(normal.y - 0.5);\n v_normal = normal;\n\n float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0);\n float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing;\n\n // Scale the extrusion vector down to a normal and then up by the line width\n // of this vertex.\n mediump vec2 dist = outset * a_extrude * scale;\n\n // Calculate the offset when drawing a line that is to the side of the actual line.\n // We do this by creating a vector that points towards the extrude, but rotate\n // it when we're drawing round end points (a_direction = -1 or 1) since their\n // extrude vector points in another direction.\n mediump float u = 0.5 * a_direction;\n mediump float t = 1.0 - abs(u);\n mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);\n\n // Remove the texture normal bit of the position before scaling it with the\n // model/view matrix.\n gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);\n v_linesofar = a_linesofar;\n\n // position of y on the screen\n float y = gl_Position.y / gl_Position.w;\n\n // how much features are squished in the y direction by the tilt\n float squish_scale = length(a_extrude) / length(u_antialiasingmatrix * a_extrude);\n\n // how much features are squished in all directions by the perspectiveness\n float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9));\n\n v_linewidth = vec2(outset, inset);\n v_gamma_scale = perspective_scale * squish_scale;\n}\n" - }, - linesdfpattern: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform lowp vec4 u_color;\nuniform lowp float u_opacity;\n\nuniform float u_blur;\nuniform sampler2D u_image;\nuniform float u_sdfgamma;\nuniform float u_mix;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying vec2 v_tex_a;\nvarying vec2 v_tex_b;\nvarying float v_gamma_scale;\n\nvoid main() {\n // Calculate the distance of the pixel from the line in pixels.\n float dist = length(v_normal) * v_linewidth.s;\n\n // Calculate the antialiasing fade factor. This is either when fading in\n // the line in case of an offset line (v_linewidth.t) or when fading out\n // (v_linewidth.s)\n float blur = u_blur * v_gamma_scale;\n float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);\n\n float sdfdist_a = texture2D(u_image, v_tex_a).a;\n float sdfdist_b = texture2D(u_image, v_tex_b).a;\n float sdfdist = mix(sdfdist_a, sdfdist_b, u_mix);\n alpha *= smoothstep(0.5 - u_sdfgamma, 0.5 + u_sdfgamma, sdfdist);\n\n gl_FragColor = u_color * (alpha * u_opacity);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n// floor(127 / 2) == 63.0\n// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is\n// stored in a byte (-128..127). we scale regular normals up to length 63, but\n// there are also \"special\" normals that have a bigger length (of up to 126 in\n// this case).\n// #define scale 63.0\n#define scale 0.015873016\n\n// We scale the distance before adding it to the buffers so that we can store\n// long distances for long segments. Use this value to unscale the distance.\n#define LINE_DISTANCE_SCALE 2.0\n\nattribute vec2 a_pos;\nattribute vec4 a_data;\n\nuniform mat4 u_matrix;\nuniform mediump float u_ratio;\nuniform mediump float u_linewidth;\nuniform mediump float u_gapwidth;\nuniform mediump float u_antialiasing;\nuniform vec2 u_patternscale_a;\nuniform float u_tex_y_a;\nuniform vec2 u_patternscale_b;\nuniform float u_tex_y_b;\nuniform float u_extra;\nuniform mat2 u_antialiasingmatrix;\nuniform mediump float u_offset;\n\nvarying vec2 v_normal;\nvarying vec2 v_linewidth;\nvarying vec2 v_tex_a;\nvarying vec2 v_tex_b;\nvarying float v_gamma_scale;\n\nvoid main() {\n vec2 a_extrude = a_data.xy - 128.0;\n float a_direction = mod(a_data.z, 4.0) - 1.0;\n float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE;\n\n // We store the texture normals in the most insignificant bit\n // transform y so that 0 => -1 and 1 => 1\n // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap\n // y is 1 if the normal points up, and -1 if it points down\n mediump vec2 normal = mod(a_pos, 2.0);\n normal.y = sign(normal.y - 0.5);\n v_normal = normal;\n\n float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0);\n float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing;\n\n // Scale the extrusion vector down to a normal and then up by the line width\n // of this vertex.\n mediump vec2 dist = outset * a_extrude * scale;\n\n // Calculate the offset when drawing a line that is to the side of the actual line.\n // We do this by creating a vector that points towards the extrude, but rotate\n // it when we're drawing round end points (a_direction = -1 or 1) since their\n // extrude vector points in another direction.\n mediump float u = 0.5 * a_direction;\n mediump float t = 1.0 - abs(u);\n mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);\n\n // Remove the texture normal bit of the position before scaling it with the\n // model/view matrix.\n gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);\n\n v_tex_a = vec2(a_linesofar * u_patternscale_a.x, normal.y * u_patternscale_a.y + u_tex_y_a);\n v_tex_b = vec2(a_linesofar * u_patternscale_b.x, normal.y * u_patternscale_b.y + u_tex_y_b);\n\n // position of y on the screen\n float y = gl_Position.y / gl_Position.w;\n\n // how much features are squished in the y direction by the tilt\n float squish_scale = length(a_extrude) / length(u_antialiasingmatrix * a_extrude);\n\n // how much features are squished in all directions by the perspectiveness\n float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9));\n\n v_linewidth = vec2(outset, inset);\n v_gamma_scale = perspective_scale * squish_scale;\n}\n" - }, - outline: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\n#pragma mapbox: define lowp vec4 outline_color\n#pragma mapbox: define lowp float opacity\n\nvarying vec2 v_pos;\n\nvoid main() {\n #pragma mapbox: initialize lowp vec4 outline_color\n #pragma mapbox: initialize lowp float opacity\n\n float dist = length(v_pos - gl_FragCoord.xy);\n float alpha = smoothstep(1.0, 0.0, dist);\n gl_FragColor = outline_color * (alpha * opacity);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\n\nuniform mat4 u_matrix;\nuniform vec2 u_world;\n\nvarying vec2 v_pos;\n\n#pragma mapbox: define lowp vec4 outline_color\n#pragma mapbox: define lowp float opacity\n\nvoid main() {\n #pragma mapbox: initialize lowp vec4 outline_color\n #pragma mapbox: initialize lowp float opacity\n\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world;\n}\n" - }, - outlinepattern: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_opacity;\nuniform vec2 u_pattern_tl_a;\nuniform vec2 u_pattern_br_a;\nuniform vec2 u_pattern_tl_b;\nuniform vec2 u_pattern_br_b;\nuniform float u_mix;\n\nuniform sampler2D u_image;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\nvarying vec2 v_pos;\n\nvoid main() {\n vec2 imagecoord = mod(v_pos_a, 1.0);\n vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord);\n vec4 color1 = texture2D(u_image, pos);\n\n vec2 imagecoord_b = mod(v_pos_b, 1.0);\n vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b);\n vec4 color2 = texture2D(u_image, pos2);\n\n // find distance to outline for alpha interpolation\n\n float dist = length(v_pos - gl_FragCoord.xy);\n float alpha = smoothstep(1.0, 0.0, dist);\n \n\n gl_FragColor = mix(color1, color2, u_mix) * alpha * u_opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform vec2 u_pattern_size_a;\nuniform vec2 u_pattern_size_b;\nuniform vec2 u_pixel_coord_upper;\nuniform vec2 u_pixel_coord_lower;\nuniform float u_scale_a;\nuniform float u_scale_b;\nuniform float u_tile_units_to_pixels;\n\nattribute vec2 a_pos;\n\nuniform mat4 u_matrix;\nuniform vec2 u_world;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\nvarying vec2 v_pos;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n vec2 scaled_size_a = u_scale_a * u_pattern_size_a;\n vec2 scaled_size_b = u_scale_b * u_pattern_size_b;\n\n // the correct offset needs to be calculated.\n //\n // The offset depends on how many pixels are between the world origin and\n // the edge of the tile:\n // vec2 offset = mod(pixel_coord, size)\n //\n // At high zoom levels there are a ton of pixels between the world origin\n // and the edge of the tile. The glsl spec only guarantees 16 bits of\n // precision for highp floats. We need more than that.\n //\n // The pixel_coord is passed in as two 16 bit values:\n // pixel_coord_upper = floor(pixel_coord / 2^16)\n // pixel_coord_lower = mod(pixel_coord, 2^16)\n //\n // The offset is calculated in a series of steps that should preserve this precision:\n vec2 offset_a = mod(mod(mod(u_pixel_coord_upper, scaled_size_a) * 256.0, scaled_size_a) * 256.0 + u_pixel_coord_lower, scaled_size_a);\n vec2 offset_b = mod(mod(mod(u_pixel_coord_upper, scaled_size_b) * 256.0, scaled_size_b) * 256.0 + u_pixel_coord_lower, scaled_size_b);\n\n v_pos_a = (u_tile_units_to_pixels * a_pos + offset_a) / scaled_size_a;\n v_pos_b = (u_tile_units_to_pixels * a_pos + offset_b) / scaled_size_b;\n\n v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world;\n}\n" - }, - pattern: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_opacity;\nuniform vec2 u_pattern_tl_a;\nuniform vec2 u_pattern_br_a;\nuniform vec2 u_pattern_tl_b;\nuniform vec2 u_pattern_br_b;\nuniform float u_mix;\n\nuniform sampler2D u_image;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\n\nvoid main() {\n\n vec2 imagecoord = mod(v_pos_a, 1.0);\n vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord);\n vec4 color1 = texture2D(u_image, pos);\n\n vec2 imagecoord_b = mod(v_pos_b, 1.0);\n vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b);\n vec4 color2 = texture2D(u_image, pos2);\n\n gl_FragColor = mix(color1, color2, u_mix) * u_opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform mat4 u_matrix;\nuniform vec2 u_pattern_size_a;\nuniform vec2 u_pattern_size_b;\nuniform vec2 u_pixel_coord_upper;\nuniform vec2 u_pixel_coord_lower;\nuniform float u_scale_a;\nuniform float u_scale_b;\nuniform float u_tile_units_to_pixels;\n\nattribute vec2 a_pos;\n\nvarying vec2 v_pos_a;\nvarying vec2 v_pos_b;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n vec2 scaled_size_a = u_scale_a * u_pattern_size_a;\n vec2 scaled_size_b = u_scale_b * u_pattern_size_b;\n\n // the correct offset needs to be calculated.\n //\n // The offset depends on how many pixels are between the world origin and\n // the edge of the tile:\n // vec2 offset = mod(pixel_coord, size)\n //\n // At high zoom levels there are a ton of pixels between the world origin\n // and the edge of the tile. The glsl spec only guarantees 16 bits of\n // precision for highp floats. We need more than that.\n //\n // The pixel_coord is passed in as two 16 bit values:\n // pixel_coord_upper = floor(pixel_coord / 2^16)\n // pixel_coord_lower = mod(pixel_coord, 2^16)\n //\n // The offset is calculated in a series of steps that should preserve this precision:\n vec2 offset_a = mod(mod(mod(u_pixel_coord_upper, scaled_size_a) * 256.0, scaled_size_a) * 256.0 + u_pixel_coord_lower, scaled_size_a);\n vec2 offset_b = mod(mod(mod(u_pixel_coord_upper, scaled_size_b) * 256.0, scaled_size_b) * 256.0 + u_pixel_coord_lower, scaled_size_b);\n\n v_pos_a = (u_tile_units_to_pixels * a_pos + offset_a) / scaled_size_a;\n v_pos_b = (u_tile_units_to_pixels * a_pos + offset_b) / scaled_size_b;\n}\n" - }, - raster: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_opacity0;\nuniform float u_opacity1;\nuniform sampler2D u_image0;\nuniform sampler2D u_image1;\nvarying vec2 v_pos0;\nvarying vec2 v_pos1;\n\nuniform float u_brightness_low;\nuniform float u_brightness_high;\n\nuniform float u_saturation_factor;\nuniform float u_contrast_factor;\nuniform vec3 u_spin_weights;\n\nvoid main() {\n\n // read and cross-fade colors from the main and parent tiles\n vec4 color0 = texture2D(u_image0, v_pos0);\n vec4 color1 = texture2D(u_image1, v_pos1);\n vec4 color = color0 * u_opacity0 + color1 * u_opacity1;\n vec3 rgb = color.rgb;\n\n // spin\n rgb = vec3(\n dot(rgb, u_spin_weights.xyz),\n dot(rgb, u_spin_weights.zxy),\n dot(rgb, u_spin_weights.yzx));\n\n // saturation\n float average = (color.r + color.g + color.b) / 3.0;\n rgb += (average - rgb) * u_saturation_factor;\n\n // contrast\n rgb = (rgb - 0.5) * u_contrast_factor + 0.5;\n\n // brightness\n vec3 u_high_vec = vec3(u_brightness_low, u_brightness_low, u_brightness_low);\n vec3 u_low_vec = vec3(u_brightness_high, u_brightness_high, u_brightness_high);\n\n gl_FragColor = vec4(mix(u_high_vec, u_low_vec, rgb), color.a);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform mat4 u_matrix;\nuniform vec2 u_tl_parent;\nuniform float u_scale_parent;\nuniform float u_buffer_scale;\n\nattribute vec2 a_pos;\nattribute vec2 a_texture_pos;\n\nvarying vec2 v_pos0;\nvarying vec2 v_pos1;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n v_pos0 = (((a_texture_pos / 32767.0) - 0.5) / u_buffer_scale ) + 0.5;\n v_pos1 = (v_pos0 * u_scale_parent) + u_tl_parent;\n}\n" - }, - icon: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform sampler2D u_texture;\nuniform sampler2D u_fadetexture;\nuniform lowp float u_opacity;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\n\nvoid main() {\n lowp float alpha = texture2D(u_fadetexture, v_fade_tex).a * u_opacity;\n gl_FragColor = texture2D(u_texture, v_tex) * alpha;\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\nattribute vec2 a_offset;\nattribute vec2 a_texture_pos;\nattribute vec4 a_data;\n\n\n// matrix is for the vertex position.\nuniform mat4 u_matrix;\n\nuniform mediump float u_zoom;\nuniform bool u_rotate_with_map;\nuniform vec2 u_extrude_scale;\n\nuniform vec2 u_texsize;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\n\nvoid main() {\n vec2 a_tex = a_texture_pos.xy;\n mediump float a_labelminzoom = a_data[0];\n mediump vec2 a_zoom = a_data.pq;\n mediump float a_minzoom = a_zoom[0];\n mediump float a_maxzoom = a_zoom[1];\n\n // u_zoom is the current zoom level adjusted for the change in font size\n mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom));\n\n vec2 extrude = u_extrude_scale * (a_offset / 64.0);\n if (u_rotate_with_map) {\n gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1);\n gl_Position.z += z * gl_Position.w;\n } else {\n gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);\n }\n\n v_tex = a_tex / u_texsize;\n v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0);\n}\n" - }, - sdf: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform sampler2D u_texture;\nuniform sampler2D u_fadetexture;\nuniform lowp vec4 u_color;\nuniform lowp float u_opacity;\nuniform lowp float u_buffer;\nuniform lowp float u_gamma;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\nvarying float v_gamma_scale;\n\nvoid main() {\n lowp float dist = texture2D(u_texture, v_tex).a;\n lowp float fade_alpha = texture2D(u_fadetexture, v_fade_tex).a;\n lowp float gamma = u_gamma * v_gamma_scale;\n lowp float alpha = smoothstep(u_buffer - gamma, u_buffer + gamma, dist) * fade_alpha;\n\n gl_FragColor = u_color * (alpha * u_opacity);\n\n#ifdef OVERDRAW_INSPECTOR\n gl_FragColor = vec4(1.0);\n#endif\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nconst float PI = 3.141592653589793;\n\nattribute vec2 a_pos;\nattribute vec2 a_offset;\nattribute vec2 a_texture_pos;\nattribute vec4 a_data;\n\n\n// matrix is for the vertex position.\nuniform mat4 u_matrix;\n\nuniform mediump float u_zoom;\nuniform bool u_rotate_with_map;\nuniform bool u_pitch_with_map;\nuniform mediump float u_pitch;\nuniform mediump float u_bearing;\nuniform mediump float u_aspect_ratio;\nuniform vec2 u_extrude_scale;\n\nuniform vec2 u_texsize;\n\nvarying vec2 v_tex;\nvarying vec2 v_fade_tex;\nvarying float v_gamma_scale;\n\nvoid main() {\n vec2 a_tex = a_texture_pos.xy;\n mediump float a_labelminzoom = a_data[0];\n mediump vec2 a_zoom = a_data.pq;\n mediump float a_minzoom = a_zoom[0];\n mediump float a_maxzoom = a_zoom[1];\n\n // u_zoom is the current zoom level adjusted for the change in font size\n mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom));\n\n // pitch-alignment: map\n // rotation-alignment: map | viewport\n if (u_pitch_with_map) {\n lowp float angle = u_rotate_with_map ? (a_data[1] / 256.0 * 2.0 * PI) : u_bearing;\n lowp float asin = sin(angle);\n lowp float acos = cos(angle);\n mat2 RotationMatrix = mat2(acos, asin, -1.0 * asin, acos);\n vec2 offset = RotationMatrix * a_offset;\n vec2 extrude = u_extrude_scale * (offset / 64.0);\n gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1);\n gl_Position.z += z * gl_Position.w;\n // pitch-alignment: viewport\n // rotation-alignment: map\n } else if (u_rotate_with_map) {\n // foreshortening factor to apply on pitched maps\n // as a label goes from horizontal <=> vertical in angle\n // it goes from 0% foreshortening to up to around 70% foreshortening\n lowp float pitchfactor = 1.0 - cos(u_pitch * sin(u_pitch * 0.75));\n\n lowp float lineangle = a_data[1] / 256.0 * 2.0 * PI;\n\n // use the lineangle to position points a,b along the line\n // project the points and calculate the label angle in projected space\n // this calculation allows labels to be rendered unskewed on pitched maps\n vec4 a = u_matrix * vec4(a_pos, 0, 1);\n vec4 b = u_matrix * vec4(a_pos + vec2(cos(lineangle),sin(lineangle)), 0, 1);\n lowp float angle = atan((b[1]/b[3] - a[1]/a[3])/u_aspect_ratio, b[0]/b[3] - a[0]/a[3]);\n lowp float asin = sin(angle);\n lowp float acos = cos(angle);\n mat2 RotationMatrix = mat2(acos, -1.0 * asin, asin, acos);\n\n vec2 offset = RotationMatrix * (vec2((1.0-pitchfactor)+(pitchfactor*cos(angle*2.0)), 1.0) * a_offset);\n vec2 extrude = u_extrude_scale * (offset / 64.0);\n gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);\n gl_Position.z += z * gl_Position.w;\n // pitch-alignment: viewport\n // rotation-alignment: viewport\n } else {\n vec2 extrude = u_extrude_scale * (a_offset / 64.0);\n gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);\n }\n\n v_gamma_scale = (gl_Position.w - 0.5);\n\n v_tex = a_tex / u_texsize;\n v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0);\n}\n" - }, - collisionbox: { - fragmentSource: "#ifdef GL_ES\nprecision mediump float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nuniform float u_zoom;\nuniform float u_maxzoom;\n\nvarying float v_max_zoom;\nvarying float v_placement_zoom;\n\nvoid main() {\n\n float alpha = 0.5;\n\n gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0) * alpha;\n\n if (v_placement_zoom > u_zoom) {\n gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * alpha;\n }\n\n if (u_zoom >= v_max_zoom) {\n gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) * alpha * 0.25;\n }\n\n if (v_placement_zoom >= u_maxzoom) {\n gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0) * alpha * 0.2;\n }\n}\n", - vertexSource: "#ifdef GL_ES\nprecision highp float;\n#else\n#define lowp\n#define mediump\n#define highp\n#endif\n\nattribute vec2 a_pos;\nattribute vec2 a_extrude;\nattribute vec2 a_data;\n\nuniform mat4 u_matrix;\nuniform float u_scale;\n\nvarying float v_max_zoom;\nvarying float v_placement_zoom;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos + a_extrude / u_scale, 0.0, 1.0);\n\n v_max_zoom = a_data.x;\n v_placement_zoom = a_data.y;\n}\n" - } -}; - -module.exports.util = "float evaluate_zoom_function_1(const vec4 values, const float t) {\n if (t < 1.0) {\n return mix(values[0], values[1], t);\n } else if (t < 2.0) {\n return mix(values[1], values[2], t - 1.0);\n } else {\n return mix(values[2], values[3], t - 2.0);\n }\n}\nvec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) {\n if (t < 1.0) {\n return mix(value0, value1, t);\n } else if (t < 2.0) {\n return mix(value1, value2, t - 1.0);\n } else {\n return mix(value2, value3, t - 2.0);\n }\n}\n"; - -},{"path":40}],1044:[function(require,module,exports){ -'use strict'; - -var format = require('util').format; - -function ValidationError(key, value /*, message, ...*/) { - this.message = ( - (key ? key + ': ' : '') + - format.apply(format, Array.prototype.slice.call(arguments, 2)) - ); - - if (value !== null && value !== undefined && value.__line__) { - this.line = value.__line__; - } -} - -module.exports = ValidationError; - -},{"util":68}],1045:[function(require,module,exports){ -'use strict'; - -module.exports = function (output) { - for (var i = 1; i < arguments.length; i++) { - var input = arguments[i]; - for (var k in input) { - output[k] = input[k]; - } - } - return output; -}; - -},{}],1046:[function(require,module,exports){ -'use strict'; - -module.exports = function getType(val) { - if (val instanceof Number) { - return 'number'; - } else if (val instanceof String) { - return 'string'; - } else if (val instanceof Boolean) { - return 'boolean'; - } else if (Array.isArray(val)) { - return 'array'; - } else if (val === null) { - return 'null'; - } else { - return typeof val; - } -}; - -},{}],1047:[function(require,module,exports){ -'use strict'; - -// Turn jsonlint-lines-primitives objects into primitive objects -module.exports = function unbundle(value) { - if (value instanceof Number || value instanceof String || value instanceof Boolean) { - return value.valueOf(); - } else { - return value; - } -}; - -},{}],1048:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var getType = require('../util/get_type'); -var extend = require('../util/extend'); - -// Main recursive validation function. Tracks: -// -// - key: string representing location of validation in style tree. Used only -// for more informative error reporting. -// - value: current value from style being evaluated. May be anything from a -// high level object that needs to be descended into deeper or a simple -// scalar value. -// - valueSpec: current spec being evaluated. Tracks value. - -module.exports = function validate(options) { - - var validateFunction = require('./validate_function'); - var validateObject = require('./validate_object'); - var VALIDATORS = { - '*': function() { - return []; - }, - 'array': require('./validate_array'), - 'boolean': require('./validate_boolean'), - 'number': require('./validate_number'), - 'color': require('./validate_color'), - 'constants': require('./validate_constants'), - 'enum': require('./validate_enum'), - 'filter': require('./validate_filter'), - 'function': require('./validate_function'), - 'layer': require('./validate_layer'), - 'object': require('./validate_object'), - 'source': require('./validate_source'), - 'string': require('./validate_string') - }; - - var value = options.value; - var valueSpec = options.valueSpec; - var key = options.key; - var styleSpec = options.styleSpec; - var style = options.style; - - if (getType(value) === 'string' && value[0] === '@') { - if (styleSpec.$version > 7) { - return [new ValidationError(key, value, 'constants have been deprecated as of v8')]; - } - if (!(value in style.constants)) { - return [new ValidationError(key, value, 'constant "%s" not found', value)]; - } - options = extend({}, options, { value: style.constants[value] }); - } - - if (valueSpec.function && getType(value) === 'object') { - return validateFunction(options); - - } else if (valueSpec.type && VALIDATORS[valueSpec.type]) { - return VALIDATORS[valueSpec.type](options); - - } else { - return validateObject(extend({}, options, { - valueSpec: valueSpec.type ? styleSpec[valueSpec.type] : valueSpec - })); - } -}; - -},{"../error/validation_error":1044,"../util/extend":1045,"../util/get_type":1046,"./validate_array":1049,"./validate_boolean":1050,"./validate_color":1051,"./validate_constants":1052,"./validate_enum":1053,"./validate_filter":1054,"./validate_function":1055,"./validate_layer":1057,"./validate_number":1059,"./validate_object":1060,"./validate_source":1062,"./validate_string":1063}],1049:[function(require,module,exports){ -'use strict'; - -var getType = require('../util/get_type'); -var validate = require('./validate'); -var ValidationError = require('../error/validation_error'); - -module.exports = function validateArray(options) { - var array = options.value; - var arraySpec = options.valueSpec; - var style = options.style; - var styleSpec = options.styleSpec; - var key = options.key; - var validateArrayElement = options.arrayElementValidator || validate; - - if (getType(array) !== 'array') { - return [new ValidationError(key, array, 'array expected, %s found', getType(array))]; - } - - if (arraySpec.length && array.length !== arraySpec.length) { - return [new ValidationError(key, array, 'array length %d expected, length %d found', arraySpec.length, array.length)]; - } - - if (arraySpec['min-length'] && array.length < arraySpec['min-length']) { - return [new ValidationError(key, array, 'array length at least %d expected, length %d found', arraySpec['min-length'], array.length)]; - } - - var arrayElementSpec = { - "type": arraySpec.value - }; - - if (styleSpec.$version < 7) { - arrayElementSpec.function = arraySpec.function; - } - - if (getType(arraySpec.value) === 'object') { - arrayElementSpec = arraySpec.value; - } - - var errors = []; - for (var i = 0; i < array.length; i++) { - errors = errors.concat(validateArrayElement({ - array: array, - arrayIndex: i, - value: array[i], - valueSpec: arrayElementSpec, - style: style, - styleSpec: styleSpec, - key: key + '[' + i + ']' - })); - } - return errors; -}; - -},{"../error/validation_error":1044,"../util/get_type":1046,"./validate":1048}],1050:[function(require,module,exports){ -'use strict'; - -var getType = require('../util/get_type'); -var ValidationError = require('../error/validation_error'); - -module.exports = function validateBoolean(options) { - var value = options.value; - var key = options.key; - var type = getType(value); - - if (type !== 'boolean') { - return [new ValidationError(key, value, 'boolean expected, %s found', type)]; - } - - return []; -}; - -},{"../error/validation_error":1044,"../util/get_type":1046}],1051:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var getType = require('../util/get_type'); -var parseCSSColor = require('csscolorparser').parseCSSColor; - -module.exports = function validateColor(options) { - var key = options.key; - var value = options.value; - var type = getType(value); - - if (type !== 'string') { - return [new ValidationError(key, value, 'color expected, %s found', type)]; - } - - if (parseCSSColor(value) === null) { - return [new ValidationError(key, value, 'color expected, "%s" found', value)]; - } - - return []; -}; - -},{"../error/validation_error":1044,"../util/get_type":1046,"csscolorparser":1017}],1052:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var getType = require('../util/get_type'); - -module.exports = function validateConstants(options) { - var key = options.key; - var constants = options.value; - var styleSpec = options.styleSpec; - - if (styleSpec.$version > 7) { - if (constants) { - return [new ValidationError(key, constants, 'constants have been deprecated as of v8')]; - } else { - return []; - } - } else { - var type = getType(constants); - if (type !== 'object') { - return [new ValidationError(key, constants, 'object expected, %s found', type)]; - } - - var errors = []; - for (var constantName in constants) { - if (constantName[0] !== '@') { - errors.push(new ValidationError(key + '.' + constantName, constants[constantName], 'constants must start with "@"')); - } - } - return errors; - } - -}; - -},{"../error/validation_error":1044,"../util/get_type":1046}],1053:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var unbundle = require('../util/unbundle_jsonlint'); - -module.exports = function validateEnum(options) { - var key = options.key; - var value = options.value; - var valueSpec = options.valueSpec; - var errors = []; - - if (valueSpec.values.indexOf(unbundle(value)) === -1) { - errors.push(new ValidationError(key, value, 'expected one of [%s], %s found', valueSpec.values.join(', '), value)); - } - return errors; -}; - -},{"../error/validation_error":1044,"../util/unbundle_jsonlint":1047}],1054:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var validateEnum = require('./validate_enum'); -var getType = require('../util/get_type'); -var unbundle = require('../util/unbundle_jsonlint'); - -module.exports = function validateFilter(options) { - var value = options.value; - var key = options.key; - var styleSpec = options.styleSpec; - var type; - - var errors = []; - - if (getType(value) !== 'array') { - return [new ValidationError(key, value, 'array expected, %s found', getType(value))]; - } - - if (value.length < 1) { - return [new ValidationError(key, value, 'filter array must have at least 1 element')]; - } - - errors = errors.concat(validateEnum({ - key: key + '[0]', - value: value[0], - valueSpec: styleSpec.filter_operator, - style: options.style, - styleSpec: options.styleSpec - })); - - switch (unbundle(value[0])) { - case '<': - case '<=': - case '>': - case '>=': - if (value.length >= 2 && value[1] == '$type') { - errors.push(new ValidationError(key, value, '"$type" cannot be use with operator "%s"', value[0])); - } - /* falls through */ - case '==': - case '!=': - if (value.length != 3) { - errors.push(new ValidationError(key, value, 'filter array for operator "%s" must have 3 elements', value[0])); - } - /* falls through */ - case 'in': - case '!in': - if (value.length >= 2) { - type = getType(value[1]); - if (type !== 'string') { - errors.push(new ValidationError(key + '[1]', value[1], 'string expected, %s found', type)); - } else if (value[1][0] === '@') { - errors.push(new ValidationError(key + '[1]', value[1], 'filter key cannot be a constant')); - } - } - for (var i = 2; i < value.length; i++) { - type = getType(value[i]); - if (value[1] == '$type') { - errors = errors.concat(validateEnum({ - key: key + '[' + i + ']', - value: value[i], - valueSpec: styleSpec.geometry_type, - style: options.style, - styleSpec: options.styleSpec - })); - } else if (type === 'string' && value[i][0] === '@') { - errors.push(new ValidationError(key + '[' + i + ']', value[i], 'filter value cannot be a constant')); - } else if (type !== 'string' && type !== 'number' && type !== 'boolean') { - errors.push(new ValidationError(key + '[' + i + ']', value[i], 'string, number, or boolean expected, %s found', type)); - } - } - break; - - case 'any': - case 'all': - case 'none': - for (i = 1; i < value.length; i++) { - errors = errors.concat(validateFilter({ - key: key + '[' + i + ']', - value: value[i], - style: options.style, - styleSpec: options.styleSpec - })); - } - break; - - case 'has': - case '!has': - type = getType(value[1]); - if (value.length !== 2) { - errors.push(new ValidationError(key, value, 'filter array for "%s" operator must have 2 elements', value[0])); - } else if (type !== 'string') { - errors.push(new ValidationError(key + '[1]', value[1], 'string expected, %s found', type)); - } else if (value[1][0] === '@') { - errors.push(new ValidationError(key + '[1]', value[1], 'filter key cannot be a constant')); - } - break; - - } - - return errors; -}; - -},{"../error/validation_error":1044,"../util/get_type":1046,"../util/unbundle_jsonlint":1047,"./validate_enum":1053}],1055:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var getType = require('../util/get_type'); -var validate = require('./validate'); -var validateObject = require('./validate_object'); -var validateArray = require('./validate_array'); -var validateNumber = require('./validate_number'); - -module.exports = function validateFunction(options) { - var functionValueSpec = options.valueSpec; - var stopKeyType; - - var isPropertyFunction = options.value.property !== undefined || stopKeyType === 'object'; - var isZoomFunction = options.value.property === undefined || stopKeyType === 'object'; - - var errors = validateObject({ - key: options.key, - value: options.value, - valueSpec: options.styleSpec.function, - style: options.style, - styleSpec: options.styleSpec, - objectElementValidators: { stops: validateFunctionStops } - }); - - if (options.styleSpec.$version >= 8) { - if (isPropertyFunction && !options.valueSpec['property-function']) { - errors.push(new ValidationError(options.key, options.value, 'property functions not supported')); - } else if (isZoomFunction && !options.valueSpec['zoom-function']) { - errors.push(new ValidationError(options.key, options.value, 'zoom functions not supported')); - } - } - - return errors; - - function validateFunctionStops(options) { - var errors = []; - var value = options.value; - - errors = errors.concat(validateArray({ - key: options.key, - value: value, - valueSpec: options.valueSpec, - style: options.style, - styleSpec: options.styleSpec, - arrayElementValidator: validateFunctionStop - })); - - if (getType(value) === 'array' && value.length === 0) { - errors.push(new ValidationError(options.key, value, 'array must have at least one stop')); - } - - return errors; - } - - function validateFunctionStop(options) { - var errors = []; - var value = options.value; - var key = options.key; - - if (getType(value) !== 'array') { - return [new ValidationError(key, value, 'array expected, %s found', getType(value))]; - } - - if (value.length !== 2) { - return [new ValidationError(key, value, 'array length %d expected, length %d found', 2, value.length)]; - } - - var type = getType(value[0]); - if (!stopKeyType) stopKeyType = type; - if (type !== stopKeyType) { - return [new ValidationError(key, value, '%s stop key type must match previous stop key type %s', type, stopKeyType)]; - } - - if (type === 'object') { - if (value[0].zoom === undefined) { - return [new ValidationError(key, value, 'object stop key must have zoom')]; - } - if (value[0].value === undefined) { - return [new ValidationError(key, value, 'object stop key must have value')]; - } - errors = errors.concat(validateObject({ - key: key + '[0]', - value: value[0], - valueSpec: { zoom: {} }, - style: options.style, - styleSpec: options.styleSpec, - objectElementValidators: { zoom: validateNumber, value: validateValue } - })); - } else { - errors = errors.concat((isZoomFunction ? validateNumber : validateValue)({ - key: key + '[0]', - value: value[0], - valueSpec: {}, - style: options.style, - styleSpec: options.styleSpec - })); - } - - errors = errors.concat(validate({ - key: key + '[1]', - value: value[1], - valueSpec: functionValueSpec, - style: options.style, - styleSpec: options.styleSpec - })); - - if (getType(value[0]) === 'number') { - if (functionValueSpec.function === 'piecewise-constant' && value[0] % 1 !== 0) { - errors.push(new ValidationError(key + '[0]', value[0], 'zoom level for piecewise-constant functions must be an integer')); - } - - if (options.arrayIndex !== 0) { - if (value[0] < options.array[options.arrayIndex - 1][0]) { - errors.push(new ValidationError(key + '[0]', value[0], 'array stops must appear in ascending order')); - } - } - } - - return errors; - } - - function validateValue(options) { - var errors = []; - var type = getType(options.value); - if (type !== 'number' && type !== 'string' && type !== 'array') { - errors.push(new ValidationError(options.key, options.value, 'property value must be a number, string or array')); - } - return errors; - } - -}; - -},{"../error/validation_error":1044,"../util/get_type":1046,"./validate":1048,"./validate_array":1049,"./validate_number":1059,"./validate_object":1060}],1056:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var validateString = require('./validate_string'); - -module.exports = function(options) { - var value = options.value; - var key = options.key; - - var errors = validateString(options); - if (errors.length) return errors; - - if (value.indexOf('{fontstack}') === -1) { - errors.push(new ValidationError(key, value, '"glyphs" url must include a "{fontstack}" token')); - } - - if (value.indexOf('{range}') === -1) { - errors.push(new ValidationError(key, value, '"glyphs" url must include a "{range}" token')); - } - - return errors; -}; - -},{"../error/validation_error":1044,"./validate_string":1063}],1057:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var unbundle = require('../util/unbundle_jsonlint'); -var validateObject = require('./validate_object'); -var validateFilter = require('./validate_filter'); -var validatePaintProperty = require('./validate_paint_property'); -var validateLayoutProperty = require('./validate_layout_property'); -var extend = require('../util/extend'); - -module.exports = function validateLayer(options) { - var errors = []; - - var layer = options.value; - var key = options.key; - var style = options.style; - var styleSpec = options.styleSpec; - - if (!layer.type && !layer.ref) { - errors.push(new ValidationError(key, layer, 'either "type" or "ref" is required')); - } - var type = unbundle(layer.type); - var ref = unbundle(layer.ref); - - if (layer.id) { - for (var i = 0; i < options.arrayIndex; i++) { - var otherLayer = style.layers[i]; - if (unbundle(otherLayer.id) === unbundle(layer.id)) { - errors.push(new ValidationError(key, layer.id, 'duplicate layer id "%s", previously used at line %d', layer.id, otherLayer.id.__line__)); - } - } - } - - if ('ref' in layer) { - ['type', 'source', 'source-layer', 'filter', 'layout'].forEach(function (p) { - if (p in layer) { - errors.push(new ValidationError(key, layer[p], '"%s" is prohibited for ref layers', p)); - } - }); - - var parent; - - style.layers.forEach(function(layer) { - if (layer.id == ref) parent = layer; - }); - - if (!parent) { - errors.push(new ValidationError(key, layer.ref, 'ref layer "%s" not found', ref)); - } else if (parent.ref) { - errors.push(new ValidationError(key, layer.ref, 'ref cannot reference another ref layer')); - } else { - type = unbundle(parent.type); - } - } else if (type !== 'background') { - if (!layer.source) { - errors.push(new ValidationError(key, layer, 'missing required property "source"')); - } else { - var source = style.sources && style.sources[layer.source]; - if (!source) { - errors.push(new ValidationError(key, layer.source, 'source "%s" not found', layer.source)); - } else if (source.type == 'vector' && type == 'raster') { - errors.push(new ValidationError(key, layer.source, 'layer "%s" requires a raster source', layer.id)); - } else if (source.type == 'raster' && type != 'raster') { - errors.push(new ValidationError(key, layer.source, 'layer "%s" requires a vector source', layer.id)); - } else if (source.type == 'vector' && !layer['source-layer']) { - errors.push(new ValidationError(key, layer, 'layer "%s" must specify a "source-layer"', layer.id)); - } - } - } - - errors = errors.concat(validateObject({ - key: key, - value: layer, - valueSpec: styleSpec.layer, - style: options.style, - styleSpec: options.styleSpec, - objectElementValidators: { - filter: validateFilter, - layout: function(options) { - return validateObject({ - layer: layer, - key: options.key, - value: options.value, - style: options.style, - styleSpec: options.styleSpec, - objectElementValidators: { - '*': function(options) { - return validateLayoutProperty(extend({layerType: type}, options)); - } - } - }); - }, - paint: function(options) { - return validateObject({ - layer: layer, - key: options.key, - value: options.value, - style: options.style, - styleSpec: options.styleSpec, - objectElementValidators: { - '*': function(options) { - return validatePaintProperty(extend({layerType: type}, options)); - } - } - }); - } - } - })); - - return errors; -}; - -},{"../error/validation_error":1044,"../util/extend":1045,"../util/unbundle_jsonlint":1047,"./validate_filter":1054,"./validate_layout_property":1058,"./validate_object":1060,"./validate_paint_property":1061}],1058:[function(require,module,exports){ -'use strict'; - -var validate = require('./validate'); -var ValidationError = require('../error/validation_error'); - -module.exports = function validateLayoutProperty(options) { - var key = options.key; - var style = options.style; - var styleSpec = options.styleSpec; - var value = options.value; - var propertyKey = options.objectKey; - var layerSpec = styleSpec['layout_' + options.layerType]; - - if (options.valueSpec || layerSpec[propertyKey]) { - var errors = []; - - if (options.layerType === 'symbol') { - if (propertyKey === 'icon-image' && style && !style.sprite) { - errors.push(new ValidationError(key, value, 'use of "icon-image" requires a style "sprite" property')); - } else if (propertyKey === 'text-field' && style && !style.glyphs) { - errors.push(new ValidationError(key, value, 'use of "text-field" requires a style "glyphs" property')); - } - } - - return errors.concat(validate({ - key: options.key, - value: value, - valueSpec: options.valueSpec || layerSpec[propertyKey], - style: style, - styleSpec: styleSpec - })); - - } else { - return [new ValidationError(key, value, 'unknown property "%s"', propertyKey)]; - } - -}; - -},{"../error/validation_error":1044,"./validate":1048}],1059:[function(require,module,exports){ -'use strict'; - -var getType = require('../util/get_type'); -var ValidationError = require('../error/validation_error'); - -module.exports = function validateNumber(options) { - var key = options.key; - var value = options.value; - var valueSpec = options.valueSpec; - var type = getType(value); - - if (type !== 'number') { - return [new ValidationError(key, value, 'number expected, %s found', type)]; - } - - if ('minimum' in valueSpec && value < valueSpec.minimum) { - return [new ValidationError(key, value, '%s is less than the minimum value %s', value, valueSpec.minimum)]; - } - - if ('maximum' in valueSpec && value > valueSpec.maximum) { - return [new ValidationError(key, value, '%s is greater than the maximum value %s', value, valueSpec.maximum)]; - } - - return []; -}; - -},{"../error/validation_error":1044,"../util/get_type":1046}],1060:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var getType = require('../util/get_type'); -var validate = require('./validate'); - -module.exports = function validateObject(options) { - var key = options.key; - var object = options.value; - var valueSpec = options.valueSpec; - var objectElementValidators = options.objectElementValidators || {}; - var style = options.style; - var styleSpec = options.styleSpec; - var errors = []; - - var type = getType(object); - if (type !== 'object') { - return [new ValidationError(key, object, 'object expected, %s found', type)]; - } - - for (var objectKey in object) { - var valueSpecKey = objectKey.split('.')[0]; // treat 'paint.*' as 'paint' - var objectElementSpec = valueSpec && (valueSpec[valueSpecKey] || valueSpec['*']); - var objectElementValidator = objectElementValidators[valueSpecKey] || objectElementValidators['*']; - - if (objectElementSpec || objectElementValidator) { - errors = errors.concat((objectElementValidator || validate)({ - key: (key ? key + '.' : key) + objectKey, - value: object[objectKey], - valueSpec: objectElementSpec, - style: style, - styleSpec: styleSpec, - object: object, - objectKey: objectKey - })); - - // tolerate root-level extra keys & arbitrary layer properties - // TODO remove this layer-specific logic - } else if (key !== '' && key.split('.').length !== 1) { - errors.push(new ValidationError(key, object[objectKey], 'unknown property "%s"', objectKey)); - } - } - - for (valueSpecKey in valueSpec) { - if (valueSpec[valueSpecKey].required && valueSpec[valueSpecKey]['default'] === undefined && object[valueSpecKey] === undefined) { - errors.push(new ValidationError(key, object, 'missing required property "%s"', valueSpecKey)); - } - } - - return errors; -}; - -},{"../error/validation_error":1044,"../util/get_type":1046,"./validate":1048}],1061:[function(require,module,exports){ -'use strict'; - -var validate = require('./validate'); -var ValidationError = require('../error/validation_error'); - -module.exports = function validatePaintProperty(options) { - var key = options.key; - var style = options.style; - var styleSpec = options.styleSpec; - var value = options.value; - var propertyKey = options.objectKey; - var layerSpec = styleSpec['paint_' + options.layerType]; - - var transitionMatch = propertyKey.match(/^(.*)-transition$/); - - if (transitionMatch && layerSpec[transitionMatch[1]] && layerSpec[transitionMatch[1]].transition) { - return validate({ - key: key, - value: value, - valueSpec: styleSpec.transition, - style: style, - styleSpec: styleSpec - }); - - } else if (options.valueSpec || layerSpec[propertyKey]) { - return validate({ - key: options.key, - value: value, - valueSpec: options.valueSpec || layerSpec[propertyKey], - style: style, - styleSpec: styleSpec - }); - - } else { - return [new ValidationError(key, value, 'unknown property "%s"', propertyKey)]; - } - -}; - -},{"../error/validation_error":1044,"./validate":1048}],1062:[function(require,module,exports){ -'use strict'; - -var ValidationError = require('../error/validation_error'); -var unbundle = require('../util/unbundle_jsonlint'); -var validateObject = require('./validate_object'); -var validateEnum = require('./validate_enum'); - -module.exports = function validateSource(options) { - var value = options.value; - var key = options.key; - var styleSpec = options.styleSpec; - var style = options.style; - - if (!value.type) { - return [new ValidationError(key, value, '"type" is required')]; - } - - var type = unbundle(value.type); - switch (type) { - case 'vector': - case 'raster': - var errors = []; - errors = errors.concat(validateObject({ - key: key, - value: value, - valueSpec: styleSpec.source_tile, - style: options.style, - styleSpec: styleSpec - })); - if ('url' in value) { - for (var prop in value) { - if (['type', 'url', 'tileSize'].indexOf(prop) < 0) { - errors.push(new ValidationError(key + '.' + prop, value[prop], 'a source with a "url" property may not include a "%s" property', prop)); - } - } - } - return errors; - - case 'geojson': - return validateObject({ - key: key, - value: value, - valueSpec: styleSpec.source_geojson, - style: style, - styleSpec: styleSpec - }); - - case 'video': - return validateObject({ - key: key, - value: value, - valueSpec: styleSpec.source_video, - style: style, - styleSpec: styleSpec - }); - - case 'image': - return validateObject({ - key: key, - value: value, - valueSpec: styleSpec.source_image, - style: style, - styleSpec: styleSpec - }); - - default: - return validateEnum({ - key: key + '.type', - value: value.type, - valueSpec: {values: ['vector', 'raster', 'geojson', 'video', 'image']}, - style: style, - styleSpec: styleSpec - }); - } -}; - -},{"../error/validation_error":1044,"../util/unbundle_jsonlint":1047,"./validate_enum":1053,"./validate_object":1060}],1063:[function(require,module,exports){ -'use strict'; - -var getType = require('../util/get_type'); -var ValidationError = require('../error/validation_error'); - -module.exports = function validateString(options) { - var value = options.value; - var key = options.key; - var type = getType(value); - - if (type !== 'string') { - return [new ValidationError(key, value, 'string expected, %s found', type)]; - } - - return []; -}; - -},{"../error/validation_error":1044,"../util/get_type":1046}],1064:[function(require,module,exports){ -'use strict'; - -var validateConstants = require('./validate/validate_constants'); -var validate = require('./validate/validate'); -var latestStyleSpec = require('../reference/latest.min'); -var validateGlyphsURL = require('./validate/validate_glyphs_url'); - -/** - * Validate a Mapbox GL style against the style specification. This entrypoint, - * `mapbox-gl-style-spec/lib/validate_style.min`, is designed to produce as - * small a browserify bundle as possible by omitting unnecessary functionality - * and legacy style specifications. - * - * @param {Object} style The style to be validated. - * @param {Object} [styleSpec] The style specification to validate against. - * If omitted, the latest style spec is used. - * @returns {Array} - * @example - * var validate = require('mapbox-gl-style-spec/lib/validate_style.min'); - * var errors = validate(style); - */ -function validateStyleMin(style, styleSpec) { - styleSpec = styleSpec || latestStyleSpec; - - var errors = []; - - errors = errors.concat(validate({ - key: '', - value: style, - valueSpec: styleSpec.$root, - styleSpec: styleSpec, - style: style, - objectElementValidators: { - glyphs: validateGlyphsURL - } - })); - - if (styleSpec.$version > 7 && style.constants) { - errors = errors.concat(validateConstants({ - key: 'constants', - value: style.constants, - style: style, - styleSpec: styleSpec - })); - } - - return sortErrors(errors); -} - -validateStyleMin.source = wrapCleanErrors(require('./validate/validate_source')); -validateStyleMin.layer = wrapCleanErrors(require('./validate/validate_layer')); -validateStyleMin.filter = wrapCleanErrors(require('./validate/validate_filter')); -validateStyleMin.paintProperty = wrapCleanErrors(require('./validate/validate_paint_property')); -validateStyleMin.layoutProperty = wrapCleanErrors(require('./validate/validate_layout_property')); - -function sortErrors(errors) { - return [].concat(errors).sort(function (a, b) { - return a.line - b.line; - }); -} - -function wrapCleanErrors(inner) { - return function() { - return sortErrors(inner.apply(this, arguments)); - }; -} - -module.exports = validateStyleMin; - -},{"../reference/latest.min":1065,"./validate/validate":1048,"./validate/validate_constants":1052,"./validate/validate_filter":1054,"./validate/validate_glyphs_url":1056,"./validate/validate_layer":1057,"./validate/validate_layout_property":1058,"./validate/validate_paint_property":1061,"./validate/validate_source":1062}],1065:[function(require,module,exports){ -module.exports = require('./v8.min.json'); - -},{"./v8.min.json":1066}],1066:[function(require,module,exports){ -module.exports={"$version":8,"$root":{"version":{"required":true,"type":"enum","values":[8]},"name":{"type":"string"},"metadata":{"type":"*"},"center":{"type":"array","value":"number"},"zoom":{"type":"number"},"bearing":{"type":"number","default":0,"period":360,"units":"degrees"},"pitch":{"type":"number","default":0,"units":"degrees"},"sources":{"required":true,"type":"sources"},"sprite":{"type":"string"},"glyphs":{"type":"string"},"transition":{"type":"transition"},"layers":{"required":true,"type":"array","value":"layer"}},"sources":{"*":{"type":"source"}},"source":["source_tile","source_geojson","source_video","source_image"],"source_tile":{"type":{"required":true,"type":"enum","values":["vector","raster"]},"url":{"type":"string"},"tiles":{"type":"array","value":"string"},"minzoom":{"type":"number","default":0},"maxzoom":{"type":"number","default":22},"tileSize":{"type":"number","default":512,"units":"pixels"},"*":{"type":"*"}},"source_geojson":{"type":{"required":true,"type":"enum","values":["geojson"]},"data":{"type":"*"},"maxzoom":{"type":"number","default":14},"buffer":{"type":"number","default":64},"tolerance":{"type":"number","default":3},"cluster":{"type":"boolean","default":false},"clusterRadius":{"type":"number","default":400},"clusterMaxZoom":{"type":"number"}},"source_video":{"type":{"required":true,"type":"enum","values":["video"]},"urls":{"required":true,"type":"array","value":"string"},"coordinates":{"required":true,"type":"array","length":4,"value":{"type":"array","length":2,"value":"number"}}},"source_image":{"type":{"required":true,"type":"enum","values":["image"]},"url":{"required":true,"type":"string"},"coordinates":{"required":true,"type":"array","length":4,"value":{"type":"array","length":2,"value":"number"}}},"layer":{"id":{"type":"string","required":true},"type":{"type":"enum","values":["fill","line","symbol","circle","raster","background"]},"metadata":{"type":"*"},"ref":{"type":"string"},"source":{"type":"string"},"source-layer":{"type":"string"},"minzoom":{"type":"number","minimum":0,"maximum":22},"maxzoom":{"type":"number","minimum":0,"maximum":22},"interactive":{"type":"boolean","default":false},"filter":{"type":"filter"},"layout":{"type":"layout"},"paint":{"type":"paint"},"paint.*":{"type":"paint"}},"layout":["layout_fill","layout_line","layout_circle","layout_symbol","layout_raster","layout_background"],"layout_background":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_fill":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_circle":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_line":{"line-cap":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["butt","round","square"],"default":"butt"},"line-join":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["bevel","round","miter"],"default":"miter"},"line-miter-limit":{"type":"number","default":2,"function":"interpolated","zoom-function":true,"property-function":true,"requires":[{"line-join":"miter"}]},"line-round-limit":{"type":"number","default":1.05,"function":"interpolated","zoom-function":true,"property-function":true,"requires":[{"line-join":"round"}]},"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_symbol":{"symbol-placement":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["point","line"],"default":"point"},"symbol-spacing":{"type":"number","default":250,"minimum":1,"function":"interpolated","zoom-function":true,"property-function":true,"units":"pixels","requires":[{"symbol-placement":"line"}]},"symbol-avoid-edges":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false},"icon-allow-overlap":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image"]},"icon-ignore-placement":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image"]},"icon-optional":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image","text-field"]},"icon-rotation-alignment":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"viewport","requires":["icon-image"]},"icon-size":{"type":"number","default":1,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"requires":["icon-image"]},"icon-text-fit":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":false,"values":["none","both","width","height"],"default":"none","requires":["icon-image","text-field"]},"icon-text-fit-padding":{"type":"array","value":"number","length":4,"default":[0,0,0,0],"units":"pixels","function":"interpolated","zoom-function":true,"property-function":true,"requires":["icon-image","icon-text-fit","text-field"]},"icon-image":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"tokens":true},"icon-rotate":{"type":"number","default":0,"period":360,"function":"interpolated","zoom-function":true,"property-function":true,"units":"degrees","requires":["icon-image"]},"icon-padding":{"type":"number","default":2,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"units":"pixels","requires":["icon-image"]},"icon-keep-upright":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["icon-image",{"icon-rotation-alignment":"map"},{"symbol-placement":"line"}]},"icon-offset":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"requires":["icon-image"]},"text-pitch-alignment":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"requires":["text-field"]},"text-rotation-alignment":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"viewport","requires":["text-field"]},"text-field":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":"","tokens":true},"text-font":{"type":"array","value":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":["Open Sans Regular","Arial Unicode MS Regular"],"requires":["text-field"]},"text-size":{"type":"number","default":16,"minimum":0,"units":"pixels","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-max-width":{"type":"number","default":10,"minimum":0,"units":"em","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-line-height":{"type":"number","default":1.2,"units":"em","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-letter-spacing":{"type":"number","default":0,"units":"em","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-justify":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["left","center","right"],"default":"center","requires":["text-field"]},"text-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["center","left","right","top","bottom","top-left","top-right","bottom-left","bottom-right"],"default":"center","requires":["text-field"]},"text-max-angle":{"type":"number","default":45,"units":"degrees","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field",{"symbol-placement":"line"}]},"text-rotate":{"type":"number","default":0,"period":360,"units":"degrees","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-padding":{"type":"number","default":2,"minimum":0,"units":"pixels","function":"interpolated","zoom-function":true,"property-function":true,"requires":["text-field"]},"text-keep-upright":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":true,"requires":["text-field",{"text-rotation-alignment":"map"},{"symbol-placement":"line"}]},"text-transform":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["none","uppercase","lowercase"],"default":"none","requires":["text-field"]},"text-offset":{"type":"array","value":"number","units":"ems","function":"interpolated","zoom-function":true,"property-function":true,"length":2,"default":[0,0],"requires":["text-field"]},"text-allow-overlap":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["text-field"]},"text-ignore-placement":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["text-field"]},"text-optional":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":false,"requires":["text-field","icon-image"]},"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"layout_raster":{"visibility":{"type":"enum","function":"piecewise-constant","zoom-function":true,"values":["visible","none"],"default":"visible"}},"filter":{"type":"array","value":"*"},"filter_operator":{"type":"enum","values":["==","!=",">",">=","<","<=","in","!in","all","any","none","has","!has"]},"geometry_type":{"type":"enum","values":["Point","LineString","Polygon"]},"color_operation":{"type":"enum","values":["lighten","saturate","spin","fade","mix"]},"function":{"stops":{"type":"array","required":true,"value":"function_stop"},"base":{"type":"number","default":1,"minimum":0},"property":{"type":"string","default":"$zoom"},"type":{"type":"enum","values":["exponential","interval","categorical"],"default":"exponential"}},"function_stop":{"type":"array","minimum":0,"maximum":22,"value":["number","color"],"length":2},"paint":["paint_fill","paint_line","paint_circle","paint_symbol","paint_raster","paint_background"],"paint_fill":{"fill-antialias":{"type":"boolean","function":"piecewise-constant","zoom-function":true,"property-function":true,"default":true},"fill-opacity":{"type":"number","function":"interpolated","zoom-function":true,"property-function":true,"default":1,"minimum":0,"maximum":1,"transition":true},"fill-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":[{"!":"fill-pattern"}]},"fill-outline-color":{"type":"color","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":[{"!":"fill-pattern"},{"fill-antialias":true}]},"fill-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"fill-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["fill-translate"]},"fill-pattern":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"transition":true}},"paint_line":{"line-opacity":{"type":"number","function":"interpolated","zoom-function":true,"property-function":true,"default":1,"minimum":0,"maximum":1,"transition":true},"line-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":[{"!":"line-pattern"}]},"line-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["line-translate"]},"line-width":{"type":"number","default":1,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-gap-width":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-offset":{"type":"number","default":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-blur":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"line-dasharray":{"type":"array","value":"number","function":"piecewise-constant","zoom-function":true,"property-function":true,"minimum":0,"transition":true,"units":"line widths","requires":[{"!":"line-pattern"}]},"line-pattern":{"type":"string","function":"piecewise-constant","zoom-function":true,"property-function":true,"transition":true}},"paint_circle":{"circle-radius":{"type":"number","default":5,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"circle-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true},"circle-blur":{"type":"number","default":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true},"circle-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true},"circle-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels"},"circle-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["circle-translate"]},"circle-pitch-scale":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map"}},"paint_symbol":{"icon-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["icon-image"]},"icon-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["icon-image"]},"icon-halo-color":{"type":"color","default":"rgba(0, 0, 0, 0)","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["icon-image"]},"icon-halo-width":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["icon-image"]},"icon-halo-blur":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["icon-image"]},"icon-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["icon-image"]},"icon-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["icon-image","icon-translate"]},"text-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["text-field"]},"text-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["text-field"]},"text-halo-color":{"type":"color","default":"rgba(0, 0, 0, 0)","function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"requires":["text-field"]},"text-halo-width":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["text-field"]},"text-halo-blur":{"type":"number","default":0,"minimum":0,"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["text-field"]},"text-translate":{"type":"array","value":"number","length":2,"default":[0,0],"function":"interpolated","zoom-function":true,"property-function":true,"transition":true,"units":"pixels","requires":["text-field"]},"text-translate-anchor":{"type":"enum","function":"piecewise-constant","zoom-function":true,"property-function":true,"values":["map","viewport"],"default":"map","requires":["text-field","text-translate"]}},"paint_raster":{"raster-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true},"raster-hue-rotate":{"type":"number","default":0,"period":360,"function":"interpolated","zoom-function":true,"transition":true,"units":"degrees"},"raster-brightness-min":{"type":"number","function":"interpolated","zoom-function":true,"default":0,"minimum":0,"maximum":1,"transition":true},"raster-brightness-max":{"type":"number","function":"interpolated","zoom-function":true,"default":1,"minimum":0,"maximum":1,"transition":true},"raster-saturation":{"type":"number","default":0,"minimum":-1,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true},"raster-contrast":{"type":"number","default":0,"minimum":-1,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true},"raster-fade-duration":{"type":"number","default":300,"minimum":0,"function":"interpolated","zoom-function":true,"transition":true,"units":"milliseconds"}},"paint_background":{"background-color":{"type":"color","default":"#000000","function":"interpolated","zoom-function":true,"transition":true,"requires":[{"!":"background-pattern"}]},"background-pattern":{"type":"string","function":"piecewise-constant","zoom-function":true,"transition":true},"background-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"function":"interpolated","zoom-function":true,"transition":true}},"transition":{"duration":{"type":"number","default":300,"minimum":0,"units":"milliseconds"},"delay":{"type":"number","default":0,"minimum":0,"units":"milliseconds"}}} -},{}],1067:[function(require,module,exports){ -'use strict'; - -if (typeof module !== 'undefined' && module.exports) { - module.exports = isSupported; -} else if (window) { - window.mapboxgl = window.mapboxgl || {}; - window.mapboxgl.supported = isSupported; -} - -/** - * Test whether the current browser supports Mapbox GL JS - * @param {Object} options - * @param {boolean} [options.failIfMajorPerformanceCaveat=false] Return `false` - * if the performance of Mapbox GL JS would be dramatically worse than - * expected (i.e. a software renderer is would be used) - * @return {boolean} - */ -function isSupported(options) { - return !!( - isBrowser() && - isArraySupported() && - isFunctionSupported() && - isObjectSupported() && - isJSONSupported() && - isWorkerSupported() && - isUint8ClampedArraySupported() && - isWebGLSupportedCached(options && options.failIfMajorPerformanceCaveat) - ); -} - -function isBrowser() { - return typeof window !== 'undefined' && typeof document !== 'undefined'; -} - -function isArraySupported() { - return ( - Array.prototype && - Array.prototype.every && - Array.prototype.filter && - Array.prototype.forEach && - Array.prototype.indexOf && - Array.prototype.lastIndexOf && - Array.prototype.map && - Array.prototype.some && - Array.prototype.reduce && - Array.prototype.reduceRight && - Array.isArray - ); -} - -function isFunctionSupported() { - return Function.prototype && Function.prototype.bind; -} - -function isObjectSupported() { - return ( - Object.keys && - Object.create && - Object.getPrototypeOf && - Object.getOwnPropertyNames && - Object.isSealed && - Object.isFrozen && - Object.isExtensible && - Object.getOwnPropertyDescriptor && - Object.defineProperty && - Object.defineProperties && - Object.seal && - Object.freeze && - Object.preventExtensions - ); -} - -function isJSONSupported() { - return 'JSON' in window && 'parse' in JSON && 'stringify' in JSON; -} - -function isWorkerSupported() { - return 'Worker' in window; -} - -// IE11 only supports `Uint8ClampedArray` as of version -// [KB2929437](https://support.microsoft.com/en-us/kb/2929437) -function isUint8ClampedArraySupported() { - return 'Uint8ClampedArray' in window; -} - -var isWebGLSupportedCache = {}; -function isWebGLSupportedCached(failIfMajorPerformanceCaveat) { - - if (isWebGLSupportedCache[failIfMajorPerformanceCaveat] === undefined) { - isWebGLSupportedCache[failIfMajorPerformanceCaveat] = isWebGLSupported(failIfMajorPerformanceCaveat); - } - - return isWebGLSupportedCache[failIfMajorPerformanceCaveat]; -} - -isSupported.webGLContextAttributes = { - antialias: false, - alpha: true, - stencil: true, - depth: true -}; - -function isWebGLSupported(failIfMajorPerformanceCaveat) { - - var canvas = document.createElement('canvas'); - - var attributes = Object.create(isSupported.webGLContextAttributes); - attributes.failIfMajorPerformanceCaveat = failIfMajorPerformanceCaveat; - - if (canvas.probablySupportsContext) { - return ( - canvas.probablySupportsContext('webgl', attributes) || - canvas.probablySupportsContext('experimental-webgl', attributes) - ); - - } else if (canvas.supportsContext) { - return ( - canvas.supportsContext('webgl', attributes) || - canvas.supportsContext('experimental-webgl', attributes) - ); - - } else { - return ( - canvas.getContext('webgl', attributes) || - canvas.getContext('experimental-webgl', attributes) - ); - } -} - -},{}],1068:[function(require,module,exports){ -'use strict'; - -// lightweight Buffer shim for pbf browser build -// based on code from github.com/feross/buffer (MIT-licensed) - -module.exports = Buffer; - -var ieee754 = require('ieee754'); - -var BufferMethods; - -function Buffer(length) { - var arr; - if (length && length.length) { - arr = length; - length = arr.length; - } - var buf = new Uint8Array(length || 0); - if (arr) buf.set(arr); - - buf.readUInt32LE = BufferMethods.readUInt32LE; - buf.writeUInt32LE = BufferMethods.writeUInt32LE; - buf.readInt32LE = BufferMethods.readInt32LE; - buf.writeInt32LE = BufferMethods.writeInt32LE; - buf.readFloatLE = BufferMethods.readFloatLE; - buf.writeFloatLE = BufferMethods.writeFloatLE; - buf.readDoubleLE = BufferMethods.readDoubleLE; - buf.writeDoubleLE = BufferMethods.writeDoubleLE; - buf.toString = BufferMethods.toString; - buf.write = BufferMethods.write; - buf.slice = BufferMethods.slice; - buf.copy = BufferMethods.copy; - - buf._isBuffer = true; - return buf; -} - -var lastStr, lastStrEncoded; - -BufferMethods = { - readUInt32LE: function(pos) { - return ((this[pos]) | - (this[pos + 1] << 8) | - (this[pos + 2] << 16)) + - (this[pos + 3] * 0x1000000); - }, - - writeUInt32LE: function(val, pos) { - this[pos] = val; - this[pos + 1] = (val >>> 8); - this[pos + 2] = (val >>> 16); - this[pos + 3] = (val >>> 24); - }, - - readInt32LE: function(pos) { - return ((this[pos]) | - (this[pos + 1] << 8) | - (this[pos + 2] << 16)) + - (this[pos + 3] << 24); - }, - - readFloatLE: function(pos) { return ieee754.read(this, pos, true, 23, 4); }, - readDoubleLE: function(pos) { return ieee754.read(this, pos, true, 52, 8); }, - - writeFloatLE: function(val, pos) { return ieee754.write(this, val, pos, true, 23, 4); }, - writeDoubleLE: function(val, pos) { return ieee754.write(this, val, pos, true, 52, 8); }, - - toString: function(encoding, start, end) { - var str = '', - tmp = ''; - - start = start || 0; - end = Math.min(this.length, end || this.length); - - for (var i = start; i < end; i++) { - var ch = this[i]; - if (ch <= 0x7F) { - str += decodeURIComponent(tmp) + String.fromCharCode(ch); - tmp = ''; - } else { - tmp += '%' + ch.toString(16); - } - } - - str += decodeURIComponent(tmp); - - return str; - }, - - write: function(str, pos) { - var bytes = str === lastStr ? lastStrEncoded : encodeString(str); - for (var i = 0; i < bytes.length; i++) { - this[pos + i] = bytes[i]; - } - }, - - slice: function(start, end) { - return this.subarray(start, end); - }, - - copy: function(buf, pos) { - pos = pos || 0; - for (var i = 0; i < this.length; i++) { - buf[pos + i] = this[i]; - } - } -}; - -BufferMethods.writeInt32LE = BufferMethods.writeUInt32LE; - -Buffer.byteLength = function(str) { - lastStr = str; - lastStrEncoded = encodeString(str); - return lastStrEncoded.length; -}; - -Buffer.isBuffer = function(buf) { - return !!(buf && buf._isBuffer); -}; - -function encodeString(str) { - var length = str.length, - bytes = []; - - for (var i = 0, c, lead; i < length; i++) { - c = str.charCodeAt(i); // code point - - if (c > 0xD7FF && c < 0xE000) { - - if (lead) { - if (c < 0xDC00) { - bytes.push(0xEF, 0xBF, 0xBD); - lead = c; - continue; - - } else { - c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000; - lead = null; - } - - } else { - if (c > 0xDBFF || (i + 1 === length)) bytes.push(0xEF, 0xBF, 0xBD); - else lead = c; - - continue; - } - - } else if (lead) { - bytes.push(0xEF, 0xBF, 0xBD); - lead = null; - } - - if (c < 0x80) bytes.push(c); - else if (c < 0x800) bytes.push(c >> 0x6 | 0xC0, c & 0x3F | 0x80); - else if (c < 0x10000) bytes.push(c >> 0xC | 0xE0, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80); - else bytes.push(c >> 0x12 | 0xF0, c >> 0xC & 0x3F | 0x80, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80); - } - return bytes; -} - -},{"ieee754":1070}],1069:[function(require,module,exports){ -(function (global){ -'use strict'; - -module.exports = Pbf; - -var Buffer = global.Buffer || require('./buffer'); - -function Pbf(buf) { - this.buf = !Buffer.isBuffer(buf) ? new Buffer(buf || 0) : buf; - this.pos = 0; - this.length = this.buf.length; -} - -Pbf.Varint = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum -Pbf.Fixed64 = 1; // 64-bit: double, fixed64, sfixed64 -Pbf.Bytes = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields -Pbf.Fixed32 = 5; // 32-bit: float, fixed32, sfixed32 - -var SHIFT_LEFT_32 = (1 << 16) * (1 << 16), - SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32, - POW_2_63 = Math.pow(2, 63); - -Pbf.prototype = { - - destroy: function() { - this.buf = null; - }, - - // === READING ================================================================= - - readFields: function(readField, result, end) { - end = end || this.length; - - while (this.pos < end) { - var val = this.readVarint(), - tag = val >> 3, - startPos = this.pos; - - readField(tag, result, this); - - if (this.pos === startPos) this.skip(val); - } - return result; - }, - - readMessage: function(readField, result) { - return this.readFields(readField, result, this.readVarint() + this.pos); - }, - - readFixed32: function() { - var val = this.buf.readUInt32LE(this.pos); - this.pos += 4; - return val; - }, - - readSFixed32: function() { - var val = this.buf.readInt32LE(this.pos); - this.pos += 4; - return val; - }, - - // 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed) - - readFixed64: function() { - var val = this.buf.readUInt32LE(this.pos) + this.buf.readUInt32LE(this.pos + 4) * SHIFT_LEFT_32; - this.pos += 8; - return val; - }, - - readSFixed64: function() { - var val = this.buf.readUInt32LE(this.pos) + this.buf.readInt32LE(this.pos + 4) * SHIFT_LEFT_32; - this.pos += 8; - return val; - }, - - readFloat: function() { - var val = this.buf.readFloatLE(this.pos); - this.pos += 4; - return val; - }, - - readDouble: function() { - var val = this.buf.readDoubleLE(this.pos); - this.pos += 8; - return val; - }, - - readVarint: function() { - var buf = this.buf, - val, b; - - b = buf[this.pos++]; val = b & 0x7f; if (b < 0x80) return val; - b = buf[this.pos++]; val |= (b & 0x7f) << 7; if (b < 0x80) return val; - b = buf[this.pos++]; val |= (b & 0x7f) << 14; if (b < 0x80) return val; - b = buf[this.pos++]; val |= (b & 0x7f) << 21; if (b < 0x80) return val; - - return readVarintRemainder(val, this); - }, - - readVarint64: function() { - var startPos = this.pos, - val = this.readVarint(); - - if (val < POW_2_63) return val; - - var pos = this.pos - 2; - while (this.buf[pos] === 0xff) pos--; - if (pos < startPos) pos = startPos; - - val = 0; - for (var i = 0; i < pos - startPos + 1; i++) { - var b = ~this.buf[startPos + i] & 0x7f; - val += i < 4 ? b << i * 7 : b * Math.pow(2, i * 7); - } - - return -val - 1; - }, - - readSVarint: function() { - var num = this.readVarint(); - return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding - }, - - readBoolean: function() { - return Boolean(this.readVarint()); - }, - - readString: function() { - var end = this.readVarint() + this.pos, - str = this.buf.toString('utf8', this.pos, end); - this.pos = end; - return str; - }, - - readBytes: function() { - var end = this.readVarint() + this.pos, - buffer = this.buf.slice(this.pos, end); - this.pos = end; - return buffer; - }, - - // verbose for performance reasons; doesn't affect gzipped size - - readPackedVarint: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readVarint()); - return arr; - }, - readPackedSVarint: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readSVarint()); - return arr; - }, - readPackedBoolean: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readBoolean()); - return arr; - }, - readPackedFloat: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readFloat()); - return arr; - }, - readPackedDouble: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readDouble()); - return arr; - }, - readPackedFixed32: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readFixed32()); - return arr; - }, - readPackedSFixed32: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readSFixed32()); - return arr; - }, - readPackedFixed64: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readFixed64()); - return arr; - }, - readPackedSFixed64: function() { - var end = this.readVarint() + this.pos, arr = []; - while (this.pos < end) arr.push(this.readSFixed64()); - return arr; - }, - - skip: function(val) { - var type = val & 0x7; - if (type === Pbf.Varint) while (this.buf[this.pos++] > 0x7f) {} - else if (type === Pbf.Bytes) this.pos = this.readVarint() + this.pos; - else if (type === Pbf.Fixed32) this.pos += 4; - else if (type === Pbf.Fixed64) this.pos += 8; - else throw new Error('Unimplemented type: ' + type); - }, - - // === WRITING ================================================================= - - writeTag: function(tag, type) { - this.writeVarint((tag << 3) | type); - }, - - realloc: function(min) { - var length = this.length || 16; - - while (length < this.pos + min) length *= 2; - - if (length !== this.length) { - var buf = new Buffer(length); - this.buf.copy(buf); - this.buf = buf; - this.length = length; - } - }, - - finish: function() { - this.length = this.pos; - this.pos = 0; - return this.buf.slice(0, this.length); - }, - - writeFixed32: function(val) { - this.realloc(4); - this.buf.writeUInt32LE(val, this.pos); - this.pos += 4; - }, - - writeSFixed32: function(val) { - this.realloc(4); - this.buf.writeInt32LE(val, this.pos); - this.pos += 4; - }, - - writeFixed64: function(val) { - this.realloc(8); - this.buf.writeInt32LE(val & -1, this.pos); - this.buf.writeUInt32LE(Math.floor(val * SHIFT_RIGHT_32), this.pos + 4); - this.pos += 8; - }, - - writeSFixed64: function(val) { - this.realloc(8); - this.buf.writeInt32LE(val & -1, this.pos); - this.buf.writeInt32LE(Math.floor(val * SHIFT_RIGHT_32), this.pos + 4); - this.pos += 8; - }, - - writeVarint: function(val) { - val = +val; - - if (val > 0xfffffff) { - writeBigVarint(val, this); - return; - } - - this.realloc(4); - - this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return; - this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return; - this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return; - this.buf[this.pos++] = (val >>> 7) & 0x7f; - }, - - writeSVarint: function(val) { - this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2); - }, - - writeBoolean: function(val) { - this.writeVarint(Boolean(val)); - }, - - writeString: function(str) { - str = String(str); - var bytes = Buffer.byteLength(str); - this.writeVarint(bytes); - this.realloc(bytes); - this.buf.write(str, this.pos); - this.pos += bytes; - }, - - writeFloat: function(val) { - this.realloc(4); - this.buf.writeFloatLE(val, this.pos); - this.pos += 4; - }, - - writeDouble: function(val) { - this.realloc(8); - this.buf.writeDoubleLE(val, this.pos); - this.pos += 8; - }, - - writeBytes: function(buffer) { - var len = buffer.length; - this.writeVarint(len); - this.realloc(len); - for (var i = 0; i < len; i++) this.buf[this.pos++] = buffer[i]; - }, - - writeRawMessage: function(fn, obj) { - this.pos++; // reserve 1 byte for short message length - - // write the message directly to the buffer and see how much was written - var startPos = this.pos; - fn(obj, this); - var len = this.pos - startPos; - - if (len >= 0x80) reallocForRawMessage(startPos, len, this); - - // finally, write the message length in the reserved place and restore the position - this.pos = startPos - 1; - this.writeVarint(len); - this.pos += len; - }, - - writeMessage: function(tag, fn, obj) { - this.writeTag(tag, Pbf.Bytes); - this.writeRawMessage(fn, obj); - }, - - writePackedVarint: function(tag, arr) { this.writeMessage(tag, writePackedVarint, arr); }, - writePackedSVarint: function(tag, arr) { this.writeMessage(tag, writePackedSVarint, arr); }, - writePackedBoolean: function(tag, arr) { this.writeMessage(tag, writePackedBoolean, arr); }, - writePackedFloat: function(tag, arr) { this.writeMessage(tag, writePackedFloat, arr); }, - writePackedDouble: function(tag, arr) { this.writeMessage(tag, writePackedDouble, arr); }, - writePackedFixed32: function(tag, arr) { this.writeMessage(tag, writePackedFixed32, arr); }, - writePackedSFixed32: function(tag, arr) { this.writeMessage(tag, writePackedSFixed32, arr); }, - writePackedFixed64: function(tag, arr) { this.writeMessage(tag, writePackedFixed64, arr); }, - writePackedSFixed64: function(tag, arr) { this.writeMessage(tag, writePackedSFixed64, arr); }, - - writeBytesField: function(tag, buffer) { - this.writeTag(tag, Pbf.Bytes); - this.writeBytes(buffer); - }, - writeFixed32Field: function(tag, val) { - this.writeTag(tag, Pbf.Fixed32); - this.writeFixed32(val); - }, - writeSFixed32Field: function(tag, val) { - this.writeTag(tag, Pbf.Fixed32); - this.writeSFixed32(val); - }, - writeFixed64Field: function(tag, val) { - this.writeTag(tag, Pbf.Fixed64); - this.writeFixed64(val); - }, - writeSFixed64Field: function(tag, val) { - this.writeTag(tag, Pbf.Fixed64); - this.writeSFixed64(val); - }, - writeVarintField: function(tag, val) { - this.writeTag(tag, Pbf.Varint); - this.writeVarint(val); - }, - writeSVarintField: function(tag, val) { - this.writeTag(tag, Pbf.Varint); - this.writeSVarint(val); - }, - writeStringField: function(tag, str) { - this.writeTag(tag, Pbf.Bytes); - this.writeString(str); - }, - writeFloatField: function(tag, val) { - this.writeTag(tag, Pbf.Fixed32); - this.writeFloat(val); - }, - writeDoubleField: function(tag, val) { - this.writeTag(tag, Pbf.Fixed64); - this.writeDouble(val); - }, - writeBooleanField: function(tag, val) { - this.writeVarintField(tag, Boolean(val)); - } -}; - -function readVarintRemainder(val, pbf) { - var buf = pbf.buf, b; - - b = buf[pbf.pos++]; val += (b & 0x7f) * 0x10000000; if (b < 0x80) return val; - b = buf[pbf.pos++]; val += (b & 0x7f) * 0x800000000; if (b < 0x80) return val; - b = buf[pbf.pos++]; val += (b & 0x7f) * 0x40000000000; if (b < 0x80) return val; - b = buf[pbf.pos++]; val += (b & 0x7f) * 0x2000000000000; if (b < 0x80) return val; - b = buf[pbf.pos++]; val += (b & 0x7f) * 0x100000000000000; if (b < 0x80) return val; - b = buf[pbf.pos++]; val += (b & 0x7f) * 0x8000000000000000; if (b < 0x80) return val; - - throw new Error('Expected varint not more than 10 bytes'); -} - -function writeBigVarint(val, pbf) { - pbf.realloc(10); - - var maxPos = pbf.pos + 10; - - while (val >= 1) { - if (pbf.pos >= maxPos) throw new Error('Given varint doesn\'t fit into 10 bytes'); - var b = val & 0xff; - pbf.buf[pbf.pos++] = b | (val >= 0x80 ? 0x80 : 0); - val /= 0x80; - } -} - -function reallocForRawMessage(startPos, len, pbf) { - var extraLen = - len <= 0x3fff ? 1 : - len <= 0x1fffff ? 2 : - len <= 0xfffffff ? 3 : Math.ceil(Math.log(len) / (Math.LN2 * 7)); - - // if 1 byte isn't enough for encoding message length, shift the data to the right - pbf.realloc(extraLen); - for (var i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i]; -} - -function writePackedVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]); } -function writePackedSVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]); } -function writePackedFloat(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]); } -function writePackedDouble(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]); } -function writePackedBoolean(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]); } -function writePackedFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]); } -function writePackedSFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]); } -function writePackedFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]); } -function writePackedSFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]); } - -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{"./buffer":1068}],1070:[function(require,module,exports){ -arguments[4][35][0].apply(exports,arguments) -},{"dup":35}],1071:[function(require,module,exports){ -'use strict'; - -module.exports = Point; - -function Point(x, y) { - this.x = x; - this.y = y; -} - -Point.prototype = { - clone: function() { return new Point(this.x, this.y); }, - - add: function(p) { return this.clone()._add(p); }, - sub: function(p) { return this.clone()._sub(p); }, - mult: function(k) { return this.clone()._mult(k); }, - div: function(k) { return this.clone()._div(k); }, - rotate: function(a) { return this.clone()._rotate(a); }, - matMult: function(m) { return this.clone()._matMult(m); }, - unit: function() { return this.clone()._unit(); }, - perp: function() { return this.clone()._perp(); }, - round: function() { return this.clone()._round(); }, - - mag: function() { - return Math.sqrt(this.x * this.x + this.y * this.y); - }, - - equals: function(p) { - return this.x === p.x && - this.y === p.y; - }, - - dist: function(p) { - return Math.sqrt(this.distSqr(p)); - }, - - distSqr: function(p) { - var dx = p.x - this.x, - dy = p.y - this.y; - return dx * dx + dy * dy; - }, - - angle: function() { - return Math.atan2(this.y, this.x); - }, - - angleTo: function(b) { - return Math.atan2(this.y - b.y, this.x - b.x); - }, - - angleWith: function(b) { - return this.angleWithSep(b.x, b.y); - }, - - // Find the angle of the two vectors, solving the formula for the cross product a x b = |a||b|sin(θ) for θ. - angleWithSep: function(x, y) { - return Math.atan2( - this.x * y - this.y * x, - this.x * x + this.y * y); - }, - - _matMult: function(m) { - var x = m[0] * this.x + m[1] * this.y, - y = m[2] * this.x + m[3] * this.y; - this.x = x; - this.y = y; - return this; - }, - - _add: function(p) { - this.x += p.x; - this.y += p.y; - return this; - }, - - _sub: function(p) { - this.x -= p.x; - this.y -= p.y; - return this; - }, - - _mult: function(k) { - this.x *= k; - this.y *= k; - return this; - }, - - _div: function(k) { - this.x /= k; - this.y /= k; - return this; - }, - - _unit: function() { - this._div(this.mag()); - return this; - }, - - _perp: function() { - var y = this.y; - this.y = this.x; - this.x = -y; - return this; - }, - - _rotate: function(angle) { - var cos = Math.cos(angle), - sin = Math.sin(angle), - x = cos * this.x - sin * this.y, - y = sin * this.x + cos * this.y; - this.x = x; - this.y = y; - return this; - }, - - _round: function() { - this.x = Math.round(this.x); - this.y = Math.round(this.y); - return this; - } -}; - -// constructs Point from an array if necessary -Point.convert = function (a) { - if (a instanceof Point) { - return a; - } - if (Array.isArray(a)) { - return new Point(a[0], a[1]); - } - return a; -}; - -},{}],1072:[function(require,module,exports){ -'use strict'; - -module.exports = partialSort; - -// Floyd-Rivest selection algorithm: -// Rearrange items so that all items in the [left, k] range are smaller than all items in (k, right]; -// The k-th element will have the (k - left + 1)th smallest value in [left, right] - -function partialSort(arr, k, left, right, compare) { - left = left || 0; - right = right || (arr.length - 1); - compare = compare || defaultCompare; - - while (right > left) { - if (right - left > 600) { - var n = right - left + 1; - var m = k - left + 1; - var z = Math.log(n); - var s = 0.5 * Math.exp(2 * z / 3); - var sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1); - var newLeft = Math.max(left, Math.floor(k - m * s / n + sd)); - var newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd)); - partialSort(arr, k, newLeft, newRight, compare); - } - - var t = arr[k]; - var i = left; - var j = right; - - swap(arr, left, k); - if (compare(arr[right], t) > 0) swap(arr, left, right); - - while (i < j) { - swap(arr, i, j); - i++; - j--; - while (compare(arr[i], t) < 0) i++; - while (compare(arr[j], t) > 0) j--; - } - - if (compare(arr[left], t) === 0) swap(arr, left, j); - else { - j++; - swap(arr, j, right); - } - - if (j <= k) left = j + 1; - if (k <= j) right = j - 1; - } -} - -function swap(arr, i, j) { - var tmp = arr[i]; - arr[i] = arr[j]; - arr[j] = tmp; -} - -function defaultCompare(a, b) { - return a < b ? -1 : a > b ? 1 : 0; -} - -},{}],1073:[function(require,module,exports){ -// Copyright 2014 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - -void (function(root, factory) { - if (typeof define === "function" && define.amd) { - define(factory) - } else if (typeof exports === "object") { - module.exports = factory() - } else { - root.resolveUrl = factory() - } -}(this, function() { - - function resolveUrl(/* ...urls */) { - var numUrls = arguments.length - - if (numUrls === 0) { - throw new Error("resolveUrl requires at least one argument; got none.") - } - - var base = document.createElement("base") - base.href = arguments[0] - - if (numUrls === 1) { - return base.href - } - - var head = document.getElementsByTagName("head")[0] - head.insertBefore(base, head.firstChild) - - var a = document.createElement("a") - var resolved - - for (var index = 1; index < numUrls; index++) { - a.href = arguments[index] - resolved = a.href - base.href = resolved - } - - head.removeChild(base) - - return resolved - } - - return resolveUrl - -})); - -},{}],1074:[function(require,module,exports){ -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global.ShelfPack = factory()); -}(this, function () { - -/** - * Create a new ShelfPack bin allocator. - * - * Uses the Shelf Best Height Fit algorithm from - * http://clb.demon.fi/files/RectangleBinPack.pdf - * - * @class ShelfPack - * @param {number} [w=64] Initial width of the sprite - * @param {number} [h=64] Initial width of the sprite - * @param {Object} [options] - * @param {boolean} [options.autoResize=false] If `true`, the sprite will automatically grow - * @example - * var sprite = new ShelfPack(64, 64, { autoResize: false }); - */ -function ShelfPack(w, h, options) { - options = options || {}; - this.w = w || 64; - this.h = h || 64; - this.autoResize = !!options.autoResize; - this.shelves = []; - this.stats = {}; - this.count = function(h) { - this.stats[h] = (this.stats[h] | 0) + 1; - }; -} - -/** - * Batch pack multiple bins into the sprite. - * - * @param {Array} bins Array of requested bins - each object should have `width`, `height` (or `w`, `h`) properties - * @param {Object} [options] - * @param {boolean} [options.inPlace=false] If `true`, the supplied bin objects will be updated inplace with `x` and `y` properties - * @returns {Array} Array of allocated bins - each bin is an object with `x`, `y`, `w`, `h` properties - * @example - * var bins = [ - * { id: 'a', width: 12, height: 12 }, - * { id: 'b', width: 12, height: 16 }, - * { id: 'c', width: 12, height: 24 } - * ]; - * var results = sprite.pack(bins, { inPlace: false }); - */ -ShelfPack.prototype.pack = function(bins, options) { - bins = [].concat(bins); - options = options || {}; - - var results = [], - w, h, allocation; - - for (var i = 0; i < bins.length; i++) { - w = bins[i].w || bins[i].width; - h = bins[i].h || bins[i].height; - if (w && h) { - allocation = this.packOne(w, h); - if (!allocation) { - continue; - } - if (options.inPlace) { - bins[i].x = allocation.x; - bins[i].y = allocation.y; - } - results.push(allocation); - } - } - - // Shrink the width/height of the sprite to the bare minimum. - // Since shelf-pack doubles first width, then height when running out of shelf space - // this can result in fairly large unused space both in width and height if that happens - // towards the end of bin packing. - if (this.shelves.length > 0) { - var w2 = 0; - var h2 = 0; - - for (var j = 0; j < this.shelves.length; j++) { - var shelf = this.shelves[j]; - h2 += shelf.h; - w2 = Math.max(shelf.w - shelf.free, w2); - } - - this.resize(w2, h2); - } - - return results; -}; - -/** - * Pack a single bin into the sprite. - * - * @param {number} w Width of the bin to allocate - * @param {number} h Height of the bin to allocate - * @returns {Object} Allocated bin object with `x`, `y`, `w`, `h` properties, or `null` if allocation failed - * @example - * var results = sprite.packOne(12, 16); - */ -ShelfPack.prototype.packOne = function(w, h) { - var y = 0, - best = { shelf: -1, waste: Infinity }, - shelf, waste; - - // find the best shelf - for (var i = 0; i < this.shelves.length; i++) { - shelf = this.shelves[i]; - y += shelf.h; - - // exactly the right height with width to spare, pack it.. - if (h === shelf.h && w <= shelf.free) { - this.count(h); - return shelf.alloc(w, h); - } - // not enough height or width, skip it.. - if (h > shelf.h || w > shelf.free) { - continue; - } - // maybe enough height or width, minimize waste.. - if (h < shelf.h && w <= shelf.free) { - waste = shelf.h - h; - if (waste < best.waste) { - best.waste = waste; - best.shelf = i; - } - } - } - - if (best.shelf !== -1) { - shelf = this.shelves[best.shelf]; - this.count(h); - return shelf.alloc(w, h); - } - - // add shelf.. - if (h <= (this.h - y) && w <= this.w) { - shelf = new Shelf(y, this.w, h); - this.shelves.push(shelf); - this.count(h); - return shelf.alloc(w, h); - } - - // no more space.. - // If `autoResize` option is set, grow the sprite as follows: - // * double whichever sprite dimension is smaller (`w1` or `h1`) - // * if sprite dimensions are equal, grow width before height - // * accomodate very large bin requests (big `w` or `h`) - if (this.autoResize) { - var h1, h2, w1, w2; - - h1 = h2 = this.h; - w1 = w2 = this.w; - - if (w1 <= h1 || w > w1) { // grow width.. - w2 = Math.max(w, w1) * 2; - } - if (h1 < w1 || h > h1) { // grow height.. - h2 = Math.max(h, h1) * 2; - } - - this.resize(w2, h2); - return this.packOne(w, h); // retry - } - - return null; -}; - -/** - * Clear the sprite. - * - * @example - * sprite.clear(); - */ -ShelfPack.prototype.clear = function() { - this.shelves = []; - this.stats = {}; -}; - -/** - * Resize the sprite. - * - * @param {number} w Requested new sprite width - * @param {number} h Requested new sprite height - * @returns {boolean} `true` if resize succeeded, `false` if failed - * @example - * sprite.resize(256, 256); - */ -ShelfPack.prototype.resize = function(w, h) { - this.w = w; - this.h = h; - for (var i = 0; i < this.shelves.length; i++) { - this.shelves[i].resize(w); - } - return true; -}; - - - -/** - * Create a new Shelf. - * - * @private - * @class Shelf - * @param {number} y Top coordinate of the new shelf - * @param {number} w Width of the new shelf - * @param {number} h Height of the new shelf - * @example - * var shelf = new Shelf(64, 512, 24); - */ -function Shelf(y, w, h) { - this.x = 0; - this.y = y; - this.w = this.free = w; - this.h = h; -} - -/** - * Allocate a single bin into the shelf. - * - * @private - * @param {number} w Width of the bin to allocate - * @param {number} h Height of the bin to allocate - * @returns {Object} Allocated bin object with `x`, `y`, `w`, `h` properties, or `null` if allocation failed - * @example - * shelf.alloc(12, 16); - */ -Shelf.prototype.alloc = function(w, h) { - if (w > this.free || h > this.h) { - return null; - } - var x = this.x; - this.x += w; - this.free -= w; - return { x: x, y: this.y, w: w, h: h, width: w, height: h }; -}; - -/** - * Resize the shelf. - * - * @private - * @param {number} w Requested new width of the shelf - * @returns {boolean} true if resize succeeded, false if failed - * @example - * shelf.resize(512); - */ -Shelf.prototype.resize = function(w) { - this.free += (w - this.w); - this.w = w; - return true; -}; - -return ShelfPack; - -})); -},{}],1075:[function(require,module,exports){ -'use strict'; - -var kdbush = require('kdbush'); - -module.exports = supercluster; - -function supercluster(options) { - return new SuperCluster(options); -} - -function SuperCluster(options) { - this.options = extend(Object.create(this.options), options); - this.trees = new Array(this.options.maxZoom + 1); -} - -SuperCluster.prototype = { - options: { - minZoom: 0, // min zoom to generate clusters on - maxZoom: 16, // max zoom level to cluster the points on - radius: 40, // cluster radius in pixels - extent: 512, // tile extent (radius is calculated relative to it) - nodeSize: 64, // size of the KD-tree leaf node, affects performance - log: false // whether to log timing info - }, - - load: function (points) { - var log = this.options.log; - - if (log) console.time('total time'); - - var timerId = 'prepare ' + points.length + ' points'; - if (log) console.time(timerId); - - this.points = points; - - // generate a cluster object for each point - var clusters = points.map(createPointCluster); - if (log) console.timeEnd(timerId); - - // cluster points on max zoom, then cluster the results on previous zoom, etc.; - // results in a cluster hierarchy across zoom levels - for (var z = this.options.maxZoom; z >= this.options.minZoom; z--) { - var now = +Date.now(); - - // index input points into a KD-tree - this.trees[z + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); - - clusters = this._cluster(clusters, z); // create a new set of clusters for the zoom - - if (log) console.log('z%d: %d clusters in %dms', z, clusters.length, +Date.now() - now); - } - - // index top-level clusters - this.trees[this.options.minZoom] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); - - if (log) console.timeEnd('total time'); - - return this; - }, - - getClusters: function (bbox, zoom) { - var tree = this.trees[this._limitZoom(zoom)]; - var ids = tree.range(lngX(bbox[0]), latY(bbox[3]), lngX(bbox[2]), latY(bbox[1])); - var clusters = []; - for (var i = 0; i < ids.length; i++) { - var c = tree.points[ids[i]]; - clusters.push(c.id !== -1 ? this.points[c.id] : getClusterJSON(c)); - } - return clusters; - }, - - getTile: function (z, x, y) { - var tree = this.trees[this._limitZoom(z)]; - var z2 = Math.pow(2, z); - var extent = this.options.extent; - var r = this.options.radius; - var p = r / extent; - var top = (y - p) / z2; - var bottom = (y + 1 + p) / z2; - - var tile = { - features: [] - }; - - this._addTileFeatures( - tree.range((x - p) / z2, top, (x + 1 + p) / z2, bottom), - tree.points, x, y, z2, tile); - - if (x === 0) { - this._addTileFeatures( - tree.range(1 - p / z2, top, 1, bottom), - tree.points, z2, y, z2, tile); - } - if (x === z2 - 1) { - this._addTileFeatures( - tree.range(0, top, p / z2, bottom), - tree.points, -1, y, z2, tile); - } - - return tile.features.length ? tile : null; - }, - - _addTileFeatures: function (ids, points, x, y, z2, tile) { - for (var i = 0; i < ids.length; i++) { - var c = points[ids[i]]; - tile.features.push({ - type: 1, - geometry: [[ - Math.round(this.options.extent * (c.x * z2 - x)), - Math.round(this.options.extent * (c.y * z2 - y)) - ]], - tags: c.id !== -1 ? this.points[c.id].properties : getClusterProperties(c) - }); - } - }, - - _limitZoom: function (z) { - return Math.max(this.options.minZoom, Math.min(z, this.options.maxZoom + 1)); - }, - - _cluster: function (points, zoom) { - var clusters = []; - var r = this.options.radius / (this.options.extent * Math.pow(2, zoom)); - - // loop through each point - for (var i = 0; i < points.length; i++) { - var p = points[i]; - // if we've already visited the point at this zoom level, skip it - if (p.zoom <= zoom) continue; - p.zoom = zoom; - - // find all nearby points - var tree = this.trees[zoom + 1]; - var neighborIds = tree.within(p.x, p.y, r); - - var foundNeighbors = false; - var numPoints = p.numPoints; - var wx = p.x * numPoints; - var wy = p.y * numPoints; - - for (var j = 0; j < neighborIds.length; j++) { - var b = tree.points[neighborIds[j]]; - // filter out neighbors that are too far or already processed - if (zoom < b.zoom) { - foundNeighbors = true; - b.zoom = zoom; // save the zoom (so it doesn't get processed twice) - wx += b.x * b.numPoints; // accumulate coordinates for calculating weighted center - wy += b.y * b.numPoints; - numPoints += b.numPoints; - } - } - - clusters.push(foundNeighbors ? createCluster(wx / numPoints, wy / numPoints, numPoints, -1) : p); - } - - return clusters; - } -}; - -function createCluster(x, y, numPoints, id) { - return { - x: x, // weighted cluster center - y: y, - zoom: Infinity, // the last zoom the cluster was processed at - id: id, // index of the source feature in the original input array - numPoints: numPoints - }; -} - -function createPointCluster(p, i) { - var coords = p.geometry.coordinates; - return createCluster(lngX(coords[0]), latY(coords[1]), 1, i); -} - -function getClusterJSON(cluster) { - return { - type: 'Feature', - properties: getClusterProperties(cluster), - geometry: { - type: 'Point', - coordinates: [xLng(cluster.x), yLat(cluster.y)] - } - }; -} - -function getClusterProperties(cluster) { - var count = cluster.numPoints; - var abbrev = count >= 10000 ? Math.round(count / 1000) + 'k' : - count >= 1000 ? (Math.round(count / 100) / 10) + 'k' : count; - return { - cluster: true, - point_count: count, - point_count_abbreviated: abbrev - }; -} - -// longitude/latitude to spherical mercator in [0..1] range -function lngX(lng) { - return lng / 360 + 0.5; -} -function latY(lat) { - var sin = Math.sin(lat * Math.PI / 180), - y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI); - return y < 0 ? 0 : - y > 1 ? 1 : y; -} - -// spherical mercator to longitude/latitude -function xLng(x) { - return (x - 0.5) * 360; -} -function yLat(y) { - var y2 = (180 - y * 360) * Math.PI / 180; - return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90; -} - -function extend(dest, src) { - for (var id in src) dest[id] = src[id]; - return dest; -} - -function getX(p) { - return p.x; -} -function getY(p) { - return p.y; -} - -},{"kdbush":1076}],1076:[function(require,module,exports){ -'use strict'; - -var sort = require('./sort'); -var range = require('./range'); -var within = require('./within'); - -module.exports = kdbush; - -function kdbush(points, getX, getY, nodeSize, ArrayType) { - return new KDBush(points, getX, getY, nodeSize, ArrayType); -} - -function KDBush(points, getX, getY, nodeSize, ArrayType) { - getX = getX || defaultGetX; - getY = getY || defaultGetY; - ArrayType = ArrayType || Array; - - this.nodeSize = nodeSize || 64; - this.points = points; - - this.ids = new ArrayType(points.length); - this.coords = new ArrayType(points.length * 2); - - for (var i = 0; i < points.length; i++) { - this.ids[i] = i; - this.coords[2 * i] = getX(points[i]); - this.coords[2 * i + 1] = getY(points[i]); - } - - sort(this.ids, this.coords, this.nodeSize, 0, this.ids.length - 1, 0); -} - -KDBush.prototype = { - range: function (minX, minY, maxX, maxY) { - return range(this.ids, this.coords, minX, minY, maxX, maxY, this.nodeSize); - }, - - within: function (x, y, r) { - return within(this.ids, this.coords, x, y, r, this.nodeSize); - } -}; - -function defaultGetX(p) { return p[0]; } -function defaultGetY(p) { return p[1]; } - -},{"./range":1077,"./sort":1078,"./within":1079}],1077:[function(require,module,exports){ -'use strict'; - -module.exports = range; - -function range(ids, coords, minX, minY, maxX, maxY, nodeSize) { - var stack = [0, ids.length - 1, 0]; - var result = []; - var x, y; - - while (stack.length) { - var axis = stack.pop(); - var right = stack.pop(); - var left = stack.pop(); - - if (right - left <= nodeSize) { - for (var i = left; i <= right; i++) { - x = coords[2 * i]; - y = coords[2 * i + 1]; - if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]); - } - continue; - } - - var m = Math.floor((left + right) / 2); - - x = coords[2 * m]; - y = coords[2 * m + 1]; - - if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]); - - var nextAxis = (axis + 1) % 2; - - if (axis === 0 ? minX <= x : minY <= y) { - stack.push(left); - stack.push(m - 1); - stack.push(nextAxis); - } - if (axis === 0 ? maxX >= x : maxY >= y) { - stack.push(m + 1); - stack.push(right); - stack.push(nextAxis); - } - } - - return result; -} - -},{}],1078:[function(require,module,exports){ -'use strict'; - -module.exports = sortKD; - -function sortKD(ids, coords, nodeSize, left, right, depth) { - if (right - left <= nodeSize) return; - - var m = Math.floor((left + right) / 2); - - select(ids, coords, m, left, right, depth % 2); - - sortKD(ids, coords, nodeSize, left, m - 1, depth + 1); - sortKD(ids, coords, nodeSize, m + 1, right, depth + 1); -} - -function select(ids, coords, k, left, right, inc) { - - while (right > left) { - if (right - left > 600) { - var n = right - left + 1; - var m = k - left + 1; - var z = Math.log(n); - var s = 0.5 * Math.exp(2 * z / 3); - var sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1); - var newLeft = Math.max(left, Math.floor(k - m * s / n + sd)); - var newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd)); - select(ids, coords, k, newLeft, newRight, inc); - } - - var t = coords[2 * k + inc]; - var i = left; - var j = right; - - swapItem(ids, coords, left, k); - if (coords[2 * right + inc] > t) swapItem(ids, coords, left, right); - - while (i < j) { - swapItem(ids, coords, i, j); - i++; - j--; - while (coords[2 * i + inc] < t) i++; - while (coords[2 * j + inc] > t) j--; - } - - if (coords[2 * left + inc] === t) swapItem(ids, coords, left, j); - else { - j++; - swapItem(ids, coords, j, right); - } - - if (j <= k) left = j + 1; - if (k <= j) right = j - 1; - } -} - -function swapItem(ids, coords, i, j) { - swap(ids, i, j); - swap(coords, 2 * i, 2 * j); - swap(coords, 2 * i + 1, 2 * j + 1); -} - -function swap(arr, i, j) { - var tmp = arr[i]; - arr[i] = arr[j]; - arr[j] = tmp; -} - -},{}],1079:[function(require,module,exports){ -'use strict'; - -module.exports = within; - -function within(ids, coords, qx, qy, r, nodeSize) { - var stack = [0, ids.length - 1, 0]; - var result = []; - var r2 = r * r; - - while (stack.length) { - var axis = stack.pop(); - var right = stack.pop(); - var left = stack.pop(); - - if (right - left <= nodeSize) { - for (var i = left; i <= right; i++) { - if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]); - } - continue; - } - - var m = Math.floor((left + right) / 2); - - var x = coords[2 * m]; - var y = coords[2 * m + 1]; - - if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]); - - var nextAxis = (axis + 1) % 2; - - if (axis === 0 ? qx - r <= x : qy - r <= y) { - stack.push(left); - stack.push(m - 1); - stack.push(nextAxis); - } - if (axis === 0 ? qx + r >= x : qy + r >= y) { - stack.push(m + 1); - stack.push(right); - stack.push(nextAxis); - } - } - - return result; -} - -function sqDist(ax, ay, bx, by) { - var dx = ax - bx; - var dy = ay - by; - return dx * dx + dy * dy; -} - -},{}],1080:[function(require,module,exports){ -/* - * Copyright (C) 2008 Apple Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Ported from Webkit - * http://svn.webkit.org/repository/webkit/trunk/Source/WebCore/platform/graphics/UnitBezier.h - */ - -module.exports = UnitBezier; - -function UnitBezier(p1x, p1y, p2x, p2y) { - // Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1). - this.cx = 3.0 * p1x; - this.bx = 3.0 * (p2x - p1x) - this.cx; - this.ax = 1.0 - this.cx - this.bx; - - this.cy = 3.0 * p1y; - this.by = 3.0 * (p2y - p1y) - this.cy; - this.ay = 1.0 - this.cy - this.by; - - this.p1x = p1x; - this.p1y = p2y; - this.p2x = p2x; - this.p2y = p2y; -} - -UnitBezier.prototype.sampleCurveX = function(t) { - // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. - return ((this.ax * t + this.bx) * t + this.cx) * t; -}; - -UnitBezier.prototype.sampleCurveY = function(t) { - return ((this.ay * t + this.by) * t + this.cy) * t; -}; - -UnitBezier.prototype.sampleCurveDerivativeX = function(t) { - return (3.0 * this.ax * t + 2.0 * this.bx) * t + this.cx; -}; - -UnitBezier.prototype.solveCurveX = function(x, epsilon) { - if (typeof epsilon === 'undefined') epsilon = 1e-6; - - var t0, t1, t2, x2, i; - - // First try a few iterations of Newton's method -- normally very fast. - for (t2 = x, i = 0; i < 8; i++) { - - x2 = this.sampleCurveX(t2) - x; - if (Math.abs(x2) < epsilon) return t2; - - var d2 = this.sampleCurveDerivativeX(t2); - if (Math.abs(d2) < 1e-6) break; - - t2 = t2 - x2 / d2; - } - - // Fall back to the bisection method for reliability. - t0 = 0.0; - t1 = 1.0; - t2 = x; - - if (t2 < t0) return t0; - if (t2 > t1) return t1; - - while (t0 < t1) { - - x2 = this.sampleCurveX(t2); - if (Math.abs(x2 - x) < epsilon) return t2; - - if (x > x2) { - t0 = t2; - } else { - t1 = t2; - } - - t2 = (t1 - t0) * 0.5 + t0; - } - - // Failure. - return t2; -}; - -UnitBezier.prototype.solve = function(x, epsilon) { - return this.sampleCurveY(this.solveCurveX(x, epsilon)); -}; - -},{}],1081:[function(require,module,exports){ -module.exports.VectorTile = require('./lib/vectortile.js'); -module.exports.VectorTileFeature = require('./lib/vectortilefeature.js'); -module.exports.VectorTileLayer = require('./lib/vectortilelayer.js'); - -},{"./lib/vectortile.js":1082,"./lib/vectortilefeature.js":1083,"./lib/vectortilelayer.js":1084}],1082:[function(require,module,exports){ -'use strict'; - -var VectorTileLayer = require('./vectortilelayer'); - -module.exports = VectorTile; - -function VectorTile(pbf, end) { - this.layers = pbf.readFields(readTile, {}, end); -} - -function readTile(tag, layers, pbf) { - if (tag === 3) { - var layer = new VectorTileLayer(pbf, pbf.readVarint() + pbf.pos); - if (layer.length) layers[layer.name] = layer; - } -} - - -},{"./vectortilelayer":1084}],1083:[function(require,module,exports){ -'use strict'; - -var Point = require('point-geometry'); - -module.exports = VectorTileFeature; - -function VectorTileFeature(pbf, end, extent, keys, values) { - // Public - this.properties = {}; - this.extent = extent; - this.type = 0; - - // Private - this._pbf = pbf; - this._geometry = -1; - this._keys = keys; - this._values = values; - - pbf.readFields(readFeature, this, end); -} - -function readFeature(tag, feature, pbf) { - if (tag == 1) feature.id = pbf.readVarint(); - else if (tag == 2) readTag(pbf, feature); - else if (tag == 3) feature.type = pbf.readVarint(); - else if (tag == 4) feature._geometry = pbf.pos; -} - -function readTag(pbf, feature) { - var end = pbf.readVarint() + pbf.pos; - - while (pbf.pos < end) { - var key = feature._keys[pbf.readVarint()], - value = feature._values[pbf.readVarint()]; - feature.properties[key] = value; - } -} - -VectorTileFeature.types = ['Unknown', 'Point', 'LineString', 'Polygon']; - -VectorTileFeature.prototype.loadGeometry = function() { - var pbf = this._pbf; - pbf.pos = this._geometry; - - var end = pbf.readVarint() + pbf.pos, - cmd = 1, - length = 0, - x = 0, - y = 0, - lines = [], - line; - - while (pbf.pos < end) { - if (!length) { - var cmdLen = pbf.readVarint(); - cmd = cmdLen & 0x7; - length = cmdLen >> 3; - } - - length--; - - if (cmd === 1 || cmd === 2) { - x += pbf.readSVarint(); - y += pbf.readSVarint(); - - if (cmd === 1) { // moveTo - if (line) lines.push(line); - line = []; - } - - line.push(new Point(x, y)); - - } else if (cmd === 7) { - - // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90 - if (line) { - line.push(line[0].clone()); // closePolygon - } - - } else { - throw new Error('unknown command ' + cmd); - } - } - - if (line) lines.push(line); - - return lines; -}; - -VectorTileFeature.prototype.bbox = function() { - var pbf = this._pbf; - pbf.pos = this._geometry; - - var end = pbf.readVarint() + pbf.pos, - cmd = 1, - length = 0, - x = 0, - y = 0, - x1 = Infinity, - x2 = -Infinity, - y1 = Infinity, - y2 = -Infinity; - - while (pbf.pos < end) { - if (!length) { - var cmdLen = pbf.readVarint(); - cmd = cmdLen & 0x7; - length = cmdLen >> 3; - } - - length--; - - if (cmd === 1 || cmd === 2) { - x += pbf.readSVarint(); - y += pbf.readSVarint(); - if (x < x1) x1 = x; - if (x > x2) x2 = x; - if (y < y1) y1 = y; - if (y > y2) y2 = y; - - } else if (cmd !== 7) { - throw new Error('unknown command ' + cmd); - } - } - - return [x1, y1, x2, y2]; -}; - -VectorTileFeature.prototype.toGeoJSON = function(x, y, z) { - var size = this.extent * Math.pow(2, z), - x0 = this.extent * x, - y0 = this.extent * y, - coords = this.loadGeometry(), - type = VectorTileFeature.types[this.type], - i, j; - - function project(line) { - for (var j = 0; j < line.length; j++) { - var p = line[j], y2 = 180 - (p.y + y0) * 360 / size; - line[j] = [ - (p.x + x0) * 360 / size - 180, - 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90 - ]; - } - } - - switch (this.type) { - case 1: - var points = []; - for (i = 0; i < coords.length; i++) { - points[i] = coords[i][0]; - } - coords = points; - project(coords); - break; - - case 2: - for (i = 0; i < coords.length; i++) { - project(coords[i]); - } - break; - - case 3: - coords = classifyRings(coords); - for (i = 0; i < coords.length; i++) { - for (j = 0; j < coords[i].length; j++) { - project(coords[i][j]); - } - } - break; - } - - if (coords.length === 1) { - coords = coords[0]; - } else { - type = 'Multi' + type; - } - - var result = { - type: "Feature", - geometry: { - type: type, - coordinates: coords - }, - properties: this.properties - }; - - if ('id' in this) { - result.id = this.id; - } - - return result; -}; - -// classifies an array of rings into polygons with outer rings and holes - -function classifyRings(rings) { - var len = rings.length; - - if (len <= 1) return [rings]; - - var polygons = [], - polygon, - ccw; - - for (var i = 0; i < len; i++) { - var area = signedArea(rings[i]); - if (area === 0) continue; - - if (ccw === undefined) ccw = area < 0; - - if (ccw === area < 0) { - if (polygon) polygons.push(polygon); - polygon = [rings[i]]; - - } else { - polygon.push(rings[i]); - } - } - if (polygon) polygons.push(polygon); - - return polygons; -} - -function signedArea(ring) { - var sum = 0; - for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) { - p1 = ring[i]; - p2 = ring[j]; - sum += (p2.x - p1.x) * (p1.y + p2.y); - } - return sum; -} - -},{"point-geometry":1071}],1084:[function(require,module,exports){ -'use strict'; - -var VectorTileFeature = require('./vectortilefeature.js'); - -module.exports = VectorTileLayer; - -function VectorTileLayer(pbf, end) { - // Public - this.version = 1; - this.name = null; - this.extent = 4096; - this.length = 0; - - // Private - this._pbf = pbf; - this._keys = []; - this._values = []; - this._features = []; - - pbf.readFields(readLayer, this, end); - - this.length = this._features.length; -} - -function readLayer(tag, layer, pbf) { - if (tag === 15) layer.version = pbf.readVarint(); - else if (tag === 1) layer.name = pbf.readString(); - else if (tag === 5) layer.extent = pbf.readVarint(); - else if (tag === 2) layer._features.push(pbf.pos); - else if (tag === 3) layer._keys.push(pbf.readString()); - else if (tag === 4) layer._values.push(readValueMessage(pbf)); -} - -function readValueMessage(pbf) { - var value = null, - end = pbf.readVarint() + pbf.pos; - - while (pbf.pos < end) { - var tag = pbf.readVarint() >> 3; - - value = tag === 1 ? pbf.readString() : - tag === 2 ? pbf.readFloat() : - tag === 3 ? pbf.readDouble() : - tag === 4 ? pbf.readVarint64() : - tag === 5 ? pbf.readVarint() : - tag === 6 ? pbf.readSVarint() : - tag === 7 ? pbf.readBoolean() : null; - } - - return value; -} - -// return feature `i` from this layer as a `VectorTileFeature` -VectorTileLayer.prototype.feature = function(i) { - if (i < 0 || i >= this._features.length) throw new Error('feature index out of bounds'); - - this._pbf.pos = this._features[i]; - - var end = this._pbf.readVarint() + this._pbf.pos; - return new VectorTileFeature(this._pbf, end, this.extent, this._keys, this._values); -}; - -},{"./vectortilefeature.js":1083}],1085:[function(require,module,exports){ -var Pbf = require('pbf') -var vtpb = require('./vector-tile-pb') -var GeoJSONWrapper = require('./lib/geojson_wrapper') - -module.exports = fromVectorTileJs -module.exports.fromVectorTileJs = fromVectorTileJs -module.exports.fromGeojsonVt = fromGeojsonVt -module.exports.GeoJSONWrapper = GeoJSONWrapper - -/** - * Serialize a vector-tile-js-created tile to pbf - * - * @param {Object} tile - * @return {Buffer} uncompressed, pbf-serialized tile data - */ -function fromVectorTileJs (tile) { - var layers = [] - for (var l in tile.layers) { - layers.push(prepareLayer(tile.layers[l])) - } - - var out = new Pbf() - vtpb.tile.write({ layers: layers }, out) - return out.finish() -} - -/** - * Serialized a geojson-vt-created tile to pbf. - * - * @param {Object} layers - An object mapping layer names to geojson-vt-created vector tile objects - * @return {Buffer} uncompressed, pbf-serialized tile data - */ -function fromGeojsonVt (layers) { - var l = {} - for (var k in layers) { - l[k] = new GeoJSONWrapper(layers[k].features) - l[k].name = k - } - return fromVectorTileJs({layers: l}) -} - -/** - * Prepare the given layer to be serialized by the auto-generated pbf - * serializer by encoding the feature geometry and properties. - */ -function prepareLayer (layer) { - var preparedLayer = { - name: layer.name || '', - version: layer.version || 1, - extent: layer.extent || 4096, - keys: [], - values: [], - features: [] - } - - var keycache = {} - var valuecache = {} - - for (var i = 0; i < layer.length; i++) { - var feature = layer.feature(i) - feature.geometry = encodeGeometry(feature.loadGeometry()) - - var tags = [] - for (var key in feature.properties) { - var keyIndex = keycache[key] - if (typeof keyIndex === 'undefined') { - preparedLayer.keys.push(key) - keyIndex = preparedLayer.keys.length - 1 - keycache[key] = keyIndex - } - var value = wrapValue(feature.properties[key]) - var valueIndex = valuecache[value.key] - if (typeof valueIndex === 'undefined') { - preparedLayer.values.push(value) - valueIndex = preparedLayer.values.length - 1 - valuecache[value.key] = valueIndex - } - tags.push(keyIndex) - tags.push(valueIndex) - } - - feature.tags = tags - preparedLayer.features.push(feature) - } - - return preparedLayer -} - -function command (cmd, length) { - return (length << 3) + (cmd & 0x7) -} - -function zigzag (num) { - return (num << 1) ^ (num >> 31) -} - -/** - * Encode a polygon's geometry into an array ready to be serialized - * to mapbox vector tile specified geometry data. - * - * @param {Array} Rings, each being an array of [x, y] tile-space coordinates - * @return {Array} encoded geometry - */ -function encodeGeometry (geometry) { - var encoded = [] - var x = 0 - var y = 0 - var rings = geometry.length - for (var r = 0; r < rings; r++) { - var ring = geometry[r] - encoded.push(command(1, 1)) // moveto - for (var i = 0; i < ring.length; i++) { - if (i === 1) { - encoded.push(command(2, ring.length - 1)) // lineto - } - var dx = ring[i].x - x - var dy = ring[i].y - y - encoded.push(zigzag(dx), zigzag(dy)) - x += dx - y += dy - } - } - - return encoded -} - -/** - * Wrap a property value according to its type. The returned object - * is of the form { xxxx_value: primitiveValue }, which is what the generated - * protobuf serializer expects. - */ -function wrapValue (value) { - var result - var type = typeof value - if (type === 'string') { - result = { string_value: value } - } else if (type === 'boolean') { - result = { bool_value: value } - } else if (type === 'number') { - if (value % 1 !== 0) { - result = { double_value: value } - } else if (value < 0) { - result = { sint_value: value } - } else { - result = { uint_value: value } - } - } else { - value = JSON.stringify(value) - result = { string_value: value } - } - - result.key = type + ':' + value - return result -} - -},{"./lib/geojson_wrapper":1086,"./vector-tile-pb":1087,"pbf":1069}],1086:[function(require,module,exports){ -'use strict' - -var Point = require('point-geometry') -var VectorTileFeature = require('vector-tile').VectorTileFeature - -module.exports = GeoJSONWrapper - -// conform to vectortile api -function GeoJSONWrapper (features) { - this.features = features - this.length = features.length -} - -GeoJSONWrapper.prototype.feature = function (i) { - return new FeatureWrapper(this.features[i]) -} - -function FeatureWrapper (feature) { - this.id = typeof feature.id === 'number' ? feature.id : undefined - this.type = feature.type - this.rawGeometry = feature.type === 1 ? [feature.geometry] : feature.geometry - this.properties = feature.tags - this.extent = 4096 -} - -FeatureWrapper.prototype.loadGeometry = function () { - var rings = this.rawGeometry - this.geometry = [] - - for (var i = 0; i < rings.length; i++) { - var ring = rings[i] - var newRing = [] - for (var j = 0; j < ring.length; j++) { - newRing.push(new Point(ring[j][0], ring[j][1])) - } - this.geometry.push(newRing) - } - return this.geometry -} - -FeatureWrapper.prototype.bbox = function () { - if (!this.geometry) this.loadGeometry() - - var rings = this.geometry - var x1 = Infinity - var x2 = -Infinity - var y1 = Infinity - var y2 = -Infinity - - for (var i = 0; i < rings.length; i++) { - var ring = rings[i] - - for (var j = 0; j < ring.length; j++) { - var coord = ring[j] - - x1 = Math.min(x1, coord.x) - x2 = Math.max(x2, coord.x) - y1 = Math.min(y1, coord.y) - y2 = Math.max(y2, coord.y) - } - } - - return [x1, y1, x2, y2] -} - -FeatureWrapper.prototype.toGeoJSON = VectorTileFeature.prototype.toGeoJSON - -},{"point-geometry":1071,"vector-tile":1081}],1087:[function(require,module,exports){ -'use strict'; - -// tile ======================================== - -var tile = exports.tile = {read: readTile, write: writeTile}; - -tile.GeomType = { - "Unknown": 0, - "Point": 1, - "LineString": 2, - "Polygon": 3 -}; - -function readTile(pbf, end) { - return pbf.readFields(readTileField, {"layers": []}, end); -} - -function readTileField(tag, tile, pbf) { - if (tag === 3) tile.layers.push(readLayer(pbf, pbf.readVarint() + pbf.pos)); -} - -function writeTile(tile, pbf) { - var i; - if (tile.layers !== undefined) for (i = 0; i < tile.layers.length; i++) pbf.writeMessage(3, writeLayer, tile.layers[i]); -} - -// value ======================================== - -tile.value = {read: readValue, write: writeValue}; - -function readValue(pbf, end) { - return pbf.readFields(readValueField, {}, end); -} - -function readValueField(tag, value, pbf) { - if (tag === 1) value.string_value = pbf.readString(); - else if (tag === 2) value.float_value = pbf.readFloat(); - else if (tag === 3) value.double_value = pbf.readDouble(); - else if (tag === 4) value.int_value = pbf.readVarint(); - else if (tag === 5) value.uint_value = pbf.readVarint(); - else if (tag === 6) value.sint_value = pbf.readSVarint(); - else if (tag === 7) value.bool_value = pbf.readBoolean(); -} - -function writeValue(value, pbf) { - if (value.string_value !== undefined) pbf.writeStringField(1, value.string_value); - if (value.float_value !== undefined) pbf.writeFloatField(2, value.float_value); - if (value.double_value !== undefined) pbf.writeDoubleField(3, value.double_value); - if (value.int_value !== undefined) pbf.writeVarintField(4, value.int_value); - if (value.uint_value !== undefined) pbf.writeVarintField(5, value.uint_value); - if (value.sint_value !== undefined) pbf.writeSVarintField(6, value.sint_value); - if (value.bool_value !== undefined) pbf.writeBooleanField(7, value.bool_value); -} - -// feature ======================================== - -tile.feature = {read: readFeature, write: writeFeature}; - -function readFeature(pbf, end) { - var feature = pbf.readFields(readFeatureField, {}, end); - if (feature.type === undefined) feature.type = "Unknown"; - return feature; -} - -function readFeatureField(tag, feature, pbf) { - if (tag === 1) feature.id = pbf.readVarint(); - else if (tag === 2) feature.tags = pbf.readPackedVarint(); - else if (tag === 3) feature.type = pbf.readVarint(); - else if (tag === 4) feature.geometry = pbf.readPackedVarint(); -} - -function writeFeature(feature, pbf) { - if (feature.id !== undefined) pbf.writeVarintField(1, feature.id); - if (feature.tags !== undefined) pbf.writePackedVarint(2, feature.tags); - if (feature.type !== undefined) pbf.writeVarintField(3, feature.type); - if (feature.geometry !== undefined) pbf.writePackedVarint(4, feature.geometry); -} - -// layer ======================================== - -tile.layer = {read: readLayer, write: writeLayer}; - -function readLayer(pbf, end) { - return pbf.readFields(readLayerField, {"features": [], "keys": [], "values": []}, end); -} - -function readLayerField(tag, layer, pbf) { - if (tag === 15) layer.version = pbf.readVarint(); - else if (tag === 1) layer.name = pbf.readString(); - else if (tag === 2) layer.features.push(readFeature(pbf, pbf.readVarint() + pbf.pos)); - else if (tag === 3) layer.keys.push(pbf.readString()); - else if (tag === 4) layer.values.push(readValue(pbf, pbf.readVarint() + pbf.pos)); - else if (tag === 5) layer.extent = pbf.readVarint(); -} - -function writeLayer(layer, pbf) { - if (layer.version !== undefined) pbf.writeVarintField(15, layer.version); - if (layer.name !== undefined) pbf.writeStringField(1, layer.name); - var i; - if (layer.features !== undefined) for (i = 0; i < layer.features.length; i++) pbf.writeMessage(2, writeFeature, layer.features[i]); - if (layer.keys !== undefined) for (i = 0; i < layer.keys.length; i++) pbf.writeStringField(3, layer.keys[i]); - if (layer.values !== undefined) for (i = 0; i < layer.values.length; i++) pbf.writeMessage(4, writeValue, layer.values[i]); - if (layer.extent !== undefined) pbf.writeVarintField(5, layer.extent); -} - -},{}],1088:[function(require,module,exports){ -arguments[4][84][0].apply(exports,arguments) -},{"dup":84}],1089:[function(require,module,exports){ -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : - typeof define === 'function' && define.amd ? define(['exports'], factory) : - (factory((global.WhooTS = global.WhooTS || {}))); -}(this, function (exports) { - -/** - * getURL - * - * @param {String} baseUrl Base url of the WMS server - * @param {String} layer Layer name - * @param {Number} x Tile coordinate x - * @param {Number} y Tile coordinate y - * @param {Number} z Tile zoom - * @param {Object} [options] - * @param {String} [options.format='image/png'] - * @param {String} [options.service='WMS'] - * @param {String} [options.version='1.1.1'] - * @param {String} [options.request='GetMap'] - * @param {String} [options.srs='EPSG:3857'] - * @param {Number} [options.width='256'] - * @param {Number} [options.height='256'] - * @returns {String} url - * @example - * var baseUrl = 'http://geodata.state.nj.us/imagerywms/Natural2015'; - * var layer = 'Natural2015'; - * var url = whoots.getURL(baseUrl, layer, 154308, 197167, 19); - */ -function getURL(baseUrl, layer, x, y, z, options) { - options = options || {}; - - var url = baseUrl + '?' + [ - 'bbox=' + getTileBBox(x, y, z), - 'format=' + (options.format || 'image/png'), - 'service=' + (options.service || 'WMS'), - 'version=' + (options.version || '1.1.1'), - 'request=' + (options.request || 'GetMap'), - 'srs=' + (options.srs || 'EPSG:3857'), - 'width=' + (options.width || 256), - 'height=' + (options.height || 256), - 'layers=' + layer - ].join('&'); - - return url; -} - - -/** - * getTileBBox - * - * @param {Number} x Tile coordinate x - * @param {Number} y Tile coordinate y - * @param {Number} z Tile zoom - * @returns {String} String of the bounding box - */ -function getTileBBox(x, y, z) { - // for Google/OSM tile scheme we need to alter the y - y = (Math.pow(2, z) - y - 1); - - var min = getMercCoords(x * 256, y * 256, z), - max = getMercCoords((x + 1) * 256, (y + 1) * 256, z); - - return min[0] + ',' + min[1] + ',' + max[0] + ',' + max[1]; -} - - -/** - * getMercCoords - * - * @param {Number} x Pixel coordinate x - * @param {Number} y Pixel coordinate y - * @param {Number} z Tile zoom - * @returns {Array} [x, y] - */ -function getMercCoords(x, y, z) { - var resolution = (2 * Math.PI * 6378137 / 256) / Math.pow(2, z), - merc_x = (x * resolution - 2 * Math.PI * 6378137 / 2.0), - merc_y = (y * resolution - 2 * Math.PI * 6378137 / 2.0); - - return [merc_x, merc_y]; -} - -exports.getURL = getURL; -exports.getTileBBox = getTileBBox; -exports.getMercCoords = getMercCoords; - -Object.defineProperty(exports, '__esModule', { value: true }); - -})); -},{}],1090:[function(require,module,exports){ +},{}],420:[function(require,module,exports){ module.exports={ - "name": "mapbox-gl", - "description": "A WebGL interactive maps library", - "version": "0.22.1", - "main": "js/mapbox-gl.js", - "license": "BSD-3-Clause", - "repository": { - "type": "git", - "url": "git://github.com/mapbox/mapbox-gl-js.git" + "_args": [ + [ + { + "raw": "mapbox-gl@^0.22.0", + "scope": null, + "escapedName": "mapbox-gl", + "name": "mapbox-gl", + "rawSpec": "^0.22.0", + "spec": ">=0.22.0 <0.23.0", + "type": "range" + }, + "/Users/ccdpandhare/Desktop/GSoC/image-sequencer/node_modules/plotly.js" + ] + ], + "_from": "mapbox-gl@>=0.22.0 <0.23.0", + "_id": "mapbox-gl@0.22.1", + "_inCache": true, + "_location": "/mapbox-gl", + "_nodeVersion": "4.4.5", + "_npmOperationalInternal": { + "host": "packages-12-west.internal.npmjs.com", + "tmp": "tmp/mapbox-gl-0.22.1.tgz_1471549891670_0.8762630566488951" }, - "engines": { - "node": ">=4.0.0" + "_npmUser": { + "name": "lucaswoj", + "email": "lucas@lucaswoj.com" + }, + "_npmVersion": "2.15.5", + "_phantomChildren": {}, + "_requested": { + "raw": "mapbox-gl@^0.22.0", + "scope": null, + "escapedName": "mapbox-gl", + "name": "mapbox-gl", + "rawSpec": "^0.22.0", + "spec": ">=0.22.0 <0.23.0", + "type": "range" + }, + "_requiredBy": [ + "/plotly.js" + ], + "_resolved": "https://registry.npmjs.org/mapbox-gl/-/mapbox-gl-0.22.1.tgz", + "_shasum": "92a965547d4c2f24c22cbc487eeda48694cb627a", + "_shrinkwrap": null, + "_spec": "mapbox-gl@^0.22.0", + "_where": "/Users/ccdpandhare/Desktop/GSoC/image-sequencer/node_modules/plotly.js", + "browser": { + "./js/util/ajax.js": "./js/util/browser/ajax.js", + "./js/util/browser.js": "./js/util/browser/browser.js", + "./js/util/canvas.js": "./js/util/browser/canvas.js", + "./js/util/dom.js": "./js/util/browser/dom.js", + "./js/util/web_worker.js": "./js/util/browser/web_worker.js" + }, + "bugs": { + "url": "https://github.com/mapbox/mapbox-gl-js/issues" }, "dependencies": { "csscolorparser": "^1.0.2", @@ -104881,6 +85463,7 @@ module.exports={ "webworkify": "^1.3.0", "whoots-js": "^2.0.0" }, + "description": "A WebGL interactive maps library", "devDependencies": { "babel-preset-react": "^6.11.1", "babelify": "^7.3.0", @@ -104921,49 +85504,18 @@ module.exports={ "webpack": "^1.13.1", "webworkify-webpack": "^1.1.3" }, - "browser": { - "./js/util/ajax.js": "./js/util/browser/ajax.js", - "./js/util/browser.js": "./js/util/browser/browser.js", - "./js/util/canvas.js": "./js/util/browser/canvas.js", - "./js/util/dom.js": "./js/util/browser/dom.js", - "./js/util/web_worker.js": "./js/util/browser/web_worker.js" - }, - "scripts": { - "build-dev": "browserify js/mapbox-gl.js --debug --standalone mapboxgl > dist/mapbox-gl-dev.js && tap --no-coverage test/build/dev.test.js", - "watch-dev": "watchify js/mapbox-gl.js --debug --standalone mapboxgl -o dist/mapbox-gl-dev.js -v", - "build-min": "browserify js/mapbox-gl.js --debug -t unassertify --plugin [minifyify --map mapbox-gl.js.map --output dist/mapbox-gl.js.map] --standalone mapboxgl > dist/mapbox-gl.js && tap --no-coverage test/build/min.test.js", - "build-token": "browserify debug/access-token-src.js --debug -t envify > debug/access-token.js", - "watch-bench": "node bench/download-data.js && watchify bench/index.js --plugin [minifyify --no-map] -t [babelify --presets react] -t unassertify -t envify -o bench/bench.js -v", - "start-server": "st --no-cache --localhost --port 9966 --index index.html .", - "start": "run-p build-token watch-dev watch-bench start-server", - "start-debug": "run-p build-token watch-dev start-server", - "start-bench": "run-p build-token watch-bench start-server", - "build-docs": "documentation build --github --format html -c documentation.yml --theme ./docs/_theme --output docs/api/", - "build": "npm run build-docs # invoked by publisher when publishing docs on the mb-pages branch", - "start-docs": "npm run build-min && npm run build-docs && jekyll serve -w", - "lint": "eslint --ignore-path .gitignore js test bench docs/_posts/examples/*.html", - "open-changed-examples": "git diff --name-only mb-pages HEAD -- docs/_posts/examples/*.html | awk '{print \"http://127.0.0.1:4000/mapbox-gl-js/example/\" substr($0,33,length($0)-37)}' | xargs open", - "test-suite": "node test/render.test.js && node test/query.test.js", - "test": "npm run lint && tap --reporter dot test/js/*/*.js test/build/webpack.test.js" - }, - "gitHead": "13a9015341f0602ccb55c98c53079838ad4b70b5", - "bugs": { - "url": "https://github.com/mapbox/mapbox-gl-js/issues" - }, - "homepage": "https://github.com/mapbox/mapbox-gl-js#readme", - "_id": "mapbox-gl@0.22.1", - "_shasum": "92a965547d4c2f24c22cbc487eeda48694cb627a", - "_from": "mapbox-gl@>=0.22.0 <0.23.0", - "_npmVersion": "2.15.5", - "_nodeVersion": "4.4.5", - "_npmUser": { - "name": "lucaswoj", - "email": "lucas@lucaswoj.com" - }, + "directories": {}, "dist": { "shasum": "92a965547d4c2f24c22cbc487eeda48694cb627a", "tarball": "https://registry.npmjs.org/mapbox-gl/-/mapbox-gl-0.22.1.tgz" }, + "engines": { + "node": ">=4.0.0" + }, + "gitHead": "13a9015341f0602ccb55c98c53079838ad4b70b5", + "homepage": "https://github.com/mapbox/mapbox-gl-js#readme", + "license": "BSD-3-Clause", + "main": "js/mapbox-gl.js", "maintainers": [ { "name": "aaronlidman", @@ -105138,24 +85690,695 @@ module.exports={ "email": "young@mapbox.com" } ], - "_npmOperationalInternal": { - "host": "packages-12-west.internal.npmjs.com", - "tmp": "tmp/mapbox-gl-0.22.1.tgz_1471549891670_0.8762630566488951" + "name": "mapbox-gl", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git://github.com/mapbox/mapbox-gl-js.git" }, - "directories": {}, - "_resolved": "https://registry.npmjs.org/mapbox-gl/-/mapbox-gl-0.22.1.tgz", - "readme": "ERROR: No README data found!" + "scripts": { + "build": "npm run build-docs # invoked by publisher when publishing docs on the mb-pages branch", + "build-dev": "browserify js/mapbox-gl.js --debug --standalone mapboxgl > dist/mapbox-gl-dev.js && tap --no-coverage test/build/dev.test.js", + "build-docs": "documentation build --github --format html -c documentation.yml --theme ./docs/_theme --output docs/api/", + "build-min": "browserify js/mapbox-gl.js --debug -t unassertify --plugin [minifyify --map mapbox-gl.js.map --output dist/mapbox-gl.js.map] --standalone mapboxgl > dist/mapbox-gl.js && tap --no-coverage test/build/min.test.js", + "build-token": "browserify debug/access-token-src.js --debug -t envify > debug/access-token.js", + "lint": "eslint --ignore-path .gitignore js test bench docs/_posts/examples/*.html", + "open-changed-examples": "git diff --name-only mb-pages HEAD -- docs/_posts/examples/*.html | awk '{print \"http://127.0.0.1:4000/mapbox-gl-js/example/\" substr($0,33,length($0)-37)}' | xargs open", + "start": "run-p build-token watch-dev watch-bench start-server", + "start-bench": "run-p build-token watch-bench start-server", + "start-debug": "run-p build-token watch-dev start-server", + "start-docs": "npm run build-min && npm run build-docs && jekyll serve -w", + "start-server": "st --no-cache --localhost --port 9966 --index index.html .", + "test": "npm run lint && tap --reporter dot test/js/*/*.js test/build/webpack.test.js", + "test-suite": "node test/render.test.js && node test/query.test.js", + "watch-bench": "node bench/download-data.js && watchify bench/index.js --plugin [minifyify --no-map] -t [babelify --presets react] -t unassertify -t envify -o bench/bench.js -v", + "watch-dev": "watchify js/mapbox-gl.js --debug --standalone mapboxgl -o dist/mapbox-gl-dev.js -v" + }, + "version": "0.22.1" } -},{}],1091:[function(require,module,exports){ +},{}],421:[function(require,module,exports){ +'use strict' + +module.exports = createTable + +var chull = require('convex-hull') + +function constructVertex(d, a, b) { + var x = new Array(d) + for(var i=0; i row[1][2]) + quaternion[0] = -quaternion[0] + if (row[0][2] > row[2][0]) + quaternion[1] = -quaternion[1] + if (row[1][0] > row[0][1]) + quaternion[2] = -quaternion[2] + return true +} + +//will be replaced by gl-vec4 eventually +function vec4multMat4(out, a, m) { + var x = a[0], y = a[1], z = a[2], w = a[3]; + out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; + out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; + out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; + out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; + return out; +} + +//gets upper-left of a 4x4 matrix into a 3x3 of vectors +function mat3from4(out, mat4x4) { + out[0][0] = mat4x4[0] + out[0][1] = mat4x4[1] + out[0][2] = mat4x4[2] + + out[1][0] = mat4x4[4] + out[1][1] = mat4x4[5] + out[1][2] = mat4x4[6] + + out[2][0] = mat4x4[8] + out[2][1] = mat4x4[9] + out[2][2] = mat4x4[10] +} + +function combine(out, a, b, scale1, scale2) { + out[0] = a[0] * scale1 + b[0] * scale2 + out[1] = a[1] * scale1 + b[1] * scale2 + out[2] = a[2] * scale1 + b[2] * scale2 +} +},{"./normalize":423,"gl-mat4/clone":150,"gl-mat4/create":151,"gl-mat4/determinant":152,"gl-mat4/invert":156,"gl-mat4/transpose":166,"gl-vec3/cross":244,"gl-vec3/dot":245,"gl-vec3/length":246,"gl-vec3/normalize":248}],423:[function(require,module,exports){ +module.exports = function normalize(out, mat) { + var m44 = mat[15] + // Cannot normalize. + if (m44 === 0) + return false + var scale = 1 / m44 + for (var i=0; i<16; i++) + out[i] = mat[i] * scale + return true +} +},{}],424:[function(require,module,exports){ +var lerp = require('gl-vec3/lerp') + +var recompose = require('mat4-recompose') +var decompose = require('mat4-decompose') +var determinant = require('gl-mat4/determinant') +var slerp = require('quat-slerp') + +var state0 = state() +var state1 = state() +var tmp = state() + +module.exports = interpolate +function interpolate(out, start, end, alpha) { + if (determinant(start) === 0 || determinant(end) === 0) + return false + + //decompose the start and end matrices into individual components + var r0 = decompose(start, state0.translate, state0.scale, state0.skew, state0.perspective, state0.quaternion) + var r1 = decompose(end, state1.translate, state1.scale, state1.skew, state1.perspective, state1.quaternion) + if (!r0 || !r1) + return false + + + //now lerp/slerp the start and end components into a temporary lerp(tmptranslate, state0.translate, state1.translate, alpha) + lerp(tmp.translate, state0.translate, state1.translate, alpha) + lerp(tmp.skew, state0.skew, state1.skew, alpha) + lerp(tmp.scale, state0.scale, state1.scale, alpha) + lerp(tmp.perspective, state0.perspective, state1.perspective, alpha) + slerp(tmp.quaternion, state0.quaternion, state1.quaternion, alpha) + + //and recompose into our 'out' matrix + recompose(out, tmp.translate, tmp.scale, tmp.skew, tmp.perspective, tmp.quaternion) + return true +} + +function state() { + return { + translate: vec3(), + scale: vec3(1), + skew: vec3(), + perspective: vec4(), + quaternion: vec4() + } +} + +function vec3(n) { + return [n||0,n||0,n||0] +} + +function vec4() { + return [0,0,0,1] +} +},{"gl-mat4/determinant":152,"gl-vec3/lerp":247,"mat4-decompose":422,"mat4-recompose":425,"quat-slerp":925}],425:[function(require,module,exports){ +/* +Input: translation ; a 3 component vector + scale ; a 3 component vector + skew ; skew factors XY,XZ,YZ represented as a 3 component vector + perspective ; a 4 component vector + quaternion ; a 4 component vector +Output: matrix ; a 4x4 matrix + +From: http://www.w3.org/TR/css3-transforms/#recomposing-to-a-3d-matrix +*/ + +var mat4 = { + identity: require('gl-mat4/identity'), + translate: require('gl-mat4/translate'), + multiply: require('gl-mat4/multiply'), + create: require('gl-mat4/create'), + scale: require('gl-mat4/scale'), + fromRotationTranslation: require('gl-mat4/fromRotationTranslation') +} + +var rotationMatrix = mat4.create() +var temp = mat4.create() + +module.exports = function recomposeMat4(matrix, translation, scale, skew, perspective, quaternion) { + mat4.identity(matrix) + + //apply translation & rotation + mat4.fromRotationTranslation(matrix, quaternion, translation) + + //apply perspective + matrix[3] = perspective[0] + matrix[7] = perspective[1] + matrix[11] = perspective[2] + matrix[15] = perspective[3] + + // apply skew + // temp is a identity 4x4 matrix initially + mat4.identity(temp) + + if (skew[2] !== 0) { + temp[9] = skew[2] + mat4.multiply(matrix, matrix, temp) + } + + if (skew[1] !== 0) { + temp[9] = 0 + temp[8] = skew[1] + mat4.multiply(matrix, matrix, temp) + } + + if (skew[0] !== 0) { + temp[8] = 0 + temp[4] = skew[0] + mat4.multiply(matrix, matrix, temp) + } + + //apply scale + mat4.scale(matrix, matrix, scale) + return matrix +} +},{"gl-mat4/create":151,"gl-mat4/fromRotationTranslation":154,"gl-mat4/identity":155,"gl-mat4/multiply":158,"gl-mat4/scale":164,"gl-mat4/translate":165}],426:[function(require,module,exports){ +'use strict' + +var bsearch = require('binary-search-bounds') +var m4interp = require('mat4-interpolate') +var invert44 = require('gl-mat4/invert') +var rotateX = require('gl-mat4/rotateX') +var rotateY = require('gl-mat4/rotateY') +var rotateZ = require('gl-mat4/rotateZ') +var lookAt = require('gl-mat4/lookAt') +var translate = require('gl-mat4/translate') +var scale = require('gl-mat4/scale') +var normalize = require('gl-vec3/normalize') + +var DEFAULT_CENTER = [0,0,0] + +module.exports = createMatrixCameraController + +function MatrixCameraController(initialMatrix) { + this._components = initialMatrix.slice() + this._time = [0] + this.prevMatrix = initialMatrix.slice() + this.nextMatrix = initialMatrix.slice() + this.computedMatrix = initialMatrix.slice() + this.computedInverse = initialMatrix.slice() + this.computedEye = [0,0,0] + this.computedUp = [0,0,0] + this.computedCenter = [0,0,0] + this.computedRadius = [0] + this._limits = [-Infinity, Infinity] +} + +var proto = MatrixCameraController.prototype + +proto.recalcMatrix = function(t) { + var time = this._time + var tidx = bsearch.le(time, t) + var mat = this.computedMatrix + if(tidx < 0) { + return + } + var comps = this._components + if(tidx === time.length-1) { + var ptr = 16*tidx + for(var i=0; i<16; ++i) { + mat[i] = comps[ptr++] + } + } else { + var dt = (time[tidx+1] - time[tidx]) + var ptr = 16*tidx + var prev = this.prevMatrix + var allEqual = true + for(var i=0; i<16; ++i) { + prev[i] = comps[ptr++] + } + var next = this.nextMatrix + for(var i=0; i<16; ++i) { + next[i] = comps[ptr++] + allEqual = allEqual && (prev[i] === next[i]) + } + if(dt < 1e-6 || allEqual) { + for(var i=0; i<16; ++i) { + mat[i] = prev[i] + } + } else { + m4interp(mat, prev, next, (t - time[tidx])/dt) + } + } + + var up = this.computedUp + up[0] = mat[1] + up[1] = mat[5] + up[2] = mat[6] + normalize(up, up) + + var imat = this.computedInverse + invert44(imat, mat) + var eye = this.computedEye + var w = imat[15] + eye[0] = imat[12]/w + eye[1] = imat[13]/w + eye[2] = imat[14]/w + + var center = this.computedCenter + var radius = Math.exp(this.computedRadius[0]) + for(var i=0; i<3; ++i) { + center[i] = eye[i] - mat[2+4*i] * radius + } +} + +proto.idle = function(t) { + if(t < this.lastT()) { + return + } + var mc = this._components + var ptr = mc.length-16 + for(var i=0; i<16; ++i) { + mc.push(mc[ptr++]) + } + this._time.push(t) +} + +proto.flush = function(t) { + var idx = bsearch.gt(this._time, t) - 2 + if(idx < 0) { + return + } + this._time.slice(0, idx) + this._components.slice(0, 16*idx) +} + +proto.lastT = function() { + return this._time[this._time.length-1] +} + +proto.lookAt = function(t, eye, center, up) { + this.recalcMatrix(t) + eye = eye || this.computedEye + center = center || DEFAULT_CENTER + up = up || this.computedUp + this.setMatrix(t, lookAt(this.computedMatrix, eye, center, up)) + var d2 = 0.0 + for(var i=0; i<3; ++i) { + d2 += Math.pow(center[i] - eye[i], 2) + } + d2 = Math.log(Math.sqrt(d2)) + this.computedRadius[0] = d2 +} + +proto.rotate = function(t, yaw, pitch, roll) { + this.recalcMatrix(t) + var mat = this.computedInverse + if(yaw) rotateY(mat, mat, yaw) + if(pitch) rotateX(mat, mat, pitch) + if(roll) rotateZ(mat, mat, roll) + this.setMatrix(t, invert44(this.computedMatrix, mat)) +} + +var tvec = [0,0,0] + +proto.pan = function(t, dx, dy, dz) { + tvec[0] = -(dx || 0.0) + tvec[1] = -(dy || 0.0) + tvec[2] = -(dz || 0.0) + this.recalcMatrix(t) + var mat = this.computedInverse + translate(mat, mat, tvec) + this.setMatrix(t, invert44(mat, mat)) +} + +proto.translate = function(t, dx, dy, dz) { + tvec[0] = dx || 0.0 + tvec[1] = dy || 0.0 + tvec[2] = dz || 0.0 + this.recalcMatrix(t) + var mat = this.computedMatrix + translate(mat, mat, tvec) + this.setMatrix(t, mat) +} + +proto.setMatrix = function(t, mat) { + if(t < this.lastT()) { + return + } + this._time.push(t) + for(var i=0; i<16; ++i) { + this._components.push(mat[i]) + } +} + +proto.setDistance = function(t, d) { + this.computedRadius[0] = d +} + +proto.setDistanceLimits = function(a,b) { + var lim = this._limits + lim[0] = a + lim[1] = b +} + +proto.getDistanceLimits = function(out) { + var lim = this._limits + if(out) { + out[0] = lim[0] + out[1] = lim[1] + return out + } + return lim +} + +function createMatrixCameraController(options) { + options = options || {} + var matrix = options.matrix || + [1,0,0,0, + 0,1,0,0, + 0,0,1,0, + 0,0,0,1] + return new MatrixCameraController(matrix) +} +},{"binary-search-bounds":32,"gl-mat4/invert":156,"gl-mat4/lookAt":157,"gl-mat4/rotateX":161,"gl-mat4/rotateY":162,"gl-mat4/rotateZ":163,"gl-mat4/scale":164,"gl-mat4/translate":165,"gl-vec3/normalize":248,"mat4-interpolate":424}],427:[function(require,module,exports){ +'use strict' + +module.exports = monotoneConvexHull2D + +var orient = require('robust-orientation')[3] + +function monotoneConvexHull2D(points) { + var n = points.length + + if(n < 3) { + var result = new Array(n) + for(var i=0; i 1 && orient( + points[lower[m-2]], + points[lower[m-1]], + p) <= 0) { + m -= 1 + lower.pop() + } + lower.push(idx) + + //Insert into upper list + m = upper.length + while(m > 1 && orient( + points[upper[m-2]], + points[upper[m-1]], + p) >= 0) { + m -= 1 + upper.pop() + } + upper.push(idx) + } + + //Merge lists together + var result = new Array(upper.length + lower.length - 2) + var ptr = 0 + for(var i=0, nl=lower.length; i0; --j) { + result[ptr++] = upper[j] + } + + //Return result + return result +} +},{"robust-orientation":954}],428:[function(require,module,exports){ 'use strict' module.exports = mouseListen var mouse = require('mouse-event') -function mouseListen (element, callback) { - if (!callback) { +function mouseListen(element, callback) { + if(!callback) { callback = element element = window } @@ -105164,63 +86387,64 @@ function mouseListen (element, callback) { var x = 0 var y = 0 var mods = { - shift: false, - alt: false, + shift: false, + alt: false, control: false, - meta: false + meta: false } var attached = false - function updateMods (ev) { + function updateMods(ev) { var changed = false - if ('altKey' in ev) { + if('altKey' in ev) { changed = changed || ev.altKey !== mods.alt mods.alt = !!ev.altKey } - if ('shiftKey' in ev) { + if('shiftKey' in ev) { changed = changed || ev.shiftKey !== mods.shift mods.shift = !!ev.shiftKey } - if ('ctrlKey' in ev) { + if('ctrlKey' in ev) { changed = changed || ev.ctrlKey !== mods.control mods.control = !!ev.ctrlKey } - if ('metaKey' in ev) { + if('metaKey' in ev) { changed = changed || ev.metaKey !== mods.meta mods.meta = !!ev.metaKey } return changed } - function handleEvent (nextButtons, ev) { + function handleEvent(nextButtons, ev) { var nextX = mouse.x(ev) var nextY = mouse.y(ev) - if ('buttons' in ev) { - nextButtons = ev.buttons | 0 + if('buttons' in ev) { + nextButtons = ev.buttons|0 } - if (nextButtons !== buttonState || - nextX !== x || - nextY !== y || - updateMods(ev)) { - buttonState = nextButtons | 0 - x = nextX || 0 - y = nextY || 0 + if(nextButtons !== buttonState || + nextX !== x || + nextY !== y || + updateMods(ev)) { + buttonState = nextButtons|0 + x = nextX||0 + y = nextY||0 callback && callback(buttonState, x, y, mods) } } - function clearState (ev) { + function clearState(ev) { handleEvent(0, ev) } - function handleBlur () { - if (buttonState || + function handleBlur() { + if(buttonState || x || y || mods.shift || mods.alt || mods.meta || mods.control) { + x = y = 0 buttonState = 0 mods.shift = mods.alt = mods.control = mods.meta = false @@ -105228,30 +86452,30 @@ function mouseListen (element, callback) { } } - function handleMods (ev) { - if (updateMods(ev)) { + function handleMods(ev) { + if(updateMods(ev)) { callback && callback(buttonState, x, y, mods) } } - function handleMouseMove (ev) { - if (mouse.buttons(ev) === 0) { + function handleMouseMove(ev) { + if(mouse.buttons(ev) === 0) { handleEvent(0, ev) } else { handleEvent(buttonState, ev) } } - function handleMouseDown (ev) { + function handleMouseDown(ev) { handleEvent(buttonState | mouse.buttons(ev), ev) } - function handleMouseUp (ev) { + function handleMouseUp(ev) { handleEvent(buttonState & ~mouse.buttons(ev), ev) } - function attachListeners () { - if (attached) { + function attachListeners() { + if(attached) { return } attached = true @@ -105273,7 +86497,7 @@ function mouseListen (element, callback) { element.addEventListener('keydown', handleMods) element.addEventListener('keypress', handleMods) - if (element !== window) { + if(element !== window) { window.addEventListener('blur', handleBlur) window.addEventListener('keyup', handleMods) @@ -105282,8 +86506,8 @@ function mouseListen (element, callback) { } } - function detachListeners () { - if (!attached) { + function detachListeners() { + if(!attached) { return } attached = false @@ -105305,7 +86529,7 @@ function mouseListen (element, callback) { element.removeEventListener('keydown', handleMods) element.removeEventListener('keypress', handleMods) - if (element !== window) { + if(element !== window) { window.removeEventListener('blur', handleBlur) window.removeEventListener('keyup', handleMods) @@ -105314,7 +86538,7 @@ function mouseListen (element, callback) { } } - // Attach listeners + //Attach listeners attachListeners() var result = { @@ -105323,30 +86547,30 @@ function mouseListen (element, callback) { Object.defineProperties(result, { enabled: { - get: function () { return attached }, - set: function (f) { - if (f) { + get: function() { return attached }, + set: function(f) { + if(f) { attachListeners() } else { - detachListeners() + detachListeners } }, enumerable: true }, buttons: { - get: function () { return buttonState }, + get: function() { return buttonState }, enumerable: true }, x: { - get: function () { return x }, + get: function() { return x }, enumerable: true }, y: { - get: function () { return y }, + get: function() { return y }, enumerable: true }, mods: { - get: function () { return mods }, + get: function() { return mods }, enumerable: true } }) @@ -105354,7 +86578,34 @@ function mouseListen (element, callback) { return result } -},{"mouse-event":1092}],1092:[function(require,module,exports){ +},{"mouse-event":430}],429:[function(require,module,exports){ +var rootPosition = { left: 0, top: 0 } + +module.exports = mouseEventOffset +function mouseEventOffset (ev, target, out) { + target = target || ev.currentTarget || ev.srcElement + if (!Array.isArray(out)) { + out = [ 0, 0 ] + } + var cx = ev.clientX || 0 + var cy = ev.clientY || 0 + var rect = getBoundingClientOffset(target) + out[0] = cx - rect.left + out[1] = cy - rect.top + return out +} + +function getBoundingClientOffset (element) { + if (element === window || + element === document || + element === document.body) { + return rootPosition + } else { + return element.getBoundingClientRect() + } +} + +},{}],430:[function(require,module,exports){ 'use strict' function mouseButtons(ev) { @@ -105416,79 +86667,7 @@ function mouseRelativeY(ev) { } exports.y = mouseRelativeY -},{}],1093:[function(require,module,exports){ -module.exports = function parseUnit(str, out) { - if (!out) - out = [ 0, '' ] - - str = String(str) - var num = parseFloat(str, 10) - out[0] = num - out[1] = str.match(/[\d.\-\+]*\s*(.*)/)[1] || '' - return out -} -},{}],1094:[function(require,module,exports){ -'use strict' - -var parseUnit = require('parse-unit') - -module.exports = toPX - -var PIXELS_PER_INCH = 96 - -function getPropertyInPX(element, prop) { - var parts = parseUnit(getComputedStyle(element).getPropertyValue(prop)) - return parts[0] * toPX(parts[1], element) -} - -//This brutal hack is needed -function getSizeBrutal(unit, element) { - var testDIV = document.createElement('div') - testDIV.style['font-size'] = '128' + unit - element.appendChild(testDIV) - var size = getPropertyInPX(testDIV, 'font-size') / 128 - element.removeChild(testDIV) - return size -} - -function toPX(str, element) { - element = element || document.body - str = (str || 'px').trim().toLowerCase() - if(element === window || element === document) { - element = document.body - } - switch(str) { - case '%': //Ambiguous, not sure if we should use width or height - return element.clientHeight / 100.0 - case 'ch': - case 'ex': - return getSizeBrutal(str, element) - case 'em': - return getPropertyInPX(element, 'font-size') - case 'rem': - return getPropertyInPX(document.body, 'font-size') - case 'vw': - return window.innerWidth/100 - case 'vh': - return window.innerHeight/100 - case 'vmin': - return Math.min(window.innerWidth, window.innerHeight) / 100 - case 'vmax': - return Math.max(window.innerWidth, window.innerHeight) / 100 - case 'in': - return PIXELS_PER_INCH - case 'cm': - return PIXELS_PER_INCH / 2.54 - case 'mm': - return PIXELS_PER_INCH / 25.4 - case 'pt': - return PIXELS_PER_INCH / 72 - case 'pc': - return PIXELS_PER_INCH / 6 - } - return 1 -} -},{"parse-unit":1093}],1095:[function(require,module,exports){ +},{}],431:[function(require,module,exports){ 'use strict' var toPX = require('to-px') @@ -105530,138 +86709,763 @@ function mouseWheelListen(element, callback, noScroll) { return listener } -},{"to-px":1094}],1096:[function(require,module,exports){ +},{"to-px":986}],432:[function(require,module,exports){ +"use strict" + +var pool = require("typedarray-pool") + +module.exports = createSurfaceExtractor + +//Helper macros +function array(i) { + return "a" + i +} +function data(i) { + return "d" + i +} +function cube(i,bitmask) { + return "c" + i + "_" + bitmask +} +function shape(i) { + return "s" + i +} +function stride(i,j) { + return "t" + i + "_" + j +} +function offset(i) { + return "o" + i +} +function scalar(i) { + return "x" + i +} +function pointer(i) { + return "p" + i +} +function delta(i,bitmask) { + return "d" + i + "_" + bitmask +} +function index(i) { + return "i" + i +} +function step(i,j) { + return "u" + i + "_" + j +} +function pcube(bitmask) { + return "b" + bitmask +} +function qcube(bitmask) { + return "y" + bitmask +} +function pdelta(bitmask) { + return "e" + bitmask +} +function vert(i) { + return "v" + i +} +var VERTEX_IDS = "V" +var PHASES = "P" +var VERTEX_COUNT = "N" +var POOL_SIZE = "Q" +var POINTER = "X" +var TEMPORARY = "T" + +function permBitmask(dimension, mask, order) { + var r = 0 + for(var i=0; i 0) { + stepVal.push(stride(i, order[j-1]) + "*" + shape(order[j-1]) ) + } + vars.push(step(i,order[j]) + "=(" + stepVal.join("-") + ")|0") + } + } + //Create index variables + for(var i=0; i=0; --i) { + sizeVariable.push(shape(order[i])) + } + //Previous phases and vertex_ids + vars.push(POOL_SIZE + "=(" + sizeVariable.join("*") + ")|0", + PHASES + "=mallocUint32(" + POOL_SIZE + ")", + VERTEX_IDS + "=mallocUint32(" + POOL_SIZE + ")", + POINTER + "=0") + //Create cube variables for phases + vars.push(pcube(0) + "=0") + for(var j=1; j<(1<=0; --i) { + forLoopBegin(i, 0) + } + var phaseFuncArgs = [] + for(var i=0; i0; k=(k-1)&subset) { + faceArgs.push(VERTEX_IDS + "[" + POINTER + "+" + pdelta(k) + "]") + } + faceArgs.push(vert(0)) + for(var k=0; k0){", + index(order[i]), "=1;") + createLoop(i-1, mask|(1< 0") + } + if(typeof args.vertex !== "function") { + error("Must specify vertex creation function") + } + if(typeof args.cell !== "function") { + error("Must specify cell creation function") + } + if(typeof args.phase !== "function") { + error("Must specify phase function") + } + var getters = args.getters || [] + var typesig = new Array(arrays) + for(var i=0; i= 0) { + typesig[i] = true + } else { + typesig[i] = false + } + } + return compileSurfaceProcedure( + args.vertex, + args.cell, + args.phase, + scalars, + order, + typesig) +} +},{"typedarray-pool":992}],433:[function(require,module,exports){ "use strict" -var fill = require('cwise/lib/wrapper')({"args":["index","array","scalar"],"pre":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"body":{"body":"{_inline_1_arg1_=_inline_1_arg2_.apply(void 0,_inline_1_arg0_)}","args":[{"name":"_inline_1_arg0_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_1_arg1_","lvalue":true,"rvalue":false,"count":1},{"name":"_inline_1_arg2_","lvalue":false,"rvalue":true,"count":1}],"thisVars":[],"localVars":[]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"debug":false,"funcName":"cwise","blockSize":64}) +var fill = require('cwise/lib/wrapper')({"args":["index","array","scalar"],"pre":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"body":{"body":"{_inline_1_arg2_.apply(void 0,_inline_1_arg0_)}","args":[{"name":"_inline_1_arg0_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_1_arg1_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_1_arg2_","lvalue":false,"rvalue":true,"count":1}],"thisVars":[],"localVars":[]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"debug":false,"funcName":"cwise","blockSize":64}) module.exports = function(array, f) { fill(array, f) return array } -},{"cwise/lib/wrapper":1097}],1097:[function(require,module,exports){ -arguments[4][287][0].apply(exports,arguments) -},{"cwise-compiler":1098,"dup":287}],1098:[function(require,module,exports){ -arguments[4][73][0].apply(exports,arguments) -},{"./lib/thunk.js":1100,"dup":73}],1099:[function(require,module,exports){ -arguments[4][74][0].apply(exports,arguments) -},{"dup":74,"uniq":1101}],1100:[function(require,module,exports){ -arguments[4][75][0].apply(exports,arguments) -},{"./compile.js":1099,"dup":75}],1101:[function(require,module,exports){ -arguments[4][76][0].apply(exports,arguments) -},{"dup":76}],1102:[function(require,module,exports){ +},{"cwise/lib/wrapper":85}],434:[function(require,module,exports){ 'use strict' -module.exports = invert +module.exports = gradient -var invert2 = require('gl-mat2/invert') -var invert3 = require('gl-mat3/invert') -var invert4 = require('gl-mat4/invert') +var dup = require('dup') +var cwiseCompiler = require('cwise-compiler') -function invert(out, M) { - switch(M.length) { - case 0: - break - case 1: - out[0] = 1.0 / M[0] - break - case 4: - invert2(out, M) - break - case 9: - invert3(out, M) - break - case 16: - invert4(out, M) - break - default: - throw new Error('currently supports matrices up to 4x4') - break +var TEMPLATE_CACHE = {} +var GRADIENT_CACHE = {} + +var EmptyProc = { + body: "", + args: [], + thisVars: [], + localVars: [] +} + +var centralDiff = cwiseCompiler({ + args: [ 'array', 'array', 'array' ], + pre: EmptyProc, + post: EmptyProc, + body: { + args: [ { + name: 'out', + lvalue: true, + rvalue: false, + count: 1 + }, { + name: 'left', + lvalue: false, + rvalue: true, + count: 1 + }, { + name: 'right', + lvalue: false, + rvalue: true, + count: 1 + }], + body: "out=0.5*(left-right)", + thisVars: [], + localVars: [] + }, + funcName: 'cdiff' +}) + +var zeroOut = cwiseCompiler({ + args: [ 'array' ], + pre: EmptyProc, + post: EmptyProc, + body: { + args: [ { + name: 'out', + lvalue: true, + rvalue: false, + count: 1 + }], + body: "out=0", + thisVars: [], + localVars: [] + }, + funcName: 'zero' +}) + +function generateTemplate(d) { + if(d in TEMPLATE_CACHE) { + return TEMPLATE_CACHE[d] } - return out -} -},{"gl-mat2/invert":1103,"gl-mat3/invert":1104,"gl-mat4/invert":235}],1103:[function(require,module,exports){ -module.exports = invert - -/** - * Inverts a mat2 - * - * @alias mat2.invert - * @param {mat2} out the receiving matrix - * @param {mat2} a the source matrix - * @returns {mat2} out - */ -function invert(out, a) { - var a0 = a[0] - var a1 = a[1] - var a2 = a[2] - var a3 = a[3] - var det = a0 * a3 - a2 * a1 - - if (!det) return null - det = 1.0 / det - - out[0] = a3 * det - out[1] = -a1 * det - out[2] = -a2 * det - out[3] = a0 * det - - return out + var code = [] + for(var i=0; i= 0) { + pickStr.push('0') + } else if(facet.indexOf(-(i+1)) >= 0) { + pickStr.push('s['+i+']-1') + } else { + pickStr.push('-1') + loStr.push('1') + hiStr.push('s['+i+']-2') + } + } + var boundStr = '.lo(' + loStr.join() + ').hi(' + hiStr.join() + ')' + if(loStr.length === 0) { + boundStr = '' + } + + if(cod > 0) { + code.push('if(1') + for(var i=0; i= 0 || facet.indexOf(-(i+1)) >= 0) { + continue + } + code.push('&&s[', i, ']>2') + } + code.push('){grad', cod, '(src.pick(', pickStr.join(), ')', boundStr) + for(var i=0; i= 0 || facet.indexOf(-(i+1)) >= 0) { + continue + } + code.push(',dst.pick(', pickStr.join(), ',', i, ')', boundStr) + } + code.push(');') + } - // Calculate the determinant - var det = a00 * b01 + a01 * b11 + a02 * b21 + for(var i=0; i1){dst.set(', + pickStr.join(), ',', bnd, ',0.5*(src.get(', + cPickStr.join(), ')-src.get(', + dPickStr.join(), ')))}else{dst.set(', + pickStr.join(), ',', bnd, ',0)};') + } else { + code.push('if(s[', bnd, ']>1){diff(', outStr, + ',src.pick(', cPickStr.join(), ')', boundStr, + ',src.pick(', dPickStr.join(), ')', boundStr, + ');}else{zero(', outStr, ');};') + } + break - 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 + case 'mirror': + if(cod === 0) { + code.push('dst.set(', pickStr.join(), ',', bnd, ',0);') + } else { + code.push('zero(', outStr, ');') + } + break - return out + case 'wrap': + var aPickStr = pickStr.slice() + var bPickStr = pickStr.slice() + if(facet[i] < 0) { + aPickStr[bnd] = 's[' + bnd + ']-2' + bPickStr[bnd] = '0' + + } else { + aPickStr[bnd] = 's[' + bnd + ']-1' + bPickStr[bnd] = '1' + } + if(cod === 0) { + code.push('if(s[', bnd, ']>2){dst.set(', + pickStr.join(), ',', bnd, ',0.5*(src.get(', + aPickStr.join(), ')-src.get(', + bPickStr.join(), ')))}else{dst.set(', + pickStr.join(), ',', bnd, ',0)};') + } else { + code.push('if(s[', bnd, ']>2){diff(', outStr, + ',src.pick(', aPickStr.join(), ')', boundStr, + ',src.pick(', bPickStr.join(), ')', boundStr, + ');}else{zero(', outStr, ');};') + } + break + + default: + throw new Error('ndarray-gradient: Invalid boundary condition') + } + } + + if(cod > 0) { + code.push('};') + } + } + + //Enumerate ridges, facets, etc. of hypercube + for(var i=0; i<(1< 1) { + var scratch_shape = [] + for(var i=1; i 1) { + + //Copy data into scratch + code.push("dptr=0;sptr=ptr") + for(var i=order.length-1; i>=0; --i) { + var j = order[i] + if(j === 0) { continue } - r[k++] = m[i][j] + code.push(["for(i",j,"=0;i",j,">1 - return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") - } -} - -function determinant(m) { - if(m.length === 2) { - return [["sum(prod(", m[0][0], ",", m[1][1], "),prod(-", m[0][1], ",", m[1][0], "))"].join("")] - } else { - var expr = [] - for(var i=0; i 0) { - if(r <= 0) { - return det - } else { - s = l + r + code.push("scratch[dptr++]=",dataRead("sptr")) + for(var i=0; i= 0) { - return det - } else { - s = -(l + r) - } - } else { - return det + code.push("sptr+=d"+j,"}") } - var tol = ERRBOUND3 * s - if(det >= tol || det <= -tol) { - return det - } - return orientation3Exact(a, b, c) - }, - function orientation4(a,b,c,d) { - var adx = a[0] - d[0] - var bdx = b[0] - d[0] - var cdx = c[0] - d[0] - var ady = a[1] - d[1] - var bdy = b[1] - d[1] - var cdy = c[1] - d[1] - var adz = a[2] - d[2] - var bdz = b[2] - d[2] - var cdz = c[2] - d[2] - var bdxcdy = bdx * cdy - var cdxbdy = cdx * bdy - var cdxady = cdx * ady - var adxcdy = adx * cdy - var adxbdy = adx * bdy - var bdxady = bdx * ady - var det = adz * (bdxcdy - cdxbdy) - + bdz * (cdxady - adxcdy) - + cdz * (adxbdy - bdxady) - var permanent = (Math.abs(bdxcdy) + Math.abs(cdxbdy)) * Math.abs(adz) - + (Math.abs(cdxady) + Math.abs(adxcdy)) * Math.abs(bdz) - + (Math.abs(adxbdy) + Math.abs(bdxady)) * Math.abs(cdz) - var tol = ERRBOUND4 * permanent - if ((det > tol) || (-det > tol)) { - return det - } - return orientation4Exact(a,b,c,d) - } -] -function slowOrient(args) { - var proc = CACHED[args.length] - if(!proc) { - proc = CACHED[args.length] = orientation(args.length) + + //Compare items in outer loop + code.push("__g:while(j-->left){", + "dptr=0", + "sptr=cptr-s0") + for(var i=1; ib){break __l}"].join("")) + for(var i=order.length-1; i>=1; --i) { + code.push( + "sptr+=e"+i, + "dptr+=f"+i, + "}") + } + + //Copy data back + code.push("dptr=cptr;sptr=cptr-s0") + for(var i=order.length-1; i>=0; --i) { + var j = order[i] + if(j === 0) { + continue + } + code.push(["for(i",j,"=0;i",j,"=0; --i) { + var j = order[i] + if(j === 0) { + continue + } + code.push(["for(i",j,"=0;i",j,"left)&&("+dataRead("cptr-s0")+">scratch)){", + dataWrite("cptr", dataRead("cptr-s0")), + "cptr-=s0", + "}", + dataWrite("cptr", "scratch")) + } + + //Close outer loop body + code.push("}") + if(order.length > 1 && allocator) { + code.push("free(scratch)") + } + code.push("} return " + funcName) + + //Compile and link function + if(allocator) { + var result = new Function("malloc", "free", code.join("\n")) + return result(allocator[0], allocator[1]) + } else { + var result = new Function(code.join("\n")) + return result() } - return proc.apply(undefined, args) } -function generateOrientationProc() { - while(CACHED.length <= NUM_EXPAND) { - CACHED.push(orientation(CACHED.length)) - } - var args = [] - var procArgs = ["slow"] - for(var i=0; i<=NUM_EXPAND; ++i) { - args.push("a" + i) - procArgs.push("o" + i) - } - var code = [ - "function getOrientation(", args.join(), "){switch(arguments.length){case 0:case 1:return 0;" +function createQuickSort(order, dtype, insertionSort) { + var code = [ "'use strict'" ] + var funcName = ["ndarrayQuickSort", order.join("d"), dtype].join("") + var funcArgs = ["left", "right", "data", "offset" ].concat(shapeArgs(order.length)) + var allocator = getMallocFree(dtype) + var labelCounter=0 + + code.push(["function ", funcName, "(", funcArgs.join(","), "){"].join("")) + + var vars = [ + "sixth=((right-left+1)/6)|0", + "index1=left+sixth", + "index5=right-sixth", + "index3=(left+right)>>1", + "index2=index3-sixth", + "index4=index3+sixth", + "el1=index1", + "el2=index2", + "el3=index3", + "el4=index4", + "el5=index5", + "less=left+1", + "great=right-1", + "pivots_are_equal=true", + "tmp", + "tmp0", + "x", + "y", + "z", + "k", + "ptr0", + "ptr1", + "ptr2", + "comp_pivot1=0", + "comp_pivot2=0", + "comp=0" ] - for(var i=2; i<=NUM_EXPAND; ++i) { - code.push("case ", i, ":return o", i, "(", args.slice(0, i).join(), ");") + + if(order.length > 1) { + var ele_size = [] + for(var i=1; i=0; --i) { + var j = order[i] + if(j === 0) { + continue + } + code.push(["for(i",j,"=0;i",j," 1) { + for(var i=0; i1) { + code.push("ptr_shift+=d"+j) + } else { + code.push("ptr0+=d"+j) + } + code.push("}") + } + } + + function lexicoLoop(label, ptrs, usePivot, body) { + if(ptrs.length === 1) { + code.push("ptr0="+toPointer(ptrs[0])) + } else { + for(var i=0; i 1) { + for(var i=0; i=1; --i) { + if(usePivot) { + code.push("pivot_ptr+=f"+i) + } + if(ptrs.length > 1) { + code.push("ptr_shift+=e"+i) + } else { + code.push("ptr0+=e"+i) + } + code.push("}") + } + } + + function cleanUp() { + if(order.length > 1 && allocator) { + code.push("free(pivot1)", "free(pivot2)") + } + } + + function compareSwap(a_id, b_id) { + var a = "el"+a_id + var b = "el"+b_id + if(order.length > 1) { + var lbl = "__l" + (++labelCounter) + lexicoLoop(lbl, [a, b], false, [ + "comp=",dataRead("ptr0"),"-",dataRead("ptr1"),"\n", + "if(comp>0){tmp0=", a, ";",a,"=",b,";", b,"=tmp0;break ", lbl,"}\n", + "if(comp<0){break ", lbl, "}" + ].join("")) + } else { + code.push(["if(", dataRead(toPointer(a)), ">", dataRead(toPointer(b)), "){tmp0=", a, ";",a,"=",b,";", b,"=tmp0}"].join("")) + } + } + + compareSwap(1, 2) + compareSwap(4, 5) + compareSwap(1, 3) + compareSwap(2, 3) + compareSwap(1, 4) + compareSwap(3, 4) + compareSwap(2, 5) + compareSwap(2, 3) + compareSwap(4, 5) + + if(order.length > 1) { + cacheLoop(["el1", "el2", "el3", "el4", "el5", "index1", "index3", "index5"], true, [ + "pivot1[pivot_ptr]=",dataRead("ptr1"),"\n", + "pivot2[pivot_ptr]=",dataRead("ptr3"),"\n", + "pivots_are_equal=pivots_are_equal&&(pivot1[pivot_ptr]===pivot2[pivot_ptr])\n", + "x=",dataRead("ptr0"),"\n", + "y=",dataRead("ptr2"),"\n", + "z=",dataRead("ptr4"),"\n", + dataWrite("ptr5", "x"),"\n", + dataWrite("ptr6", "y"),"\n", + dataWrite("ptr7", "z") + ].join("")) + } else { + code.push([ + "pivot1=", dataRead(toPointer("el2")), "\n", + "pivot2=", dataRead(toPointer("el4")), "\n", + "pivots_are_equal=pivot1===pivot2\n", + "x=", dataRead(toPointer("el1")), "\n", + "y=", dataRead(toPointer("el3")), "\n", + "z=", dataRead(toPointer("el5")), "\n", + dataWrite(toPointer("index1"), "x"), "\n", + dataWrite(toPointer("index3"), "y"), "\n", + dataWrite(toPointer("index5"), "z") + ].join("")) + } + - var proc = Function.apply(undefined, procArgs) - module.exports = proc.apply(undefined, [slowOrient].concat(CACHED)) - for(var i=0; i<=NUM_EXPAND; ++i) { - module.exports[i] = CACHED[i] + function moveElement(dst, src) { + if(order.length > 1) { + cacheLoop([dst, src], false, + dataWrite("ptr0", dataRead("ptr1")) + ) + } else { + code.push(dataWrite(toPointer(dst), dataRead(toPointer(src)))) + } } + + moveElement("index2", "left") + moveElement("index4", "right") + + function comparePivot(result, ptr, n) { + if(order.length > 1) { + var lbl = "__l" + (++labelCounter) + lexicoLoop(lbl, [ptr], true, [ + result,"=",dataRead("ptr0"),"-pivot",n,"[pivot_ptr]\n", + "if(",result,"!==0){break ", lbl, "}" + ].join("")) + } else { + code.push([result,"=", dataRead(toPointer(ptr)), "-pivot", n].join("")) + } + } + + function swapElements(a, b) { + if(order.length > 1) { + cacheLoop([a,b],false,[ + "tmp=",dataRead("ptr0"),"\n", + dataWrite("ptr0", dataRead("ptr1")),"\n", + dataWrite("ptr1", "tmp") + ].join("")) + } else { + code.push([ + "ptr0=",toPointer(a),"\n", + "ptr1=",toPointer(b),"\n", + "tmp=",dataRead("ptr0"),"\n", + dataWrite("ptr0", dataRead("ptr1")),"\n", + dataWrite("ptr1", "tmp") + ].join("")) + } + } + + function tripleSwap(k, less, great) { + if(order.length > 1) { + cacheLoop([k,less,great], false, [ + "tmp=",dataRead("ptr0"),"\n", + dataWrite("ptr0", dataRead("ptr1")),"\n", + dataWrite("ptr1", dataRead("ptr2")),"\n", + dataWrite("ptr2", "tmp") + ].join("")) + code.push("++"+less, "--"+great) + } else { + code.push([ + "ptr0=",toPointer(k),"\n", + "ptr1=",toPointer(less),"\n", + "ptr2=",toPointer(great),"\n", + "++",less,"\n", + "--",great,"\n", + "tmp=", dataRead("ptr0"), "\n", + dataWrite("ptr0", dataRead("ptr1")), "\n", + dataWrite("ptr1", dataRead("ptr2")), "\n", + dataWrite("ptr2", "tmp") + ].join("")) + } + } + + function swapAndDecrement(k, great) { + swapElements(k, great) + code.push("--"+great) + } + + code.push("if(pivots_are_equal){") + //Pivots are equal case + code.push("for(k=less;k<=great;++k){") + comparePivot("comp", "k", 1) + code.push("if(comp===0){continue}") + code.push("if(comp<0){") + code.push("if(k!==less){") + swapElements("k", "less") + code.push("}") + code.push("++less") + code.push("}else{") + code.push("while(true){") + comparePivot("comp", "great", 1) + code.push("if(comp>0){") + code.push("great--") + code.push("}else if(comp<0){") + tripleSwap("k", "less", "great") + code.push("break") + code.push("}else{") + swapAndDecrement("k", "great") + code.push("break") + code.push("}") + code.push("}") + code.push("}") + code.push("}") + code.push("}else{") + //Pivots not equal case + code.push("for(k=less;k<=great;++k){") + comparePivot("comp_pivot1", "k", 1) + code.push("if(comp_pivot1<0){") + code.push("if(k!==less){") + swapElements("k", "less") + code.push("}") + code.push("++less") + code.push("}else{") + comparePivot("comp_pivot2", "k", 2) + code.push("if(comp_pivot2>0){") + code.push("while(true){") + comparePivot("comp", "great", 2) + code.push("if(comp>0){") + code.push("if(--great1) { + cacheLoop([mem_dest, pivot_dest], true, [ + dataWrite("ptr0", dataRead("ptr1")), "\n", + dataWrite("ptr1", ["pivot",pivot,"[pivot_ptr]"].join("")) + ].join("")) + } else { + code.push( + dataWrite(toPointer(mem_dest), dataRead(toPointer(pivot_dest))), + dataWrite(toPointer(pivot_dest), "pivot"+pivot)) + } + } + + storePivot("left", "(less-1)", 1) + storePivot("right", "(great+1)", 2) + + //Recursive sort call + function doSort(left, right) { + code.push([ + "if((",right,"-",left,")<=",INSERTION_SORT_THRESHOLD,"){\n", + "insertionSort(", left, ",", right, ",data,offset,", shapeArgs(order.length).join(","), ")\n", + "}else{\n", + funcName, "(", left, ",", right, ",data,offset,", shapeArgs(order.length).join(","), ")\n", + "}" + ].join("")) + } + doSort("left", "(less-2)") + doSort("(great+2)", "right") + + //If pivots are equal, then early out + code.push("if(pivots_are_equal){") + cleanUp() + code.push("return") + code.push("}") + + function walkPointer(ptr, pivot, body) { + if(order.length > 1) { + code.push(["__l",++labelCounter,":while(true){"].join("")) + cacheLoop([ptr], true, [ + "if(", dataRead("ptr0"), "!==pivot", pivot, "[pivot_ptr]){break __l", labelCounter, "}" + ].join("")) + code.push(body, "}") + } else { + code.push(["while(", dataRead(toPointer(ptr)), "===pivot", pivot, "){", body, "}"].join("")) + } + } + + //Check bounds + code.push("if(lessindex5){") + + walkPointer("less", 1, "++less") + walkPointer("great", 2, "--great") + + code.push("for(k=less;k<=great;++k){") + comparePivot("comp_pivot1", "k", 1) + code.push("if(comp_pivot1===0){") + code.push("if(k!==less){") + swapElements("k", "less") + code.push("}") + code.push("++less") + code.push("}else{") + comparePivot("comp_pivot2", "k", 2) + code.push("if(comp_pivot2===0){") + code.push("while(true){") + comparePivot("comp", "great", 2) + code.push("if(comp===0){") + code.push("if(--great 1 && allocator) { + var compiled = new Function("insertionSort", "malloc", "free", code.join("\n")) + return compiled(insertionSort, allocator[0], allocator[1]) + } + var compiled = new Function("insertionSort", code.join("\n")) + return compiled(insertionSort) } -generateOrientationProc() -},{"robust-scale":1123,"robust-subtract":1124,"robust-sum":1125,"two-product":1126}],1128:[function(require,module,exports){ +function compileSort(order, dtype) { + var code = ["'use strict'"] + var funcName = ["ndarraySortWrapper", order.join("d"), dtype].join("") + var funcArgs = [ "array" ] + + code.push(["function ", funcName, "(", funcArgs.join(","), "){"].join("")) + + //Unpack local variables from array + var vars = ["data=array.data,offset=array.offset|0,shape=array.shape,stride=array.stride"] + for(var i=0; i 0) { + vars.push(["d",j,"=s",j,"-d",p,"*n",p].join("")) + } else { + vars.push(["d",j,"=s",j].join("")) + } + p = j + } + var k = order.length-1-i + if(k !== 0) { + if(q > 0) { + vars.push(["e",k,"=s",k,"-e",q,"*n",q, + ",f",k,"=",scratch_stride[k],"-f",q,"*n",q].join("")) + } else { + vars.push(["e",k,"=s",k,",f",k,"=",scratch_stride[k]].join("")) + } + q = k + } + } + + //Declare local variables + code.push("var " + vars.join(",")) + + //Create arguments for subroutine + var sortArgs = ["0", "n0-1", "data", "offset"].concat(shapeArgs(order.length)) + + //Call main sorting routine + code.push([ + "if(n0<=",INSERTION_SORT_THRESHOLD,"){", + "insertionSort(", sortArgs.join(","), ")}else{", + "quickSort(", sortArgs.join(","), + ")}" + ].join("")) + + //Return + code.push("}return " + funcName) + + //Link everything together + var result = new Function("insertionSort", "quickSort", code.join("\n")) + var insertionSort = createInsertionSort(order, dtype) + var quickSort = createQuickSort(order, dtype, insertionSort) + return result(insertionSort, quickSort) +} + +module.exports = compileSort +},{"typedarray-pool":992}],441:[function(require,module,exports){ +"use strict" + +var compile = require("./lib/compile_sort.js") +var CACHE = {} + +function sort(array) { + var order = array.order + var dtype = array.dtype + var typeSig = [order, dtype ] + var typeName = typeSig.join(":") + var compiled = CACHE[typeName] + if(!compiled) { + CACHE[typeName] = compiled = compile(order, dtype) + } + compiled(array) + return array +} + +module.exports = sort +},{"./lib/compile_sort.js":440}],442:[function(require,module,exports){ 'use strict' -module.exports = toSuperScript +var interp = require('ndarray-linear-interpolate') -var SUPERSCRIPTS = { - ' ': ' ', - '0': '⁰', - '1': '¹', - '2': '²', - '3': '³', - '4': '⁴', - '5': '⁵', - '6': '⁶', - '7': '⁷', - '8': '⁸', - '9': '⁹', - '+': '⁺', - '-': '⁻', - 'a': 'ᵃ', - 'b': 'ᵇ', - 'c': 'ᶜ', - 'd': 'ᵈ', - 'e': 'ᵉ', - 'f': 'ᶠ', - 'g': 'ᵍ', - 'h': 'ʰ', - 'i': 'ⁱ', - 'j': 'ʲ', - 'k': 'ᵏ', - 'l': 'ˡ', - 'm': 'ᵐ', - 'n': 'ⁿ', - 'o': 'ᵒ', - 'p': 'ᵖ', - 'r': 'ʳ', - 's': 'ˢ', - 't': 'ᵗ', - 'u': 'ᵘ', - 'v': 'ᵛ', - 'w': 'ʷ', - 'x': 'ˣ', - 'y': 'ʸ', - 'z': 'ᶻ' + +var do_warp = require('cwise/lib/wrapper')({"args":["index","array","scalar","scalar","scalar"],"pre":{"body":"{this_warped=new Array(_inline_3_arg4_)}","args":[{"name":"_inline_3_arg0_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_3_arg1_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_3_arg2_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_3_arg3_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_3_arg4_","lvalue":false,"rvalue":true,"count":1}],"thisVars":["this_warped"],"localVars":[]},"body":{"body":"{_inline_4_arg2_(this_warped,_inline_4_arg0_),_inline_4_arg3_.apply(void 0,this_warped)}","args":[{"name":"_inline_4_arg0_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_4_arg1_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_4_arg2_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_4_arg3_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_4_arg4_","lvalue":false,"rvalue":false,"count":0}],"thisVars":["this_warped"],"localVars":[]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"debug":false,"funcName":"warpND","blockSize":64}) + +var do_warp_1 = require('cwise/lib/wrapper')({"args":["index","array","scalar","scalar","scalar"],"pre":{"body":"{this_warped=[0]}","args":[],"thisVars":["this_warped"],"localVars":[]},"body":{"body":"{_inline_7_arg2_(this_warped,_inline_7_arg0_),_inline_7_arg3_(_inline_7_arg4_,this_warped[0])}","args":[{"name":"_inline_7_arg0_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_7_arg1_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_7_arg2_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_7_arg3_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_7_arg4_","lvalue":false,"rvalue":true,"count":1}],"thisVars":["this_warped"],"localVars":[]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"debug":false,"funcName":"warp1D","blockSize":64}) + +var do_warp_2 = require('cwise/lib/wrapper')({"args":["index","array","scalar","scalar","scalar"],"pre":{"body":"{this_warped=[0,0]}","args":[],"thisVars":["this_warped"],"localVars":[]},"body":{"body":"{_inline_10_arg2_(this_warped,_inline_10_arg0_),_inline_10_arg3_(_inline_10_arg4_,this_warped[0],this_warped[1])}","args":[{"name":"_inline_10_arg0_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_10_arg1_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_10_arg2_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_10_arg3_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_10_arg4_","lvalue":false,"rvalue":true,"count":1}],"thisVars":["this_warped"],"localVars":[]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"debug":false,"funcName":"warp2D","blockSize":64}) + +var do_warp_3 = require('cwise/lib/wrapper')({"args":["index","array","scalar","scalar","scalar"],"pre":{"body":"{this_warped=[0,0,0]}","args":[],"thisVars":["this_warped"],"localVars":[]},"body":{"body":"{_inline_13_arg2_(this_warped,_inline_13_arg0_),_inline_13_arg3_(_inline_13_arg4_,this_warped[0],this_warped[1],this_warped[2])}","args":[{"name":"_inline_13_arg0_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_13_arg1_","lvalue":false,"rvalue":false,"count":0},{"name":"_inline_13_arg2_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_13_arg3_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_13_arg4_","lvalue":false,"rvalue":true,"count":1}],"thisVars":["this_warped"],"localVars":[]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"debug":false,"funcName":"warp3D","blockSize":64}) + +module.exports = function warp(dest, src, func) { + switch(src.shape.length) { + case 1: + do_warp_1(dest, func, interp.d1, src) + break + case 2: + do_warp_2(dest, func, interp.d2, src) + break + case 3: + do_warp_3(dest, func, interp.d3, src) + break + default: + do_warp(dest, func, interp.bind(undefined, src), src.shape.length) + break + } + return dest } -function toSuperScript(x) { - return x.split('').map(function(c) { - if(c in SUPERSCRIPTS) { - return SUPERSCRIPTS[c] - } - return '' - }).join('') +},{"cwise/lib/wrapper":85,"ndarray-linear-interpolate":436}],443:[function(require,module,exports){ +var iota = require("iota-array") +var isBuffer = require("is-buffer") + +var hasTypedArrays = ((typeof Float64Array) !== "undefined") + +function compare1st(a, b) { + return a[0] - b[0] } -},{}],1129:[function(require,module,exports){ -// TinyColor v1.4.1 -// https://github.com/bgrins/TinyColor -// Brian Grinstead, MIT License - -(function(Math) { - -var trimLeft = /^\s+/, - trimRight = /\s+$/, - tinyCounter = 0, - mathRound = Math.round, - mathMin = Math.min, - mathMax = Math.max, - mathRandom = Math.random; - -function tinycolor (color, opts) { - - color = (color) ? color : ''; - opts = opts || { }; - - // If input is already a tinycolor, return itself - if (color instanceof tinycolor) { - return color; - } - // If we are called as a function, call using new instead - if (!(this instanceof tinycolor)) { - return new tinycolor(color, opts); - } - - var rgb = inputToRGB(color); - this._originalInput = color, - this._r = rgb.r, - this._g = rgb.g, - this._b = rgb.b, - this._a = rgb.a, - this._roundA = mathRound(100*this._a) / 100, - this._format = opts.format || rgb.format; - this._gradientType = opts.gradientType; - - // Don't let the range of [0,255] come back in [0,1]. - // Potentially lose a little bit of precision here, but will fix issues where - // .5 gets interpreted as half of the total, instead of half of 1 - // If it was supposed to be 128, this was already taken care of by `inputToRgb` - if (this._r < 1) { this._r = mathRound(this._r); } - if (this._g < 1) { this._g = mathRound(this._g); } - if (this._b < 1) { this._b = mathRound(this._b); } - - this._ok = rgb.ok; - this._tc_id = tinyCounter++; +function order() { + var stride = this.stride + var terms = new Array(stride.length) + var i + for(i=0; i= 0; - var needsAlphaFormat = !formatSet && hasAlpha && (format === "hex" || format === "hex6" || format === "hex3" || format === "hex4" || format === "hex8" || format === "name"); - - if (needsAlphaFormat) { - // Special case for "transparent", all other non-alpha formats - // will return rgba when there is transparency. - if (format === "name" && this._a === 0) { - return this.toName(); - } - return this.toRgbString(); - } - if (format === "rgb") { - formattedString = this.toRgbString(); - } - if (format === "prgb") { - formattedString = this.toPercentageRgbString(); - } - if (format === "hex" || format === "hex6") { - formattedString = this.toHexString(); - } - if (format === "hex3") { - formattedString = this.toHexString(true); - } - if (format === "hex4") { - formattedString = this.toHex8String(true); - } - if (format === "hex8") { - formattedString = this.toHex8String(); - } - if (format === "name") { - formattedString = this.toName(); - } - if (format === "hsl") { - formattedString = this.toHslString(); - } - if (format === "hsv") { - formattedString = this.toHsvString(); - } - - return formattedString || this.toHexString(); - }, - clone: function() { - return tinycolor(this.toString()); - }, - - _applyModification: function(fn, args) { - var color = fn.apply(null, [this].concat([].slice.call(args))); - this._r = color._r; - this._g = color._g; - this._b = color._b; - this.setAlpha(color._a); - return this; - }, - lighten: function() { - return this._applyModification(lighten, arguments); - }, - brighten: function() { - return this._applyModification(brighten, arguments); - }, - darken: function() { - return this._applyModification(darken, arguments); - }, - desaturate: function() { - return this._applyModification(desaturate, arguments); - }, - saturate: function() { - return this._applyModification(saturate, arguments); - }, - greyscale: function() { - return this._applyModification(greyscale, arguments); - }, - spin: function() { - return this._applyModification(spin, arguments); - }, - - _applyCombination: function(fn, args) { - return fn.apply(null, [this].concat([].slice.call(args))); - }, - analogous: function() { - return this._applyCombination(analogous, arguments); - }, - complement: function() { - return this._applyCombination(complement, arguments); - }, - monochromatic: function() { - return this._applyCombination(monochromatic, arguments); - }, - splitcomplement: function() { - return this._applyCombination(splitcomplement, arguments); - }, - triad: function() { - return this._applyCombination(triad, arguments); - }, - tetrad: function() { - return this._applyCombination(tetrad, arguments); - } -}; - -// If input is an object, force 1 into "1.0" to handle ratios properly -// String input requires "1.0" as input, so 1 will be treated as 1 -tinycolor.fromRatio = function(color, opts) { - if (typeof color == "object") { - var newColor = {}; - for (var i in color) { - if (color.hasOwnProperty(i)) { - if (i === "a") { - newColor[i] = color[i]; - } - else { - newColor[i] = convertToPercentage(color[i]); - } - } - } - color = newColor; - } - - return tinycolor(color, opts); -}; - -// Given a string or object, convert that input to RGB -// Possible string inputs: -// -// "red" -// "#f00" or "f00" -// "#ff0000" or "ff0000" -// "#ff000000" or "ff000000" -// "rgb 255 0 0" or "rgb (255, 0, 0)" -// "rgb 1.0 0 0" or "rgb (1, 0, 0)" -// "rgba (255, 0, 0, 1)" or "rgba 255, 0, 0, 1" -// "rgba (1.0, 0, 0, 1)" or "rgba 1.0, 0, 0, 1" -// "hsl(0, 100%, 50%)" or "hsl 0 100% 50%" -// "hsla(0, 100%, 50%, 1)" or "hsla 0 100% 50%, 1" -// "hsv(0, 100%, 100%)" or "hsv 0 100% 100%" -// -function inputToRGB(color) { - - var rgb = { r: 0, g: 0, b: 0 }; - var a = 1; - var s = null; - var v = null; - var l = null; - var ok = false; - var format = false; - - if (typeof color == "string") { - color = stringInputToObject(color); - } - - if (typeof color == "object") { - if (isValidCSSUnit(color.r) && isValidCSSUnit(color.g) && isValidCSSUnit(color.b)) { - rgb = rgbToRgb(color.r, color.g, color.b); - ok = true; - format = String(color.r).substr(-1) === "%" ? "prgb" : "rgb"; - } - else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.v)) { - s = convertToPercentage(color.s); - v = convertToPercentage(color.v); - rgb = hsvToRgb(color.h, s, v); - ok = true; - format = "hsv"; - } - else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.l)) { - s = convertToPercentage(color.s); - l = convertToPercentage(color.l); - rgb = hslToRgb(color.h, s, l); - ok = true; - format = "hsl"; - } - - if (color.hasOwnProperty("a")) { - a = color.a; - } - } - - a = boundAlpha(a); - - return { - ok: ok, - format: color.format || format, - r: mathMin(255, mathMax(rgb.r, 0)), - g: mathMin(255, mathMax(rgb.g, 0)), - b: mathMin(255, mathMax(rgb.b, 0)), - a: a - }; -} - - -// Conversion Functions -// -------------------- - -// `rgbToHsl`, `rgbToHsv`, `hslToRgb`, `hsvToRgb` modified from: -// - -// `rgbToRgb` -// Handle bounds / percentage checking to conform to CSS color spec -// -// *Assumes:* r, g, b in [0, 255] or [0, 1] -// *Returns:* { r, g, b } in [0, 255] -function rgbToRgb(r, g, b){ - return { - r: bound01(r, 255) * 255, - g: bound01(g, 255) * 255, - b: bound01(b, 255) * 255 - }; -} - -// `rgbToHsl` -// Converts an RGB color value to HSL. -// *Assumes:* r, g, and b are contained in [0, 255] or [0, 1] -// *Returns:* { h, s, l } in [0,1] -function rgbToHsl(r, g, b) { - - r = bound01(r, 255); - g = bound01(g, 255); - b = bound01(b, 255); - - var max = mathMax(r, g, b), min = mathMin(r, g, b); - var h, s, l = (max + min) / 2; - - if(max == min) { - h = s = 0; // achromatic - } - else { - var d = max - min; - s = l > 0.5 ? d / (2 - max - min) : d / (max + min); - switch(max) { - case r: h = (g - b) / d + (g < b ? 6 : 0); break; - case g: h = (b - r) / d + 2; break; - case b: h = (r - g) / d + 4; break; - } - - h /= 6; - } - - return { h: h, s: s, l: l }; -} - -// `hslToRgb` -// Converts an HSL color value to RGB. -// *Assumes:* h is contained in [0, 1] or [0, 360] and s and l are contained [0, 1] or [0, 100] -// *Returns:* { r, g, b } in the set [0, 255] -function hslToRgb(h, s, l) { - var r, g, b; - - h = bound01(h, 360); - s = bound01(s, 100); - l = bound01(l, 100); - - function hue2rgb(p, q, t) { - if(t < 0) t += 1; - if(t > 1) t -= 1; - if(t < 1/6) return p + (q - p) * 6 * t; - if(t < 1/2) return q; - if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; - return p; - } - - if(s === 0) { - r = g = b = l; // achromatic - } - else { - var q = l < 0.5 ? l * (1 + s) : l + s - l * s; - var p = 2 * l - q; - r = hue2rgb(p, q, h + 1/3); - g = hue2rgb(p, q, h); - b = hue2rgb(p, q, h - 1/3); - } - - return { r: r * 255, g: g * 255, b: b * 255 }; -} - -// `rgbToHsv` -// Converts an RGB color value to HSV -// *Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1] -// *Returns:* { h, s, v } in [0,1] -function rgbToHsv(r, g, b) { - - r = bound01(r, 255); - g = bound01(g, 255); - b = bound01(b, 255); - - var max = mathMax(r, g, b), min = mathMin(r, g, b); - var h, s, v = max; - - var d = max - min; - s = max === 0 ? 0 : d / max; - - if(max == min) { - h = 0; // achromatic - } - else { - switch(max) { - case r: h = (g - b) / d + (g < b ? 6 : 0); break; - case g: h = (b - r) / d + 2; break; - case b: h = (r - g) / d + 4; break; - } - h /= 6; - } - return { h: h, s: s, v: v }; -} - -// `hsvToRgb` -// Converts an HSV color value to RGB. -// *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100] -// *Returns:* { r, g, b } in the set [0, 255] - function hsvToRgb(h, s, v) { - - h = bound01(h, 360) * 6; - s = bound01(s, 100); - v = bound01(v, 100); - - var i = Math.floor(h), - f = h - i, - p = v * (1 - s), - q = v * (1 - f * s), - t = v * (1 - (1 - f) * s), - mod = i % 6, - r = [v, q, p, p, t, v][mod], - g = [t, v, v, q, p, p][mod], - b = [p, p, t, v, v, q][mod]; - - return { r: r * 255, g: g * 255, b: b * 255 }; -} - -// `rgbToHex` -// Converts an RGB color to hex -// Assumes r, g, and b are contained in the set [0, 255] -// Returns a 3 or 6 character hex -function rgbToHex(r, g, b, allow3Char) { - - var hex = [ - pad2(mathRound(r).toString(16)), - pad2(mathRound(g).toString(16)), - pad2(mathRound(b).toString(16)) - ]; - - // Return a 3 character hex if possible - if (allow3Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1)) { - return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0); - } - - return hex.join(""); -} - -// `rgbaToHex` -// Converts an RGBA color plus alpha transparency to hex -// Assumes r, g, b are contained in the set [0, 255] and -// a in [0, 1]. Returns a 4 or 8 character rgba hex -function rgbaToHex(r, g, b, a, allow4Char) { - - var hex = [ - pad2(mathRound(r).toString(16)), - pad2(mathRound(g).toString(16)), - pad2(mathRound(b).toString(16)), - pad2(convertDecimalToHex(a)) - ]; - - // Return a 4 character hex if possible - if (allow4Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1) && hex[3].charAt(0) == hex[3].charAt(1)) { - return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0) + hex[3].charAt(0); - } - - return hex.join(""); -} - -// `rgbaToArgbHex` -// Converts an RGBA color to an ARGB Hex8 string -// Rarely used, but required for "toFilter()" -function rgbaToArgbHex(r, g, b, a) { - - var hex = [ - pad2(convertDecimalToHex(a)), - pad2(mathRound(r).toString(16)), - pad2(mathRound(g).toString(16)), - pad2(mathRound(b).toString(16)) - ]; - - return hex.join(""); -} - -// `equals` -// Can be called with any tinycolor input -tinycolor.equals = function (color1, color2) { - if (!color1 || !color2) { return false; } - return tinycolor(color1).toRgbString() == tinycolor(color2).toRgbString(); -}; - -tinycolor.random = function() { - return tinycolor.fromRatio({ - r: mathRandom(), - g: mathRandom(), - b: mathRandom() - }); -}; - - -// Modification Functions -// ---------------------- -// Thanks to less.js for some of the basics here -// - -function desaturate(color, amount) { - amount = (amount === 0) ? 0 : (amount || 10); - var hsl = tinycolor(color).toHsl(); - hsl.s -= amount / 100; - hsl.s = clamp01(hsl.s); - return tinycolor(hsl); -} - -function saturate(color, amount) { - amount = (amount === 0) ? 0 : (amount || 10); - var hsl = tinycolor(color).toHsl(); - hsl.s += amount / 100; - hsl.s = clamp01(hsl.s); - return tinycolor(hsl); -} - -function greyscale(color) { - return tinycolor(color).desaturate(100); -} - -function lighten (color, amount) { - amount = (amount === 0) ? 0 : (amount || 10); - var hsl = tinycolor(color).toHsl(); - hsl.l += amount / 100; - hsl.l = clamp01(hsl.l); - return tinycolor(hsl); -} - -function brighten(color, amount) { - amount = (amount === 0) ? 0 : (amount || 10); - var rgb = tinycolor(color).toRgb(); - rgb.r = mathMax(0, mathMin(255, rgb.r - mathRound(255 * - (amount / 100)))); - rgb.g = mathMax(0, mathMin(255, rgb.g - mathRound(255 * - (amount / 100)))); - rgb.b = mathMax(0, mathMin(255, rgb.b - mathRound(255 * - (amount / 100)))); - return tinycolor(rgb); -} - -function darken (color, amount) { - amount = (amount === 0) ? 0 : (amount || 10); - var hsl = tinycolor(color).toHsl(); - hsl.l -= amount / 100; - hsl.l = clamp01(hsl.l); - return tinycolor(hsl); -} - -// Spin takes a positive or negative amount within [-360, 360] indicating the change of hue. -// Values outside of this range will be wrapped into this range. -function spin(color, amount) { - var hsl = tinycolor(color).toHsl(); - var hue = (hsl.h + amount) % 360; - hsl.h = hue < 0 ? 360 + hue : hue; - return tinycolor(hsl); -} - -// Combination Functions -// --------------------- -// Thanks to jQuery xColor for some of the ideas behind these -// - -function complement(color) { - var hsl = tinycolor(color).toHsl(); - hsl.h = (hsl.h + 180) % 360; - return tinycolor(hsl); -} - -function triad(color) { - var hsl = tinycolor(color).toHsl(); - var h = hsl.h; - return [ - tinycolor(color), - tinycolor({ h: (h + 120) % 360, s: hsl.s, l: hsl.l }), - tinycolor({ h: (h + 240) % 360, s: hsl.s, l: hsl.l }) - ]; -} - -function tetrad(color) { - var hsl = tinycolor(color).toHsl(); - var h = hsl.h; - return [ - tinycolor(color), - tinycolor({ h: (h + 90) % 360, s: hsl.s, l: hsl.l }), - tinycolor({ h: (h + 180) % 360, s: hsl.s, l: hsl.l }), - tinycolor({ h: (h + 270) % 360, s: hsl.s, l: hsl.l }) - ]; -} - -function splitcomplement(color) { - var hsl = tinycolor(color).toHsl(); - var h = hsl.h; - return [ - tinycolor(color), - tinycolor({ h: (h + 72) % 360, s: hsl.s, l: hsl.l}), - tinycolor({ h: (h + 216) % 360, s: hsl.s, l: hsl.l}) - ]; -} - -function analogous(color, results, slices) { - results = results || 6; - slices = slices || 30; - - var hsl = tinycolor(color).toHsl(); - var part = 360 / slices; - var ret = [tinycolor(color)]; - - for (hsl.h = ((hsl.h - (part * results >> 1)) + 720) % 360; --results; ) { - hsl.h = (hsl.h + part) % 360; - ret.push(tinycolor(hsl)); - } - return ret; -} - -function monochromatic(color, results) { - results = results || 6; - var hsv = tinycolor(color).toHsv(); - var h = hsv.h, s = hsv.s, v = hsv.v; - var ret = []; - var modification = 1 / results; - - while (results--) { - ret.push(tinycolor({ h: h, s: s, v: v})); - v = (v + modification) % 1; - } - - return ret; -} - -// Utility Functions -// --------------------- - -tinycolor.mix = function(color1, color2, amount) { - amount = (amount === 0) ? 0 : (amount || 50); - - var rgb1 = tinycolor(color1).toRgb(); - var rgb2 = tinycolor(color2).toRgb(); - - var p = amount / 100; - - var rgba = { - r: ((rgb2.r - rgb1.r) * p) + rgb1.r, - g: ((rgb2.g - rgb1.g) * p) + rgb1.g, - b: ((rgb2.b - rgb1.b) * p) + rgb1.b, - a: ((rgb2.a - rgb1.a) * p) + rgb1.a - }; - - return tinycolor(rgba); -}; - - -// Readability Functions -// --------------------- -// false -// tinycolor.isReadable("#000", "#111",{level:"AA",size:"large"}) => false -tinycolor.isReadable = function(color1, color2, wcag2) { - var readability = tinycolor.readability(color1, color2); - var wcag2Parms, out; - - out = false; - - wcag2Parms = validateWCAG2Parms(wcag2); - switch (wcag2Parms.level + wcag2Parms.size) { - case "AAsmall": - case "AAAlarge": - out = readability >= 4.5; - break; - case "AAlarge": - out = readability >= 3; - break; - case "AAAsmall": - out = readability >= 7; - break; - } - return out; - -}; - -// `mostReadable` -// Given a base color and a list of possible foreground or background -// colors for that base, returns the most readable color. -// Optionally returns Black or White if the most readable color is unreadable. -// *Example* -// tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:false}).toHexString(); // "#112255" -// tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:true}).toHexString(); // "#ffffff" -// tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"large"}).toHexString(); // "#faf3f3" -// tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"small"}).toHexString(); // "#ffffff" -tinycolor.mostReadable = function(baseColor, colorList, args) { - var bestColor = null; - var bestScore = 0; - var readability; - var includeFallbackColors, level, size ; - args = args || {}; - includeFallbackColors = args.includeFallbackColors ; - level = args.level; - size = args.size; - - for (var i= 0; i < colorList.length ; i++) { - readability = tinycolor.readability(baseColor, colorList[i]); - if (readability > bestScore) { - bestScore = readability; - bestColor = tinycolor(colorList[i]); - } - } - - if (tinycolor.isReadable(baseColor, bestColor, {"level":level,"size":size}) || !includeFallbackColors) { - return bestColor; - } - else { - args.includeFallbackColors=false; - return tinycolor.mostReadable(baseColor,["#fff", "#000"],args); - } -}; - - -// Big List of Colors -// ------------------ -// -var names = tinycolor.names = { - aliceblue: "f0f8ff", - antiquewhite: "faebd7", - aqua: "0ff", - aquamarine: "7fffd4", - azure: "f0ffff", - beige: "f5f5dc", - bisque: "ffe4c4", - black: "000", - blanchedalmond: "ffebcd", - blue: "00f", - blueviolet: "8a2be2", - brown: "a52a2a", - burlywood: "deb887", - burntsienna: "ea7e5d", - cadetblue: "5f9ea0", - chartreuse: "7fff00", - chocolate: "d2691e", - coral: "ff7f50", - cornflowerblue: "6495ed", - cornsilk: "fff8dc", - crimson: "dc143c", - cyan: "0ff", - darkblue: "00008b", - darkcyan: "008b8b", - darkgoldenrod: "b8860b", - darkgray: "a9a9a9", - darkgreen: "006400", - darkgrey: "a9a9a9", - darkkhaki: "bdb76b", - darkmagenta: "8b008b", - darkolivegreen: "556b2f", - darkorange: "ff8c00", - darkorchid: "9932cc", - darkred: "8b0000", - darksalmon: "e9967a", - darkseagreen: "8fbc8f", - darkslateblue: "483d8b", - darkslategray: "2f4f4f", - darkslategrey: "2f4f4f", - darkturquoise: "00ced1", - darkviolet: "9400d3", - deeppink: "ff1493", - deepskyblue: "00bfff", - dimgray: "696969", - dimgrey: "696969", - dodgerblue: "1e90ff", - firebrick: "b22222", - floralwhite: "fffaf0", - forestgreen: "228b22", - fuchsia: "f0f", - gainsboro: "dcdcdc", - ghostwhite: "f8f8ff", - gold: "ffd700", - goldenrod: "daa520", - gray: "808080", - green: "008000", - greenyellow: "adff2f", - grey: "808080", - honeydew: "f0fff0", - hotpink: "ff69b4", - indianred: "cd5c5c", - indigo: "4b0082", - ivory: "fffff0", - khaki: "f0e68c", - lavender: "e6e6fa", - lavenderblush: "fff0f5", - lawngreen: "7cfc00", - lemonchiffon: "fffacd", - lightblue: "add8e6", - lightcoral: "f08080", - lightcyan: "e0ffff", - lightgoldenrodyellow: "fafad2", - lightgray: "d3d3d3", - lightgreen: "90ee90", - lightgrey: "d3d3d3", - lightpink: "ffb6c1", - lightsalmon: "ffa07a", - lightseagreen: "20b2aa", - lightskyblue: "87cefa", - lightslategray: "789", - lightslategrey: "789", - lightsteelblue: "b0c4de", - lightyellow: "ffffe0", - lime: "0f0", - limegreen: "32cd32", - linen: "faf0e6", - magenta: "f0f", - maroon: "800000", - mediumaquamarine: "66cdaa", - mediumblue: "0000cd", - mediumorchid: "ba55d3", - mediumpurple: "9370db", - mediumseagreen: "3cb371", - mediumslateblue: "7b68ee", - mediumspringgreen: "00fa9a", - mediumturquoise: "48d1cc", - mediumvioletred: "c71585", - midnightblue: "191970", - mintcream: "f5fffa", - mistyrose: "ffe4e1", - moccasin: "ffe4b5", - navajowhite: "ffdead", - navy: "000080", - oldlace: "fdf5e6", - olive: "808000", - olivedrab: "6b8e23", - orange: "ffa500", - orangered: "ff4500", - orchid: "da70d6", - palegoldenrod: "eee8aa", - palegreen: "98fb98", - paleturquoise: "afeeee", - palevioletred: "db7093", - papayawhip: "ffefd5", - peachpuff: "ffdab9", - peru: "cd853f", - pink: "ffc0cb", - plum: "dda0dd", - powderblue: "b0e0e6", - purple: "800080", - rebeccapurple: "663399", - red: "f00", - rosybrown: "bc8f8f", - royalblue: "4169e1", - saddlebrown: "8b4513", - salmon: "fa8072", - sandybrown: "f4a460", - seagreen: "2e8b57", - seashell: "fff5ee", - sienna: "a0522d", - silver: "c0c0c0", - skyblue: "87ceeb", - slateblue: "6a5acd", - slategray: "708090", - slategrey: "708090", - snow: "fffafa", - springgreen: "00ff7f", - steelblue: "4682b4", - tan: "d2b48c", - teal: "008080", - thistle: "d8bfd8", - tomato: "ff6347", - turquoise: "40e0d0", - violet: "ee82ee", - wheat: "f5deb3", - white: "fff", - whitesmoke: "f5f5f5", - yellow: "ff0", - yellowgreen: "9acd32" -}; - -// Make it easy to access colors via `hexNames[hex]` -var hexNames = tinycolor.hexNames = flip(names); - - -// Utilities -// --------- - -// `{ 'name1': 'val1' }` becomes `{ 'val1': 'name1' }` -function flip(o) { - var flipped = { }; - for (var i in o) { - if (o.hasOwnProperty(i)) { - flipped[o[i]] = i; - } - } - return flipped; -} - -// Return a valid alpha value [0,1] with all invalid values being set to 1 -function boundAlpha(a) { - a = parseFloat(a); - - if (isNaN(a) || a < 0 || a > 1) { - a = 1; - } - - return a; -} - -// Take input from [0, n] and return it as [0, 1] -function bound01(n, max) { - if (isOnePointZero(n)) { n = "100%"; } - - var processPercent = isPercentage(n); - n = mathMin(max, mathMax(0, parseFloat(n))); - - // Automatically convert percentage into number - if (processPercent) { - n = parseInt(n * max, 10) / 100; - } - - // Handle floating point rounding errors - if ((Math.abs(n - max) < 0.000001)) { - return 1; - } - - // Convert into [0, 1] range if it isn't already - return (n % max) / parseFloat(max); -} - -// Force a number between 0 and 1 -function clamp01(val) { - return mathMin(1, mathMax(0, val)); -} - -// Parse a base-16 hex value into a base-10 integer -function parseIntFromHex(val) { - return parseInt(val, 16); -} - -// Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1 -// -function isOnePointZero(n) { - return typeof n == "string" && n.indexOf('.') != -1 && parseFloat(n) === 1; -} - -// Check to see if string passed in is a percentage -function isPercentage(n) { - return typeof n === "string" && n.indexOf('%') != -1; -} - -// Force a hex value to have 2 characters -function pad2(c) { - return c.length == 1 ? '0' + c : '' + c; -} - -// Replace a decimal with it's percentage value -function convertToPercentage(n) { - if (n <= 1) { - n = (n * 100) + "%"; - } - - return n; -} - -// Converts a decimal to a hex value -function convertDecimalToHex(d) { - return Math.round(parseFloat(d) * 255).toString(16); -} -// Converts a hex value to a decimal -function convertHexToDecimal(h) { - return (parseIntFromHex(h) / 255); -} - -var matchers = (function() { - - // - var CSS_INTEGER = "[-\\+]?\\d+%?"; - - // - var CSS_NUMBER = "[-\\+]?\\d*\\.\\d+%?"; - - // Allow positive/negative integer/number. Don't capture the either/or, just the entire outcome. - var CSS_UNIT = "(?:" + CSS_NUMBER + ")|(?:" + CSS_INTEGER + ")"; - - // Actual matching. - // Parentheses and commas are optional, but not required. - // Whitespace can take the place of commas or opening paren - var PERMISSIVE_MATCH3 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?"; - var PERMISSIVE_MATCH4 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?"; - - return { - CSS_UNIT: new RegExp(CSS_UNIT), - rgb: new RegExp("rgb" + PERMISSIVE_MATCH3), - rgba: new RegExp("rgba" + PERMISSIVE_MATCH4), - hsl: new RegExp("hsl" + PERMISSIVE_MATCH3), - hsla: new RegExp("hsla" + PERMISSIVE_MATCH4), - hsv: new RegExp("hsv" + PERMISSIVE_MATCH3), - hsva: new RegExp("hsva" + PERMISSIVE_MATCH4), - hex3: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, - hex6: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/, - hex4: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, - hex8: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/ - }; -})(); - -// `isValidCSSUnit` -// Take in a single string / number and check to see if it looks like a CSS unit -// (see `matchers` above for definition). -function isValidCSSUnit(color) { - return !!matchers.CSS_UNIT.exec(color); -} - -// `stringInputToObject` -// Permissive string parsing. Take in a number of formats, and output an object -// based on detected format. Returns `{ r, g, b }` or `{ h, s, l }` or `{ h, s, v}` -function stringInputToObject(color) { - - color = color.replace(trimLeft,'').replace(trimRight, '').toLowerCase(); - var named = false; - if (names[color]) { - color = names[color]; - named = true; - } - else if (color == 'transparent') { - return { r: 0, g: 0, b: 0, a: 0, format: "name" }; - } - - // Try to match string input using regular expressions. - // Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360] - // Just return an object and let the conversion functions handle that. - // This way the result will be the same whether the tinycolor is initialized with string or object. - var match; - if ((match = matchers.rgb.exec(color))) { - return { r: match[1], g: match[2], b: match[3] }; - } - if ((match = matchers.rgba.exec(color))) { - return { r: match[1], g: match[2], b: match[3], a: match[4] }; - } - if ((match = matchers.hsl.exec(color))) { - return { h: match[1], s: match[2], l: match[3] }; - } - if ((match = matchers.hsla.exec(color))) { - return { h: match[1], s: match[2], l: match[3], a: match[4] }; - } - if ((match = matchers.hsv.exec(color))) { - return { h: match[1], s: match[2], v: match[3] }; - } - if ((match = matchers.hsva.exec(color))) { - return { h: match[1], s: match[2], v: match[3], a: match[4] }; - } - if ((match = matchers.hex8.exec(color))) { - return { - r: parseIntFromHex(match[1]), - g: parseIntFromHex(match[2]), - b: parseIntFromHex(match[3]), - a: convertHexToDecimal(match[4]), - format: named ? "name" : "hex8" - }; - } - if ((match = matchers.hex6.exec(color))) { - return { - r: parseIntFromHex(match[1]), - g: parseIntFromHex(match[2]), - b: parseIntFromHex(match[3]), - format: named ? "name" : "hex" - }; - } - if ((match = matchers.hex4.exec(color))) { - return { - r: parseIntFromHex(match[1] + '' + match[1]), - g: parseIntFromHex(match[2] + '' + match[2]), - b: parseIntFromHex(match[3] + '' + match[3]), - a: convertHexToDecimal(match[4] + '' + match[4]), - format: named ? "name" : "hex8" - }; - } - if ((match = matchers.hex3.exec(color))) { - return { - r: parseIntFromHex(match[1] + '' + match[1]), - g: parseIntFromHex(match[2] + '' + match[2]), - b: parseIntFromHex(match[3] + '' + match[3]), - format: named ? "name" : "hex" - }; - } - - return false; -} - -function validateWCAG2Parms(parms) { - // return valid WCAG2 parms for isReadable. - // If input parms are invalid, return {"level":"AA", "size":"small"} - var level, size; - parms = parms || {"level":"AA", "size":"small"}; - level = (parms.level || "AA").toUpperCase(); - size = (parms.size || "small").toLowerCase(); - if (level !== "AA" && level !== "AAA") { - level = "AA"; - } - if (size !== "small" && size !== "large") { - size = "small"; - } - return {"level":level, "size":size}; -} - -// Node: Export function -if (typeof module !== "undefined" && module.exports) { - module.exports = tinycolor; -} -// AMD/requirejs: Define the module -else if (typeof define === 'function' && define.amd) { - define(function () {return tinycolor;}); -} -// Browser: Expose to window -else { - window.tinycolor = tinycolor; -} - -})(Math); - -},{}],1130:[function(require,module,exports){ -// https://github.com/topojson/topojson-client Version 2.1.0. Copyright 2016 Mike Bostock. -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : - typeof define === 'function' && define.amd ? define(['exports'], factory) : - (factory((global.topojson = global.topojson || {}))); -}(this, (function (exports) { 'use strict'; - -var identity = function(x) { - return x; -}; - -var transform = function(topology) { - if ((transform = topology.transform) == null) return identity; - var transform, - x0, - y0, - kx = transform.scale[0], - ky = transform.scale[1], - dx = transform.translate[0], - dy = transform.translate[1]; - return function(point, i) { - if (!i) x0 = y0 = 0; - point[0] = (x0 += point[0]) * kx + dx; - point[1] = (y0 += point[1]) * ky + dy; - return point; - }; -}; - -var bbox = function(topology) { - var bbox = topology.bbox; - - function bboxPoint(p0) { - p1[0] = p0[0], p1[1] = p0[1], t(p1); - if (p1[0] < x0) x0 = p1[0]; - if (p1[0] > x1) x1 = p1[0]; - if (p1[1] < y0) y0 = p1[1]; - if (p1[1] > y1) y1 = p1[1]; +function compileConstructor(dtype, dimension) { + var className = ["View", dimension, "d", dtype].join("") + if(dimension < 0) { + className = "View_Nil" + dtype + } + var useGetters = (dtype === "generic") + + if(dimension === -1) { + //Special case for trivial arrays + var code = + "function "+className+"(a){this.data=a;};\ +var proto="+className+".prototype;\ +proto.dtype='"+dtype+"';\ +proto.index=function(){return -1};\ +proto.size=0;\ +proto.dimension=-1;\ +proto.shape=proto.stride=proto.order=[];\ +proto.lo=proto.hi=proto.transpose=proto.step=\ +function(){return new "+className+"(this.data);};\ +proto.get=proto.set=function(){};\ +proto.pick=function(){return null};\ +return function construct_"+className+"(a){return new "+className+"(a);}" + var procedure = new Function(code) + return procedure() + } else if(dimension === 0) { + //Special case for 0d arrays + var code = + "function "+className+"(a,d) {\ +this.data = a;\ +this.offset = d\ +};\ +var proto="+className+".prototype;\ +proto.dtype='"+dtype+"';\ +proto.index=function(){return this.offset};\ +proto.dimension=0;\ +proto.size=1;\ +proto.shape=\ +proto.stride=\ +proto.order=[];\ +proto.lo=\ +proto.hi=\ +proto.transpose=\ +proto.step=function "+className+"_copy() {\ +return new "+className+"(this.data,this.offset)\ +};\ +proto.pick=function "+className+"_pick(){\ +return TrivialArray(this.data);\ +};\ +proto.valueOf=proto.get=function "+className+"_get(){\ +return "+(useGetters ? "this.data.get(this.offset)" : "this.data[this.offset]")+ +"};\ +proto.set=function "+className+"_set(v){\ +return "+(useGetters ? "this.data.set(this.offset,v)" : "this.data[this.offset]=v")+"\ +};\ +return function construct_"+className+"(a,b,c,d){return new "+className+"(a,d)}" + var procedure = new Function("TrivialArray", code) + return procedure(CACHED_CONSTRUCTORS[dtype][0]) } - function bboxGeometry(o) { - switch (o.type) { - case "GeometryCollection": o.geometries.forEach(bboxGeometry); break; - case "Point": bboxPoint(o.coordinates); break; - case "MultiPoint": o.coordinates.forEach(bboxPoint); break; - } - } + var code = ["'use strict'"] - if (!bbox) { - var t = transform(topology), p0, p1 = new Array(2), name, - x0 = Infinity, y0 = x0, x1 = -x0, y1 = -x0; + //Create constructor for view + var indices = iota(dimension) + var args = indices.map(function(i) { return "i"+i }) + var index_str = "this.offset+" + indices.map(function(i) { + return "this.stride[" + i + "]*i" + i + }).join("+") + var shapeArg = indices.map(function(i) { + return "b"+i + }).join(",") + var strideArg = indices.map(function(i) { + return "c"+i + }).join(",") + code.push( + "function "+className+"(a," + shapeArg + "," + strideArg + ",d){this.data=a", + "this.shape=[" + shapeArg + "]", + "this.stride=[" + strideArg + "]", + "this.offset=d|0}", + "var proto="+className+".prototype", + "proto.dtype='"+dtype+"'", + "proto.dimension="+dimension) - topology.arcs.forEach(function(arc) { - var i = -1, n = arc.length; - while (++i < n) { - p0 = arc[i], p1[0] = p0[0], p1[1] = p0[1], t(p1, i); - if (p1[0] < x0) x0 = p1[0]; - if (p1[0] > x1) x1 = p1[0]; - if (p1[1] < y0) y0 = p1[1]; - if (p1[1] > y1) y1 = p1[1]; - } - }); + //view.size: + code.push("Object.defineProperty(proto,'size',{get:function "+className+"_size(){\ +return "+indices.map(function(i) { return "this.shape["+i+"]" }).join("*"), +"}})") - for (name in topology.objects) { - bboxGeometry(topology.objects[name]); - } - - bbox = topology.bbox = [x0, y0, x1, y1]; - } - - return bbox; -}; - -var reverse = function(array, n) { - var t, j = array.length, i = j - n; - while (i < --j) t = array[i], array[i++] = array[j], array[j] = t; -}; - -var feature = function(topology, o) { - return o.type === "GeometryCollection" - ? {type: "FeatureCollection", features: o.geometries.map(function(o) { return feature$1(topology, o); })} - : feature$1(topology, o); -}; - -function feature$1(topology, o) { - var id = o.id, - bbox = o.bbox, - properties = o.properties == null ? {} : o.properties, - geometry = object(topology, o); - return id == null && bbox == null ? {type: "Feature", properties: properties, geometry: geometry} - : bbox == null ? {type: "Feature", id: id, properties: properties, geometry: geometry} - : {type: "Feature", id: id, bbox: bbox, properties: properties, geometry: geometry}; -} - -function object(topology, o) { - var transformPoint = transform(topology), - arcs = topology.arcs; - - function arc(i, points) { - if (points.length) points.pop(); - for (var a = arcs[i < 0 ? ~i : i], k = 0, n = a.length; k < n; ++k) { - points.push(transformPoint(a[k].slice(), k)); - } - if (i < 0) reverse(points, n); - } - - function point(p) { - return transformPoint(p.slice()); - } - - function line(arcs) { - var points = []; - for (var i = 0, n = arcs.length; i < n; ++i) arc(arcs[i], points); - if (points.length < 2) points.push(points[0].slice()); - return points; - } - - function ring(arcs) { - var points = line(arcs); - while (points.length < 4) points.push(points[0].slice()); - return points; - } - - function polygon(arcs) { - return arcs.map(ring); - } - - function geometry(o) { - var type = o.type, coordinates; - switch (type) { - case "GeometryCollection": return {type: type, geometries: o.geometries.map(geometry)}; - case "Point": coordinates = point(o.coordinates); break; - case "MultiPoint": coordinates = o.coordinates.map(point); break; - case "LineString": coordinates = line(o.arcs); break; - case "MultiLineString": coordinates = o.arcs.map(line); break; - case "Polygon": coordinates = polygon(o.arcs); break; - case "MultiPolygon": coordinates = o.arcs.map(polygon); break; - default: return null; - } - return {type: type, coordinates: coordinates}; - } - - return geometry(o); -} - -var stitch = function(topology, arcs) { - var stitchedArcs = {}, - fragmentByStart = {}, - fragmentByEnd = {}, - fragments = [], - emptyIndex = -1; - - // Stitch empty arcs first, since they may be subsumed by other arcs. - arcs.forEach(function(i, j) { - var arc = topology.arcs[i < 0 ? ~i : i], t; - if (arc.length < 3 && !arc[1][0] && !arc[1][1]) { - t = arcs[++emptyIndex], arcs[emptyIndex] = i, arcs[j] = t; - } - }); - - arcs.forEach(function(i) { - var e = ends(i), - start = e[0], - end = e[1], - f, g; - - if (f = fragmentByEnd[start]) { - delete fragmentByEnd[f.end]; - f.push(i); - f.end = end; - if (g = fragmentByStart[end]) { - delete fragmentByStart[g.start]; - var fg = g === f ? f : f.concat(g); - fragmentByStart[fg.start = f.start] = fragmentByEnd[fg.end = g.end] = fg; - } else { - fragmentByStart[f.start] = fragmentByEnd[f.end] = f; - } - } else if (f = fragmentByStart[end]) { - delete fragmentByStart[f.start]; - f.unshift(i); - f.start = start; - if (g = fragmentByEnd[start]) { - delete fragmentByEnd[g.end]; - var gf = g === f ? f : g.concat(f); - fragmentByStart[gf.start = g.start] = fragmentByEnd[gf.end = f.end] = gf; - } else { - fragmentByStart[f.start] = fragmentByEnd[f.end] = f; + //view.order: + if(dimension === 1) { + code.push("proto.order=[0]") + } else { + code.push("Object.defineProperty(proto,'order',{get:") + if(dimension < 4) { + code.push("function "+className+"_order(){") + if(dimension === 2) { + code.push("return (Math.abs(this.stride[0])>Math.abs(this.stride[1]))?[1,0]:[0,1]}})") + } else if(dimension === 3) { + code.push( +"var s0=Math.abs(this.stride[0]),s1=Math.abs(this.stride[1]),s2=Math.abs(this.stride[2]);\ +if(s0>s1){\ +if(s1>s2){\ +return [2,1,0];\ +}else if(s0>s2){\ +return [1,2,0];\ +}else{\ +return [1,0,2];\ +}\ +}else if(s0>s2){\ +return [2,0,1];\ +}else if(s2>s1){\ +return [0,1,2];\ +}else{\ +return [0,2,1];\ +}}})") } } else { - f = [i]; - fragmentByStart[f.start = start] = fragmentByEnd[f.end = end] = f; - } - }); - - function ends(i) { - var arc = topology.arcs[i < 0 ? ~i : i], p0 = arc[0], p1; - if (topology.transform) p1 = [0, 0], arc.forEach(function(dp) { p1[0] += dp[0], p1[1] += dp[1]; }); - else p1 = arc[arc.length - 1]; - return i < 0 ? [p1, p0] : [p0, p1]; - } - - function flush(fragmentByEnd, fragmentByStart) { - for (var k in fragmentByEnd) { - var f = fragmentByEnd[k]; - delete fragmentByStart[f.start]; - delete f.start; - delete f.end; - f.forEach(function(i) { stitchedArcs[i < 0 ? ~i : i] = 1; }); - fragments.push(f); + code.push("ORDER})") } } - flush(fragmentByEnd, fragmentByStart); - flush(fragmentByStart, fragmentByEnd); - arcs.forEach(function(i) { if (!stitchedArcs[i < 0 ? ~i : i]) fragments.push([i]); }); + //view.set(i0, ..., v): + code.push( +"proto.set=function "+className+"_set("+args.join(",")+",v){") + if(useGetters) { + code.push("return this.data.set("+index_str+",v)}") + } else { + code.push("return this.data["+index_str+"]=v}") + } - return fragments; -}; + //view.get(i0, ...): + code.push("proto.get=function "+className+"_get("+args.join(",")+"){") + if(useGetters) { + code.push("return this.data.get("+index_str+")}") + } else { + code.push("return this.data["+index_str+"]}") + } -var mesh = function(topology) { - return object(topology, meshArcs.apply(this, arguments)); -}; + //view.index: + code.push( + "proto.index=function "+className+"_index(", args.join(), "){return "+index_str+"}") -function meshArcs(topology, object$$1, filter) { - var arcs, i, n; - if (arguments.length > 1) arcs = extractArcs(topology, object$$1, filter); - else for (i = 0, arcs = new Array(n = topology.arcs.length); i < n; ++i) arcs[i] = i; - return {type: "MultiLineString", arcs: stitch(topology, arcs)}; + //view.hi(): + code.push("proto.hi=function "+className+"_hi("+args.join(",")+"){return new "+className+"(this.data,"+ + indices.map(function(i) { + return ["(typeof i",i,"!=='number'||i",i,"<0)?this.shape[", i, "]:i", i,"|0"].join("") + }).join(",")+","+ + indices.map(function(i) { + return "this.stride["+i + "]" + }).join(",")+",this.offset)}") + + //view.lo(): + var a_vars = indices.map(function(i) { return "a"+i+"=this.shape["+i+"]" }) + var c_vars = indices.map(function(i) { return "c"+i+"=this.stride["+i+"]" }) + code.push("proto.lo=function "+className+"_lo("+args.join(",")+"){var b=this.offset,d=0,"+a_vars.join(",")+","+c_vars.join(",")) + for(var i=0; i=0){\ +d=i"+i+"|0;\ +b+=c"+i+"*d;\ +a"+i+"-=d}") + } + code.push("return new "+className+"(this.data,"+ + indices.map(function(i) { + return "a"+i + }).join(",")+","+ + indices.map(function(i) { + return "c"+i + }).join(",")+",b)}") + + //view.step(): + code.push("proto.step=function "+className+"_step("+args.join(",")+"){var "+ + indices.map(function(i) { + return "a"+i+"=this.shape["+i+"]" + }).join(",")+","+ + indices.map(function(i) { + return "b"+i+"=this.stride["+i+"]" + }).join(",")+",c=this.offset,d=0,ceil=Math.ceil") + for(var i=0; i=0){c=(c+this.stride["+i+"]*i"+i+")|0}else{a.push(this.shape["+i+"]);b.push(this.stride["+i+"])}") + } + code.push("var ctor=CTOR_LIST[a.length+1];return ctor(this.data,a,b,c)}") + + //Add return statement + code.push("return function construct_"+className+"(data,shape,stride,offset){return new "+className+"(data,"+ + indices.map(function(i) { + return "shape["+i+"]" + }).join(",")+","+ + indices.map(function(i) { + return "stride["+i+"]" + }).join(",")+",offset)}") + + //Compile procedure + var procedure = new Function("CTOR_LIST", "ORDER", code.join("\n")) + return procedure(CACHED_CONSTRUCTORS[dtype], order) } -function extractArcs(topology, object$$1, filter) { - var arcs = [], - geomsByArc = [], - geom; - - function extract0(i) { - var j = i < 0 ? ~i : i; - (geomsByArc[j] || (geomsByArc[j] = [])).push({i: i, g: geom}); +function arrayDType(data) { + if(isBuffer(data)) { + return "buffer" } - - function extract1(arcs) { - arcs.forEach(extract0); - } - - function extract2(arcs) { - arcs.forEach(extract1); - } - - function extract3(arcs) { - arcs.forEach(extract2); - } - - function geometry(o) { - switch (geom = o, o.type) { - case "GeometryCollection": o.geometries.forEach(geometry); break; - case "LineString": extract1(o.arcs); break; - case "MultiLineString": case "Polygon": extract2(o.arcs); break; - case "MultiPolygon": extract3(o.arcs); break; + if(hasTypedArrays) { + switch(Object.prototype.toString.call(data)) { + case "[object Float64Array]": + return "float64" + case "[object Float32Array]": + return "float32" + case "[object Int8Array]": + return "int8" + case "[object Int16Array]": + return "int16" + case "[object Int32Array]": + return "int32" + case "[object Uint8Array]": + return "uint8" + case "[object Uint16Array]": + return "uint16" + case "[object Uint32Array]": + return "uint32" + case "[object Uint8ClampedArray]": + return "uint8_clamped" } } - - geometry(object$$1); - - geomsByArc.forEach(filter == null - ? function(geoms) { arcs.push(geoms[0].i); } - : function(geoms) { if (filter(geoms[0].g, geoms[geoms.length - 1].g)) arcs.push(geoms[0].i); }); - - return arcs; + if(Array.isArray(data)) { + return "array" + } + return "generic" } -function planarRingArea(ring) { - var i = -1, n = ring.length, a, b = ring[n - 1], area = 0; - while (++i < n) a = b, b = ring[i], area += a[0] * b[1] - a[1] * b[0]; - return Math.abs(area); // Note: doubled area! +var CACHED_CONSTRUCTORS = { + "float32":[], + "float64":[], + "int8":[], + "int16":[], + "int32":[], + "uint8":[], + "uint16":[], + "uint32":[], + "array":[], + "uint8_clamped":[], + "buffer":[], + "generic":[] } -var merge = function(topology) { - return object(topology, mergeArcs.apply(this, arguments)); -}; +;(function() { + for(var id in CACHED_CONSTRUCTORS) { + CACHED_CONSTRUCTORS[id].push(compileConstructor(id, -1)) + } +}); -function mergeArcs(topology, objects) { - var polygonsByArc = {}, - polygons = [], - groups = []; - - objects.forEach(geometry); - - function geometry(o) { - switch (o.type) { - case "GeometryCollection": o.geometries.forEach(geometry); break; - case "Polygon": extract(o.arcs); break; - case "MultiPolygon": o.arcs.forEach(extract); break; +function wrappedNDArrayCtor(data, shape, stride, offset) { + if(data === undefined) { + var ctor = CACHED_CONSTRUCTORS.array[0] + return ctor([]) + } else if(typeof data === "number") { + data = [data] + } + if(shape === undefined) { + shape = [ data.length ] + } + var d = shape.length + if(stride === undefined) { + stride = new Array(d) + for(var i=d-1, sz=1; i>=0; --i) { + stride[i] = sz + sz *= shape[i] } } - - function extract(polygon) { - polygon.forEach(function(ring) { - ring.forEach(function(arc) { - (polygonsByArc[arc = arc < 0 ? ~arc : arc] || (polygonsByArc[arc] = [])).push(polygon); - }); - }); - polygons.push(polygon); - } - - function area(ring) { - return planarRingArea(object(topology, {type: "Polygon", arcs: [ring]}).coordinates[0]); - } - - polygons.forEach(function(polygon) { - if (!polygon._) { - var group = [], - neighbors = [polygon]; - polygon._ = 1; - groups.push(group); - while (polygon = neighbors.pop()) { - group.push(polygon); - polygon.forEach(function(ring) { - ring.forEach(function(arc) { - polygonsByArc[arc < 0 ? ~arc : arc].forEach(function(polygon) { - if (!polygon._) { - polygon._ = 1; - neighbors.push(polygon); - } - }); - }); - }); + if(offset === undefined) { + offset = 0 + for(var i=0; i>>0 - // If more than one ring is returned, - // at most one of these rings can be the exterior; - // choose the one with the greatest absolute area. - if ((n = arcs.length) > 1) { - for (var i = 1, k = area(arcs[0]), ki, t; i < n; ++i) { - if ((ki = area(arcs[i])) > k) { - t = arcs[0], arcs[0] = arcs[i], arcs[i] = t, k = ki; - } +module.exports = nextafter + +function nextafter(x, y) { + if(isNaN(x) || isNaN(y)) { + return NaN + } + if(x === y) { + return x + } + if(x === 0) { + if(y < 0) { + return -SMALLEST_DENORM + } else { + return SMALLEST_DENORM + } + } + var hi = doubleBits.hi(x) + var lo = doubleBits.lo(x) + if((y > x) === (x > 0)) { + if(lo === UINT_MAX) { + hi += 1 + lo = 0 + } else { + lo += 1 + } + } else { + if(lo === 0) { + lo = UINT_MAX + hi -= 1 + } else { + lo -= 1 + } + } + return doubleBits.pack(lo, hi) +} +},{"double-bits":89}],445:[function(require,module,exports){ +var DEFAULT_NORMALS_EPSILON = 1e-6; +var DEFAULT_FACE_EPSILON = 1e-6; + +//Estimate the vertex normals of a mesh +exports.vertexNormals = function(faces, positions, specifiedEpsilon) { + + var N = positions.length; + var normals = new Array(N); + var epsilon = specifiedEpsilon === void(0) ? DEFAULT_NORMALS_EPSILON : specifiedEpsilon; + + //Initialize normal array + for(var i=0; i epsilon) { + var norm = normals[c]; + var w = 1.0 / Math.sqrt(m01 * m21); + for(var k=0; k<3; ++k) { + var u = (k+1)%3; + var v = (k+2)%3; + norm[k] += w * (d21[u] * d01[v] - d21[v] * d01[u]); } } - - return arcs; - }) - }; -} - -var bisect = function(a, x) { - var lo = 0, hi = a.length; - while (lo < hi) { - var mid = lo + hi >>> 1; - if (a[mid] < x) lo = mid + 1; - else hi = mid; - } - return lo; -}; - -var neighbors = function(objects) { - var indexesByArc = {}, // arc index -> array of object indexes - neighbors = objects.map(function() { return []; }); - - function line(arcs, i) { - arcs.forEach(function(a) { - if (a < 0) a = ~a; - var o = indexesByArc[a]; - if (o) o.push(i); - else indexesByArc[a] = [i]; - }); - } - - function polygon(arcs, i) { - arcs.forEach(function(arc) { line(arc, i); }); - } - - function geometry(o, i) { - if (o.type === "GeometryCollection") o.geometries.forEach(function(o) { geometry(o, i); }); - else if (o.type in geometryType) geometryType[o.type](o.arcs, i); - } - - var geometryType = { - LineString: line, - MultiLineString: polygon, - Polygon: polygon, - MultiPolygon: function(arcs, i) { arcs.forEach(function(arc) { polygon(arc, i); }); } - }; - - objects.forEach(geometry); - - for (var i in indexesByArc) { - for (var indexes = indexesByArc[i], m = indexes.length, j = 0; j < m; ++j) { - for (var k = j + 1; k < m; ++k) { - var ij = indexes[j], ik = indexes[k], n; - if ((n = neighbors[ij])[i = bisect(n, ik)] !== ik) n.splice(i, 0, ik); - if ((n = neighbors[ik])[i = bisect(n, ij)] !== ij) n.splice(i, 0, ij); - } } } - return neighbors; -}; - -var quantize = function(topology, n) { - if (!((n = Math.floor(n)) >= 2)) throw new Error("n must be ≥2"); - if (topology.transform) throw new Error("already quantized"); - var bb = bbox(topology), name, - dx = bb[0], kx = (bb[2] - dx) / (n - 1) || 1, - dy = bb[1], ky = (bb[3] - dy) / (n - 1) || 1; - - function quantizePoint(p) { - p[0] = Math.round((p[0] - dx) / kx); - p[1] = Math.round((p[1] - dy) / ky); - } - - function quantizeGeometry(o) { - switch (o.type) { - case "GeometryCollection": o.geometries.forEach(quantizeGeometry); break; - case "Point": quantizePoint(o.coordinates); break; - case "MultiPoint": o.coordinates.forEach(quantizePoint); break; + //Scale all normals to unit length + for(var i=0; i epsilon) { + var w = 1.0 / Math.sqrt(m); + for(var k=0; k<3; ++k) { + norm[k] *= w; + } + } else { + for(var k=0; k<3; ++k) { + norm[k] = 0.0; } } - - if (j < 2) { - pj = arc[j++]; - pj[0] = 0; - pj[1] = 0; - } - - arc.length = j; - }); - - for (name in topology.objects) { - quantizeGeometry(topology.objects[name]); } - topology.transform = { - scale: [kx, ky], - translate: [dx, dy] - }; - - return topology; -}; - -var untransform = function(topology) { - if ((transform = topology.transform) == null) return identity; - var transform, - x0, - y0, - kx = transform.scale[0], - ky = transform.scale[1], - dx = transform.translate[0], - dy = transform.translate[1]; - return function(point, i) { - if (!i) x0 = y0 = 0; - var x1 = Math.round((point[0] - dx) / kx), - y1 = Math.round((point[1] - dy) / ky); - point[0] = x1 - x0, x0 = x1; - point[1] = y1 - y0, y0 = y1; - return point; - }; -}; - -exports.bbox = bbox; -exports.feature = feature; -exports.mesh = mesh; -exports.meshArcs = meshArcs; -exports.merge = merge; -exports.mergeArcs = mergeArcs; -exports.neighbors = neighbors; -exports.quantize = quantize; -exports.transform = transform; -exports.untransform = untransform; - -Object.defineProperty(exports, '__esModule', { value: true }); - -}))); - -},{}],1131:[function(require,module,exports){ -var getContext = require('get-canvas-context') - -module.exports = function getWebGLContext (opt) { - return getContext('webgl', opt) + //Return the resulting set of patches + return normals; } -},{"get-canvas-context":1132}],1132:[function(require,module,exports){ -module.exports = getCanvasContext -function getCanvasContext (type, opts) { - if (typeof type !== 'string') { - throw new TypeError('must specify type string') - } +//Compute face normals of a mesh +exports.faceNormals = function(faces, positions, specifiedEpsilon) { - opts = opts || {} + var N = faces.length; + var normals = new Array(N); + var epsilon = specifiedEpsilon === void(0) ? DEFAULT_FACE_EPSILON : specifiedEpsilon; - if (typeof document === 'undefined' && !opts.canvas) { - return null // check for Node - } - - var canvas = opts.canvas || document.createElement('canvas') - if (typeof opts.width === 'number') { - canvas.width = opts.width - } - if (typeof opts.height === 'number') { - canvas.height = opts.height - } - - var attribs = opts - var gl - try { - var names = [ type ] - // prefix GL contexts - if (type.indexOf('webgl') === 0) { - names.push('experimental-' + type) + for(var i=0; itrue if has a year zero, false if not. - @memberof ChineseCalendar */ - hasYearZero: false, - /** The minimum month number. - This calendar uses month indices to account for intercalary months. - @memberof ChineseCalendar */ - minMonth: 0, - /** The first month in the year. - This calendar uses month indices to account for intercalary months. - @memberof ChineseCalendar */ - firstMonth: 0, - /** The minimum day number. - @memberof ChineseCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof ChineseCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Chinese', - epochs: ['BEC', 'EC'], - monthNumbers: function(date, padded) { - if (typeof date === 'string') { - var match = date.match(MONTH_NUMBER_REGEXP); - return (match) ? match[0] : ''; - } - - var year = this._validateYear(date); - var monthIndex = date.month(); - - var month = '' + this.toChineseMonth(year, monthIndex); - - if (padded && month.length < 2) { - month = "0" + month; - } - - if (this.isIntercalaryMonth(year, monthIndex)) { - month += 'i'; - } - - return month; - }, - monthNames: function(date) { - if (typeof date === 'string') { - var match = date.match(MONTH_NAME_REGEXP); - return (match) ? match[0] : ''; - } - - var year = this._validateYear(date); - var monthIndex = date.month(); - - var month = this.toChineseMonth(year, monthIndex); - - var monthName = ['一月','二月','三月','四月','五月','六月', - '七月','八月','九月','十月','十一月','十二月'][month - 1]; - - if (this.isIntercalaryMonth(year, monthIndex)) { - monthName = '闰' + monthName; - } - - return monthName; - }, - monthNamesShort: function(date) { - if (typeof date === 'string') { - var match = date.match(MONTH_SHORT_NAME_REGEXP); - return (match) ? match[0] : ''; - } - - var year = this._validateYear(date); - var monthIndex = date.month(); - - var month = this.toChineseMonth(year, monthIndex); - - var monthName = ['一','二','三','四','五','六', - '七','八','九','十','十一','十二'][month - 1]; - - if (this.isIntercalaryMonth(year, monthIndex)) { - monthName = '闰' + monthName; - } - - return monthName; - }, - parseMonth: function(year, monthString) { - year = this._validateYear(year); - var month = parseInt(monthString); - var isIntercalary; - - if (!isNaN(month)) { - var i = monthString[monthString.length - 1]; - isIntercalary = (i === 'i' || i === 'I'); - } else { - if (monthString[0] === '闰') { - isIntercalary = true; - monthString = monthString.substring(1); - } - if (monthString[monthString.length - 1] === '月') { - monthString = monthString.substring(0, monthString.length - 1); - } - month = 1 + - ['一','二','三','四','五','六', - '七','八','九','十','十一','十二'].indexOf(monthString); - } - - var monthIndex = this.toMonthIndex(year, month, isIntercalary); - return monthIndex; - }, - dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], - digits: null, - dateFormat: 'yyyy/mm/dd', - firstDay: 1, - isRTL: false - } - }, - - /** Check that a candidate date is from the same calendar and is valid. - @memberof BaseCalendar - @private - @param year {CDate|number} The date or the year to validate. - @param error {string} Error message if invalid. - @return {number} The year. - @throws Error if year out of range. */ - _validateYear: function(year, error) { - if (year.year) { - year = year.year(); - } - - if (typeof year !== 'number' || year < 1888 || year > 2111) { - throw error.replace(/\{0\}/, this.local.name); - } - - return year; - }, - - /** Retrieve the month index (i.e. accounting for intercalary months). - @memberof ChineseCalendar - @param year {number} The year. - @param month {number} The month (1 for first month). - @param [isIntercalary=false] {boolean} If month is intercalary. - @return {number} The month index (0 for first month). - @throws Error if an invalid month/year or a different calendar used. */ - toMonthIndex: function(year, month, isIntercalary) { - // compute intercalary month in the year (0 if none) - var intercalaryMonth = this.intercalaryMonth(year); - - // validate month - var invalidIntercalaryMonth = - (isIntercalary && month !== intercalaryMonth); - if (invalidIntercalaryMonth || month < 1 || month > 12) { - throw main.local.invalidMonth - .replace(/\{0\}/, this.local.name); - } - - // compute month index - var monthIndex; - - if (!intercalaryMonth) { - monthIndex = month - 1; - } else if(!isIntercalary && month <= intercalaryMonth) { - monthIndex = month - 1; - } else { - monthIndex = month; - } - - return monthIndex; - }, - - /** Retrieve the month (i.e. accounting for intercalary months). - @memberof ChineseCalendar - @param year {CDate|number} The date or the year to examine. - @param monthIndex {number} The month index (0 for first month). - @return {number} The month (1 for first month). - @throws Error if an invalid month/year or a different calendar used. */ - toChineseMonth: function(year, monthIndex) { - if (year.year) { - year = year.year(); - monthIndex = year.month(); - } - - // compute intercalary month in the year (0 if none) - var intercalaryMonth = this.intercalaryMonth(year); - - // validate month - var maxMonthIndex = (intercalaryMonth) ? 12 : 11; - if (monthIndex < 0 || monthIndex > maxMonthIndex) { - throw main.local.invalidMonth - .replace(/\{0\}/, this.local.name); - } - - // compute Chinese month - var month; - - if (!intercalaryMonth) { - month = monthIndex + 1; - } else if(monthIndex < intercalaryMonth) { - month = monthIndex + 1; - } else { - month = monthIndex; - } - - return month; - }, - - /** Determine the intercalary month of a year (if any). - @memberof ChineseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The intercalary month number, or 0 if none. - @throws Error if an invalid year or a different calendar used. */ - intercalaryMonth: function(year) { - year = this._validateYear(year); - - var monthDaysTable = LUNAR_MONTH_DAYS[year - LUNAR_MONTH_DAYS[0]]; - var intercalaryMonth = monthDaysTable >> 13; - - return intercalaryMonth; - }, - - /** Determine whether this date is an intercalary month. - @memberof ChineseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [monthIndex] {number} The month index to examine. - @return {boolean} true if this is an intercalary month, false if not. - @throws Error if an invalid year or a different calendar used. */ - isIntercalaryMonth: function(year, monthIndex) { - if (year.year) { - year = year.year(); - monthIndex = year.month(); - } - - var intercalaryMonth = this.intercalaryMonth(year); - - return !!intercalaryMonth && intercalaryMonth === monthIndex; - }, - - /** Determine whether this date is in a leap year. - @memberof ChineseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - return (this.intercalaryMonth(year) !== 0); - }, - - /** Determine the week of the year for a date - ISO 8601. - @memberof ChineseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [monthIndex] {number} The month index to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, monthIndex, day) { - // compute Chinese new year - var validatedYear = - this._validateYear(year, main.local.invalidyear); - var packedDate = - CHINESE_NEW_YEAR[validatedYear - CHINESE_NEW_YEAR[0]]; - - var y = (packedDate >> 9) & 0xFFF; - var m = (packedDate >> 5) & 0x0F; - var d = packedDate & 0x1F; - - // find first Thrusday of the year - var firstThursday; - firstThursday = gregorianCalendar.newDate(y, m, d); - firstThursday.add(4 - (firstThursday.dayOfWeek() || 7), 'd'); - - // compute days from first Thursday - var offset = - this.toJD(year, monthIndex, day) - firstThursday.toJD(); - return 1 + Math.floor(offset / 7); - }, - - /** Retrieve the number of months in a year. - @memberof ChineseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of months. - @throws Error if an invalid year or a different calendar used. */ - monthsInYear: function(year) { - return (this.leapYear(year)) ? 13 : 12; - }, - - /** Retrieve the number of days in a month. - @memberof ChineseCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [monthIndex] {number} The month index. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, monthIndex) { - if (year.year) { - monthIndex = year.month(); - year = year.year(); - } - - year = this._validateYear(year); - - var monthDaysTable = LUNAR_MONTH_DAYS[year - LUNAR_MONTH_DAYS[0]]; - - var intercalaryMonth = monthDaysTable >> 13; - var maxMonthIndex = (intercalaryMonth) ? 12 : 11; - if (monthIndex > maxMonthIndex) { - throw main.local.invalidMonth - .replace(/\{0\}/, this.local.name); - } - - var daysInMonth = (monthDaysTable & (1 << (12 - monthIndex))) ? - 30 : 29; - - return daysInMonth; - }, - - /** Determine whether this date is a week day. - @memberof ChineseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [monthIndex] {number} The month index to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, monthIndex, day) { - return (this.dayOfWeek(year, monthIndex, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof ChineseCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [monthIndex] {number} The month index to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, monthIndex, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = this._validateYear(date.year()); - monthIndex = date.month(); - day = date.day(); - - var isIntercalary = this.isIntercalaryMonth(year, monthIndex); - var month = this.toChineseMonth(year, monthIndex); - - var solar = toSolar(year, month, day, isIntercalary); - - return gregorianCalendar.toJD(solar.year, solar.month, solar.day); - }, - - /** Create a new date from a Julian date. - @memberof ChineseCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - var date = gregorianCalendar.fromJD(jd); - var lunar = toLunar(date.year(), date.month(), date.day()); - var monthIndex = this.toMonthIndex( - lunar.year, lunar.month, lunar.isIntercalary); - return this.newDate(lunar.year, monthIndex, lunar.day); - }, - - /** Create a new date from a string. - @memberof ChineseCalendar - @param dateString {string} String representing a Chinese date - @return {CDate} The new date. - @throws Error if an invalid date. */ - fromString: function(dateString) { - var match = dateString.match(DATE_REGEXP); - - var year = this._validateYear(+match[1]); - - var month = +match[2]; - var isIntercalary = !!match[3]; - var monthIndex = this.toMonthIndex(year, month, isIntercalary); - - var day = +match[4]; - - return this.newDate(year, monthIndex, day); - }, - - /** Add period(s) to a date. - Cater for no year zero. - @memberof ChineseCalendar - @param date {CDate} The starting date. - @param offset {number} The number of periods to adjust by. - @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. - @return {CDate} The updated date. - @throws Error if a different calendar used. */ - add: function(date, offset, period) { - var year = date.year(); - var monthIndex = date.month(); - var isIntercalary = this.isIntercalaryMonth(year, monthIndex); - var month = this.toChineseMonth(year, monthIndex); - - var cdate = Object.getPrototypeOf(ChineseCalendar.prototype) - .add.call(this, date, offset, period); - - if (period === 'y') { - // Resync month - var resultYear = cdate.year(); - var resultMonthIndex = cdate.month(); - - // Using the fact the month index of an intercalary month - // equals its month number: - var resultCanBeIntercalaryMonth = - this.isIntercalaryMonth(resultYear, month); - - var correctedMonthIndex = - (isIntercalary && resultCanBeIntercalaryMonth) ? - this.toMonthIndex(resultYear, month, true) : - this.toMonthIndex(resultYear, month, false); - - if (correctedMonthIndex !== resultMonthIndex) { - cdate.month(correctedMonthIndex); - } - } - - return cdate; - }, -}); - -// Used by ChineseCalendar.prototype.fromString -var DATE_REGEXP = /^\s*(-?\d\d\d\d|\d\d)[-/](\d?\d)([iI]?)[-/](\d?\d)/m; -var MONTH_NUMBER_REGEXP = /^\d?\d[iI]?/m; -var MONTH_NAME_REGEXP = /^闰?十?[一二三四五六七八九]?月/m; -var MONTH_SHORT_NAME_REGEXP = /^闰?十?[一二三四五六七八九]?/m; - -// Chinese calendar implementation -main.calendars.chinese = ChineseCalendar; - -// Chinese calendar tables from year 1888 to 2111 -// -// Source: -// https://github.com/isee15/Lunar-Solar-Calendar-Converter.git - -// Table of intercalary months and days per month from year 1888 to 2111 -// -// bit (12 - i): days in the i^th month -// (= 0 if i^th lunar month has 29 days) -// (= 1 if i^th lunar month has 30 days) -// (first month in lunar year is i = 0) -// bits (13,14,15,16): intercalary month -// (= 0 if lunar year has no intercalary month) -var LUNAR_MONTH_DAYS = [1887, 0x1694, 0x16aa, 0x4ad5, - 0xab6, 0xc4b7, 0x4ae, 0xa56, 0xb52a, 0x1d2a, 0xd54, 0x75aa, 0x156a, - 0x1096d, 0x95c, 0x14ae, 0xaa4d, 0x1a4c, 0x1b2a, 0x8d55, 0xad4, - 0x135a, 0x495d, 0x95c, 0xd49b, 0x149a, 0x1a4a, 0xbaa5, 0x16a8, - 0x1ad4, 0x52da, 0x12b6, 0xe937, 0x92e, 0x1496, 0xb64b, 0xd4a, - 0xda8, 0x95b5, 0x56c, 0x12ae, 0x492f, 0x92e, 0xcc96, 0x1a94, - 0x1d4a, 0xada9, 0xb5a, 0x56c, 0x726e, 0x125c, 0xf92d, 0x192a, - 0x1a94, 0xdb4a, 0x16aa, 0xad4, 0x955b, 0x4ba, 0x125a, 0x592b, - 0x152a, 0xf695, 0xd94, 0x16aa, 0xaab5, 0x9b4, 0x14b6, 0x6a57, - 0xa56, 0x1152a, 0x1d2a, 0xd54, 0xd5aa, 0x156a, 0x96c, 0x94ae, - 0x14ae, 0xa4c, 0x7d26, 0x1b2a, 0xeb55, 0xad4, 0x12da, 0xa95d, - 0x95a, 0x149a, 0x9a4d, 0x1a4a, 0x11aa5, 0x16a8, 0x16d4, 0xd2da, - 0x12b6, 0x936, 0x9497, 0x1496, 0x1564b, 0xd4a, 0xda8, 0xd5b4, - 0x156c, 0x12ae, 0xa92f, 0x92e, 0xc96, 0x6d4a, 0x1d4a, 0x10d65, - 0xb58, 0x156c, 0xb26d, 0x125c, 0x192c, 0x9a95, 0x1a94, 0x1b4a, - 0x4b55, 0xad4, 0xf55b, 0x4ba, 0x125a, 0xb92b, 0x152a, 0x1694, - 0x96aa, 0x15aa, 0x12ab5, 0x974, 0x14b6, 0xca57, 0xa56, 0x1526, - 0x8e95, 0xd54, 0x15aa, 0x49b5, 0x96c, 0xd4ae, 0x149c, 0x1a4c, - 0xbd26, 0x1aa6, 0xb54, 0x6d6a, 0x12da, 0x1695d, 0x95a, 0x149a, - 0xda4b, 0x1a4a, 0x1aa4, 0xbb54, 0x16b4, 0xada, 0x495b, 0x936, - 0xf497, 0x1496, 0x154a, 0xb6a5, 0xda4, 0x15b4, 0x6ab6, 0x126e, - 0x1092f, 0x92e, 0xc96, 0xcd4a, 0x1d4a, 0xd64, 0x956c, 0x155c, - 0x125c, 0x792e, 0x192c, 0xfa95, 0x1a94, 0x1b4a, 0xab55, 0xad4, - 0x14da, 0x8a5d, 0xa5a, 0x1152b, 0x152a, 0x1694, 0xd6aa, 0x15aa, - 0xab4, 0x94ba, 0x14b6, 0xa56, 0x7527, 0xd26, 0xee53, 0xd54, 0x15aa, - 0xa9b5, 0x96c, 0x14ae, 0x8a4e, 0x1a4c, 0x11d26, 0x1aa4, 0x1b54, - 0xcd6a, 0xada, 0x95c, 0x949d, 0x149a, 0x1a2a, 0x5b25, 0x1aa4, - 0xfb52, 0x16b4, 0xaba, 0xa95b, 0x936, 0x1496, 0x9a4b, 0x154a, - 0x136a5, 0xda4, 0x15ac]; - -// Table of Chinese New Years from year 1888 to 2111 -// -// bits (0 to 4): solar day -// bits (5 to 8): solar month -// bits (9 to 20): solar year -var CHINESE_NEW_YEAR = [1887, 0xec04c, 0xec23f, 0xec435, 0xec649, - 0xec83e, 0xeca51, 0xecc46, 0xece3a, 0xed04d, 0xed242, 0xed436, - 0xed64a, 0xed83f, 0xeda53, 0xedc48, 0xede3d, 0xee050, 0xee244, - 0xee439, 0xee64d, 0xee842, 0xeea36, 0xeec4a, 0xeee3e, 0xef052, - 0xef246, 0xef43a, 0xef64e, 0xef843, 0xefa37, 0xefc4b, 0xefe41, - 0xf0054, 0xf0248, 0xf043c, 0xf0650, 0xf0845, 0xf0a38, 0xf0c4d, - 0xf0e42, 0xf1037, 0xf124a, 0xf143e, 0xf1651, 0xf1846, 0xf1a3a, - 0xf1c4e, 0xf1e44, 0xf2038, 0xf224b, 0xf243f, 0xf2653, 0xf2848, - 0xf2a3b, 0xf2c4f, 0xf2e45, 0xf3039, 0xf324d, 0xf3442, 0xf3636, - 0xf384a, 0xf3a3d, 0xf3c51, 0xf3e46, 0xf403b, 0xf424e, 0xf4443, - 0xf4638, 0xf484c, 0xf4a3f, 0xf4c52, 0xf4e48, 0xf503c, 0xf524f, - 0xf5445, 0xf5639, 0xf584d, 0xf5a42, 0xf5c35, 0xf5e49, 0xf603e, - 0xf6251, 0xf6446, 0xf663b, 0xf684f, 0xf6a43, 0xf6c37, 0xf6e4b, - 0xf703f, 0xf7252, 0xf7447, 0xf763c, 0xf7850, 0xf7a45, 0xf7c39, - 0xf7e4d, 0xf8042, 0xf8254, 0xf8449, 0xf863d, 0xf8851, 0xf8a46, - 0xf8c3b, 0xf8e4f, 0xf9044, 0xf9237, 0xf944a, 0xf963f, 0xf9853, - 0xf9a47, 0xf9c3c, 0xf9e50, 0xfa045, 0xfa238, 0xfa44c, 0xfa641, - 0xfa836, 0xfaa49, 0xfac3d, 0xfae52, 0xfb047, 0xfb23a, 0xfb44e, - 0xfb643, 0xfb837, 0xfba4a, 0xfbc3f, 0xfbe53, 0xfc048, 0xfc23c, - 0xfc450, 0xfc645, 0xfc839, 0xfca4c, 0xfcc41, 0xfce36, 0xfd04a, - 0xfd23d, 0xfd451, 0xfd646, 0xfd83a, 0xfda4d, 0xfdc43, 0xfde37, - 0xfe04b, 0xfe23f, 0xfe453, 0xfe648, 0xfe83c, 0xfea4f, 0xfec44, - 0xfee38, 0xff04c, 0xff241, 0xff436, 0xff64a, 0xff83e, 0xffa51, - 0xffc46, 0xffe3a, 0x10004e, 0x100242, 0x100437, 0x10064b, 0x100841, - 0x100a53, 0x100c48, 0x100e3c, 0x10104f, 0x101244, 0x101438, - 0x10164c, 0x101842, 0x101a35, 0x101c49, 0x101e3d, 0x102051, - 0x102245, 0x10243a, 0x10264e, 0x102843, 0x102a37, 0x102c4b, - 0x102e3f, 0x103053, 0x103247, 0x10343b, 0x10364f, 0x103845, - 0x103a38, 0x103c4c, 0x103e42, 0x104036, 0x104249, 0x10443d, - 0x104651, 0x104846, 0x104a3a, 0x104c4e, 0x104e43, 0x105038, - 0x10524a, 0x10543e, 0x105652, 0x105847, 0x105a3b, 0x105c4f, - 0x105e45, 0x106039, 0x10624c, 0x106441, 0x106635, 0x106849, - 0x106a3d, 0x106c51, 0x106e47, 0x10703c, 0x10724f, 0x107444, - 0x107638, 0x10784c, 0x107a3f, 0x107c53, 0x107e48]; - -function toLunar(yearOrDate, monthOrResult, day, result) { - var solarDate; - var lunarDate; - - if(typeof yearOrDate === 'object') { - solarDate = yearOrDate; - lunarDate = monthOrResult || {}; + var n = new Array(3); + var l = 0.0; + for(var j=0; j<3; ++j) { + var u = (j+1)%3; + var v = (j+2)%3; + n[j] = d01[u] * d21[v] - d01[v] * d21[u]; + l += n[j] * n[j]; + } + if(l > epsilon) { + l = 1.0 / Math.sqrt(l); } else { - var isValidYear = (typeof yearOrDate === 'number') && - (yearOrDate >= 1888) && (yearOrDate <= 2111); - if(!isValidYear) - throw new Error("Solar year outside range 1888-2111"); - - var isValidMonth = (typeof monthOrResult === 'number') && - (monthOrResult >= 1) && (monthOrResult <= 12); - if(!isValidMonth) - throw new Error("Solar month outside range 1 - 12"); - - var isValidDay = (typeof day === 'number') && (day >= 1) && (day <= 31); - if(!isValidDay) - throw new Error("Solar day outside range 1 - 31"); - - solarDate = { - year: yearOrDate, - month: monthOrResult, - day: day, - }; - lunarDate = result || {}; + l = 0.0; } - - // Compute Chinese new year and lunar year - var chineseNewYearPackedDate = - CHINESE_NEW_YEAR[solarDate.year - CHINESE_NEW_YEAR[0]]; - - var packedDate = (solarDate.year << 9) | (solarDate.month << 5) - | solarDate.day; - - lunarDate.year = (packedDate >= chineseNewYearPackedDate) ? - solarDate.year : - solarDate.year - 1; - - chineseNewYearPackedDate = - CHINESE_NEW_YEAR[lunarDate.year - CHINESE_NEW_YEAR[0]]; - - var y = (chineseNewYearPackedDate >> 9) & 0xFFF; - var m = (chineseNewYearPackedDate >> 5) & 0x0F; - var d = chineseNewYearPackedDate & 0x1F; - - // Compute days from new year - var daysFromNewYear; - - var chineseNewYearJSDate = new Date(y, m -1, d); - var jsDate = new Date(solarDate.year, solarDate.month - 1, solarDate.day); - - daysFromNewYear = Math.round( - (jsDate - chineseNewYearJSDate) / (24 * 3600 * 1000)); - - // Compute lunar month and day - var monthDaysTable = LUNAR_MONTH_DAYS[lunarDate.year - LUNAR_MONTH_DAYS[0]]; - - var i; - for(i = 0; i < 13; i++) { - var daysInMonth = (monthDaysTable & (1 << (12 - i))) ? 30 : 29; - - if (daysFromNewYear < daysInMonth) { - break; - } - - daysFromNewYear -= daysInMonth; + for(var j=0; j<3; ++j) { + n[j] *= l; } - - var intercalaryMonth = monthDaysTable >> 13; - if (!intercalaryMonth || i < intercalaryMonth) { - lunarDate.isIntercalary = false; - lunarDate.month = 1 + i; - } else if (i === intercalaryMonth) { - lunarDate.isIntercalary = true; - lunarDate.month = i; - } else { - lunarDate.isIntercalary = false; - lunarDate.month = i; - } - - lunarDate.day = 1 + daysFromNewYear; - - return lunarDate; -} - -function toSolar(yearOrDate, monthOrResult, day, isIntercalaryOrResult, result) { - var solarDate; - var lunarDate; - - if(typeof yearOrDate === 'object') { - lunarDate = yearOrDate; - solarDate = monthOrResult || {}; - - } else { - var isValidYear = (typeof yearOrDate === 'number') && - (yearOrDate >= 1888) && (yearOrDate <= 2111); - if(!isValidYear) - throw new Error("Lunar year outside range 1888-2111"); - - var isValidMonth = (typeof monthOrResult === 'number') && - (monthOrResult >= 1) && (monthOrResult <= 12); - if(!isValidMonth) - throw new Error("Lunar month outside range 1 - 12"); - - var isValidDay = (typeof day === 'number') && (day >= 1) && (day <= 30); - if(!isValidDay) - throw new Error("Lunar day outside range 1 - 30"); - - var isIntercalary; - if(typeof isIntercalaryOrResult === 'object') { - isIntercalary = false; - solarDate = isIntercalaryOrResult; - } else { - isIntercalary = !!isIntercalaryOrResult; - solarDate = result || {}; - } - - lunarDate = { - year: yearOrDate, - month: monthOrResult, - day: day, - isIntercalary: isIntercalary, - }; - } - - // Compute days from new year - var daysFromNewYear; - - daysFromNewYear = lunarDate.day - 1; - - var monthDaysTable = LUNAR_MONTH_DAYS[lunarDate.year - LUNAR_MONTH_DAYS[0]]; - var intercalaryMonth = monthDaysTable >> 13; - - var monthsFromNewYear; - if (!intercalaryMonth) { - monthsFromNewYear = lunarDate.month - 1; - } else if (lunarDate.month > intercalaryMonth) { - monthsFromNewYear = lunarDate.month; - } else if (lunarDate.isIntercalary) { - monthsFromNewYear = lunarDate.month; - } else { - monthsFromNewYear = lunarDate.month - 1; - } - - for(var i = 0; i < monthsFromNewYear; i++) { - var daysInMonth = (monthDaysTable & (1 << (12 - i))) ? 30 : 29; - daysFromNewYear += daysInMonth; - } - - // Compute Chinese new year - var packedDate = CHINESE_NEW_YEAR[lunarDate.year - CHINESE_NEW_YEAR[0]]; - - var y = (packedDate >> 9) & 0xFFF; - var m = (packedDate >> 5) & 0x0F; - var d = packedDate & 0x1F; - - // Compute solar date - var jsDate = new Date(y, m - 1, d + daysFromNewYear); - - solarDate.year = jsDate.getFullYear(); - solarDate.month = 1 + jsDate.getMonth(); - solarDate.day = jsDate.getDate(); - - return solarDate; + normals[i] = n; + } + return normals; } -},{"../main":1147,"object-assign":1149}],1134:[function(require,module,exports){ + +},{}],446:[function(require,module,exports){ /* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ +object-assign +(c) Sindre Sorhus +@license MIT +*/ -/* http://keith-wood.name/calendars.html - Coptic calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Coptic calendar. - See http://en.wikipedia.org/wiki/Coptic_calendar. - See also Calendrical Calculations: The Millennium Edition - (http://emr.cs.iit.edu/home/reingold/calendar-book/index.shtml). - @class CopticCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function CopticCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -CopticCalendar.prototype = new main.baseCalendar; - -assign(CopticCalendar.prototype, { - /** The calendar name. - @memberof CopticCalendar */ - name: 'Coptic', - /** Julian date of start of Coptic epoch: 29 August 284 CE (Gregorian). - @memberof CopticCalendar */ - jdEpoch: 1825029.5, - /** Days per month in a common year. - @memberof CopticCalendar */ - daysPerMonth: [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 5], - /** true if has a year zero, false if not. - @memberof CopticCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof CopticCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof CopticCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof CopticCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof CopticCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Coptic', - epochs: ['BAM', 'AM'], - monthNames: ['Thout', 'Paopi', 'Hathor', 'Koiak', 'Tobi', 'Meshir', - 'Paremhat', 'Paremoude', 'Pashons', 'Paoni', 'Epip', 'Mesori', 'Pi Kogi Enavot'], - monthNamesShort: ['Tho', 'Pao', 'Hath', 'Koi', 'Tob', 'Mesh', - 'Pat', 'Pad', 'Pash', 'Pao', 'Epi', 'Meso', 'PiK'], - dayNames: ['Tkyriaka', 'Pesnau', 'Pshoment', 'Peftoou', 'Ptiou', 'Psoou', 'Psabbaton'], - dayNamesShort: ['Tky', 'Pes', 'Psh', 'Pef', 'Pti', 'Pso', 'Psa'], - dayNamesMin: ['Tk', 'Pes', 'Psh', 'Pef', 'Pt', 'Pso', 'Psa'], - digits: null, - dateFormat: 'dd/mm/yyyy', - firstDay: 0, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof CopticCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - var year = date.year() + (date.year() < 0 ? 1 : 0); // No year zero - return year % 4 === 3 || year % 4 === -1; - }, - - /** Retrieve the number of months in a year. - @memberof CopticCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of months. - @throws Error if an invalid year or a different calendar used. */ - monthsInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, - main.local.invalidYear || main.regionalOptions[''].invalidYear); - return 13; - }, - - /** Determine the week of the year for a date. - @memberof CopticCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number) the month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Sunday of this week starting on Sunday - var checkDate = this.newDate(year, month, day); - checkDate.add(-checkDate.dayOfWeek(), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a month. - @memberof CopticCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 13 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof CopticCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param month {number} The month to examine. - @param day {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return (this.dayOfWeek(year, month, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof CopticCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number) the month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = date.year(); - if (year < 0) { year++; } // No year zero - return date.day() + (date.month() - 1) * 30 + - (year - 1) * 365 + Math.floor(year / 4) + this.jdEpoch - 1; - }, - - /** Create a new date from a Julian date. - @memberof CopticCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - var c = Math.floor(jd) + 0.5 - this.jdEpoch; - var year = Math.floor((c - Math.floor((c + 366) / 1461)) / 365) + 1; - if (year <= 0) { year--; } // No year zero - c = Math.floor(jd) + 0.5 - this.newDate(year, 1, 1).toJD(); - var month = Math.floor(c / 30) + 1; - var day = c - (month - 1) * 30 + 1; - return this.newDate(year, month, day); - } -}); - -// Coptic calendar implementation -main.calendars.coptic = CopticCalendar; - - -},{"../main":1147,"object-assign":1149}],1135:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Discworld calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) January 2016. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Discworld calendar - Unseen University version. - See also http://wiki.lspace.org/mediawiki/Discworld_calendar - and http://discworld.wikia.com/wiki/Discworld_calendar. - @class DiscworldCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function DiscworldCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -DiscworldCalendar.prototype = new main.baseCalendar; - -assign(DiscworldCalendar.prototype, { - /** The calendar name. - @memberof DiscworldCalendar */ - name: 'Discworld', - /** Julian date of start of Discworld epoch: 1 January 0001 CE. - @memberof DiscworldCalendar */ - jdEpoch: 1721425.5, - /** Days per month in a common year. - @memberof DiscworldCalendar */ - daysPerMonth: [16, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32], - /** true if has a year zero, false if not. - @memberof DiscworldCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof DiscworldCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof DiscworldCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof DiscworldCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof DiscworldCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Discworld', - epochs: ['BUC', 'UC'], - monthNames: ['Ick', 'Offle', 'February', 'March', 'April', 'May', 'June', - 'Grune', 'August', 'Spune', 'Sektober', 'Ember', 'December'], - monthNamesShort: ['Ick', 'Off', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Gru', 'Aug', 'Spu', 'Sek', 'Emb', 'Dec'], - dayNames: ['Sunday', 'Octeday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - dayNamesShort: ['Sun', 'Oct', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - dayNamesMin: ['Su', 'Oc', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], - digits: null, - dateFormat: 'yyyy/mm/dd', - firstDay: 2, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return false; - }, - - /** Retrieve the number of months in a year. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of months. - @throws Error if an invalid year or a different calendar used. */ - monthsInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return 13; - }, - - /** Retrieve the number of days in a year. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of days. - @throws Error if an invalid year or a different calendar used. */ - daysInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return 400; - }, - - /** Determine the week of the year for a date. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Sunday of this week starting on Sunday - var checkDate = this.newDate(year, month, day); - checkDate.add(-checkDate.dayOfWeek(), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 8) + 1; - }, - - /** Retrieve the number of days in a month. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1]; - }, - - /** Retrieve the number of days in a week. - @memberof DiscworldCalendar - @return {number} The number of days. */ - daysInWeek: function() { - return 8; - }, - - /** Retrieve the day of the week for a date. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The day of the week: 0 to number of days - 1. - @throws Error if an invalid date or a different calendar used. */ - dayOfWeek: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - return (date.day() + 1) % 8; - }, - - /** Determine whether this date is a week day. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - var dow = this.dayOfWeek(year, month, day); - return (dow >= 2 && dow <= 6); - }, - - /** Retrieve additional information about a date. - @memberof DiscworldCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {object} Additional information - contents depends on calendar. - @throws Error if an invalid date or a different calendar used. */ - extraInfo: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - return {century: centuries[Math.floor((date.year() - 1) / 100) + 1] || ''}; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof DiscworldCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = date.year() + (date.year() < 0 ? 1 : 0); - month = date.month(); - day = date.day(); - return day + (month > 1 ? 16 : 0) + (month > 2 ? (month - 2) * 32 : 0) + - (year - 1) * 400 + this.jdEpoch - 1; - }, - - /** Create a new date from a Julian date. - @memberof DiscworldCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - jd = Math.floor(jd + 0.5) - Math.floor(this.jdEpoch) - 1; - var year = Math.floor(jd / 400) + 1; - jd -= (year - 1) * 400; - jd += (jd > 15 ? 16 : 0); - var month = Math.floor(jd / 32) + 1; - var day = jd - (month - 1) * 32 + 1; - return this.newDate(year <= 0 ? year - 1 : year, month, day); - } -}); - -// Names of the centuries -var centuries = { - 20: 'Fruitbat', - 21: 'Anchovy' -}; - -// Discworld calendar implementation -main.calendars.discworld = DiscworldCalendar; - - -},{"../main":1147,"object-assign":1149}],1136:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Ethiopian calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Ethiopian calendar. - See http://en.wikipedia.org/wiki/Ethiopian_calendar. - See also Calendrical Calculations: The Millennium Edition - (http://emr.cs.iit.edu/home/reingold/calendar-book/index.shtml). - @class EthiopianCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function EthiopianCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -EthiopianCalendar.prototype = new main.baseCalendar; - -assign(EthiopianCalendar.prototype, { - /** The calendar name. - @memberof EthiopianCalendar */ - name: 'Ethiopian', - /** Julian date of start of Ethiopian epoch: 27 August 8 CE (Gregorian). - @memberof EthiopianCalendar */ - jdEpoch: 1724220.5, - /** Days per month in a common year. - @memberof EthiopianCalendar */ - daysPerMonth: [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 5], - /** true if has a year zero, false if not. - @memberof EthiopianCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof EthiopianCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof EthiopianCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof EthiopianCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof EthiopianCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Ethiopian', - epochs: ['BEE', 'EE'], - monthNames: ['Meskerem', 'Tikemet', 'Hidar', 'Tahesas', 'Tir', 'Yekatit', - 'Megabit', 'Miazia', 'Genbot', 'Sene', 'Hamle', 'Nehase', 'Pagume'], - monthNamesShort: ['Mes', 'Tik', 'Hid', 'Tah', 'Tir', 'Yek', - 'Meg', 'Mia', 'Gen', 'Sen', 'Ham', 'Neh', 'Pag'], - dayNames: ['Ehud', 'Segno', 'Maksegno', 'Irob', 'Hamus', 'Arb', 'Kidame'], - dayNamesShort: ['Ehu', 'Seg', 'Mak', 'Iro', 'Ham', 'Arb', 'Kid'], - dayNamesMin: ['Eh', 'Se', 'Ma', 'Ir', 'Ha', 'Ar', 'Ki'], - digits: null, - dateFormat: 'dd/mm/yyyy', - firstDay: 0, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof EthiopianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - var year = date.year() + (date.year() < 0 ? 1 : 0); // No year zero - return year % 4 === 3 || year % 4 === -1; - }, - - /** Retrieve the number of months in a year. - @memberof EthiopianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of months. - @throws Error if an invalid year or a different calendar used. */ - monthsInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, - main.local.invalidYear || main.regionalOptions[''].invalidYear); - return 13; - }, - - /** Determine the week of the year for a date. - @memberof EthiopianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Sunday of this week starting on Sunday - var checkDate = this.newDate(year, month, day); - checkDate.add(-checkDate.dayOfWeek(), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a month. - @memberof EthiopianCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 13 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof EthiopianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return (this.dayOfWeek(year, month, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof EthiopianCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = date.year(); - if (year < 0) { year++; } // No year zero - return date.day() + (date.month() - 1) * 30 + - (year - 1) * 365 + Math.floor(year / 4) + this.jdEpoch - 1; - }, - - /** Create a new date from a Julian date. - @memberof EthiopianCalendar - @param jd {number} the Julian date to convert. - @return {CDate} the equivalent date. */ - fromJD: function(jd) { - var c = Math.floor(jd) + 0.5 - this.jdEpoch; - var year = Math.floor((c - Math.floor((c + 366) / 1461)) / 365) + 1; - if (year <= 0) { year--; } // No year zero - c = Math.floor(jd) + 0.5 - this.newDate(year, 1, 1).toJD(); - var month = Math.floor(c / 30) + 1; - var day = c - (month - 1) * 30 + 1; - return this.newDate(year, month, day); - } -}); - -// Ethiopian calendar implementation -main.calendars.ethiopian = EthiopianCalendar; - - -},{"../main":1147,"object-assign":1149}],1137:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Hebrew calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Hebrew civil calendar. - Based on code from http://www.fourmilab.ch/documents/calendar/. - See also http://en.wikipedia.org/wiki/Hebrew_calendar. - @class HebrewCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function HebrewCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -HebrewCalendar.prototype = new main.baseCalendar; - -assign(HebrewCalendar.prototype, { - /** The calendar name. - @memberof HebrewCalendar */ - name: 'Hebrew', - /** Julian date of start of Hebrew epoch: 7 October 3761 BCE. - @memberof HebrewCalendar */ - jdEpoch: 347995.5, - /** Days per month in a common year. - @memberof HebrewCalendar */ - daysPerMonth: [30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 29], - /** true if has a year zero, false if not. - @memberof HebrewCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof HebrewCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof HebrewCalendar */ - firstMonth: 7, - /** The minimum day number. - @memberof HebrewCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof HebrewCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Hebrew', - epochs: ['BAM', 'AM'], - monthNames: ['Nisan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul', - 'Tishrei', 'Cheshvan', 'Kislev', 'Tevet', 'Shevat', 'Adar', 'Adar II'], - monthNamesShort: ['Nis', 'Iya', 'Siv', 'Tam', 'Av', 'Elu', 'Tis', 'Che', 'Kis', 'Tev', 'She', 'Ada', 'Ad2'], - dayNames: ['Yom Rishon', 'Yom Sheni', 'Yom Shlishi', 'Yom Revi\'i', 'Yom Chamishi', 'Yom Shishi', 'Yom Shabbat'], - dayNamesShort: ['Ris', 'She', 'Shl', 'Rev', 'Cha', 'Shi', 'Sha'], - dayNamesMin: ['Ri','She','Shl','Re','Ch','Shi','Sha'], - digits: null, - dateFormat: 'dd/mm/yyyy', - firstDay: 0, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof HebrewCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return this._leapYear(date.year()); - }, - - /** Determine whether this date is in a leap year. - @memberof HebrewCalendar - @private - @param year {number} The year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - _leapYear: function(year) { - year = (year < 0 ? year + 1 : year); - return mod(year * 7 + 1, 19) < 7; - }, - - /** Retrieve the number of months in a year. - @memberof HebrewCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of months. - @throws Error if an invalid year or a different calendar used. */ - monthsInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return this._leapYear(year.year ? year.year() : year) ? 13 : 12; - }, - - /** Determine the week of the year for a date. - @memberof HebrewCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Sunday of this week starting on Sunday - var checkDate = this.newDate(year, month, day); - checkDate.add(-checkDate.dayOfWeek(), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a year. - @memberof HebrewCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of days. - @throws Error if an invalid year or a different calendar used. */ - daysInYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - year = date.year(); - return this.toJD((year === -1 ? +1 : year + 1), 7, 1) - this.toJD(year, 7, 1); - }, - - /** Retrieve the number of days in a month. - @memberof HebrewCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - if (year.year) { - month = year.month(); - year = year.year(); - } - this._validate(year, month, this.minDay, main.local.invalidMonth); - return (month === 12 && this.leapYear(year) ? 30 : // Adar I - (month === 8 && mod(this.daysInYear(year), 10) === 5 ? 30 : // Cheshvan in shlemah year - (month === 9 && mod(this.daysInYear(year), 10) === 3 ? 29 : // Kislev in chaserah year - this.daysPerMonth[month - 1]))); - }, - - /** Determine whether this date is a week day. - @memberof HebrewCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return this.dayOfWeek(year, month, day) !== 6; - }, - - /** Retrieve additional information about a date - year type. - @memberof HebrewCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {object} Additional information - contents depends on calendar. - @throws Error if an invalid date or a different calendar used. */ - extraInfo: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - return {yearType: (this.leapYear(date) ? 'embolismic' : 'common') + ' ' + - ['deficient', 'regular', 'complete'][this.daysInYear(date) % 10 - 3]}; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof HebrewCalendar - @param year {CDate)|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = date.year(); - month = date.month(); - day = date.day(); - var adjYear = (year <= 0 ? year + 1 : year); - var jd = this.jdEpoch + this._delay1(adjYear) + - this._delay2(adjYear) + day + 1; - if (month < 7) { - for (var m = 7; m <= this.monthsInYear(year); m++) { - jd += this.daysInMonth(year, m); - } - for (var m = 1; m < month; m++) { - jd += this.daysInMonth(year, m); - } - } - else { - for (var m = 7; m < month; m++) { - jd += this.daysInMonth(year, m); - } - } - return jd; - }, - - /** Test for delay of start of new year and to avoid - Sunday, Wednesday, or Friday as start of the new year. - @memberof HebrewCalendar - @private - @param year {number} The year to examine. - @return {number} The days to offset by. */ - _delay1: function(year) { - var months = Math.floor((235 * year - 234) / 19); - var parts = 12084 + 13753 * months; - var day = months * 29 + Math.floor(parts / 25920); - if (mod(3 * (day + 1), 7) < 3) { - day++; - } - return day; - }, - - /** Check for delay in start of new year due to length of adjacent years. - @memberof HebrewCalendar - @private - @param year {number} The year to examine. - @return {number} The days to offset by. */ - _delay2: function(year) { - var last = this._delay1(year - 1); - var present = this._delay1(year); - var next = this._delay1(year + 1); - return ((next - present) === 356 ? 2 : ((present - last) === 382 ? 1 : 0)); - }, - - /** Create a new date from a Julian date. - @memberof HebrewCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - jd = Math.floor(jd) + 0.5; - var year = Math.floor(((jd - this.jdEpoch) * 98496.0) / 35975351.0) - 1; - while (jd >= this.toJD((year === -1 ? +1 : year + 1), 7, 1)) { - year++; - } - var month = (jd < this.toJD(year, 1, 1)) ? 7 : 1; - while (jd > this.toJD(year, month, this.daysInMonth(year, month))) { - month++; - } - var day = jd - this.toJD(year, month, 1) + 1; - return this.newDate(year, month, day); - } -}); - -// Modulus function which works for non-integers. -function mod(a, b) { - return a - (b * Math.floor(a / b)); -} - -// Hebrew calendar implementation -main.calendars.hebrew = HebrewCalendar; - - -},{"../main":1147,"object-assign":1149}],1138:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Islamic calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Islamic or '16 civil' calendar. - Based on code from http://www.iranchamber.com/calendar/converter/iranian_calendar_converter.php. - See also http://en.wikipedia.org/wiki/Islamic_calendar. - @class IslamicCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function IslamicCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -IslamicCalendar.prototype = new main.baseCalendar; - -assign(IslamicCalendar.prototype, { - /** The calendar name. - @memberof IslamicCalendar */ - name: 'Islamic', - /** Julian date of start of Islamic epoch: 16 July 622 CE. - @memberof IslamicCalendar */ - jdEpoch: 1948439.5, - /** Days per month in a common year. - @memberof IslamicCalendar */ - daysPerMonth: [30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29], - /** true if has a year zero, false if not. - @memberof IslamicCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof IslamicCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof IslamicCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof IslamicCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof IslamicCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Islamic', - epochs: ['BH', 'AH'], - monthNames: ['Muharram', 'Safar', 'Rabi\' al-awwal', 'Rabi\' al-thani', 'Jumada al-awwal', 'Jumada al-thani', - 'Rajab', 'Sha\'aban', 'Ramadan', 'Shawwal', 'Dhu al-Qi\'dah', 'Dhu al-Hijjah'], - monthNamesShort: ['Muh', 'Saf', 'Rab1', 'Rab2', 'Jum1', 'Jum2', 'Raj', 'Sha\'', 'Ram', 'Shaw', 'DhuQ', 'DhuH'], - dayNames: ['Yawm al-ahad', 'Yawm al-ithnayn', 'Yawm ath-thulaathaa\'', - 'Yawm al-arbi\'aa\'', 'Yawm al-khamīs', 'Yawm al-jum\'a', 'Yawm as-sabt'], - dayNamesShort: ['Aha', 'Ith', 'Thu', 'Arb', 'Kha', 'Jum', 'Sab'], - dayNamesMin: ['Ah','It','Th','Ar','Kh','Ju','Sa'], - digits: null, - dateFormat: 'yyyy/mm/dd', - firstDay: 6, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof IslamicCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return (date.year() * 11 + 14) % 30 < 11; - }, - - /** Determine the week of the year for a date. - @memberof IslamicCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Sunday of this week starting on Sunday - var checkDate = this.newDate(year, month, day); - checkDate.add(-checkDate.dayOfWeek(), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a year. - @memberof IslamicCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of days. - @throws Error if an invalid year or a different calendar used. */ - daysInYear: function(year) { - return (this.leapYear(year) ? 355 : 354); - }, - - /** Retrieve the number of days in a month. - @memberof IslamicCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 12 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof IslamicCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return this.dayOfWeek(year, month, day) !== 5; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof IslamicCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = date.year(); - month = date.month(); - day = date.day(); - year = (year <= 0 ? year + 1 : year); - return day + Math.ceil(29.5 * (month - 1)) + (year - 1) * 354 + - Math.floor((3 + (11 * year)) / 30) + this.jdEpoch - 1; - }, - - /** Create a new date from a Julian date. - @memberof IslamicCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - jd = Math.floor(jd) + 0.5; - var year = Math.floor((30 * (jd - this.jdEpoch) + 10646) / 10631); - year = (year <= 0 ? year - 1 : year); - var month = Math.min(12, Math.ceil((jd - 29 - this.toJD(year, 1, 1)) / 29.5) + 1); - var day = jd - this.toJD(year, month, 1) + 1; - return this.newDate(year, month, day); - } -}); - -// Islamic (16 civil) calendar implementation -main.calendars.islamic = IslamicCalendar; - - -},{"../main":1147,"object-assign":1149}],1139:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Julian calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Julian calendar. - Based on code from http://www.fourmilab.ch/documents/calendar/. - See also http://en.wikipedia.org/wiki/Julian_calendar. - @class JulianCalendar - @augments BaseCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function JulianCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -JulianCalendar.prototype = new main.baseCalendar; - -assign(JulianCalendar.prototype, { - /** The calendar name. - @memberof JulianCalendar */ - name: 'Julian', - /** Julian date of start of Julian epoch: 1 January 0001 AD = 30 December 0001 BCE. - @memberof JulianCalendar */ - jdEpoch: 1721423.5, - /** Days per month in a common year. - @memberof JulianCalendar */ - daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - /** true if has a year zero, false if not. - @memberof JulianCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof JulianCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof JulianCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof JulianCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof JulianCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Julian', - epochs: ['BC', 'AD'], - monthNames: ['January', 'February', 'March', 'April', 'May', 'June', - 'July', 'August', 'September', 'October', 'November', 'December'], - monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], - dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], - digits: null, - dateFormat: 'mm/dd/yyyy', - firstDay: 0, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof JulianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - var year = (date.year() < 0 ? date.year() + 1 : date.year()); // No year zero - return (year % 4) === 0; - }, - - /** Determine the week of the year for a date - ISO 8601. - @memberof JulianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Thursday of this week starting on Monday - var checkDate = this.newDate(year, month, day); - checkDate.add(4 - (checkDate.dayOfWeek() || 7), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a month. - @memberof JulianCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof JulianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} True if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return (this.dayOfWeek(year, month, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof JulianCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = date.year(); - month = date.month(); - day = date.day(); - if (year < 0) { year++; } // No year zero - // Jean Meeus algorithm, "Astronomical Algorithms", 1991 - if (month <= 2) { - year--; - month += 12; - } - return Math.floor(365.25 * (year + 4716)) + - Math.floor(30.6001 * (month + 1)) + day - 1524.5; - }, - - /** Create a new date from a Julian date. - @memberof JulianCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - // Jean Meeus algorithm, "Astronomical Algorithms", 1991 - var a = Math.floor(jd + 0.5); - var b = a + 1524; - var c = Math.floor((b - 122.1) / 365.25); - var d = Math.floor(365.25 * c); - var e = Math.floor((b - d) / 30.6001); - var month = e - Math.floor(e < 14 ? 1 : 13); - var year = c - Math.floor(month > 2 ? 4716 : 4715); - var day = b - d - Math.floor(30.6001 * e); - if (year <= 0) { year--; } // No year zero - return this.newDate(year, month, day); - } -}); - -// Julian calendar implementation -main.calendars.julian = JulianCalendar; - - -},{"../main":1147,"object-assign":1149}],1140:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Mayan calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Mayan Long Count calendar. - See also http://en.wikipedia.org/wiki/Mayan_calendar. - @class MayanCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function MayanCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -MayanCalendar.prototype = new main.baseCalendar; - -assign(MayanCalendar.prototype, { - /** The calendar name. - @memberof MayanCalendar */ - name: 'Mayan', - /** Julian date of start of Mayan epoch: 11 August 3114 BCE. - @memberof MayanCalendar */ - jdEpoch: 584282.5, - /** true if has a year zero, false if not. - @memberof MayanCalendar */ - hasYearZero: true, - /** The minimum month number. - @memberof MayanCalendar */ - minMonth: 0, - /** The first month in the year. - @memberof MayanCalendar */ - firstMonth: 0, - /** The minimum day number. - @memberof MayanCalendar */ - minDay: 0, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof MayanCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. - @property haabMonths {string[]} The names of the Haab months. - @property tzolkinMonths {string[]} The names of the Tzolkin months. */ - regionalOptions: { // Localisations - '': { - name: 'Mayan', - epochs: ['', ''], - monthNames: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - '10', '11', '12', '13', '14', '15', '16', '17'], - monthNamesShort: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - '10', '11', '12', '13', '14', '15', '16', '17'], - dayNames: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], - dayNamesShort: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], - dayNamesMin: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], - digits: null, - dateFormat: 'YYYY.m.d', - firstDay: 0, - isRTL: false, - haabMonths: ['Pop', 'Uo', 'Zip', 'Zotz', 'Tzec', 'Xul', 'Yaxkin', 'Mol', 'Chen', 'Yax', - 'Zac', 'Ceh', 'Mac', 'Kankin', 'Muan', 'Pax', 'Kayab', 'Cumku', 'Uayeb'], - tzolkinMonths: ['Imix', 'Ik', 'Akbal', 'Kan', 'Chicchan', 'Cimi', 'Manik', 'Lamat', 'Muluc', 'Oc', - 'Chuen', 'Eb', 'Ben', 'Ix', 'Men', 'Cib', 'Caban', 'Etznab', 'Cauac', 'Ahau'] - } - }, - - /** Determine whether this date is in a leap year. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return false; - }, - - /** Format the year, if not a simple sequential number. - @memberof MayanCalendar - @param year {CDate|number} The date to format or the year to format. - @return {string} The formatted year. - @throws Error if an invalid year or a different calendar used. */ - formatYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - year = date.year(); - var baktun = Math.floor(year / 400); - year = year % 400; - year += (year < 0 ? 400 : 0); - var katun = Math.floor(year / 20); - return baktun + '.' + katun + '.' + (year % 20); - }, - - /** Convert from the formatted year back to a single number. - @memberof MayanCalendar - @param years {string} The year as n.n.n. - @return {number} The sequential year. - @throws Error if an invalid value is supplied. */ - forYear: function(years) { - years = years.split('.'); - if (years.length < 3) { - throw 'Invalid Mayan year'; - } - var year = 0; - for (var i = 0; i < years.length; i++) { - var y = parseInt(years[i], 10); - if (Math.abs(y) > 19 || (i > 0 && y < 0)) { - throw 'Invalid Mayan year'; - } - year = year * 20 + y; - } - return year; - }, - - /** Retrieve the number of months in a year. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of months. - @throws Error if an invalid year or a different calendar used. */ - monthsInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return 18; - }, - - /** Determine the week of the year for a date. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - this._validate(year, month, day, main.local.invalidDate); - return 0; - }, - - /** Retrieve the number of days in a year. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of days. - @throws Error if an invalid year or a different calendar used. */ - daysInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return 360; - }, - - /** Retrieve the number of days in a month. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - this._validate(year, month, this.minDay, main.local.invalidMonth); - return 20; - }, - - /** Retrieve the number of days in a week. - @memberof MayanCalendar - @return {number} The number of days. */ - daysInWeek: function() { - return 5; // Just for formatting - }, - - /** Retrieve the day of the week for a date. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The day of the week: 0 to number of days - 1. - @throws Error if an invalid date or a different calendar used. */ - dayOfWeek: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - return date.day(); - }, - - /** Determine whether this date is a week day. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - this._validate(year, month, day, main.local.invalidDate); - return true; - }, - - /** Retrieve additional information about a date - Haab and Tzolkin equivalents. - @memberof MayanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {object} Additional information - contents depends on calendar. - @throws Error if an invalid date or a different calendar used. */ - extraInfo: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - var jd = date.toJD(); - var haab = this._toHaab(jd); - var tzolkin = this._toTzolkin(jd); - return {haabMonthName: this.local.haabMonths[haab[0] - 1], - haabMonth: haab[0], haabDay: haab[1], - tzolkinDayName: this.local.tzolkinMonths[tzolkin[0] - 1], - tzolkinDay: tzolkin[0], tzolkinTrecena: tzolkin[1]}; - }, - - /** Retrieve Haab date from a Julian date. - @memberof MayanCalendar - @private - @param jd {number} The Julian date. - @return {number[]} Corresponding Haab month and day. */ - _toHaab: function(jd) { - jd -= this.jdEpoch; - var day = mod(jd + 8 + ((18 - 1) * 20), 365); - return [Math.floor(day / 20) + 1, mod(day, 20)]; - }, - - /** Retrieve Tzolkin date from a Julian date. - @memberof MayanCalendar - @private - @param jd {number} The Julian date. - @return {number[]} Corresponding Tzolkin day and trecena. */ - _toTzolkin: function(jd) { - jd -= this.jdEpoch; - return [amod(jd + 20, 20), amod(jd + 4, 13)]; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof MayanCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - return date.day() + (date.month() * 20) + (date.year() * 360) + this.jdEpoch; - }, - - /** Create a new date from a Julian date. - @memberof MayanCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - jd = Math.floor(jd) + 0.5 - this.jdEpoch; - var year = Math.floor(jd / 360); - jd = jd % 360; - jd += (jd < 0 ? 360 : 0); - var month = Math.floor(jd / 20); - var day = jd % 20; - return this.newDate(year, month, day); - } -}); - -// Modulus function which works for non-integers. -function mod(a, b) { - return a - (b * Math.floor(a / b)); -} - -// Modulus function which returns numerator if modulus is zero. -function amod(a, b) { - return mod(a - 1, b) + 1; -} - -// Mayan calendar implementation -main.calendars.mayan = MayanCalendar; - - -},{"../main":1147,"object-assign":1149}],1141:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Nanakshahi calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) January 2016. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Nanakshahi calendar. - See also https://en.wikipedia.org/wiki/Nanakshahi_calendar. - @class NanakshahiCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function NanakshahiCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -NanakshahiCalendar.prototype = new main.baseCalendar; - -var gregorian = main.instance('gregorian'); - -assign(NanakshahiCalendar.prototype, { - /** The calendar name. - @memberof NanakshahiCalendar */ - name: 'Nanakshahi', - /** Julian date of start of Nanakshahi epoch: 14 March 1469 CE. - @memberof NanakshahiCalendar */ - jdEpoch: 2257673.5, - /** Days per month in a common year. - @memberof NanakshahiCalendar */ - daysPerMonth: [31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30], - /** true if has a year zero, false if not. - @memberof NanakshahiCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof NanakshahiCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof NanakshahiCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof NanakshahiCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof NanakshahiCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Nanakshahi', - epochs: ['BN', 'AN'], - monthNames: ['Chet', 'Vaisakh', 'Jeth', 'Harh', 'Sawan', 'Bhadon', - 'Assu', 'Katak', 'Maghar', 'Poh', 'Magh', 'Phagun'], - monthNamesShort: ['Che', 'Vai', 'Jet', 'Har', 'Saw', 'Bha', 'Ass', 'Kat', 'Mgr', 'Poh', 'Mgh', 'Pha'], - dayNames: ['Somvaar', 'Mangalvar', 'Budhvaar', 'Veervaar', 'Shukarvaar', 'Sanicharvaar', 'Etvaar'], - dayNamesShort: ['Som', 'Mangal', 'Budh', 'Veer', 'Shukar', 'Sanichar', 'Et'], - dayNamesMin: ['So', 'Ma', 'Bu', 'Ve', 'Sh', 'Sa', 'Et'], - digits: null, - dateFormat: 'dd-mm-yyyy', - firstDay: 0, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof NanakshahiCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, - main.local.invalidYear || main.regionalOptions[''].invalidYear); - return gregorian.leapYear(date.year() + (date.year() < 1 ? 1 : 0) + 1469); - }, - - /** Determine the week of the year for a date. - @memberof NanakshahiCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Monday of this week starting on Monday - var checkDate = this.newDate(year, month, day); - checkDate.add(1 - (checkDate.dayOfWeek() || 7), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a month. - @memberof NanakshahiCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 12 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof NanakshahiCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return (this.dayOfWeek(year, month, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof NanakshahiCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidMonth); - var year = date.year(); - if (year < 0) { year++; } // No year zero - var doy = date.day(); - for (var m = 1; m < date.month(); m++) { - doy += this.daysPerMonth[m - 1]; - } - return doy + gregorian.toJD(year + 1468, 3, 13); - }, - - /** Create a new date from a Julian date. - @memberof NanakshahiCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - jd = Math.floor(jd + 0.5); - var year = Math.floor((jd - (this.jdEpoch - 1)) / 366); - while (jd >= this.toJD(year + 1, 1, 1)) { - year++; - } - var day = jd - Math.floor(this.toJD(year, 1, 1) + 0.5) + 1; - var month = 1; - while (day > this.daysInMonth(year, month)) { - day -= this.daysInMonth(year, month); - month++; - } - return this.newDate(year, month, day); - } -}); - -// Nanakshahi calendar implementation -main.calendars.nanakshahi = NanakshahiCalendar; - - -},{"../main":1147,"object-assign":1149}],1142:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Nepali calendar for jQuery v2.0.2. - Written by Artur Neumann (ict.projects{at}nepal.inf.org) April 2013. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Nepali civil calendar. - Based on the ideas from - http://codeissue.com/articles/a04e050dea7468f/algorithm-to-convert-english-date-to-nepali-date-using-c-net - and http://birenj2ee.blogspot.com/2011/04/nepali-calendar-in-java.html - See also http://en.wikipedia.org/wiki/Nepali_calendar - and https://en.wikipedia.org/wiki/Bikram_Samwat. - @class NepaliCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function NepaliCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -NepaliCalendar.prototype = new main.baseCalendar; - -assign(NepaliCalendar.prototype, { - /** The calendar name. - @memberof NepaliCalendar */ - name: 'Nepali', - /** Julian date of start of Nepali epoch: 14 April 57 BCE. - @memberof NepaliCalendar */ - jdEpoch: 1700709.5, - /** Days per month in a common year. - @memberof NepaliCalendar */ - daysPerMonth: [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - /** true if has a year zero, false if not. - @memberof NepaliCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof NepaliCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof NepaliCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof NepaliCalendar */ - minDay: 1, - /** The number of days in the year. - @memberof NepaliCalendar */ - daysPerYear: 365, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof NepaliCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Nepali', - epochs: ['BBS', 'ABS'], - monthNames: ['Baisakh', 'Jestha', 'Ashadh', 'Shrawan', 'Bhadra', 'Ashwin', - 'Kartik', 'Mangsir', 'Paush', 'Mangh', 'Falgun', 'Chaitra'], - monthNamesShort: ['Bai', 'Je', 'As', 'Shra', 'Bha', 'Ash', 'Kar', 'Mang', 'Pau', 'Ma', 'Fal', 'Chai'], - dayNames: ['Aaitabaar', 'Sombaar', 'Manglbaar', 'Budhabaar', 'Bihibaar', 'Shukrabaar', 'Shanibaar'], - dayNamesShort: ['Aaita', 'Som', 'Mangl', 'Budha', 'Bihi', 'Shukra', 'Shani'], - dayNamesMin: ['Aai', 'So', 'Man', 'Bu', 'Bi', 'Shu', 'Sha'], - digits: null, - dateFormat: 'dd/mm/yyyy', - firstDay: 1, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof NepaliCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - return this.daysInYear(year) !== this.daysPerYear; - }, - - /** Determine the week of the year for a date. - @memberof NepaliCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Sunday of this week starting on Sunday - var checkDate = this.newDate(year, month, day); - checkDate.add(-checkDate.dayOfWeek(), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a year. - @memberof NepaliCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of days. - @throws Error if an invalid year or a different calendar used. */ - daysInYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - year = date.year(); - if (typeof this.NEPALI_CALENDAR_DATA[year] === 'undefined') { - return this.daysPerYear; - } - var daysPerYear = 0; - for (var month_number = this.minMonth; month_number <= 12; month_number++) { - daysPerYear += this.NEPALI_CALENDAR_DATA[year][month_number]; - } - return daysPerYear; - }, - - /** Retrieve the number of days in a month. - @memberof NepaliCalendar - @param year {CDate|number| The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - if (year.year) { - month = year.month(); - year = year.year(); - } - this._validate(year, month, this.minDay, main.local.invalidMonth); - return (typeof this.NEPALI_CALENDAR_DATA[year] === 'undefined' ? - this.daysPerMonth[month - 1] : this.NEPALI_CALENDAR_DATA[year][month]); - }, - - /** Determine whether this date is a week day. - @memberof NepaliCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return this.dayOfWeek(year, month, day) !== 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof NepaliCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(nepaliYear, nepaliMonth, nepaliDay) { - var date = this._validate(nepaliYear, nepaliMonth, nepaliDay, main.local.invalidDate); - nepaliYear = date.year(); - nepaliMonth = date.month(); - nepaliDay = date.day(); - var gregorianCalendar = main.instance(); - var gregorianDayOfYear = 0; // We will add all the days that went by since - // the 1st. January and then we can get the Gregorian Date - var nepaliMonthToCheck = nepaliMonth; - var nepaliYearToCheck = nepaliYear; - this._createMissingCalendarData(nepaliYear); - // Get the correct year - var gregorianYear = nepaliYear - (nepaliMonthToCheck > 9 || (nepaliMonthToCheck === 9 && - nepaliDay >= this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][0]) ? 56 : 57); - // First we add the amount of days in the actual Nepali month as the day of year in the - // Gregorian one because at least this days are gone since the 1st. Jan. - if (nepaliMonth !== 9) { - gregorianDayOfYear = nepaliDay; - nepaliMonthToCheck--; - } - // Now we loop throw all Nepali month and add the amount of days to gregorianDayOfYear - // we do this till we reach Paush (9th month). 1st. January always falls in this month - while (nepaliMonthToCheck !== 9) { - if (nepaliMonthToCheck <= 0) { - nepaliMonthToCheck = 12; - nepaliYearToCheck--; - } - gregorianDayOfYear += this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][nepaliMonthToCheck]; - nepaliMonthToCheck--; - } - // If the date that has to be converted is in Paush (month no. 9) we have to do some other calculation - if (nepaliMonth === 9) { - // Add the days that are passed since the first day of Paush and substract the - // amount of days that lie between 1st. Jan and 1st Paush - gregorianDayOfYear += nepaliDay - this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][0]; - // For the first days of Paush we are now in negative values, - // because in the end of the gregorian year we substract - // 365 / 366 days (P.S. remember math in school + - gives -) - if (gregorianDayOfYear < 0) { - gregorianDayOfYear += gregorianCalendar.daysInYear(gregorianYear); - } - } - else { - gregorianDayOfYear += this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][9] - - this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][0]; - } - return gregorianCalendar.newDate(gregorianYear, 1 ,1).add(gregorianDayOfYear, 'd').toJD(); - }, - - /** Create a new date from a Julian date. - @memberof NepaliCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - var gregorianCalendar = main.instance(); - var gregorianDate = gregorianCalendar.fromJD(jd); - var gregorianYear = gregorianDate.year(); - var gregorianDayOfYear = gregorianDate.dayOfYear(); - var nepaliYear = gregorianYear + 56; //this is not final, it could be also +57 but +56 is always true for 1st Jan. - this._createMissingCalendarData(nepaliYear); - var nepaliMonth = 9; // Jan 1 always fall in Nepali month Paush which is the 9th month of Nepali calendar. - // Get the Nepali day in Paush (month 9) of 1st January - var dayOfFirstJanInPaush = this.NEPALI_CALENDAR_DATA[nepaliYear][0]; - // Check how many days are left of Paush . - // Days calculated from 1st Jan till the end of the actual Nepali month, - // we use this value to check if the gregorian Date is in the actual Nepali month. - var daysSinceJanFirstToEndOfNepaliMonth = - this.NEPALI_CALENDAR_DATA[nepaliYear][nepaliMonth] - dayOfFirstJanInPaush + 1; - // If the gregorian day-of-year is smaller o equal than the sum of days between the 1st January and - // the end of the actual nepali month we found the correct nepali month. - // Example: - // The 4th February 2011 is the gregorianDayOfYear 35 (31 days of January + 4) - // 1st January 2011 is in the nepali year 2067, where 1st. January is in the 17th day of Paush (9th month) - // In 2067 Paush has 30days, This means (30-17+1=14) there are 14days between 1st January and end of Paush - // (including 17th January) - // The gregorianDayOfYear (35) is bigger than 14, so we check the next month - // The next nepali month (Mangh) has 29 days - // 29+14=43, this is bigger than gregorianDayOfYear(35) so, we found the correct nepali month - while (gregorianDayOfYear > daysSinceJanFirstToEndOfNepaliMonth) { - nepaliMonth++; - if (nepaliMonth > 12) { - nepaliMonth = 1; - nepaliYear++; - } - daysSinceJanFirstToEndOfNepaliMonth += this.NEPALI_CALENDAR_DATA[nepaliYear][nepaliMonth]; - } - // The last step is to calculate the nepali day-of-month - // to continue our example from before: - // we calculated there are 43 days from 1st. January (17 Paush) till end of Mangh (29 days) - // when we subtract from this 43 days the day-of-year of the the Gregorian date (35), - // we know how far the searched day is away from the end of the Nepali month. - // So we simply subtract this number from the amount of days in this month (30) - var nepaliDayOfMonth = this.NEPALI_CALENDAR_DATA[nepaliYear][nepaliMonth] - - (daysSinceJanFirstToEndOfNepaliMonth - gregorianDayOfYear); - return this.newDate(nepaliYear, nepaliMonth, nepaliDayOfMonth); - }, - - /** Creates missing data in the NEPALI_CALENDAR_DATA table. - This data will not be correct but just give an estimated result. Mostly -/+ 1 day - @private - @param nepaliYear {number} The missing year number. */ - _createMissingCalendarData: function(nepaliYear) { - var tmp_calendar_data = this.daysPerMonth.slice(0); - tmp_calendar_data.unshift(17); - for (var nepaliYearToCreate = (nepaliYear - 1); nepaliYearToCreate < (nepaliYear + 2); nepaliYearToCreate++) { - if (typeof this.NEPALI_CALENDAR_DATA[nepaliYearToCreate] === 'undefined') { - this.NEPALI_CALENDAR_DATA[nepaliYearToCreate] = tmp_calendar_data; - } - } - }, - - NEPALI_CALENDAR_DATA: { - // These data are from http://www.ashesh.com.np - 1970: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1971: [18, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], - 1972: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - 1973: [19, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 1974: [19, 31, 31, 32, 30, 31, 31, 30, 29, 30, 29, 30, 30], - 1975: [18, 31, 31, 32, 32, 30, 31, 30, 29, 30, 29, 30, 30], - 1976: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 1977: [18, 31, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], - 1978: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1979: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 1980: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 1981: [18, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - 1982: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1983: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 1984: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 1985: [18, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - 1986: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1987: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 1988: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 1989: [18, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 1990: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1991: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - // These data are from http://nepalicalendar.rat32.com/index.php - 1992: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 1993: [18, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 1994: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1995: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - 1996: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 1997: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1998: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 1999: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2000: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2001: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2002: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2003: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2004: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2005: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2006: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2007: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2008: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], - 2009: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2010: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2011: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2012: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - 2013: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2014: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2015: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2016: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - 2017: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2018: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2019: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2020: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 2021: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2022: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - 2023: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2024: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 2025: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2026: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2027: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2028: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2029: [18, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], - 2030: [17, 31, 32, 31, 32, 31, 30, 30, 30, 30, 30, 30, 31], - 2031: [17, 31, 32, 31, 32, 31, 31, 31, 31, 31, 31, 31, 31], - 2032: [17, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32], - 2033: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2034: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2035: [17, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], - 2036: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2037: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2038: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2039: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - 2040: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2041: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2042: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2043: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - 2044: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2045: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2046: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2047: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 2048: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2049: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - 2050: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2051: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 2052: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2053: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - 2054: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2055: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 29, 30], - 2056: [17, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], - 2057: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2058: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2059: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2060: [17, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2061: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2062: [17, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], - 2063: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2064: [17, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2065: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2066: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], - 2067: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2068: [17, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2069: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2070: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - 2071: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2072: [17, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2073: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - 2074: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 2075: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2076: [16, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - 2077: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - 2078: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - 2079: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - 2080: [16, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - // These data are from http://www.ashesh.com.np/nepali-calendar/ - 2081: [17, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2082: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2083: [17, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], - 2084: [17, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], - 2085: [17, 31, 32, 31, 32, 31, 31, 30, 30, 29, 30, 30, 30], - 2086: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2087: [16, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], - 2088: [16, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], - 2089: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2090: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2091: [16, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], - 2092: [16, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2093: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2094: [17, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], - 2095: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30], - 2096: [17, 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - 2097: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - 2098: [17, 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 30, 31], - 2099: [17, 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30], - 2100: [17, 31, 32, 31, 32, 30, 31, 30, 29, 30, 29, 30, 30] - } -}); - -// Nepali calendar implementation -main.calendars.nepali = NepaliCalendar; - - -},{"../main":1147,"object-assign":1149}],1143:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Persian calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the Persian or Jalali calendar. - Based on code from http://www.iranchamber.com/calendar/converter/iranian_calendar_converter.php. - See also http://en.wikipedia.org/wiki/Iranian_calendar. - @class PersianCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function PersianCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -PersianCalendar.prototype = new main.baseCalendar; - -assign(PersianCalendar.prototype, { - /** The calendar name. - @memberof PersianCalendar */ - name: 'Persian', - /** Julian date of start of Persian epoch: 19 March 622 CE. - @memberof PersianCalendar */ - jdEpoch: 1948320.5, - /** Days per month in a common year. - @memberof PersianCalendar */ - daysPerMonth: [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29], - /** true if has a year zero, false if not. - @memberof PersianCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof PersianCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof PersianCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof PersianCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof PersianCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Persian', - epochs: ['BP', 'AP'], - monthNames: ['Farvardin', 'Ordibehesht', 'Khordad', 'Tir', 'Mordad', 'Shahrivar', - 'Mehr', 'Aban', 'Azar', 'Day', 'Bahman', 'Esfand'], - monthNamesShort: ['Far', 'Ord', 'Kho', 'Tir', 'Mor', 'Sha', 'Meh', 'Aba', 'Aza', 'Day', 'Bah', 'Esf'], - dayNames: ['Yekshambe', 'Doshambe', 'Seshambe', 'Chæharshambe', 'Panjshambe', 'Jom\'e', 'Shambe'], - dayNamesShort: ['Yek', 'Do', 'Se', 'Chæ', 'Panj', 'Jom', 'Sha'], - dayNamesMin: ['Ye','Do','Se','Ch','Pa','Jo','Sh'], - digits: null, - dateFormat: 'yyyy/mm/dd', - firstDay: 6, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof PersianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return (((((date.year() - (date.year() > 0 ? 474 : 473)) % 2820) + - 474 + 38) * 682) % 2816) < 682; - }, - - /** Determine the week of the year for a date. - @memberof PersianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Saturday of this week starting on Saturday - var checkDate = this.newDate(year, month, day); - checkDate.add(-((checkDate.dayOfWeek() + 1) % 7), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a month. - @memberof PersianCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 12 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof PersianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return this.dayOfWeek(year, month, day) !== 5; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof PersianCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - year = date.year(); - month = date.month(); - day = date.day(); - var epBase = year - (year >= 0 ? 474 : 473); - var epYear = 474 + mod(epBase, 2820); - return day + (month <= 7 ? (month - 1) * 31 : (month - 1) * 30 + 6) + - Math.floor((epYear * 682 - 110) / 2816) + (epYear - 1) * 365 + - Math.floor(epBase / 2820) * 1029983 + this.jdEpoch - 1; - }, - - /** Create a new date from a Julian date. - @memberof PersianCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - jd = Math.floor(jd) + 0.5; - var depoch = jd - this.toJD(475, 1, 1); - var cycle = Math.floor(depoch / 1029983); - var cyear = mod(depoch, 1029983); - var ycycle = 2820; - if (cyear !== 1029982) { - var aux1 = Math.floor(cyear / 366); - var aux2 = mod(cyear, 366); - ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1; - } - var year = ycycle + (2820 * cycle) + 474; - year = (year <= 0 ? year - 1 : year); - var yday = jd - this.toJD(year, 1, 1) + 1; - var month = (yday <= 186 ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30)); - var day = jd - this.toJD(year, month, 1) + 1; - return this.newDate(year, month, day); - } -}); - -// Modulus function which works for non-integers. -function mod(a, b) { - return a - (b * Math.floor(a / b)); -} - -// Persian (Jalali) calendar implementation -main.calendars.persian = PersianCalendar; -main.calendars.jalali = PersianCalendar; - - -},{"../main":1147,"object-assign":1149}],1144:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Taiwanese (Minguo) calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -var gregorianCalendar = main.instance(); - -/** Implementation of the Taiwanese calendar. - See http://en.wikipedia.org/wiki/Minguo_calendar. - @class TaiwanCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function TaiwanCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -TaiwanCalendar.prototype = new main.baseCalendar; - -assign(TaiwanCalendar.prototype, { - /** The calendar name. - @memberof TaiwanCalendar */ - name: 'Taiwan', - /** Julian date of start of Taiwan epoch: 1 January 1912 CE (Gregorian). - @memberof TaiwanCalendar */ - jdEpoch: 2419402.5, - /** Difference in years between Taiwan and Gregorian calendars. - @memberof TaiwanCalendar */ - yearsOffset: 1911, - /** Days per month in a common year. - @memberof TaiwanCalendar */ - daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - /** true if has a year zero, false if not. - @memberof TaiwanCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof TaiwanCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof TaiwanCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof TaiwanCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof TaiwanCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Taiwan', - epochs: ['BROC', 'ROC'], - monthNames: ['January', 'February', 'March', 'April', 'May', 'June', - 'July', 'August', 'September', 'October', 'November', 'December'], - monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], - dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], - digits: null, - dateFormat: 'yyyy/mm/dd', - firstDay: 1, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof TaiwanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - var year = this._t2gYear(date.year()); - return gregorianCalendar.leapYear(year); - }, - - /** Determine the week of the year for a date - ISO 8601. - @memberof TaiwanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - var year = this._t2gYear(date.year()); - return gregorianCalendar.weekOfYear(year, date.month(), date.day()); - }, - - /** Retrieve the number of days in a month. - @memberof TaiwanCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof TaiwanCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return (this.dayOfWeek(year, month, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof TaiwanCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - var year = this._t2gYear(date.year()); - return gregorianCalendar.toJD(year, date.month(), date.day()); - }, - - /** Create a new date from a Julian date. - @memberof TaiwanCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - var date = gregorianCalendar.fromJD(jd); - var year = this._g2tYear(date.year()); - return this.newDate(year, date.month(), date.day()); - }, - - /** Convert Taiwanese to Gregorian year. - @memberof TaiwanCalendar - @private - @param year {number} The Taiwanese year. - @return {number} The corresponding Gregorian year. */ - _t2gYear: function(year) { - return year + this.yearsOffset + (year >= -this.yearsOffset && year <= -1 ? 1 : 0); - }, - - /** Convert Gregorian to Taiwanese year. - @memberof TaiwanCalendar - @private - @param year {number} The Gregorian year. - @return {number} The corresponding Taiwanese year. */ - _g2tYear: function(year) { - return year - this.yearsOffset - (year >= 1 && year <= this.yearsOffset ? 1 : 0); - } -}); - -// Taiwan calendar implementation -main.calendars.taiwan = TaiwanCalendar; - - -},{"../main":1147,"object-assign":1149}],1145:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Thai calendar for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -var gregorianCalendar = main.instance(); - -/** Implementation of the Thai calendar. - See http://en.wikipedia.org/wiki/Thai_calendar. - @class ThaiCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function ThaiCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -ThaiCalendar.prototype = new main.baseCalendar; - -assign(ThaiCalendar.prototype, { - /** The calendar name. - @memberof ThaiCalendar */ - name: 'Thai', - /** Julian date of start of Thai epoch: 1 January 543 BCE (Gregorian). - @memberof ThaiCalendar */ - jdEpoch: 1523098.5, - /** Difference in years between Thai and Gregorian calendars. - @memberof ThaiCalendar */ - yearsOffset: 543, - /** Days per month in a common year. - @memberof ThaiCalendar */ - daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - /** true if has a year zero, false if not. - @memberof ThaiCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof ThaiCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof ThaiCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof ThaiCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof ThaiCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Thai', - epochs: ['BBE', 'BE'], - monthNames: ['January', 'February', 'March', 'April', 'May', 'June', - 'July', 'August', 'September', 'October', 'November', 'December'], - monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], - dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], - digits: null, - dateFormat: 'dd/mm/yyyy', - firstDay: 0, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof ThaiCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - var year = this._t2gYear(date.year()); - return gregorianCalendar.leapYear(year); - }, - - /** Determine the week of the year for a date - ISO 8601. - @memberof ThaiCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - var year = this._t2gYear(date.year()); - return gregorianCalendar.weekOfYear(year, date.month(), date.day()); - }, - - /** Retrieve the number of days in a month. - @memberof ThaiCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof ThaiCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return (this.dayOfWeek(year, month, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof ThaiCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - var year = this._t2gYear(date.year()); - return gregorianCalendar.toJD(year, date.month(), date.day()); - }, - - /** Create a new date from a Julian date. - @memberof ThaiCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - var date = gregorianCalendar.fromJD(jd); - var year = this._g2tYear(date.year()); - return this.newDate(year, date.month(), date.day()); - }, - - /** Convert Thai to Gregorian year. - @memberof ThaiCalendar - @private - @param year {number} The Thai year. - @return {number} The corresponding Gregorian year. */ - _t2gYear: function(year) { - return year - this.yearsOffset - (year >= 1 && year <= this.yearsOffset ? 1 : 0); - }, - - /** Convert Gregorian to Thai year. - @memberof ThaiCalendar - @private - @param year {number} The Gregorian year. - @return {number} The corresponding Thai year. */ - _g2tYear: function(year) { - return year + this.yearsOffset + (year >= -this.yearsOffset && year <= -1 ? 1 : 0); - } -}); - -// Thai calendar implementation -main.calendars.thai = ThaiCalendar; - - -},{"../main":1147,"object-assign":1149}],1146:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - UmmAlQura calendar for jQuery v2.0.2. - Written by Amro Osama March 2013. - Modified by Binnooh.com & www.elm.sa - 2014 - Added dates back to 1276 Hijri year. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var main = require('../main'); -var assign = require('object-assign'); - - -/** Implementation of the UmmAlQura or 'saudi' calendar. - See also http://en.wikipedia.org/wiki/Islamic_calendar#Saudi_Arabia.27s_Umm_al-Qura_calendar. - http://www.ummulqura.org.sa/About.aspx - http://www.staff.science.uu.nl/~gent0113/islam/ummalqura.htm - @class UmmAlQuraCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function UmmAlQuraCalendar(language) { - this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; -} - -UmmAlQuraCalendar.prototype = new main.baseCalendar; - -assign(UmmAlQuraCalendar.prototype, { - /** The calendar name. - @memberof UmmAlQuraCalendar */ - name: 'UmmAlQura', - //jdEpoch: 1948440, // Julian date of start of UmmAlQura epoch: 14 March 1937 CE - //daysPerMonth: // Days per month in a common year, replaced by a method. - /** true if has a year zero, false if not. - @memberof UmmAlQuraCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof UmmAlQuraCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof UmmAlQuraCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof UmmAlQuraCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof UmmAlQuraCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Umm al-Qura', - epochs: ['BH', 'AH'], - monthNames: ['Al-Muharram', 'Safar', 'Rabi\' al-awwal', 'Rabi\' Al-Thani', 'Jumada Al-Awwal', 'Jumada Al-Thani', - 'Rajab', 'Sha\'aban', 'Ramadan', 'Shawwal', 'Dhu al-Qi\'dah', 'Dhu al-Hijjah'], - monthNamesShort: ['Muh', 'Saf', 'Rab1', 'Rab2', 'Jum1', 'Jum2', 'Raj', 'Sha\'', 'Ram', 'Shaw', 'DhuQ', 'DhuH'], - dayNames: ['Yawm al-Ahad', 'Yawm al-Ithnain', 'Yawm al-Thalāthā’', 'Yawm al-Arba‘ā’', 'Yawm al-Khamīs', 'Yawm al-Jum‘a', 'Yawm al-Sabt'], - dayNamesMin: ['Ah', 'Ith', 'Th', 'Ar', 'Kh', 'Ju', 'Sa'], - digits: null, - dateFormat: 'yyyy/mm/dd', - firstDay: 6, - isRTL: true - } - }, - - /** Determine whether this date is in a leap year. - @memberof UmmAlQuraCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function (year) { - var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); - return (this.daysInYear(date.year()) === 355); - }, - - /** Determine the week of the year for a date. - @memberof UmmAlQuraCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function (year, month, day) { - // Find Sunday of this week starting on Sunday - var checkDate = this.newDate(year, month, day); - checkDate.add(-checkDate.dayOfWeek(), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a year. - @memberof UmmAlQuraCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of days. - @throws Error if an invalid year or a different calendar used. */ - daysInYear: function (year) { - var daysCount = 0; - for (var i = 1; i <= 12; i++) { - daysCount += this.daysInMonth(year, i); - } - return daysCount; - }, - - /** Retrieve the number of days in a month. - @memberof UmmAlQuraCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function (year, month) { - var date = this._validate(year, month, this.minDay, main.local.invalidMonth); - var mcjdn = date.toJD() - 2400000 + 0.5; // Modified Chronological Julian Day Number (MCJDN) - // the MCJDN's of the start of the lunations in the Umm al-Qura calendar are stored in the 'ummalqura_dat' array - var index = 0; - for (var i = 0; i < ummalqura_dat.length; i++) { - if (ummalqura_dat[i] > mcjdn) { - return (ummalqura_dat[index] - ummalqura_dat[index - 1]); - } - index++; - } - return 30; // Unknown outside - }, - - /** Determine whether this date is a week day. - @memberof UmmAlQuraCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function (year, month, day) { - return this.dayOfWeek(year, month, day) !== 5; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof UmmAlQuraCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function (year, month, day) { - var date = this._validate(year, month, day, main.local.invalidDate); - var index = (12 * (date.year() - 1)) + date.month() - 15292; - var mcjdn = date.day() + ummalqura_dat[index - 1] - 1; - return mcjdn + 2400000 - 0.5; // Modified Chronological Julian Day Number (MCJDN) - }, - - /** Create a new date from a Julian date. - @memberof UmmAlQuraCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function (jd) { - var mcjdn = jd - 2400000 + 0.5; // Modified Chronological Julian Day Number (MCJDN) - // the MCJDN's of the start of the lunations in the Umm al-Qura calendar - // are stored in the 'ummalqura_dat' array - var index = 0; - for (var i = 0; i < ummalqura_dat.length; i++) { - if (ummalqura_dat[i] > mcjdn) break; - index++; - } - var lunation = index + 15292; //UmmAlQura Lunation Number - var ii = Math.floor((lunation - 1) / 12); - var year = ii + 1; - var month = lunation - 12 * ii; - var day = mcjdn - ummalqura_dat[index - 1] + 1; - return this.newDate(year, month, day); - }, - - /** Determine whether a date is valid for this calendar. - @memberof UmmAlQuraCalendar - @param year {number} The year to examine. - @param month {number} The month to examine. - @param day {number} The day to examine. - @return {boolean} true if a valid date, false if not. */ - isValid: function(year, month, day) { - var valid = main.baseCalendar.prototype.isValid.apply(this, arguments); - if (valid) { - year = (year.year != null ? year.year : year); - valid = (year >= 1276 && year <= 1500); - } - return valid; - }, - - /** Check that a candidate date is from the same calendar and is valid. - @memberof UmmAlQuraCalendar - @private - @param year {CDate|number} The date to validate or the year to validate. - @param month {number} The month to validate. - @param day {number} The day to validate. - @param error {string} Error message if invalid. - @throws Error if different calendars used or invalid date. */ - _validate: function(year, month, day, error) { - var date = main.baseCalendar.prototype._validate.apply(this, arguments); - if (date.year < 1276 || date.year > 1500) { - throw error.replace(/\{0\}/, this.local.name); - } - return date; - } -}); - -// UmmAlQura calendar implementation -main.calendars.ummalqura = UmmAlQuraCalendar; - -var ummalqura_dat = [ - 20, 50, 79, 109, 138, 168, 197, 227, 256, 286, 315, 345, 374, 404, 433, 463, 492, 522, 551, 581, - 611, 641, 670, 700, 729, 759, 788, 818, 847, 877, 906, 936, 965, 995, 1024, 1054, 1083, 1113, 1142, 1172, - 1201, 1231, 1260, 1290, 1320, 1350, 1379, 1409, 1438, 1468, 1497, 1527, 1556, 1586, 1615, 1645, 1674, 1704, 1733, 1763, - 1792, 1822, 1851, 1881, 1910, 1940, 1969, 1999, 2028, 2058, 2087, 2117, 2146, 2176, 2205, 2235, 2264, 2294, 2323, 2353, - 2383, 2413, 2442, 2472, 2501, 2531, 2560, 2590, 2619, 2649, 2678, 2708, 2737, 2767, 2796, 2826, 2855, 2885, 2914, 2944, - 2973, 3003, 3032, 3062, 3091, 3121, 3150, 3180, 3209, 3239, 3268, 3298, 3327, 3357, 3386, 3416, 3446, 3476, 3505, 3535, - 3564, 3594, 3623, 3653, 3682, 3712, 3741, 3771, 3800, 3830, 3859, 3889, 3918, 3948, 3977, 4007, 4036, 4066, 4095, 4125, - 4155, 4185, 4214, 4244, 4273, 4303, 4332, 4362, 4391, 4421, 4450, 4480, 4509, 4539, 4568, 4598, 4627, 4657, 4686, 4716, - 4745, 4775, 4804, 4834, 4863, 4893, 4922, 4952, 4981, 5011, 5040, 5070, 5099, 5129, 5158, 5188, 5218, 5248, 5277, 5307, - 5336, 5366, 5395, 5425, 5454, 5484, 5513, 5543, 5572, 5602, 5631, 5661, 5690, 5720, 5749, 5779, 5808, 5838, 5867, 5897, - 5926, 5956, 5985, 6015, 6044, 6074, 6103, 6133, 6162, 6192, 6221, 6251, 6281, 6311, 6340, 6370, 6399, 6429, 6458, 6488, - 6517, 6547, 6576, 6606, 6635, 6665, 6694, 6724, 6753, 6783, 6812, 6842, 6871, 6901, 6930, 6960, 6989, 7019, 7048, 7078, - 7107, 7137, 7166, 7196, 7225, 7255, 7284, 7314, 7344, 7374, 7403, 7433, 7462, 7492, 7521, 7551, 7580, 7610, 7639, 7669, - 7698, 7728, 7757, 7787, 7816, 7846, 7875, 7905, 7934, 7964, 7993, 8023, 8053, 8083, 8112, 8142, 8171, 8201, 8230, 8260, - 8289, 8319, 8348, 8378, 8407, 8437, 8466, 8496, 8525, 8555, 8584, 8614, 8643, 8673, 8702, 8732, 8761, 8791, 8821, 8850, - 8880, 8909, 8938, 8968, 8997, 9027, 9056, 9086, 9115, 9145, 9175, 9205, 9234, 9264, 9293, 9322, 9352, 9381, 9410, 9440, - 9470, 9499, 9529, 9559, 9589, 9618, 9648, 9677, 9706, 9736, 9765, 9794, 9824, 9853, 9883, 9913, 9943, 9972, 10002, 10032, - 10061, 10090, 10120, 10149, 10178, 10208, 10237, 10267, 10297, 10326, 10356, 10386, 10415, 10445, 10474, 10504, 10533, 10562, 10592, 10621, - 10651, 10680, 10710, 10740, 10770, 10799, 10829, 10858, 10888, 10917, 10947, 10976, 11005, 11035, 11064, 11094, 11124, 11153, 11183, 11213, - 11242, 11272, 11301, 11331, 11360, 11389, 11419, 11448, 11478, 11507, 11537, 11567, 11596, 11626, 11655, 11685, 11715, 11744, 11774, 11803, - 11832, 11862, 11891, 11921, 11950, 11980, 12010, 12039, 12069, 12099, 12128, 12158, 12187, 12216, 12246, 12275, 12304, 12334, 12364, 12393, - 12423, 12453, 12483, 12512, 12542, 12571, 12600, 12630, 12659, 12688, 12718, 12747, 12777, 12807, 12837, 12866, 12896, 12926, 12955, 12984, - 13014, 13043, 13072, 13102, 13131, 13161, 13191, 13220, 13250, 13280, 13310, 13339, 13368, 13398, 13427, 13456, 13486, 13515, 13545, 13574, - 13604, 13634, 13664, 13693, 13723, 13752, 13782, 13811, 13840, 13870, 13899, 13929, 13958, 13988, 14018, 14047, 14077, 14107, 14136, 14166, - 14195, 14224, 14254, 14283, 14313, 14342, 14372, 14401, 14431, 14461, 14490, 14520, 14550, 14579, 14609, 14638, 14667, 14697, 14726, 14756, - 14785, 14815, 14844, 14874, 14904, 14933, 14963, 14993, 15021, 15051, 15081, 15110, 15140, 15169, 15199, 15228, 15258, 15287, 15317, 15347, - 15377, 15406, 15436, 15465, 15494, 15524, 15553, 15582, 15612, 15641, 15671, 15701, 15731, 15760, 15790, 15820, 15849, 15878, 15908, 15937, - 15966, 15996, 16025, 16055, 16085, 16114, 16144, 16174, 16204, 16233, 16262, 16292, 16321, 16350, 16380, 16409, 16439, 16468, 16498, 16528, - 16558, 16587, 16617, 16646, 16676, 16705, 16734, 16764, 16793, 16823, 16852, 16882, 16912, 16941, 16971, 17001, 17030, 17060, 17089, 17118, - 17148, 17177, 17207, 17236, 17266, 17295, 17325, 17355, 17384, 17414, 17444, 17473, 17502, 17532, 17561, 17591, 17620, 17650, 17679, 17709, - 17738, 17768, 17798, 17827, 17857, 17886, 17916, 17945, 17975, 18004, 18034, 18063, 18093, 18122, 18152, 18181, 18211, 18241, 18270, 18300, - 18330, 18359, 18388, 18418, 18447, 18476, 18506, 18535, 18565, 18595, 18625, 18654, 18684, 18714, 18743, 18772, 18802, 18831, 18860, 18890, - 18919, 18949, 18979, 19008, 19038, 19068, 19098, 19127, 19156, 19186, 19215, 19244, 19274, 19303, 19333, 19362, 19392, 19422, 19452, 19481, - 19511, 19540, 19570, 19599, 19628, 19658, 19687, 19717, 19746, 19776, 19806, 19836, 19865, 19895, 19924, 19954, 19983, 20012, 20042, 20071, - 20101, 20130, 20160, 20190, 20219, 20249, 20279, 20308, 20338, 20367, 20396, 20426, 20455, 20485, 20514, 20544, 20573, 20603, 20633, 20662, - 20692, 20721, 20751, 20780, 20810, 20839, 20869, 20898, 20928, 20957, 20987, 21016, 21046, 21076, 21105, 21135, 21164, 21194, 21223, 21253, - 21282, 21312, 21341, 21371, 21400, 21430, 21459, 21489, 21519, 21548, 21578, 21607, 21637, 21666, 21696, 21725, 21754, 21784, 21813, 21843, - 21873, 21902, 21932, 21962, 21991, 22021, 22050, 22080, 22109, 22138, 22168, 22197, 22227, 22256, 22286, 22316, 22346, 22375, 22405, 22434, - 22464, 22493, 22522, 22552, 22581, 22611, 22640, 22670, 22700, 22730, 22759, 22789, 22818, 22848, 22877, 22906, 22936, 22965, 22994, 23024, - 23054, 23083, 23113, 23143, 23173, 23202, 23232, 23261, 23290, 23320, 23349, 23379, 23408, 23438, 23467, 23497, 23527, 23556, 23586, 23616, - 23645, 23674, 23704, 23733, 23763, 23792, 23822, 23851, 23881, 23910, 23940, 23970, 23999, 24029, 24058, 24088, 24117, 24147, 24176, 24206, - 24235, 24265, 24294, 24324, 24353, 24383, 24413, 24442, 24472, 24501, 24531, 24560, 24590, 24619, 24648, 24678, 24707, 24737, 24767, 24796, - 24826, 24856, 24885, 24915, 24944, 24974, 25003, 25032, 25062, 25091, 25121, 25150, 25180, 25210, 25240, 25269, 25299, 25328, 25358, 25387, - 25416, 25446, 25475, 25505, 25534, 25564, 25594, 25624, 25653, 25683, 25712, 25742, 25771, 25800, 25830, 25859, 25888, 25918, 25948, 25977, - 26007, 26037, 26067, 26096, 26126, 26155, 26184, 26214, 26243, 26272, 26302, 26332, 26361, 26391, 26421, 26451, 26480, 26510, 26539, 26568, - 26598, 26627, 26656, 26686, 26715, 26745, 26775, 26805, 26834, 26864, 26893, 26923, 26952, 26982, 27011, 27041, 27070, 27099, 27129, 27159, - 27188, 27218, 27248, 27277, 27307, 27336, 27366, 27395, 27425, 27454, 27484, 27513, 27542, 27572, 27602, 27631, 27661, 27691, 27720, 27750, - 27779, 27809, 27838, 27868, 27897, 27926, 27956, 27985, 28015, 28045, 28074, 28104, 28134, 28163, 28193, 28222, 28252, 28281, 28310, 28340, - 28369, 28399, 28428, 28458, 28488, 28517, 28547, 28577, - // From 1356 - 28607, 28636, 28665, 28695, 28724, 28754, 28783, 28813, 28843, 28872, 28901, 28931, 28960, 28990, 29019, 29049, 29078, 29108, 29137, 29167, - 29196, 29226, 29255, 29285, 29315, 29345, 29375, 29404, 29434, 29463, 29492, 29522, 29551, 29580, 29610, 29640, 29669, 29699, 29729, 29759, - 29788, 29818, 29847, 29876, 29906, 29935, 29964, 29994, 30023, 30053, 30082, 30112, 30141, 30171, 30200, 30230, 30259, 30289, 30318, 30348, - 30378, 30408, 30437, 30467, 30496, 30526, 30555, 30585, 30614, 30644, 30673, 30703, 30732, 30762, 30791, 30821, 30850, 30880, 30909, 30939, - 30968, 30998, 31027, 31057, 31086, 31116, 31145, 31175, 31204, 31234, 31263, 31293, 31322, 31352, 31381, 31411, 31441, 31471, 31500, 31530, - 31559, 31589, 31618, 31648, 31676, 31706, 31736, 31766, 31795, 31825, 31854, 31884, 31913, 31943, 31972, 32002, 32031, 32061, 32090, 32120, - 32150, 32180, 32209, 32239, 32268, 32298, 32327, 32357, 32386, 32416, 32445, 32475, 32504, 32534, 32563, 32593, 32622, 32652, 32681, 32711, - 32740, 32770, 32799, 32829, 32858, 32888, 32917, 32947, 32976, 33006, 33035, 33065, 33094, 33124, 33153, 33183, 33213, 33243, 33272, 33302, - 33331, 33361, 33390, 33420, 33450, 33479, 33509, 33539, 33568, 33598, 33627, 33657, 33686, 33716, 33745, 33775, 33804, 33834, 33863, 33893, - 33922, 33952, 33981, 34011, 34040, 34069, 34099, 34128, 34158, 34187, 34217, 34247, 34277, 34306, 34336, 34365, 34395, 34424, 34454, 34483, - 34512, 34542, 34571, 34601, 34631, 34660, 34690, 34719, 34749, 34778, 34808, 34837, 34867, 34896, 34926, 34955, 34985, 35015, 35044, 35074, - 35103, 35133, 35162, 35192, 35222, 35251, 35280, 35310, 35340, 35370, 35399, 35429, 35458, 35488, 35517, 35547, 35576, 35605, 35635, 35665, - 35694, 35723, 35753, 35782, 35811, 35841, 35871, 35901, 35930, 35960, 35989, 36019, 36048, 36078, 36107, 36136, 36166, 36195, 36225, 36254, - 36284, 36314, 36343, 36373, 36403, 36433, 36462, 36492, 36521, 36551, 36580, 36610, 36639, 36669, 36698, 36728, 36757, 36786, 36816, 36845, - 36875, 36904, 36934, 36963, 36993, 37022, 37052, 37081, 37111, 37141, 37170, 37200, 37229, 37259, 37288, 37318, 37347, 37377, 37406, 37436, - 37465, 37495, 37524, 37554, 37584, 37613, 37643, 37672, 37701, 37731, 37760, 37790, 37819, 37849, 37878, 37908, 37938, 37967, 37997, 38027, - 38056, 38085, 38115, 38144, 38174, 38203, 38233, 38262, 38292, 38322, 38351, 38381, 38410, 38440, 38469, 38499, 38528, 38558, 38587, 38617, - 38646, 38676, 38705, 38735, 38764, 38794, 38823, 38853, 38882, 38912, 38941, 38971, 39001, 39030, 39059, 39089, 39118, 39148, 39178, 39208, - 39237, 39267, 39297, 39326, 39355, 39385, 39414, 39444, 39473, 39503, 39532, 39562, 39592, 39621, 39650, 39680, 39709, 39739, 39768, 39798, - 39827, 39857, 39886, 39916, 39946, 39975, 40005, 40035, 40064, 40094, 40123, 40153, 40182, 40212, 40241, 40271, 40300, 40330, 40359, 40389, - 40418, 40448, 40477, 40507, 40536, 40566, 40595, 40625, 40655, 40685, 40714, 40744, 40773, 40803, 40832, 40862, 40892, 40921, 40951, 40980, - 41009, 41039, 41068, 41098, 41127, 41157, 41186, 41216, 41245, 41275, 41304, 41334, 41364, 41393, 41422, 41452, 41481, 41511, 41540, 41570, - 41599, 41629, 41658, 41688, 41718, 41748, 41777, 41807, 41836, 41865, 41894, 41924, 41953, 41983, 42012, 42042, 42072, 42102, 42131, 42161, - 42190, 42220, 42249, 42279, 42308, 42337, 42367, 42397, 42426, 42456, 42485, 42515, 42545, 42574, 42604, 42633, 42662, 42692, 42721, 42751, - 42780, 42810, 42839, 42869, 42899, 42929, 42958, 42988, 43017, 43046, 43076, 43105, 43135, 43164, 43194, 43223, 43253, 43283, 43312, 43342, - 43371, 43401, 43430, 43460, 43489, 43519, 43548, 43578, 43607, 43637, 43666, 43696, 43726, 43755, 43785, 43814, 43844, 43873, 43903, 43932, - 43962, 43991, 44021, 44050, 44080, 44109, 44139, 44169, 44198, 44228, 44258, 44287, 44317, 44346, 44375, 44405, 44434, 44464, 44493, 44523, - 44553, 44582, 44612, 44641, 44671, 44700, 44730, 44759, 44788, 44818, 44847, 44877, 44906, 44936, 44966, 44996, 45025, 45055, 45084, 45114, - 45143, 45172, 45202, 45231, 45261, 45290, 45320, 45350, 45380, 45409, 45439, 45468, 45498, 45527, 45556, 45586, 45615, 45644, 45674, 45704, - 45733, 45763, 45793, 45823, 45852, 45882, 45911, 45940, 45970, 45999, 46028, 46058, 46088, 46117, 46147, 46177, 46206, 46236, 46265, 46295, - 46324, 46354, 46383, 46413, 46442, 46472, 46501, 46531, 46560, 46590, 46620, 46649, 46679, 46708, 46738, 46767, 46797, 46826, 46856, 46885, - 46915, 46944, 46974, 47003, 47033, 47063, 47092, 47122, 47151, 47181, 47210, 47240, 47269, 47298, 47328, 47357, 47387, 47417, 47446, 47476, - 47506, 47535, 47565, 47594, 47624, 47653, 47682, 47712, 47741, 47771, 47800, 47830, 47860, 47890, 47919, 47949, 47978, 48008, 48037, 48066, - 48096, 48125, 48155, 48184, 48214, 48244, 48273, 48303, 48333, 48362, 48392, 48421, 48450, 48480, 48509, 48538, 48568, 48598, 48627, 48657, - 48687, 48717, 48746, 48776, 48805, 48834, 48864, 48893, 48922, 48952, 48982, 49011, 49041, 49071, 49100, 49130, 49160, 49189, 49218, 49248, - 49277, 49306, 49336, 49365, 49395, 49425, 49455, 49484, 49514, 49543, 49573, 49602, 49632, 49661, 49690, 49720, 49749, 49779, 49809, 49838, - 49868, 49898, 49927, 49957, 49986, 50016, 50045, 50075, 50104, 50133, 50163, 50192, 50222, 50252, 50281, 50311, 50340, 50370, 50400, 50429, - 50459, 50488, 50518, 50547, 50576, 50606, 50635, 50665, 50694, 50724, 50754, 50784, 50813, 50843, 50872, 50902, 50931, 50960, 50990, 51019, - 51049, 51078, 51108, 51138, 51167, 51197, 51227, 51256, 51286, 51315, 51345, 51374, 51403, 51433, 51462, 51492, 51522, 51552, 51582, 51611, - 51641, 51670, 51699, 51729, 51758, 51787, 51816, 51846, 51876, 51906, 51936, 51965, 51995, 52025, 52054, 52083, 52113, 52142, 52171, 52200, - 52230, 52260, 52290, 52319, 52349, 52379, 52408, 52438, 52467, 52497, 52526, 52555, 52585, 52614, 52644, 52673, 52703, 52733, 52762, 52792, - 52822, 52851, 52881, 52910, 52939, 52969, 52998, 53028, 53057, 53087, 53116, 53146, 53176, 53205, 53235, 53264, 53294, 53324, 53353, 53383, - 53412, 53441, 53471, 53500, 53530, 53559, 53589, 53619, 53648, 53678, 53708, 53737, 53767, 53796, 53825, 53855, 53884, 53913, 53943, 53973, - 54003, 54032, 54062, 54092, 54121, 54151, 54180, 54209, 54239, 54268, 54297, 54327, 54357, 54387, 54416, 54446, 54476, 54505, 54535, 54564, - 54593, 54623, 54652, 54681, 54711, 54741, 54770, 54800, 54830, 54859, 54889, 54919, 54948, 54977, 55007, 55036, 55066, 55095, 55125, 55154, - 55184, 55213, 55243, 55273, 55302, 55332, 55361, 55391, 55420, 55450, 55479, 55508, 55538, 55567, 55597, 55627, 55657, 55686, 55716, 55745, - 55775, 55804, 55834, 55863, 55892, 55922, 55951, 55981, 56011, 56040, 56070, 56100, 56129, 56159, 56188, 56218, 56247, 56276, 56306, 56335, - 56365, 56394, 56424, 56454, 56483, 56513, 56543, 56572, 56601, 56631, 56660, 56690, 56719, 56749, 56778, 56808, 56837, 56867, 56897, 56926, - 56956, 56985, 57015, 57044, 57074, 57103, 57133, 57162, 57192, 57221, 57251, 57280, 57310, 57340, 57369, 57399, 57429, 57458, 57487, 57517, - 57546, 57576, 57605, 57634, 57664, 57694, 57723, 57753, 57783, 57813, 57842, 57871, 57901, 57930, 57959, 57989, 58018, 58048, 58077, 58107, - 58137, 58167, 58196, 58226, 58255, 58285, 58314, 58343, 58373, 58402, 58432, 58461, 58491, 58521, 58551, 58580, 58610, 58639, 58669, 58698, - 58727, 58757, 58786, 58816, 58845, 58875, 58905, 58934, 58964, 58994, 59023, 59053, 59082, 59111, 59141, 59170, 59200, 59229, 59259, 59288, - 59318, 59348, 59377, 59407, 59436, 59466, 59495, 59525, 59554, 59584, 59613, 59643, 59672, 59702, 59731, 59761, 59791, 59820, 59850, 59879, - 59909, 59939, 59968, 59997, 60027, 60056, 60086, 60115, 60145, 60174, 60204, 60234, 60264, 60293, 60323, 60352, 60381, 60411, 60440, 60469, - 60499, 60528, 60558, 60588, 60618, 60648, 60677, 60707, 60736, 60765, 60795, 60824, 60853, 60883, 60912, 60942, 60972, 61002, 61031, 61061, - 61090, 61120, 61149, 61179, 61208, 61237, 61267, 61296, 61326, 61356, 61385, 61415, 61445, 61474, 61504, 61533, 61563, 61592, 61621, 61651, - 61680, 61710, 61739, 61769, 61799, 61828, 61858, 61888, 61917, 61947, 61976, 62006, 62035, 62064, 62094, 62123, 62153, 62182, 62212, 62242, - 62271, 62301, 62331, 62360, 62390, 62419, 62448, 62478, 62507, 62537, 62566, 62596, 62625, 62655, 62685, 62715, 62744, 62774, 62803, 62832, - 62862, 62891, 62921, 62950, 62980, 63009, 63039, 63069, 63099, 63128, 63157, 63187, 63216, 63246, 63275, 63305, 63334, 63363, 63393, 63423, - 63453, 63482, 63512, 63541, 63571, 63600, 63630, 63659, 63689, 63718, 63747, 63777, 63807, 63836, 63866, 63895, 63925, 63955, 63984, 64014, - 64043, 64073, 64102, 64131, 64161, 64190, 64220, 64249, 64279, 64309, 64339, 64368, 64398, 64427, 64457, 64486, 64515, 64545, 64574, 64603, - 64633, 64663, 64692, 64722, 64752, 64782, 64811, 64841, 64870, 64899, 64929, 64958, 64987, 65017, 65047, 65076, 65106, 65136, 65166, 65195, - 65225, 65254, 65283, 65313, 65342, 65371, 65401, 65431, 65460, 65490, 65520, 65549, 65579, 65608, 65638, 65667, 65697, 65726, 65755, 65785, - 65815, 65844, 65874, 65903, 65933, 65963, 65992, 66022, 66051, 66081, 66110, 66140, 66169, 66199, 66228, 66258, 66287, 66317, 66346, 66376, - 66405, 66435, 66465, 66494, 66524, 66553, 66583, 66612, 66641, 66671, 66700, 66730, 66760, 66789, 66819, 66849, 66878, 66908, 66937, 66967, - 66996, 67025, 67055, 67084, 67114, 67143, 67173, 67203, 67233, 67262, 67292, 67321, 67351, 67380, 67409, 67439, 67468, 67497, 67527, 67557, - 67587, 67617, 67646, 67676, 67705, 67735, 67764, 67793, 67823, 67852, 67882, 67911, 67941, 67971, 68000, 68030, 68060, 68089, 68119, 68148, - 68177, 68207, 68236, 68266, 68295, 68325, 68354, 68384, 68414, 68443, 68473, 68502, 68532, 68561, 68591, 68620, 68650, 68679, 68708, 68738, - 68768, 68797, 68827, 68857, 68886, 68916, 68946, 68975, 69004, 69034, 69063, 69092, 69122, 69152, 69181, 69211, 69240, 69270, 69300, 69330, - 69359, 69388, 69418, 69447, 69476, 69506, 69535, 69565, 69595, 69624, 69654, 69684, 69713, 69743, 69772, 69802, 69831, 69861, 69890, 69919, - 69949, 69978, 70008, 70038, 70067, 70097, 70126, 70156, 70186, 70215, 70245, 70274, 70303, 70333, 70362, 70392, 70421, 70451, 70481, 70510, - 70540, 70570, 70599, 70629, 70658, 70687, 70717, 70746, 70776, 70805, 70835, 70864, 70894, 70924, 70954, 70983, 71013, 71042, 71071, 71101, - 71130, 71159, 71189, 71218, 71248, 71278, 71308, 71337, 71367, 71397, 71426, 71455, 71485, 71514, 71543, 71573, 71602, 71632, 71662, 71691, - 71721, 71751, 71781, 71810, 71839, 71869, 71898, 71927, 71957, 71986, 72016, 72046, 72075, 72105, 72135, 72164, 72194, 72223, 72253, 72282, - 72311, 72341, 72370, 72400, 72429, 72459, 72489, 72518, 72548, 72577, 72607, 72637, 72666, 72695, 72725, 72754, 72784, 72813, 72843, 72872, - 72902, 72931, 72961, 72991, 73020, 73050, 73080, 73109, 73139, 73168, 73197, 73227, 73256, 73286, 73315, 73345, 73375, 73404, 73434, 73464, - 73493, 73523, 73552, 73581, 73611, 73640, 73669, 73699, 73729, 73758, 73788, 73818, 73848, 73877, 73907, 73936, 73965, 73995, 74024, 74053, - 74083, 74113, 74142, 74172, 74202, 74231, 74261, 74291, 74320, 74349, 74379, 74408, 74437, 74467, 74497, 74526, 74556, 74586, 74615, 74645, - 74675, 74704, 74733, 74763, 74792, 74822, 74851, 74881, 74910, 74940, 74969, 74999, 75029, 75058, 75088, 75117, 75147, 75176, 75206, 75235, - 75264, 75294, 75323, 75353, 75383, 75412, 75442, 75472, 75501, 75531, 75560, 75590, 75619, 75648, 75678, 75707, 75737, 75766, 75796, 75826, - 75856, 75885, 75915, 75944, 75974, 76003, 76032, 76062, 76091, 76121, 76150, 76180, 76210, 76239, 76269, 76299, 76328, 76358, 76387, 76416, - 76446, 76475, 76505, 76534, 76564, 76593, 76623, 76653, 76682, 76712, 76741, 76771, 76801, 76830, 76859, 76889, 76918, 76948, 76977, 77007, - 77036, 77066, 77096, 77125, 77155, 77185, 77214, 77243, 77273, 77302, 77332, 77361, 77390, 77420, 77450, 77479, 77509, 77539, 77569, 77598, - 77627, 77657, 77686, 77715, 77745, 77774, 77804, 77833, 77863, 77893, 77923, 77952, 77982, 78011, 78041, 78070, 78099, 78129, 78158, 78188, - 78217, 78247, 78277, 78307, 78336, 78366, 78395, 78425, 78454, 78483, 78513, 78542, 78572, 78601, 78631, 78661, 78690, 78720, 78750, 78779, - 78808, 78838, 78867, 78897, 78926, 78956, 78985, 79015, 79044, 79074, 79104, 79133, 79163, 79192, 79222, 79251, 79281, 79310, 79340, 79369, - 79399, 79428, 79458, 79487, 79517, 79546, 79576, 79606, 79635, 79665, 79695, 79724, 79753, 79783, 79812, 79841, 79871, 79900, 79930, 79960, - 79990]; - - -},{"../main":1147,"object-assign":1149}],1147:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Calendars for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var assign = require('object-assign'); - - -function Calendars() { - this.regionalOptions = []; - this.regionalOptions[''] = { - invalidCalendar: 'Calendar {0} not found', - invalidDate: 'Invalid {0} date', - invalidMonth: 'Invalid {0} month', - invalidYear: 'Invalid {0} year', - differentCalendars: 'Cannot mix {0} and {1} dates' - }; - this.local = this.regionalOptions['']; - this.calendars = {}; - this._localCals = {}; -} - -/** Create the calendars plugin. -

Provides support for various world calendars in a consistent manner.

- @class Calendars - @example _exports.instance('julian').newDate(2014, 12, 25) */ -assign(Calendars.prototype, { - - /** Obtain a calendar implementation and localisation. - @memberof Calendars - @param [name='gregorian'] {string} The name of the calendar, e.g. 'gregorian', 'persian', 'islamic'. - @param [language=''] {string} The language code to use for localisation (default is English). - @return {Calendar} The calendar and localisation. - @throws Error if calendar not found. */ - instance: function(name, language) { - name = (name || 'gregorian').toLowerCase(); - language = language || ''; - var cal = this._localCals[name + '-' + language]; - if (!cal && this.calendars[name]) { - cal = new this.calendars[name](language); - this._localCals[name + '-' + language] = cal; - } - if (!cal) { - throw (this.local.invalidCalendar || this.regionalOptions[''].invalidCalendar). - replace(/\{0\}/, name); - } - return cal; - }, - - /** Create a new date - for today if no other parameters given. - @memberof Calendars - @param year {CDate|number} The date to copy or the year for the date. - @param [month] {number} The month for the date. - @param [day] {number} The day for the date. - @param [calendar='gregorian'] {BaseCalendar|string} The underlying calendar or the name of the calendar. - @param [language=''] {string} The language to use for localisation (default English). - @return {CDate} The new date. - @throws Error if an invalid date. */ - newDate: function(year, month, day, calendar, language) { - calendar = (year != null && year.year ? year.calendar() : (typeof calendar === 'string' ? - this.instance(calendar, language) : calendar)) || this.instance(); - return calendar.newDate(year, month, day); - }, - - /** A simple digit substitution function for localising numbers via the Calendar digits option. - @member Calendars - @param digits {string[]} The substitute digits, for 0 through 9. - @return {function} The substitution function. */ - substituteDigits: function(digits) { - return function(value) { - return (value + '').replace(/[0-9]/g, function(digit) { - return digits[digit]; - }); - } - }, - - /** Digit substitution function for localising Chinese style numbers via the Calendar digits option. - @member Calendars - @param digits {string[]} The substitute digits, for 0 through 9. - @param powers {string[]} The characters denoting powers of 10, i.e. 1, 10, 100, 1000. - @return {function} The substitution function. */ - substituteChineseDigits: function(digits, powers) { - return function(value) { - var localNumber = ''; - var power = 0; - while (value > 0) { - var units = value % 10; - localNumber = (units === 0 ? '' : digits[units] + powers[power]) + localNumber; - power++; - value = Math.floor(value / 10); - } - if (localNumber.indexOf(digits[1] + powers[1]) === 0) { - localNumber = localNumber.substr(1); - } - return localNumber || digits[0]; - } - } -}); - -/** Generic date, based on a particular calendar. - @class CDate - @param calendar {BaseCalendar} The underlying calendar implementation. - @param year {number} The year for this date. - @param month {number} The month for this date. - @param day {number} The day for this date. - @return {CDate} The date object. - @throws Error if an invalid date. */ -function CDate(calendar, year, month, day) { - this._calendar = calendar; - this._year = year; - this._month = month; - this._day = day; - if (this._calendar._validateLevel === 0 && - !this._calendar.isValid(this._year, this._month, this._day)) { - throw (_exports.local.invalidDate || _exports.regionalOptions[''].invalidDate). - replace(/\{0\}/, this._calendar.local.name); - } -} - -/** Pad a numeric value with leading zeroes. - @private - @param value {number} The number to format. - @param length {number} The minimum length. - @return {string} The formatted number. */ -function pad(value, length) { - value = '' + value; - return '000000'.substring(0, length - value.length) + value; -} - -assign(CDate.prototype, { - - /** Create a new date. - @memberof CDate - @param [year] {CDate|number} The date to copy or the year for the date (default this date). - @param [month] {number} The month for the date. - @param [day] {number} The day for the date. - @return {CDate} The new date. - @throws Error if an invalid date. */ - newDate: function(year, month, day) { - return this._calendar.newDate((year == null ? this : year), month, day); - }, - - /** Set or retrieve the year for this date. - @memberof CDate - @param [year] {number} The year for the date. - @return {number|CDate} The date's year (if no parameter) or the updated date. - @throws Error if an invalid date. */ - year: function(year) { - return (arguments.length === 0 ? this._year : this.set(year, 'y')); - }, - - /** Set or retrieve the month for this date. - @memberof CDate - @param [month] {number} The month for the date. - @return {number|CDate} The date's month (if no parameter) or the updated date. - @throws Error if an invalid date. */ - month: function(month) { - return (arguments.length === 0 ? this._month : this.set(month, 'm')); - }, - - /** Set or retrieve the day for this date. - @memberof CDate - @param [day] {number} The day for the date. - @return {number|CData} The date's day (if no parameter) or the updated date. - @throws Error if an invalid date. */ - day: function(day) { - return (arguments.length === 0 ? this._day : this.set(day, 'd')); - }, - - /** Set new values for this date. - @memberof CDate - @param year {number} The year for the date. - @param month {number} The month for the date. - @param day {number} The day for the date. - @return {CDate} The updated date. - @throws Error if an invalid date. */ - date: function(year, month, day) { - if (!this._calendar.isValid(year, month, day)) { - throw (_exports.local.invalidDate || _exports.regionalOptions[''].invalidDate). - replace(/\{0\}/, this._calendar.local.name); - } - this._year = year; - this._month = month; - this._day = day; - return this; - }, - - /** Determine whether this date is in a leap year. - @memberof CDate - @return {boolean} true if this is a leap year, false if not. */ - leapYear: function() { - return this._calendar.leapYear(this); - }, - - /** Retrieve the epoch designator for this date, e.g. BCE or CE. - @memberof CDate - @return {string} The current epoch. */ - epoch: function() { - return this._calendar.epoch(this); - }, - - /** Format the year, if not a simple sequential number. - @memberof CDate - @return {string} The formatted year. */ - formatYear: function() { - return this._calendar.formatYear(this); - }, - - /** Retrieve the month of the year for this date, - i.e. the month's position within a numbered year. - @memberof CDate - @return {number} The month of the year: minMonth to months per year. */ - monthOfYear: function() { - return this._calendar.monthOfYear(this); - }, - - /** Retrieve the week of the year for this date. - @memberof CDate - @return {number} The week of the year: 1 to weeks per year. */ - weekOfYear: function() { - return this._calendar.weekOfYear(this); - }, - - /** Retrieve the number of days in the year for this date. - @memberof CDate - @return {number} The number of days in this year. */ - daysInYear: function() { - return this._calendar.daysInYear(this); - }, - - /** Retrieve the day of the year for this date. - @memberof CDate - @return {number} The day of the year: 1 to days per year. */ - dayOfYear: function() { - return this._calendar.dayOfYear(this); - }, - - /** Retrieve the number of days in the month for this date. - @memberof CDate - @return {number} The number of days. */ - daysInMonth: function() { - return this._calendar.daysInMonth(this); - }, - - /** Retrieve the day of the week for this date. - @memberof CDate - @return {number} The day of the week: 0 to number of days - 1. */ - dayOfWeek: function() { - return this._calendar.dayOfWeek(this); - }, - - /** Determine whether this date is a week day. - @memberof CDate - @return {boolean} true if a week day, false if not. */ - weekDay: function() { - return this._calendar.weekDay(this); - }, - - /** Retrieve additional information about this date. - @memberof CDate - @return {object} Additional information - contents depends on calendar. */ - extraInfo: function() { - return this._calendar.extraInfo(this); - }, - - /** Add period(s) to a date. - @memberof CDate - @param offset {number} The number of periods to adjust by. - @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. - @return {CDate} The updated date. */ - add: function(offset, period) { - return this._calendar.add(this, offset, period); - }, - - /** Set a portion of the date. - @memberof CDate - @param value {number} The new value for the period. - @param period {string} One of 'y' for year, 'm' for month, 'd' for day. - @return {CDate} The updated date. - @throws Error if not a valid date. */ - set: function(value, period) { - return this._calendar.set(this, value, period); - }, - - /** Compare this date to another date. - @memberof CDate - @param date {CDate} The other date. - @return {number} -1 if this date is before the other date, - 0 if they are equal, or +1 if this date is after the other date. */ - compareTo: function(date) { - if (this._calendar.name !== date._calendar.name) { - throw (_exports.local.differentCalendars || _exports.regionalOptions[''].differentCalendars). - replace(/\{0\}/, this._calendar.local.name).replace(/\{1\}/, date._calendar.local.name); - } - var c = (this._year !== date._year ? this._year - date._year : - this._month !== date._month ? this.monthOfYear() - date.monthOfYear() : - this._day - date._day); - return (c === 0 ? 0 : (c < 0 ? -1 : +1)); - }, - - /** Retrieve the calendar backing this date. - @memberof CDate - @return {BaseCalendar} The calendar implementation. */ - calendar: function() { - return this._calendar; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof CDate - @return {number} The equivalent Julian date. */ - toJD: function() { - return this._calendar.toJD(this); - }, - - /** Create a new date from a Julian date. - @memberof CDate - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - return this._calendar.fromJD(jd); - }, - - /** Convert this date to a standard (Gregorian) JavaScript Date. - @memberof CDate - @return {Date} The equivalent JavaScript date. */ - toJSDate: function() { - return this._calendar.toJSDate(this); - }, - - /** Create a new date from a standard (Gregorian) JavaScript Date. - @memberof CDate - @param jsd {Date} The JavaScript date to convert. - @return {CDate} The equivalent date. */ - fromJSDate: function(jsd) { - return this._calendar.fromJSDate(jsd); - }, - - /** Convert to a string for display. - @memberof CDate - @return {string} This date as a string. */ - toString: function() { - return (this.year() < 0 ? '-' : '') + pad(Math.abs(this.year()), 4) + - '-' + pad(this.month(), 2) + '-' + pad(this.day(), 2); - } -}); - -/** Basic functionality for all calendars. - Other calendars should extend this: -
OtherCalendar.prototype = new BaseCalendar;
- @class BaseCalendar */ -function BaseCalendar() { - this.shortYearCutoff = '+10'; -} - -assign(BaseCalendar.prototype, { - _validateLevel: 0, // "Stack" to turn validation on/off - - /** Create a new date within this calendar - today if no parameters given. - @memberof BaseCalendar - @param year {CDate|number} The date to duplicate or the year for the date. - @param [month] {number} The month for the date. - @param [day] {number} The day for the date. - @return {CDate} The new date. - @throws Error if not a valid date or a different calendar used. */ - newDate: function(year, month, day) { - if (year == null) { - return this.today(); - } - if (year.year) { - this._validate(year, month, day, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - day = year.day(); - month = year.month(); - year = year.year(); - } - return new CDate(this, year, month, day); - }, - - /** Create a new date for today. - @memberof BaseCalendar - @return {CDate} Today's date. */ - today: function() { - return this.fromJSDate(new Date()); - }, - - /** Retrieve the epoch designator for this date. - @memberof BaseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {string} The current epoch. - @throws Error if an invalid year or a different calendar used. */ - epoch: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, - _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); - return (date.year() < 0 ? this.local.epochs[0] : this.local.epochs[1]); - }, - - /** Format the year, if not a simple sequential number - @memberof BaseCalendar - @param year {CDate|number} The date to format or the year to format. - @return {string} The formatted year. - @throws Error if an invalid year or a different calendar used. */ - formatYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, - _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); - return (date.year() < 0 ? '-' : '') + pad(Math.abs(date.year()), 4) - }, - - /** Retrieve the number of months in a year. - @memberof BaseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of months. - @throws Error if an invalid year or a different calendar used. */ - monthsInYear: function(year) { - this._validate(year, this.minMonth, this.minDay, - _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); - return 12; - }, - - /** Calculate the month's ordinal position within the year - - for those calendars that don't start at month 1! - @memberof BaseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param month {number} The month to examine. - @return {number} The ordinal position, starting from minMonth. - @throws Error if an invalid year/month or a different calendar used. */ - monthOfYear: function(year, month) { - var date = this._validate(year, month, this.minDay, - _exports.local.invalidMonth || _exports.regionalOptions[''].invalidMonth); - return (date.month() + this.monthsInYear(date) - this.firstMonth) % - this.monthsInYear(date) + this.minMonth; - }, - - /** Calculate actual month from ordinal position, starting from minMonth. - @memberof BaseCalendar - @param year {number} The year to examine. - @param ord {number} The month's ordinal position. - @return {number} The month's number. - @throws Error if an invalid year/month. */ - fromMonthOfYear: function(year, ord) { - var m = (ord + this.firstMonth - 2 * this.minMonth) % - this.monthsInYear(year) + this.minMonth; - this._validate(year, m, this.minDay, - _exports.local.invalidMonth || _exports.regionalOptions[''].invalidMonth); - return m; - }, - - /** Retrieve the number of days in a year. - @memberof BaseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {number} The number of days. - @throws Error if an invalid year or a different calendar used. */ - daysInYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, - _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); - return (this.leapYear(date) ? 366 : 365); - }, - - /** Retrieve the day of the year for a date. - @memberof BaseCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The day of the year. - @throws Error if an invalid date or a different calendar used. */ - dayOfYear: function(year, month, day) { - var date = this._validate(year, month, day, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - return date.toJD() - this.newDate(date.year(), - this.fromMonthOfYear(date.year(), this.minMonth), this.minDay).toJD() + 1; - }, - - /** Retrieve the number of days in a week. - @memberof BaseCalendar - @return {number} The number of days. */ - daysInWeek: function() { - return 7; - }, - - /** Retrieve the day of the week for a date. - @memberof BaseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The day of the week: 0 to number of days - 1. - @throws Error if an invalid date or a different calendar used. */ - dayOfWeek: function(year, month, day) { - var date = this._validate(year, month, day, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - return (Math.floor(this.toJD(date)) + 2) % this.daysInWeek(); - }, - - /** Retrieve additional information about a date. - @memberof BaseCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {object} Additional information - contents depends on calendar. - @throws Error if an invalid date or a different calendar used. */ - extraInfo: function(year, month, day) { - this._validate(year, month, day, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - return {}; - }, - - /** Add period(s) to a date. - Cater for no year zero. - @memberof BaseCalendar - @param date {CDate} The starting date. - @param offset {number} The number of periods to adjust by. - @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. - @return {CDate} The updated date. - @throws Error if a different calendar used. */ - add: function(date, offset, period) { - this._validate(date, this.minMonth, this.minDay, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - return this._correctAdd(date, this._add(date, offset, period), offset, period); - }, - - /** Add period(s) to a date. - @memberof BaseCalendar - @private - @param date {CDate} The starting date. - @param offset {number} The number of periods to adjust by. - @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. - @return {CDate} The updated date. */ - _add: function(date, offset, period) { - this._validateLevel++; - if (period === 'd' || period === 'w') { - var jd = date.toJD() + offset * (period === 'w' ? this.daysInWeek() : 1); - var d = date.calendar().fromJD(jd); - this._validateLevel--; - return [d.year(), d.month(), d.day()]; - } - try { - var y = date.year() + (period === 'y' ? offset : 0); - var m = date.monthOfYear() + (period === 'm' ? offset : 0); - var d = date.day();// + (period === 'd' ? offset : 0) + - //(period === 'w' ? offset * this.daysInWeek() : 0); - var resyncYearMonth = function(calendar) { - while (m < calendar.minMonth) { - y--; - m += calendar.monthsInYear(y); - } - var yearMonths = calendar.monthsInYear(y); - while (m > yearMonths - 1 + calendar.minMonth) { - y++; - m -= yearMonths; - yearMonths = calendar.monthsInYear(y); - } - }; - if (period === 'y') { - if (date.month() !== this.fromMonthOfYear(y, m)) { // Hebrew - m = this.newDate(y, date.month(), this.minDay).monthOfYear(); - } - m = Math.min(m, this.monthsInYear(y)); - d = Math.min(d, this.daysInMonth(y, this.fromMonthOfYear(y, m))); - } - else if (period === 'm') { - resyncYearMonth(this); - d = Math.min(d, this.daysInMonth(y, this.fromMonthOfYear(y, m))); - } - var ymd = [y, this.fromMonthOfYear(y, m), d]; - this._validateLevel--; - return ymd; - } - catch (e) { - this._validateLevel--; - throw e; - } - }, - - /** Correct a candidate date after adding period(s) to a date. - Handle no year zero if necessary. - @memberof BaseCalendar - @private - @param date {CDate} The starting date. - @param ymd {number[]} The added date. - @param offset {number} The number of periods to adjust by. - @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. - @return {CDate} The updated date. */ - _correctAdd: function(date, ymd, offset, period) { - if (!this.hasYearZero && (period === 'y' || period === 'm')) { - if (ymd[0] === 0 || // In year zero - (date.year() > 0) !== (ymd[0] > 0)) { // Crossed year zero - var adj = {y: [1, 1, 'y'], m: [1, this.monthsInYear(-1), 'm'], - w: [this.daysInWeek(), this.daysInYear(-1), 'd'], - d: [1, this.daysInYear(-1), 'd']}[period]; - var dir = (offset < 0 ? -1 : +1); - ymd = this._add(date, offset * adj[0] + dir * adj[1], adj[2]); - } - } - return date.date(ymd[0], ymd[1], ymd[2]); - }, - - /** Set a portion of the date. - @memberof BaseCalendar - @param date {CDate} The starting date. - @param value {number} The new value for the period. - @param period {string} One of 'y' for year, 'm' for month, 'd' for day. - @return {CDate} The updated date. - @throws Error if an invalid date or a different calendar used. */ - set: function(date, value, period) { - this._validate(date, this.minMonth, this.minDay, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - var y = (period === 'y' ? value : date.year()); - var m = (period === 'm' ? value : date.month()); - var d = (period === 'd' ? value : date.day()); - if (period === 'y' || period === 'm') { - d = Math.min(d, this.daysInMonth(y, m)); - } - return date.date(y, m, d); - }, - - /** Determine whether a date is valid for this calendar. - @memberof BaseCalendar - @param year {number} The year to examine. - @param month {number} The month to examine. - @param day {number} The day to examine. - @return {boolean} true if a valid date, false if not. */ - isValid: function(year, month, day) { - this._validateLevel++; - var valid = (this.hasYearZero || year !== 0); - if (valid) { - var date = this.newDate(year, month, this.minDay); - valid = (month >= this.minMonth && month - this.minMonth < this.monthsInYear(date)) && - (day >= this.minDay && day - this.minDay < this.daysInMonth(date)); - } - this._validateLevel--; - return valid; - }, - - /** Convert the date to a standard (Gregorian) JavaScript Date. - @memberof BaseCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {Date} The equivalent JavaScript date. - @throws Error if an invalid date or a different calendar used. */ - toJSDate: function(year, month, day) { - var date = this._validate(year, month, day, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - return _exports.instance().fromJD(this.toJD(date)).toJSDate(); - }, - - /** Convert the date from a standard (Gregorian) JavaScript Date. - @memberof BaseCalendar - @param jsd {Date} The JavaScript date. - @return {CDate} The equivalent calendar date. */ - fromJSDate: function(jsd) { - return this.fromJD(_exports.instance().fromJSDate(jsd).toJD()); - }, - - /** Check that a candidate date is from the same calendar and is valid. - @memberof BaseCalendar - @private - @param year {CDate|number} The date to validate or the year to validate. - @param [month] {number} The month to validate. - @param [day] {number} The day to validate. - @param error {string} Rrror message if invalid. - @throws Error if different calendars used or invalid date. */ - _validate: function(year, month, day, error) { - if (year.year) { - if (this._validateLevel === 0 && this.name !== year.calendar().name) { - throw (_exports.local.differentCalendars || _exports.regionalOptions[''].differentCalendars). - replace(/\{0\}/, this.local.name).replace(/\{1\}/, year.calendar().local.name); - } - return year; - } - try { - this._validateLevel++; - if (this._validateLevel === 1 && !this.isValid(year, month, day)) { - throw error.replace(/\{0\}/, this.local.name); - } - var date = this.newDate(year, month, day); - this._validateLevel--; - return date; - } - catch (e) { - this._validateLevel--; - throw e; - } - } -}); - -/** Implementation of the Proleptic Gregorian Calendar. - See http://en.wikipedia.org/wiki/Gregorian_calendar - and http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar. - @class GregorianCalendar - @augments BaseCalendar - @param [language=''] {string} The language code (default English) for localisation. */ -function GregorianCalendar(language) { - this.local = this.regionalOptions[language] || this.regionalOptions['']; -} - -GregorianCalendar.prototype = new BaseCalendar; - -assign(GregorianCalendar.prototype, { - /** The calendar name. - @memberof GregorianCalendar */ - name: 'Gregorian', - /** Julian date of start of Gregorian epoch: 1 January 0001 CE. - @memberof GregorianCalendar */ - jdEpoch: 1721425.5, - /** Days per month in a common year. - @memberof GregorianCalendar */ - daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - /** true if has a year zero, false if not. - @memberof GregorianCalendar */ - hasYearZero: false, - /** The minimum month number. - @memberof GregorianCalendar */ - minMonth: 1, - /** The first month in the year. - @memberof GregorianCalendar */ - firstMonth: 1, - /** The minimum day number. - @memberof GregorianCalendar */ - minDay: 1, - - /** Localisations for the plugin. - Entries are objects indexed by the language code ('' being the default US/English). - Each object has the following attributes. - @memberof GregorianCalendar - @property name {string} The calendar name. - @property epochs {string[]} The epoch names. - @property monthNames {string[]} The long names of the months of the year. - @property monthNamesShort {string[]} The short names of the months of the year. - @property dayNames {string[]} The long names of the days of the week. - @property dayNamesShort {string[]} The short names of the days of the week. - @property dayNamesMin {string[]} The minimal names of the days of the week. - @property dateFormat {string} The date format for this calendar. - See the options on formatDate for details. - @property firstDay {number} The number of the first day of the week, starting at 0. - @property isRTL {number} true if this localisation reads right-to-left. */ - regionalOptions: { // Localisations - '': { - name: 'Gregorian', - epochs: ['BCE', 'CE'], - monthNames: ['January', 'February', 'March', 'April', 'May', 'June', - 'July', 'August', 'September', 'October', 'November', 'December'], - monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], - dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], - digits: null, - dateFormat: 'mm/dd/yyyy', - firstDay: 0, - isRTL: false - } - }, - - /** Determine whether this date is in a leap year. - @memberof GregorianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @return {boolean} true if this is a leap year, false if not. - @throws Error if an invalid year or a different calendar used. */ - leapYear: function(year) { - var date = this._validate(year, this.minMonth, this.minDay, - _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); - var year = date.year() + (date.year() < 0 ? 1 : 0); // No year zero - return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); - }, - - /** Determine the week of the year for a date - ISO 8601. - @memberof GregorianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {number} The week of the year, starting from 1. - @throws Error if an invalid date or a different calendar used. */ - weekOfYear: function(year, month, day) { - // Find Thursday of this week starting on Monday - var checkDate = this.newDate(year, month, day); - checkDate.add(4 - (checkDate.dayOfWeek() || 7), 'd'); - return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; - }, - - /** Retrieve the number of days in a month. - @memberof GregorianCalendar - @param year {CDate|number} The date to examine or the year of the month. - @param [month] {number} The month. - @return {number} The number of days in this month. - @throws Error if an invalid month/year or a different calendar used. */ - daysInMonth: function(year, month) { - var date = this._validate(year, month, this.minDay, - _exports.local.invalidMonth || _exports.regionalOptions[''].invalidMonth); - return this.daysPerMonth[date.month() - 1] + - (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); - }, - - /** Determine whether this date is a week day. - @memberof GregorianCalendar - @param year {CDate|number} The date to examine or the year to examine. - @param [month] {number} The month to examine. - @param [day] {number} The day to examine. - @return {boolean} true if a week day, false if not. - @throws Error if an invalid date or a different calendar used. */ - weekDay: function(year, month, day) { - return (this.dayOfWeek(year, month, day) || 7) < 6; - }, - - /** Retrieve the Julian date equivalent for this date, - i.e. days since January 1, 4713 BCE Greenwich noon. - @memberof GregorianCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {number} The equivalent Julian date. - @throws Error if an invalid date or a different calendar used. */ - toJD: function(year, month, day) { - var date = this._validate(year, month, day, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - year = date.year(); - month = date.month(); - day = date.day(); - if (year < 0) { year++; } // No year zero - // Jean Meeus algorithm, "Astronomical Algorithms", 1991 - if (month < 3) { - month += 12; - year--; - } - var a = Math.floor(year / 100); - var b = 2 - a + Math.floor(a / 4); - return Math.floor(365.25 * (year + 4716)) + - Math.floor(30.6001 * (month + 1)) + day + b - 1524.5; - }, - - /** Create a new date from a Julian date. - @memberof GregorianCalendar - @param jd {number} The Julian date to convert. - @return {CDate} The equivalent date. */ - fromJD: function(jd) { - // Jean Meeus algorithm, "Astronomical Algorithms", 1991 - var z = Math.floor(jd + 0.5); - var a = Math.floor((z - 1867216.25) / 36524.25); - a = z + 1 + a - Math.floor(a / 4); - var b = a + 1524; - var c = Math.floor((b - 122.1) / 365.25); - var d = Math.floor(365.25 * c); - var e = Math.floor((b - d) / 30.6001); - var day = b - d - Math.floor(e * 30.6001); - var month = e - (e > 13.5 ? 13 : 1); - var year = c - (month > 2.5 ? 4716 : 4715); - if (year <= 0) { year--; } // No year zero - return this.newDate(year, month, day); - }, - - /** Convert this date to a standard (Gregorian) JavaScript Date. - @memberof GregorianCalendar - @param year {CDate|number} The date to convert or the year to convert. - @param [month] {number} The month to convert. - @param [day] {number} The day to convert. - @return {Date} The equivalent JavaScript date. - @throws Error if an invalid date or a different calendar used. */ - toJSDate: function(year, month, day) { - var date = this._validate(year, month, day, - _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); - var jsd = new Date(date.year(), date.month() - 1, date.day()); - jsd.setHours(0); - jsd.setMinutes(0); - jsd.setSeconds(0); - jsd.setMilliseconds(0); - // Hours may be non-zero on daylight saving cut-over: - // > 12 when midnight changeover, but then cannot generate - // midnight datetime, so jump to 1AM, otherwise reset. - jsd.setHours(jsd.getHours() > 12 ? jsd.getHours() + 2 : 0); - return jsd; - }, - - /** Create a new date from a standard (Gregorian) JavaScript Date. - @memberof GregorianCalendar - @param jsd {Date} The JavaScript date to convert. - @return {CDate} The equivalent date. */ - fromJSDate: function(jsd) { - return this.newDate(jsd.getFullYear(), jsd.getMonth() + 1, jsd.getDate()); - } -}); - -// Singleton manager -var _exports = module.exports = new Calendars(); - -// Date template -_exports.cdate = CDate; - -// Base calendar template -_exports.baseCalendar = BaseCalendar; - -// Gregorian calendar implementation -_exports.calendars.gregorian = GregorianCalendar; - - -},{"object-assign":1149}],1148:[function(require,module,exports){ -/* - * World Calendars - * https://github.com/alexcjohnson/world-calendars - * - * Batch-converted from kbwood/calendars - * Many thanks to Keith Wood and all of the contributors to the original project! - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -/* http://keith-wood.name/calendars.html - Calendars extras for jQuery v2.0.2. - Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. - Available under the MIT (http://keith-wood.name/licence.html) license. - Please attribute the author if you use it. */ - -var assign = require('object-assign'); -var main = require('./main'); - - -assign(main.regionalOptions[''], { - invalidArguments: 'Invalid arguments', - invalidFormat: 'Cannot format a date from another calendar', - missingNumberAt: 'Missing number at position {0}', - unknownNameAt: 'Unknown name at position {0}', - unexpectedLiteralAt: 'Unexpected literal at position {0}', - unexpectedText: 'Additional text found at end' -}); -main.local = main.regionalOptions['']; - -assign(main.cdate.prototype, { - - /** Format this date. - Found in the jquery.calendars.plus.js module. - @memberof CDate - @param [format] {string} The date format to use (see formatDate). - @param [settings] {object} Options for the formatDate function. - @return {string} The formatted date. */ - formatDate: function(format, settings) { - if (typeof format !== 'string') { - settings = format; - format = ''; - } - return this._calendar.formatDate(format || '', this, settings); - } -}); - -assign(main.baseCalendar.prototype, { - - UNIX_EPOCH: main.instance().newDate(1970, 1, 1).toJD(), - SECS_PER_DAY: 24 * 60 * 60, - TICKS_EPOCH: main.instance().jdEpoch, // 1 January 0001 CE - TICKS_PER_DAY: 24 * 60 * 60 * 10000000, - - /** Date form for ATOM (RFC 3339/ISO 8601). - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - ATOM: 'yyyy-mm-dd', - /** Date form for cookies. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - COOKIE: 'D, dd M yyyy', - /** Date form for full date. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - FULL: 'DD, MM d, yyyy', - /** Date form for ISO 8601. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - ISO_8601: 'yyyy-mm-dd', - /** Date form for Julian date. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - JULIAN: 'J', - /** Date form for RFC 822. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - RFC_822: 'D, d M yy', - /** Date form for RFC 850. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - RFC_850: 'DD, dd-M-yy', - /** Date form for RFC 1036. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - RFC_1036: 'D, d M yy', - /** Date form for RFC 1123. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - RFC_1123: 'D, d M yyyy', - /** Date form for RFC 2822. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - RFC_2822: 'D, d M yyyy', - /** Date form for RSS (RFC 822). - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - RSS: 'D, d M yy', - /** Date form for Windows ticks. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - TICKS: '!', - /** Date form for Unix timestamp. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - TIMESTAMP: '@', - /** Date form for W3c (ISO 8601). - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar */ - W3C: 'yyyy-mm-dd', - - /** Format a date object into a string value. - The format can be combinations of the following: -
    -
  • d - day of month (no leading zero)
  • -
  • dd - day of month (two digit)
  • -
  • o - day of year (no leading zeros)
  • -
  • oo - day of year (three digit)
  • -
  • D - day name short
  • -
  • DD - day name long
  • -
  • w - week of year (no leading zero)
  • -
  • ww - week of year (two digit)
  • -
  • m - month of year (no leading zero)
  • -
  • mm - month of year (two digit)
  • -
  • M - month name short
  • -
  • MM - month name long
  • -
  • yy - year (two digit)
  • -
  • yyyy - year (four digit)
  • -
  • YYYY - formatted year
  • -
  • J - Julian date (days since January 1, 4713 BCE Greenwich noon)
  • -
  • @ - Unix timestamp (s since 01/01/1970)
  • -
  • ! - Windows ticks (100ns since 01/01/0001)
  • -
  • '...' - literal text
  • -
  • '' - single quote
  • -
- Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar - @param [format] {string} The desired format of the date (defaults to calendar format). - @param date {CDate} The date value to format. - @param [settings] {object} Addition options, whose attributes include: - @property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday. - @property [dayNames] {string[]} Names of the days from Sunday. - @property [monthNamesShort] {string[]} Abbreviated names of the months. - @property [monthNames] {string[]} Names of the months. - @property [calculateWeek] {CalendarsPickerCalculateWeek} Function that determines week of the year. - @property [localNumbers=false] {boolean} true to localise numbers (if available), - false to use normal Arabic numerals. - @return {string} The date in the above format. - @throws Errors if the date is from a different calendar. */ - formatDate: function(format, date, settings) { - if (typeof format !== 'string') { - settings = date; - date = format; - format = ''; - } - if (!date) { - return ''; - } - if (date.calendar() !== this) { - throw main.local.invalidFormat || main.regionalOptions[''].invalidFormat; - } - format = format || this.local.dateFormat; - settings = settings || {}; - var dayNamesShort = settings.dayNamesShort || this.local.dayNamesShort; - var dayNames = settings.dayNames || this.local.dayNames; - var monthNumbers = settings.monthNumbers || this.local.monthNumbers; - var monthNamesShort = settings.monthNamesShort || this.local.monthNamesShort; - var monthNames = settings.monthNames || this.local.monthNames; - var calculateWeek = settings.calculateWeek || this.local.calculateWeek; - // Check whether a format character is doubled - var doubled = function(match, step) { - var matches = 1; - while (iFormat + matches < format.length && format.charAt(iFormat + matches) === match) { - matches++; - } - iFormat += matches - 1; - return Math.floor(matches / (step || 1)) > 1; - }; - // Format a number, with leading zeroes if necessary - var formatNumber = function(match, value, len, step) { - var num = '' + value; - if (doubled(match, step)) { - while (num.length < len) { - num = '0' + num; - } - } - return num; - }; - // Format a name, short or long as requested - var formatName = function(match, value, shortNames, longNames) { - return (doubled(match) ? longNames[value] : shortNames[value]); - }; - // Format month number - // (e.g. Chinese calendar needs to account for intercalary months) - var calendar = this; - var formatMonth = function(date) { - return (typeof monthNumbers === 'function') ? - monthNumbers.call(calendar, date, doubled('m')) : - localiseNumbers(formatNumber('m', date.month(), 2)); - }; - // Format a month name, short or long as requested - var formatMonthName = function(date, useLongName) { - if (useLongName) { - return (typeof monthNames === 'function') ? - monthNames.call(calendar, date) : - monthNames[date.month() - calendar.minMonth]; - } else { - return (typeof monthNamesShort === 'function') ? - monthNamesShort.call(calendar, date) : - monthNamesShort[date.month() - calendar.minMonth]; - } - }; - // Localise numbers if requested and available - var digits = this.local.digits; - var localiseNumbers = function(value) { - return (settings.localNumbers && digits ? digits(value) : value); - }; - var output = ''; - var literal = false; - for (var iFormat = 0; iFormat < format.length; iFormat++) { - if (literal) { - if (format.charAt(iFormat) === "'" && !doubled("'")) { - literal = false; - } - else { - output += format.charAt(iFormat); - } - } - else { - switch (format.charAt(iFormat)) { - case 'd': output += localiseNumbers(formatNumber('d', date.day(), 2)); break; - case 'D': output += formatName('D', date.dayOfWeek(), - dayNamesShort, dayNames); break; - case 'o': output += formatNumber('o', date.dayOfYear(), 3); break; - case 'w': output += formatNumber('w', date.weekOfYear(), 2); break; - case 'm': output += formatMonth(date); break; - case 'M': output += formatMonthName(date, doubled('M')); break; - case 'y': - output += (doubled('y', 2) ? date.year() : - (date.year() % 100 < 10 ? '0' : '') + date.year() % 100); - break; - case 'Y': - doubled('Y', 2); - output += date.formatYear(); - break; - case 'J': output += date.toJD(); break; - case '@': output += (date.toJD() - this.UNIX_EPOCH) * this.SECS_PER_DAY; break; - case '!': output += (date.toJD() - this.TICKS_EPOCH) * this.TICKS_PER_DAY; break; - case "'": - if (doubled("'")) { - output += "'"; - } - else { - literal = true; - } - break; - default: - output += format.charAt(iFormat); - } - } - } - return output; - }, - - /** Parse a string value into a date object. - See formatDate for the possible formats, plus: -
    -
  • * - ignore rest of string
  • -
- Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar - @param format {string} The expected format of the date ('' for default calendar format). - @param value {string} The date in the above format. - @param [settings] {object} Additional options whose attributes include: - @property [shortYearCutoff] {number} The cutoff year for determining the century. - @property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday. - @property [dayNames] {string[]} Names of the days from Sunday. - @property [monthNamesShort] {string[]} Abbreviated names of the months. - @property [monthNames] {string[]} Names of the months. - @return {CDate} The extracted date value or null if value is blank. - @throws Errors if the format and/or value are missing, - if the value doesn't match the format, or if the date is invalid. */ - parseDate: function(format, value, settings) { - if (value == null) { - throw main.local.invalidArguments || main.regionalOptions[''].invalidArguments; - } - value = (typeof value === 'object' ? value.toString() : value + ''); - if (value === '') { - return null; - } - format = format || this.local.dateFormat; - settings = settings || {}; - var shortYearCutoff = settings.shortYearCutoff || this.shortYearCutoff; - shortYearCutoff = (typeof shortYearCutoff !== 'string' ? shortYearCutoff : - this.today().year() % 100 + parseInt(shortYearCutoff, 10)); - var dayNamesShort = settings.dayNamesShort || this.local.dayNamesShort; - var dayNames = settings.dayNames || this.local.dayNames; - var parseMonth = settings.parseMonth || this.local.parseMonth; - var monthNumbers = settings.monthNumbers || this.local.monthNumbers; - var monthNamesShort = settings.monthNamesShort || this.local.monthNamesShort; - var monthNames = settings.monthNames || this.local.monthNames; - var jd = -1; - var year = -1; - var month = -1; - var day = -1; - var doy = -1; - var shortYear = false; - var literal = false; - // Check whether a format character is doubled - var doubled = function(match, step) { - var matches = 1; - while (iFormat + matches < format.length && format.charAt(iFormat + matches) === match) { - matches++; - } - iFormat += matches - 1; - return Math.floor(matches / (step || 1)) > 1; - }; - // Extract a number from the string value - var getNumber = function(match, step) { - var isDoubled = doubled(match, step); - var size = [2, 3, isDoubled ? 4 : 2, isDoubled ? 4 : 2, 10, 11, 20]['oyYJ@!'.indexOf(match) + 1]; - var digits = new RegExp('^-?\\d{1,' + size + '}'); - var num = value.substring(iValue).match(digits); - if (!num) { - throw (main.local.missingNumberAt || main.regionalOptions[''].missingNumberAt). - replace(/\{0\}/, iValue); - } - iValue += num[0].length; - return parseInt(num[0], 10); - }; - // Extract a month number from the string value - var calendar = this; - var getMonthNumber = function() { - if (typeof monthNumbers === 'function') { - doubled('m'); // update iFormat - var month = monthNumbers.call(calendar, value.substring(iValue)); - iValue += month.length; - return month; - } - - return getNumber('m'); - }; - // Extract a name from the string value and convert to an index - var getName = function(match, shortNames, longNames, step) { - var names = (doubled(match, step) ? longNames : shortNames); - for (var i = 0; i < names.length; i++) { - if (value.substr(iValue, names[i].length).toLowerCase() === names[i].toLowerCase()) { - iValue += names[i].length; - return i + calendar.minMonth; - } - } - throw (main.local.unknownNameAt || main.regionalOptions[''].unknownNameAt). - replace(/\{0\}/, iValue); - }; - // Extract a month number from the string value - var getMonthName = function() { - if (typeof monthNames === 'function') { - var month = doubled('M') ? - monthNames.call(calendar, value.substring(iValue)) : - monthNamesShort.call(calendar, value.substring(iValue)); - iValue += month.length; - return month; - } - - return getName('M', monthNamesShort, monthNames); - }; - // Confirm that a literal character matches the string value - var checkLiteral = function() { - if (value.charAt(iValue) !== format.charAt(iFormat)) { - throw (main.local.unexpectedLiteralAt || - main.regionalOptions[''].unexpectedLiteralAt).replace(/\{0\}/, iValue); - } - iValue++; - }; - var iValue = 0; - for (var iFormat = 0; iFormat < format.length; iFormat++) { - if (literal) { - if (format.charAt(iFormat) === "'" && !doubled("'")) { - literal = false; - } - else { - checkLiteral(); - } - } - else { - switch (format.charAt(iFormat)) { - case 'd': day = getNumber('d'); break; - case 'D': getName('D', dayNamesShort, dayNames); break; - case 'o': doy = getNumber('o'); break; - case 'w': getNumber('w'); break; - case 'm': month = getMonthNumber(); break; - case 'M': month = getMonthName(); break; - case 'y': - var iSave = iFormat; - shortYear = !doubled('y', 2); - iFormat = iSave; - year = getNumber('y', 2); - break; - case 'Y': year = getNumber('Y', 2); break; - case 'J': - jd = getNumber('J') + 0.5; - if (value.charAt(iValue) === '.') { - iValue++; - getNumber('J'); - } - break; - case '@': jd = getNumber('@') / this.SECS_PER_DAY + this.UNIX_EPOCH; break; - case '!': jd = getNumber('!') / this.TICKS_PER_DAY + this.TICKS_EPOCH; break; - case '*': iValue = value.length; break; - case "'": - if (doubled("'")) { - checkLiteral(); - } - else { - literal = true; - } - break; - default: checkLiteral(); - } - } - } - if (iValue < value.length) { - throw main.local.unexpectedText || main.regionalOptions[''].unexpectedText; - } - if (year === -1) { - year = this.today().year(); - } - else if (year < 100 && shortYear) { - year += (shortYearCutoff === -1 ? 1900 : this.today().year() - - this.today().year() % 100 - (year <= shortYearCutoff ? 0 : 100)); - } - if (typeof month === 'string') { - month = parseMonth.call(this, year, month); - } - if (doy > -1) { - month = 1; - day = doy; - for (var dim = this.daysInMonth(year, month); day > dim; dim = this.daysInMonth(year, month)) { - month++; - day -= dim; - } - } - return (jd > -1 ? this.fromJD(jd) : this.newDate(year, month, day)); - }, - - /** A date may be specified as an exact value or a relative one. - Found in the jquery.calendars.plus.js module. - @memberof BaseCalendar - @param dateSpec {CDate|number|string} The date as an object or string in the given format or - an offset - numeric days from today, or string amounts and periods, e.g. '+1m +2w'. - @param defaultDate {CDate} The date to use if no other supplied, may be null. - @param currentDate {CDate} The current date as a possible basis for relative dates, - if null today is used (optional) - @param [dateFormat] {string} The expected date format - see formatDate. - @param [settings] {object} Additional options whose attributes include: - @property [shortYearCutoff] {number} The cutoff year for determining the century. - @property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday. - @property [dayNames] {string[]} Names of the days from Sunday. - @property [monthNamesShort] {string[]} Abbreviated names of the months. - @property [monthNames] {string[]} Names of the months. - @return {CDate} The decoded date. */ - determineDate: function(dateSpec, defaultDate, currentDate, dateFormat, settings) { - if (currentDate && typeof currentDate !== 'object') { - settings = dateFormat; - dateFormat = currentDate; - currentDate = null; - } - if (typeof dateFormat !== 'string') { - settings = dateFormat; - dateFormat = ''; - } - var calendar = this; - var offsetString = function(offset) { - try { - return calendar.parseDate(dateFormat, offset, settings); - } - catch (e) { - // Ignore - } - offset = offset.toLowerCase(); - var date = (offset.match(/^c/) && currentDate ? - currentDate.newDate() : null) || calendar.today(); - var pattern = /([+-]?[0-9]+)\s*(d|w|m|y)?/g; - var matches = pattern.exec(offset); - while (matches) { - date.add(parseInt(matches[1], 10), matches[2] || 'd'); - matches = pattern.exec(offset); - } - return date; - }; - defaultDate = (defaultDate ? defaultDate.newDate() : null); - dateSpec = (dateSpec == null ? defaultDate : - (typeof dateSpec === 'string' ? offsetString(dateSpec) : (typeof dateSpec === 'number' ? - (isNaN(dateSpec) || dateSpec === Infinity || dateSpec === -Infinity ? defaultDate : - calendar.today().add(dateSpec, 'd')) : calendar.newDate(dateSpec)))); - return dateSpec; - } -}); - - -},{"./main":1147,"object-assign":1149}],1149:[function(require,module,exports){ 'use strict'; /* eslint-disable no-unused-vars */ +var getOwnPropertySymbols = Object.getOwnPropertySymbols; var hasOwnProperty = Object.prototype.hasOwnProperty; var propIsEnumerable = Object.prototype.propertyIsEnumerable; @@ -113564,7 +89389,7 @@ function shouldUseNative() { // Detect buggy property enumeration order in older V8 versions. // https://bugs.chromium.org/p/v8/issues/detail?id=4118 - var test1 = new String('abc'); // eslint-disable-line + var test1 = new String('abc'); // eslint-disable-line no-new-wrappers test1[5] = 'de'; if (Object.getOwnPropertyNames(test1)[0] === '5') { return false; @@ -113593,7 +89418,7 @@ function shouldUseNative() { } return true; - } catch (e) { + } catch (err) { // We don't expect any of the above to throw, but better to be safe. return false; } @@ -113613,8 +89438,8 @@ module.exports = shouldUseNative() ? Object.assign : function (target, source) { } } - if (Object.getOwnPropertySymbols) { - symbols = Object.getOwnPropertySymbols(from); + if (getOwnPropertySymbols) { + symbols = getOwnPropertySymbols(from); for (var i = 0; i < symbols.length; i++) { if (propIsEnumerable.call(from, symbols[i])) { to[symbols[i]] = from[symbols[i]]; @@ -113626,9 +89451,9856 @@ module.exports = shouldUseNative() ? Object.assign : function (target, source) { return to; }; -},{}],1150:[function(require,module,exports){ +},{}],447:[function(require,module,exports){ +// (c) Dean McNamee , 2013. +// +// https://github.com/deanm/omggif +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// +// omggif is a JavaScript implementation of a GIF 89a encoder and decoder, +// including animation and compression. It does not rely on any specific +// underlying system, so should run in the browser, Node, or Plask. + +function GifWriter(buf, width, height, gopts) { + var p = 0; + + var gopts = gopts === undefined ? { } : gopts; + var loop_count = gopts.loop === undefined ? null : gopts.loop; + var global_palette = gopts.palette === undefined ? null : gopts.palette; + + if (width <= 0 || height <= 0 || width > 65535 || height > 65535) + throw "Width/Height invalid." + + function check_palette_and_num_colors(palette) { + var num_colors = palette.length; + if (num_colors < 2 || num_colors > 256 || num_colors & (num_colors-1)) + throw "Invalid code/color length, must be power of 2 and 2 .. 256."; + return num_colors; + } + + // - Header. + buf[p++] = 0x47; buf[p++] = 0x49; buf[p++] = 0x46; // GIF + buf[p++] = 0x38; buf[p++] = 0x39; buf[p++] = 0x61; // 89a + + // Handling of Global Color Table (palette) and background index. + var gp_num_colors_pow2 = 0; + var background = 0; + if (global_palette !== null) { + var gp_num_colors = check_palette_and_num_colors(global_palette); + while (gp_num_colors >>= 1) ++gp_num_colors_pow2; + gp_num_colors = 1 << gp_num_colors_pow2; + --gp_num_colors_pow2; + if (gopts.background !== undefined) { + background = gopts.background; + if (background >= gp_num_colors) throw "Background index out of range."; + // The GIF spec states that a background index of 0 should be ignored, so + // this is probably a mistake and you really want to set it to another + // slot in the palette. But actually in the end most browsers, etc end + // up ignoring this almost completely (including for dispose background). + if (background === 0) + throw "Background index explicitly passed as 0."; + } + } + + // - Logical Screen Descriptor. + // NOTE(deanm): w/h apparently ignored by implementations, but set anyway. + buf[p++] = width & 0xff; buf[p++] = width >> 8 & 0xff; + buf[p++] = height & 0xff; buf[p++] = height >> 8 & 0xff; + // NOTE: Indicates 0-bpp original color resolution (unused?). + buf[p++] = (global_palette !== null ? 0x80 : 0) | // Global Color Table Flag. + gp_num_colors_pow2; // NOTE: No sort flag (unused?). + buf[p++] = background; // Background Color Index. + buf[p++] = 0; // Pixel aspect ratio (unused?). + + // - Global Color Table + if (global_palette !== null) { + for (var i = 0, il = global_palette.length; i < il; ++i) { + var rgb = global_palette[i]; + buf[p++] = rgb >> 16 & 0xff; + buf[p++] = rgb >> 8 & 0xff; + buf[p++] = rgb & 0xff; + } + } + + if (loop_count !== null) { // Netscape block for looping. + if (loop_count < 0 || loop_count > 65535) + throw "Loop count invalid." + // Extension code, label, and length. + buf[p++] = 0x21; buf[p++] = 0xff; buf[p++] = 0x0b; + // NETSCAPE2.0 + buf[p++] = 0x4e; buf[p++] = 0x45; buf[p++] = 0x54; buf[p++] = 0x53; + buf[p++] = 0x43; buf[p++] = 0x41; buf[p++] = 0x50; buf[p++] = 0x45; + buf[p++] = 0x32; buf[p++] = 0x2e; buf[p++] = 0x30; + // Sub-block + buf[p++] = 0x03; buf[p++] = 0x01; + buf[p++] = loop_count & 0xff; buf[p++] = loop_count >> 8 & 0xff; + buf[p++] = 0x00; // Terminator. + } + + + var ended = false; + + this.addFrame = function(x, y, w, h, indexed_pixels, opts) { + if (ended === true) { --p; ended = false; } // Un-end. + + opts = opts === undefined ? { } : opts; + + // TODO(deanm): Bounds check x, y. Do they need to be within the virtual + // canvas width/height, I imagine? + if (x < 0 || y < 0 || x > 65535 || y > 65535) + throw "x/y invalid." + + if (w <= 0 || h <= 0 || w > 65535 || h > 65535) + throw "Width/Height invalid." + + if (indexed_pixels.length < w * h) + throw "Not enough pixels for the frame size."; + + var using_local_palette = true; + var palette = opts.palette; + if (palette === undefined || palette === null) { + using_local_palette = false; + palette = global_palette; + } + + if (palette === undefined || palette === null) + throw "Must supply either a local or global palette."; + + var num_colors = check_palette_and_num_colors(palette); + + // Compute the min_code_size (power of 2), destroying num_colors. + var min_code_size = 0; + while (num_colors >>= 1) ++min_code_size; + num_colors = 1 << min_code_size; // Now we can easily get it back. + + var delay = opts.delay === undefined ? 0 : opts.delay; + + // From the spec: + // 0 - No disposal specified. The decoder is + // not required to take any action. + // 1 - Do not dispose. The graphic is to be left + // in place. + // 2 - Restore to background color. The area used by the + // graphic must be restored to the background color. + // 3 - Restore to previous. The decoder is required to + // restore the area overwritten by the graphic with + // what was there prior to rendering the graphic. + // 4-7 - To be defined. + // NOTE(deanm): Dispose background doesn't really work, apparently most + // browsers ignore the background palette index and clear to transparency. + var disposal = opts.disposal === undefined ? 0 : opts.disposal; + if (disposal < 0 || disposal > 3) // 4-7 is reserved. + throw "Disposal out of range."; + + var use_transparency = false; + var transparent_index = 0; + if (opts.transparent !== undefined && opts.transparent !== null) { + use_transparency = true; + transparent_index = opts.transparent; + if (transparent_index < 0 || transparent_index >= num_colors) + throw "Transparent color index."; + } + + if (disposal !== 0 || use_transparency || delay !== 0) { + // - Graphics Control Extension + buf[p++] = 0x21; buf[p++] = 0xf9; // Extension / Label. + buf[p++] = 4; // Byte size. + + buf[p++] = disposal << 2 | (use_transparency === true ? 1 : 0); + buf[p++] = delay & 0xff; buf[p++] = delay >> 8 & 0xff; + buf[p++] = transparent_index; // Transparent color index. + buf[p++] = 0; // Block Terminator. + } + + // - Image Descriptor + buf[p++] = 0x2c; // Image Seperator. + buf[p++] = x & 0xff; buf[p++] = x >> 8 & 0xff; // Left. + buf[p++] = y & 0xff; buf[p++] = y >> 8 & 0xff; // Top. + buf[p++] = w & 0xff; buf[p++] = w >> 8 & 0xff; + buf[p++] = h & 0xff; buf[p++] = h >> 8 & 0xff; + // NOTE: No sort flag (unused?). + // TODO(deanm): Support interlace. + buf[p++] = using_local_palette === true ? (0x80 | (min_code_size-1)) : 0; + + // - Local Color Table + if (using_local_palette === true) { + for (var i = 0, il = palette.length; i < il; ++i) { + var rgb = palette[i]; + buf[p++] = rgb >> 16 & 0xff; + buf[p++] = rgb >> 8 & 0xff; + buf[p++] = rgb & 0xff; + } + } + + p = GifWriterOutputLZWCodeStream( + buf, p, min_code_size < 2 ? 2 : min_code_size, indexed_pixels); + }; + + this.end = function() { + if (ended === false) { + buf[p++] = 0x3b; // Trailer. + ended = true; + } + return p; + }; +} + +// Main compression routine, palette indexes -> LZW code stream. +// |index_stream| must have at least one entry. +function GifWriterOutputLZWCodeStream(buf, p, min_code_size, index_stream) { + buf[p++] = min_code_size; + var cur_subblock = p++; // Pointing at the length field. + + var clear_code = 1 << min_code_size; + var code_mask = clear_code - 1; + var eoi_code = clear_code + 1; + var next_code = eoi_code + 1; + + var cur_code_size = min_code_size + 1; // Number of bits per code. + var cur_shift = 0; + // We have at most 12-bit codes, so we should have to hold a max of 19 + // bits here (and then we would write out). + var cur = 0; + + function emit_bytes_to_buffer(bit_block_size) { + while (cur_shift >= bit_block_size) { + buf[p++] = cur & 0xff; + cur >>= 8; cur_shift -= 8; + if (p === cur_subblock + 256) { // Finished a subblock. + buf[cur_subblock] = 255; + cur_subblock = p++; + } + } + } + + function emit_code(c) { + cur |= c << cur_shift; + cur_shift += cur_code_size; + emit_bytes_to_buffer(8); + } + + // I am not an expert on the topic, and I don't want to write a thesis. + // However, it is good to outline here the basic algorithm and the few data + // structures and optimizations here that make this implementation fast. + // The basic idea behind LZW is to build a table of previously seen runs + // addressed by a short id (herein called output code). All data is + // referenced by a code, which represents one or more values from the + // original input stream. All input bytes can be referenced as the same + // value as an output code. So if you didn't want any compression, you + // could more or less just output the original bytes as codes (there are + // some details to this, but it is the idea). In order to achieve + // compression, values greater then the input range (codes can be up to + // 12-bit while input only 8-bit) represent a sequence of previously seen + // inputs. The decompressor is able to build the same mapping while + // decoding, so there is always a shared common knowledge between the + // encoding and decoder, which is also important for "timing" aspects like + // how to handle variable bit width code encoding. + // + // One obvious but very important consequence of the table system is there + // is always a unique id (at most 12-bits) to map the runs. 'A' might be + // 4, then 'AA' might be 10, 'AAA' 11, 'AAAA' 12, etc. This relationship + // can be used for an effecient lookup strategy for the code mapping. We + // need to know if a run has been seen before, and be able to map that run + // to the output code. Since we start with known unique ids (input bytes), + // and then from those build more unique ids (table entries), we can + // continue this chain (almost like a linked list) to always have small + // integer values that represent the current byte chains in the encoder. + // This means instead of tracking the input bytes (AAAABCD) to know our + // current state, we can track the table entry for AAAABC (it is guaranteed + // to exist by the nature of the algorithm) and the next character D. + // Therefor the tuple of (table_entry, byte) is guaranteed to also be + // unique. This allows us to create a simple lookup key for mapping input + // sequences to codes (table indices) without having to store or search + // any of the code sequences. So if 'AAAA' has a table entry of 12, the + // tuple of ('AAAA', K) for any input byte K will be unique, and can be our + // key. This leads to a integer value at most 20-bits, which can always + // fit in an SMI value and be used as a fast sparse array / object key. + + // Output code for the current contents of the index buffer. + var ib_code = index_stream[0] & code_mask; // Load first input index. + var code_table = { }; // Key'd on our 20-bit "tuple". + + emit_code(clear_code); // Spec says first code should be a clear code. + + // First index already loaded, process the rest of the stream. + for (var i = 1, il = index_stream.length; i < il; ++i) { + var k = index_stream[i] & code_mask; + var cur_key = ib_code << 8 | k; // (prev, k) unique tuple. + var cur_code = code_table[cur_key]; // buffer + k. + + // Check if we have to create a new code table entry. + if (cur_code === undefined) { // We don't have buffer + k. + // Emit index buffer (without k). + // This is an inline version of emit_code, because this is the core + // writing routine of the compressor (and V8 cannot inline emit_code + // because it is a closure here in a different context). Additionally + // we can call emit_byte_to_buffer less often, because we can have + // 30-bits (from our 31-bit signed SMI), and we know our codes will only + // be 12-bits, so can safely have 18-bits there without overflow. + // emit_code(ib_code); + cur |= ib_code << cur_shift; + cur_shift += cur_code_size; + while (cur_shift >= 8) { + buf[p++] = cur & 0xff; + cur >>= 8; cur_shift -= 8; + if (p === cur_subblock + 256) { // Finished a subblock. + buf[cur_subblock] = 255; + cur_subblock = p++; + } + } + + if (next_code === 4096) { // Table full, need a clear. + emit_code(clear_code); + next_code = eoi_code + 1; + cur_code_size = min_code_size + 1; + code_table = { }; + } else { // Table not full, insert a new entry. + // Increase our variable bit code sizes if necessary. This is a bit + // tricky as it is based on "timing" between the encoding and + // decoder. From the encoders perspective this should happen after + // we've already emitted the index buffer and are about to create the + // first table entry that would overflow our current code bit size. + if (next_code >= (1 << cur_code_size)) ++cur_code_size; + code_table[cur_key] = next_code++; // Insert into code table. + } + + ib_code = k; // Index buffer to single input k. + } else { + ib_code = cur_code; // Index buffer to sequence in code table. + } + } + + emit_code(ib_code); // There will still be something in the index buffer. + emit_code(eoi_code); // End Of Information. + + // Flush / finalize the sub-blocks stream to the buffer. + emit_bytes_to_buffer(1); + + // Finish the sub-blocks, writing out any unfinished lengths and + // terminating with a sub-block of length 0. If we have already started + // but not yet used a sub-block it can just become the terminator. + if (cur_subblock + 1 === p) { // Started but unused. + buf[cur_subblock] = 0; + } else { // Started and used, write length and additional terminator block. + buf[cur_subblock] = p - cur_subblock - 1; + buf[p++] = 0; + } + return p; +} + +function GifReader(buf) { + var p = 0; + + // - Header (GIF87a or GIF89a). + if (buf[p++] !== 0x47 || buf[p++] !== 0x49 || buf[p++] !== 0x46 || + buf[p++] !== 0x38 || (buf[p++]+1 & 0xfd) !== 0x38 || buf[p++] !== 0x61) { + throw "Invalid GIF 87a/89a header."; + } + + // - Logical Screen Descriptor. + var width = buf[p++] | buf[p++] << 8; + var height = buf[p++] | buf[p++] << 8; + var pf0 = buf[p++]; // . + var global_palette_flag = pf0 >> 7; + var num_global_colors_pow2 = pf0 & 0x7; + var num_global_colors = 1 << (num_global_colors_pow2 + 1); + var background = buf[p++]; + buf[p++]; // Pixel aspect ratio (unused?). + + var global_palette_offset = null; + + if (global_palette_flag) { + global_palette_offset = p; + p += num_global_colors * 3; // Seek past palette. + } + + var no_eof = true; + + var frames = [ ]; + + var delay = 0; + var transparent_index = null; + var disposal = 0; // 0 - No disposal specified. + var loop_count = null; + + this.width = width; + this.height = height; + + while (no_eof && p < buf.length) { + switch (buf[p++]) { + case 0x21: // Graphics Control Extension Block + switch (buf[p++]) { + case 0xff: // Application specific block + // Try if it's a Netscape block (with animation loop counter). + if (buf[p ] !== 0x0b || // 21 FF already read, check block size. + // NETSCAPE2.0 + buf[p+1 ] == 0x4e && buf[p+2 ] == 0x45 && buf[p+3 ] == 0x54 && + buf[p+4 ] == 0x53 && buf[p+5 ] == 0x43 && buf[p+6 ] == 0x41 && + buf[p+7 ] == 0x50 && buf[p+8 ] == 0x45 && buf[p+9 ] == 0x32 && + buf[p+10] == 0x2e && buf[p+11] == 0x30 && + // Sub-block + buf[p+12] == 0x03 && buf[p+13] == 0x01 && buf[p+16] == 0) { + p += 14; + loop_count = buf[p++] | buf[p++] << 8; + p++; // Skip terminator. + } else { // We don't know what it is, just try to get past it. + p += 12; + while (true) { // Seek through subblocks. + var block_size = buf[p++]; + if (block_size === 0) break; + p += block_size; + } + } + break; + + case 0xf9: // Graphics Control Extension + if (buf[p++] !== 0x4 || buf[p+4] !== 0) + throw "Invalid graphics extension block."; + var pf1 = buf[p++]; + delay = buf[p++] | buf[p++] << 8; + transparent_index = buf[p++]; + if ((pf1 & 1) === 0) transparent_index = null; + disposal = pf1 >> 2 & 0x7; + p++; // Skip terminator. + break; + + case 0xfe: // Comment Extension. + while (true) { // Seek through subblocks. + var block_size = buf[p++]; + if (block_size === 0) break; + // console.log(buf.slice(p, p+block_size).toString('ascii')); + p += block_size; + } + break; + + default: + throw "Unknown graphic control label: 0x" + buf[p-1].toString(16); + } + break; + + case 0x2c: // Image Descriptor. + var x = buf[p++] | buf[p++] << 8; + var y = buf[p++] | buf[p++] << 8; + var w = buf[p++] | buf[p++] << 8; + var h = buf[p++] | buf[p++] << 8; + var pf2 = buf[p++]; + var local_palette_flag = pf2 >> 7; + var interlace_flag = pf2 >> 6 & 1; + var num_local_colors_pow2 = pf2 & 0x7; + var num_local_colors = 1 << (num_local_colors_pow2 + 1); + var palette_offset = global_palette_offset; + var has_local_palette = false; + if (local_palette_flag) { + var has_local_palette = true; + palette_offset = p; // Override with local palette. + p += num_local_colors * 3; // Seek past palette. + } + + var data_offset = p; + + p++; // codesize + while (true) { + var block_size = buf[p++]; + if (block_size === 0) break; + p += block_size; + } + + frames.push({x: x, y: y, width: w, height: h, + has_local_palette: has_local_palette, + palette_offset: palette_offset, + data_offset: data_offset, + data_length: p - data_offset, + transparent_index: transparent_index, + interlaced: !!interlace_flag, + delay: delay, + disposal: disposal}); + break; + + case 0x3b: // Trailer Marker (end of file). + no_eof = false; + break; + + default: + throw "Unknown gif block: 0x" + buf[p-1].toString(16); + break; + } + } + + this.numFrames = function() { + return frames.length; + }; + + this.loopCount = function() { + return loop_count; + }; + + this.frameInfo = function(frame_num) { + if (frame_num < 0 || frame_num >= frames.length) + throw "Frame index out of range."; + return frames[frame_num]; + } + + this.decodeAndBlitFrameBGRA = function(frame_num, pixels) { + var frame = this.frameInfo(frame_num); + var num_pixels = frame.width * frame.height; + var index_stream = new Uint8Array(num_pixels); // At most 8-bit indices. + GifReaderLZWOutputIndexStream( + buf, frame.data_offset, index_stream, num_pixels); + var palette_offset = frame.palette_offset; + + // NOTE(deanm): It seems to be much faster to compare index to 256 than + // to === null. Not sure why, but CompareStub_EQ_STRICT shows up high in + // the profile, not sure if it's related to using a Uint8Array. + var trans = frame.transparent_index; + if (trans === null) trans = 256; + + // We are possibly just blitting to a portion of the entire frame. + // That is a subrect within the framerect, so the additional pixels + // must be skipped over after we finished a scanline. + var framewidth = frame.width; + var framestride = width - framewidth; + var xleft = framewidth; // Number of subrect pixels left in scanline. + + // Output indicies of the top left and bottom right corners of the subrect. + var opbeg = ((frame.y * width) + frame.x) * 4; + var opend = ((frame.y + frame.height) * width + frame.x) * 4; + var op = opbeg; + + var scanstride = framestride * 4; + + // Use scanstride to skip past the rows when interlacing. This is skipping + // 7 rows for the first two passes, then 3 then 1. + if (frame.interlaced === true) { + scanstride += width * 4 * 7; // Pass 1. + } + + var interlaceskip = 8; // Tracking the row interval in the current pass. + + for (var i = 0, il = index_stream.length; i < il; ++i) { + var index = index_stream[i]; + + if (xleft === 0) { // Beginning of new scan line + op += scanstride; + xleft = framewidth; + if (op >= opend) { // Catch the wrap to switch passes when interlacing. + scanstride = framestride * 4 + width * 4 * (interlaceskip-1); + // interlaceskip / 2 * 4 is interlaceskip << 1. + op = opbeg + (framewidth + framestride) * (interlaceskip << 1); + interlaceskip >>= 1; + } + } + + if (index === trans) { + op += 4; + } else { + var r = buf[palette_offset + index * 3]; + var g = buf[palette_offset + index * 3 + 1]; + var b = buf[palette_offset + index * 3 + 2]; + pixels[op++] = b; + pixels[op++] = g; + pixels[op++] = r; + pixels[op++] = 255; + } + --xleft; + } + }; + + // I will go to copy and paste hell one day... + this.decodeAndBlitFrameRGBA = function(frame_num, pixels) { + var frame = this.frameInfo(frame_num); + var num_pixels = frame.width * frame.height; + var index_stream = new Uint8Array(num_pixels); // At most 8-bit indices. + GifReaderLZWOutputIndexStream( + buf, frame.data_offset, index_stream, num_pixels); + var palette_offset = frame.palette_offset; + + // NOTE(deanm): It seems to be much faster to compare index to 256 than + // to === null. Not sure why, but CompareStub_EQ_STRICT shows up high in + // the profile, not sure if it's related to using a Uint8Array. + var trans = frame.transparent_index; + if (trans === null) trans = 256; + + // We are possibly just blitting to a portion of the entire frame. + // That is a subrect within the framerect, so the additional pixels + // must be skipped over after we finished a scanline. + var framewidth = frame.width; + var framestride = width - framewidth; + var xleft = framewidth; // Number of subrect pixels left in scanline. + + // Output indicies of the top left and bottom right corners of the subrect. + var opbeg = ((frame.y * width) + frame.x) * 4; + var opend = ((frame.y + frame.height) * width + frame.x) * 4; + var op = opbeg; + + var scanstride = framestride * 4; + + // Use scanstride to skip past the rows when interlacing. This is skipping + // 7 rows for the first two passes, then 3 then 1. + if (frame.interlaced === true) { + scanstride += width * 4 * 7; // Pass 1. + } + + var interlaceskip = 8; // Tracking the row interval in the current pass. + + for (var i = 0, il = index_stream.length; i < il; ++i) { + var index = index_stream[i]; + + if (xleft === 0) { // Beginning of new scan line + op += scanstride; + xleft = framewidth; + if (op >= opend) { // Catch the wrap to switch passes when interlacing. + scanstride = framestride * 4 + width * 4 * (interlaceskip-1); + // interlaceskip / 2 * 4 is interlaceskip << 1. + op = opbeg + (framewidth + framestride) * (interlaceskip << 1); + interlaceskip >>= 1; + } + } + + if (index === trans) { + op += 4; + } else { + var r = buf[palette_offset + index * 3]; + var g = buf[palette_offset + index * 3 + 1]; + var b = buf[palette_offset + index * 3 + 2]; + pixels[op++] = r; + pixels[op++] = g; + pixels[op++] = b; + pixels[op++] = 255; + } + --xleft; + } + }; +} + +function GifReaderLZWOutputIndexStream(code_stream, p, output, output_length) { + var min_code_size = code_stream[p++]; + + var clear_code = 1 << min_code_size; + var eoi_code = clear_code + 1; + var next_code = eoi_code + 1; + + var cur_code_size = min_code_size + 1; // Number of bits per code. + // NOTE: This shares the same name as the encoder, but has a different + // meaning here. Here this masks each code coming from the code stream. + var code_mask = (1 << cur_code_size) - 1; + var cur_shift = 0; + var cur = 0; + + var op = 0; // Output pointer. + + var subblock_size = code_stream[p++]; + + // TODO(deanm): Would using a TypedArray be any faster? At least it would + // solve the fast mode / backing store uncertainty. + // var code_table = Array(4096); + var code_table = new Int32Array(4096); // Can be signed, we only use 20 bits. + + var prev_code = null; // Track code-1. + + while (true) { + // Read up to two bytes, making sure we always 12-bits for max sized code. + while (cur_shift < 16) { + if (subblock_size === 0) break; // No more data to be read. + + cur |= code_stream[p++] << cur_shift; + cur_shift += 8; + + if (subblock_size === 1) { // Never let it get to 0 to hold logic above. + subblock_size = code_stream[p++]; // Next subblock. + } else { + --subblock_size; + } + } + + // TODO(deanm): We should never really get here, we should have received + // and EOI. + if (cur_shift < cur_code_size) + break; + + var code = cur & code_mask; + cur >>= cur_code_size; + cur_shift -= cur_code_size; + + // TODO(deanm): Maybe should check that the first code was a clear code, + // at least this is what you're supposed to do. But actually our encoder + // now doesn't emit a clear code first anyway. + if (code === clear_code) { + // We don't actually have to clear the table. This could be a good idea + // for greater error checking, but we don't really do any anyway. We + // will just track it with next_code and overwrite old entries. + + next_code = eoi_code + 1; + cur_code_size = min_code_size + 1; + code_mask = (1 << cur_code_size) - 1; + + // Don't update prev_code ? + prev_code = null; + continue; + } else if (code === eoi_code) { + break; + } + + // We have a similar situation as the decoder, where we want to store + // variable length entries (code table entries), but we want to do in a + // faster manner than an array of arrays. The code below stores sort of a + // linked list within the code table, and then "chases" through it to + // construct the dictionary entries. When a new entry is created, just the + // last byte is stored, and the rest (prefix) of the entry is only + // referenced by its table entry. Then the code chases through the + // prefixes until it reaches a single byte code. We have to chase twice, + // first to compute the length, and then to actually copy the data to the + // output (backwards, since we know the length). The alternative would be + // storing something in an intermediate stack, but that doesn't make any + // more sense. I implemented an approach where it also stored the length + // in the code table, although it's a bit tricky because you run out of + // bits (12 + 12 + 8), but I didn't measure much improvements (the table + // entries are generally not the long). Even when I created benchmarks for + // very long table entries the complexity did not seem worth it. + // The code table stores the prefix entry in 12 bits and then the suffix + // byte in 8 bits, so each entry is 20 bits. + + var chase_code = code < next_code ? code : prev_code; + + // Chase what we will output, either {CODE} or {CODE-1}. + var chase_length = 0; + var chase = chase_code; + while (chase > clear_code) { + chase = code_table[chase] >> 8; + ++chase_length; + } + + var k = chase; + + var op_end = op + chase_length + (chase_code !== code ? 1 : 0); + if (op_end > output_length) { + console.log("Warning, gif stream longer than expected."); + return; + } + + // Already have the first byte from the chase, might as well write it fast. + output[op++] = k; + + op += chase_length; + var b = op; // Track pointer, writing backwards. + + if (chase_code !== code) // The case of emitting {CODE-1} + k. + output[op++] = k; + + chase = chase_code; + while (chase_length--) { + chase = code_table[chase]; + output[--b] = chase & 0xff; // Write backwards. + chase >>= 8; // Pull down to the prefix code. + } + + if (prev_code !== null && next_code < 4096) { + code_table[next_code++] = prev_code << 8 | k; + // TODO(deanm): Figure out this clearing vs code growth logic better. I + // have an feeling that it should just happen somewhere else, for now it + // is awkward between when we grow past the max and then hit a clear code. + // For now just check if we hit the max 12-bits (then a clear code should + // follow, also of course encoded in 12-bits). + if (next_code >= code_mask+1 && cur_code_size < 12) { + ++cur_code_size; + code_mask = code_mask << 1 | 1; + } + } + + prev_code = code; + } + + if (op !== output_length) { + console.log("Warning, gif stream shorter than expected."); + } + + return output; +} + +try { exports.GifWriter = GifWriter; exports.GifReader = GifReader } catch(e) { } // CommonJS. + +},{}],448:[function(require,module,exports){ +'use strict' + +module.exports = quatFromFrame + +function quatFromFrame( + out, + rx, ry, rz, + ux, uy, uz, + fx, fy, fz) { + var tr = rx + uy + fz + if(l > 0) { + var l = Math.sqrt(tr + 1.0) + out[0] = 0.5 * (uz - fy) / l + out[1] = 0.5 * (fx - rz) / l + out[2] = 0.5 * (ry - uy) / l + out[3] = 0.5 * l + } else { + var tf = Math.max(rx, uy, fz) + var l = Math.sqrt(2 * tf - tr + 1.0) + if(rx >= tf) { + //x y z order + out[0] = 0.5 * l + out[1] = 0.5 * (ux + ry) / l + out[2] = 0.5 * (fx + rz) / l + out[3] = 0.5 * (uz - fy) / l + } else if(uy >= tf) { + //y z x order + out[0] = 0.5 * (ry + ux) / l + out[1] = 0.5 * l + out[2] = 0.5 * (fy + uz) / l + out[3] = 0.5 * (fx - rz) / l + } else { + //z x y order + out[0] = 0.5 * (rz + fx) / l + out[1] = 0.5 * (uz + fy) / l + out[2] = 0.5 * l + out[3] = 0.5 * (ry - ux) / l + } + } + return out +} +},{}],449:[function(require,module,exports){ +'use strict' + +module.exports = createOrbitController + +var filterVector = require('filtered-vector') +var lookAt = require('gl-mat4/lookAt') +var mat4FromQuat = require('gl-mat4/fromQuat') +var invert44 = require('gl-mat4/invert') +var quatFromFrame = require('./lib/quatFromFrame') + +function len3(x,y,z) { + return Math.sqrt(Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2)) +} + +function len4(w,x,y,z) { + return Math.sqrt(Math.pow(w,2) + Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2)) +} + +function normalize4(out, a) { + var ax = a[0] + var ay = a[1] + var az = a[2] + var aw = a[3] + var al = len4(ax, ay, az, aw) + if(al > 1e-6) { + out[0] = ax/al + out[1] = ay/al + out[2] = az/al + out[3] = aw/al + } else { + out[0] = out[1] = out[2] = 0.0 + out[3] = 1.0 + } +} + +function OrbitCameraController(initQuat, initCenter, initRadius) { + this.radius = filterVector([initRadius]) + this.center = filterVector(initCenter) + this.rotation = filterVector(initQuat) + + this.computedRadius = this.radius.curve(0) + this.computedCenter = this.center.curve(0) + this.computedRotation = this.rotation.curve(0) + this.computedUp = [0.1,0,0] + this.computedEye = [0.1,0,0] + this.computedMatrix = [0.1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + + this.recalcMatrix(0) +} + +var proto = OrbitCameraController.prototype + +proto.lastT = function() { + return Math.max( + this.radius.lastT(), + this.center.lastT(), + this.rotation.lastT()) +} + +proto.recalcMatrix = function(t) { + this.radius.curve(t) + this.center.curve(t) + this.rotation.curve(t) + + var quat = this.computedRotation + normalize4(quat, quat) + + var mat = this.computedMatrix + mat4FromQuat(mat, quat) + + var center = this.computedCenter + var eye = this.computedEye + var up = this.computedUp + var radius = Math.exp(this.computedRadius[0]) + + eye[0] = center[0] + radius * mat[2] + eye[1] = center[1] + radius * mat[6] + eye[2] = center[2] + radius * mat[10] + up[0] = mat[1] + up[1] = mat[5] + up[2] = mat[9] + + for(var i=0; i<3; ++i) { + var rr = 0.0 + for(var j=0; j<3; ++j) { + rr += mat[i+4*j] * eye[j] + } + mat[12+i] = -rr + } +} + +proto.getMatrix = function(t, result) { + this.recalcMatrix(t) + var m = this.computedMatrix + if(result) { + for(var i=0; i<16; ++i) { + result[i] = m[i] + } + return result + } + return m +} + +proto.idle = function(t) { + this.center.idle(t) + this.radius.idle(t) + this.rotation.idle(t) +} + +proto.flush = function(t) { + this.center.flush(t) + this.radius.flush(t) + this.rotation.flush(t) +} + +proto.pan = function(t, dx, dy, dz) { + dx = dx || 0.0 + dy = dy || 0.0 + dz = dz || 0.0 + + this.recalcMatrix(t) + var mat = this.computedMatrix + + var ux = mat[1] + var uy = mat[5] + var uz = mat[9] + var ul = len3(ux, uy, uz) + ux /= ul + uy /= ul + uz /= ul + + var rx = mat[0] + var ry = mat[4] + var rz = mat[8] + var ru = rx * ux + ry * uy + rz * uz + rx -= ux * ru + ry -= uy * ru + rz -= uz * ru + var rl = len3(rx, ry, rz) + rx /= rl + ry /= rl + rz /= rl + + var fx = mat[2] + var fy = mat[6] + var fz = mat[10] + var fu = fx * ux + fy * uy + fz * uz + var fr = fx * rx + fy * ry + fz * rz + fx -= fu * ux + fr * rx + fy -= fu * uy + fr * ry + fz -= fu * uz + fr * rz + var fl = len3(fx, fy, fz) + fx /= fl + fy /= fl + fz /= fl + + var vx = rx * dx + ux * dy + var vy = ry * dx + uy * dy + var vz = rz * dx + uz * dy + + this.center.move(t, vx, vy, vz) + + //Update z-component of radius + var radius = Math.exp(this.computedRadius[0]) + radius = Math.max(1e-4, radius + dz) + this.radius.set(t, Math.log(radius)) +} + +proto.rotate = function(t, dx, dy, dz) { + this.recalcMatrix(t) + + dx = dx||0.0 + dy = dy||0.0 + + var mat = this.computedMatrix + + var rx = mat[0] + var ry = mat[4] + var rz = mat[8] + + var ux = mat[1] + var uy = mat[5] + var uz = mat[9] + + var fx = mat[2] + var fy = mat[6] + var fz = mat[10] + + var qx = dx * rx + dy * ux + var qy = dx * ry + dy * uy + var qz = dx * rz + dy * uz + + var bx = -(fy * qz - fz * qy) + var by = -(fz * qx - fx * qz) + var bz = -(fx * qy - fy * qx) + var bw = Math.sqrt(Math.max(0.0, 1.0 - Math.pow(bx,2) - Math.pow(by,2) - Math.pow(bz,2))) + var bl = len4(bx, by, bz, bw) + if(bl > 1e-6) { + bx /= bl + by /= bl + bz /= bl + bw /= bl + } else { + bx = by = bz = 0.0 + bw = 1.0 + } + + var rotation = this.computedRotation + var ax = rotation[0] + var ay = rotation[1] + var az = rotation[2] + var aw = rotation[3] + + var cx = ax*bw + aw*bx + ay*bz - az*by + var cy = ay*bw + aw*by + az*bx - ax*bz + var cz = az*bw + aw*bz + ax*by - ay*bx + var cw = aw*bw - ax*bx - ay*by - az*bz + + //Apply roll + if(dz) { + bx = fx + by = fy + bz = fz + var s = Math.sin(dz) / len3(bx, by, bz) + bx *= s + by *= s + bz *= s + bw = Math.cos(dx) + cx = cx*bw + cw*bx + cy*bz - cz*by + cy = cy*bw + cw*by + cz*bx - cx*bz + cz = cz*bw + cw*bz + cx*by - cy*bx + cw = cw*bw - cx*bx - cy*by - cz*bz + } + + var cl = len4(cx, cy, cz, cw) + if(cl > 1e-6) { + cx /= cl + cy /= cl + cz /= cl + cw /= cl + } else { + cx = cy = cz = 0.0 + cw = 1.0 + } + + this.rotation.set(t, cx, cy, cz, cw) +} + +proto.lookAt = function(t, eye, center, up) { + this.recalcMatrix(t) + + center = center || this.computedCenter + eye = eye || this.computedEye + up = up || this.computedUp + + var mat = this.computedMatrix + lookAt(mat, eye, center, up) + + var rotation = this.computedRotation + quatFromFrame(rotation, + mat[0], mat[1], mat[2], + mat[4], mat[5], mat[6], + mat[8], mat[9], mat[10]) + normalize4(rotation, rotation) + this.rotation.set(t, rotation[0], rotation[1], rotation[2], rotation[3]) + + var fl = 0.0 + for(var i=0; i<3; ++i) { + fl += Math.pow(center[i] - eye[i], 2) + } + this.radius.set(t, 0.5 * Math.log(Math.max(fl, 1e-6))) + + this.center.set(t, center[0], center[1], center[2]) +} + +proto.translate = function(t, dx, dy, dz) { + this.center.move(t, + dx||0.0, + dy||0.0, + dz||0.0) +} + +proto.setMatrix = function(t, matrix) { + + var rotation = this.computedRotation + quatFromFrame(rotation, + matrix[0], matrix[1], matrix[2], + matrix[4], matrix[5], matrix[6], + matrix[8], matrix[9], matrix[10]) + normalize4(rotation, rotation) + this.rotation.set(t, rotation[0], rotation[1], rotation[2], rotation[3]) + + var mat = this.computedMatrix + invert44(mat, matrix) + var w = mat[15] + if(Math.abs(w) > 1e-6) { + var cx = mat[12]/w + var cy = mat[13]/w + var cz = mat[14]/w + + this.recalcMatrix(t) + var r = Math.exp(this.computedRadius[0]) + this.center.set(t, cx-mat[2]*r, cy-mat[6]*r, cz-mat[10]*r) + this.radius.idle(t) + } else { + this.center.idle(t) + this.radius.idle(t) + } +} + +proto.setDistance = function(t, d) { + if(d > 0) { + this.radius.set(t, Math.log(d)) + } +} + +proto.setDistanceLimits = function(lo, hi) { + if(lo > 0) { + lo = Math.log(lo) + } else { + lo = -Infinity + } + if(hi > 0) { + hi = Math.log(hi) + } else { + hi = Infinity + } + hi = Math.max(hi, lo) + this.radius.bounds[0][0] = lo + this.radius.bounds[1][0] = hi +} + +proto.getDistanceLimits = function(out) { + var bounds = this.radius.bounds + if(out) { + out[0] = Math.exp(bounds[0][0]) + out[1] = Math.exp(bounds[1][0]) + return out + } + return [ Math.exp(bounds[0][0]), Math.exp(bounds[1][0]) ] +} + +proto.toJSON = function() { + this.recalcMatrix(this.lastT()) + return { + center: this.computedCenter.slice(), + rotation: this.computedRotation.slice(), + distance: Math.log(this.computedRadius[0]), + zoomMin: this.radius.bounds[0][0], + zoomMax: this.radius.bounds[1][0] + } +} + +proto.fromJSON = function(options) { + var t = this.lastT() + var c = options.center + if(c) { + this.center.set(t, c[0], c[1], c[2]) + } + var r = options.rotation + if(r) { + this.rotation.set(t, r[0], r[1], r[2], r[3]) + } + var d = options.distance + if(d && d > 0) { + this.radius.set(t, Math.log(d)) + } + this.setDistanceLimits(options.zoomMin, options.zoomMax) +} + +function createOrbitController(options) { + options = options || {} + var center = options.center || [0,0,0] + var rotation = options.rotation || [0,0,0,1] + var radius = options.radius || 1.0 + + center = [].slice.call(center, 0, 3) + rotation = [].slice.call(rotation, 0, 4) + normalize4(rotation, rotation) + + var result = new OrbitCameraController( + rotation, + center, + Math.log(radius)) + + result.setDistanceLimits(options.zoomMin, options.zoomMax) + + if('eye' in options || 'up' in options) { + result.lookAt(0, options.eye, options.center, options.up) + } + + return result +} +},{"./lib/quatFromFrame":448,"filtered-vector":99,"gl-mat4/fromQuat":153,"gl-mat4/invert":156,"gl-mat4/lookAt":157}],450:[function(require,module,exports){ +/*! + * pad-left + * + * Copyright (c) 2014-2015, Jon Schlinkert. + * Licensed under the MIT license. + */ + +'use strict'; + +var repeat = require('repeat-string'); + +module.exports = function padLeft(str, num, ch) { + ch = typeof ch !== 'undefined' ? (ch + '') : ' '; + return repeat(ch, num) + str; +}; +},{"repeat-string":946}],451:[function(require,module,exports){ +'use strict'; + + +var TYPED_OK = (typeof Uint8Array !== 'undefined') && + (typeof Uint16Array !== 'undefined') && + (typeof Int32Array !== 'undefined'); + + +exports.assign = function (obj /*from1, from2, from3, ...*/) { + var sources = Array.prototype.slice.call(arguments, 1); + while (sources.length) { + var source = sources.shift(); + if (!source) { continue; } + + if (typeof source !== 'object') { + throw new TypeError(source + 'must be non-object'); + } + + for (var p in source) { + if (source.hasOwnProperty(p)) { + obj[p] = source[p]; + } + } + } + + return obj; +}; + + +// reduce buffer size, avoiding mem copy +exports.shrinkBuf = function (buf, size) { + if (buf.length === size) { return buf; } + if (buf.subarray) { return buf.subarray(0, size); } + buf.length = size; + return buf; +}; + + +var fnTyped = { + arraySet: function (dest, src, src_offs, len, dest_offs) { + if (src.subarray && dest.subarray) { + dest.set(src.subarray(src_offs, src_offs + len), dest_offs); + return; + } + // Fallback to ordinary array + for (var i = 0; i < len; i++) { + dest[dest_offs + i] = src[src_offs + i]; + } + }, + // Join array of chunks to single array. + flattenChunks: function (chunks) { + var i, l, len, pos, chunk, result; + + // calculate data length + len = 0; + for (i = 0, l = chunks.length; i < l; i++) { + len += chunks[i].length; + } + + // join chunks + result = new Uint8Array(len); + pos = 0; + for (i = 0, l = chunks.length; i < l; i++) { + chunk = chunks[i]; + result.set(chunk, pos); + pos += chunk.length; + } + + return result; + } +}; + +var fnUntyped = { + arraySet: function (dest, src, src_offs, len, dest_offs) { + for (var i = 0; i < len; i++) { + dest[dest_offs + i] = src[src_offs + i]; + } + }, + // Join array of chunks to single array. + flattenChunks: function (chunks) { + return [].concat.apply([], chunks); + } +}; + + +// Enable/Disable typed arrays use, for testing +// +exports.setTyped = function (on) { + if (on) { + exports.Buf8 = Uint8Array; + exports.Buf16 = Uint16Array; + exports.Buf32 = Int32Array; + exports.assign(exports, fnTyped); + } else { + exports.Buf8 = Array; + exports.Buf16 = Array; + exports.Buf32 = Array; + exports.assign(exports, fnUntyped); + } +}; + +exports.setTyped(TYPED_OK); + +},{}],452:[function(require,module,exports){ +'use strict'; + +// Note: adler32 takes 12% for level 0 and 2% for level 6. +// It doesn't worth to make additional optimizationa as in original. +// Small size is preferable. + +function adler32(adler, buf, len, pos) { + var s1 = (adler & 0xffff) |0, + s2 = ((adler >>> 16) & 0xffff) |0, + n = 0; + + while (len !== 0) { + // Set limit ~ twice less than 5552, to keep + // s2 in 31-bits, because we force signed ints. + // in other case %= will fail. + n = len > 2000 ? 2000 : len; + len -= n; + + do { + s1 = (s1 + buf[pos++]) |0; + s2 = (s2 + s1) |0; + } while (--n); + + s1 %= 65521; + s2 %= 65521; + } + + return (s1 | (s2 << 16)) |0; +} + + +module.exports = adler32; + +},{}],453:[function(require,module,exports){ +'use strict'; + + +module.exports = { + + /* Allowed flush values; see deflate() and inflate() below for details */ + Z_NO_FLUSH: 0, + Z_PARTIAL_FLUSH: 1, + Z_SYNC_FLUSH: 2, + Z_FULL_FLUSH: 3, + Z_FINISH: 4, + Z_BLOCK: 5, + Z_TREES: 6, + + /* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ + Z_OK: 0, + Z_STREAM_END: 1, + Z_NEED_DICT: 2, + Z_ERRNO: -1, + Z_STREAM_ERROR: -2, + Z_DATA_ERROR: -3, + //Z_MEM_ERROR: -4, + Z_BUF_ERROR: -5, + //Z_VERSION_ERROR: -6, + + /* compression levels */ + Z_NO_COMPRESSION: 0, + Z_BEST_SPEED: 1, + Z_BEST_COMPRESSION: 9, + Z_DEFAULT_COMPRESSION: -1, + + + Z_FILTERED: 1, + Z_HUFFMAN_ONLY: 2, + Z_RLE: 3, + Z_FIXED: 4, + Z_DEFAULT_STRATEGY: 0, + + /* Possible values of the data_type field (though see inflate()) */ + Z_BINARY: 0, + Z_TEXT: 1, + //Z_ASCII: 1, // = Z_TEXT (deprecated) + Z_UNKNOWN: 2, + + /* The deflate compression method */ + Z_DEFLATED: 8 + //Z_NULL: null // Use -1 or null inline, depending on var type +}; + +},{}],454:[function(require,module,exports){ +'use strict'; + +// Note: we can't get significant speed boost here. +// So write code to minimize size - no pregenerated tables +// and array tools dependencies. + + +// Use ordinary array, since untyped makes no boost here +function makeTable() { + var c, table = []; + + for (var n = 0; n < 256; n++) { + c = n; + for (var k = 0; k < 8; k++) { + c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); + } + table[n] = c; + } + + return table; +} + +// Create table on load. Just 255 signed longs. Not a problem. +var crcTable = makeTable(); + + +function crc32(crc, buf, len, pos) { + var t = crcTable, + end = pos + len; + + crc ^= -1; + + for (var i = pos; i < end; i++) { + crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF]; + } + + return (crc ^ (-1)); // >>> 0; +} + + +module.exports = crc32; + +},{}],455:[function(require,module,exports){ +'use strict'; + +var utils = require('../utils/common'); +var trees = require('./trees'); +var adler32 = require('./adler32'); +var crc32 = require('./crc32'); +var msg = require('./messages'); + +/* Public constants ==========================================================*/ +/* ===========================================================================*/ + + +/* Allowed flush values; see deflate() and inflate() below for details */ +var Z_NO_FLUSH = 0; +var Z_PARTIAL_FLUSH = 1; +//var Z_SYNC_FLUSH = 2; +var Z_FULL_FLUSH = 3; +var Z_FINISH = 4; +var Z_BLOCK = 5; +//var Z_TREES = 6; + + +/* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ +var Z_OK = 0; +var Z_STREAM_END = 1; +//var Z_NEED_DICT = 2; +//var Z_ERRNO = -1; +var Z_STREAM_ERROR = -2; +var Z_DATA_ERROR = -3; +//var Z_MEM_ERROR = -4; +var Z_BUF_ERROR = -5; +//var Z_VERSION_ERROR = -6; + + +/* compression levels */ +//var Z_NO_COMPRESSION = 0; +//var Z_BEST_SPEED = 1; +//var Z_BEST_COMPRESSION = 9; +var Z_DEFAULT_COMPRESSION = -1; + + +var Z_FILTERED = 1; +var Z_HUFFMAN_ONLY = 2; +var Z_RLE = 3; +var Z_FIXED = 4; +var Z_DEFAULT_STRATEGY = 0; + +/* Possible values of the data_type field (though see inflate()) */ +//var Z_BINARY = 0; +//var Z_TEXT = 1; +//var Z_ASCII = 1; // = Z_TEXT +var Z_UNKNOWN = 2; + + +/* The deflate compression method */ +var Z_DEFLATED = 8; + +/*============================================================================*/ + + +var MAX_MEM_LEVEL = 9; +/* Maximum value for memLevel in deflateInit2 */ +var MAX_WBITS = 15; +/* 32K LZ77 window */ +var DEF_MEM_LEVEL = 8; + + +var LENGTH_CODES = 29; +/* number of length codes, not counting the special END_BLOCK code */ +var LITERALS = 256; +/* number of literal bytes 0..255 */ +var L_CODES = LITERALS + 1 + LENGTH_CODES; +/* number of Literal or Length codes, including the END_BLOCK code */ +var D_CODES = 30; +/* number of distance codes */ +var BL_CODES = 19; +/* number of codes used to transfer the bit lengths */ +var HEAP_SIZE = 2 * L_CODES + 1; +/* maximum heap size */ +var MAX_BITS = 15; +/* All codes must not exceed MAX_BITS bits */ + +var MIN_MATCH = 3; +var MAX_MATCH = 258; +var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1); + +var PRESET_DICT = 0x20; + +var INIT_STATE = 42; +var EXTRA_STATE = 69; +var NAME_STATE = 73; +var COMMENT_STATE = 91; +var HCRC_STATE = 103; +var BUSY_STATE = 113; +var FINISH_STATE = 666; + +var BS_NEED_MORE = 1; /* block not completed, need more input or more output */ +var BS_BLOCK_DONE = 2; /* block flush performed */ +var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */ +var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */ + +var OS_CODE = 0x03; // Unix :) . Don't detect, use this default. + +function err(strm, errorCode) { + strm.msg = msg[errorCode]; + return errorCode; +} + +function rank(f) { + return ((f) << 1) - ((f) > 4 ? 9 : 0); +} + +function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } + + +/* ========================================================================= + * Flush as much pending output as possible. All deflate() output goes + * through this function so some applications may wish to modify it + * to avoid allocating a large strm->output buffer and copying into it. + * (See also read_buf()). + */ +function flush_pending(strm) { + var s = strm.state; + + //_tr_flush_bits(s); + var len = s.pending; + if (len > strm.avail_out) { + len = strm.avail_out; + } + if (len === 0) { return; } + + utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out); + strm.next_out += len; + s.pending_out += len; + strm.total_out += len; + strm.avail_out -= len; + s.pending -= len; + if (s.pending === 0) { + s.pending_out = 0; + } +} + + +function flush_block_only(s, last) { + trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last); + s.block_start = s.strstart; + flush_pending(s.strm); +} + + +function put_byte(s, b) { + s.pending_buf[s.pending++] = b; +} + + +/* ========================================================================= + * Put a short in the pending buffer. The 16-bit value is put in MSB order. + * IN assertion: the stream state is correct and there is enough room in + * pending_buf. + */ +function putShortMSB(s, b) { +// put_byte(s, (Byte)(b >> 8)); +// put_byte(s, (Byte)(b & 0xff)); + s.pending_buf[s.pending++] = (b >>> 8) & 0xff; + s.pending_buf[s.pending++] = b & 0xff; +} + + +/* =========================================================================== + * Read a new buffer from the current input stream, update the adler32 + * and total number of bytes read. All deflate() input goes through + * this function so some applications may wish to modify it to avoid + * allocating a large strm->input buffer and copying from it. + * (See also flush_pending()). + */ +function read_buf(strm, buf, start, size) { + var len = strm.avail_in; + + if (len > size) { len = size; } + if (len === 0) { return 0; } + + strm.avail_in -= len; + + // zmemcpy(buf, strm->next_in, len); + utils.arraySet(buf, strm.input, strm.next_in, len, start); + if (strm.state.wrap === 1) { + strm.adler = adler32(strm.adler, buf, len, start); + } + + else if (strm.state.wrap === 2) { + strm.adler = crc32(strm.adler, buf, len, start); + } + + strm.next_in += len; + strm.total_in += len; + + return len; +} + + +/* =========================================================================== + * Set match_start to the longest match starting at the given string and + * return its length. Matches shorter or equal to prev_length are discarded, + * in which case the result is equal to prev_length and match_start is + * garbage. + * IN assertions: cur_match is the head of the hash chain for the current + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 + * OUT assertion: the match length is not greater than s->lookahead. + */ +function longest_match(s, cur_match) { + var chain_length = s.max_chain_length; /* max hash chain length */ + var scan = s.strstart; /* current string */ + var match; /* matched string */ + var len; /* length of current match */ + var best_len = s.prev_length; /* best match length so far */ + var nice_match = s.nice_match; /* stop if match long enough */ + var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ? + s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/; + + var _win = s.window; // shortcut + + var wmask = s.w_mask; + var prev = s.prev; + + /* Stop when cur_match becomes <= limit. To simplify the code, + * we prevent matches with the string of window index 0. + */ + + var strend = s.strstart + MAX_MATCH; + var scan_end1 = _win[scan + best_len - 1]; + var scan_end = _win[scan + best_len]; + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + /* Do not waste too much time if we already have a good match: */ + if (s.prev_length >= s.good_match) { + chain_length >>= 2; + } + /* Do not look for matches beyond the end of the input. This is necessary + * to make deflate deterministic. + */ + if (nice_match > s.lookahead) { nice_match = s.lookahead; } + + // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + do { + // Assert(cur_match < s->strstart, "no future"); + match = cur_match; + + /* Skip to next match if the match length cannot increase + * or if the match length is less than 2. Note that the checks below + * for insufficient lookahead only occur occasionally for performance + * reasons. Therefore uninitialized memory will be accessed, and + * conditional jumps will be made that depend on those values. + * However the length of the match is limited to the lookahead, so + * the output of deflate is not affected by the uninitialized values. + */ + + if (_win[match + best_len] !== scan_end || + _win[match + best_len - 1] !== scan_end1 || + _win[match] !== _win[scan] || + _win[++match] !== _win[scan + 1]) { + continue; + } + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2; + match++; + // Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + /*jshint noempty:false*/ + } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && + _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && + _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && + _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && + scan < strend); + + // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (strend - scan); + scan = strend - MAX_MATCH; + + if (len > best_len) { + s.match_start = cur_match; + best_len = len; + if (len >= nice_match) { + break; + } + scan_end1 = _win[scan + best_len - 1]; + scan_end = _win[scan + best_len]; + } + } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0); + + if (best_len <= s.lookahead) { + return best_len; + } + return s.lookahead; +} + + +/* =========================================================================== + * Fill the window when the lookahead becomes insufficient. + * Updates strstart and lookahead. + * + * IN assertion: lookahead < MIN_LOOKAHEAD + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD + * At least one byte has been read, or avail_in == 0; reads are + * performed for at least two bytes (required for the zip translate_eol + * option -- not supported here). + */ +function fill_window(s) { + var _w_size = s.w_size; + var p, n, m, more, str; + + //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); + + do { + more = s.window_size - s.lookahead - s.strstart; + + // JS ints have 32 bit, block below not needed + /* Deal with !@#$% 64K limit: */ + //if (sizeof(int) <= 2) { + // if (more == 0 && s->strstart == 0 && s->lookahead == 0) { + // more = wsize; + // + // } else if (more == (unsigned)(-1)) { + // /* Very unlikely, but possible on 16 bit machine if + // * strstart == 0 && lookahead == 1 (input done a byte at time) + // */ + // more--; + // } + //} + + + /* If the window is almost full and there is insufficient lookahead, + * move the upper half to the lower one to make room in the upper half. + */ + if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) { + + utils.arraySet(s.window, s.window, _w_size, _w_size, 0); + s.match_start -= _w_size; + s.strstart -= _w_size; + /* we now have strstart >= MAX_DIST */ + s.block_start -= _w_size; + + /* Slide the hash table (could be avoided with 32 bit values + at the expense of memory usage). We slide even when level == 0 + to keep the hash table consistent if we switch back to level > 0 + later. (Using level 0 permanently is not an optimal usage of + zlib, so we don't care about this pathological case.) + */ + + n = s.hash_size; + p = n; + do { + m = s.head[--p]; + s.head[p] = (m >= _w_size ? m - _w_size : 0); + } while (--n); + + n = _w_size; + p = n; + do { + m = s.prev[--p]; + s.prev[p] = (m >= _w_size ? m - _w_size : 0); + /* If n is not on any hash chain, prev[n] is garbage but + * its value will never be used. + */ + } while (--n); + + more += _w_size; + } + if (s.strm.avail_in === 0) { + break; + } + + /* If there was no sliding: + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && + * more == window_size - lookahead - strstart + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) + * => more >= window_size - 2*WSIZE + 2 + * In the BIG_MEM or MMAP case (not yet supported), + * window_size == input_size + MIN_LOOKAHEAD && + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. + * Otherwise, window_size == 2*WSIZE so more >= 2. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2. + */ + //Assert(more >= 2, "more < 2"); + n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more); + s.lookahead += n; + + /* Initialize the hash value now that we have some input: */ + if (s.lookahead + s.insert >= MIN_MATCH) { + str = s.strstart - s.insert; + s.ins_h = s.window[str]; + + /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask; +//#if MIN_MATCH != 3 +// Call update_hash() MIN_MATCH-3 more times +//#endif + while (s.insert) { + /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask; + + s.prev[str & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = str; + str++; + s.insert--; + if (s.lookahead + s.insert < MIN_MATCH) { + break; + } + } + } + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, + * but this is not important since only literal bytes will be emitted. + */ + + } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0); + + /* If the WIN_INIT bytes after the end of the current data have never been + * written, then zero those bytes in order to avoid memory check reports of + * the use of uninitialized (or uninitialised as Julian writes) bytes by + * the longest match routines. Update the high water mark for the next + * time through here. WIN_INIT is set to MAX_MATCH since the longest match + * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. + */ +// if (s.high_water < s.window_size) { +// var curr = s.strstart + s.lookahead; +// var init = 0; +// +// if (s.high_water < curr) { +// /* Previous high water mark below current data -- zero WIN_INIT +// * bytes or up to end of window, whichever is less. +// */ +// init = s.window_size - curr; +// if (init > WIN_INIT) +// init = WIN_INIT; +// zmemzero(s->window + curr, (unsigned)init); +// s->high_water = curr + init; +// } +// else if (s->high_water < (ulg)curr + WIN_INIT) { +// /* High water mark at or above current data, but below current data +// * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up +// * to end of window, whichever is less. +// */ +// init = (ulg)curr + WIN_INIT - s->high_water; +// if (init > s->window_size - s->high_water) +// init = s->window_size - s->high_water; +// zmemzero(s->window + s->high_water, (unsigned)init); +// s->high_water += init; +// } +// } +// +// Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, +// "not enough room for search"); +} + +/* =========================================================================== + * Copy without compression as much as possible from the input stream, return + * the current block state. + * This function does not insert new strings in the dictionary since + * uncompressible data is probably not useful. This function is used + * only for the level=0 compression option. + * NOTE: this function should be optimized to avoid extra copying from + * window to pending_buf. + */ +function deflate_stored(s, flush) { + /* Stored blocks are limited to 0xffff bytes, pending_buf is limited + * to pending_buf_size, and each stored block has a 5 byte header: + */ + var max_block_size = 0xffff; + + if (max_block_size > s.pending_buf_size - 5) { + max_block_size = s.pending_buf_size - 5; + } + + /* Copy as much as possible from input to output: */ + for (;;) { + /* Fill the window as much as possible: */ + if (s.lookahead <= 1) { + + //Assert(s->strstart < s->w_size+MAX_DIST(s) || + // s->block_start >= (long)s->w_size, "slide too late"); +// if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) || +// s.block_start >= s.w_size)) { +// throw new Error("slide too late"); +// } + + fill_window(s); + if (s.lookahead === 0 && flush === Z_NO_FLUSH) { + return BS_NEED_MORE; + } + + if (s.lookahead === 0) { + break; + } + /* flush the current block */ + } + //Assert(s->block_start >= 0L, "block gone"); +// if (s.block_start < 0) throw new Error("block gone"); + + s.strstart += s.lookahead; + s.lookahead = 0; + + /* Emit a stored block if pending_buf will be full: */ + var max_start = s.block_start + max_block_size; + + if (s.strstart === 0 || s.strstart >= max_start) { + /* strstart == 0 is possible when wraparound on 16-bit machine */ + s.lookahead = s.strstart - max_start; + s.strstart = max_start; + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + + } + /* Flush if we may have to slide, otherwise block_start may become + * negative and the data will be gone: + */ + if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + } + + s.insert = 0; + + if (flush === Z_FINISH) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + return BS_FINISH_DONE; + } + + if (s.strstart > s.block_start) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + + return BS_NEED_MORE; +} + +/* =========================================================================== + * Compress as much as possible from the input stream, return the current + * block state. + * This function does not perform lazy evaluation of matches and inserts + * new strings in the dictionary only for unmatched strings or for short + * matches. It is used only for the fast compression options. + */ +function deflate_fast(s, flush) { + var hash_head; /* head of the hash chain */ + var bflush; /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s.lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) { + return BS_NEED_MORE; + } + if (s.lookahead === 0) { + break; /* flush the current block */ + } + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + hash_head = 0/*NIL*/; + if (s.lookahead >= MIN_MATCH) { + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + } + + /* Find the longest match, discarding those <= prev_length. + * At this point we have always match_length < MIN_MATCH + */ + if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s.match_length = longest_match(s, hash_head); + /* longest_match() sets match_start */ + } + if (s.match_length >= MIN_MATCH) { + // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only + + /*** _tr_tally_dist(s, s.strstart - s.match_start, + s.match_length - MIN_MATCH, bflush); ***/ + bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH); + + s.lookahead -= s.match_length; + + /* Insert new strings in the hash table only if the match length + * is not too large. This saves time but degrades compression. + */ + if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) { + s.match_length--; /* string at strstart already in table */ + do { + s.strstart++; + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + /* strstart never exceeds WSIZE-MAX_MATCH, so there are + * always MIN_MATCH bytes ahead. + */ + } while (--s.match_length !== 0); + s.strstart++; + } else + { + s.strstart += s.match_length; + s.match_length = 0; + s.ins_h = s.window[s.strstart]; + /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask; + +//#if MIN_MATCH != 3 +// Call UPDATE_HASH() MIN_MATCH-3 more times +//#endif + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not + * matter since it will be recomputed at next deflate call. + */ + } + } else { + /* No match, output a literal byte */ + //Tracevv((stderr,"%c", s.window[s.strstart])); + /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ + bflush = trees._tr_tally(s, 0, s.window[s.strstart]); + + s.lookahead--; + s.strstart++; + } + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + } + s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1); + if (flush === Z_FINISH) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + return BS_FINISH_DONE; + } + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + return BS_BLOCK_DONE; +} + +/* =========================================================================== + * Same as above, but achieves better compression. We use a lazy + * evaluation for matches: a match is finally adopted only if there is + * no better match at the next window position. + */ +function deflate_slow(s, flush) { + var hash_head; /* head of hash chain */ + var bflush; /* set if current block must be flushed */ + + var max_insert; + + /* Process the input block. */ + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s.lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) { + return BS_NEED_MORE; + } + if (s.lookahead === 0) { break; } /* flush the current block */ + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + hash_head = 0/*NIL*/; + if (s.lookahead >= MIN_MATCH) { + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + } + + /* Find the longest match, discarding those <= prev_length. + */ + s.prev_length = s.match_length; + s.prev_match = s.match_start; + s.match_length = MIN_MATCH - 1; + + if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match && + s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s.match_length = longest_match(s, hash_head); + /* longest_match() sets match_start */ + + if (s.match_length <= 5 && + (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) { + + /* If prev_match is also MIN_MATCH, match_start is garbage + * but we will ignore the current match anyway. + */ + s.match_length = MIN_MATCH - 1; + } + } + /* If there was a match at the previous step and the current + * match is not better, output the previous match: + */ + if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) { + max_insert = s.strstart + s.lookahead - MIN_MATCH; + /* Do not insert strings in hash table beyond this. */ + + //check_match(s, s.strstart-1, s.prev_match, s.prev_length); + + /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match, + s.prev_length - MIN_MATCH, bflush);***/ + bflush = trees._tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH); + /* Insert in hash table all strings up to the end of the match. + * strstart-1 and strstart are already inserted. If there is not + * enough lookahead, the last two strings are not inserted in + * the hash table. + */ + s.lookahead -= s.prev_length - 1; + s.prev_length -= 2; + do { + if (++s.strstart <= max_insert) { + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask; + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + } + } while (--s.prev_length !== 0); + s.match_available = 0; + s.match_length = MIN_MATCH - 1; + s.strstart++; + + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + + } else if (s.match_available) { + /* If there was no match at the previous position, output a + * single literal. If there was a match but the current match + * is longer, truncate the previous match to a single literal. + */ + //Tracevv((stderr,"%c", s->window[s->strstart-1])); + /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ + bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]); + + if (bflush) { + /*** FLUSH_BLOCK_ONLY(s, 0) ***/ + flush_block_only(s, false); + /***/ + } + s.strstart++; + s.lookahead--; + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + } else { + /* There is no previous match to compare with, wait for + * the next step to decide. + */ + s.match_available = 1; + s.strstart++; + s.lookahead--; + } + } + //Assert (flush != Z_NO_FLUSH, "no flush?"); + if (s.match_available) { + //Tracevv((stderr,"%c", s->window[s->strstart-1])); + /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ + bflush = trees._tr_tally(s, 0, s.window[s.strstart - 1]); + + s.match_available = 0; + } + s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1; + if (flush === Z_FINISH) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + return BS_FINISH_DONE; + } + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + + return BS_BLOCK_DONE; +} + + +/* =========================================================================== + * For Z_RLE, simply look for runs of bytes, generate matches only of distance + * one. Do not maintain a hash table. (It will be regenerated if this run of + * deflate switches away from Z_RLE.) + */ +function deflate_rle(s, flush) { + var bflush; /* set if current block must be flushed */ + var prev; /* byte at distance one to match */ + var scan, strend; /* scan goes up to strend for length of run */ + + var _win = s.window; + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the longest run, plus one for the unrolled loop. + */ + if (s.lookahead <= MAX_MATCH) { + fill_window(s); + if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) { + return BS_NEED_MORE; + } + if (s.lookahead === 0) { break; } /* flush the current block */ + } + + /* See how many times the previous byte repeats */ + s.match_length = 0; + if (s.lookahead >= MIN_MATCH && s.strstart > 0) { + scan = s.strstart - 1; + prev = _win[scan]; + if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) { + strend = s.strstart + MAX_MATCH; + do { + /*jshint noempty:false*/ + } while (prev === _win[++scan] && prev === _win[++scan] && + prev === _win[++scan] && prev === _win[++scan] && + prev === _win[++scan] && prev === _win[++scan] && + prev === _win[++scan] && prev === _win[++scan] && + scan < strend); + s.match_length = MAX_MATCH - (strend - scan); + if (s.match_length > s.lookahead) { + s.match_length = s.lookahead; + } + } + //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); + } + + /* Emit match if have run of MIN_MATCH or longer, else emit literal */ + if (s.match_length >= MIN_MATCH) { + //check_match(s, s.strstart, s.strstart - 1, s.match_length); + + /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/ + bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH); + + s.lookahead -= s.match_length; + s.strstart += s.match_length; + s.match_length = 0; + } else { + /* No match, output a literal byte */ + //Tracevv((stderr,"%c", s->window[s->strstart])); + /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ + bflush = trees._tr_tally(s, 0, s.window[s.strstart]); + + s.lookahead--; + s.strstart++; + } + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + } + s.insert = 0; + if (flush === Z_FINISH) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + return BS_FINISH_DONE; + } + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + return BS_BLOCK_DONE; +} + +/* =========================================================================== + * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. + * (It will be regenerated if this run of deflate switches away from Huffman.) + */ +function deflate_huff(s, flush) { + var bflush; /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we have a literal to write. */ + if (s.lookahead === 0) { + fill_window(s); + if (s.lookahead === 0) { + if (flush === Z_NO_FLUSH) { + return BS_NEED_MORE; + } + break; /* flush the current block */ + } + } + + /* Output a literal byte */ + s.match_length = 0; + //Tracevv((stderr,"%c", s->window[s->strstart])); + /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ + bflush = trees._tr_tally(s, 0, s.window[s.strstart]); + s.lookahead--; + s.strstart++; + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + } + s.insert = 0; + if (flush === Z_FINISH) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + return BS_FINISH_DONE; + } + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + } + return BS_BLOCK_DONE; +} + +/* Values for max_lazy_match, good_match and max_chain_length, depending on + * the desired pack level (0..9). The values given below have been tuned to + * exclude worst case performance for pathological files. Better values may be + * found for specific files. + */ +function Config(good_length, max_lazy, nice_length, max_chain, func) { + this.good_length = good_length; + this.max_lazy = max_lazy; + this.nice_length = nice_length; + this.max_chain = max_chain; + this.func = func; +} + +var configuration_table; + +configuration_table = [ + /* good lazy nice chain */ + new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */ + new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */ + new Config(4, 5, 16, 8, deflate_fast), /* 2 */ + new Config(4, 6, 32, 32, deflate_fast), /* 3 */ + + new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */ + new Config(8, 16, 32, 32, deflate_slow), /* 5 */ + new Config(8, 16, 128, 128, deflate_slow), /* 6 */ + new Config(8, 32, 128, 256, deflate_slow), /* 7 */ + new Config(32, 128, 258, 1024, deflate_slow), /* 8 */ + new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */ +]; + + +/* =========================================================================== + * Initialize the "longest match" routines for a new zlib stream + */ +function lm_init(s) { + s.window_size = 2 * s.w_size; + + /*** CLEAR_HASH(s); ***/ + zero(s.head); // Fill with NIL (= 0); + + /* Set the default configuration parameters: + */ + s.max_lazy_match = configuration_table[s.level].max_lazy; + s.good_match = configuration_table[s.level].good_length; + s.nice_match = configuration_table[s.level].nice_length; + s.max_chain_length = configuration_table[s.level].max_chain; + + s.strstart = 0; + s.block_start = 0; + s.lookahead = 0; + s.insert = 0; + s.match_length = s.prev_length = MIN_MATCH - 1; + s.match_available = 0; + s.ins_h = 0; +} + + +function DeflateState() { + this.strm = null; /* pointer back to this zlib stream */ + this.status = 0; /* as the name implies */ + this.pending_buf = null; /* output still pending */ + this.pending_buf_size = 0; /* size of pending_buf */ + this.pending_out = 0; /* next pending byte to output to the stream */ + this.pending = 0; /* nb of bytes in the pending buffer */ + this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ + this.gzhead = null; /* gzip header information to write */ + this.gzindex = 0; /* where in extra, name, or comment */ + this.method = Z_DEFLATED; /* can only be DEFLATED */ + this.last_flush = -1; /* value of flush param for previous deflate call */ + + this.w_size = 0; /* LZ77 window size (32K by default) */ + this.w_bits = 0; /* log2(w_size) (8..16) */ + this.w_mask = 0; /* w_size - 1 */ + + this.window = null; + /* Sliding window. Input bytes are read into the second half of the window, + * and move to the first half later to keep a dictionary of at least wSize + * bytes. With this organization, matches are limited to a distance of + * wSize-MAX_MATCH bytes, but this ensures that IO is always + * performed with a length multiple of the block size. + */ + + this.window_size = 0; + /* Actual size of window: 2*wSize, except when the user input buffer + * is directly used as sliding window. + */ + + this.prev = null; + /* Link to older string with same hash index. To limit the size of this + * array to 64K, this link is maintained only for the last 32K strings. + * An index in this array is thus a window index modulo 32K. + */ + + this.head = null; /* Heads of the hash chains or NIL. */ + + this.ins_h = 0; /* hash index of string to be inserted */ + this.hash_size = 0; /* number of elements in hash table */ + this.hash_bits = 0; /* log2(hash_size) */ + this.hash_mask = 0; /* hash_size-1 */ + + this.hash_shift = 0; + /* Number of bits by which ins_h must be shifted at each input + * step. It must be such that after MIN_MATCH steps, the oldest + * byte no longer takes part in the hash key, that is: + * hash_shift * MIN_MATCH >= hash_bits + */ + + this.block_start = 0; + /* Window position at the beginning of the current output block. Gets + * negative when the window is moved backwards. + */ + + this.match_length = 0; /* length of best match */ + this.prev_match = 0; /* previous match */ + this.match_available = 0; /* set if previous match exists */ + this.strstart = 0; /* start of string to insert */ + this.match_start = 0; /* start of matching string */ + this.lookahead = 0; /* number of valid bytes ahead in window */ + + this.prev_length = 0; + /* Length of the best match at previous step. Matches not greater than this + * are discarded. This is used in the lazy match evaluation. + */ + + this.max_chain_length = 0; + /* To speed up deflation, hash chains are never searched beyond this + * length. A higher limit improves compression ratio but degrades the + * speed. + */ + + this.max_lazy_match = 0; + /* Attempt to find a better match only when the current match is strictly + * smaller than this value. This mechanism is used only for compression + * levels >= 4. + */ + // That's alias to max_lazy_match, don't use directly + //this.max_insert_length = 0; + /* Insert new strings in the hash table only if the match length is not + * greater than this length. This saves time but degrades compression. + * max_insert_length is used only for compression levels <= 3. + */ + + this.level = 0; /* compression level (1..9) */ + this.strategy = 0; /* favor or force Huffman coding*/ + + this.good_match = 0; + /* Use a faster search when the previous match is longer than this */ + + this.nice_match = 0; /* Stop searching when current match exceeds this */ + + /* used by trees.c: */ + + /* Didn't use ct_data typedef below to suppress compiler warning */ + + // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ + // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ + // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ + + // Use flat array of DOUBLE size, with interleaved fata, + // because JS does not support effective + this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2); + this.dyn_dtree = new utils.Buf16((2 * D_CODES + 1) * 2); + this.bl_tree = new utils.Buf16((2 * BL_CODES + 1) * 2); + zero(this.dyn_ltree); + zero(this.dyn_dtree); + zero(this.bl_tree); + + this.l_desc = null; /* desc. for literal tree */ + this.d_desc = null; /* desc. for distance tree */ + this.bl_desc = null; /* desc. for bit length tree */ + + //ush bl_count[MAX_BITS+1]; + this.bl_count = new utils.Buf16(MAX_BITS + 1); + /* number of codes at each bit length for an optimal tree */ + + //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ + this.heap = new utils.Buf16(2 * L_CODES + 1); /* heap used to build the Huffman trees */ + zero(this.heap); + + this.heap_len = 0; /* number of elements in the heap */ + this.heap_max = 0; /* element of largest frequency */ + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. + * The same heap array is used to build all trees. + */ + + this.depth = new utils.Buf16(2 * L_CODES + 1); //uch depth[2*L_CODES+1]; + zero(this.depth); + /* Depth of each subtree used as tie breaker for trees of equal frequency + */ + + this.l_buf = 0; /* buffer index for literals or lengths */ + + this.lit_bufsize = 0; + /* Size of match buffer for literals/lengths. There are 4 reasons for + * limiting lit_bufsize to 64K: + * - frequencies can be kept in 16 bit counters + * - if compression is not successful for the first block, all input + * data is still in the window so we can still emit a stored block even + * when input comes from standard input. (This can also be done for + * all blocks if lit_bufsize is not greater than 32K.) + * - if compression is not successful for a file smaller than 64K, we can + * even emit a stored file instead of a stored block (saving 5 bytes). + * This is applicable only for zip (not gzip or zlib). + * - creating new Huffman trees less frequently may not provide fast + * adaptation to changes in the input data statistics. (Take for + * example a binary file with poorly compressible code followed by + * a highly compressible string table.) Smaller buffer sizes give + * fast adaptation but have of course the overhead of transmitting + * trees more frequently. + * - I can't count above 4 + */ + + this.last_lit = 0; /* running index in l_buf */ + + this.d_buf = 0; + /* Buffer index for distances. To simplify the code, d_buf and l_buf have + * the same number of elements. To use different lengths, an extra flag + * array would be necessary. + */ + + this.opt_len = 0; /* bit length of current block with optimal trees */ + this.static_len = 0; /* bit length of current block with static trees */ + this.matches = 0; /* number of string matches in current block */ + this.insert = 0; /* bytes at end of window left to insert */ + + + this.bi_buf = 0; + /* Output buffer. bits are inserted starting at the bottom (least + * significant bits). + */ + this.bi_valid = 0; + /* Number of valid bits in bi_buf. All bits above the last valid bit + * are always zero. + */ + + // Used for window memory init. We safely ignore it for JS. That makes + // sense only for pointers and memory check tools. + //this.high_water = 0; + /* High water mark offset in window for initialized bytes -- bytes above + * this are set to zero in order to avoid memory check warnings when + * longest match routines access bytes past the input. This is then + * updated to the new high water mark. + */ +} + + +function deflateResetKeep(strm) { + var s; + + if (!strm || !strm.state) { + return err(strm, Z_STREAM_ERROR); + } + + strm.total_in = strm.total_out = 0; + strm.data_type = Z_UNKNOWN; + + s = strm.state; + s.pending = 0; + s.pending_out = 0; + + if (s.wrap < 0) { + s.wrap = -s.wrap; + /* was made negative by deflate(..., Z_FINISH); */ + } + s.status = (s.wrap ? INIT_STATE : BUSY_STATE); + strm.adler = (s.wrap === 2) ? + 0 // crc32(0, Z_NULL, 0) + : + 1; // adler32(0, Z_NULL, 0) + s.last_flush = Z_NO_FLUSH; + trees._tr_init(s); + return Z_OK; +} + + +function deflateReset(strm) { + var ret = deflateResetKeep(strm); + if (ret === Z_OK) { + lm_init(strm.state); + } + return ret; +} + + +function deflateSetHeader(strm, head) { + if (!strm || !strm.state) { return Z_STREAM_ERROR; } + if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; } + strm.state.gzhead = head; + return Z_OK; +} + + +function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { + if (!strm) { // === Z_NULL + return Z_STREAM_ERROR; + } + var wrap = 1; + + if (level === Z_DEFAULT_COMPRESSION) { + level = 6; + } + + if (windowBits < 0) { /* suppress zlib wrapper */ + wrap = 0; + windowBits = -windowBits; + } + + else if (windowBits > 15) { + wrap = 2; /* write gzip wrapper instead */ + windowBits -= 16; + } + + + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED || + windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || + strategy < 0 || strategy > Z_FIXED) { + return err(strm, Z_STREAM_ERROR); + } + + + if (windowBits === 8) { + windowBits = 9; + } + /* until 256-byte window bug fixed */ + + var s = new DeflateState(); + + strm.state = s; + s.strm = strm; + + s.wrap = wrap; + s.gzhead = null; + s.w_bits = windowBits; + s.w_size = 1 << s.w_bits; + s.w_mask = s.w_size - 1; + + s.hash_bits = memLevel + 7; + s.hash_size = 1 << s.hash_bits; + s.hash_mask = s.hash_size - 1; + s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH); + + s.window = new utils.Buf8(s.w_size * 2); + s.head = new utils.Buf16(s.hash_size); + s.prev = new utils.Buf16(s.w_size); + + // Don't need mem init magic for JS. + //s.high_water = 0; /* nothing written to s->window yet */ + + s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ + + s.pending_buf_size = s.lit_bufsize * 4; + + //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); + //s->pending_buf = (uchf *) overlay; + s.pending_buf = new utils.Buf8(s.pending_buf_size); + + // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`) + //s->d_buf = overlay + s->lit_bufsize/sizeof(ush); + s.d_buf = 1 * s.lit_bufsize; + + //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; + s.l_buf = (1 + 2) * s.lit_bufsize; + + s.level = level; + s.strategy = strategy; + s.method = method; + + return deflateReset(strm); +} + +function deflateInit(strm, level) { + return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); +} + + +function deflate(strm, flush) { + var old_flush, s; + var beg, val; // for gzip header write only + + if (!strm || !strm.state || + flush > Z_BLOCK || flush < 0) { + return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR; + } + + s = strm.state; + + if (!strm.output || + (!strm.input && strm.avail_in !== 0) || + (s.status === FINISH_STATE && flush !== Z_FINISH)) { + return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR); + } + + s.strm = strm; /* just in case */ + old_flush = s.last_flush; + s.last_flush = flush; + + /* Write the header */ + if (s.status === INIT_STATE) { + + if (s.wrap === 2) { // GZIP header + strm.adler = 0; //crc32(0L, Z_NULL, 0); + put_byte(s, 31); + put_byte(s, 139); + put_byte(s, 8); + if (!s.gzhead) { // s->gzhead == Z_NULL + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, s.level === 9 ? 2 : + (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? + 4 : 0)); + put_byte(s, OS_CODE); + s.status = BUSY_STATE; + } + else { + put_byte(s, (s.gzhead.text ? 1 : 0) + + (s.gzhead.hcrc ? 2 : 0) + + (!s.gzhead.extra ? 0 : 4) + + (!s.gzhead.name ? 0 : 8) + + (!s.gzhead.comment ? 0 : 16) + ); + put_byte(s, s.gzhead.time & 0xff); + put_byte(s, (s.gzhead.time >> 8) & 0xff); + put_byte(s, (s.gzhead.time >> 16) & 0xff); + put_byte(s, (s.gzhead.time >> 24) & 0xff); + put_byte(s, s.level === 9 ? 2 : + (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? + 4 : 0)); + put_byte(s, s.gzhead.os & 0xff); + if (s.gzhead.extra && s.gzhead.extra.length) { + put_byte(s, s.gzhead.extra.length & 0xff); + put_byte(s, (s.gzhead.extra.length >> 8) & 0xff); + } + if (s.gzhead.hcrc) { + strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0); + } + s.gzindex = 0; + s.status = EXTRA_STATE; + } + } + else // DEFLATE header + { + var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8; + var level_flags = -1; + + if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) { + level_flags = 0; + } else if (s.level < 6) { + level_flags = 1; + } else if (s.level === 6) { + level_flags = 2; + } else { + level_flags = 3; + } + header |= (level_flags << 6); + if (s.strstart !== 0) { header |= PRESET_DICT; } + header += 31 - (header % 31); + + s.status = BUSY_STATE; + putShortMSB(s, header); + + /* Save the adler32 of the preset dictionary: */ + if (s.strstart !== 0) { + putShortMSB(s, strm.adler >>> 16); + putShortMSB(s, strm.adler & 0xffff); + } + strm.adler = 1; // adler32(0L, Z_NULL, 0); + } + } + +//#ifdef GZIP + if (s.status === EXTRA_STATE) { + if (s.gzhead.extra/* != Z_NULL*/) { + beg = s.pending; /* start of bytes to update crc */ + + while (s.gzindex < (s.gzhead.extra.length & 0xffff)) { + if (s.pending === s.pending_buf_size) { + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); + } + flush_pending(strm); + beg = s.pending; + if (s.pending === s.pending_buf_size) { + break; + } + } + put_byte(s, s.gzhead.extra[s.gzindex] & 0xff); + s.gzindex++; + } + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); + } + if (s.gzindex === s.gzhead.extra.length) { + s.gzindex = 0; + s.status = NAME_STATE; + } + } + else { + s.status = NAME_STATE; + } + } + if (s.status === NAME_STATE) { + if (s.gzhead.name/* != Z_NULL*/) { + beg = s.pending; /* start of bytes to update crc */ + //int val; + + do { + if (s.pending === s.pending_buf_size) { + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); + } + flush_pending(strm); + beg = s.pending; + if (s.pending === s.pending_buf_size) { + val = 1; + break; + } + } + // JS specific: little magic to add zero terminator to end of string + if (s.gzindex < s.gzhead.name.length) { + val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff; + } else { + val = 0; + } + put_byte(s, val); + } while (val !== 0); + + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); + } + if (val === 0) { + s.gzindex = 0; + s.status = COMMENT_STATE; + } + } + else { + s.status = COMMENT_STATE; + } + } + if (s.status === COMMENT_STATE) { + if (s.gzhead.comment/* != Z_NULL*/) { + beg = s.pending; /* start of bytes to update crc */ + //int val; + + do { + if (s.pending === s.pending_buf_size) { + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); + } + flush_pending(strm); + beg = s.pending; + if (s.pending === s.pending_buf_size) { + val = 1; + break; + } + } + // JS specific: little magic to add zero terminator to end of string + if (s.gzindex < s.gzhead.comment.length) { + val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff; + } else { + val = 0; + } + put_byte(s, val); + } while (val !== 0); + + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg); + } + if (val === 0) { + s.status = HCRC_STATE; + } + } + else { + s.status = HCRC_STATE; + } + } + if (s.status === HCRC_STATE) { + if (s.gzhead.hcrc) { + if (s.pending + 2 > s.pending_buf_size) { + flush_pending(strm); + } + if (s.pending + 2 <= s.pending_buf_size) { + put_byte(s, strm.adler & 0xff); + put_byte(s, (strm.adler >> 8) & 0xff); + strm.adler = 0; //crc32(0L, Z_NULL, 0); + s.status = BUSY_STATE; + } + } + else { + s.status = BUSY_STATE; + } + } +//#endif + + /* Flush as much pending output as possible */ + if (s.pending !== 0) { + flush_pending(strm); + if (strm.avail_out === 0) { + /* Since avail_out is 0, deflate will be called again with + * more output space, but possibly with both pending and + * avail_in equal to zero. There won't be anything to do, + * but this is not an error situation so make sure we + * return OK instead of BUF_ERROR at next call of deflate: + */ + s.last_flush = -1; + return Z_OK; + } + + /* Make sure there is something to do and avoid duplicate consecutive + * flushes. For repeated and useless calls with Z_FINISH, we keep + * returning Z_STREAM_END instead of Z_BUF_ERROR. + */ + } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) && + flush !== Z_FINISH) { + return err(strm, Z_BUF_ERROR); + } + + /* User must not provide more input after the first FINISH: */ + if (s.status === FINISH_STATE && strm.avail_in !== 0) { + return err(strm, Z_BUF_ERROR); + } + + /* Start a new block or continue the current one. + */ + if (strm.avail_in !== 0 || s.lookahead !== 0 || + (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) { + var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) : + (s.strategy === Z_RLE ? deflate_rle(s, flush) : + configuration_table[s.level].func(s, flush)); + + if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) { + s.status = FINISH_STATE; + } + if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) { + if (strm.avail_out === 0) { + s.last_flush = -1; + /* avoid BUF_ERROR next call, see above */ + } + return Z_OK; + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call + * of deflate should use the same flush parameter to make sure + * that the flush is complete. So we don't have to output an + * empty block here, this will be done at next call. This also + * ensures that for a very small output buffer, we emit at most + * one empty block. + */ + } + if (bstate === BS_BLOCK_DONE) { + if (flush === Z_PARTIAL_FLUSH) { + trees._tr_align(s); + } + else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ + + trees._tr_stored_block(s, 0, 0, false); + /* For a full flush, this empty block will be recognized + * as a special marker by inflate_sync(). + */ + if (flush === Z_FULL_FLUSH) { + /*** CLEAR_HASH(s); ***/ /* forget history */ + zero(s.head); // Fill with NIL (= 0); + + if (s.lookahead === 0) { + s.strstart = 0; + s.block_start = 0; + s.insert = 0; + } + } + } + flush_pending(strm); + if (strm.avail_out === 0) { + s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */ + return Z_OK; + } + } + } + //Assert(strm->avail_out > 0, "bug2"); + //if (strm.avail_out <= 0) { throw new Error("bug2");} + + if (flush !== Z_FINISH) { return Z_OK; } + if (s.wrap <= 0) { return Z_STREAM_END; } + + /* Write the trailer */ + if (s.wrap === 2) { + put_byte(s, strm.adler & 0xff); + put_byte(s, (strm.adler >> 8) & 0xff); + put_byte(s, (strm.adler >> 16) & 0xff); + put_byte(s, (strm.adler >> 24) & 0xff); + put_byte(s, strm.total_in & 0xff); + put_byte(s, (strm.total_in >> 8) & 0xff); + put_byte(s, (strm.total_in >> 16) & 0xff); + put_byte(s, (strm.total_in >> 24) & 0xff); + } + else + { + putShortMSB(s, strm.adler >>> 16); + putShortMSB(s, strm.adler & 0xffff); + } + + flush_pending(strm); + /* If avail_out is zero, the application will call deflate again + * to flush the rest. + */ + if (s.wrap > 0) { s.wrap = -s.wrap; } + /* write the trailer only once! */ + return s.pending !== 0 ? Z_OK : Z_STREAM_END; +} + +function deflateEnd(strm) { + var status; + + if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) { + return Z_STREAM_ERROR; + } + + status = strm.state.status; + if (status !== INIT_STATE && + status !== EXTRA_STATE && + status !== NAME_STATE && + status !== COMMENT_STATE && + status !== HCRC_STATE && + status !== BUSY_STATE && + status !== FINISH_STATE + ) { + return err(strm, Z_STREAM_ERROR); + } + + strm.state = null; + + return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK; +} + + +/* ========================================================================= + * Initializes the compression dictionary from the given byte + * sequence without producing any compressed output. + */ +function deflateSetDictionary(strm, dictionary) { + var dictLength = dictionary.length; + + var s; + var str, n; + var wrap; + var avail; + var next; + var input; + var tmpDict; + + if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) { + return Z_STREAM_ERROR; + } + + s = strm.state; + wrap = s.wrap; + + if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) { + return Z_STREAM_ERROR; + } + + /* when using zlib wrappers, compute Adler-32 for provided dictionary */ + if (wrap === 1) { + /* adler32(strm->adler, dictionary, dictLength); */ + strm.adler = adler32(strm.adler, dictionary, dictLength, 0); + } + + s.wrap = 0; /* avoid computing Adler-32 in read_buf */ + + /* if dictionary would fill window, just replace the history */ + if (dictLength >= s.w_size) { + if (wrap === 0) { /* already empty otherwise */ + /*** CLEAR_HASH(s); ***/ + zero(s.head); // Fill with NIL (= 0); + s.strstart = 0; + s.block_start = 0; + s.insert = 0; + } + /* use the tail */ + // dictionary = dictionary.slice(dictLength - s.w_size); + tmpDict = new utils.Buf8(s.w_size); + utils.arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0); + dictionary = tmpDict; + dictLength = s.w_size; + } + /* insert dictionary into window and hash */ + avail = strm.avail_in; + next = strm.next_in; + input = strm.input; + strm.avail_in = dictLength; + strm.next_in = 0; + strm.input = dictionary; + fill_window(s); + while (s.lookahead >= MIN_MATCH) { + str = s.strstart; + n = s.lookahead - (MIN_MATCH - 1); + do { + /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ + s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH - 1]) & s.hash_mask; + + s.prev[str & s.w_mask] = s.head[s.ins_h]; + + s.head[s.ins_h] = str; + str++; + } while (--n); + s.strstart = str; + s.lookahead = MIN_MATCH - 1; + fill_window(s); + } + s.strstart += s.lookahead; + s.block_start = s.strstart; + s.insert = s.lookahead; + s.lookahead = 0; + s.match_length = s.prev_length = MIN_MATCH - 1; + s.match_available = 0; + strm.next_in = next; + strm.input = input; + strm.avail_in = avail; + s.wrap = wrap; + return Z_OK; +} + + +exports.deflateInit = deflateInit; +exports.deflateInit2 = deflateInit2; +exports.deflateReset = deflateReset; +exports.deflateResetKeep = deflateResetKeep; +exports.deflateSetHeader = deflateSetHeader; +exports.deflate = deflate; +exports.deflateEnd = deflateEnd; +exports.deflateSetDictionary = deflateSetDictionary; +exports.deflateInfo = 'pako deflate (from Nodeca project)'; + +/* Not implemented +exports.deflateBound = deflateBound; +exports.deflateCopy = deflateCopy; +exports.deflateParams = deflateParams; +exports.deflatePending = deflatePending; +exports.deflatePrime = deflatePrime; +exports.deflateTune = deflateTune; +*/ + +},{"../utils/common":451,"./adler32":452,"./crc32":454,"./messages":459,"./trees":460}],456:[function(require,module,exports){ +'use strict'; + +// See state defs from inflate.js +var BAD = 30; /* got a data error -- remain here until reset */ +var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ + +/* + Decode literal, length, and distance codes and write out the resulting + literal and match bytes until either not enough input or output is + available, an end-of-block is encountered, or a data error is encountered. + When large enough input and output buffers are supplied to inflate(), for + example, a 16K input buffer and a 64K output buffer, more than 95% of the + inflate execution time is spent in this routine. + + Entry assumptions: + + state.mode === LEN + strm.avail_in >= 6 + strm.avail_out >= 258 + start >= strm.avail_out + state.bits < 8 + + On return, state.mode is one of: + + LEN -- ran out of enough output space or enough available input + TYPE -- reached end of block code, inflate() to interpret next block + BAD -- error in block data + + Notes: + + - The maximum input bits used by a length/distance pair is 15 bits for the + length code, 5 bits for the length extra, 15 bits for the distance code, + and 13 bits for the distance extra. This totals 48 bits, or six bytes. + Therefore if strm.avail_in >= 6, then there is enough input to avoid + checking for available input while decoding. + + - The maximum bytes that a single length/distance pair can output is 258 + bytes, which is the maximum length that can be coded. inflate_fast() + requires strm.avail_out >= 258 for each loop to avoid checking for + output space. + */ +module.exports = function inflate_fast(strm, start) { + var state; + var _in; /* local strm.input */ + var last; /* have enough input while in < last */ + var _out; /* local strm.output */ + var beg; /* inflate()'s initial strm.output */ + var end; /* while out < end, enough space available */ +//#ifdef INFLATE_STRICT + var dmax; /* maximum distance from zlib header */ +//#endif + var wsize; /* window size or zero if not using window */ + var whave; /* valid bytes in the window */ + var wnext; /* window write index */ + // Use `s_window` instead `window`, avoid conflict with instrumentation tools + var s_window; /* allocated sliding window, if wsize != 0 */ + var hold; /* local strm.hold */ + var bits; /* local strm.bits */ + var lcode; /* local strm.lencode */ + var dcode; /* local strm.distcode */ + var lmask; /* mask for first level of length codes */ + var dmask; /* mask for first level of distance codes */ + var here; /* retrieved table entry */ + var op; /* code bits, operation, extra bits, or */ + /* window position, window bytes to copy */ + var len; /* match length, unused bytes */ + var dist; /* match distance */ + var from; /* where to copy match from */ + var from_source; + + + var input, output; // JS specific, because we have no pointers + + /* copy state to local variables */ + state = strm.state; + //here = state.here; + _in = strm.next_in; + input = strm.input; + last = _in + (strm.avail_in - 5); + _out = strm.next_out; + output = strm.output; + beg = _out - (start - strm.avail_out); + end = _out + (strm.avail_out - 257); +//#ifdef INFLATE_STRICT + dmax = state.dmax; +//#endif + wsize = state.wsize; + whave = state.whave; + wnext = state.wnext; + s_window = state.window; + hold = state.hold; + bits = state.bits; + lcode = state.lencode; + dcode = state.distcode; + lmask = (1 << state.lenbits) - 1; + dmask = (1 << state.distbits) - 1; + + + /* decode literals and length/distances until end-of-block or not enough + input data or output space */ + + top: + do { + if (bits < 15) { + hold += input[_in++] << bits; + bits += 8; + hold += input[_in++] << bits; + bits += 8; + } + + here = lcode[hold & lmask]; + + dolen: + for (;;) { // Goto emulation + op = here >>> 24/*here.bits*/; + hold >>>= op; + bits -= op; + op = (here >>> 16) & 0xff/*here.op*/; + if (op === 0) { /* literal */ + //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + // "inflate: literal '%c'\n" : + // "inflate: literal 0x%02x\n", here.val)); + output[_out++] = here & 0xffff/*here.val*/; + } + else if (op & 16) { /* length base */ + len = here & 0xffff/*here.val*/; + op &= 15; /* number of extra bits */ + if (op) { + if (bits < op) { + hold += input[_in++] << bits; + bits += 8; + } + len += hold & ((1 << op) - 1); + hold >>>= op; + bits -= op; + } + //Tracevv((stderr, "inflate: length %u\n", len)); + if (bits < 15) { + hold += input[_in++] << bits; + bits += 8; + hold += input[_in++] << bits; + bits += 8; + } + here = dcode[hold & dmask]; + + dodist: + for (;;) { // goto emulation + op = here >>> 24/*here.bits*/; + hold >>>= op; + bits -= op; + op = (here >>> 16) & 0xff/*here.op*/; + + if (op & 16) { /* distance base */ + dist = here & 0xffff/*here.val*/; + op &= 15; /* number of extra bits */ + if (bits < op) { + hold += input[_in++] << bits; + bits += 8; + if (bits < op) { + hold += input[_in++] << bits; + bits += 8; + } + } + dist += hold & ((1 << op) - 1); +//#ifdef INFLATE_STRICT + if (dist > dmax) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD; + break top; + } +//#endif + hold >>>= op; + bits -= op; + //Tracevv((stderr, "inflate: distance %u\n", dist)); + op = _out - beg; /* max distance in output */ + if (dist > op) { /* see if copy from window */ + op = dist - op; /* distance back in window */ + if (op > whave) { + if (state.sane) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD; + break top; + } + +// (!) This block is disabled in zlib defailts, +// don't enable it for binary compatibility +//#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR +// if (len <= op - whave) { +// do { +// output[_out++] = 0; +// } while (--len); +// continue top; +// } +// len -= op - whave; +// do { +// output[_out++] = 0; +// } while (--op > whave); +// if (op === 0) { +// from = _out - dist; +// do { +// output[_out++] = output[from++]; +// } while (--len); +// continue top; +// } +//#endif + } + from = 0; // window index + from_source = s_window; + if (wnext === 0) { /* very common case */ + from += wsize - op; + if (op < len) { /* some from window */ + len -= op; + do { + output[_out++] = s_window[from++]; + } while (--op); + from = _out - dist; /* rest from output */ + from_source = output; + } + } + else if (wnext < op) { /* wrap around window */ + from += wsize + wnext - op; + op -= wnext; + if (op < len) { /* some from end of window */ + len -= op; + do { + output[_out++] = s_window[from++]; + } while (--op); + from = 0; + if (wnext < len) { /* some from start of window */ + op = wnext; + len -= op; + do { + output[_out++] = s_window[from++]; + } while (--op); + from = _out - dist; /* rest from output */ + from_source = output; + } + } + } + else { /* contiguous in window */ + from += wnext - op; + if (op < len) { /* some from window */ + len -= op; + do { + output[_out++] = s_window[from++]; + } while (--op); + from = _out - dist; /* rest from output */ + from_source = output; + } + } + while (len > 2) { + output[_out++] = from_source[from++]; + output[_out++] = from_source[from++]; + output[_out++] = from_source[from++]; + len -= 3; + } + if (len) { + output[_out++] = from_source[from++]; + if (len > 1) { + output[_out++] = from_source[from++]; + } + } + } + else { + from = _out - dist; /* copy direct from output */ + do { /* minimum length is three */ + output[_out++] = output[from++]; + output[_out++] = output[from++]; + output[_out++] = output[from++]; + len -= 3; + } while (len > 2); + if (len) { + output[_out++] = output[from++]; + if (len > 1) { + output[_out++] = output[from++]; + } + } + } + } + else if ((op & 64) === 0) { /* 2nd level distance code */ + here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))]; + continue dodist; + } + else { + strm.msg = 'invalid distance code'; + state.mode = BAD; + break top; + } + + break; // need to emulate goto via "continue" + } + } + else if ((op & 64) === 0) { /* 2nd level length code */ + here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))]; + continue dolen; + } + else if (op & 32) { /* end-of-block */ + //Tracevv((stderr, "inflate: end of block\n")); + state.mode = TYPE; + break top; + } + else { + strm.msg = 'invalid literal/length code'; + state.mode = BAD; + break top; + } + + break; // need to emulate goto via "continue" + } + } while (_in < last && _out < end); + + /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ + len = bits >> 3; + _in -= len; + bits -= len << 3; + hold &= (1 << bits) - 1; + + /* update state and return */ + strm.next_in = _in; + strm.next_out = _out; + strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last)); + strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end)); + state.hold = hold; + state.bits = bits; + return; +}; + +},{}],457:[function(require,module,exports){ +'use strict'; + + +var utils = require('../utils/common'); +var adler32 = require('./adler32'); +var crc32 = require('./crc32'); +var inflate_fast = require('./inffast'); +var inflate_table = require('./inftrees'); + +var CODES = 0; +var LENS = 1; +var DISTS = 2; + +/* Public constants ==========================================================*/ +/* ===========================================================================*/ + + +/* Allowed flush values; see deflate() and inflate() below for details */ +//var Z_NO_FLUSH = 0; +//var Z_PARTIAL_FLUSH = 1; +//var Z_SYNC_FLUSH = 2; +//var Z_FULL_FLUSH = 3; +var Z_FINISH = 4; +var Z_BLOCK = 5; +var Z_TREES = 6; + + +/* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ +var Z_OK = 0; +var Z_STREAM_END = 1; +var Z_NEED_DICT = 2; +//var Z_ERRNO = -1; +var Z_STREAM_ERROR = -2; +var Z_DATA_ERROR = -3; +var Z_MEM_ERROR = -4; +var Z_BUF_ERROR = -5; +//var Z_VERSION_ERROR = -6; + +/* The deflate compression method */ +var Z_DEFLATED = 8; + + +/* STATES ====================================================================*/ +/* ===========================================================================*/ + + +var HEAD = 1; /* i: waiting for magic header */ +var FLAGS = 2; /* i: waiting for method and flags (gzip) */ +var TIME = 3; /* i: waiting for modification time (gzip) */ +var OS = 4; /* i: waiting for extra flags and operating system (gzip) */ +var EXLEN = 5; /* i: waiting for extra length (gzip) */ +var EXTRA = 6; /* i: waiting for extra bytes (gzip) */ +var NAME = 7; /* i: waiting for end of file name (gzip) */ +var COMMENT = 8; /* i: waiting for end of comment (gzip) */ +var HCRC = 9; /* i: waiting for header crc (gzip) */ +var DICTID = 10; /* i: waiting for dictionary check value */ +var DICT = 11; /* waiting for inflateSetDictionary() call */ +var TYPE = 12; /* i: waiting for type bits, including last-flag bit */ +var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */ +var STORED = 14; /* i: waiting for stored size (length and complement) */ +var COPY_ = 15; /* i/o: same as COPY below, but only first time in */ +var COPY = 16; /* i/o: waiting for input or output to copy stored block */ +var TABLE = 17; /* i: waiting for dynamic block table lengths */ +var LENLENS = 18; /* i: waiting for code length code lengths */ +var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */ +var LEN_ = 20; /* i: same as LEN below, but only first time in */ +var LEN = 21; /* i: waiting for length/lit/eob code */ +var LENEXT = 22; /* i: waiting for length extra bits */ +var DIST = 23; /* i: waiting for distance code */ +var DISTEXT = 24; /* i: waiting for distance extra bits */ +var MATCH = 25; /* o: waiting for output space to copy string */ +var LIT = 26; /* o: waiting for output space to write literal */ +var CHECK = 27; /* i: waiting for 32-bit check value */ +var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */ +var DONE = 29; /* finished check, done -- remain here until reset */ +var BAD = 30; /* got a data error -- remain here until reset */ +var MEM = 31; /* got an inflate() memory error -- remain here until reset */ +var SYNC = 32; /* looking for synchronization bytes to restart inflate() */ + +/* ===========================================================================*/ + + + +var ENOUGH_LENS = 852; +var ENOUGH_DISTS = 592; +//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); + +var MAX_WBITS = 15; +/* 32K LZ77 window */ +var DEF_WBITS = MAX_WBITS; + + +function zswap32(q) { + return (((q >>> 24) & 0xff) + + ((q >>> 8) & 0xff00) + + ((q & 0xff00) << 8) + + ((q & 0xff) << 24)); +} + + +function InflateState() { + this.mode = 0; /* current inflate mode */ + this.last = false; /* true if processing last block */ + this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */ + this.havedict = false; /* true if dictionary provided */ + this.flags = 0; /* gzip header method and flags (0 if zlib) */ + this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */ + this.check = 0; /* protected copy of check value */ + this.total = 0; /* protected copy of output count */ + // TODO: may be {} + this.head = null; /* where to save gzip header information */ + + /* sliding window */ + this.wbits = 0; /* log base 2 of requested window size */ + this.wsize = 0; /* window size or zero if not using window */ + this.whave = 0; /* valid bytes in the window */ + this.wnext = 0; /* window write index */ + this.window = null; /* allocated sliding window, if needed */ + + /* bit accumulator */ + this.hold = 0; /* input bit accumulator */ + this.bits = 0; /* number of bits in "in" */ + + /* for string and stored block copying */ + this.length = 0; /* literal or length of data to copy */ + this.offset = 0; /* distance back to copy string from */ + + /* for table and code decoding */ + this.extra = 0; /* extra bits needed */ + + /* fixed and dynamic code tables */ + this.lencode = null; /* starting table for length/literal codes */ + this.distcode = null; /* starting table for distance codes */ + this.lenbits = 0; /* index bits for lencode */ + this.distbits = 0; /* index bits for distcode */ + + /* dynamic table building */ + this.ncode = 0; /* number of code length code lengths */ + this.nlen = 0; /* number of length code lengths */ + this.ndist = 0; /* number of distance code lengths */ + this.have = 0; /* number of code lengths in lens[] */ + this.next = null; /* next available space in codes[] */ + + this.lens = new utils.Buf16(320); /* temporary storage for code lengths */ + this.work = new utils.Buf16(288); /* work area for code table building */ + + /* + because we don't have pointers in js, we use lencode and distcode directly + as buffers so we don't need codes + */ + //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */ + this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */ + this.distdyn = null; /* dynamic table for distance codes (JS specific) */ + this.sane = 0; /* if false, allow invalid distance too far */ + this.back = 0; /* bits back of last unprocessed length/lit */ + this.was = 0; /* initial length of match */ +} + +function inflateResetKeep(strm) { + var state; + + if (!strm || !strm.state) { return Z_STREAM_ERROR; } + state = strm.state; + strm.total_in = strm.total_out = state.total = 0; + strm.msg = ''; /*Z_NULL*/ + if (state.wrap) { /* to support ill-conceived Java test suite */ + strm.adler = state.wrap & 1; + } + state.mode = HEAD; + state.last = 0; + state.havedict = 0; + state.dmax = 32768; + state.head = null/*Z_NULL*/; + state.hold = 0; + state.bits = 0; + //state.lencode = state.distcode = state.next = state.codes; + state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS); + state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS); + + state.sane = 1; + state.back = -1; + //Tracev((stderr, "inflate: reset\n")); + return Z_OK; +} + +function inflateReset(strm) { + var state; + + if (!strm || !strm.state) { return Z_STREAM_ERROR; } + state = strm.state; + state.wsize = 0; + state.whave = 0; + state.wnext = 0; + return inflateResetKeep(strm); + +} + +function inflateReset2(strm, windowBits) { + var wrap; + var state; + + /* get the state */ + if (!strm || !strm.state) { return Z_STREAM_ERROR; } + state = strm.state; + + /* extract wrap request from windowBits parameter */ + if (windowBits < 0) { + wrap = 0; + windowBits = -windowBits; + } + else { + wrap = (windowBits >> 4) + 1; + if (windowBits < 48) { + windowBits &= 15; + } + } + + /* set number of window bits, free window if different */ + if (windowBits && (windowBits < 8 || windowBits > 15)) { + return Z_STREAM_ERROR; + } + if (state.window !== null && state.wbits !== windowBits) { + state.window = null; + } + + /* update state and reset the rest of it */ + state.wrap = wrap; + state.wbits = windowBits; + return inflateReset(strm); +} + +function inflateInit2(strm, windowBits) { + var ret; + var state; + + if (!strm) { return Z_STREAM_ERROR; } + //strm.msg = Z_NULL; /* in case we return an error */ + + state = new InflateState(); + + //if (state === Z_NULL) return Z_MEM_ERROR; + //Tracev((stderr, "inflate: allocated\n")); + strm.state = state; + state.window = null/*Z_NULL*/; + ret = inflateReset2(strm, windowBits); + if (ret !== Z_OK) { + strm.state = null/*Z_NULL*/; + } + return ret; +} + +function inflateInit(strm) { + return inflateInit2(strm, DEF_WBITS); +} + + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +var virgin = true; + +var lenfix, distfix; // We have no pointers in JS, so keep tables separate + +function fixedtables(state) { + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + var sym; + + lenfix = new utils.Buf32(512); + distfix = new utils.Buf32(32); + + /* literal/length table */ + sym = 0; + while (sym < 144) { state.lens[sym++] = 8; } + while (sym < 256) { state.lens[sym++] = 9; } + while (sym < 280) { state.lens[sym++] = 7; } + while (sym < 288) { state.lens[sym++] = 8; } + + inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 }); + + /* distance table */ + sym = 0; + while (sym < 32) { state.lens[sym++] = 5; } + + inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 }); + + /* do this just once */ + virgin = false; + } + + state.lencode = lenfix; + state.lenbits = 9; + state.distcode = distfix; + state.distbits = 5; +} + + +/* + Update the window with the last wsize (normally 32K) bytes written before + returning. If window does not exist yet, create it. This is only called + when a window is already in use, or when output has been written during this + inflate call, but the end of the deflate stream has not been reached yet. + It is also called to create a window for dictionary data when a dictionary + is loaded. + + Providing output buffers larger than 32K to inflate() should provide a speed + advantage, since only the last 32K of output is copied to the sliding window + upon return from inflate(), and since all distances after the first 32K of + output will fall in the output data, making match copies simpler and faster. + The advantage may be dependent on the size of the processor's data caches. + */ +function updatewindow(strm, src, end, copy) { + var dist; + var state = strm.state; + + /* if it hasn't been done already, allocate space for the window */ + if (state.window === null) { + state.wsize = 1 << state.wbits; + state.wnext = 0; + state.whave = 0; + + state.window = new utils.Buf8(state.wsize); + } + + /* copy state->wsize or less output bytes into the circular window */ + if (copy >= state.wsize) { + utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0); + state.wnext = 0; + state.whave = state.wsize; + } + else { + dist = state.wsize - state.wnext; + if (dist > copy) { + dist = copy; + } + //zmemcpy(state->window + state->wnext, end - copy, dist); + utils.arraySet(state.window, src, end - copy, dist, state.wnext); + copy -= dist; + if (copy) { + //zmemcpy(state->window, end - copy, copy); + utils.arraySet(state.window, src, end - copy, copy, 0); + state.wnext = copy; + state.whave = state.wsize; + } + else { + state.wnext += dist; + if (state.wnext === state.wsize) { state.wnext = 0; } + if (state.whave < state.wsize) { state.whave += dist; } + } + } + return 0; +} + +function inflate(strm, flush) { + var state; + var input, output; // input/output buffers + var next; /* next input INDEX */ + var put; /* next output INDEX */ + var have, left; /* available input and output */ + var hold; /* bit buffer */ + var bits; /* bits in bit buffer */ + var _in, _out; /* save starting available input and output */ + var copy; /* number of stored or match bytes to copy */ + var from; /* where to copy match bytes from */ + var from_source; + var here = 0; /* current decoding table entry */ + var here_bits, here_op, here_val; // paked "here" denormalized (JS specific) + //var last; /* parent table entry */ + var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) + var len; /* length to copy for repeats, bits to drop */ + var ret; /* return code */ + var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */ + var opts; + + var n; // temporary var for NEED_BITS + + var order = /* permutation of code lengths */ + [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]; + + + if (!strm || !strm.state || !strm.output || + (!strm.input && strm.avail_in !== 0)) { + return Z_STREAM_ERROR; + } + + state = strm.state; + if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */ + + + //--- LOAD() --- + put = strm.next_out; + output = strm.output; + left = strm.avail_out; + next = strm.next_in; + input = strm.input; + have = strm.avail_in; + hold = state.hold; + bits = state.bits; + //--- + + _in = have; + _out = left; + ret = Z_OK; + + inf_leave: // goto emulation + for (;;) { + switch (state.mode) { + case HEAD: + if (state.wrap === 0) { + state.mode = TYPEDO; + break; + } + //=== NEEDBITS(16); + while (bits < 16) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */ + state.check = 0/*crc32(0L, Z_NULL, 0)*/; + //=== CRC2(state.check, hold); + hbuf[0] = hold & 0xff; + hbuf[1] = (hold >>> 8) & 0xff; + state.check = crc32(state.check, hbuf, 2, 0); + //===// + + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + state.mode = FLAGS; + break; + } + state.flags = 0; /* expect zlib header */ + if (state.head) { + state.head.done = false; + } + if (!(state.wrap & 1) || /* check if zlib header allowed */ + (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) { + strm.msg = 'incorrect header check'; + state.mode = BAD; + break; + } + if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) { + strm.msg = 'unknown compression method'; + state.mode = BAD; + break; + } + //--- DROPBITS(4) ---// + hold >>>= 4; + bits -= 4; + //---// + len = (hold & 0x0f)/*BITS(4)*/ + 8; + if (state.wbits === 0) { + state.wbits = len; + } + else if (len > state.wbits) { + strm.msg = 'invalid window size'; + state.mode = BAD; + break; + } + state.dmax = 1 << len; + //Tracev((stderr, "inflate: zlib header ok\n")); + strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; + state.mode = hold & 0x200 ? DICTID : TYPE; + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + break; + case FLAGS: + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + state.flags = hold; + if ((state.flags & 0xff) !== Z_DEFLATED) { + strm.msg = 'unknown compression method'; + state.mode = BAD; + break; + } + if (state.flags & 0xe000) { + strm.msg = 'unknown header flags set'; + state.mode = BAD; + break; + } + if (state.head) { + state.head.text = ((hold >> 8) & 1); + } + if (state.flags & 0x0200) { + //=== CRC2(state.check, hold); + hbuf[0] = hold & 0xff; + hbuf[1] = (hold >>> 8) & 0xff; + state.check = crc32(state.check, hbuf, 2, 0); + //===// + } + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + state.mode = TIME; + /* falls through */ + case TIME: + //=== NEEDBITS(32); */ + while (bits < 32) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + if (state.head) { + state.head.time = hold; + } + if (state.flags & 0x0200) { + //=== CRC4(state.check, hold) + hbuf[0] = hold & 0xff; + hbuf[1] = (hold >>> 8) & 0xff; + hbuf[2] = (hold >>> 16) & 0xff; + hbuf[3] = (hold >>> 24) & 0xff; + state.check = crc32(state.check, hbuf, 4, 0); + //=== + } + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + state.mode = OS; + /* falls through */ + case OS: + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + if (state.head) { + state.head.xflags = (hold & 0xff); + state.head.os = (hold >> 8); + } + if (state.flags & 0x0200) { + //=== CRC2(state.check, hold); + hbuf[0] = hold & 0xff; + hbuf[1] = (hold >>> 8) & 0xff; + state.check = crc32(state.check, hbuf, 2, 0); + //===// + } + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + state.mode = EXLEN; + /* falls through */ + case EXLEN: + if (state.flags & 0x0400) { + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + state.length = hold; + if (state.head) { + state.head.extra_len = hold; + } + if (state.flags & 0x0200) { + //=== CRC2(state.check, hold); + hbuf[0] = hold & 0xff; + hbuf[1] = (hold >>> 8) & 0xff; + state.check = crc32(state.check, hbuf, 2, 0); + //===// + } + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + } + else if (state.head) { + state.head.extra = null/*Z_NULL*/; + } + state.mode = EXTRA; + /* falls through */ + case EXTRA: + if (state.flags & 0x0400) { + copy = state.length; + if (copy > have) { copy = have; } + if (copy) { + if (state.head) { + len = state.head.extra_len - state.length; + if (!state.head.extra) { + // Use untyped array for more conveniend processing later + state.head.extra = new Array(state.head.extra_len); + } + utils.arraySet( + state.head.extra, + input, + next, + // extra field is limited to 65536 bytes + // - no need for additional size check + copy, + /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ + len + ); + //zmemcpy(state.head.extra + len, next, + // len + copy > state.head.extra_max ? + // state.head.extra_max - len : copy); + } + if (state.flags & 0x0200) { + state.check = crc32(state.check, input, copy, next); + } + have -= copy; + next += copy; + state.length -= copy; + } + if (state.length) { break inf_leave; } + } + state.length = 0; + state.mode = NAME; + /* falls through */ + case NAME: + if (state.flags & 0x0800) { + if (have === 0) { break inf_leave; } + copy = 0; + do { + // TODO: 2 or 1 bytes? + len = input[next + copy++]; + /* use constant limit because in js we should not preallocate memory */ + if (state.head && len && + (state.length < 65536 /*state.head.name_max*/)) { + state.head.name += String.fromCharCode(len); + } + } while (len && copy < have); + + if (state.flags & 0x0200) { + state.check = crc32(state.check, input, copy, next); + } + have -= copy; + next += copy; + if (len) { break inf_leave; } + } + else if (state.head) { + state.head.name = null; + } + state.length = 0; + state.mode = COMMENT; + /* falls through */ + case COMMENT: + if (state.flags & 0x1000) { + if (have === 0) { break inf_leave; } + copy = 0; + do { + len = input[next + copy++]; + /* use constant limit because in js we should not preallocate memory */ + if (state.head && len && + (state.length < 65536 /*state.head.comm_max*/)) { + state.head.comment += String.fromCharCode(len); + } + } while (len && copy < have); + if (state.flags & 0x0200) { + state.check = crc32(state.check, input, copy, next); + } + have -= copy; + next += copy; + if (len) { break inf_leave; } + } + else if (state.head) { + state.head.comment = null; + } + state.mode = HCRC; + /* falls through */ + case HCRC: + if (state.flags & 0x0200) { + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + if (hold !== (state.check & 0xffff)) { + strm.msg = 'header crc mismatch'; + state.mode = BAD; + break; + } + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + } + if (state.head) { + state.head.hcrc = ((state.flags >> 9) & 1); + state.head.done = true; + } + strm.adler = state.check = 0; + state.mode = TYPE; + break; + case DICTID: + //=== NEEDBITS(32); */ + while (bits < 32) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + strm.adler = state.check = zswap32(hold); + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + state.mode = DICT; + /* falls through */ + case DICT: + if (state.havedict === 0) { + //--- RESTORE() --- + strm.next_out = put; + strm.avail_out = left; + strm.next_in = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; + //--- + return Z_NEED_DICT; + } + strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/; + state.mode = TYPE; + /* falls through */ + case TYPE: + if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; } + /* falls through */ + case TYPEDO: + if (state.last) { + //--- BYTEBITS() ---// + hold >>>= bits & 7; + bits -= bits & 7; + //---// + state.mode = CHECK; + break; + } + //=== NEEDBITS(3); */ + while (bits < 3) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + state.last = (hold & 0x01)/*BITS(1)*/; + //--- DROPBITS(1) ---// + hold >>>= 1; + bits -= 1; + //---// + + switch ((hold & 0x03)/*BITS(2)*/) { + case 0: /* stored block */ + //Tracev((stderr, "inflate: stored block%s\n", + // state.last ? " (last)" : "")); + state.mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + //Tracev((stderr, "inflate: fixed codes block%s\n", + // state.last ? " (last)" : "")); + state.mode = LEN_; /* decode codes */ + if (flush === Z_TREES) { + //--- DROPBITS(2) ---// + hold >>>= 2; + bits -= 2; + //---// + break inf_leave; + } + break; + case 2: /* dynamic block */ + //Tracev((stderr, "inflate: dynamic codes block%s\n", + // state.last ? " (last)" : "")); + state.mode = TABLE; + break; + case 3: + strm.msg = 'invalid block type'; + state.mode = BAD; + } + //--- DROPBITS(2) ---// + hold >>>= 2; + bits -= 2; + //---// + break; + case STORED: + //--- BYTEBITS() ---// /* go to byte boundary */ + hold >>>= bits & 7; + bits -= bits & 7; + //---// + //=== NEEDBITS(32); */ + while (bits < 32) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) { + strm.msg = 'invalid stored block lengths'; + state.mode = BAD; + break; + } + state.length = hold & 0xffff; + //Tracev((stderr, "inflate: stored length %u\n", + // state.length)); + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + state.mode = COPY_; + if (flush === Z_TREES) { break inf_leave; } + /* falls through */ + case COPY_: + state.mode = COPY; + /* falls through */ + case COPY: + copy = state.length; + if (copy) { + if (copy > have) { copy = have; } + if (copy > left) { copy = left; } + if (copy === 0) { break inf_leave; } + //--- zmemcpy(put, next, copy); --- + utils.arraySet(output, input, next, copy, put); + //---// + have -= copy; + next += copy; + left -= copy; + put += copy; + state.length -= copy; + break; + } + //Tracev((stderr, "inflate: stored end\n")); + state.mode = TYPE; + break; + case TABLE: + //=== NEEDBITS(14); */ + while (bits < 14) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257; + //--- DROPBITS(5) ---// + hold >>>= 5; + bits -= 5; + //---// + state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1; + //--- DROPBITS(5) ---// + hold >>>= 5; + bits -= 5; + //---// + state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4; + //--- DROPBITS(4) ---// + hold >>>= 4; + bits -= 4; + //---// +//#ifndef PKZIP_BUG_WORKAROUND + if (state.nlen > 286 || state.ndist > 30) { + strm.msg = 'too many length or distance symbols'; + state.mode = BAD; + break; + } +//#endif + //Tracev((stderr, "inflate: table sizes ok\n")); + state.have = 0; + state.mode = LENLENS; + /* falls through */ + case LENLENS: + while (state.have < state.ncode) { + //=== NEEDBITS(3); + while (bits < 3) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + state.lens[order[state.have++]] = (hold & 0x07);//BITS(3); + //--- DROPBITS(3) ---// + hold >>>= 3; + bits -= 3; + //---// + } + while (state.have < 19) { + state.lens[order[state.have++]] = 0; + } + // We have separate tables & no pointers. 2 commented lines below not needed. + //state.next = state.codes; + //state.lencode = state.next; + // Switch to use dynamic table + state.lencode = state.lendyn; + state.lenbits = 7; + + opts = { bits: state.lenbits }; + ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts); + state.lenbits = opts.bits; + + if (ret) { + strm.msg = 'invalid code lengths set'; + state.mode = BAD; + break; + } + //Tracev((stderr, "inflate: code lengths ok\n")); + state.have = 0; + state.mode = CODELENS; + /* falls through */ + case CODELENS: + while (state.have < state.nlen + state.ndist) { + for (;;) { + here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/ + here_bits = here >>> 24; + here_op = (here >>> 16) & 0xff; + here_val = here & 0xffff; + + if ((here_bits) <= bits) { break; } + //--- PULLBYTE() ---// + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + //---// + } + if (here_val < 16) { + //--- DROPBITS(here.bits) ---// + hold >>>= here_bits; + bits -= here_bits; + //---// + state.lens[state.have++] = here_val; + } + else { + if (here_val === 16) { + //=== NEEDBITS(here.bits + 2); + n = here_bits + 2; + while (bits < n) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + //--- DROPBITS(here.bits) ---// + hold >>>= here_bits; + bits -= here_bits; + //---// + if (state.have === 0) { + strm.msg = 'invalid bit length repeat'; + state.mode = BAD; + break; + } + len = state.lens[state.have - 1]; + copy = 3 + (hold & 0x03);//BITS(2); + //--- DROPBITS(2) ---// + hold >>>= 2; + bits -= 2; + //---// + } + else if (here_val === 17) { + //=== NEEDBITS(here.bits + 3); + n = here_bits + 3; + while (bits < n) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + //--- DROPBITS(here.bits) ---// + hold >>>= here_bits; + bits -= here_bits; + //---// + len = 0; + copy = 3 + (hold & 0x07);//BITS(3); + //--- DROPBITS(3) ---// + hold >>>= 3; + bits -= 3; + //---// + } + else { + //=== NEEDBITS(here.bits + 7); + n = here_bits + 7; + while (bits < n) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + //--- DROPBITS(here.bits) ---// + hold >>>= here_bits; + bits -= here_bits; + //---// + len = 0; + copy = 11 + (hold & 0x7f);//BITS(7); + //--- DROPBITS(7) ---// + hold >>>= 7; + bits -= 7; + //---// + } + if (state.have + copy > state.nlen + state.ndist) { + strm.msg = 'invalid bit length repeat'; + state.mode = BAD; + break; + } + while (copy--) { + state.lens[state.have++] = len; + } + } + } + + /* handle error breaks in while */ + if (state.mode === BAD) { break; } + + /* check for end-of-block code (better have one) */ + if (state.lens[256] === 0) { + strm.msg = 'invalid code -- missing end-of-block'; + state.mode = BAD; + break; + } + + /* build code tables -- note: do not change the lenbits or distbits + values here (9 and 6) without reading the comments in inftrees.h + concerning the ENOUGH constants, which depend on those values */ + state.lenbits = 9; + + opts = { bits: state.lenbits }; + ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); + // We have separate tables & no pointers. 2 commented lines below not needed. + // state.next_index = opts.table_index; + state.lenbits = opts.bits; + // state.lencode = state.next; + + if (ret) { + strm.msg = 'invalid literal/lengths set'; + state.mode = BAD; + break; + } + + state.distbits = 6; + //state.distcode.copy(state.codes); + // Switch to use dynamic table + state.distcode = state.distdyn; + opts = { bits: state.distbits }; + ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); + // We have separate tables & no pointers. 2 commented lines below not needed. + // state.next_index = opts.table_index; + state.distbits = opts.bits; + // state.distcode = state.next; + + if (ret) { + strm.msg = 'invalid distances set'; + state.mode = BAD; + break; + } + //Tracev((stderr, 'inflate: codes ok\n')); + state.mode = LEN_; + if (flush === Z_TREES) { break inf_leave; } + /* falls through */ + case LEN_: + state.mode = LEN; + /* falls through */ + case LEN: + if (have >= 6 && left >= 258) { + //--- RESTORE() --- + strm.next_out = put; + strm.avail_out = left; + strm.next_in = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; + //--- + inflate_fast(strm, _out); + //--- LOAD() --- + put = strm.next_out; + output = strm.output; + left = strm.avail_out; + next = strm.next_in; + input = strm.input; + have = strm.avail_in; + hold = state.hold; + bits = state.bits; + //--- + + if (state.mode === TYPE) { + state.back = -1; + } + break; + } + state.back = 0; + for (;;) { + here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/ + here_bits = here >>> 24; + here_op = (here >>> 16) & 0xff; + here_val = here & 0xffff; + + if (here_bits <= bits) { break; } + //--- PULLBYTE() ---// + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + //---// + } + if (here_op && (here_op & 0xf0) === 0) { + last_bits = here_bits; + last_op = here_op; + last_val = here_val; + for (;;) { + here = state.lencode[last_val + + ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; + here_bits = here >>> 24; + here_op = (here >>> 16) & 0xff; + here_val = here & 0xffff; + + if ((last_bits + here_bits) <= bits) { break; } + //--- PULLBYTE() ---// + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + //---// + } + //--- DROPBITS(last.bits) ---// + hold >>>= last_bits; + bits -= last_bits; + //---// + state.back += last_bits; + } + //--- DROPBITS(here.bits) ---// + hold >>>= here_bits; + bits -= here_bits; + //---// + state.back += here_bits; + state.length = here_val; + if (here_op === 0) { + //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + // "inflate: literal '%c'\n" : + // "inflate: literal 0x%02x\n", here.val)); + state.mode = LIT; + break; + } + if (here_op & 32) { + //Tracevv((stderr, "inflate: end of block\n")); + state.back = -1; + state.mode = TYPE; + break; + } + if (here_op & 64) { + strm.msg = 'invalid literal/length code'; + state.mode = BAD; + break; + } + state.extra = here_op & 15; + state.mode = LENEXT; + /* falls through */ + case LENEXT: + if (state.extra) { + //=== NEEDBITS(state.extra); + n = state.extra; + while (bits < n) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; + //--- DROPBITS(state.extra) ---// + hold >>>= state.extra; + bits -= state.extra; + //---// + state.back += state.extra; + } + //Tracevv((stderr, "inflate: length %u\n", state.length)); + state.was = state.length; + state.mode = DIST; + /* falls through */ + case DIST: + for (;;) { + here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/ + here_bits = here >>> 24; + here_op = (here >>> 16) & 0xff; + here_val = here & 0xffff; + + if ((here_bits) <= bits) { break; } + //--- PULLBYTE() ---// + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + //---// + } + if ((here_op & 0xf0) === 0) { + last_bits = here_bits; + last_op = here_op; + last_val = here_val; + for (;;) { + here = state.distcode[last_val + + ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)]; + here_bits = here >>> 24; + here_op = (here >>> 16) & 0xff; + here_val = here & 0xffff; + + if ((last_bits + here_bits) <= bits) { break; } + //--- PULLBYTE() ---// + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + //---// + } + //--- DROPBITS(last.bits) ---// + hold >>>= last_bits; + bits -= last_bits; + //---// + state.back += last_bits; + } + //--- DROPBITS(here.bits) ---// + hold >>>= here_bits; + bits -= here_bits; + //---// + state.back += here_bits; + if (here_op & 64) { + strm.msg = 'invalid distance code'; + state.mode = BAD; + break; + } + state.offset = here_val; + state.extra = (here_op) & 15; + state.mode = DISTEXT; + /* falls through */ + case DISTEXT: + if (state.extra) { + //=== NEEDBITS(state.extra); + n = state.extra; + while (bits < n) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/; + //--- DROPBITS(state.extra) ---// + hold >>>= state.extra; + bits -= state.extra; + //---// + state.back += state.extra; + } +//#ifdef INFLATE_STRICT + if (state.offset > state.dmax) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD; + break; + } +//#endif + //Tracevv((stderr, "inflate: distance %u\n", state.offset)); + state.mode = MATCH; + /* falls through */ + case MATCH: + if (left === 0) { break inf_leave; } + copy = _out - left; + if (state.offset > copy) { /* copy from window */ + copy = state.offset - copy; + if (copy > state.whave) { + if (state.sane) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD; + break; + } +// (!) This block is disabled in zlib defailts, +// don't enable it for binary compatibility +//#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR +// Trace((stderr, "inflate.c too far\n")); +// copy -= state.whave; +// if (copy > state.length) { copy = state.length; } +// if (copy > left) { copy = left; } +// left -= copy; +// state.length -= copy; +// do { +// output[put++] = 0; +// } while (--copy); +// if (state.length === 0) { state.mode = LEN; } +// break; +//#endif + } + if (copy > state.wnext) { + copy -= state.wnext; + from = state.wsize - copy; + } + else { + from = state.wnext - copy; + } + if (copy > state.length) { copy = state.length; } + from_source = state.window; + } + else { /* copy from output */ + from_source = output; + from = put - state.offset; + copy = state.length; + } + if (copy > left) { copy = left; } + left -= copy; + state.length -= copy; + do { + output[put++] = from_source[from++]; + } while (--copy); + if (state.length === 0) { state.mode = LEN; } + break; + case LIT: + if (left === 0) { break inf_leave; } + output[put++] = state.length; + left--; + state.mode = LEN; + break; + case CHECK: + if (state.wrap) { + //=== NEEDBITS(32); + while (bits < 32) { + if (have === 0) { break inf_leave; } + have--; + // Use '|' insdead of '+' to make sure that result is signed + hold |= input[next++] << bits; + bits += 8; + } + //===// + _out -= left; + strm.total_out += _out; + state.total += _out; + if (_out) { + strm.adler = state.check = + /*UPDATE(state.check, put - _out, _out);*/ + (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out)); + + } + _out = left; + // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too + if ((state.flags ? hold : zswap32(hold)) !== state.check) { + strm.msg = 'incorrect data check'; + state.mode = BAD; + break; + } + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + //Tracev((stderr, "inflate: check matches trailer\n")); + } + state.mode = LENGTH; + /* falls through */ + case LENGTH: + if (state.wrap && state.flags) { + //=== NEEDBITS(32); + while (bits < 32) { + if (have === 0) { break inf_leave; } + have--; + hold += input[next++] << bits; + bits += 8; + } + //===// + if (hold !== (state.total & 0xffffffff)) { + strm.msg = 'incorrect length check'; + state.mode = BAD; + break; + } + //=== INITBITS(); + hold = 0; + bits = 0; + //===// + //Tracev((stderr, "inflate: length matches trailer\n")); + } + state.mode = DONE; + /* falls through */ + case DONE: + ret = Z_STREAM_END; + break inf_leave; + case BAD: + ret = Z_DATA_ERROR; + break inf_leave; + case MEM: + return Z_MEM_ERROR; + case SYNC: + /* falls through */ + default: + return Z_STREAM_ERROR; + } + } + + // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave" + + /* + Return from inflate(), updating the total counts and the check value. + If there was no progress during the inflate() call, return a buffer + error. Call updatewindow() to create and/or update the window state. + Note: a memory error from inflate() is non-recoverable. + */ + + //--- RESTORE() --- + strm.next_out = put; + strm.avail_out = left; + strm.next_in = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; + //--- + + if (state.wsize || (_out !== strm.avail_out && state.mode < BAD && + (state.mode < CHECK || flush !== Z_FINISH))) { + if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) { + state.mode = MEM; + return Z_MEM_ERROR; + } + } + _in -= strm.avail_in; + _out -= strm.avail_out; + strm.total_in += _in; + strm.total_out += _out; + state.total += _out; + if (state.wrap && _out) { + strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/ + (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out)); + } + strm.data_type = state.bits + (state.last ? 64 : 0) + + (state.mode === TYPE ? 128 : 0) + + (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); + if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) { + ret = Z_BUF_ERROR; + } + return ret; +} + +function inflateEnd(strm) { + + if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) { + return Z_STREAM_ERROR; + } + + var state = strm.state; + if (state.window) { + state.window = null; + } + strm.state = null; + return Z_OK; +} + +function inflateGetHeader(strm, head) { + var state; + + /* check state */ + if (!strm || !strm.state) { return Z_STREAM_ERROR; } + state = strm.state; + if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; } + + /* save header structure */ + state.head = head; + head.done = false; + return Z_OK; +} + +function inflateSetDictionary(strm, dictionary) { + var dictLength = dictionary.length; + + var state; + var dictid; + var ret; + + /* check state */ + if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR; } + state = strm.state; + + if (state.wrap !== 0 && state.mode !== DICT) { + return Z_STREAM_ERROR; + } + + /* check for correct dictionary identifier */ + if (state.mode === DICT) { + dictid = 1; /* adler32(0, null, 0)*/ + /* dictid = adler32(dictid, dictionary, dictLength); */ + dictid = adler32(dictid, dictionary, dictLength, 0); + if (dictid !== state.check) { + return Z_DATA_ERROR; + } + } + /* copy dictionary to window using updatewindow(), which will amend the + existing dictionary if appropriate */ + ret = updatewindow(strm, dictionary, dictLength, dictLength); + if (ret) { + state.mode = MEM; + return Z_MEM_ERROR; + } + state.havedict = 1; + // Tracev((stderr, "inflate: dictionary set\n")); + return Z_OK; +} + +exports.inflateReset = inflateReset; +exports.inflateReset2 = inflateReset2; +exports.inflateResetKeep = inflateResetKeep; +exports.inflateInit = inflateInit; +exports.inflateInit2 = inflateInit2; +exports.inflate = inflate; +exports.inflateEnd = inflateEnd; +exports.inflateGetHeader = inflateGetHeader; +exports.inflateSetDictionary = inflateSetDictionary; +exports.inflateInfo = 'pako inflate (from Nodeca project)'; + +/* Not implemented +exports.inflateCopy = inflateCopy; +exports.inflateGetDictionary = inflateGetDictionary; +exports.inflateMark = inflateMark; +exports.inflatePrime = inflatePrime; +exports.inflateSync = inflateSync; +exports.inflateSyncPoint = inflateSyncPoint; +exports.inflateUndermine = inflateUndermine; +*/ + +},{"../utils/common":451,"./adler32":452,"./crc32":454,"./inffast":456,"./inftrees":458}],458:[function(require,module,exports){ +'use strict'; + + +var utils = require('../utils/common'); + +var MAXBITS = 15; +var ENOUGH_LENS = 852; +var ENOUGH_DISTS = 592; +//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); + +var CODES = 0; +var LENS = 1; +var DISTS = 2; + +var lbase = [ /* Length codes 257..285 base */ + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 +]; + +var lext = [ /* Length codes 257..285 extra */ + 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, + 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78 +]; + +var dbase = [ /* Distance codes 0..29 base */ + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, + 8193, 12289, 16385, 24577, 0, 0 +]; + +var dext = [ /* Distance codes 0..29 extra */ + 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, + 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, + 28, 28, 29, 29, 64, 64 +]; + +module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts) +{ + var bits = opts.bits; + //here = opts.here; /* table entry for duplication */ + + var len = 0; /* a code's length in bits */ + var sym = 0; /* index of code symbols */ + var min = 0, max = 0; /* minimum and maximum code lengths */ + var root = 0; /* number of index bits for root table */ + var curr = 0; /* number of index bits for current table */ + var drop = 0; /* code bits to drop for sub-table */ + var left = 0; /* number of prefix codes available */ + var used = 0; /* code entries in table used */ + var huff = 0; /* Huffman code */ + var incr; /* for incrementing code, index */ + var fill; /* index for replicating entries */ + var low; /* low bits for current root entry */ + var mask; /* mask for low root bits */ + var next; /* next available space in table */ + var base = null; /* base value table to use */ + var base_index = 0; +// var shoextra; /* extra bits table to use */ + var end; /* use base and extra for symbol > end */ + var count = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */ + var offs = new utils.Buf16(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */ + var extra = null; + var extra_index = 0; + + var here_bits, here_op, here_val; + + /* + Process a set of code lengths to create a canonical Huffman code. The + code lengths are lens[0..codes-1]. Each length corresponds to the + symbols 0..codes-1. The Huffman code is generated by first sorting the + symbols by length from short to long, and retaining the symbol order + for codes with equal lengths. Then the code starts with all zero bits + for the first code of the shortest length, and the codes are integer + increments for the same length, and zeros are appended as the length + increases. For the deflate format, these bits are stored backwards + from their more natural integer increment ordering, and so when the + decoding tables are built in the large loop below, the integer codes + are incremented backwards. + + This routine assumes, but does not check, that all of the entries in + lens[] are in the range 0..MAXBITS. The caller must assure this. + 1..MAXBITS is interpreted as that code length. zero means that that + symbol does not occur in this code. + + The codes are sorted by computing a count of codes for each length, + creating from that a table of starting indices for each length in the + sorted table, and then entering the symbols in order in the sorted + table. The sorted table is work[], with that space being provided by + the caller. + + The length counts are used for other purposes as well, i.e. finding + the minimum and maximum length codes, determining if there are any + codes at all, checking for a valid set of lengths, and looking ahead + at length counts to determine sub-table sizes when building the + decoding tables. + */ + + /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ + for (len = 0; len <= MAXBITS; len++) { + count[len] = 0; + } + for (sym = 0; sym < codes; sym++) { + count[lens[lens_index + sym]]++; + } + + /* bound code lengths, force root to be within code lengths */ + root = bits; + for (max = MAXBITS; max >= 1; max--) { + if (count[max] !== 0) { break; } + } + if (root > max) { + root = max; + } + if (max === 0) { /* no symbols to code at all */ + //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */ + //table.bits[opts.table_index] = 1; //here.bits = (var char)1; + //table.val[opts.table_index++] = 0; //here.val = (var short)0; + table[table_index++] = (1 << 24) | (64 << 16) | 0; + + + //table.op[opts.table_index] = 64; + //table.bits[opts.table_index] = 1; + //table.val[opts.table_index++] = 0; + table[table_index++] = (1 << 24) | (64 << 16) | 0; + + opts.bits = 1; + return 0; /* no symbols, but wait for decoding to report error */ + } + for (min = 1; min < max; min++) { + if (count[min] !== 0) { break; } + } + if (root < min) { + root = min; + } + + /* check for an over-subscribed or incomplete set of lengths */ + left = 1; + for (len = 1; len <= MAXBITS; len++) { + left <<= 1; + left -= count[len]; + if (left < 0) { + return -1; + } /* over-subscribed */ + } + if (left > 0 && (type === CODES || max !== 1)) { + return -1; /* incomplete set */ + } + + /* generate offsets into symbol table for each length for sorting */ + offs[1] = 0; + for (len = 1; len < MAXBITS; len++) { + offs[len + 1] = offs[len] + count[len]; + } + + /* sort symbols by length, by symbol order within each length */ + for (sym = 0; sym < codes; sym++) { + if (lens[lens_index + sym] !== 0) { + work[offs[lens[lens_index + sym]]++] = sym; + } + } + + /* + Create and fill in decoding tables. In this loop, the table being + filled is at next and has curr index bits. The code being used is huff + with length len. That code is converted to an index by dropping drop + bits off of the bottom. For codes where len is less than drop + curr, + those top drop + curr - len bits are incremented through all values to + fill the table with replicated entries. + + root is the number of index bits for the root table. When len exceeds + root, sub-tables are created pointed to by the root entry with an index + of the low root bits of huff. This is saved in low to check for when a + new sub-table should be started. drop is zero when the root table is + being filled, and drop is root when sub-tables are being filled. + + When a new sub-table is needed, it is necessary to look ahead in the + code lengths to determine what size sub-table is needed. The length + counts are used for this, and so count[] is decremented as codes are + entered in the tables. + + used keeps track of how many table entries have been allocated from the + provided *table space. It is checked for LENS and DIST tables against + the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in + the initial root table size constants. See the comments in inftrees.h + for more information. + + sym increments through all symbols, and the loop terminates when + all codes of length max, i.e. all codes, have been processed. This + routine permits incomplete codes, so another loop after this one fills + in the rest of the decoding tables with invalid code markers. + */ + + /* set up for code type */ + // poor man optimization - use if-else instead of switch, + // to avoid deopts in old v8 + if (type === CODES) { + base = extra = work; /* dummy value--not used */ + end = 19; + + } else if (type === LENS) { + base = lbase; + base_index -= 257; + extra = lext; + extra_index -= 257; + end = 256; + + } else { /* DISTS */ + base = dbase; + extra = dext; + end = -1; + } + + /* initialize opts for loop */ + huff = 0; /* starting code */ + sym = 0; /* starting code symbol */ + len = min; /* starting code length */ + next = table_index; /* current table to fill in */ + curr = root; /* current table index bits */ + drop = 0; /* current bits to drop from code for index */ + low = -1; /* trigger new sub-table when len > root */ + used = 1 << root; /* use root table entries */ + mask = used - 1; /* mask for comparing low */ + + /* check available table space */ + if ((type === LENS && used > ENOUGH_LENS) || + (type === DISTS && used > ENOUGH_DISTS)) { + return 1; + } + + var i = 0; + /* process all codes and make table entries */ + for (;;) { + i++; + /* create table entry */ + here_bits = len - drop; + if (work[sym] < end) { + here_op = 0; + here_val = work[sym]; + } + else if (work[sym] > end) { + here_op = extra[extra_index + work[sym]]; + here_val = base[base_index + work[sym]]; + } + else { + here_op = 32 + 64; /* end of block */ + here_val = 0; + } + + /* replicate for those indices with low len bits equal to huff */ + incr = 1 << (len - drop); + fill = 1 << curr; + min = fill; /* save offset to next table */ + do { + fill -= incr; + table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0; + } while (fill !== 0); + + /* backwards increment the len-bit code huff */ + incr = 1 << (len - 1); + while (huff & incr) { + incr >>= 1; + } + if (incr !== 0) { + huff &= incr - 1; + huff += incr; + } else { + huff = 0; + } + + /* go to next symbol, update count, len */ + sym++; + if (--count[len] === 0) { + if (len === max) { break; } + len = lens[lens_index + work[sym]]; + } + + /* create new sub-table if needed */ + if (len > root && (huff & mask) !== low) { + /* if first time, transition to sub-tables */ + if (drop === 0) { + drop = root; + } + + /* increment past last table */ + next += min; /* here min is 1 << curr */ + + /* determine length of next table */ + curr = len - drop; + left = 1 << curr; + while (curr + drop < max) { + left -= count[curr + drop]; + if (left <= 0) { break; } + curr++; + left <<= 1; + } + + /* check for enough space */ + used += 1 << curr; + if ((type === LENS && used > ENOUGH_LENS) || + (type === DISTS && used > ENOUGH_DISTS)) { + return 1; + } + + /* point entry in root table to sub-table */ + low = huff & mask; + /*table.op[low] = curr; + table.bits[low] = root; + table.val[low] = next - opts.table_index;*/ + table[low] = (root << 24) | (curr << 16) | (next - table_index) |0; + } + } + + /* fill in remaining table entry if code is incomplete (guaranteed to have + at most one remaining entry, since if the code is incomplete, the + maximum code length that was allowed to get this far is one bit) */ + if (huff !== 0) { + //table.op[next + huff] = 64; /* invalid code marker */ + //table.bits[next + huff] = len - drop; + //table.val[next + huff] = 0; + table[next + huff] = ((len - drop) << 24) | (64 << 16) |0; + } + + /* set return parameters */ + //opts.table_index += used; + opts.bits = root; + return 0; +}; + +},{"../utils/common":451}],459:[function(require,module,exports){ +'use strict'; + +module.exports = { + 2: 'need dictionary', /* Z_NEED_DICT 2 */ + 1: 'stream end', /* Z_STREAM_END 1 */ + 0: '', /* Z_OK 0 */ + '-1': 'file error', /* Z_ERRNO (-1) */ + '-2': 'stream error', /* Z_STREAM_ERROR (-2) */ + '-3': 'data error', /* Z_DATA_ERROR (-3) */ + '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */ + '-5': 'buffer error', /* Z_BUF_ERROR (-5) */ + '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */ +}; + +},{}],460:[function(require,module,exports){ +'use strict'; + + +var utils = require('../utils/common'); + +/* Public constants ==========================================================*/ +/* ===========================================================================*/ + + +//var Z_FILTERED = 1; +//var Z_HUFFMAN_ONLY = 2; +//var Z_RLE = 3; +var Z_FIXED = 4; +//var Z_DEFAULT_STRATEGY = 0; + +/* Possible values of the data_type field (though see inflate()) */ +var Z_BINARY = 0; +var Z_TEXT = 1; +//var Z_ASCII = 1; // = Z_TEXT +var Z_UNKNOWN = 2; + +/*============================================================================*/ + + +function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } } + +// From zutil.h + +var STORED_BLOCK = 0; +var STATIC_TREES = 1; +var DYN_TREES = 2; +/* The three kinds of block type */ + +var MIN_MATCH = 3; +var MAX_MATCH = 258; +/* The minimum and maximum match lengths */ + +// From deflate.h +/* =========================================================================== + * Internal compression state. + */ + +var LENGTH_CODES = 29; +/* number of length codes, not counting the special END_BLOCK code */ + +var LITERALS = 256; +/* number of literal bytes 0..255 */ + +var L_CODES = LITERALS + 1 + LENGTH_CODES; +/* number of Literal or Length codes, including the END_BLOCK code */ + +var D_CODES = 30; +/* number of distance codes */ + +var BL_CODES = 19; +/* number of codes used to transfer the bit lengths */ + +var HEAP_SIZE = 2 * L_CODES + 1; +/* maximum heap size */ + +var MAX_BITS = 15; +/* All codes must not exceed MAX_BITS bits */ + +var Buf_size = 16; +/* size of bit buffer in bi_buf */ + + +/* =========================================================================== + * Constants + */ + +var MAX_BL_BITS = 7; +/* Bit length codes must not exceed MAX_BL_BITS bits */ + +var END_BLOCK = 256; +/* end of block literal code */ + +var REP_3_6 = 16; +/* repeat previous bit length 3-6 times (2 bits of repeat count) */ + +var REPZ_3_10 = 17; +/* repeat a zero length 3-10 times (3 bits of repeat count) */ + +var REPZ_11_138 = 18; +/* repeat a zero length 11-138 times (7 bits of repeat count) */ + +/* eslint-disable comma-spacing,array-bracket-spacing */ +var extra_lbits = /* extra bits for each length code */ + [0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0]; + +var extra_dbits = /* extra bits for each distance code */ + [0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13]; + +var extra_blbits = /* extra bits for each bit length code */ + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]; + +var bl_order = + [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]; +/* eslint-enable comma-spacing,array-bracket-spacing */ + +/* The lengths of the bit length codes are sent in order of decreasing + * probability, to avoid transmitting the lengths for unused bit length codes. + */ + +/* =========================================================================== + * Local data. These are initialized only once. + */ + +// We pre-fill arrays with 0 to avoid uninitialized gaps + +var DIST_CODE_LEN = 512; /* see definition of array dist_code below */ + +// !!!! Use flat array insdead of structure, Freq = i*2, Len = i*2+1 +var static_ltree = new Array((L_CODES + 2) * 2); +zero(static_ltree); +/* The static literal tree. Since the bit lengths are imposed, there is no + * need for the L_CODES extra codes used during heap construction. However + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init + * below). + */ + +var static_dtree = new Array(D_CODES * 2); +zero(static_dtree); +/* The static distance tree. (Actually a trivial tree since all codes use + * 5 bits.) + */ + +var _dist_code = new Array(DIST_CODE_LEN); +zero(_dist_code); +/* Distance codes. The first 256 values correspond to the distances + * 3 .. 258, the last 256 values correspond to the top 8 bits of + * the 15 bit distances. + */ + +var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1); +zero(_length_code); +/* length code for each normalized match length (0 == MIN_MATCH) */ + +var base_length = new Array(LENGTH_CODES); +zero(base_length); +/* First normalized length for each code (0 = MIN_MATCH) */ + +var base_dist = new Array(D_CODES); +zero(base_dist); +/* First normalized distance for each code (0 = distance of 1) */ + + +function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) { + + this.static_tree = static_tree; /* static tree or NULL */ + this.extra_bits = extra_bits; /* extra bits for each code or NULL */ + this.extra_base = extra_base; /* base index for extra_bits */ + this.elems = elems; /* max number of elements in the tree */ + this.max_length = max_length; /* max bit length for the codes */ + + // show if `static_tree` has data or dummy - needed for monomorphic objects + this.has_stree = static_tree && static_tree.length; +} + + +var static_l_desc; +var static_d_desc; +var static_bl_desc; + + +function TreeDesc(dyn_tree, stat_desc) { + this.dyn_tree = dyn_tree; /* the dynamic tree */ + this.max_code = 0; /* largest code with non zero frequency */ + this.stat_desc = stat_desc; /* the corresponding static tree */ +} + + + +function d_code(dist) { + return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)]; +} + + +/* =========================================================================== + * Output a short LSB first on the stream. + * IN assertion: there is enough room in pendingBuf. + */ +function put_short(s, w) { +// put_byte(s, (uch)((w) & 0xff)); +// put_byte(s, (uch)((ush)(w) >> 8)); + s.pending_buf[s.pending++] = (w) & 0xff; + s.pending_buf[s.pending++] = (w >>> 8) & 0xff; +} + + +/* =========================================================================== + * Send a value on a given number of bits. + * IN assertion: length <= 16 and value fits in length bits. + */ +function send_bits(s, value, length) { + if (s.bi_valid > (Buf_size - length)) { + s.bi_buf |= (value << s.bi_valid) & 0xffff; + put_short(s, s.bi_buf); + s.bi_buf = value >> (Buf_size - s.bi_valid); + s.bi_valid += length - Buf_size; + } else { + s.bi_buf |= (value << s.bi_valid) & 0xffff; + s.bi_valid += length; + } +} + + +function send_code(s, c, tree) { + send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/); +} + + +/* =========================================================================== + * Reverse the first len bits of a code, using straightforward code (a faster + * method would use a table) + * IN assertion: 1 <= len <= 15 + */ +function bi_reverse(code, len) { + var res = 0; + do { + res |= code & 1; + code >>>= 1; + res <<= 1; + } while (--len > 0); + return res >>> 1; +} + + +/* =========================================================================== + * Flush the bit buffer, keeping at most 7 bits in it. + */ +function bi_flush(s) { + if (s.bi_valid === 16) { + put_short(s, s.bi_buf); + s.bi_buf = 0; + s.bi_valid = 0; + + } else if (s.bi_valid >= 8) { + s.pending_buf[s.pending++] = s.bi_buf & 0xff; + s.bi_buf >>= 8; + s.bi_valid -= 8; + } +} + + +/* =========================================================================== + * Compute the optimal bit lengths for a tree and update the total bit length + * for the current block. + * IN assertion: the fields freq and dad are set, heap[heap_max] and + * above are the tree nodes sorted by increasing frequency. + * OUT assertions: the field len is set to the optimal bit length, the + * array bl_count contains the frequencies for each bit length. + * The length opt_len is updated; static_len is also updated if stree is + * not null. + */ +function gen_bitlen(s, desc) +// deflate_state *s; +// tree_desc *desc; /* the tree descriptor */ +{ + var tree = desc.dyn_tree; + var max_code = desc.max_code; + var stree = desc.stat_desc.static_tree; + var has_stree = desc.stat_desc.has_stree; + var extra = desc.stat_desc.extra_bits; + var base = desc.stat_desc.extra_base; + var max_length = desc.stat_desc.max_length; + var h; /* heap index */ + var n, m; /* iterate over the tree elements */ + var bits; /* bit length */ + var xbits; /* extra bits */ + var f; /* frequency */ + var overflow = 0; /* number of elements with bit length too large */ + + for (bits = 0; bits <= MAX_BITS; bits++) { + s.bl_count[bits] = 0; + } + + /* In a first pass, compute the optimal bit lengths (which may + * overflow in the case of the bit length tree). + */ + tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */ + + for (h = s.heap_max + 1; h < HEAP_SIZE; h++) { + n = s.heap[h]; + bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1; + if (bits > max_length) { + bits = max_length; + overflow++; + } + tree[n * 2 + 1]/*.Len*/ = bits; + /* We overwrite tree[n].Dad which is no longer needed */ + + if (n > max_code) { continue; } /* not a leaf node */ + + s.bl_count[bits]++; + xbits = 0; + if (n >= base) { + xbits = extra[n - base]; + } + f = tree[n * 2]/*.Freq*/; + s.opt_len += f * (bits + xbits); + if (has_stree) { + s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits); + } + } + if (overflow === 0) { return; } + + // Trace((stderr,"\nbit length overflow\n")); + /* This happens for example on obj2 and pic of the Calgary corpus */ + + /* Find the first bit length which could increase: */ + do { + bits = max_length - 1; + while (s.bl_count[bits] === 0) { bits--; } + s.bl_count[bits]--; /* move one leaf down the tree */ + s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */ + s.bl_count[max_length]--; + /* The brother of the overflow item also moves one step up, + * but this does not affect bl_count[max_length] + */ + overflow -= 2; + } while (overflow > 0); + + /* Now recompute all bit lengths, scanning in increasing frequency. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all + * lengths instead of fixing only the wrong ones. This idea is taken + * from 'ar' written by Haruhiko Okumura.) + */ + for (bits = max_length; bits !== 0; bits--) { + n = s.bl_count[bits]; + while (n !== 0) { + m = s.heap[--h]; + if (m > max_code) { continue; } + if (tree[m * 2 + 1]/*.Len*/ !== bits) { + // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); + s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/; + tree[m * 2 + 1]/*.Len*/ = bits; + } + n--; + } + } +} + + +/* =========================================================================== + * Generate the codes for a given tree and bit counts (which need not be + * optimal). + * IN assertion: the array bl_count contains the bit length statistics for + * the given tree and the field len is set for all tree elements. + * OUT assertion: the field code is set for all tree elements of non + * zero code length. + */ +function gen_codes(tree, max_code, bl_count) +// ct_data *tree; /* the tree to decorate */ +// int max_code; /* largest code with non zero frequency */ +// ushf *bl_count; /* number of codes at each bit length */ +{ + var next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */ + var code = 0; /* running code value */ + var bits; /* bit index */ + var n; /* code index */ + + /* The distribution counts are first used to generate the code values + * without bit reversal. + */ + for (bits = 1; bits <= MAX_BITS; bits++) { + next_code[bits] = code = (code + bl_count[bits - 1]) << 1; + } + /* Check that the bit counts in bl_count are consistent. The last code + * must be all ones. + */ + //Assert (code + bl_count[MAX_BITS]-1 == (1< length code (0..28) */ + length = 0; + for (code = 0; code < LENGTH_CODES - 1; code++) { + base_length[code] = length; + for (n = 0; n < (1 << extra_lbits[code]); n++) { + _length_code[length++] = code; + } + } + //Assert (length == 256, "tr_static_init: length != 256"); + /* Note that the length 255 (match length 258) can be represented + * in two different ways: code 284 + 5 bits or code 285, so we + * overwrite length_code[255] to use the best encoding: + */ + _length_code[length - 1] = code; + + /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ + dist = 0; + for (code = 0; code < 16; code++) { + base_dist[code] = dist; + for (n = 0; n < (1 << extra_dbits[code]); n++) { + _dist_code[dist++] = code; + } + } + //Assert (dist == 256, "tr_static_init: dist != 256"); + dist >>= 7; /* from now on, all distances are divided by 128 */ + for (; code < D_CODES; code++) { + base_dist[code] = dist << 7; + for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { + _dist_code[256 + dist++] = code; + } + } + //Assert (dist == 256, "tr_static_init: 256+dist != 512"); + + /* Construct the codes of the static literal tree */ + for (bits = 0; bits <= MAX_BITS; bits++) { + bl_count[bits] = 0; + } + + n = 0; + while (n <= 143) { + static_ltree[n * 2 + 1]/*.Len*/ = 8; + n++; + bl_count[8]++; + } + while (n <= 255) { + static_ltree[n * 2 + 1]/*.Len*/ = 9; + n++; + bl_count[9]++; + } + while (n <= 279) { + static_ltree[n * 2 + 1]/*.Len*/ = 7; + n++; + bl_count[7]++; + } + while (n <= 287) { + static_ltree[n * 2 + 1]/*.Len*/ = 8; + n++; + bl_count[8]++; + } + /* Codes 286 and 287 do not exist, but we must include them in the + * tree construction to get a canonical Huffman tree (longest code + * all ones) + */ + gen_codes(static_ltree, L_CODES + 1, bl_count); + + /* The static distance tree is trivial: */ + for (n = 0; n < D_CODES; n++) { + static_dtree[n * 2 + 1]/*.Len*/ = 5; + static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5); + } + + // Now data ready and we can init static trees + static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS); + static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS); + static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS); + + //static_init_done = true; +} + + +/* =========================================================================== + * Initialize a new block. + */ +function init_block(s) { + var n; /* iterates over tree elements */ + + /* Initialize the trees. */ + for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; } + for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; } + for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; } + + s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1; + s.opt_len = s.static_len = 0; + s.last_lit = s.matches = 0; +} + + +/* =========================================================================== + * Flush the bit buffer and align the output on a byte boundary + */ +function bi_windup(s) +{ + if (s.bi_valid > 8) { + put_short(s, s.bi_buf); + } else if (s.bi_valid > 0) { + //put_byte(s, (Byte)s->bi_buf); + s.pending_buf[s.pending++] = s.bi_buf; + } + s.bi_buf = 0; + s.bi_valid = 0; +} + +/* =========================================================================== + * Copy a stored block, storing first the length and its + * one's complement if requested. + */ +function copy_block(s, buf, len, header) +//DeflateState *s; +//charf *buf; /* the input data */ +//unsigned len; /* its length */ +//int header; /* true if block header must be written */ +{ + bi_windup(s); /* align on byte boundary */ + + if (header) { + put_short(s, len); + put_short(s, ~len); + } +// while (len--) { +// put_byte(s, *buf++); +// } + utils.arraySet(s.pending_buf, s.window, buf, len, s.pending); + s.pending += len; +} + +/* =========================================================================== + * Compares to subtrees, using the tree depth as tie breaker when + * the subtrees have equal frequency. This minimizes the worst case length. + */ +function smaller(tree, n, m, depth) { + var _n2 = n * 2; + var _m2 = m * 2; + return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ || + (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m])); +} + +/* =========================================================================== + * Restore the heap property by moving down the tree starting at node k, + * exchanging a node with the smallest of its two sons if necessary, stopping + * when the heap property is re-established (each father smaller than its + * two sons). + */ +function pqdownheap(s, tree, k) +// deflate_state *s; +// ct_data *tree; /* the tree to restore */ +// int k; /* node to move down */ +{ + var v = s.heap[k]; + var j = k << 1; /* left son of k */ + while (j <= s.heap_len) { + /* Set j to the smallest of the two sons: */ + if (j < s.heap_len && + smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) { + j++; + } + /* Exit if v is smaller than both sons */ + if (smaller(tree, v, s.heap[j], s.depth)) { break; } + + /* Exchange v with the smallest son */ + s.heap[k] = s.heap[j]; + k = j; + + /* And continue down the tree, setting j to the left son of k */ + j <<= 1; + } + s.heap[k] = v; +} + + +// inlined manually +// var SMALLEST = 1; + +/* =========================================================================== + * Send the block data compressed using the given Huffman trees + */ +function compress_block(s, ltree, dtree) +// deflate_state *s; +// const ct_data *ltree; /* literal tree */ +// const ct_data *dtree; /* distance tree */ +{ + var dist; /* distance of matched string */ + var lc; /* match length or unmatched char (if dist == 0) */ + var lx = 0; /* running index in l_buf */ + var code; /* the code to send */ + var extra; /* number of extra bits to send */ + + if (s.last_lit !== 0) { + do { + dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]); + lc = s.pending_buf[s.l_buf + lx]; + lx++; + + if (dist === 0) { + send_code(s, lc, ltree); /* send a literal byte */ + //Tracecv(isgraph(lc), (stderr," '%c' ", lc)); + } else { + /* Here, lc is the match length - MIN_MATCH */ + code = _length_code[lc]; + send_code(s, code + LITERALS + 1, ltree); /* send the length code */ + extra = extra_lbits[code]; + if (extra !== 0) { + lc -= base_length[code]; + send_bits(s, lc, extra); /* send the extra length bits */ + } + dist--; /* dist is now the match distance - 1 */ + code = d_code(dist); + //Assert (code < D_CODES, "bad d_code"); + + send_code(s, code, dtree); /* send the distance code */ + extra = extra_dbits[code]; + if (extra !== 0) { + dist -= base_dist[code]; + send_bits(s, dist, extra); /* send the extra distance bits */ + } + } /* literal or match pair ? */ + + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ + //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, + // "pendingBuf overflow"); + + } while (lx < s.last_lit); + } + + send_code(s, END_BLOCK, ltree); +} + + +/* =========================================================================== + * Construct one Huffman tree and assigns the code bit strings and lengths. + * Update the total bit length for the current block. + * IN assertion: the field freq is set for all tree elements. + * OUT assertions: the fields len and code are set to the optimal bit length + * and corresponding code. The length opt_len is updated; static_len is + * also updated if stree is not null. The field max_code is set. + */ +function build_tree(s, desc) +// deflate_state *s; +// tree_desc *desc; /* the tree descriptor */ +{ + var tree = desc.dyn_tree; + var stree = desc.stat_desc.static_tree; + var has_stree = desc.stat_desc.has_stree; + var elems = desc.stat_desc.elems; + var n, m; /* iterate over heap elements */ + var max_code = -1; /* largest code with non zero frequency */ + var node; /* new node being created */ + + /* Construct the initial heap, with least frequent element in + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. + * heap[0] is not used. + */ + s.heap_len = 0; + s.heap_max = HEAP_SIZE; + + for (n = 0; n < elems; n++) { + if (tree[n * 2]/*.Freq*/ !== 0) { + s.heap[++s.heap_len] = max_code = n; + s.depth[n] = 0; + + } else { + tree[n * 2 + 1]/*.Len*/ = 0; + } + } + + /* The pkzip format requires that at least one distance code exists, + * and that at least one bit should be sent even if there is only one + * possible code. So to avoid special checks later on we force at least + * two codes of non zero frequency. + */ + while (s.heap_len < 2) { + node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0); + tree[node * 2]/*.Freq*/ = 1; + s.depth[node] = 0; + s.opt_len--; + + if (has_stree) { + s.static_len -= stree[node * 2 + 1]/*.Len*/; + } + /* node is 0 or 1 so it does not have extra bits */ + } + desc.max_code = max_code; + + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, + * establish sub-heaps of increasing lengths: + */ + for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); } + + /* Construct the Huffman tree by repeatedly combining the least two + * frequent nodes. + */ + node = elems; /* next internal node of the tree */ + do { + //pqremove(s, tree, n); /* n = node of least frequency */ + /*** pqremove ***/ + n = s.heap[1/*SMALLEST*/]; + s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--]; + pqdownheap(s, tree, 1/*SMALLEST*/); + /***/ + + m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */ + + s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */ + s.heap[--s.heap_max] = m; + + /* Create a new node father of n and m */ + tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/; + s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1; + tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node; + + /* and insert the new node in the heap */ + s.heap[1/*SMALLEST*/] = node++; + pqdownheap(s, tree, 1/*SMALLEST*/); + + } while (s.heap_len >= 2); + + s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/]; + + /* At this point, the fields freq and dad are set. We can now + * generate the bit lengths. + */ + gen_bitlen(s, desc); + + /* The field len is now set, we can generate the bit codes */ + gen_codes(tree, max_code, s.bl_count); +} + + +/* =========================================================================== + * Scan a literal or distance tree to determine the frequencies of the codes + * in the bit length tree. + */ +function scan_tree(s, tree, max_code) +// deflate_state *s; +// ct_data *tree; /* the tree to be scanned */ +// int max_code; /* and its largest code of non zero frequency */ +{ + var n; /* iterates over all tree elements */ + var prevlen = -1; /* last emitted length */ + var curlen; /* length of current code */ + + var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */ + + var count = 0; /* repeat count of the current code */ + var max_count = 7; /* max repeat count */ + var min_count = 4; /* min repeat count */ + + if (nextlen === 0) { + max_count = 138; + min_count = 3; + } + tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */ + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; + nextlen = tree[(n + 1) * 2 + 1]/*.Len*/; + + if (++count < max_count && curlen === nextlen) { + continue; + + } else if (count < min_count) { + s.bl_tree[curlen * 2]/*.Freq*/ += count; + + } else if (curlen !== 0) { + + if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; } + s.bl_tree[REP_3_6 * 2]/*.Freq*/++; + + } else if (count <= 10) { + s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++; + + } else { + s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++; + } + + count = 0; + prevlen = curlen; + + if (nextlen === 0) { + max_count = 138; + min_count = 3; + + } else if (curlen === nextlen) { + max_count = 6; + min_count = 3; + + } else { + max_count = 7; + min_count = 4; + } + } +} + + +/* =========================================================================== + * Send a literal or distance tree in compressed form, using the codes in + * bl_tree. + */ +function send_tree(s, tree, max_code) +// deflate_state *s; +// ct_data *tree; /* the tree to be scanned */ +// int max_code; /* and its largest code of non zero frequency */ +{ + var n; /* iterates over all tree elements */ + var prevlen = -1; /* last emitted length */ + var curlen; /* length of current code */ + + var nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */ + + var count = 0; /* repeat count of the current code */ + var max_count = 7; /* max repeat count */ + var min_count = 4; /* min repeat count */ + + /* tree[max_code+1].Len = -1; */ /* guard already set */ + if (nextlen === 0) { + max_count = 138; + min_count = 3; + } + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; + nextlen = tree[(n + 1) * 2 + 1]/*.Len*/; + + if (++count < max_count && curlen === nextlen) { + continue; + + } else if (count < min_count) { + do { send_code(s, curlen, s.bl_tree); } while (--count !== 0); + + } else if (curlen !== 0) { + if (curlen !== prevlen) { + send_code(s, curlen, s.bl_tree); + count--; + } + //Assert(count >= 3 && count <= 6, " 3_6?"); + send_code(s, REP_3_6, s.bl_tree); + send_bits(s, count - 3, 2); + + } else if (count <= 10) { + send_code(s, REPZ_3_10, s.bl_tree); + send_bits(s, count - 3, 3); + + } else { + send_code(s, REPZ_11_138, s.bl_tree); + send_bits(s, count - 11, 7); + } + + count = 0; + prevlen = curlen; + if (nextlen === 0) { + max_count = 138; + min_count = 3; + + } else if (curlen === nextlen) { + max_count = 6; + min_count = 3; + + } else { + max_count = 7; + min_count = 4; + } + } +} + + +/* =========================================================================== + * Construct the Huffman tree for the bit lengths and return the index in + * bl_order of the last bit length code to send. + */ +function build_bl_tree(s) { + var max_blindex; /* index of last bit length code of non zero freq */ + + /* Determine the bit length frequencies for literal and distance trees */ + scan_tree(s, s.dyn_ltree, s.l_desc.max_code); + scan_tree(s, s.dyn_dtree, s.d_desc.max_code); + + /* Build the bit length tree: */ + build_tree(s, s.bl_desc); + /* opt_len now includes the length of the tree representations, except + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. + */ + + /* Determine the number of bit length codes to send. The pkzip format + * requires that at least 4 bit length codes be sent. (appnote.txt says + * 3 but the actual value used is 4.) + */ + for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) { + if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) { + break; + } + } + /* Update opt_len to include the bit length tree and counts */ + s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; + //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", + // s->opt_len, s->static_len)); + + return max_blindex; +} + + +/* =========================================================================== + * Send the header for a block using dynamic Huffman trees: the counts, the + * lengths of the bit length codes, the literal tree and the distance tree. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. + */ +function send_all_trees(s, lcodes, dcodes, blcodes) +// deflate_state *s; +// int lcodes, dcodes, blcodes; /* number of codes for each tree */ +{ + var rank; /* index in bl_order */ + + //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); + //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, + // "too many codes"); + //Tracev((stderr, "\nbl counts: ")); + send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ + send_bits(s, dcodes - 1, 5); + send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */ + for (rank = 0; rank < blcodes; rank++) { + //Tracev((stderr, "\nbl code %2d ", bl_order[rank])); + send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3); + } + //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); + + send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */ + //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); + + send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */ + //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); +} + + +/* =========================================================================== + * Check if the data type is TEXT or BINARY, using the following algorithm: + * - TEXT if the two conditions below are satisfied: + * a) There are no non-portable control characters belonging to the + * "black list" (0..6, 14..25, 28..31). + * b) There is at least one printable character belonging to the + * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). + * - BINARY otherwise. + * - The following partially-portable control characters form a + * "gray list" that is ignored in this detection algorithm: + * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). + * IN assertion: the fields Freq of dyn_ltree are set. + */ +function detect_data_type(s) { + /* black_mask is the bit mask of black-listed bytes + * set bits 0..6, 14..25, and 28..31 + * 0xf3ffc07f = binary 11110011111111111100000001111111 + */ + var black_mask = 0xf3ffc07f; + var n; + + /* Check for non-textual ("black-listed") bytes. */ + for (n = 0; n <= 31; n++, black_mask >>>= 1) { + if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) { + return Z_BINARY; + } + } + + /* Check for textual ("white-listed") bytes. */ + if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 || + s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) { + return Z_TEXT; + } + for (n = 32; n < LITERALS; n++) { + if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) { + return Z_TEXT; + } + } + + /* There are no "black-listed" or "white-listed" bytes: + * this stream either is empty or has tolerated ("gray-listed") bytes only. + */ + return Z_BINARY; +} + + +var static_init_done = false; + +/* =========================================================================== + * Initialize the tree data structures for a new zlib stream. + */ +function _tr_init(s) +{ + + if (!static_init_done) { + tr_static_init(); + static_init_done = true; + } + + s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc); + s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc); + s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc); + + s.bi_buf = 0; + s.bi_valid = 0; + + /* Initialize the first block of the first file: */ + init_block(s); +} + + +/* =========================================================================== + * Send a stored block + */ +function _tr_stored_block(s, buf, stored_len, last) +//DeflateState *s; +//charf *buf; /* input block */ +//ulg stored_len; /* length of input block */ +//int last; /* one if this is the last block for a file */ +{ + send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */ + copy_block(s, buf, stored_len, true); /* with header */ +} + + +/* =========================================================================== + * Send one empty static block to give enough lookahead for inflate. + * This takes 10 bits, of which 7 may remain in the bit buffer. + */ +function _tr_align(s) { + send_bits(s, STATIC_TREES << 1, 3); + send_code(s, END_BLOCK, static_ltree); + bi_flush(s); +} + + +/* =========================================================================== + * Determine the best encoding for the current block: dynamic trees, static + * trees or store, and output the encoded block to the zip file. + */ +function _tr_flush_block(s, buf, stored_len, last) +//DeflateState *s; +//charf *buf; /* input block, or NULL if too old */ +//ulg stored_len; /* length of input block */ +//int last; /* one if this is the last block for a file */ +{ + var opt_lenb, static_lenb; /* opt_len and static_len in bytes */ + var max_blindex = 0; /* index of last bit length code of non zero freq */ + + /* Build the Huffman trees unless a stored block is forced */ + if (s.level > 0) { + + /* Check if the file is binary or text */ + if (s.strm.data_type === Z_UNKNOWN) { + s.strm.data_type = detect_data_type(s); + } + + /* Construct the literal and distance trees */ + build_tree(s, s.l_desc); + // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, + // s->static_len)); + + build_tree(s, s.d_desc); + // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, + // s->static_len)); + /* At this point, opt_len and static_len are the total bit lengths of + * the compressed block data, excluding the tree representations. + */ + + /* Build the bit length tree for the above two trees, and get the index + * in bl_order of the last bit length code to send. + */ + max_blindex = build_bl_tree(s); + + /* Determine the best encoding. Compute the block lengths in bytes. */ + opt_lenb = (s.opt_len + 3 + 7) >>> 3; + static_lenb = (s.static_len + 3 + 7) >>> 3; + + // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, + // s->last_lit)); + + if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; } + + } else { + // Assert(buf != (char*)0, "lost buf"); + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ + } + + if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) { + /* 4: two words for the lengths */ + + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. + * Otherwise we can't have processed more than WSIZE input bytes since + * the last block flush, because compression would have been + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to + * transform a block into a stored block. + */ + _tr_stored_block(s, buf, stored_len, last); + + } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) { + + send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3); + compress_block(s, static_ltree, static_dtree); + + } else { + send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3); + send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1); + compress_block(s, s.dyn_ltree, s.dyn_dtree); + } + // Assert (s->compressed_len == s->bits_sent, "bad compressed size"); + /* The above check is made mod 2^32, for files larger than 512 MB + * and uLong implemented on 32 bits. + */ + init_block(s); + + if (last) { + bi_windup(s); + } + // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, + // s->compressed_len-7*last)); +} + +/* =========================================================================== + * Save the match info and tally the frequency counts. Return true if + * the current block must be flushed. + */ +function _tr_tally(s, dist, lc) +// deflate_state *s; +// unsigned dist; /* distance of matched string */ +// unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ +{ + //var out_length, in_length, dcode; + + s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff; + s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff; + + s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff; + s.last_lit++; + + if (dist === 0) { + /* lc is the unmatched char */ + s.dyn_ltree[lc * 2]/*.Freq*/++; + } else { + s.matches++; + /* Here, lc is the match length - MIN_MATCH */ + dist--; /* dist = match distance - 1 */ + //Assert((ush)dist < (ush)MAX_DIST(s) && + // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && + // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); + + s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++; + s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++; + } + +// (!) This block is disabled in zlib defailts, +// don't enable it for binary compatibility + +//#ifdef TRUNCATE_BLOCK +// /* Try to guess if it is profitable to stop the current block here */ +// if ((s.last_lit & 0x1fff) === 0 && s.level > 2) { +// /* Compute an upper bound for the compressed length */ +// out_length = s.last_lit*8; +// in_length = s.strstart - s.block_start; +// +// for (dcode = 0; dcode < D_CODES; dcode++) { +// out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]); +// } +// out_length >>>= 3; +// //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", +// // s->last_lit, in_length, out_length, +// // 100L - out_length*100L/in_length)); +// if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) { +// return true; +// } +// } +//#endif + + return (s.last_lit === s.lit_bufsize - 1); + /* We avoid equality with lit_bufsize because of wraparound at 64K + * on 16 bit machines and because stored blocks are restricted to + * 64K-1 bytes. + */ +} + +exports._tr_init = _tr_init; +exports._tr_stored_block = _tr_stored_block; +exports._tr_flush_block = _tr_flush_block; +exports._tr_tally = _tr_tally; +exports._tr_align = _tr_align; + +},{"../utils/common":451}],461:[function(require,module,exports){ +'use strict'; + + +function ZStream() { + /* next input byte */ + this.input = null; // JS specific, because we have no pointers + this.next_in = 0; + /* number of bytes available at input */ + this.avail_in = 0; + /* total number of input bytes read so far */ + this.total_in = 0; + /* next output byte should be put there */ + this.output = null; // JS specific, because we have no pointers + this.next_out = 0; + /* remaining free space at output */ + this.avail_out = 0; + /* total number of bytes output so far */ + this.total_out = 0; + /* last error message, NULL if no error */ + this.msg = ''/*Z_NULL*/; + /* not visible by applications */ + this.state = null; + /* best guess about the data type: binary or text */ + this.data_type = 2/*Z_UNKNOWN*/; + /* adler32 value of the uncompressed data */ + this.adler = 0; +} + +module.exports = ZStream; + +},{}],462:[function(require,module,exports){ +module.exports = function parseUnit(str, out) { + if (!out) + out = [ 0, '' ] + + str = String(str) + var num = parseFloat(str, 10) + out[0] = num + out[1] = str.match(/[\d.\-\+]*\s*(.*)/)[1] || '' + return out +} +},{}],463:[function(require,module,exports){ +(function (process){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// resolves . and .. elements in a path array with directory names there +// must be no slashes, empty elements, or device names (c:\) in the array +// (so also no leading and trailing slashes - it does not distinguish +// relative and absolute paths) +function normalizeArray(parts, allowAboveRoot) { + // if the path tries to go above the root, `up` ends up > 0 + var up = 0; + for (var i = parts.length - 1; i >= 0; i--) { + var last = parts[i]; + if (last === '.') { + parts.splice(i, 1); + } else if (last === '..') { + parts.splice(i, 1); + up++; + } else if (up) { + parts.splice(i, 1); + up--; + } + } + + // if the path is allowed to go above the root, restore leading ..s + if (allowAboveRoot) { + for (; up--; up) { + parts.unshift('..'); + } + } + + return parts; +} + +// Split a filename into [root, dir, basename, ext], unix version +// 'root' is just a slash, or nothing. +var splitPathRe = + /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; +var splitPath = function(filename) { + return splitPathRe.exec(filename).slice(1); +}; + +// path.resolve([from ...], to) +// posix version +exports.resolve = function() { + var resolvedPath = '', + resolvedAbsolute = false; + + for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { + var path = (i >= 0) ? arguments[i] : process.cwd(); + + // Skip empty and invalid entries + if (typeof path !== 'string') { + throw new TypeError('Arguments to path.resolve must be strings'); + } else if (!path) { + continue; + } + + resolvedPath = path + '/' + resolvedPath; + resolvedAbsolute = path.charAt(0) === '/'; + } + + // At this point the path should be resolved to a full absolute path, but + // handle relative paths to be safe (might happen when process.cwd() fails) + + // Normalize the path + resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) { + return !!p; + }), !resolvedAbsolute).join('/'); + + return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; +}; + +// path.normalize(path) +// posix version +exports.normalize = function(path) { + var isAbsolute = exports.isAbsolute(path), + trailingSlash = substr(path, -1) === '/'; + + // Normalize the path + path = normalizeArray(filter(path.split('/'), function(p) { + return !!p; + }), !isAbsolute).join('/'); + + if (!path && !isAbsolute) { + path = '.'; + } + if (path && trailingSlash) { + path += '/'; + } + + return (isAbsolute ? '/' : '') + path; +}; + +// posix version +exports.isAbsolute = function(path) { + return path.charAt(0) === '/'; +}; + +// posix version +exports.join = function() { + var paths = Array.prototype.slice.call(arguments, 0); + return exports.normalize(filter(paths, function(p, index) { + if (typeof p !== 'string') { + throw new TypeError('Arguments to path.join must be strings'); + } + return p; + }).join('/')); +}; + + +// path.relative(from, to) +// posix version +exports.relative = function(from, to) { + from = exports.resolve(from).substr(1); + to = exports.resolve(to).substr(1); + + function trim(arr) { + var start = 0; + for (; start < arr.length; start++) { + if (arr[start] !== '') break; + } + + var end = arr.length - 1; + for (; end >= 0; end--) { + if (arr[end] !== '') break; + } + + if (start > end) return []; + return arr.slice(start, end - start + 1); + } + + var fromParts = trim(from.split('/')); + var toParts = trim(to.split('/')); + + var length = Math.min(fromParts.length, toParts.length); + var samePartsLength = length; + for (var i = 0; i < length; i++) { + if (fromParts[i] !== toParts[i]) { + samePartsLength = i; + break; + } + } + + var outputParts = []; + for (var i = samePartsLength; i < fromParts.length; i++) { + outputParts.push('..'); + } + + outputParts = outputParts.concat(toParts.slice(samePartsLength)); + + return outputParts.join('/'); +}; + +exports.sep = '/'; +exports.delimiter = ':'; + +exports.dirname = function(path) { + var result = splitPath(path), + root = result[0], + dir = result[1]; + + if (!root && !dir) { + // No dirname whatsoever + return '.'; + } + + if (dir) { + // It has a dirname, strip trailing slash + dir = dir.substr(0, dir.length - 1); + } + + return root + dir; +}; + + +exports.basename = function(path, ext) { + var f = splitPath(path)[2]; + // TODO: make this comparison case-insensitive on windows? + if (ext && f.substr(-1 * ext.length) === ext) { + f = f.substr(0, f.length - ext.length); + } + return f; +}; + + +exports.extname = function(path) { + return splitPath(path)[3]; +}; + +function filter (xs, f) { + if (xs.filter) return xs.filter(f); + var res = []; + for (var i = 0; i < xs.length; i++) { + if (f(xs[i], i, xs)) res.push(xs[i]); + } + return res; +} + +// String.prototype.substr - negative index don't work in IE8 +var substr = 'ab'.substr(-1) === 'b' + ? function (str, start, len) { return str.substr(start, len) } + : function (str, start, len) { + if (start < 0) start = str.length + start; + return str.substr(start, len); + } +; + +}).call(this,require('_process')) +},{"_process":923}],464:[function(require,module,exports){ +'use strict'; + +// lightweight Buffer shim for pbf browser build +// based on code from github.com/feross/buffer (MIT-licensed) + +module.exports = Buffer; + +var ieee754 = require('ieee754'); + +var BufferMethods; + +function Buffer(length) { + var arr; + if (length && length.length) { + arr = length; + length = arr.length; + } + var buf = new Uint8Array(length || 0); + if (arr) buf.set(arr); + + buf.readUInt32LE = BufferMethods.readUInt32LE; + buf.writeUInt32LE = BufferMethods.writeUInt32LE; + buf.readInt32LE = BufferMethods.readInt32LE; + buf.writeInt32LE = BufferMethods.writeInt32LE; + buf.readFloatLE = BufferMethods.readFloatLE; + buf.writeFloatLE = BufferMethods.writeFloatLE; + buf.readDoubleLE = BufferMethods.readDoubleLE; + buf.writeDoubleLE = BufferMethods.writeDoubleLE; + buf.toString = BufferMethods.toString; + buf.write = BufferMethods.write; + buf.slice = BufferMethods.slice; + buf.copy = BufferMethods.copy; + + buf._isBuffer = true; + return buf; +} + +var lastStr, lastStrEncoded; + +BufferMethods = { + readUInt32LE: function(pos) { + return ((this[pos]) | + (this[pos + 1] << 8) | + (this[pos + 2] << 16)) + + (this[pos + 3] * 0x1000000); + }, + + writeUInt32LE: function(val, pos) { + this[pos] = val; + this[pos + 1] = (val >>> 8); + this[pos + 2] = (val >>> 16); + this[pos + 3] = (val >>> 24); + }, + + readInt32LE: function(pos) { + return ((this[pos]) | + (this[pos + 1] << 8) | + (this[pos + 2] << 16)) + + (this[pos + 3] << 24); + }, + + readFloatLE: function(pos) { return ieee754.read(this, pos, true, 23, 4); }, + readDoubleLE: function(pos) { return ieee754.read(this, pos, true, 52, 8); }, + + writeFloatLE: function(val, pos) { return ieee754.write(this, val, pos, true, 23, 4); }, + writeDoubleLE: function(val, pos) { return ieee754.write(this, val, pos, true, 52, 8); }, + + toString: function(encoding, start, end) { + var str = '', + tmp = ''; + + start = start || 0; + end = Math.min(this.length, end || this.length); + + for (var i = start; i < end; i++) { + var ch = this[i]; + if (ch <= 0x7F) { + str += decodeURIComponent(tmp) + String.fromCharCode(ch); + tmp = ''; + } else { + tmp += '%' + ch.toString(16); + } + } + + str += decodeURIComponent(tmp); + + return str; + }, + + write: function(str, pos) { + var bytes = str === lastStr ? lastStrEncoded : encodeString(str); + for (var i = 0; i < bytes.length; i++) { + this[pos + i] = bytes[i]; + } + }, + + slice: function(start, end) { + return this.subarray(start, end); + }, + + copy: function(buf, pos) { + pos = pos || 0; + for (var i = 0; i < this.length; i++) { + buf[pos + i] = this[i]; + } + } +}; + +BufferMethods.writeInt32LE = BufferMethods.writeUInt32LE; + +Buffer.byteLength = function(str) { + lastStr = str; + lastStrEncoded = encodeString(str); + return lastStrEncoded.length; +}; + +Buffer.isBuffer = function(buf) { + return !!(buf && buf._isBuffer); +}; + +function encodeString(str) { + var length = str.length, + bytes = []; + + for (var i = 0, c, lead; i < length; i++) { + c = str.charCodeAt(i); // code point + + if (c > 0xD7FF && c < 0xE000) { + + if (lead) { + if (c < 0xDC00) { + bytes.push(0xEF, 0xBF, 0xBD); + lead = c; + continue; + + } else { + c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000; + lead = null; + } + + } else { + if (c > 0xDBFF || (i + 1 === length)) bytes.push(0xEF, 0xBF, 0xBD); + else lead = c; + + continue; + } + + } else if (lead) { + bytes.push(0xEF, 0xBF, 0xBD); + lead = null; + } + + if (c < 0x80) bytes.push(c); + else if (c < 0x800) bytes.push(c >> 0x6 | 0xC0, c & 0x3F | 0x80); + else if (c < 0x10000) bytes.push(c >> 0xC | 0xE0, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80); + else bytes.push(c >> 0x12 | 0xF0, c >> 0xC & 0x3F | 0x80, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80); + } + return bytes; +} + +},{"ieee754":260}],465:[function(require,module,exports){ +(function (global){ +'use strict'; + +module.exports = Pbf; + +var Buffer = global.Buffer || require('./buffer'); + +function Pbf(buf) { + this.buf = !Buffer.isBuffer(buf) ? new Buffer(buf || 0) : buf; + this.pos = 0; + this.length = this.buf.length; +} + +Pbf.Varint = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum +Pbf.Fixed64 = 1; // 64-bit: double, fixed64, sfixed64 +Pbf.Bytes = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields +Pbf.Fixed32 = 5; // 32-bit: float, fixed32, sfixed32 + +var SHIFT_LEFT_32 = (1 << 16) * (1 << 16), + SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32, + POW_2_63 = Math.pow(2, 63); + +Pbf.prototype = { + + destroy: function() { + this.buf = null; + }, + + // === READING ================================================================= + + readFields: function(readField, result, end) { + end = end || this.length; + + while (this.pos < end) { + var val = this.readVarint(), + tag = val >> 3, + startPos = this.pos; + + readField(tag, result, this); + + if (this.pos === startPos) this.skip(val); + } + return result; + }, + + readMessage: function(readField, result) { + return this.readFields(readField, result, this.readVarint() + this.pos); + }, + + readFixed32: function() { + var val = this.buf.readUInt32LE(this.pos); + this.pos += 4; + return val; + }, + + readSFixed32: function() { + var val = this.buf.readInt32LE(this.pos); + this.pos += 4; + return val; + }, + + // 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed) + + readFixed64: function() { + var val = this.buf.readUInt32LE(this.pos) + this.buf.readUInt32LE(this.pos + 4) * SHIFT_LEFT_32; + this.pos += 8; + return val; + }, + + readSFixed64: function() { + var val = this.buf.readUInt32LE(this.pos) + this.buf.readInt32LE(this.pos + 4) * SHIFT_LEFT_32; + this.pos += 8; + return val; + }, + + readFloat: function() { + var val = this.buf.readFloatLE(this.pos); + this.pos += 4; + return val; + }, + + readDouble: function() { + var val = this.buf.readDoubleLE(this.pos); + this.pos += 8; + return val; + }, + + readVarint: function() { + var buf = this.buf, + val, b; + + b = buf[this.pos++]; val = b & 0x7f; if (b < 0x80) return val; + b = buf[this.pos++]; val |= (b & 0x7f) << 7; if (b < 0x80) return val; + b = buf[this.pos++]; val |= (b & 0x7f) << 14; if (b < 0x80) return val; + b = buf[this.pos++]; val |= (b & 0x7f) << 21; if (b < 0x80) return val; + + return readVarintRemainder(val, this); + }, + + readVarint64: function() { + var startPos = this.pos, + val = this.readVarint(); + + if (val < POW_2_63) return val; + + var pos = this.pos - 2; + while (this.buf[pos] === 0xff) pos--; + if (pos < startPos) pos = startPos; + + val = 0; + for (var i = 0; i < pos - startPos + 1; i++) { + var b = ~this.buf[startPos + i] & 0x7f; + val += i < 4 ? b << i * 7 : b * Math.pow(2, i * 7); + } + + return -val - 1; + }, + + readSVarint: function() { + var num = this.readVarint(); + return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding + }, + + readBoolean: function() { + return Boolean(this.readVarint()); + }, + + readString: function() { + var end = this.readVarint() + this.pos, + str = this.buf.toString('utf8', this.pos, end); + this.pos = end; + return str; + }, + + readBytes: function() { + var end = this.readVarint() + this.pos, + buffer = this.buf.slice(this.pos, end); + this.pos = end; + return buffer; + }, + + // verbose for performance reasons; doesn't affect gzipped size + + readPackedVarint: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readVarint()); + return arr; + }, + readPackedSVarint: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readSVarint()); + return arr; + }, + readPackedBoolean: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readBoolean()); + return arr; + }, + readPackedFloat: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readFloat()); + return arr; + }, + readPackedDouble: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readDouble()); + return arr; + }, + readPackedFixed32: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readFixed32()); + return arr; + }, + readPackedSFixed32: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readSFixed32()); + return arr; + }, + readPackedFixed64: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readFixed64()); + return arr; + }, + readPackedSFixed64: function() { + var end = this.readVarint() + this.pos, arr = []; + while (this.pos < end) arr.push(this.readSFixed64()); + return arr; + }, + + skip: function(val) { + var type = val & 0x7; + if (type === Pbf.Varint) while (this.buf[this.pos++] > 0x7f) {} + else if (type === Pbf.Bytes) this.pos = this.readVarint() + this.pos; + else if (type === Pbf.Fixed32) this.pos += 4; + else if (type === Pbf.Fixed64) this.pos += 8; + else throw new Error('Unimplemented type: ' + type); + }, + + // === WRITING ================================================================= + + writeTag: function(tag, type) { + this.writeVarint((tag << 3) | type); + }, + + realloc: function(min) { + var length = this.length || 16; + + while (length < this.pos + min) length *= 2; + + if (length !== this.length) { + var buf = new Buffer(length); + this.buf.copy(buf); + this.buf = buf; + this.length = length; + } + }, + + finish: function() { + this.length = this.pos; + this.pos = 0; + return this.buf.slice(0, this.length); + }, + + writeFixed32: function(val) { + this.realloc(4); + this.buf.writeUInt32LE(val, this.pos); + this.pos += 4; + }, + + writeSFixed32: function(val) { + this.realloc(4); + this.buf.writeInt32LE(val, this.pos); + this.pos += 4; + }, + + writeFixed64: function(val) { + this.realloc(8); + this.buf.writeInt32LE(val & -1, this.pos); + this.buf.writeUInt32LE(Math.floor(val * SHIFT_RIGHT_32), this.pos + 4); + this.pos += 8; + }, + + writeSFixed64: function(val) { + this.realloc(8); + this.buf.writeInt32LE(val & -1, this.pos); + this.buf.writeInt32LE(Math.floor(val * SHIFT_RIGHT_32), this.pos + 4); + this.pos += 8; + }, + + writeVarint: function(val) { + val = +val; + + if (val > 0xfffffff) { + writeBigVarint(val, this); + return; + } + + this.realloc(4); + + this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return; + this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return; + this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return; + this.buf[this.pos++] = (val >>> 7) & 0x7f; + }, + + writeSVarint: function(val) { + this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2); + }, + + writeBoolean: function(val) { + this.writeVarint(Boolean(val)); + }, + + writeString: function(str) { + str = String(str); + var bytes = Buffer.byteLength(str); + this.writeVarint(bytes); + this.realloc(bytes); + this.buf.write(str, this.pos); + this.pos += bytes; + }, + + writeFloat: function(val) { + this.realloc(4); + this.buf.writeFloatLE(val, this.pos); + this.pos += 4; + }, + + writeDouble: function(val) { + this.realloc(8); + this.buf.writeDoubleLE(val, this.pos); + this.pos += 8; + }, + + writeBytes: function(buffer) { + var len = buffer.length; + this.writeVarint(len); + this.realloc(len); + for (var i = 0; i < len; i++) this.buf[this.pos++] = buffer[i]; + }, + + writeRawMessage: function(fn, obj) { + this.pos++; // reserve 1 byte for short message length + + // write the message directly to the buffer and see how much was written + var startPos = this.pos; + fn(obj, this); + var len = this.pos - startPos; + + if (len >= 0x80) reallocForRawMessage(startPos, len, this); + + // finally, write the message length in the reserved place and restore the position + this.pos = startPos - 1; + this.writeVarint(len); + this.pos += len; + }, + + writeMessage: function(tag, fn, obj) { + this.writeTag(tag, Pbf.Bytes); + this.writeRawMessage(fn, obj); + }, + + writePackedVarint: function(tag, arr) { this.writeMessage(tag, writePackedVarint, arr); }, + writePackedSVarint: function(tag, arr) { this.writeMessage(tag, writePackedSVarint, arr); }, + writePackedBoolean: function(tag, arr) { this.writeMessage(tag, writePackedBoolean, arr); }, + writePackedFloat: function(tag, arr) { this.writeMessage(tag, writePackedFloat, arr); }, + writePackedDouble: function(tag, arr) { this.writeMessage(tag, writePackedDouble, arr); }, + writePackedFixed32: function(tag, arr) { this.writeMessage(tag, writePackedFixed32, arr); }, + writePackedSFixed32: function(tag, arr) { this.writeMessage(tag, writePackedSFixed32, arr); }, + writePackedFixed64: function(tag, arr) { this.writeMessage(tag, writePackedFixed64, arr); }, + writePackedSFixed64: function(tag, arr) { this.writeMessage(tag, writePackedSFixed64, arr); }, + + writeBytesField: function(tag, buffer) { + this.writeTag(tag, Pbf.Bytes); + this.writeBytes(buffer); + }, + writeFixed32Field: function(tag, val) { + this.writeTag(tag, Pbf.Fixed32); + this.writeFixed32(val); + }, + writeSFixed32Field: function(tag, val) { + this.writeTag(tag, Pbf.Fixed32); + this.writeSFixed32(val); + }, + writeFixed64Field: function(tag, val) { + this.writeTag(tag, Pbf.Fixed64); + this.writeFixed64(val); + }, + writeSFixed64Field: function(tag, val) { + this.writeTag(tag, Pbf.Fixed64); + this.writeSFixed64(val); + }, + writeVarintField: function(tag, val) { + this.writeTag(tag, Pbf.Varint); + this.writeVarint(val); + }, + writeSVarintField: function(tag, val) { + this.writeTag(tag, Pbf.Varint); + this.writeSVarint(val); + }, + writeStringField: function(tag, str) { + this.writeTag(tag, Pbf.Bytes); + this.writeString(str); + }, + writeFloatField: function(tag, val) { + this.writeTag(tag, Pbf.Fixed32); + this.writeFloat(val); + }, + writeDoubleField: function(tag, val) { + this.writeTag(tag, Pbf.Fixed64); + this.writeDouble(val); + }, + writeBooleanField: function(tag, val) { + this.writeVarintField(tag, Boolean(val)); + } +}; + +function readVarintRemainder(val, pbf) { + var buf = pbf.buf, b; + + b = buf[pbf.pos++]; val += (b & 0x7f) * 0x10000000; if (b < 0x80) return val; + b = buf[pbf.pos++]; val += (b & 0x7f) * 0x800000000; if (b < 0x80) return val; + b = buf[pbf.pos++]; val += (b & 0x7f) * 0x40000000000; if (b < 0x80) return val; + b = buf[pbf.pos++]; val += (b & 0x7f) * 0x2000000000000; if (b < 0x80) return val; + b = buf[pbf.pos++]; val += (b & 0x7f) * 0x100000000000000; if (b < 0x80) return val; + b = buf[pbf.pos++]; val += (b & 0x7f) * 0x8000000000000000; if (b < 0x80) return val; + + throw new Error('Expected varint not more than 10 bytes'); +} + +function writeBigVarint(val, pbf) { + pbf.realloc(10); + + var maxPos = pbf.pos + 10; + + while (val >= 1) { + if (pbf.pos >= maxPos) throw new Error('Given varint doesn\'t fit into 10 bytes'); + var b = val & 0xff; + pbf.buf[pbf.pos++] = b | (val >= 0x80 ? 0x80 : 0); + val /= 0x80; + } +} + +function reallocForRawMessage(startPos, len, pbf) { + var extraLen = + len <= 0x3fff ? 1 : + len <= 0x1fffff ? 2 : + len <= 0xfffffff ? 3 : Math.ceil(Math.log(len) / (Math.LN2 * 7)); + + // if 1 byte isn't enough for encoding message length, shift the data to the right + pbf.realloc(extraLen); + for (var i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i]; +} + +function writePackedVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]); } +function writePackedSVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]); } +function writePackedFloat(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]); } +function writePackedDouble(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]); } +function writePackedBoolean(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]); } +function writePackedFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]); } +function writePackedSFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]); } +function writePackedFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]); } +function writePackedSFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]); } + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"./buffer":464}],466:[function(require,module,exports){ +"use strict" + +module.exports = permutationSign + +var BRUTE_FORCE_CUTOFF = 32 + +var pool = require("typedarray-pool") + +function permutationSign(p) { + var n = p.length + if(n < BRUTE_FORCE_CUTOFF) { + //Use quadratic algorithm for small n + var sgn = 1 + for(var i=0; i0; --i) { + t = pinv[i] + s = p[i] + p[i] = p[t] + p[t] = s + pinv[i] = pinv[s] + pinv[s] = t + r = (r + s) * i + } + pool.freeUint32(pinv) + pool.freeUint32(p) + return r +} + +function unrank(n, r, p) { + switch(n) { + case 0: + if(p) { return p } + return [] + case 1: + if(p) { + p[0] = 0 + return p + } else { + return [0] + } + case 2: + if(p) { + if(r) { + p[0] = 0 + p[1] = 1 + } else { + p[0] = 1 + p[1] = 0 + } + return p + } else { + return r ? [0,1] : [1,0] + } + default: + break + } + p = p || new Array(n) + var s, t, i, nf=1 + p[0] = 0 + for(i=1; i0; --i) { + s = (r / nf)|0 + r = (r - s * nf)|0 + nf = (nf / i)|0 + t = p[i]|0 + p[i] = p[s]|0 + p[s] = t|0 + } + return p +} + +exports.rank = rank +exports.unrank = unrank + +},{"invert-permutation":268,"typedarray-pool":992}],468:[function(require,module,exports){ +"use strict" + +module.exports = planarDual + +var compareAngle = require("compare-angle") + +function planarDual(cells, positions) { + + var numVertices = positions.length|0 + var numEdges = cells.length + var adj = [new Array(numVertices), new Array(numVertices)] + for(var i=0; i 0) { + nextCell = adj[i][b][0] + nextDir = i + break + } + } + nextVertex = nextCell[nextDir^1] + + for(var dir=0; dir<2; ++dir) { + var nbhd = adj[dir][b] + for(var k=0; k 0) { + nextCell = e + nextVertex = p + nextDir = dir + } + } + } + if(noCut) { + return nextVertex + } + if(nextCell) { + cut(nextCell, nextDir) + } + return nextVertex + } + + function extractCycle(v, dir) { + var e0 = adj[dir][v][0] + var cycle = [v] + cut(e0, dir) + var u = e0[dir^1] + var d0 = dir + while(true) { + while(u !== v) { + cycle.push(u) + u = next(cycle[cycle.length-2], u, false) + } + if(adj[0][v].length + adj[1][v].length === 0) { + break + } + var a = cycle[cycle.length-1] + var b = v + var c = cycle[1] + var d = next(a, b, true) + if(compareAngle(positions[a], positions[b], positions[c], positions[d]) < 0) { + break + } + cycle.push(v) + u = next(a, b) + } + return cycle + } + + function shouldGlue(pcycle, ncycle) { + return (ncycle[1] === ncycle[ncycle.length-1]) + } + + for(var i=0; i 0) { + var ni = adj[0][i].length + var ncycle = extractCycle(i,j) + if(shouldGlue(pcycle, ncycle)) { + //Glue together trivial cycles + pcycle.push.apply(pcycle, ncycle) + } else { + if(pcycle.length > 0) { + cycles.push(pcycle) + } + pcycle = ncycle + } + } + if(pcycle.length > 0) { + cycles.push(pcycle) + } + } + } + + //Combine paths and loops together + return cycles +} +},{"compare-angle":63}],469:[function(require,module,exports){ +'use strict' + +module.exports = trimLeaves + +var e2a = require('edges-to-adjacency-list') + +function trimLeaves(edges, positions) { + var adj = e2a(edges, positions.length) + var live = new Array(positions.length) + var nbhd = new Array(positions.length) + + var dead = [] + for(var i=0; i 0) { + var v = dead.pop() + live[v] = false + var n = adj[v] + for(var i=0; i 0 + } + + //Extract all clockwise faces + faces = faces.filter(ccw) + + //Detect which loops are contained in one another to handle parent-of relation + var numFaces = faces.length + var parent = new Array(numFaces) + var containment = new Array(numFaces) + for(var i=0; i 0) { + var top = toVisit.pop() + var nbhd = fadj[top] + uniq(nbhd, function(a,b) { + return a-b + }) + var nnbhr = nbhd.length + var p = parity[top] + var polyline + if(p === 0) { + var c = faces[top] + polyline = [c] + } + for(var i=0; i= 0) { + continue + } + parity[f] = p^1 + toVisit.push(f) + if(p === 0) { + var c = faces[f] + if(!sharedBoundary(c)) { + c.reverse() + polyline.push(c) + } + } + } + if(p === 0) { + result.push(polyline) + } + } + + return result +} +},{"./lib/trim-leaves":469,"edges-to-adjacency-list":92,"planar-dual":468,"point-in-big-polygon":920,"robust-sum":959,"two-product":990,"uniq":994}],471:[function(require,module,exports){ +'use strict'; + +var Lib = require('../src/lib'); +var rules = { + "X,X div": "font-family:'Open Sans', verdana, arial, sans-serif;margin:0;padding:0;", + "X input,X button": "font-family:'Open Sans', verdana, arial, sans-serif;", + "X input:focus,X button:focus": "outline:none;", + "X a": "text-decoration:none;", + "X a:hover": "text-decoration:none;", + "X .crisp": "shape-rendering:crispEdges;", + "X .user-select-none": "-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;-o-user-select:none;user-select:none;", + "X svg": "overflow:hidden;", + "X svg a": "fill:#447adb;", + "X svg a:hover": "fill:#3c6dc5;", + "X .main-svg": "position:absolute;top:0;left:0;pointer-events:none;", + "X .main-svg .draglayer": "pointer-events:all;", + "X .cursor-pointer": "cursor:pointer;", + "X .cursor-crosshair": "cursor:crosshair;", + "X .cursor-move": "cursor:move;", + "X .cursor-col-resize": "cursor:col-resize;", + "X .cursor-row-resize": "cursor:row-resize;", + "X .cursor-ns-resize": "cursor:ns-resize;", + "X .cursor-ew-resize": "cursor:ew-resize;", + "X .cursor-sw-resize": "cursor:sw-resize;", + "X .cursor-s-resize": "cursor:s-resize;", + "X .cursor-se-resize": "cursor:se-resize;", + "X .cursor-w-resize": "cursor:w-resize;", + "X .cursor-e-resize": "cursor:e-resize;", + "X .cursor-nw-resize": "cursor:nw-resize;", + "X .cursor-n-resize": "cursor:n-resize;", + "X .cursor-ne-resize": "cursor:ne-resize;", + "X .modebar": "position:absolute;top:2px;right:2px;z-index:1001;background:rgba(255,255,255,0.7);", + "X .modebar--hover": "opacity:0;-webkit-transition:opacity 0.3s ease 0s;-moz-transition:opacity 0.3s ease 0s;-ms-transition:opacity 0.3s ease 0s;-o-transition:opacity 0.3s ease 0s;transition:opacity 0.3s ease 0s;", + "X:hover .modebar--hover": "opacity:1;", + "X .modebar-group": "float:left;display:inline-block;box-sizing:border-box;margin-left:8px;position:relative;vertical-align:middle;white-space:nowrap;", + "X .modebar-group:first-child": "margin-left:0px;", + "X .modebar-btn": "position:relative;font-size:16px;padding:3px 4px;cursor:pointer;line-height:normal;box-sizing:border-box;", + "X .modebar-btn svg": "position:relative;top:2px;", + "X .modebar-btn path": "fill:rgba(0,31,95,0.3);", + "X .modebar-btn.active path,X .modebar-btn:hover path": "fill:rgba(0,22,72,0.5);", + "X .modebar-btn.modebar-btn--logo": "padding:3px 1px;", + "X .modebar-btn.modebar-btn--logo path": "fill:#447adb !important;", + "X [data-title]:before,X [data-title]:after": "position:absolute;-webkit-transform:translate3d(0, 0, 0);-moz-transform:translate3d(0, 0, 0);-ms-transform:translate3d(0, 0, 0);-o-transform:translate3d(0, 0, 0);transform:translate3d(0, 0, 0);display:none;opacity:0;z-index:1001;pointer-events:none;top:110%;right:50%;", + "X [data-title]:hover:before,X [data-title]:hover:after": "display:block;opacity:1;", + "X [data-title]:before": "content:'';position:absolute;background:transparent;border:6px solid transparent;z-index:1002;margin-top:-12px;border-bottom-color:#69738a;margin-right:-6px;", + "X [data-title]:after": "content:attr(data-title);background:#69738a;color:white;padding:8px 10px;font-size:12px;line-height:12px;white-space:nowrap;margin-right:-18px;border-radius:2px;", + "X .select-outline": "fill:none;stroke-width:1;shape-rendering:crispEdges;", + "X .select-outline-1": "stroke:white;", + "X .select-outline-2": "stroke:black;stroke-dasharray:2px 2px;", + Y: "font-family:'Open Sans';position:fixed;top:50px;right:20px;z-index:10000;font-size:10pt;max-width:180px;", + "Y p": "margin:0;", + "Y .notifier-note": "min-width:180px;max-width:250px;border:1px solid #fff;z-index:3000;margin:0;background-color:#8c97af;background-color:rgba(140,151,175,0.9);color:#fff;padding:10px;", + "Y .notifier-close": "color:#fff;opacity:0.8;float:right;padding:0 5px;background:none;border:none;font-size:20px;font-weight:bold;line-height:20px;", + "Y .notifier-close:hover": "color:#444;text-decoration:none;cursor:pointer;" +}; + +for(var selector in rules) { + var fullSelector = selector.replace(/^,/,' ,') + .replace(/X/g, '.js-plotly-plot .plotly') + .replace(/Y/g, '.plotly-notifier'); + Lib.addStyleRule(fullSelector, rules[selector]); +} + +},{"../src/lib":610}],472:[function(require,module,exports){ +'use strict'; + +module.exports = { + 'undo': { + 'width': 857.1, + 'path': 'm857 350q0-87-34-166t-91-137-137-92-166-34q-96 0-183 41t-147 114q-4 6-4 13t5 11l76 77q6 5 14 5 9-1 13-7 41-53 100-82t126-29q58 0 110 23t92 61 61 91 22 111-22 111-61 91-92 61-110 23q-55 0-105-20t-90-57l77-77q17-16 8-38-10-23-33-23h-250q-15 0-25 11t-11 25v250q0 24 22 33 22 10 39-8l72-72q60 57 137 88t159 31q87 0 166-34t137-92 91-137 34-166z', + 'ascent': 850, + 'descent': -150 + }, + 'home': { + 'width': 928.6, + 'path': 'm786 296v-267q0-15-11-26t-25-10h-214v214h-143v-214h-214q-15 0-25 10t-11 26v267q0 1 0 2t0 2l321 264 321-264q1-1 1-4z m124 39l-34-41q-5-5-12-6h-2q-7 0-12 3l-386 322-386-322q-7-4-13-4-7 2-12 7l-35 41q-4 5-3 13t6 12l401 334q18 15 42 15t43-15l136-114v109q0 8 5 13t13 5h107q8 0 13-5t5-13v-227l122-102q5-5 6-12t-4-13z', + 'ascent': 850, + 'descent': -150 + }, + 'camera-retro': { + 'width': 1000, + 'path': 'm518 386q0 8-5 13t-13 5q-37 0-63-27t-26-63q0-8 5-13t13-5 12 5 5 13q0 23 16 38t38 16q8 0 13 5t5 13z m125-73q0-59-42-101t-101-42-101 42-42 101 42 101 101 42 101-42 42-101z m-572-320h858v71h-858v-71z m643 320q0 89-62 152t-152 62-151-62-63-152 63-151 151-63 152 63 62 151z m-571 358h214v72h-214v-72z m-72-107h858v143h-462l-36-71h-360v-72z m929 143v-714q0-30-21-51t-50-21h-858q-29 0-50 21t-21 51v714q0 30 21 51t50 21h858q29 0 50-21t21-51z', + 'ascent': 850, + 'descent': -150 + }, + 'zoombox': { + 'width': 1000, + 'path': 'm1000-25l-250 251c40 63 63 138 63 218 0 224-182 406-407 406-224 0-406-182-406-406s183-406 407-406c80 0 155 22 218 62l250-250 125 125z m-812 250l0 438 437 0 0-438-437 0z m62 375l313 0 0-312-313 0 0 312z', + 'ascent': 850, + 'descent': -150 + }, + 'pan': { + 'width': 1000, + 'path': 'm1000 350l-187 188 0-125-250 0 0 250 125 0-188 187-187-187 125 0 0-250-250 0 0 125-188-188 186-187 0 125 252 0 0-250-125 0 187-188 188 188-125 0 0 250 250 0 0-126 187 188z', + 'ascent': 850, + 'descent': -150 + }, + 'zoom_plus': { + 'width': 1000, + 'path': 'm1 787l0-875 875 0 0 875-875 0z m687-500l-187 0 0-187-125 0 0 187-188 0 0 125 188 0 0 187 125 0 0-187 187 0 0-125z', + 'ascent': 850, + 'descent': -150 + }, + 'zoom_minus': { + 'width': 1000, + 'path': 'm0 788l0-876 875 0 0 876-875 0z m688-500l-500 0 0 125 500 0 0-125z', + 'ascent': 850, + 'descent': -150 + }, + 'autoscale': { + 'width': 1000, + 'path': 'm250 850l-187 0-63 0 0-62 0-188 63 0 0 188 187 0 0 62z m688 0l-188 0 0-62 188 0 0-188 62 0 0 188 0 62-62 0z m-875-938l0 188-63 0 0-188 0-62 63 0 187 0 0 62-187 0z m875 188l0-188-188 0 0-62 188 0 62 0 0 62 0 188-62 0z m-125 188l-1 0-93-94-156 156 156 156 92-93 2 0 0 250-250 0 0-2 93-92-156-156-156 156 94 92 0 2-250 0 0-250 0 0 93 93 157-156-157-156-93 94 0 0 0-250 250 0 0 0-94 93 156 157 156-157-93-93 0 0 250 0 0 250z', + 'ascent': 850, + 'descent': -150 + }, + 'tooltip_basic': { + 'width': 1500, + 'path': 'm375 725l0 0-375-375 375-374 0-1 1125 0 0 750-1125 0z', + 'ascent': 850, + 'descent': -150 + }, + 'tooltip_compare': { + 'width': 1125, + 'path': 'm187 786l0 2-187-188 188-187 0 0 937 0 0 373-938 0z m0-499l0 1-187-188 188-188 0 0 937 0 0 376-938-1z', + 'ascent': 850, + 'descent': -150 + }, + 'plotlylogo': { + 'width': 1542, + 'path': 'm0-10h182v-140h-182v140z m228 146h183v-286h-183v286z m225 714h182v-1000h-182v1000z m225-285h182v-715h-182v715z m225 142h183v-857h-183v857z m231-428h182v-429h-182v429z m225-291h183v-138h-183v138z', + 'ascent': 850, + 'descent': -150 + }, + 'z-axis': { + 'width': 1000, + 'path': 'm833 5l-17 108v41l-130-65 130-66c0 0 0 38 0 39 0-1 36-14 39-25 4-15-6-22-16-30-15-12-39-16-56-20-90-22-187-23-279-23-261 0-341 34-353 59 3 60 228 110 228 110-140-8-351-35-351-116 0-120 293-142 474-142 155 0 477 22 477 142 0 50-74 79-163 96z m-374 94c-58-5-99-21-99-40 0-24 65-43 144-43 79 0 143 19 143 43 0 19-42 34-98 40v216h87l-132 135-133-135h88v-216z m167 515h-136v1c16 16 31 34 46 52l84 109v54h-230v-71h124v-1c-16-17-28-32-44-51l-89-114v-51h245v72z', + 'ascent': 850, + 'descent': -150 + }, + '3d_rotate': { + 'width': 1000, + 'path': 'm922 660c-5 4-9 7-14 11-359 263-580-31-580-31l-102 28 58-400c0 1 1 1 2 2 118 108 351 249 351 249s-62 27-100 42c88 83 222 183 347 122 16-8 30-17 44-27-2 1-4 2-6 4z m36-329c0 0 64 229-88 296-62 27-124 14-175-11 157-78 225-208 249-266 8-19 11-31 11-31 2 5 6 15 11 32-5-13-8-20-8-20z m-775-239c70-31 117-50 198-32-121 80-199 346-199 346l-96-15-58-12c0 0 55-226 155-287z m603 133l-317-139c0 0 4-4 19-14 7-5 24-15 24-15s-177-147-389 4c235-287 536-112 536-112l31-22 100 299-4-1z m-298-153c6-4 14-9 24-15 0 0-17 10-24 15z', + 'ascent': 850, + 'descent': -150 + }, + 'camera': { + 'width': 1000, + 'path': 'm500 450c-83 0-150-67-150-150 0-83 67-150 150-150 83 0 150 67 150 150 0 83-67 150-150 150z m400 150h-120c-16 0-34 13-39 29l-31 93c-6 15-23 28-40 28h-340c-16 0-34-13-39-28l-31-94c-6-15-23-28-40-28h-120c-55 0-100-45-100-100v-450c0-55 45-100 100-100h800c55 0 100 45 100 100v450c0 55-45 100-100 100z m-400-550c-138 0-250 112-250 250 0 138 112 250 250 250 138 0 250-112 250-250 0-138-112-250-250-250z m365 380c-19 0-35 16-35 35 0 19 16 35 35 35 19 0 35-16 35-35 0-19-16-35-35-35z', + 'ascent': 850, + 'descent': -150 + }, + 'movie': { + 'width': 1000, + 'path': 'm938 413l-188-125c0 37-17 71-44 94 64 38 107 107 107 187 0 121-98 219-219 219-121 0-219-98-219-219 0-61 25-117 66-156h-115c30 33 49 76 49 125 0 103-84 187-187 187s-188-84-188-187c0-57 26-107 65-141-38-22-65-62-65-109v-250c0-70 56-126 125-126h500c69 0 125 56 125 126l188-126c34 0 62 28 62 63v375c0 35-28 63-62 63z m-750 0c-69 0-125 56-125 125s56 125 125 125 125-56 125-125-56-125-125-125z m406-1c-87 0-157 70-157 157 0 86 70 156 157 156s156-70 156-156-70-157-156-157z', + 'ascent': 850, + 'descent': -150 + }, + 'question': { + 'width': 857.1, + 'path': 'm500 82v107q0 8-5 13t-13 5h-107q-8 0-13-5t-5-13v-107q0-8 5-13t13-5h107q8 0 13 5t5 13z m143 375q0 49-31 91t-77 65-95 23q-136 0-207-119-9-14 4-24l74-55q4-4 10-4 9 0 14 7 30 38 48 51 19 14 48 14 27 0 48-15t21-33q0-21-11-34t-38-25q-35-16-65-48t-29-70v-20q0-8 5-13t13-5h107q8 0 13 5t5 13q0 10 12 27t30 28q18 10 28 16t25 19 25 27 16 34 7 45z m214-107q0-117-57-215t-156-156-215-58-216 58-155 156-58 215 58 215 155 156 216 58 215-58 156-156 57-215z', + 'ascent': 850, + 'descent': -150 + }, + 'disk': { + 'width': 857.1, + 'path': 'm214-7h429v214h-429v-214z m500 0h72v500q0 8-6 21t-11 20l-157 156q-5 6-19 12t-22 5v-232q0-22-15-38t-38-16h-322q-22 0-37 16t-16 38v232h-72v-714h72v232q0 22 16 38t37 16h465q22 0 38-16t15-38v-232z m-214 518v178q0 8-5 13t-13 5h-107q-7 0-13-5t-5-13v-178q0-8 5-13t13-5h107q7 0 13 5t5 13z m357-18v-518q0-22-15-38t-38-16h-750q-23 0-38 16t-16 38v750q0 22 16 38t38 16h517q23 0 50-12t42-26l156-157q16-15 27-42t11-49z', + 'ascent': 850, + 'descent': -150 + }, + 'lasso': { + 'width': 1031, + 'path': 'm1018 538c-36 207-290 336-568 286-277-48-473-256-436-463 10-57 36-108 76-151-13-66 11-137 68-183 34-28 75-41 114-42l-55-70 0 0c-2-1-3-2-4-3-10-14-8-34 5-45 14-11 34-8 45 4 1 1 2 3 2 5l0 0 113 140c16 11 31 24 45 40 4 3 6 7 8 11 48-3 100 0 151 9 278 48 473 255 436 462z m-624-379c-80 14-149 48-197 96 42 42 109 47 156 9 33-26 47-66 41-105z m-187-74c-19 16-33 37-39 60 50-32 109-55 174-68-42-25-95-24-135 8z m360 75c-34-7-69-9-102-8 8 62-16 128-68 170-73 59-175 54-244-5-9 20-16 40-20 61-28 159 121 317 333 354s407-60 434-217c28-159-121-318-333-355z', + 'ascent': 850, + 'descent': -150 + }, + 'selectbox': { + 'width': 1000, + 'path': 'm0 850l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m285 0l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m-857-286l0-143 143 0 0 143-143 0z m857 0l0-143 143 0 0 143-143 0z m-857-285l0-143 143 0 0 143-143 0z m857 0l0-143 143 0 0 143-143 0z m-857-286l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m285 0l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z', + 'ascent': 850, + 'descent': -150 + } +}; + +},{}],473:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/bar'); + +},{"../src/traces/bar":731}],474:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/box'); + +},{"../src/traces/box":743}],475:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/components/calendars'); + +},{"../src/components/calendars":508}],476:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/candlestick'); + +},{"../src/traces/candlestick":751}],477:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/choropleth'); + +},{"../src/traces/choropleth":756}],478:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/contour'); + +},{"../src/traces/contour":765}],479:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/core'); + +},{"../src/core":596}],480:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/transforms/filter'); + +},{"../src/transforms/filter":896}],481:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/transforms/groupby'); + +},{"../src/transforms/groupby":897}],482:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/heatmap'); + +},{"../src/traces/heatmap":780}],483:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/heatmapgl'); + +},{"../src/traces/heatmapgl":789}],484:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/histogram'); + +},{"../src/traces/histogram":797}],485:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/histogram2d'); + +},{"../src/traces/histogram2d":802}],486:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/histogram2dcontour'); + +},{"../src/traces/histogram2dcontour":806}],487:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +var Plotly = require('./core'); + +// traces +Plotly.register([ + require('./bar'), + require('./box'), + require('./heatmap'), + require('./histogram'), + require('./histogram2d'), + require('./histogram2dcontour'), + require('./pie'), + require('./contour'), + require('./scatterternary'), + + require('./scatter3d'), + require('./surface'), + require('./mesh3d'), + + require('./scattergeo'), + require('./choropleth'), + + require('./scattergl'), + require('./pointcloud'), + require('./heatmapgl'), + + require('./scattermapbox'), + + require('./ohlc'), + require('./candlestick') +]); + +// transforms +// +// Please note that all *transform* methods are executed before +// all *calcTransform* methods - which could possibly lead to +// unexpected results when applying multiple transforms of different types +// to a given trace. +// +// For more info, see: +// https://github.com/plotly/plotly.js/pull/978#pullrequestreview-2403353 +// +Plotly.register([ + require('./filter'), + require('./groupby') +]); + +// components +Plotly.register([ + require('./calendars') +]); + +module.exports = Plotly; + +},{"./bar":473,"./box":474,"./calendars":475,"./candlestick":476,"./choropleth":477,"./contour":478,"./core":479,"./filter":480,"./groupby":481,"./heatmap":482,"./heatmapgl":483,"./histogram":484,"./histogram2d":485,"./histogram2dcontour":486,"./mesh3d":488,"./ohlc":489,"./pie":490,"./pointcloud":491,"./scatter3d":492,"./scattergeo":493,"./scattergl":494,"./scattermapbox":495,"./scatterternary":496,"./surface":497}],488:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/mesh3d'); + +},{"../src/traces/mesh3d":810}],489:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/ohlc'); + +},{"../src/traces/ohlc":815}],490:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/pie'); + +},{"../src/traces/pie":823}],491:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/pointcloud'); + +},{"../src/traces/pointcloud":832}],492:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/scatter3d'); + +},{"../src/traces/scatter3d":862}],493:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/scattergeo'); + +},{"../src/traces/scattergeo":868}],494:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/scattergl'); + +},{"../src/traces/scattergl":873}],495:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/scattermapbox'); + +},{"../src/traces/scattermapbox":880}],496:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/scatterternary'); + +},{"../src/traces/scatterternary":886}],497:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +module.exports = require('../src/traces/surface'); + +},{"../src/traces/surface":895}],498:[function(require,module,exports){ +(function (process,global){ +/*! + * @overview es6-promise - a tiny implementation of Promises/A+. + * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) + * @license Licensed under MIT license + * See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE + * @version 3.3.1 + */ + +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global.ES6Promise = factory()); +}(this, (function () { 'use strict'; + +function objectOrFunction(x) { + return typeof x === 'function' || typeof x === 'object' && x !== null; +} + +function isFunction(x) { + return typeof x === 'function'; +} + +var _isArray = undefined; +if (!Array.isArray) { + _isArray = function (x) { + return Object.prototype.toString.call(x) === '[object Array]'; + }; +} else { + _isArray = Array.isArray; +} + +var isArray = _isArray; + +var len = 0; +var vertxNext = undefined; +var customSchedulerFn = undefined; + +var asap = function asap(callback, arg) { + queue[len] = callback; + queue[len + 1] = arg; + len += 2; + if (len === 2) { + // If len is 2, that means that we need to schedule an async flush. + // If additional callbacks are queued before the queue is flushed, they + // will be processed by this flush that we are scheduling. + if (customSchedulerFn) { + customSchedulerFn(flush); + } else { + scheduleFlush(); + } + } +}; + +function setScheduler(scheduleFn) { + customSchedulerFn = scheduleFn; +} + +function setAsap(asapFn) { + asap = asapFn; +} + +var browserWindow = typeof window !== 'undefined' ? window : undefined; +var browserGlobal = browserWindow || {}; +var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; +var isNode = typeof self === 'undefined' && typeof process !== 'undefined' && ({}).toString.call(process) === '[object process]'; + +// test for web worker but not in IE10 +var isWorker = typeof Uint8ClampedArray !== 'undefined' && typeof importScripts !== 'undefined' && typeof MessageChannel !== 'undefined'; + +// node +function useNextTick() { + // node version 0.10.x displays a deprecation warning when nextTick is used recursively + // see https://github.com/cujojs/when/issues/410 for details + return function () { + return process.nextTick(flush); + }; +} + +// vertx +function useVertxTimer() { + return function () { + vertxNext(flush); + }; +} + +function useMutationObserver() { + var iterations = 0; + var observer = new BrowserMutationObserver(flush); + var node = document.createTextNode(''); + observer.observe(node, { characterData: true }); + + return function () { + node.data = iterations = ++iterations % 2; + }; +} + +// web worker +function useMessageChannel() { + var channel = new MessageChannel(); + channel.port1.onmessage = flush; + return function () { + return channel.port2.postMessage(0); + }; +} + +function useSetTimeout() { + // Store setTimeout reference so es6-promise will be unaffected by + // other code modifying setTimeout (like sinon.useFakeTimers()) + var globalSetTimeout = setTimeout; + return function () { + return globalSetTimeout(flush, 1); + }; +} + +var queue = new Array(1000); +function flush() { + for (var i = 0; i < len; i += 2) { + var callback = queue[i]; + var arg = queue[i + 1]; + + callback(arg); + + queue[i] = undefined; + queue[i + 1] = undefined; + } + + len = 0; +} + +function attemptVertx() { + try { + var r = require; + var vertx = r('vertx'); + vertxNext = vertx.runOnLoop || vertx.runOnContext; + return useVertxTimer(); + } catch (e) { + return useSetTimeout(); + } +} + +var scheduleFlush = undefined; +// Decide what async method to use to triggering processing of queued callbacks: +if (isNode) { + scheduleFlush = useNextTick(); +} else if (BrowserMutationObserver) { + scheduleFlush = useMutationObserver(); +} else if (isWorker) { + scheduleFlush = useMessageChannel(); +} else if (browserWindow === undefined && typeof require === 'function') { + scheduleFlush = attemptVertx(); +} else { + scheduleFlush = useSetTimeout(); +} + +function then(onFulfillment, onRejection) { + var _arguments = arguments; + + var parent = this; + + var child = new this.constructor(noop); + + if (child[PROMISE_ID] === undefined) { + makePromise(child); + } + + var _state = parent._state; + + if (_state) { + (function () { + var callback = _arguments[_state - 1]; + asap(function () { + return invokeCallback(_state, child, callback, parent._result); + }); + })(); + } else { + subscribe(parent, child, onFulfillment, onRejection); + } + + return child; +} + +/** + `Promise.resolve` returns a promise that will become resolved with the + passed `value`. It is shorthand for the following: + + ```javascript + let promise = new Promise(function(resolve, reject){ + resolve(1); + }); + + promise.then(function(value){ + // value === 1 + }); + ``` + + Instead of writing the above, your code now simply becomes the following: + + ```javascript + let promise = Promise.resolve(1); + + promise.then(function(value){ + // value === 1 + }); + ``` + + @method resolve + @static + @param {Any} value value that the returned promise will be resolved with + Useful for tooling. + @return {Promise} a promise that will become fulfilled with the given + `value` +*/ +function resolve(object) { + /*jshint validthis:true */ + var Constructor = this; + + if (object && typeof object === 'object' && object.constructor === Constructor) { + return object; + } + + var promise = new Constructor(noop); + _resolve(promise, object); + return promise; +} + +var PROMISE_ID = Math.random().toString(36).substring(16); + +function noop() {} + +var PENDING = void 0; +var FULFILLED = 1; +var REJECTED = 2; + +var GET_THEN_ERROR = new ErrorObject(); + +function selfFulfillment() { + return new TypeError("You cannot resolve a promise with itself"); +} + +function cannotReturnOwn() { + return new TypeError('A promises callback cannot return that same promise.'); +} + +function getThen(promise) { + try { + return promise.then; + } catch (error) { + GET_THEN_ERROR.error = error; + return GET_THEN_ERROR; + } +} + +function tryThen(then, value, fulfillmentHandler, rejectionHandler) { + try { + then.call(value, fulfillmentHandler, rejectionHandler); + } catch (e) { + return e; + } +} + +function handleForeignThenable(promise, thenable, then) { + asap(function (promise) { + var sealed = false; + var error = tryThen(then, thenable, function (value) { + if (sealed) { + return; + } + sealed = true; + if (thenable !== value) { + _resolve(promise, value); + } else { + fulfill(promise, value); + } + }, function (reason) { + if (sealed) { + return; + } + sealed = true; + + _reject(promise, reason); + }, 'Settle: ' + (promise._label || ' unknown promise')); + + if (!sealed && error) { + sealed = true; + _reject(promise, error); + } + }, promise); +} + +function handleOwnThenable(promise, thenable) { + if (thenable._state === FULFILLED) { + fulfill(promise, thenable._result); + } else if (thenable._state === REJECTED) { + _reject(promise, thenable._result); + } else { + subscribe(thenable, undefined, function (value) { + return _resolve(promise, value); + }, function (reason) { + return _reject(promise, reason); + }); + } +} + +function handleMaybeThenable(promise, maybeThenable, then$$) { + if (maybeThenable.constructor === promise.constructor && then$$ === then && maybeThenable.constructor.resolve === resolve) { + handleOwnThenable(promise, maybeThenable); + } else { + if (then$$ === GET_THEN_ERROR) { + _reject(promise, GET_THEN_ERROR.error); + } else if (then$$ === undefined) { + fulfill(promise, maybeThenable); + } else if (isFunction(then$$)) { + handleForeignThenable(promise, maybeThenable, then$$); + } else { + fulfill(promise, maybeThenable); + } + } +} + +function _resolve(promise, value) { + if (promise === value) { + _reject(promise, selfFulfillment()); + } else if (objectOrFunction(value)) { + handleMaybeThenable(promise, value, getThen(value)); + } else { + fulfill(promise, value); + } +} + +function publishRejection(promise) { + if (promise._onerror) { + promise._onerror(promise._result); + } + + publish(promise); +} + +function fulfill(promise, value) { + if (promise._state !== PENDING) { + return; + } + + promise._result = value; + promise._state = FULFILLED; + + if (promise._subscribers.length !== 0) { + asap(publish, promise); + } +} + +function _reject(promise, reason) { + if (promise._state !== PENDING) { + return; + } + promise._state = REJECTED; + promise._result = reason; + + asap(publishRejection, promise); +} + +function subscribe(parent, child, onFulfillment, onRejection) { + var _subscribers = parent._subscribers; + var length = _subscribers.length; + + parent._onerror = null; + + _subscribers[length] = child; + _subscribers[length + FULFILLED] = onFulfillment; + _subscribers[length + REJECTED] = onRejection; + + if (length === 0 && parent._state) { + asap(publish, parent); + } +} + +function publish(promise) { + var subscribers = promise._subscribers; + var settled = promise._state; + + if (subscribers.length === 0) { + return; + } + + var child = undefined, + callback = undefined, + detail = promise._result; + + for (var i = 0; i < subscribers.length; i += 3) { + child = subscribers[i]; + callback = subscribers[i + settled]; + + if (child) { + invokeCallback(settled, child, callback, detail); + } else { + callback(detail); + } + } + + promise._subscribers.length = 0; +} + +function ErrorObject() { + this.error = null; +} + +var TRY_CATCH_ERROR = new ErrorObject(); + +function tryCatch(callback, detail) { + try { + return callback(detail); + } catch (e) { + TRY_CATCH_ERROR.error = e; + return TRY_CATCH_ERROR; + } +} + +function invokeCallback(settled, promise, callback, detail) { + var hasCallback = isFunction(callback), + value = undefined, + error = undefined, + succeeded = undefined, + failed = undefined; + + if (hasCallback) { + value = tryCatch(callback, detail); + + if (value === TRY_CATCH_ERROR) { + failed = true; + error = value.error; + value = null; + } else { + succeeded = true; + } + + if (promise === value) { + _reject(promise, cannotReturnOwn()); + return; + } + } else { + value = detail; + succeeded = true; + } + + if (promise._state !== PENDING) { + // noop + } else if (hasCallback && succeeded) { + _resolve(promise, value); + } else if (failed) { + _reject(promise, error); + } else if (settled === FULFILLED) { + fulfill(promise, value); + } else if (settled === REJECTED) { + _reject(promise, value); + } +} + +function initializePromise(promise, resolver) { + try { + resolver(function resolvePromise(value) { + _resolve(promise, value); + }, function rejectPromise(reason) { + _reject(promise, reason); + }); + } catch (e) { + _reject(promise, e); + } +} + +var id = 0; +function nextId() { + return id++; +} + +function makePromise(promise) { + promise[PROMISE_ID] = id++; + promise._state = undefined; + promise._result = undefined; + promise._subscribers = []; +} + +function Enumerator(Constructor, input) { + this._instanceConstructor = Constructor; + this.promise = new Constructor(noop); + + if (!this.promise[PROMISE_ID]) { + makePromise(this.promise); + } + + if (isArray(input)) { + this._input = input; + this.length = input.length; + this._remaining = input.length; + + this._result = new Array(this.length); + + if (this.length === 0) { + fulfill(this.promise, this._result); + } else { + this.length = this.length || 0; + this._enumerate(); + if (this._remaining === 0) { + fulfill(this.promise, this._result); + } + } + } else { + _reject(this.promise, validationError()); + } +} + +function validationError() { + return new Error('Array Methods must be provided an Array'); +}; + +Enumerator.prototype._enumerate = function () { + var length = this.length; + var _input = this._input; + + for (var i = 0; this._state === PENDING && i < length; i++) { + this._eachEntry(_input[i], i); + } +}; + +Enumerator.prototype._eachEntry = function (entry, i) { + var c = this._instanceConstructor; + var resolve$$ = c.resolve; + + if (resolve$$ === resolve) { + var _then = getThen(entry); + + if (_then === then && entry._state !== PENDING) { + this._settledAt(entry._state, i, entry._result); + } else if (typeof _then !== 'function') { + this._remaining--; + this._result[i] = entry; + } else if (c === Promise) { + var promise = new c(noop); + handleMaybeThenable(promise, entry, _then); + this._willSettleAt(promise, i); + } else { + this._willSettleAt(new c(function (resolve$$) { + return resolve$$(entry); + }), i); + } + } else { + this._willSettleAt(resolve$$(entry), i); + } +}; + +Enumerator.prototype._settledAt = function (state, i, value) { + var promise = this.promise; + + if (promise._state === PENDING) { + this._remaining--; + + if (state === REJECTED) { + _reject(promise, value); + } else { + this._result[i] = value; + } + } + + if (this._remaining === 0) { + fulfill(promise, this._result); + } +}; + +Enumerator.prototype._willSettleAt = function (promise, i) { + var enumerator = this; + + subscribe(promise, undefined, function (value) { + return enumerator._settledAt(FULFILLED, i, value); + }, function (reason) { + return enumerator._settledAt(REJECTED, i, reason); + }); +}; + +/** + `Promise.all` accepts an array of promises, and returns a new promise which + is fulfilled with an array of fulfillment values for the passed promises, or + rejected with the reason of the first passed promise to be rejected. It casts all + elements of the passed iterable to promises as it runs this algorithm. + + Example: + + ```javascript + let promise1 = resolve(1); + let promise2 = resolve(2); + let promise3 = resolve(3); + let promises = [ promise1, promise2, promise3 ]; + + Promise.all(promises).then(function(array){ + // The array here would be [ 1, 2, 3 ]; + }); + ``` + + If any of the `promises` given to `all` are rejected, the first promise + that is rejected will be given as an argument to the returned promises's + rejection handler. For example: + + Example: + + ```javascript + let promise1 = resolve(1); + let promise2 = reject(new Error("2")); + let promise3 = reject(new Error("3")); + let promises = [ promise1, promise2, promise3 ]; + + Promise.all(promises).then(function(array){ + // Code here never runs because there are rejected promises! + }, function(error) { + // error.message === "2" + }); + ``` + + @method all + @static + @param {Array} entries array of promises + @param {String} label optional string for labeling the promise. + Useful for tooling. + @return {Promise} promise that is fulfilled when all `promises` have been + fulfilled, or rejected if any of them become rejected. + @static +*/ +function all(entries) { + return new Enumerator(this, entries).promise; +} + +/** + `Promise.race` returns a new promise which is settled in the same way as the + first passed promise to settle. + + Example: + + ```javascript + let promise1 = new Promise(function(resolve, reject){ + setTimeout(function(){ + resolve('promise 1'); + }, 200); + }); + + let promise2 = new Promise(function(resolve, reject){ + setTimeout(function(){ + resolve('promise 2'); + }, 100); + }); + + Promise.race([promise1, promise2]).then(function(result){ + // result === 'promise 2' because it was resolved before promise1 + // was resolved. + }); + ``` + + `Promise.race` is deterministic in that only the state of the first + settled promise matters. For example, even if other promises given to the + `promises` array argument are resolved, but the first settled promise has + become rejected before the other promises became fulfilled, the returned + promise will become rejected: + + ```javascript + let promise1 = new Promise(function(resolve, reject){ + setTimeout(function(){ + resolve('promise 1'); + }, 200); + }); + + let promise2 = new Promise(function(resolve, reject){ + setTimeout(function(){ + reject(new Error('promise 2')); + }, 100); + }); + + Promise.race([promise1, promise2]).then(function(result){ + // Code here never runs + }, function(reason){ + // reason.message === 'promise 2' because promise 2 became rejected before + // promise 1 became fulfilled + }); + ``` + + An example real-world use case is implementing timeouts: + + ```javascript + Promise.race([ajax('foo.json'), timeout(5000)]) + ``` + + @method race + @static + @param {Array} promises array of promises to observe + Useful for tooling. + @return {Promise} a promise which settles in the same way as the first passed + promise to settle. +*/ +function race(entries) { + /*jshint validthis:true */ + var Constructor = this; + + if (!isArray(entries)) { + return new Constructor(function (_, reject) { + return reject(new TypeError('You must pass an array to race.')); + }); + } else { + return new Constructor(function (resolve, reject) { + var length = entries.length; + for (var i = 0; i < length; i++) { + Constructor.resolve(entries[i]).then(resolve, reject); + } + }); + } +} + +/** + `Promise.reject` returns a promise rejected with the passed `reason`. + It is shorthand for the following: + + ```javascript + let promise = new Promise(function(resolve, reject){ + reject(new Error('WHOOPS')); + }); + + promise.then(function(value){ + // Code here doesn't run because the promise is rejected! + }, function(reason){ + // reason.message === 'WHOOPS' + }); + ``` + + Instead of writing the above, your code now simply becomes the following: + + ```javascript + let promise = Promise.reject(new Error('WHOOPS')); + + promise.then(function(value){ + // Code here doesn't run because the promise is rejected! + }, function(reason){ + // reason.message === 'WHOOPS' + }); + ``` + + @method reject + @static + @param {Any} reason value that the returned promise will be rejected with. + Useful for tooling. + @return {Promise} a promise rejected with the given `reason`. +*/ +function reject(reason) { + /*jshint validthis:true */ + var Constructor = this; + var promise = new Constructor(noop); + _reject(promise, reason); + return promise; +} + +function needsResolver() { + throw new TypeError('You must pass a resolver function as the first argument to the promise constructor'); +} + +function needsNew() { + throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function."); +} + +/** + Promise objects represent the eventual result of an asynchronous operation. The + primary way of interacting with a promise is through its `then` method, which + registers callbacks to receive either a promise's eventual value or the reason + why the promise cannot be fulfilled. + + Terminology + ----------- + + - `promise` is an object or function with a `then` method whose behavior conforms to this specification. + - `thenable` is an object or function that defines a `then` method. + - `value` is any legal JavaScript value (including undefined, a thenable, or a promise). + - `exception` is a value that is thrown using the throw statement. + - `reason` is a value that indicates why a promise was rejected. + - `settled` the final resting state of a promise, fulfilled or rejected. + + A promise can be in one of three states: pending, fulfilled, or rejected. + + Promises that are fulfilled have a fulfillment value and are in the fulfilled + state. Promises that are rejected have a rejection reason and are in the + rejected state. A fulfillment value is never a thenable. + + Promises can also be said to *resolve* a value. If this value is also a + promise, then the original promise's settled state will match the value's + settled state. So a promise that *resolves* a promise that rejects will + itself reject, and a promise that *resolves* a promise that fulfills will + itself fulfill. + + + Basic Usage: + ------------ + + ```js + let promise = new Promise(function(resolve, reject) { + // on success + resolve(value); + + // on failure + reject(reason); + }); + + promise.then(function(value) { + // on fulfillment + }, function(reason) { + // on rejection + }); + ``` + + Advanced Usage: + --------------- + + Promises shine when abstracting away asynchronous interactions such as + `XMLHttpRequest`s. + + ```js + function getJSON(url) { + return new Promise(function(resolve, reject){ + let xhr = new XMLHttpRequest(); + + xhr.open('GET', url); + xhr.onreadystatechange = handler; + xhr.responseType = 'json'; + xhr.setRequestHeader('Accept', 'application/json'); + xhr.send(); + + function handler() { + if (this.readyState === this.DONE) { + if (this.status === 200) { + resolve(this.response); + } else { + reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']')); + } + } + }; + }); + } + + getJSON('/posts.json').then(function(json) { + // on fulfillment + }, function(reason) { + // on rejection + }); + ``` + + Unlike callbacks, promises are great composable primitives. + + ```js + Promise.all([ + getJSON('/posts'), + getJSON('/comments') + ]).then(function(values){ + values[0] // => postsJSON + values[1] // => commentsJSON + + return values; + }); + ``` + + @class Promise + @param {function} resolver + Useful for tooling. + @constructor +*/ +function Promise(resolver) { + this[PROMISE_ID] = nextId(); + this._result = this._state = undefined; + this._subscribers = []; + + if (noop !== resolver) { + typeof resolver !== 'function' && needsResolver(); + this instanceof Promise ? initializePromise(this, resolver) : needsNew(); + } +} + +Promise.all = all; +Promise.race = race; +Promise.resolve = resolve; +Promise.reject = reject; +Promise._setScheduler = setScheduler; +Promise._setAsap = setAsap; +Promise._asap = asap; + +Promise.prototype = { + constructor: Promise, + + /** + The primary way of interacting with a promise is through its `then` method, + which registers callbacks to receive either a promise's eventual value or the + reason why the promise cannot be fulfilled. + + ```js + findUser().then(function(user){ + // user is available + }, function(reason){ + // user is unavailable, and you are given the reason why + }); + ``` + + Chaining + -------- + + The return value of `then` is itself a promise. This second, 'downstream' + promise is resolved with the return value of the first promise's fulfillment + or rejection handler, or rejected if the handler throws an exception. + + ```js + findUser().then(function (user) { + return user.name; + }, function (reason) { + return 'default name'; + }).then(function (userName) { + // If `findUser` fulfilled, `userName` will be the user's name, otherwise it + // will be `'default name'` + }); + + findUser().then(function (user) { + throw new Error('Found user, but still unhappy'); + }, function (reason) { + throw new Error('`findUser` rejected and we're unhappy'); + }).then(function (value) { + // never reached + }, function (reason) { + // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'. + // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'. + }); + ``` + If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream. + + ```js + findUser().then(function (user) { + throw new PedagogicalException('Upstream error'); + }).then(function (value) { + // never reached + }).then(function (value) { + // never reached + }, function (reason) { + // The `PedgagocialException` is propagated all the way down to here + }); + ``` + + Assimilation + ------------ + + Sometimes the value you want to propagate to a downstream promise can only be + retrieved asynchronously. This can be achieved by returning a promise in the + fulfillment or rejection handler. The downstream promise will then be pending + until the returned promise is settled. This is called *assimilation*. + + ```js + findUser().then(function (user) { + return findCommentsByAuthor(user); + }).then(function (comments) { + // The user's comments are now available + }); + ``` + + If the assimliated promise rejects, then the downstream promise will also reject. + + ```js + findUser().then(function (user) { + return findCommentsByAuthor(user); + }).then(function (comments) { + // If `findCommentsByAuthor` fulfills, we'll have the value here + }, function (reason) { + // If `findCommentsByAuthor` rejects, we'll have the reason here + }); + ``` + + Simple Example + -------------- + + Synchronous Example + + ```javascript + let result; + + try { + result = findResult(); + // success + } catch(reason) { + // failure + } + ``` + + Errback Example + + ```js + findResult(function(result, err){ + if (err) { + // failure + } else { + // success + } + }); + ``` + + Promise Example; + + ```javascript + findResult().then(function(result){ + // success + }, function(reason){ + // failure + }); + ``` + + Advanced Example + -------------- + + Synchronous Example + + ```javascript + let author, books; + + try { + author = findAuthor(); + books = findBooksByAuthor(author); + // success + } catch(reason) { + // failure + } + ``` + + Errback Example + + ```js + + function foundBooks(books) { + + } + + function failure(reason) { + + } + + findAuthor(function(author, err){ + if (err) { + failure(err); + // failure + } else { + try { + findBoooksByAuthor(author, function(books, err) { + if (err) { + failure(err); + } else { + try { + foundBooks(books); + } catch(reason) { + failure(reason); + } + } + }); + } catch(error) { + failure(err); + } + // success + } + }); + ``` + + Promise Example; + + ```javascript + findAuthor(). + then(findBooksByAuthor). + then(function(books){ + // found books + }).catch(function(reason){ + // something went wrong + }); + ``` + + @method then + @param {Function} onFulfilled + @param {Function} onRejected + Useful for tooling. + @return {Promise} + */ + then: then, + + /** + `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same + as the catch block of a try/catch statement. + + ```js + function findAuthor(){ + throw new Error('couldn't find that author'); + } + + // synchronous + try { + findAuthor(); + } catch(reason) { + // something went wrong + } + + // async with promises + findAuthor().catch(function(reason){ + // something went wrong + }); + ``` + + @method catch + @param {Function} onRejection + Useful for tooling. + @return {Promise} + */ + 'catch': function _catch(onRejection) { + return this.then(null, onRejection); + } +}; + +function polyfill() { + var local = undefined; + + if (typeof global !== 'undefined') { + local = global; + } else if (typeof self !== 'undefined') { + local = self; + } else { + try { + local = Function('return this')(); + } catch (e) { + throw new Error('polyfill failed because global object is unavailable in this environment'); + } + } + + var P = local.Promise; + + if (P) { + var promiseToString = null; + try { + promiseToString = Object.prototype.toString.call(P.resolve()); + } catch (e) { + // silently ignored + } + + if (promiseToString === '[object Promise]' && !P.cast) { + return; + } + } + + local.Promise = Promise; +} + +polyfill(); +// Strange compat.. +Promise.polyfill = polyfill; +Promise.Promise = Promise; + +return Promise; + +}))); + +}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"_process":923}],499:[function(require,module,exports){ +/** +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -113723,9 +99395,9 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op return annOut; }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"../color":1161,"./attributes":1152}],1151:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"../color":510,"./attributes":501}],500:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -113785,9 +99457,9 @@ module.exports = [ } ]; -},{}],1152:[function(require,module,exports){ +},{}],501:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -114099,9 +99771,9 @@ module.exports = { } }; -},{"../../lib/extend":1254,"../../plots/cartesian/constants":1298,"../../plots/font_attributes":1313,"./arrow_paths":1151}],1153:[function(require,module,exports){ +},{"../../lib/extend":603,"../../plots/cartesian/constants":647,"../../plots/font_attributes":662,"./arrow_paths":500}],502:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -114186,9 +99858,9 @@ function annAutorange(gd) { }); } -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"./draw":1155}],1154:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"./draw":504}],503:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -114211,9 +99883,9 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) { handleArrayContainerDefaults(layoutIn, layoutOut, opts); }; -},{"../../plots/array_container_defaults":1290,"./annotation_defaults":1150}],1155:[function(require,module,exports){ +},{"../../plots/array_container_defaults":639,"./annotation_defaults":499}],504:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -114971,9 +100643,9 @@ function lineIntersect(x1, y1, x2, y2, x3, y3, x4, y4) { return {x: x1 + a * t, y: y1 + d * t}; } -},{"../../lib":1261,"../../lib/setcursor":1272,"../../lib/svg_text_utils":1276,"../../plotly":1288,"../../plots/cartesian/axes":1293,"../../plots/plots":1353,"../color":1161,"../dragelement":1182,"../drawing":1184,"./annotation_defaults":1150,"./defaults":1154,"./draw_arrow_head":1156,"d3":173,"fast-isnumeric":181}],1156:[function(require,module,exports){ +},{"../../lib":610,"../../lib/setcursor":621,"../../lib/svg_text_utils":625,"../../plotly":637,"../../plots/cartesian/axes":642,"../../plots/plots":702,"../color":510,"../dragelement":531,"../drawing":533,"./annotation_defaults":499,"./defaults":503,"./draw_arrow_head":505,"d3":86,"fast-isnumeric":97}],505:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115090,9 +100762,9 @@ module.exports = function drawArrowHead(el3, style, ends, mag) { if(doEnd) drawhead(end, endRot); }; -},{"../color":1161,"../drawing":1184,"./arrow_paths":1151,"d3":173,"fast-isnumeric":181}],1157:[function(require,module,exports){ +},{"../color":510,"../drawing":533,"./arrow_paths":500,"d3":86,"fast-isnumeric":97}],506:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115116,9 +100788,9 @@ module.exports = { drawOne: drawModule.drawOne }; -},{"./attributes":1152,"./calc_autorange":1153,"./defaults":1154,"./draw":1155}],1158:[function(require,module,exports){ +},{"./attributes":501,"./calc_autorange":502,"./defaults":503,"./draw":504}],507:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115149,9 +100821,9 @@ require('world-calendars/dist/calendars/taiwan'); require('world-calendars/dist/calendars/thai'); require('world-calendars/dist/calendars/ummalqura'); -},{"world-calendars/dist/calendars/chinese":1133,"world-calendars/dist/calendars/coptic":1134,"world-calendars/dist/calendars/discworld":1135,"world-calendars/dist/calendars/ethiopian":1136,"world-calendars/dist/calendars/hebrew":1137,"world-calendars/dist/calendars/islamic":1138,"world-calendars/dist/calendars/julian":1139,"world-calendars/dist/calendars/mayan":1140,"world-calendars/dist/calendars/nanakshahi":1141,"world-calendars/dist/calendars/nepali":1142,"world-calendars/dist/calendars/persian":1143,"world-calendars/dist/calendars/taiwan":1144,"world-calendars/dist/calendars/thai":1145,"world-calendars/dist/calendars/ummalqura":1146,"world-calendars/dist/main":1147,"world-calendars/dist/plus":1148}],1159:[function(require,module,exports){ +},{"world-calendars/dist/calendars/chinese":1019,"world-calendars/dist/calendars/coptic":1020,"world-calendars/dist/calendars/discworld":1021,"world-calendars/dist/calendars/ethiopian":1022,"world-calendars/dist/calendars/hebrew":1023,"world-calendars/dist/calendars/islamic":1024,"world-calendars/dist/calendars/julian":1025,"world-calendars/dist/calendars/mayan":1026,"world-calendars/dist/calendars/nanakshahi":1027,"world-calendars/dist/calendars/nepali":1028,"world-calendars/dist/calendars/persian":1029,"world-calendars/dist/calendars/taiwan":1030,"world-calendars/dist/calendars/thai":1031,"world-calendars/dist/calendars/ummalqura":1032,"world-calendars/dist/main":1033,"world-calendars/dist/plus":1034}],508:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115408,9 +101080,9 @@ module.exports = { worldCalFmt: worldCalFmt }; -},{"../../constants/numerical":1244,"../../lib":1261,"./calendars":1158}],1160:[function(require,module,exports){ +},{"../../constants/numerical":593,"../../lib":610,"./calendars":507}],509:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115448,9 +101120,9 @@ exports.borderLine = '#BEC8D9'; // gives back exactly lightLine if the other colors are defaults. exports.lightFraction = 100 * (0xe - 0x4) / (0xf - 0x4); -},{}],1161:[function(require,module,exports){ +},{}],510:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115605,9 +101277,9 @@ function cleanOne(val) { return 'rgb(' + rgbStr + ')'; } -},{"./attributes":1160,"fast-isnumeric":181,"tinycolor2":1129}],1162:[function(require,module,exports){ +},{"./attributes":509,"fast-isnumeric":97,"tinycolor2":985}],511:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115801,9 +101473,9 @@ module.exports = { } }; -},{"../../lib/extend":1254,"../../plots/cartesian/layout_attributes":1302,"../../plots/font_attributes":1313}],1163:[function(require,module,exports){ +},{"../../lib/extend":603,"../../plots/cartesian/layout_attributes":651,"../../plots/font_attributes":662}],512:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -115868,9 +101540,9 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) { coerce('titleside'); }; -},{"../../lib":1261,"../../plots/cartesian/tick_label_defaults":1308,"../../plots/cartesian/tick_mark_defaults":1309,"../../plots/cartesian/tick_value_defaults":1310,"./attributes":1162}],1164:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/tick_label_defaults":657,"../../plots/cartesian/tick_mark_defaults":658,"../../plots/cartesian/tick_value_defaults":659,"./attributes":511}],513:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116501,9 +102173,9 @@ module.exports = function draw(gd, id) { return component; }; -},{"../../lib":1261,"../../lib/extend":1254,"../../lib/setcursor":1272,"../../plotly":1288,"../../plots/cartesian/axes":1293,"../../plots/cartesian/axis_defaults":1295,"../../plots/cartesian/layout_attributes":1302,"../../plots/cartesian/position_defaults":1305,"../../plots/plots":1353,"../../registry":1368,"../color":1161,"../dragelement":1182,"../drawing":1184,"../titles":1235,"./attributes":1162,"d3":173,"tinycolor2":1129}],1165:[function(require,module,exports){ +},{"../../lib":610,"../../lib/extend":603,"../../lib/setcursor":621,"../../plotly":637,"../../plots/cartesian/axes":642,"../../plots/cartesian/axis_defaults":644,"../../plots/cartesian/layout_attributes":651,"../../plots/cartesian/position_defaults":654,"../../plots/plots":702,"../../registry":717,"../color":510,"../dragelement":531,"../drawing":533,"../titles":584,"./attributes":511,"d3":86,"tinycolor2":985}],514:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116520,9 +102192,9 @@ module.exports = function hasColorbar(container) { return Lib.isPlainObject(container.colorbar); }; -},{"../../lib":1261}],1166:[function(require,module,exports){ +},{"../../lib":610}],515:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116593,9 +102265,9 @@ module.exports = { } }; -},{}],1167:[function(require,module,exports){ +},{}],516:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116659,9 +102331,9 @@ module.exports = function calc(trace, vals, containerStr, cLetter) { } }; -},{"../../lib":1261,"./flip_scale":1172,"./scales":1179}],1168:[function(require,module,exports){ +},{"../../lib":610,"./flip_scale":521,"./scales":528}],517:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116749,9 +102421,9 @@ module.exports = function makeColorScaleAttributes(context) { }; }; -},{"../../lib/extend":1254,"./attributes":1166,"./scales.js":1179}],1169:[function(require,module,exports){ +},{"../../lib/extend":603,"./attributes":515,"./scales.js":528}],518:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116765,9 +102437,9 @@ var scales = require('./scales'); module.exports = scales.RdBu; -},{"./scales":1179}],1170:[function(require,module,exports){ +},{"./scales":528}],519:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116829,9 +102501,9 @@ module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, if(showScale) colorbarDefaults(containerIn, containerOut, layout); }; -},{"../../lib":1261,"../colorbar/defaults":1163,"../colorbar/has_colorbar":1165,"./flip_scale":1172,"./is_valid_scale":1176,"fast-isnumeric":181}],1171:[function(require,module,exports){ +},{"../../lib":610,"../colorbar/defaults":512,"../colorbar/has_colorbar":514,"./flip_scale":521,"./is_valid_scale":525,"fast-isnumeric":97}],520:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116866,9 +102538,9 @@ module.exports = function extractScale(scl, cmin, cmax) { }; }; -},{}],1172:[function(require,module,exports){ +},{}],521:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116891,9 +102563,9 @@ module.exports = function flipScale(scl) { return sclNew; }; -},{}],1173:[function(require,module,exports){ +},{}],522:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116931,9 +102603,9 @@ module.exports = function getScale(scl, dflt) { return scl; }; -},{"./default_scale":1169,"./is_valid_scale_array":1177,"./scales":1179}],1174:[function(require,module,exports){ +},{"./default_scale":518,"./is_valid_scale_array":526,"./scales":528}],523:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -116977,9 +102649,9 @@ module.exports = function hasColorscale(trace, containerStr) { ); }; -},{"../../lib":1261,"./is_valid_scale":1176,"fast-isnumeric":181}],1175:[function(require,module,exports){ +},{"../../lib":610,"./is_valid_scale":525,"fast-isnumeric":97}],524:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117011,9 +102683,9 @@ exports.extractScale = require('./extract_scale'); exports.makeColorScaleFunc = require('./make_color_scale_func'); -},{"./attributes":1166,"./calc":1167,"./default_scale":1169,"./defaults":1170,"./extract_scale":1171,"./flip_scale":1172,"./get_scale":1173,"./has_colorscale":1174,"./is_valid_scale":1176,"./make_color_scale_func":1178,"./scales":1179}],1176:[function(require,module,exports){ +},{"./attributes":515,"./calc":516,"./default_scale":518,"./defaults":519,"./extract_scale":520,"./flip_scale":521,"./get_scale":522,"./has_colorscale":523,"./is_valid_scale":525,"./make_color_scale_func":527,"./scales":528}],525:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117032,9 +102704,9 @@ module.exports = function isValidScale(scl) { else return isValidScaleArray(scl); }; -},{"./is_valid_scale_array":1177,"./scales":1179}],1177:[function(require,module,exports){ +},{"./is_valid_scale_array":526,"./scales":528}],526:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117069,9 +102741,9 @@ module.exports = function isValidScaleArray(scl) { return true; }; -},{"tinycolor2":1129}],1178:[function(require,module,exports){ +},{"tinycolor2":985}],527:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117165,9 +102837,9 @@ function colorArray2rbga(colorArray) { return tinycolor(colorObj).toRgbString(); } -},{"../color":1161,"d3":173,"fast-isnumeric":181,"tinycolor2":1129}],1179:[function(require,module,exports){ +},{"../color":510,"d3":86,"fast-isnumeric":97,"tinycolor2":985}],528:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117296,9 +102968,9 @@ module.exports = { ] }; -},{}],1180:[function(require,module,exports){ +},{}],529:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117329,9 +103001,9 @@ module.exports = function align(v, dv, v0, v1, anchor) { return vc; }; -},{}],1181:[function(require,module,exports){ +},{}],530:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117367,9 +103039,9 @@ module.exports = function getCursor(x, y, xanchor, yanchor) { return cursorset[y][x]; }; -},{"../../lib":1261}],1182:[function(require,module,exports){ +},{"../../lib":610}],531:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117554,9 +103226,9 @@ function finishDrag(gd) { if(gd._replotPending) Plotly.plot(gd); } -},{"../../lib":1261,"../../plotly":1288,"../../plots/cartesian/constants":1298,"./align":1180,"./cursor":1181,"./unhover":1183}],1183:[function(require,module,exports){ +},{"../../lib":610,"../../plotly":637,"../../plots/cartesian/constants":647,"./align":529,"./cursor":530,"./unhover":532}],532:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -117605,9 +103277,9 @@ unhover.raw = function unhoverRaw(gd, evt) { gd._hoverdata = undefined; }; -},{"../../lib/events":1253}],1184:[function(require,module,exports){ +},{"../../lib/events":602}],533:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -118184,9 +103856,9 @@ drawing.setClipUrl = function(s, localId) { s.attr('clip-path', 'url(' + url + ')'); }; -},{"../../constants/xmlns_namespaces":1246,"../../lib":1261,"../../lib/svg_text_utils":1276,"../../registry":1368,"../../traces/scatter/make_bubble_size_func":1500,"../../traces/scatter/subtypes":1505,"../color":1161,"../colorscale":1175,"./symbol_defs":1185,"d3":173,"fast-isnumeric":181}],1185:[function(require,module,exports){ +},{"../../constants/xmlns_namespaces":595,"../../lib":610,"../../lib/svg_text_utils":625,"../../registry":717,"../../traces/scatter/make_bubble_size_func":849,"../../traces/scatter/subtypes":854,"../color":510,"../colorscale":524,"./symbol_defs":534,"d3":86,"fast-isnumeric":97}],534:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -118660,9 +104332,9 @@ module.exports = { } }; -},{"d3":173}],1186:[function(require,module,exports){ +},{"d3":86}],535:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -118802,9 +104474,9 @@ module.exports = { } }; -},{}],1187:[function(require,module,exports){ +},{}],536:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -118865,9 +104537,9 @@ function calcOneAxis(calcTrace, trace, axis, coord) { Axes.expand(axis, vals, {padded: true}); } -},{"../../plots/cartesian/axes":1293,"../../registry":1368,"./compute_error":1188,"fast-isnumeric":181}],1188:[function(require,module,exports){ +},{"../../plots/cartesian/axes":642,"../../registry":717,"./compute_error":537,"fast-isnumeric":97}],537:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -118961,9 +104633,9 @@ function makeComputeErrorValue(type, value) { } } -},{}],1189:[function(require,module,exports){ +},{}],538:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119038,9 +104710,9 @@ module.exports = function(traceIn, traceOut, defaultColor, opts) { } }; -},{"../../lib":1261,"../../registry":1368,"./attributes":1186,"fast-isnumeric":181}],1190:[function(require,module,exports){ +},{"../../lib":610,"../../registry":717,"./attributes":535,"fast-isnumeric":97}],539:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119097,9 +104769,9 @@ errorBars.hoverInfo = function(calcPoint, trace, hoverPoint) { } }; -},{"./attributes":1186,"./calc":1187,"./defaults":1189,"./plot":1191,"./style":1192}],1191:[function(require,module,exports){ +},{"./attributes":535,"./calc":536,"./defaults":538,"./plot":540,"./style":541}],540:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119261,9 +104933,9 @@ function errorCoords(d, xa, ya) { return out; } -},{"../../traces/scatter/subtypes":1505,"d3":173,"fast-isnumeric":181}],1192:[function(require,module,exports){ +},{"../../traces/scatter/subtypes":854,"d3":86,"fast-isnumeric":97}],541:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119298,9 +104970,9 @@ module.exports = function style(traces) { }); }; -},{"../color":1161,"d3":173}],1193:[function(require,module,exports){ +},{"../color":510,"d3":86}],542:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119467,9 +105139,9 @@ module.exports = { } }; -},{"../../plots/cartesian/constants":1298}],1194:[function(require,module,exports){ +},{"../../plots/cartesian/constants":647}],543:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119527,9 +105199,9 @@ function imageDefaults(imageIn, imageOut, fullLayout) { return imageOut; } -},{"../../lib":1261,"../../plots/array_container_defaults":1290,"../../plots/cartesian/axes":1293,"./attributes":1193}],1195:[function(require,module,exports){ +},{"../../lib":610,"../../plots/array_container_defaults":639,"../../plots/cartesian/axes":642,"./attributes":542}],544:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119707,9 +105379,9 @@ module.exports = function draw(gd) { }); }; -},{"../../constants/xmlns_namespaces":1246,"../../plots/cartesian/axes":1293,"../drawing":1184,"d3":173}],1196:[function(require,module,exports){ +},{"../../constants/xmlns_namespaces":595,"../../plots/cartesian/axes":642,"../drawing":533,"d3":86}],545:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119728,9 +105400,9 @@ module.exports = { draw: require('./draw') }; -},{"./attributes":1193,"./defaults":1194,"./draw":1195}],1197:[function(require,module,exports){ +},{"./attributes":542,"./defaults":543,"./draw":544}],546:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119777,9 +105449,9 @@ exports.isMiddleAnchor = function isMiddleAnchor(opts) { ); }; -},{}],1198:[function(require,module,exports){ +},{}],547:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119892,9 +105564,9 @@ module.exports = { } }; -},{"../../lib/extend":1254,"../../plots/font_attributes":1313,"../color/attributes":1160}],1199:[function(require,module,exports){ +},{"../../lib/extend":603,"../../plots/font_attributes":662,"../color/attributes":509}],548:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -119910,9 +105582,9 @@ module.exports = { scrollBarMargin: 4 }; -},{}],1200:[function(require,module,exports){ +},{}],549:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -120003,9 +105675,9 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) { Lib.noneOrAll(containerIn, containerOut, ['x', 'y']); }; -},{"../../lib":1261,"../../plots/layout_attributes":1344,"../../registry":1368,"./attributes":1198,"./helpers":1203}],1201:[function(require,module,exports){ +},{"../../lib":610,"../../plots/layout_attributes":693,"../../registry":717,"./attributes":547,"./helpers":552}],550:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -120719,9 +106391,9 @@ function expandHorizontalMargin(gd) { }); } -},{"../../lib":1261,"../../lib/svg_text_utils":1276,"../../plotly":1288,"../../plots/plots":1353,"../../registry":1368,"../color":1161,"../dragelement":1182,"../drawing":1184,"./anchor_utils":1197,"./constants":1199,"./get_legend_data":1202,"./helpers":1203,"./style":1205,"d3":173}],1202:[function(require,module,exports){ +},{"../../lib":610,"../../lib/svg_text_utils":625,"../../plotly":637,"../../plots/plots":702,"../../registry":717,"../color":510,"../dragelement":531,"../drawing":533,"./anchor_utils":546,"./constants":548,"./get_legend_data":551,"./helpers":552,"./style":554,"d3":86}],551:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -120824,9 +106496,9 @@ module.exports = function getLegendData(calcdata, opts) { return legendData; }; -},{"../../registry":1368,"./helpers":1203}],1203:[function(require,module,exports){ +},{"../../registry":717,"./helpers":552}],552:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -120855,9 +106527,9 @@ exports.isReversed = function isReversed(legendLayout) { return (legendLayout.traceorder || '').indexOf('reversed') !== -1; }; -},{"../../registry":1368}],1204:[function(require,module,exports){ +},{"../../registry":717}],553:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -120879,9 +106551,9 @@ module.exports = { style: require('./style') }; -},{"./attributes":1198,"./defaults":1200,"./draw":1201,"./style":1205}],1205:[function(require,module,exports){ +},{"./attributes":547,"./defaults":549,"./draw":550,"./style":554}],554:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -121101,9 +106773,9 @@ function stylePies(d) { if(pts.size()) pts.call(stylePie, d[0], trace); } -},{"../../lib":1261,"../../registry":1368,"../../traces/pie/style_one":1479,"../../traces/scatter/subtypes":1505,"../color":1161,"../drawing":1184,"d3":173}],1206:[function(require,module,exports){ +},{"../../lib":610,"../../registry":717,"../../traces/pie/style_one":828,"../../traces/scatter/subtypes":854,"../color":510,"../drawing":533,"d3":86}],555:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -121620,9 +107292,9 @@ modeBarButtons.resetViews = { } }; -},{"../../../build/ploticon":91,"../../lib":1261,"../../plotly":1288,"../../plots/cartesian/axes":1293,"../../plots/plots":1353,"../../snapshot/download":1370}],1207:[function(require,module,exports){ +},{"../../../build/ploticon":472,"../../lib":610,"../../plotly":637,"../../plots/cartesian/axes":642,"../../plots/plots":702,"../../snapshot/download":719}],556:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -121634,9 +107306,9 @@ modeBarButtons.resetViews = { exports.manage = require('./manage'); -},{"./manage":1208}],1208:[function(require,module,exports){ +},{"./manage":557}],557:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -121862,9 +107534,9 @@ function fillCustomButton(customButtons) { return customButtons; } -},{"../../plots/cartesian/axes":1293,"../../traces/scatter/subtypes":1505,"./buttons":1206,"./modebar":1209}],1209:[function(require,module,exports){ +},{"../../plots/cartesian/axes":642,"../../traces/scatter/subtypes":854,"./buttons":555,"./modebar":558}],558:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122152,9 +107824,9 @@ function createModeBar(gd, buttons) { module.exports = createModeBar; -},{"../../../build/ploticon":91,"../../lib":1261,"d3":173}],1210:[function(require,module,exports){ +},{"../../../build/ploticon":472,"../../lib":610,"d3":86}],559:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122257,9 +107929,9 @@ module.exports = { } }; -},{"../../lib/extend":1254,"../../plots/font_attributes":1313,"../color/attributes":1160,"./button_attributes":1211}],1211:[function(require,module,exports){ +},{"../../lib/extend":603,"../../plots/font_attributes":662,"../color/attributes":509,"./button_attributes":560}],560:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122315,9 +107987,9 @@ module.exports = { } }; -},{}],1212:[function(require,module,exports){ +},{}],561:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122344,9 +108016,9 @@ module.exports = { darkAmount: 10 }; -},{}],1213:[function(require,module,exports){ +},{}],562:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122443,9 +108115,9 @@ function getPosDflt(containerOut, layout, counterAxes) { return [containerOut.domain[0], posY + constants.yPad]; } -},{"../../lib":1261,"../color":1161,"./attributes":1210,"./button_attributes":1211,"./constants":1212}],1214:[function(require,module,exports){ +},{"../../lib":610,"../color":510,"./attributes":559,"./button_attributes":560,"./constants":561}],563:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122718,9 +108390,9 @@ function reposition(gd, buttons, opts, axName) { }); } -},{"../../lib/svg_text_utils":1276,"../../plotly":1288,"../../plots/cartesian/axis_ids":1296,"../../plots/plots":1353,"../color":1161,"../drawing":1184,"../legend/anchor_utils":1197,"./constants":1212,"./get_update_object":1215,"d3":173}],1215:[function(require,module,exports){ +},{"../../lib/svg_text_utils":625,"../../plotly":637,"../../plots/cartesian/axis_ids":645,"../../plots/plots":702,"../color":510,"../drawing":533,"../legend/anchor_utils":546,"./constants":561,"./get_update_object":564,"d3":86}],564:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122775,9 +108447,9 @@ function getXRange(axisLayout, buttonLayout) { return [range0, range1]; } -},{"d3":173}],1216:[function(require,module,exports){ +},{"d3":86}],565:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122802,9 +108474,9 @@ module.exports = { draw: require('./draw') }; -},{"./attributes":1210,"./defaults":1213,"./draw":1214}],1217:[function(require,module,exports){ +},{"./attributes":559,"./defaults":562,"./draw":563}],566:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122877,9 +108549,9 @@ module.exports = { } }; -},{"../color/attributes":1160}],1218:[function(require,module,exports){ +},{"../color/attributes":509}],567:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122930,9 +108602,9 @@ module.exports = { handleStroke: '#666', }; -},{}],1219:[function(require,module,exports){ +},{}],568:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -122992,9 +108664,9 @@ module.exports = function handleDefaults(layoutIn, layoutOut, axName, counterAxe containerOut._input = containerIn; }; -},{"../../lib":1261,"./attributes":1217}],1220:[function(require,module,exports){ +},{"../../lib":610,"./attributes":566}],569:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -123519,9 +109191,9 @@ function clearPushMargins(gd) { } } -},{"../../lib":1261,"../../lib/setcursor":1272,"../../plotly":1288,"../../plots/cartesian":1301,"../../plots/cartesian/axes":1293,"../../plots/plots":1353,"../color":1161,"../dragelement":1182,"../drawing":1184,"./constants":1218,"d3":173}],1221:[function(require,module,exports){ +},{"../../lib":610,"../../lib/setcursor":621,"../../plotly":637,"../../plots/cartesian":650,"../../plots/cartesian/axes":642,"../../plots/plots":702,"../color":510,"../dragelement":531,"../drawing":533,"./constants":567,"d3":86}],570:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -123546,9 +109218,9 @@ module.exports = { draw: require('./draw') }; -},{"./attributes":1217,"./defaults":1219,"./draw":1220}],1222:[function(require,module,exports){ +},{"./attributes":566,"./defaults":568,"./draw":569}],571:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -123713,9 +109385,9 @@ module.exports = { } }; -},{"../../lib/extend":1254,"../../traces/scatter/attributes":1485,"../annotations/attributes":1152}],1223:[function(require,module,exports){ +},{"../../lib/extend":603,"../../traces/scatter/attributes":834,"../annotations/attributes":501}],572:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -123790,9 +109462,9 @@ function shapeBounds(ax, v0, v1, path, paramsToUse) { if(max >= min) return [min, max]; } -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"./constants":1224,"./helpers":1227}],1224:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"./constants":573,"./helpers":576}],573:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -123854,9 +109526,9 @@ module.exports = { } }; -},{}],1225:[function(require,module,exports){ +},{}],574:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -123879,9 +109551,9 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) { handleArrayContainerDefaults(layoutIn, layoutOut, opts); }; -},{"../../plots/array_container_defaults":1290,"./shape_defaults":1229}],1226:[function(require,module,exports){ +},{"../../plots/array_container_defaults":639,"./shape_defaults":578}],575:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -124446,9 +110118,9 @@ function movePath(pathIn, moveX, moveY) { }); } -},{"../../lib":1261,"../../lib/setcursor":1272,"../../plotly":1288,"../../plots/cartesian/axes":1293,"../color":1161,"../dragelement":1182,"../drawing":1184,"./constants":1224,"./defaults":1225,"./helpers":1227,"./shape_defaults":1229,"fast-isnumeric":181}],1227:[function(require,module,exports){ +},{"../../lib":610,"../../lib/setcursor":621,"../../plotly":637,"../../plots/cartesian/axes":642,"../color":510,"../dragelement":531,"../drawing":533,"./constants":573,"./defaults":574,"./helpers":576,"./shape_defaults":578,"fast-isnumeric":97}],576:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -124527,9 +110199,9 @@ exports.getPixelToData = function(gd, axis, isVertical) { return pixelToData; }; -},{}],1228:[function(require,module,exports){ +},{}],577:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -124553,9 +110225,9 @@ module.exports = { drawOne: drawModule.drawOne }; -},{"./attributes":1222,"./calc_autorange":1223,"./defaults":1225,"./draw":1226}],1229:[function(require,module,exports){ +},{"./attributes":571,"./calc_autorange":572,"./defaults":574,"./draw":575}],578:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -124652,9 +110324,9 @@ module.exports = function handleShapeDefaults(shapeIn, shapeOut, fullLayout, opt return shapeOut; }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"./attributes":1222,"./helpers":1227}],1230:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"./attributes":571,"./helpers":576}],579:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -124926,9 +110598,9 @@ module.exports = { }, }; -},{"../../lib/extend":1254,"../../plots/animation_attributes":1289,"../../plots/font_attributes":1313,"../../plots/pad_attributes":1352,"./constants":1231}],1231:[function(require,module,exports){ +},{"../../lib/extend":603,"../../plots/animation_attributes":638,"../../plots/font_attributes":662,"../../plots/pad_attributes":701,"./constants":580}],580:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -125023,9 +110695,9 @@ module.exports = { currentValueInset: 0, }; -},{}],1232:[function(require,module,exports){ +},{}],581:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -125136,9 +110808,9 @@ function stepsDefaults(sliderIn, sliderOut) { return valuesOut; } -},{"../../lib":1261,"../../plots/array_container_defaults":1290,"./attributes":1230,"./constants":1231}],1233:[function(require,module,exports){ +},{"../../lib":610,"../../plots/array_container_defaults":639,"./attributes":579,"./constants":580}],582:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -125739,9 +111411,9 @@ function clearPushMargins(gd) { } } -},{"../../lib":1261,"../../lib/svg_text_utils":1276,"../../plots/plots":1353,"../color":1161,"../drawing":1184,"../legend/anchor_utils":1197,"./constants":1231,"d3":173}],1234:[function(require,module,exports){ +},{"../../lib":610,"../../lib/svg_text_utils":625,"../../plots/plots":702,"../color":510,"../drawing":533,"../legend/anchor_utils":546,"./constants":580,"d3":86}],583:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -125762,9 +111434,9 @@ module.exports = { draw: require('./draw') }; -},{"./attributes":1230,"./constants":1231,"./defaults":1232,"./draw":1233}],1235:[function(require,module,exports){ +},{"./attributes":579,"./constants":580,"./defaults":581,"./draw":582}],584:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -125992,9 +111664,9 @@ Titles.draw = function(gd, titleClass, options) { el.classed('js-placeholder', isplaceholder); }; -},{"../../lib":1261,"../../lib/svg_text_utils":1276,"../../plotly":1288,"../../plots/plots":1353,"../color":1161,"../drawing":1184,"d3":173,"fast-isnumeric":181}],1236:[function(require,module,exports){ +},{"../../lib":610,"../../lib/svg_text_utils":625,"../../plotly":637,"../../plots/plots":702,"../color":510,"../drawing":533,"d3":86,"fast-isnumeric":97}],585:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -126164,9 +111836,9 @@ module.exports = { } }; -},{"../../lib/extend":1254,"../../plots/font_attributes":1313,"../../plots/pad_attributes":1352,"../color/attributes":1160}],1237:[function(require,module,exports){ +},{"../../lib/extend":603,"../../plots/font_attributes":662,"../../plots/pad_attributes":701,"../color/attributes":509}],586:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -126240,9 +111912,9 @@ module.exports = { hoverColor: '#F4FAFF' }; -},{}],1238:[function(require,module,exports){ +},{}],587:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -126334,9 +112006,9 @@ function buttonsDefaults(menuIn, menuOut) { return buttonsOut; } -},{"../../lib":1261,"../../plots/array_container_defaults":1290,"./attributes":1236,"./constants":1237}],1239:[function(require,module,exports){ +},{"../../lib":610,"../../plots/array_container_defaults":639,"./attributes":585,"./constants":586}],588:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -126917,11 +112589,11 @@ function clearPushMargins(gd) { } } -},{"../../lib":1261,"../../lib/svg_text_utils":1276,"../../plots/plots":1353,"../color":1161,"../drawing":1184,"../legend/anchor_utils":1197,"./constants":1237,"d3":173}],1240:[function(require,module,exports){ -arguments[4][1234][0].apply(exports,arguments) -},{"./attributes":1236,"./constants":1237,"./defaults":1238,"./draw":1239,"dup":1234}],1241:[function(require,module,exports){ +},{"../../lib":610,"../../lib/svg_text_utils":625,"../../plots/plots":702,"../color":510,"../drawing":533,"../legend/anchor_utils":546,"./constants":586,"d3":86}],589:[function(require,module,exports){ +arguments[4][583][0].apply(exports,arguments) +},{"./attributes":585,"./constants":586,"./defaults":587,"./draw":588,"dup":583}],590:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -126940,9 +112612,9 @@ module.exports = { longdashdot: [8, 1, 1, 1] }; -},{}],1242:[function(require,module,exports){ +},{}],591:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -126961,9 +112633,9 @@ module.exports = { longdashdot: [[0.5, 0.7, 0.8, 1], 10] }; -},{}],1243:[function(require,module,exports){ +},{}],592:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -126984,9 +112656,9 @@ module.exports = { x: '❌' }; -},{}],1244:[function(require,module,exports){ +},{}],593:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127032,9 +112704,9 @@ module.exports = { EPOCHJD: 2440587.5 }; -},{}],1245:[function(require,module,exports){ +},{}],594:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127070,9 +112742,9 @@ module.exports = { }; -},{}],1246:[function(require,module,exports){ +},{}],595:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127094,9 +112766,9 @@ exports.svgAttrs = { 'xmlns:xlink': exports.xlink }; -},{}],1247:[function(require,module,exports){ +},{}],596:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127112,7 +112784,7 @@ exports.svgAttrs = { var Plotly = require('./plotly'); // package version injected by `npm run preprocess` -exports.version = '1.21.2'; +exports.version = '1.21.3'; // inject promise polyfill require('es6-promise').polyfill(); @@ -127173,9 +112845,9 @@ exports.Queue = require('./lib/queue'); // export d3 used in the bundle exports.d3 = require('d3'); -},{"../build/plotcss":90,"../build/ploticon":91,"./components/annotations":1157,"./components/images":1196,"./components/legend":1204,"./components/rangeselector":1216,"./components/rangeslider":1221,"./components/shapes":1228,"./components/sliders":1234,"./components/updatemenus":1240,"./fonts/mathjax_config":1248,"./lib/queue":1270,"./plot_api/plot_schema":1282,"./plot_api/register":1283,"./plot_api/set_plot_config":1284,"./plot_api/to_image":1286,"./plot_api/validate":1287,"./plotly":1288,"./snapshot":1373,"./snapshot/download":1370,"./traces/scatter":1495,"d3":173,"es6-promise":180}],1248:[function(require,module,exports){ +},{"../build/plotcss":471,"../build/ploticon":472,"./components/annotations":506,"./components/images":545,"./components/legend":553,"./components/rangeselector":565,"./components/rangeslider":570,"./components/shapes":577,"./components/sliders":583,"./components/updatemenus":589,"./fonts/mathjax_config":597,"./lib/queue":619,"./plot_api/plot_schema":631,"./plot_api/register":632,"./plot_api/set_plot_config":633,"./plot_api/to_image":635,"./plot_api/validate":636,"./plotly":637,"./snapshot":722,"./snapshot/download":719,"./traces/scatter":844,"d3":86,"es6-promise":498}],597:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127206,9 +112878,9 @@ if(typeof MathJax !== 'undefined') { exports.MathJax = false; } -},{}],1249:[function(require,module,exports){ +},{}],598:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127223,9 +112895,9 @@ module.exports = function arrayToCalcItem(traceAttr, calcItem, calcAttr, i) { if(Array.isArray(traceAttr)) calcItem[calcAttr] = traceAttr[i]; }; -},{}],1250:[function(require,module,exports){ +},{}],599:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127256,9 +112928,9 @@ module.exports = function cleanNumber(v) { return BADNUM; }; -},{"../constants/numerical":1244,"fast-isnumeric":181}],1251:[function(require,module,exports){ +},{"../constants/numerical":593,"fast-isnumeric":97}],600:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -127574,9 +113246,10 @@ exports.coerce = function(containerIn, containerOut, attributes, attribute, dflt */ exports.coerce2 = function(containerIn, containerOut, attributes, attribute, dflt) { var propIn = nestedProperty(containerIn, attribute), - propOut = exports.coerce(containerIn, containerOut, attributes, attribute, dflt); + propOut = exports.coerce(containerIn, containerOut, attributes, attribute, dflt), + valIn = propIn.get(); - return propIn.get() ? propOut : false; + return (valIn !== undefined && valIn !== null) ? propOut : false; }; /* @@ -127615,9 +113288,9 @@ exports.validate = function(value, opts) { return out !== failed; }; -},{"../components/colorscale/get_scale":1173,"../components/colorscale/scales":1179,"./nested_property":1267,"fast-isnumeric":181,"tinycolor2":1129}],1252:[function(require,module,exports){ +},{"../components/colorscale/get_scale":522,"../components/colorscale/scales":528,"./nested_property":616,"fast-isnumeric":97,"tinycolor2":985}],601:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128243,9 +113916,9 @@ exports.findExactDates = function(data, calendar) { }; }; -},{"../constants/numerical":1244,"../registry":1368,"./loggers":1264,"./mod":1266,"d3":173,"fast-isnumeric":181}],1253:[function(require,module,exports){ +},{"../constants/numerical":593,"../registry":717,"./loggers":613,"./mod":615,"d3":86,"fast-isnumeric":97}],602:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128409,9 +114082,9 @@ var Events = { module.exports = Events; -},{"events":37}],1254:[function(require,module,exports){ +},{"events":95}],603:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128523,9 +114196,9 @@ function _extend(inputs, isDeep, keepAllKeys, noArrayCopies) { return target; } -},{"./is_plain_object.js":1263}],1255:[function(require,module,exports){ +},{"./is_plain_object.js":612}],604:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128574,9 +114247,9 @@ module.exports = function filterUnique(array) { return out; }; -},{}],1256:[function(require,module,exports){ +},{}],605:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128605,9 +114278,9 @@ module.exports = function filterVisible(container) { return out; }; -},{}],1257:[function(require,module,exports){ +},{}],606:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128667,9 +114340,9 @@ function countryNameToISO3(countryName) { return false; } -},{"../lib":1261,"country-regex":172}],1258:[function(require,module,exports){ +},{"../lib":610,"country-regex":79}],607:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128799,9 +114472,9 @@ exports.makeBlank = function() { }; }; -},{}],1259:[function(require,module,exports){ +},{}],608:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128882,9 +114555,9 @@ function formatColor(containerIn, opacityIn, len) { module.exports = formatColor; -},{"../components/color/attributes":1160,"../components/colorscale":1175,"./str2rgbarray":1275,"fast-isnumeric":181,"tinycolor2":1129}],1260:[function(require,module,exports){ +},{"../components/color/attributes":509,"../components/colorscale":524,"./str2rgbarray":624,"fast-isnumeric":97,"tinycolor2":985}],609:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -128951,9 +114624,9 @@ function convertHTMLToUnicode(html) { module.exports = convertHTMLToUnicode; -},{"../constants/string_mappings":1245,"superscript-text":1128}],1261:[function(require,module,exports){ +},{"../constants/string_mappings":594,"superscript-text":981}],610:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -129702,9 +115375,9 @@ lib.numSeparate = function(value, separators, separatethousands) { return x1 + x2; }; -},{"./clean_number":1250,"./coerce":1251,"./dates":1252,"./extend":1254,"./filter_unique":1255,"./filter_visible":1256,"./is_array":1262,"./is_plain_object":1263,"./loggers":1264,"./matrix":1265,"./mod":1266,"./nested_property":1267,"./notifier":1268,"./search":1271,"./stats":1274,"d3":173}],1262:[function(require,module,exports){ +},{"./clean_number":599,"./coerce":600,"./dates":601,"./extend":603,"./filter_unique":604,"./filter_visible":605,"./is_array":611,"./is_plain_object":612,"./loggers":613,"./matrix":614,"./mod":615,"./nested_property":616,"./notifier":617,"./search":620,"./stats":623,"d3":86}],611:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -129720,9 +115393,9 @@ module.exports = function isArray(a) { return Array.isArray(a) || ArrayBuffer.isView(a); }; -},{}],1263:[function(require,module,exports){ +},{}],612:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -129749,9 +115422,9 @@ module.exports = function isPlainObject(obj) { ); }; -},{}],1264:[function(require,module,exports){ +},{}],613:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -129816,9 +115489,9 @@ loggers.error = function() { } }; -},{"../plot_api/plot_config":1281}],1265:[function(require,module,exports){ +},{"../plot_api/plot_config":630}],614:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -129926,9 +115599,9 @@ exports.apply2DTransform2 = function(transform) { }; }; -},{}],1266:[function(require,module,exports){ +},{}],615:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -129946,9 +115619,9 @@ module.exports = function mod(v, d) { return out < 0 ? out + d : out; }; -},{}],1267:[function(require,module,exports){ +},{}],616:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -130203,9 +115876,9 @@ function badContainer(container, propStr, propParts) { }; } -},{"./is_array":1262,"fast-isnumeric":181}],1268:[function(require,module,exports){ +},{"./is_array":611,"fast-isnumeric":97}],617:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -130280,9 +115953,9 @@ module.exports = function(text, displayLength) { }); }; -},{"d3":173,"fast-isnumeric":181}],1269:[function(require,module,exports){ +},{"d3":86,"fast-isnumeric":97}],618:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -130520,9 +116193,9 @@ polygon.filter = function filter(pts, tolerance) { }; }; -},{"./matrix":1265}],1270:[function(require,module,exports){ +},{"./matrix":614}],619:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -130731,9 +116404,9 @@ queue.plotDo = function(gd, func, args) { module.exports = queue; -},{"../lib":1261,"../plot_api/plot_config":1281}],1271:[function(require,module,exports){ +},{"../lib":610,"../plot_api/plot_config":630}],620:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -130842,9 +116515,9 @@ exports.roundUp = function(val, arrayIn, reverse) { return arrayIn[low]; }; -},{"./loggers":1264,"fast-isnumeric":181}],1272:[function(require,module,exports){ +},{"./loggers":613,"fast-isnumeric":97}],621:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -130865,9 +116538,9 @@ module.exports = function setCursor(el3, csr) { if(csr) el3.classed('cursor-' + csr, true); }; -},{}],1273:[function(require,module,exports){ +},{}],622:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -130914,9 +116587,9 @@ module.exports = function showWebGlMsg(scene) { return false; }; -},{"../components/color":1161}],1274:[function(require,module,exports){ +},{"../components/color":510}],623:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -131010,9 +116683,9 @@ exports.interp = function(arr, n) { return frac * arr[Math.ceil(n)] + (1 - frac) * arr[Math.floor(n)]; }; -},{"fast-isnumeric":181}],1275:[function(require,module,exports){ +},{"fast-isnumeric":97}],624:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -131032,9 +116705,9 @@ function str2RgbaArray(color) { module.exports = str2RgbaArray; -},{"arraytools":161,"tinycolor2":1129}],1276:[function(require,module,exports){ +},{"arraytools":8,"tinycolor2":985}],625:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -131559,9 +117232,9 @@ exports.makeEditable = function(context, _delegate, options) { return d3.rebind(this, dispatch, 'on'); }; -},{"../constants/string_mappings":1245,"../constants/xmlns_namespaces":1246,"../lib":1261,"d3":173}],1277:[function(require,module,exports){ +},{"../constants/string_mappings":594,"../constants/xmlns_namespaces":595,"../lib":610,"d3":86}],626:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -131595,9 +117268,9 @@ topojsonUtils.getTopojsonFeatures = function(trace, topojson) { return topojsonFeature(topojson, obj).features; }; -},{"../plots/geo/constants":1315,"topojson-client":1130}],1278:[function(require,module,exports){ +},{"../plots/geo/constants":664,"topojson-client":987}],627:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -131629,9 +117302,9 @@ module.exports = function truncate(arrayIn, len) { throw new Error('This array type is not yet supported by `truncate`.'); }; -},{}],1279:[function(require,module,exports){ +},{}],628:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -132117,9 +117790,9 @@ exports.manageArrayContainers = function(np, newVal, undoit) { } }; -},{"../components/color":1161,"../lib":1261,"../plots/cartesian/axes":1293,"../plots/plots":1353,"../registry":1368,"fast-isnumeric":181,"gl-mat4/fromQuat":232}],1280:[function(require,module,exports){ +},{"../components/color":510,"../lib":610,"../plots/cartesian/axes":642,"../plots/plots":702,"../registry":717,"fast-isnumeric":97,"gl-mat4/fromQuat":153}],629:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -134942,9 +120615,9 @@ function makePlotFramework(gd) { gd.emit('plotly_framework'); } -},{"../components/drawing":1184,"../components/errorbars":1190,"../constants/xmlns_namespaces":1246,"../lib":1261,"../lib/events":1253,"../lib/queue":1270,"../lib/svg_text_utils":1276,"../plotly":1288,"../plots/cartesian/graph_interact":1300,"../plots/plots":1353,"../plots/polar":1356,"../registry":1368,"./helpers":1279,"./subroutines":1285,"d3":173,"fast-isnumeric":181}],1281:[function(require,module,exports){ +},{"../components/drawing":533,"../components/errorbars":539,"../constants/xmlns_namespaces":595,"../lib":610,"../lib/events":602,"../lib/queue":619,"../lib/svg_text_utils":625,"../plotly":637,"../plots/cartesian/graph_interact":649,"../plots/plots":702,"../plots/polar":705,"../registry":717,"./helpers":628,"./subroutines":634,"d3":86,"fast-isnumeric":97}],630:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -135062,9 +120735,9 @@ function defaultSetBackground(gd, bgColor) { } } -},{}],1282:[function(require,module,exports){ +},{}],631:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -135460,9 +121133,9 @@ function insertAttrs(baseAttrs, newAttrs, astr) { np.set(extendDeep(np.get() || {}, newAttrs)); } -},{"../lib":1261,"../plots/animation_attributes":1289,"../plots/attributes":1291,"../plots/frame_attributes":1314,"../plots/layout_attributes":1344,"../plots/polar/area_attributes":1354,"../plots/polar/axis_attributes":1355,"../registry":1368}],1283:[function(require,module,exports){ +},{"../lib":610,"../plots/animation_attributes":638,"../plots/attributes":640,"../plots/frame_attributes":663,"../plots/layout_attributes":693,"../plots/polar/area_attributes":703,"../plots/polar/axis_attributes":704,"../registry":717}],632:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -135559,9 +121232,9 @@ function registerComponentModule(newModule) { Registry.registerComponent(newModule); } -},{"../lib":1261,"../registry":1368}],1284:[function(require,module,exports){ +},{"../lib":610,"../registry":717}],633:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -135585,9 +121258,9 @@ module.exports = function setPlotConfig(configObj) { return Lib.extendFlat(Plotly.defaultConfig, configObj); }; -},{"../lib":1261,"../plotly":1288}],1285:[function(require,module,exports){ +},{"../lib":610,"../plotly":637}],634:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -135824,7 +121497,7 @@ exports.doTraceStyle = function(gd) { _module = ((cdi[0] || {}).trace || {})._module || {}, arraysToCalcdata = _module.arraysToCalcdata; - if(arraysToCalcdata) arraysToCalcdata(cdi); + if(arraysToCalcdata) arraysToCalcdata(cdi, cdi[0].trace); } Plots.style(gd); @@ -135907,9 +121580,9 @@ exports.doModeBar = function(gd) { return Plots.previousPromises(gd); }; -},{"../components/color":1161,"../components/drawing":1184,"../components/modebar":1207,"../components/titles":1235,"../lib":1261,"../plotly":1288,"../plots/plots":1353,"../registry":1368}],1286:[function(require,module,exports){ +},{"../components/color":510,"../components/drawing":533,"../components/modebar":556,"../components/titles":584,"../lib":610,"../plotly":637,"../plots/plots":702,"../registry":717}],635:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -136017,9 +121690,9 @@ function toImage(gd, opts) { module.exports = toImage; -},{"../lib":1261,"../plotly":1288,"../snapshot/cloneplot":1369,"../snapshot/helpers":1372,"../snapshot/svgtoimg":1374,"../snapshot/tosvg":1376,"fast-isnumeric":181}],1287:[function(require,module,exports){ +},{"../lib":610,"../plotly":637,"../snapshot/cloneplot":718,"../snapshot/helpers":721,"../snapshot/svgtoimg":723,"../snapshot/tosvg":725,"fast-isnumeric":97}],636:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -136387,9 +122060,9 @@ function convertPathToAttributeString(path) { return astr; } -},{"../lib":1261,"../plots/plots":1353,"./plot_schema":1282}],1288:[function(require,module,exports){ +},{"../lib":610,"../plots/plots":702,"./plot_schema":631}],637:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -136420,9 +122093,9 @@ exports.ModeBar = require('./components/modebar'); // plot api require('./plot_api/plot_api'); -},{"./components/modebar":1207,"./plot_api/plot_api":1280,"./plot_api/plot_config":1281,"./plots/cartesian/axes":1293,"./plots/cartesian/graph_interact":1300,"./plots/plots":1353}],1289:[function(require,module,exports){ +},{"./components/modebar":556,"./plot_api/plot_api":629,"./plot_api/plot_config":630,"./plots/cartesian/axes":642,"./plots/cartesian/graph_interact":649,"./plots/plots":702}],638:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -136544,9 +122217,9 @@ module.exports = { } }; -},{}],1290:[function(require,module,exports){ +},{}],639:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -136613,9 +122286,9 @@ module.exports = function handleArrayContainerDefaults(parentObjIn, parentObjOut } }; -},{"../lib":1261}],1291:[function(require,module,exports){ +},{"../lib":610}],640:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -136723,9 +122396,9 @@ module.exports = { } }; -},{}],1292:[function(require,module,exports){ +},{}],641:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -136762,9 +122435,9 @@ module.exports = { } }; -},{}],1293:[function(require,module,exports){ +},{}],642:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -138964,9 +124637,9 @@ function swapAxisAttrs(layout, key, xFullAxes, yFullAxes) { } } -},{"../../components/color":1161,"../../components/drawing":1184,"../../components/titles":1235,"../../constants/numerical":1244,"../../lib":1261,"../../lib/svg_text_utils":1276,"../../registry":1368,"./axis_ids":1296,"./layout_attributes":1302,"./layout_defaults":1303,"./set_convert":1307,"d3":173,"fast-isnumeric":181}],1294:[function(require,module,exports){ +},{"../../components/color":510,"../../components/drawing":533,"../../components/titles":584,"../../constants/numerical":593,"../../lib":610,"../../lib/svg_text_utils":625,"../../registry":717,"./axis_ids":645,"./layout_attributes":651,"./layout_defaults":652,"./set_convert":656,"d3":86,"fast-isnumeric":97}],643:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -139039,9 +124712,9 @@ function category(a) { return curvecats > curvenums * 2; } -},{"../../constants/numerical":1244,"../../lib":1261,"fast-isnumeric":181}],1295:[function(require,module,exports){ +},{"../../constants/numerical":593,"../../lib":610,"fast-isnumeric":97}],644:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -139272,9 +124945,9 @@ function getFirstNonEmptyTrace(data, id, axLetter) { } } -},{"../../components/color/attributes":1160,"../../lib":1261,"../../registry":1368,"./axis_autotype":1294,"./axis_ids":1296,"./category_order_defaults":1297,"./layout_attributes":1302,"./ordered_categories":1304,"./set_convert":1307,"./tick_label_defaults":1308,"./tick_mark_defaults":1309,"./tick_value_defaults":1310,"fast-isnumeric":181,"tinycolor2":1129}],1296:[function(require,module,exports){ +},{"../../components/color/attributes":509,"../../lib":610,"../../registry":717,"./axis_autotype":643,"./axis_ids":645,"./category_order_defaults":646,"./layout_attributes":651,"./ordered_categories":653,"./set_convert":656,"./tick_label_defaults":657,"./tick_mark_defaults":658,"./tick_value_defaults":659,"fast-isnumeric":97,"tinycolor2":985}],645:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -139394,9 +125067,9 @@ exports.getFromTrace = function(gd, fullTrace, type) { return ax; }; -},{"../../lib":1261,"../../registry":1368,"../plots":1353,"./constants":1298}],1297:[function(require,module,exports){ +},{"../../lib":610,"../../registry":717,"../plots":702,"./constants":647}],646:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -139428,9 +125101,9 @@ module.exports = function handleCategoryOrderDefaults(containerIn, containerOut, } }; -},{}],1298:[function(require,module,exports){ +},{}],647:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -139502,9 +125175,9 @@ module.exports = { DFLTRANGEY: [-1, 4] }; -},{}],1299:[function(require,module,exports){ +},{}],648:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -140267,9 +125940,9 @@ function isSelectOrLasso(dragmode) { return modes.indexOf(dragmode) !== -1; } -},{"../../components/color":1161,"../../components/dragelement":1182,"../../components/drawing":1184,"../../lib":1261,"../../lib/setcursor":1272,"../../lib/svg_text_utils":1276,"../../plotly":1288,"../../registry":1368,"./axes":1293,"./constants":1298,"./select":1306,"d3":173,"tinycolor2":1129}],1300:[function(require,module,exports){ +},{"../../components/color":510,"../../components/dragelement":531,"../../components/drawing":533,"../../lib":610,"../../lib/setcursor":621,"../../lib/svg_text_utils":625,"../../plotly":637,"../../registry":717,"./axes":642,"./constants":647,"./select":655,"d3":86,"tinycolor2":985}],649:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -141616,9 +127289,9 @@ fx.inbox = function(v0, v1) { return Infinity; }; -},{"../../components/color":1161,"../../components/dragelement":1182,"../../components/drawing":1184,"../../lib":1261,"../../lib/events":1253,"../../lib/svg_text_utils":1276,"../layout_attributes":1344,"./axes":1293,"./constants":1298,"./dragbox":1299,"d3":173,"fast-isnumeric":181,"tinycolor2":1129}],1301:[function(require,module,exports){ +},{"../../components/color":510,"../../components/dragelement":531,"../../components/drawing":533,"../../lib":610,"../../lib/events":602,"../../lib/svg_text_utils":625,"../layout_attributes":693,"./axes":642,"./constants":647,"./dragbox":648,"d3":86,"fast-isnumeric":97,"tinycolor2":985}],650:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -141993,9 +127666,9 @@ function joinLayer(parent, nodeType, className) { return layer; } -},{"../../lib":1261,"../plots":1353,"./attributes":1292,"./axes":1293,"./constants":1298,"./layout_attributes":1302,"./transition_axes":1311,"d3":173}],1302:[function(require,module,exports){ +},{"../../lib":610,"../plots":702,"./attributes":641,"./axes":642,"./constants":647,"./layout_attributes":651,"./transition_axes":660,"d3":86}],651:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -142527,9 +128200,9 @@ module.exports = { } }; -},{"../../components/color/attributes":1160,"../../lib/extend":1254,"../font_attributes":1313,"./constants":1298}],1303:[function(require,module,exports){ +},{"../../components/color/attributes":509,"../../lib/extend":603,"../font_attributes":662,"./constants":647}],652:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -142703,9 +128376,9 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) { }); }; -},{"../../components/color":1161,"../../lib":1261,"../../registry":1368,"../layout_attributes":1344,"./axis_defaults":1295,"./axis_ids":1296,"./constants":1298,"./layout_attributes":1302,"./position_defaults":1305}],1304:[function(require,module,exports){ +},{"../../components/color":510,"../../lib":610,"../../registry":717,"../layout_attributes":693,"./axis_defaults":644,"./axis_ids":645,"./constants":647,"./layout_attributes":651,"./position_defaults":654}],653:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -142782,9 +128455,9 @@ module.exports = function orderedCategories(axisLetter, categoryorder, categorya } }; -},{"d3":173}],1305:[function(require,module,exports){ +},{"d3":86}],654:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -142847,9 +128520,9 @@ module.exports = function handlePositionDefaults(containerIn, containerOut, coer return containerOut; }; -},{"../../lib":1261,"fast-isnumeric":181}],1306:[function(require,module,exports){ +},{"../../lib":610,"fast-isnumeric":97}],655:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -143047,9 +128720,9 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) { }; }; -},{"../../components/color":1161,"../../lib/polygon":1269,"./axes":1293,"./constants":1298}],1307:[function(require,module,exports){ +},{"../../components/color":510,"../../lib/polygon":618,"./axes":642,"./constants":647}],656:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -143463,9 +129136,9 @@ module.exports = function setConvert(ax) { delete ax._forceTick0; }; -},{"../../constants/numerical":1244,"../../lib":1261,"./axis_ids":1296,"./constants":1298,"d3":173,"fast-isnumeric":181}],1308:[function(require,module,exports){ +},{"../../constants/numerical":593,"../../lib":610,"./axis_ids":645,"./constants":647,"d3":86,"fast-isnumeric":97}],657:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -143547,9 +129220,9 @@ function getShowAttrDflt(containerIn) { } } -},{"../../lib":1261}],1309:[function(require,module,exports){ +},{"../../lib":610}],658:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -143580,9 +129253,9 @@ module.exports = function handleTickDefaults(containerIn, containerOut, coerce, } }; -},{"../../lib":1261,"./layout_attributes":1302}],1310:[function(require,module,exports){ +},{"../../lib":610,"./layout_attributes":651}],659:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -143664,9 +129337,9 @@ module.exports = function handleTickValueDefaults(containerIn, containerOut, coe } }; -},{"../../constants/numerical":1244,"../../lib":1261,"fast-isnumeric":181}],1311:[function(require,module,exports){ +},{"../../constants/numerical":593,"../../lib":610,"fast-isnumeric":97}],660:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -143976,9 +129649,9 @@ module.exports = function transitionAxes(gd, newLayout, transitionOpts, makeOnCo return Promise.resolve(); }; -},{"../../lib":1261,"../../plotly":1288,"../../registry":1368,"./axes":1293,"d3":173}],1312:[function(require,module,exports){ +},{"../../lib":610,"../../plotly":637,"../../registry":717,"./axes":642,"d3":86}],661:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -144393,9 +130066,9 @@ function crawl(attrs, callback, path, depth) { }); } -},{"../lib":1261,"../plotly":1288}],1313:[function(require,module,exports){ +},{"../lib":610,"../plotly":637}],662:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -144435,9 +130108,9 @@ module.exports = { } }; -},{}],1314:[function(require,module,exports){ +},{}],663:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -144492,9 +130165,9 @@ module.exports = { } }; -},{}],1315:[function(require,module,exports){ +},{}],664:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -144650,9 +130323,9 @@ params.layerNameToAdjective = { // base layers drawn over choropleth params.baseLayersOverChoropleth = ['rivers', 'lakes']; -},{}],1316:[function(require,module,exports){ +},{}],665:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -145212,9 +130885,9 @@ function createMockAxis(fullLayout) { return mockAxis; } -},{"../../components/color":1161,"../../components/drawing":1184,"../../constants/xmlns_namespaces":1246,"../../lib/topojson_utils":1277,"../../plots/cartesian/axes":1293,"../../plots/cartesian/graph_interact":1300,"./constants":1315,"./projections":1323,"./set_scale":1324,"./zoom":1325,"./zoom_reset":1326,"d3":173,"topojson-client":1130}],1317:[function(require,module,exports){ +},{"../../components/color":510,"../../components/drawing":533,"../../constants/xmlns_namespaces":595,"../../lib/topojson_utils":626,"../../plots/cartesian/axes":642,"../../plots/cartesian/graph_interact":649,"./constants":664,"./projections":672,"./set_scale":673,"./zoom":674,"./zoom_reset":675,"d3":86,"topojson-client":987}],666:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -145331,9 +131004,9 @@ function getSubplotCalcData(calcData, id) { return subplotCalcData; } -},{"../../plots/plots":1353,"./geo":1316,"./layout/attributes":1318,"./layout/defaults":1321,"./layout/layout_attributes":1322}],1318:[function(require,module,exports){ +},{"../../plots/plots":702,"./geo":665,"./layout/attributes":667,"./layout/defaults":670,"./layout/layout_attributes":671}],667:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -145359,9 +131032,9 @@ module.exports = { } }; -},{}],1319:[function(require,module,exports){ +},{}],668:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -145422,9 +131095,9 @@ module.exports = { } }; -},{"../../../components/color/attributes":1160}],1320:[function(require,module,exports){ +},{"../../../components/color/attributes":509}],669:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -145496,9 +131169,9 @@ module.exports = function supplyGeoAxisLayoutDefaults(geoLayoutIn, geoLayoutOut) } }; -},{"../../../lib":1261,"../constants":1315,"./axis_attributes":1319}],1321:[function(require,module,exports){ +},{"../../../lib":610,"../constants":664,"./axis_attributes":668}],670:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -145615,9 +131288,9 @@ function handleGeoDefaults(geoLayoutIn, geoLayoutOut, coerce) { ]; } -},{"../../subplot_defaults":1360,"../constants":1315,"./axis_defaults":1320,"./layout_attributes":1322}],1322:[function(require,module,exports){ +},{"../../subplot_defaults":709,"../constants":664,"./axis_defaults":669,"./layout_attributes":671}],671:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -145874,9 +131547,9 @@ module.exports = { lataxis: geoAxesAttrs }; -},{"../../../components/color/attributes":1160,"../constants":1315,"./axis_attributes":1319}],1323:[function(require,module,exports){ +},{"../../../components/color/attributes":509,"../constants":664,"./axis_attributes":668}],672:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -146320,9 +131993,9 @@ function addProjectionsToD3(d3) { module.exports = addProjectionsToD3; -},{}],1324:[function(require,module,exports){ +},{}],673:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -146471,9 +132144,9 @@ function getBounds(projection, rangeBox) { return d3.geo.path().projection(projection).bounds(rangeBox); } -},{"./constants":1315,"d3":173}],1325:[function(require,module,exports){ +},{"./constants":664,"d3":86}],674:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -146898,9 +132571,9 @@ function d3_eventDispatch(target) { return dispatch; } -},{"d3":173}],1326:[function(require,module,exports){ +},{"d3":86}],675:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -146933,9 +132606,9 @@ function createGeoZoomReset(geo, geoLayout) { module.exports = createGeoZoomReset; -},{"../cartesian/graph_interact":1300}],1327:[function(require,module,exports){ +},{"../cartesian/graph_interact":649}],676:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -147097,9 +132770,9 @@ function createCamera(scene) { return result; } -},{"mouse-change":1091,"mouse-wheel":1095}],1328:[function(require,module,exports){ +},{"mouse-change":428,"mouse-wheel":431}],677:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -147343,9 +133016,9 @@ function createAxes2D(scene) { module.exports = createAxes2D; -},{"../../lib/html2unicode":1260,"../../lib/str2rgbarray":1275,"../cartesian/axes":1293,"../plots":1353}],1329:[function(require,module,exports){ +},{"../../lib/html2unicode":609,"../../lib/str2rgbarray":624,"../cartesian/axes":642,"../plots":702}],678:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -147455,9 +133128,9 @@ exports.toSVG = function(gd) { } }; -},{"../../constants/xmlns_namespaces":1246,"../cartesian/attributes":1292,"../plots":1353,"./scene2d":1330}],1330:[function(require,module,exports){ +},{"../../constants/xmlns_namespaces":595,"../cartesian/attributes":641,"../plots":702,"./scene2d":679}],679:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148076,9 +133749,9 @@ proto.hoverFormatter = function(axisName, val) { return Axes.tickText(axis, axis.c2l(val), 'hover').text; }; -},{"../../lib/html2unicode":1260,"../../lib/show_no_webgl_msg":1273,"../../plots/cartesian/axes":1293,"../../plots/cartesian/graph_interact":1300,"./camera":1327,"./convert":1328,"gl-plot2d":421,"gl-select-box":834,"gl-spikes2d":861,"webgl-context":1131}],1331:[function(require,module,exports){ +},{"../../lib/html2unicode":609,"../../lib/show_no_webgl_msg":622,"../../plots/cartesian/axes":642,"../../plots/cartesian/graph_interact":649,"./camera":676,"./convert":677,"gl-plot2d":194,"gl-select-box":225,"gl-spikes2d":234,"webgl-context":1015}],680:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148319,9 +133992,9 @@ function createCamera(element, options) { return camera; } -},{"3d-view":142,"mouse-change":1091,"mouse-wheel":1095,"right-now":1121}],1332:[function(require,module,exports){ +},{"3d-view":2,"mouse-change":428,"mouse-wheel":431,"right-now":948}],681:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148453,9 +134126,9 @@ function initAxes(gd, sceneLayout) { } } -},{"../../constants/xmlns_namespaces":1246,"../plots":1353,"./layout/attributes":1333,"./layout/defaults":1337,"./layout/layout_attributes":1338,"./scene":1342,"./set_convert":1343}],1333:[function(require,module,exports){ +},{"../../constants/xmlns_namespaces":595,"../plots":702,"./layout/attributes":682,"./layout/defaults":686,"./layout/layout_attributes":687,"./scene":691,"./set_convert":692}],682:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148481,9 +134154,9 @@ module.exports = { } }; -},{}],1334:[function(require,module,exports){ +},{}],683:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148598,9 +134271,9 @@ module.exports = { zerolinewidth: axesAttrs.zerolinewidth }; -},{"../../../components/color":1161,"../../../lib/extend":1254,"../../cartesian/layout_attributes":1302}],1335:[function(require,module,exports){ +},{"../../../components/color":510,"../../../lib/extend":603,"../../cartesian/layout_attributes":651}],684:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148667,9 +134340,9 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, options) { } }; -},{"../../../lib":1261,"../../cartesian/axis_defaults":1295,"./axis_attributes":1334,"tinycolor2":1129}],1336:[function(require,module,exports){ +},{"../../../lib":610,"../../cartesian/axis_defaults":644,"./axis_attributes":683,"tinycolor2":985}],685:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148822,9 +134495,9 @@ function createAxesOptions(plotlyOptions) { module.exports = createAxesOptions; -},{"../../../lib/html2unicode":1260,"../../../lib/str2rgbarray":1275,"arraytools":161}],1337:[function(require,module,exports){ +},{"../../../lib/html2unicode":609,"../../../lib/str2rgbarray":624,"arraytools":8}],686:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -148931,9 +134604,9 @@ function handleGl3dDefaults(sceneLayoutIn, sceneLayoutOut, coerce, opts) { coerce('hovermode', opts.getDfltFromLayout('hovermode')); } -},{"../../../components/color":1161,"../../subplot_defaults":1360,"./axis_defaults":1335,"./layout_attributes":1338}],1338:[function(require,module,exports){ +},{"../../../components/color":510,"../../subplot_defaults":709,"./axis_defaults":684,"./layout_attributes":687}],687:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -149101,9 +134774,9 @@ module.exports = { } }; -},{"../../../lib/extend":1254,"./axis_attributes":1334}],1339:[function(require,module,exports){ +},{"../../../lib/extend":603,"./axis_attributes":683}],688:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -149147,9 +134820,9 @@ function createSpikeOptions(layout) { module.exports = createSpikeOptions; -},{"../../../lib/str2rgbarray":1275}],1340:[function(require,module,exports){ +},{"../../../lib/str2rgbarray":624}],689:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -149243,9 +134916,9 @@ function computeTickMarks(scene) { scene.contourLevels = contourLevelsFromTicks(ticks); } -},{"../../../lib":1261,"../../../lib/html2unicode":1260,"../../cartesian/axes":1293}],1341:[function(require,module,exports){ +},{"../../../lib":610,"../../../lib/html2unicode":609,"../../cartesian/axes":642}],690:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -149277,9 +134950,9 @@ function project(camera, v) { module.exports = project; -},{}],1342:[function(require,module,exports){ +},{}],691:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150006,9 +135679,9 @@ proto.toImage = function(format) { module.exports = Scene; -},{"../../lib":1261,"../../lib/show_no_webgl_msg":1273,"../../lib/str2rgbarray":1275,"../../plots/cartesian/axes":1293,"../../plots/cartesian/graph_interact":1300,"./camera":1331,"./layout/convert":1336,"./layout/spikes":1339,"./layout/tick_marks":1340,"./project":1341,"./set_convert":1343,"gl-plot3d":569,"webgl-context":1131}],1343:[function(require,module,exports){ +},{"../../lib":610,"../../lib/show_no_webgl_msg":622,"../../lib/str2rgbarray":624,"../../plots/cartesian/axes":642,"../../plots/cartesian/graph_interact":649,"./camera":680,"./layout/convert":685,"./layout/spikes":688,"./layout/tick_marks":689,"./project":690,"./set_convert":692,"gl-plot3d":196,"webgl-context":1015}],692:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150027,9 +135700,9 @@ module.exports = function setConvert(containerOut) { containerOut.setScale = Lib.noop; }; -},{"../../lib":1261,"../cartesian/axes":1293}],1344:[function(require,module,exports){ +},{"../../lib":610,"../cartesian/axes":642}],693:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150220,9 +135893,9 @@ module.exports = { } }; -},{"../components/color/attributes":1160,"../lib":1261,"./font_attributes":1313}],1345:[function(require,module,exports){ +},{"../components/color/attributes":509,"../lib":610,"./font_attributes":662}],694:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150250,9 +135923,9 @@ module.exports = { mapOnErrorMsg: 'Mapbox error.' }; -},{}],1346:[function(require,module,exports){ +},{}],695:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150324,9 +135997,9 @@ module.exports = function convertTextOpts(textposition, iconSize) { return { anchor: anchor, offset: offset }; }; -},{"../../lib":1261}],1347:[function(require,module,exports){ +},{"../../lib":610}],696:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150485,9 +136158,9 @@ function findAccessToken(gd, mapboxIds) { return accessToken; } -},{"../../constants/xmlns_namespaces":1246,"../plots":1353,"./constants":1345,"./layout_attributes":1349,"./layout_defaults":1350,"./mapbox":1351,"mapbox-gl":916}],1348:[function(require,module,exports){ +},{"../../constants/xmlns_namespaces":595,"../plots":702,"./constants":694,"./layout_attributes":698,"./layout_defaults":699,"./mapbox":700,"mapbox-gl":319}],697:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150710,9 +136383,9 @@ module.exports = function createMapboxLayer(mapbox, index, opts) { return mapboxLayer; }; -},{"../../lib":1261,"./convert_text_opts":1346}],1349:[function(require,module,exports){ +},{"../../lib":610,"./convert_text_opts":695}],698:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -150977,9 +136650,9 @@ module.exports = { }; -},{"../../components/color":1161,"../../lib":1261,"../../traces/scatter/attributes":1485,"../font_attributes":1313}],1350:[function(require,module,exports){ +},{"../../components/color":510,"../../lib":610,"../../traces/scatter/attributes":834,"../font_attributes":662}],699:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -151073,9 +136746,9 @@ function handleLayerDefaults(containerIn, containerOut) { } } -},{"../../lib":1261,"../subplot_defaults":1360,"./layout_attributes":1349}],1351:[function(require,module,exports){ +},{"../../lib":610,"../subplot_defaults":709,"./layout_attributes":698}],700:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -151534,9 +137207,9 @@ function convertCenter(center) { return [center.lon, center.lat]; } -},{"../../lib":1261,"../cartesian/graph_interact":1300,"./constants":1345,"./layers":1348,"./layout_attributes":1349,"mapbox-gl":916}],1352:[function(require,module,exports){ +},{"../../lib":610,"../cartesian/graph_interact":649,"./constants":694,"./layers":697,"./layout_attributes":698,"mapbox-gl":319}],701:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -151572,9 +137245,9 @@ module.exports = { } }; -},{}],1353:[function(require,module,exports){ +},{}],702:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -153496,8 +139169,9 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts) plots.doCalcdata = function(gd, traces) { var axList = Plotly.Axes.list(gd), fullData = gd._fullData, - fullLayout = gd._fullLayout, - i, j; + fullLayout = gd._fullLayout; + + var trace, _module, i, j; // XXX: Is this correct? Needs a closer look so that *some* traces can be recomputed without // *all* needing doCalcdata: @@ -153526,46 +139200,57 @@ plots.doCalcdata = function(gd, traces) { axList[i]._categories = axList[i]._initialCategories.slice(); } + // If traces were specified and this trace was not included, + // then transfer it over from the old calcdata: for(i = 0; i < fullData.length; i++) { - // If traces were specified and this trace was not included, then transfer it over from - // the old calcdata: if(Array.isArray(traces) && traces.indexOf(i) === -1) { calcdata[i] = oldCalcdata[i]; continue; } + } - var trace = fullData[i], - cd = []; + var hasCalcTransform = false; - // If traces were specified and this trace was not included, then transfer it over from - // the old calcdata: - if(Array.isArray(traces) && traces.indexOf(i) === -1) { - calcdata[i] = oldCalcdata[i]; - continue; - } + // transform loop + for(i = 0; i < fullData.length; i++) { + trace = fullData[i]; - var _module; - if(trace.visible === true) { + if(trace.visible === true && trace.transforms) { + _module = trace._module; - // call calcTransform method if any - if(trace.transforms) { + // we need one round of trace module calc before + // the calc transform to 'fill in' the categories list + // used for example in the data-to-coordinate method + if(_module && _module.calc) _module.calc(gd, trace); - // we need one round of trace module calc before - // the calc transform to 'fill in' the categories list - // used for example in the data-to-coordinate method - _module = trace._module; - if(_module && _module.calc) _module.calc(gd, trace); + for(j = 0; j < trace.transforms.length; j++) { + var transform = trace.transforms[j]; - for(j = 0; j < trace.transforms.length; j++) { - var transform = trace.transforms[j]; - - _module = transformsRegistry[transform.type]; - if(_module && _module.calcTransform) { - _module.calcTransform(gd, trace, transform); - } + _module = transformsRegistry[transform.type]; + if(_module && _module.calcTransform) { + hasCalcTransform = true; + _module.calcTransform(gd, trace, transform); } } + } + } + // clear stuff that should recomputed in 'regular' loop + if(hasCalcTransform) { + for(i = 0; i < axList.length; i++) { + axList[i]._min = []; + axList[i]._max = []; + axList[i]._categories = []; + } + } + + // 'regular' loop + for(i = 0; i < fullData.length; i++) { + var cd = []; + + trace = fullData[i]; + + if(trace.visible === true) { _module = trace._module; if(_module && _module.calc) cd = _module.calc(gd, trace); } @@ -153591,9 +139276,9 @@ plots.doCalcdata = function(gd, traces) { } }; -},{"../components/color":1161,"../components/errorbars":1190,"../lib":1261,"../plotly":1288,"../registry":1368,"./animation_attributes":1289,"./attributes":1291,"./command":1312,"./font_attributes":1313,"./frame_attributes":1314,"./layout_attributes":1344,"d3":173,"fast-isnumeric":181}],1354:[function(require,module,exports){ +},{"../components/color":510,"../components/errorbars":539,"../lib":610,"../plotly":637,"../registry":717,"./animation_attributes":638,"./attributes":640,"./command":661,"./font_attributes":662,"./frame_attributes":663,"./layout_attributes":693,"d3":86,"fast-isnumeric":97}],703:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -153616,9 +139301,9 @@ module.exports = { } }; -},{"../../traces/scatter/attributes":1485}],1355:[function(require,module,exports){ +},{"../../traces/scatter/attributes":834}],704:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -153766,9 +139451,9 @@ module.exports = { } }; -},{"../../lib/extend":1254,"../cartesian/layout_attributes":1302}],1356:[function(require,module,exports){ +},{"../../lib/extend":603,"../cartesian/layout_attributes":651}],705:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -153781,9 +139466,9 @@ var Polar = module.exports = require('./micropolar'); Polar.manager = require('./micropolar_manager'); -},{"./micropolar":1357,"./micropolar_manager":1358}],1357:[function(require,module,exports){ +},{"./micropolar":706,"./micropolar_manager":707}],706:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155200,9 +140885,9 @@ var µ = module.exports = { version: '0.2.2' }; return exports; }; -},{"../../lib":1261,"d3":173}],1358:[function(require,module,exports){ +},{"../../lib":610,"d3":86}],707:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155286,9 +140971,9 @@ manager.fillLayout = function(_gd) { _gd._fullLayout = extendDeepAll(dflts, _gd.layout); }; -},{"../../components/color":1161,"../../lib":1261,"./micropolar":1357,"./undo_manager":1359,"d3":173}],1359:[function(require,module,exports){ +},{"../../components/color":510,"../../lib":610,"./micropolar":706,"./undo_manager":708,"d3":86}],708:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155352,9 +141037,9 @@ module.exports = function UndoManager() { }; }; -},{}],1360:[function(require,module,exports){ +},{}],709:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155427,9 +141112,9 @@ module.exports = function handleSubplotDefaults(layoutIn, layoutOut, fullData, o } }; -},{"../lib":1261,"./plots":1353}],1361:[function(require,module,exports){ +},{"../lib":610,"./plots":702}],710:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155501,9 +141186,9 @@ exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) } }; -},{"../../plots/plots":1353,"./layout/attributes":1362,"./layout/defaults":1365,"./layout/layout_attributes":1366,"./ternary":1367}],1362:[function(require,module,exports){ +},{"../../plots/plots":702,"./layout/attributes":711,"./layout/defaults":714,"./layout/layout_attributes":715,"./ternary":716}],711:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155527,9 +141212,9 @@ module.exports = { } }; -},{}],1363:[function(require,module,exports){ +},{}],712:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155592,9 +141277,9 @@ module.exports = { } }; -},{"../../../lib/extend":1254,"../../cartesian/layout_attributes":1302}],1364:[function(require,module,exports){ +},{"../../../lib/extend":603,"../../cartesian/layout_attributes":651}],713:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155676,9 +141361,9 @@ module.exports = function supplyLayoutDefaults(containerIn, containerOut, option } }; -},{"../../../lib":1261,"../../cartesian/tick_label_defaults":1308,"../../cartesian/tick_mark_defaults":1309,"../../cartesian/tick_value_defaults":1310,"./axis_attributes":1363,"tinycolor2":1129}],1365:[function(require,module,exports){ +},{"../../../lib":610,"../../cartesian/tick_label_defaults":657,"../../cartesian/tick_mark_defaults":658,"../../cartesian/tick_value_defaults":659,"./axis_attributes":712,"tinycolor2":985}],714:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155739,9 +141424,9 @@ function handleTernaryDefaults(ternaryLayoutIn, ternaryLayoutOut, coerce, option } } -},{"../../../components/color":1161,"../../subplot_defaults":1360,"./axis_defaults":1364,"./layout_attributes":1366}],1366:[function(require,module,exports){ +},{"../../../components/color":510,"../../subplot_defaults":709,"./axis_defaults":713,"./layout_attributes":715}],715:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -155804,9 +141489,9 @@ module.exports = { caxis: ternaryAxesAttrs }; -},{"../../../components/color/attributes":1160,"./axis_attributes":1363}],1367:[function(require,module,exports){ +},{"../../../components/color/attributes":509,"./axis_attributes":712}],716:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -156493,9 +142178,9 @@ function removeZoombox(gd) { .remove(); } -},{"../../components/color":1161,"../../components/dragelement":1182,"../../components/drawing":1184,"../../components/titles":1235,"../../lib":1261,"../../lib/extend":1254,"../../plotly":1288,"../cartesian/axes":1293,"../cartesian/constants":1298,"../cartesian/graph_interact":1300,"../cartesian/select":1306,"../cartesian/set_convert":1307,"d3":173,"tinycolor2":1129}],1368:[function(require,module,exports){ +},{"../../components/color":510,"../../components/dragelement":531,"../../components/drawing":533,"../../components/titles":584,"../../lib":610,"../../lib/extend":603,"../../plotly":637,"../cartesian/axes":642,"../cartesian/constants":647,"../cartesian/graph_interact":649,"../cartesian/select":655,"../cartesian/set_convert":656,"d3":86,"tinycolor2":985}],717:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -156661,9 +142346,9 @@ function getTraceType(traceType) { return traceType; } -},{"./lib":1261,"./plots/attributes":1291}],1369:[function(require,module,exports){ +},{"./lib":610,"./plots/attributes":640}],718:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -156819,9 +142504,9 @@ module.exports = function clonePlot(graphObj, options) { return plotTile; }; -},{"../lib":1261,"../plots/plots":1353}],1370:[function(require,module,exports){ +},{"../lib":610,"../plots/plots":702}],719:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -156885,9 +142570,9 @@ function downloadImage(gd, opts) { module.exports = downloadImage; -},{"../lib":1261,"../plot_api/to_image":1286,"./filesaver":1371}],1371:[function(require,module,exports){ +},{"../lib":610,"../plot_api/to_image":635,"./filesaver":720}],720:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -156953,9 +142638,9 @@ var fileSaver = function(url, name) { module.exports = fileSaver; -},{}],1372:[function(require,module,exports){ +},{}],721:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -156986,9 +142671,9 @@ exports.getRedrawFunc = function(gd) { }; }; -},{}],1373:[function(require,module,exports){ +},{}],722:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157012,9 +142697,9 @@ var Snapshot = { module.exports = Snapshot; -},{"./cloneplot":1369,"./download":1370,"./helpers":1372,"./svgtoimg":1374,"./toimage":1375,"./tosvg":1376}],1374:[function(require,module,exports){ +},{"./cloneplot":718,"./download":719,"./helpers":721,"./svgtoimg":723,"./toimage":724,"./tosvg":725}],723:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157143,9 +142828,9 @@ function svgToImg(opts) { module.exports = svgToImg; -},{"../lib":1261,"events":37}],1375:[function(require,module,exports){ +},{"../lib":610,"events":95}],724:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157223,9 +142908,9 @@ function toImage(gd, opts) { module.exports = toImage; -},{"../lib":1261,"../plotly":1288,"./cloneplot":1369,"./helpers":1372,"./svgtoimg":1374,"./tosvg":1376,"events":37}],1376:[function(require,module,exports){ +},{"../lib":610,"../plotly":637,"./cloneplot":718,"./helpers":721,"./svgtoimg":723,"./tosvg":725,"events":95}],725:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157342,9 +143027,9 @@ module.exports = function toSVG(gd, format) { return s; }; -},{"../components/color":1161,"../components/drawing":1184,"../constants/xmlns_namespaces":1246,"../lib/svg_text_utils":1276,"d3":173}],1377:[function(require,module,exports){ +},{"../components/color":510,"../components/drawing":533,"../constants/xmlns_namespaces":595,"../lib/svg_text_utils":625,"d3":86}],726:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157358,25 +143043,25 @@ var mergeArray = require('../../lib').mergeArray; // arrayOk attributes, merge them into calcdata array -module.exports = function arraysToCalcdata(cd) { - var trace = cd[0].trace, - marker = trace.marker; - +module.exports = function arraysToCalcdata(cd, trace) { mergeArray(trace.text, cd, 'tx'); - if(marker && marker.line) { - var markerLine = marker.line; - + var marker = trace.marker; + if(marker) { mergeArray(marker.opacity, cd, 'mo'); mergeArray(marker.color, cd, 'mc'); - mergeArray(markerLine.color, cd, 'mlc'); - mergeArray(markerLine.width, cd, 'mlw'); + + var markerLine = marker.line; + if(markerLine) { + mergeArray(markerLine.color, cd, 'mlc'); + mergeArray(markerLine.width, cd, 'mlw'); + } } }; -},{"../../lib":1261}],1378:[function(require,module,exports){ +},{"../../lib":610}],727:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157523,9 +143208,9 @@ module.exports = { } }; -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/color_attributes":1168,"../../components/errorbars/attributes":1186,"../../lib/extend":1254,"../../plots/font_attributes":1313,"../scatter/attributes":1485}],1379:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/color_attributes":517,"../../components/errorbars/attributes":535,"../../lib/extend":603,"../../plots/font_attributes":662,"../scatter/attributes":834}],728:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157541,6 +143226,8 @@ var Axes = require('../../plots/cartesian/axes'); var hasColorscale = require('../../components/colorscale/has_colorscale'); var colorscaleCalc = require('../../components/colorscale/calc'); +var arraysToCalcdata = require('./arrays_to_calcdata'); + module.exports = function calc(gd, trace) { // depending on bar direction, set position and size axes @@ -157623,12 +143310,14 @@ module.exports = function calc(gd, trace) { colorscaleCalc(trace, trace.marker.line.color, 'marker.line', 'c'); } + arraysToCalcdata(cd, trace); + return cd; }; -},{"../../components/colorscale/calc":1167,"../../components/colorscale/has_colorscale":1174,"../../plots/cartesian/axes":1293,"fast-isnumeric":181}],1380:[function(require,module,exports){ +},{"../../components/colorscale/calc":516,"../../components/colorscale/has_colorscale":523,"../../plots/cartesian/axes":642,"./arrays_to_calcdata":726,"fast-isnumeric":97}],729:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157685,9 +143374,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout errorBarsSupplyDefaults(traceIn, traceOut, Color.defaultLine, {axis: 'x', inherit: 'y'}); }; -},{"../../components/color":1161,"../../components/errorbars/defaults":1189,"../../lib":1261,"../bar/style_defaults":1389,"../scatter/xy_defaults":1507,"./attributes":1378}],1381:[function(require,module,exports){ +},{"../../components/color":510,"../../components/errorbars/defaults":538,"../../lib":610,"../bar/style_defaults":738,"../scatter/xy_defaults":856,"./attributes":727}],730:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157778,9 +143467,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { return [pointData]; }; -},{"../../components/color":1161,"../../components/errorbars":1190,"../../plots/cartesian/graph_interact":1300}],1382:[function(require,module,exports){ +},{"../../components/color":510,"../../components/errorbars":539,"../../plots/cartesian/graph_interact":649}],731:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157819,9 +143508,9 @@ Bar.meta = { module.exports = Bar; -},{"../../plots/cartesian":1301,"../scatter/colorbar":1488,"./arrays_to_calcdata":1377,"./attributes":1378,"./calc":1379,"./defaults":1380,"./hover":1381,"./layout_attributes":1383,"./layout_defaults":1384,"./plot":1385,"./set_positions":1386,"./style":1388}],1383:[function(require,module,exports){ +},{"../../plots/cartesian":650,"../scatter/colorbar":837,"./arrays_to_calcdata":726,"./attributes":727,"./calc":728,"./defaults":729,"./hover":730,"./layout_attributes":732,"./layout_defaults":733,"./plot":734,"./set_positions":735,"./style":737}],732:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157884,9 +143573,9 @@ module.exports = { } }; -},{}],1384:[function(require,module,exports){ +},{}],733:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157942,9 +143631,9 @@ module.exports = function(layoutIn, layoutOut, fullData) { coerce('bargroupgap'); }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"../../registry":1368,"./layout_attributes":1383}],1385:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"../../registry":717,"./layout_attributes":732}],734:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -157965,8 +143654,6 @@ var Color = require('../../components/color'); var Drawing = require('../../components/drawing'); var ErrorBars = require('../../components/errorbars'); -var arraysToCalcdata = require('./arrays_to_calcdata'); - var attributes = require('./attributes'), attributeText = attributes.text, attributeTextPosition = attributes.textposition, @@ -157998,8 +143685,6 @@ module.exports = function plot(gd, plotinfo, cdbar) { barwidth = t.barwidth, barwidthIsArray = Array.isArray(barwidth); - arraysToCalcdata(d); - d3.select(this).selectAll('g.point') .data(Lib.identity) .enter().append('g').classed('point', true) @@ -158467,9 +144152,9 @@ function coerceColor(attributeDefinition, value, defaultValue) { attributeDefinition.dflt; } -},{"../../components/color":1161,"../../components/drawing":1184,"../../components/errorbars":1190,"../../lib":1261,"../../lib/svg_text_utils":1276,"./arrays_to_calcdata":1377,"./attributes":1378,"d3":173,"fast-isnumeric":181,"tinycolor2":1129}],1386:[function(require,module,exports){ +},{"../../components/color":510,"../../components/drawing":533,"../../components/errorbars":539,"../../lib":610,"../../lib/svg_text_utils":625,"./attributes":727,"d3":86,"fast-isnumeric":97,"tinycolor2":985}],735:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159076,9 +144761,9 @@ function getAxisLetter(ax) { return ax._id.charAt(0); } -},{"../../plots/cartesian/axes":1293,"../../registry":1368,"./sieve.js":1387,"fast-isnumeric":181}],1387:[function(require,module,exports){ +},{"../../plots/cartesian/axes":642,"../../registry":717,"./sieve.js":736,"fast-isnumeric":97}],736:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159177,9 +144862,9 @@ Sieve.prototype.getLabel = function getLabel(position, value) { return prefix + label; }; -},{"../../lib":1261}],1388:[function(require,module,exports){ +},{"../../lib":610}],737:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159255,9 +144940,9 @@ module.exports = function style(gd) { s.call(ErrorBars.style); }; -},{"../../components/color":1161,"../../components/drawing":1184,"../../components/errorbars":1190,"d3":173}],1389:[function(require,module,exports){ +},{"../../components/color":510,"../../components/drawing":533,"../../components/errorbars":539,"d3":86}],738:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159292,9 +144977,9 @@ module.exports = function handleStyleDefaults(traceIn, traceOut, coerce, default coerce('marker.line.width'); }; -},{"../../components/color":1161,"../../components/colorscale/defaults":1170,"../../components/colorscale/has_colorscale":1174}],1390:[function(require,module,exports){ +},{"../../components/color":510,"../../components/colorscale/defaults":519,"../../components/colorscale/has_colorscale":523}],739:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159472,9 +145157,9 @@ module.exports = { fillcolor: scatterAttrs.fillcolor }; -},{"../../components/color/attributes":1160,"../../lib/extend":1254,"../scatter/attributes":1485}],1391:[function(require,module,exports){ +},{"../../components/color/attributes":509,"../../lib/extend":603,"../scatter/attributes":834}],740:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159621,9 +145306,9 @@ module.exports = function calc(gd, trace) { return cd; }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"fast-isnumeric":181}],1392:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"fast-isnumeric":97}],741:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159694,9 +145379,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout } }; -},{"../../components/color":1161,"../../lib":1261,"../../registry":1368,"./attributes":1390}],1393:[function(require,module,exports){ +},{"../../components/color":510,"../../lib":610,"../../registry":717,"./attributes":739}],742:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159803,9 +145488,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { return closeData; }; -},{"../../components/color":1161,"../../lib":1261,"../../plots/cartesian/axes":1293,"../../plots/cartesian/graph_interact":1300}],1394:[function(require,module,exports){ +},{"../../components/color":510,"../../lib":610,"../../plots/cartesian/axes":642,"../../plots/cartesian/graph_interact":649}],743:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159849,9 +145534,9 @@ Box.meta = { module.exports = Box; -},{"../../plots/cartesian":1301,"./attributes":1390,"./calc":1391,"./defaults":1392,"./hover":1393,"./layout_attributes":1395,"./layout_defaults":1396,"./plot":1397,"./set_positions":1398,"./style":1399}],1395:[function(require,module,exports){ +},{"../../plots/cartesian":650,"./attributes":739,"./calc":740,"./defaults":741,"./hover":742,"./layout_attributes":744,"./layout_defaults":745,"./plot":746,"./set_positions":747,"./style":748}],744:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159900,9 +145585,9 @@ module.exports = { } }; -},{}],1396:[function(require,module,exports){ +},{}],745:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -159934,9 +145619,9 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) { coerce('boxgroupgap'); }; -},{"../../lib":1261,"../../registry":1368,"./layout_attributes":1395}],1397:[function(require,module,exports){ +},{"../../lib":610,"../../registry":717,"./layout_attributes":744}],746:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160174,9 +145859,9 @@ module.exports = function plot(gd, plotinfo, cdbox) { }); }; -},{"../../components/drawing":1184,"../../lib":1261,"d3":173}],1398:[function(require,module,exports){ +},{"../../components/drawing":533,"../../lib":610,"d3":86}],747:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160268,9 +145953,9 @@ module.exports = function setPositions(gd, plotinfo) { } }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"../../registry":1368}],1399:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"../../registry":717}],748:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160307,9 +145992,9 @@ module.exports = function style(gd) { }); }; -},{"../../components/color":1161,"../../components/drawing":1184,"d3":173}],1400:[function(require,module,exports){ +},{"../../components/color":510,"../../components/drawing":533,"d3":86}],749:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160365,9 +146050,9 @@ module.exports = { whiskerwidth: Lib.extendFlat({}, boxAttrs.whiskerwidth, { dflt: 0 }) }; -},{"../../lib":1261,"../box/attributes":1390,"../ohlc/attributes":1462}],1401:[function(require,module,exports){ +},{"../../lib":610,"../box/attributes":739,"../ohlc/attributes":811}],750:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160413,9 +146098,9 @@ function handleDirection(traceIn, traceOut, coerce, direction) { coerce(direction + '.fillcolor'); } -},{"../../lib":1261,"../ohlc/direction_defaults":1464,"../ohlc/helpers":1465,"../ohlc/ohlc_defaults":1467,"./attributes":1400}],1402:[function(require,module,exports){ +},{"../../lib":610,"../ohlc/direction_defaults":813,"../ohlc/helpers":814,"../ohlc/ohlc_defaults":816,"./attributes":749}],751:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160455,9 +146140,9 @@ module.exports = { register(require('../box')); register(require('./transform')); -},{"../../plot_api/register":1283,"../../plots/cartesian":1301,"../box":1394,"./attributes":1400,"./defaults":1401,"./transform":1403}],1403:[function(require,module,exports){ +},{"../../plot_api/register":632,"../../plots/cartesian":650,"../box":743,"./attributes":749,"./defaults":750,"./transform":752}],752:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160583,9 +146268,9 @@ exports.calcTransform = function calcTransform(gd, trace, opts) { trace.y = y; }; -},{"../../lib":1261,"../ohlc/helpers":1465}],1404:[function(require,module,exports){ +},{"../../lib":610,"../ohlc/helpers":814}],753:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160634,9 +146319,9 @@ module.exports = extendFlat({}, { { colorbar: colorbarAttrs } ); -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254,"../../plots/attributes":1291,"../scattergeo/attributes":1514}],1405:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603,"../../plots/attributes":640,"../scattergeo/attributes":863}],754:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160653,9 +146338,9 @@ module.exports = function calc(gd, trace) { colorscaleCalc(trace, trace.z, '', 'z'); }; -},{"../../components/colorscale/calc":1167}],1406:[function(require,module,exports){ +},{"../../components/colorscale/calc":516}],755:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160708,9 +146393,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('hoverinfo', (layout._dataLength === 1) ? 'location+z+text' : undefined); }; -},{"../../components/colorscale/defaults":1170,"../../lib":1261,"./attributes":1404}],1407:[function(require,module,exports){ +},{"../../components/colorscale/defaults":519,"../../lib":610,"./attributes":753}],756:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160746,9 +146431,9 @@ Choropleth.meta = { module.exports = Choropleth; -},{"../../plots/geo":1317,"../heatmap/colorbar":1425,"./attributes":1404,"./calc":1405,"./defaults":1406,"./plot":1408}],1408:[function(require,module,exports){ +},{"../../plots/geo":666,"../heatmap/colorbar":774,"./attributes":753,"./calc":754,"./defaults":755,"./plot":757}],757:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -160975,9 +146660,9 @@ function makeEventDataFunc(trace) { }; } -},{"../../components/color":1161,"../../components/colorscale":1175,"../../components/drawing":1184,"../../lib/array_to_calc_item":1249,"../../lib/geo_location_utils":1257,"../../lib/topojson_utils":1277,"../../plots/cartesian/axes":1293,"../../plots/cartesian/graph_interact":1300,"../../plots/geo/constants":1315,"./attributes":1404,"d3":173}],1409:[function(require,module,exports){ +},{"../../components/color":510,"../../components/colorscale":524,"../../components/drawing":533,"../../lib/array_to_calc_item":598,"../../lib/geo_location_utils":606,"../../lib/topojson_utils":626,"../../plots/cartesian/axes":642,"../../plots/cartesian/graph_interact":649,"../../plots/geo/constants":664,"./attributes":753,"d3":86}],758:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161022,13 +146707,15 @@ module.exports = extendFlat({}, { }, ncontours: { valType: 'integer', - dflt: 0, + dflt: 15, + min: 1, role: 'style', description: [ 'Sets the maximum number of contour levels. The actual number', 'of contours will be chosen automatically to be less than or', 'equal to the value of `ncontours`.', - 'Has an effect only if `autocontour` is *true*.' + 'Has an effect only if `autocontour` is *true* or if', + '`contours.size` is missing.' ].join(' ') }, @@ -161037,19 +146724,29 @@ module.exports = extendFlat({}, { valType: 'number', dflt: null, role: 'style', - description: 'Sets the starting contour level value.' + description: [ + 'Sets the starting contour level value.', + 'Must be less than `contours.end`' + ].join(' ') }, end: { valType: 'number', dflt: null, role: 'style', - description: 'Sets the end contour level value.' + description: [ + 'Sets the end contour level value.', + 'Must be more than `contours.start`' + ].join(' ') }, size: { valType: 'number', dflt: null, + min: 0, role: 'style', - description: 'Sets the step between each contour level.' + description: [ + 'Sets the step between each contour level.', + 'Must be positive.' + ].join(' ') }, coloring: { valType: 'enumerated', @@ -161098,9 +146795,9 @@ module.exports = extendFlat({}, { { colorbar: colorbarAttrs } ); -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254,"../heatmap/attributes":1422,"../scatter/attributes":1485}],1410:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603,"../heatmap/attributes":771,"../scatter/attributes":834}],759:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161111,6 +146808,7 @@ module.exports = extendFlat({}, { 'use strict'; var Axes = require('../../plots/cartesian/axes'); +var extendFlat = require('../../lib').extendFlat; var heatmapCalc = require('../heatmap/calc'); @@ -161123,37 +146821,79 @@ module.exports = function calc(gd, trace) { // check if we need to auto-choose contour levels if(trace.autocontour !== false) { - var dummyAx = { - type: 'linear', - range: [trace.zmin, trace.zmax] - }; + var dummyAx = autoContours(trace.zmin, trace.zmax, trace.ncontours); - Axes.autoTicks( - dummyAx, - (trace.zmax - trace.zmin) / (trace.ncontours || 15) - ); + contours.size = dummyAx.dtick; contours.start = Axes.tickFirst(dummyAx); - contours.size = dummyAx.dtick; dummyAx.range.reverse(); contours.end = Axes.tickFirst(dummyAx); if(contours.start === trace.zmin) contours.start += contours.size; if(contours.end === trace.zmax) contours.end -= contours.size; - // so rounding errors don't cause us to miss the last contour - contours.end += contours.size / 100; + // if you set a small ncontours, *and* the ends are exactly on zmin/zmax + // there's an edge case where start > end now. Make sure there's at least + // one meaningful contour, put it midway between the crossed values + if(contours.start > contours.end) { + contours.start = contours.end = (contours.start + contours.end) / 2; + } // copy auto-contour info back to the source data. - trace._input.contours = contours; + trace._input.contours = extendFlat({}, contours); + } + else { + // sanity checks on manually-supplied start/end/size + var start = contours.start, + end = contours.end, + inputContours = trace._input.contours; + + if(start > end) { + contours.start = inputContours.start = end; + end = contours.end = inputContours.end = start; + start = contours.start; + } + + if(!(contours.size > 0)) { + var sizeOut; + if(start === end) sizeOut = 1; + else sizeOut = autoContours(start, end, trace.ncontours).dtick; + + inputContours.size = contours.size = sizeOut; + } } return cd; }; -},{"../../plots/cartesian/axes":1293,"../heatmap/calc":1423}],1411:[function(require,module,exports){ +/* + * autoContours: make a dummy axis object with dtick we can use + * as contours.size, and if needed we can use Axes.tickFirst + * with this axis object to calculate the start and end too + * + * start: the value to start the contours at + * end: the value to end at (must be > start) + * ncontours: max number of contours to make, like roughDTick + * + * returns: an axis object + */ +function autoContours(start, end, ncontours) { + var dummyAx = { + type: 'linear', + range: [start, end] + }; + + Axes.autoTicks( + dummyAx, + (end - start) / (ncontours || 15) + ); + + return dummyAx; +} + +},{"../../lib":610,"../../plots/cartesian/axes":642,"../heatmap/calc":772}],760:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161212,9 +146952,9 @@ module.exports = function colorbar(gd, cd) { .options(trace.colorbar)(); }; -},{"../../components/colorbar/draw":1164,"../../plots/plots":1353,"./make_color_map":1417}],1412:[function(require,module,exports){ +},{"../../components/colorbar/draw":513,"../../plots/plots":702,"./make_color_map":766}],761:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161252,9 +146992,9 @@ module.exports.CHOOSESADDLE = { // substitute to be used up later? module.exports.SADDLEREMAINDER = {1: 4, 2: 8, 4: 1, 7: 13, 8: 2, 11: 14, 13: 7, 14: 11}; -},{}],1413:[function(require,module,exports){ +},{}],762:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161288,17 +147028,26 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout var contourStart = Lib.coerce2(traceIn, traceOut, attributes, 'contours.start'), contourEnd = Lib.coerce2(traceIn, traceOut, attributes, 'contours.end'), - autocontour = coerce('autocontour', !(contourStart && contourEnd)); + missingEnd = (contourStart === false) || (contourEnd === false), - if(autocontour) coerce('ncontours'); - else coerce('contours.size'); + // normally we only need size if autocontour is off. But contour.calc + // pushes its calculated contour size back to the input trace, so for + // things like restyle that can call supplyDefaults without calc + // after the initial draw, we can just reuse the previous calculation + contourSize = coerce('contours.size'), + autoContour; + + if(missingEnd) autoContour = traceOut.autocontour = true; + else autoContour = coerce('autocontour', false); + + if(autoContour || !contourSize) coerce('ncontours'); handleStyleDefaults(traceIn, traceOut, coerce, layout); }; -},{"../../lib":1261,"../contour/style_defaults":1421,"../heatmap/has_columns":1429,"../heatmap/xyz_defaults":1437,"./attributes":1409}],1414:[function(require,module,exports){ +},{"../../lib":610,"../contour/style_defaults":770,"../heatmap/has_columns":778,"../heatmap/xyz_defaults":786,"./attributes":758}],763:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161566,9 +147315,9 @@ function getInterpPx(pi, loc, step) { } } -},{"../../lib":1261,"./constants":1412}],1415:[function(require,module,exports){ +},{"../../lib":610,"./constants":761}],764:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161585,9 +147334,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { return heatmapHoverPoints(pointData, xval, yval, hovermode, true); }; -},{"../heatmap/hover":1430}],1416:[function(require,module,exports){ +},{"../heatmap/hover":779}],765:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161626,9 +147375,9 @@ Contour.meta = { module.exports = Contour; -},{"../../plots/cartesian":1301,"./attributes":1409,"./calc":1410,"./colorbar":1411,"./defaults":1413,"./hover":1415,"./plot":1419,"./style":1420}],1417:[function(require,module,exports){ +},{"../../plots/cartesian":650,"./attributes":758,"./calc":759,"./colorbar":760,"./defaults":762,"./hover":764,"./plot":768,"./style":769}],766:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161704,9 +147453,9 @@ module.exports = function makeColorMap(trace) { }); }; -},{"../../components/colorscale":1175,"d3":173}],1418:[function(require,module,exports){ +},{"../../components/colorscale":524,"d3":86}],767:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161796,9 +147545,9 @@ function getMarchingIndex(val, corners) { return (mi === 15) ? 0 : mi; } -},{"./constants":1412}],1419:[function(require,module,exports){ +},{"./constants":761}],768:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -161879,8 +147628,9 @@ function plotOne(gd, plotinfo, cd) { } function emptyPathinfo(contours, plotinfo, cd0) { - var cs = contours.size || 1, + var cs = contours.size, pathinfo = []; + for(var ci = contours.start; ci < contours.end + cs / 10; ci += cs) { pathinfo.push({ level: ci, @@ -161902,6 +147652,11 @@ function emptyPathinfo(contours, plotinfo, cd0) { z: cd0.z, smoothing: cd0.trace.line.smoothing }); + + if(pathinfo.length > 1000) { + Lib.warn('Too many contours, clipping at 1000', contours); + break; + } } return pathinfo; } @@ -162145,9 +147900,9 @@ function makeClipMask(cd0) { return z; } -},{"../../components/drawing":1184,"../../lib":1261,"../heatmap/plot":1435,"./find_all_paths":1414,"./make_crossings":1418,"d3":173}],1420:[function(require,module,exports){ +},{"../../components/drawing":533,"../../lib":610,"../heatmap/plot":784,"./find_all_paths":763,"./make_crossings":767,"d3":86}],769:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162202,9 +147957,9 @@ module.exports = function style(gd) { heatmapStyle(gd); }; -},{"../../components/drawing":1184,"../heatmap/style":1436,"./make_color_map":1417,"d3":173}],1421:[function(require,module,exports){ +},{"../../components/drawing":533,"../heatmap/style":785,"./make_color_map":766,"d3":86}],770:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162238,9 +147993,9 @@ module.exports = function handleStyleDefaults(traceIn, traceOut, coerce, layout) } }; -},{"../../components/colorscale/defaults":1170}],1422:[function(require,module,exports){ +},{"../../components/colorscale/defaults":519}],771:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162338,9 +148093,9 @@ module.exports = extendFlat({}, { { colorbar: colorbarAttrs } ); -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254,"../scatter/attributes":1485}],1423:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603,"../scatter/attributes":834}],772:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162480,9 +148235,9 @@ module.exports = function calc(gd, trace) { return [cd0]; }; -},{"../../components/colorscale/calc":1167,"../../lib":1261,"../../plots/cartesian/axes":1293,"../../registry":1368,"../histogram2d/calc":1451,"./clean_2d_array":1424,"./convert_column_xyz":1426,"./find_empties":1428,"./has_columns":1429,"./interp2d":1432,"./make_bound_array":1433,"./max_row_length":1434}],1424:[function(require,module,exports){ +},{"../../components/colorscale/calc":516,"../../lib":610,"../../plots/cartesian/axes":642,"../../registry":717,"../histogram2d/calc":800,"./clean_2d_array":773,"./convert_column_xyz":775,"./find_empties":777,"./has_columns":778,"./interp2d":781,"./make_bound_array":782,"./max_row_length":783}],773:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162525,9 +148280,9 @@ module.exports = function clean2dArray(zOld, transpose) { return zNew; }; -},{"fast-isnumeric":181}],1425:[function(require,module,exports){ +},{"fast-isnumeric":97}],774:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162576,9 +148331,9 @@ module.exports = function colorbar(gd, cd) { .options(trace.colorbar)(); }; -},{"../../components/colorbar/draw":1164,"../../components/colorscale":1175,"../../lib":1261,"../../plots/plots":1353,"fast-isnumeric":181}],1426:[function(require,module,exports){ +},{"../../components/colorbar/draw":513,"../../components/colorscale":524,"../../lib":610,"../../plots/plots":702,"fast-isnumeric":97}],775:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162635,9 +148390,9 @@ module.exports = function convertColumnXYZ(trace, xa, ya) { if(hasColumnText) trace.text = text; }; -},{"../../lib":1261}],1427:[function(require,module,exports){ +},{"../../lib":610}],776:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162680,9 +148435,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'}); }; -},{"../../components/colorscale/defaults":1170,"../../lib":1261,"./attributes":1422,"./has_columns":1429,"./xyz_defaults":1437}],1428:[function(require,module,exports){ +},{"../../components/colorscale/defaults":519,"../../lib":610,"./attributes":771,"./has_columns":778,"./xyz_defaults":786}],777:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162786,9 +148541,9 @@ module.exports = function findEmpties(z) { return empties.sort(function(a, b) { return b[2] - a[2]; }); }; -},{"./max_row_length":1434}],1429:[function(require,module,exports){ +},{"./max_row_length":783}],778:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162802,9 +148557,9 @@ module.exports = function(trace) { return !Array.isArray(trace.z[0]); }; -},{}],1430:[function(require,module,exports){ +},{}],779:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162920,9 +148675,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour) })]; }; -},{"../../lib":1261,"../../plots/cartesian/constants":1298,"../../plots/cartesian/graph_interact":1300}],1431:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/constants":647,"../../plots/cartesian/graph_interact":649}],780:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -162975,9 +148730,9 @@ Heatmap.meta = { module.exports = Heatmap; -},{"../../plots/cartesian":1301,"./attributes":1422,"./calc":1423,"./colorbar":1425,"./defaults":1427,"./hover":1430,"./plot":1435,"./style":1436}],1432:[function(require,module,exports){ +},{"../../plots/cartesian":650,"./attributes":771,"./calc":772,"./colorbar":774,"./defaults":776,"./hover":779,"./plot":784,"./style":785}],781:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163107,9 +148862,9 @@ function iterateInterp2d(z, emptyPoints, overshoot) { return maxFractionalChange; } -},{"../../lib":1261}],1433:[function(require,module,exports){ +},{"../../lib":610}],782:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163189,9 +148944,9 @@ module.exports = function makeBoundArray(trace, arrayIn, v0In, dvIn, numbricks, return arrayOut; }; -},{"../../registry":1368}],1434:[function(require,module,exports){ +},{"../../registry":717}],783:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163211,9 +148966,9 @@ module.exports = function maxRowLength(z) { return len; }; -},{}],1435:[function(require,module,exports){ +},{}],784:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163661,9 +149416,9 @@ function plotOne(gd, plotinfo, cd) { image3.exit().remove(); } -},{"../../components/colorscale":1175,"../../constants/xmlns_namespaces":1246,"../../lib":1261,"../../registry":1368,"./max_row_length":1434,"tinycolor2":1129}],1436:[function(require,module,exports){ +},{"../../components/colorscale":524,"../../constants/xmlns_namespaces":595,"../../lib":610,"../../registry":717,"./max_row_length":783,"tinycolor2":985}],785:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163682,9 +149437,9 @@ module.exports = function style(gd) { }); }; -},{"d3":173}],1437:[function(require,module,exports){ +},{"d3":86}],786:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163775,9 +149530,9 @@ function isValidZ(z) { return (allRowsAreArrays && oneRowIsFilled && hasOneNumber); } -},{"../../registry":1368,"./has_columns":1429,"fast-isnumeric":181}],1438:[function(require,module,exports){ +},{"../../registry":717,"./has_columns":778,"fast-isnumeric":97}],787:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163817,9 +149572,9 @@ extendFlat( module.exports = attrs; -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254,"../heatmap/attributes":1422}],1439:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603,"../heatmap/attributes":771}],788:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163956,9 +149711,9 @@ function createHeatmap(scene, fullTrace, calcTrace) { module.exports = createHeatmap; -},{"../../lib/str2rgbarray":1275,"../../plots/cartesian/axes":1293,"gl-heatmap2d":198}],1440:[function(require,module,exports){ +},{"../../lib/str2rgbarray":624,"../../plots/cartesian/axes":642,"gl-heatmap2d":141}],789:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -163989,9 +149744,9 @@ HeatmapGl.meta = { module.exports = HeatmapGl; -},{"../../plots/gl2d":1329,"../heatmap/calc":1423,"../heatmap/colorbar":1425,"../heatmap/defaults":1427,"./attributes":1438,"./convert":1439}],1441:[function(require,module,exports){ +},{"../../plots/gl2d":678,"../heatmap/calc":772,"../heatmap/colorbar":774,"../heatmap/defaults":776,"./attributes":787,"./convert":788}],790:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164153,9 +149908,9 @@ function makeBinsAttr(axLetter) { }; } -},{"../bar/attributes":1378}],1442:[function(require,module,exports){ +},{"../bar/attributes":727}],791:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164179,9 +149934,9 @@ module.exports = function doAvg(size, counts) { return total; }; -},{}],1443:[function(require,module,exports){ +},{}],792:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164212,9 +149967,9 @@ module.exports = function handleBinDefaults(traceIn, traceOut, coerce, binDirect return traceOut; }; -},{}],1444:[function(require,module,exports){ +},{}],793:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164286,9 +150041,9 @@ module.exports = { } }; -},{"fast-isnumeric":181}],1445:[function(require,module,exports){ +},{"fast-isnumeric":97}],794:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164432,9 +150187,9 @@ module.exports = function calc(gd, trace) { return cd; }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"./average":1442,"./bin_functions":1444,"./clean_bins":1446,"./norm_functions":1449,"fast-isnumeric":181}],1446:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"./average":791,"./bin_functions":793,"./clean_bins":795,"./norm_functions":798,"fast-isnumeric":97}],795:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164509,9 +150264,9 @@ module.exports = function cleanBins(trace, ax, binDirection) { if(!trace[autoBinAttr]) delete trace['nbins' + binDirection]; }; -},{"../../constants/numerical":1244,"../../lib":1261,"fast-isnumeric":181}],1447:[function(require,module,exports){ +},{"../../constants/numerical":593,"../../lib":610,"fast-isnumeric":97}],796:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164565,9 +150320,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout errorBarsSupplyDefaults(traceIn, traceOut, Color.defaultLine, {axis: 'x', inherit: 'y'}); }; -},{"../../components/color":1161,"../../components/errorbars/defaults":1189,"../../lib":1261,"../../registry":1368,"../bar/style_defaults":1389,"./attributes":1441,"./bin_defaults":1443}],1448:[function(require,module,exports){ +},{"../../components/color":510,"../../components/errorbars/defaults":538,"../../lib":610,"../../registry":717,"../bar/style_defaults":738,"./attributes":790,"./bin_defaults":792}],797:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164620,9 +150375,9 @@ Histogram.meta = { module.exports = Histogram; -},{"../../plots/cartesian":1301,"../bar/hover":1381,"../bar/layout_attributes":1383,"../bar/layout_defaults":1384,"../bar/plot":1385,"../bar/set_positions":1386,"../bar/style":1388,"../scatter/colorbar":1488,"./attributes":1441,"./calc":1445,"./defaults":1447}],1449:[function(require,module,exports){ +},{"../../plots/cartesian":650,"../bar/hover":730,"../bar/layout_attributes":732,"../bar/layout_defaults":733,"../bar/plot":734,"../bar/set_positions":735,"../bar/style":737,"../scatter/colorbar":837,"./attributes":790,"./calc":794,"./defaults":796}],798:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164655,9 +150410,9 @@ module.exports = { } }; -},{}],1450:[function(require,module,exports){ +},{}],799:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164707,9 +150462,9 @@ module.exports = extendFlat({}, { colorbar: colorbarAttrs } ); -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254,"../heatmap/attributes":1422,"../histogram/attributes":1441}],1451:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603,"../heatmap/attributes":771,"../histogram/attributes":790}],800:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164910,9 +150665,9 @@ module.exports = function calc(gd, trace) { }; }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"../histogram/average":1442,"../histogram/bin_functions":1444,"../histogram/clean_bins":1446,"../histogram/norm_functions":1449}],1452:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"../histogram/average":791,"../histogram/bin_functions":793,"../histogram/clean_bins":795,"../histogram/norm_functions":798}],801:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164948,9 +150703,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout ); }; -},{"../../components/colorscale/defaults":1170,"../../lib":1261,"./attributes":1450,"./sample_defaults":1454}],1453:[function(require,module,exports){ +},{"../../components/colorscale/defaults":519,"../../lib":610,"./attributes":799,"./sample_defaults":803}],802:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -164988,9 +150743,9 @@ Histogram2D.meta = { module.exports = Histogram2D; -},{"../../plots/cartesian":1301,"../heatmap/calc":1423,"../heatmap/colorbar":1425,"../heatmap/hover":1430,"../heatmap/plot":1435,"../heatmap/style":1436,"./attributes":1450,"./defaults":1452}],1454:[function(require,module,exports){ +},{"../../plots/cartesian":650,"../heatmap/calc":772,"../heatmap/colorbar":774,"../heatmap/hover":779,"../heatmap/plot":784,"../heatmap/style":785,"./attributes":799,"./defaults":801}],803:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165028,9 +150783,9 @@ module.exports = function handleSampleDefaults(traceIn, traceOut, coerce, layout handleBinDefaults(traceIn, traceOut, coerce, binDirections); }; -},{"../../registry":1368,"../histogram/bin_defaults":1443}],1455:[function(require,module,exports){ +},{"../../registry":717,"../histogram/bin_defaults":792}],804:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165070,9 +150825,9 @@ module.exports = extendFlat({}, { { colorbar: colorbarAttrs } ); -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254,"../contour/attributes":1409,"../histogram2d/attributes":1450}],1456:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603,"../contour/attributes":758,"../histogram2d/attributes":799}],805:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165106,9 +150861,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout handleStyleDefaults(traceIn, traceOut, coerce, layout); }; -},{"../../lib":1261,"../contour/style_defaults":1421,"../histogram2d/sample_defaults":1454,"./attributes":1455}],1457:[function(require,module,exports){ +},{"../../lib":610,"../contour/style_defaults":770,"../histogram2d/sample_defaults":803,"./attributes":804}],806:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165146,9 +150901,9 @@ Histogram2dContour.meta = { module.exports = Histogram2dContour; -},{"../../plots/cartesian":1301,"../contour/calc":1410,"../contour/colorbar":1411,"../contour/hover":1415,"../contour/plot":1419,"../contour/style":1420,"./attributes":1455,"./defaults":1456}],1458:[function(require,module,exports){ +},{"../../plots/cartesian":650,"../contour/calc":759,"../contour/colorbar":760,"../contour/hover":764,"../contour/plot":768,"../contour/style":769,"./attributes":804,"./defaults":805}],807:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165344,9 +151099,9 @@ module.exports = { }, surfaceAtts.lighting) }; -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254,"../surface/attributes":1541}],1459:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603,"../surface/attributes":890}],808:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165506,9 +151261,9 @@ function createMesh3DTrace(scene, data) { module.exports = createMesh3DTrace; -},{"../../lib/str2rgbarray":1275,"alpha-shape":143,"convex-hull":162,"delaunay-triangulate":179,"gl-mesh3d":248,"tinycolor2":1129}],1460:[function(require,module,exports){ +},{"../../lib/str2rgbarray":624,"alpha-shape":7,"convex-hull":74,"delaunay-triangulate":88,"gl-mesh3d":180,"tinycolor2":985}],809:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165607,9 +151362,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout } }; -},{"../../components/colorbar/defaults":1163,"../../lib":1261,"../../registry":1368,"./attributes":1458}],1461:[function(require,module,exports){ +},{"../../components/colorbar/defaults":512,"../../lib":610,"../../registry":717,"./attributes":807}],810:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165643,9 +151398,9 @@ Mesh3D.meta = { module.exports = Mesh3D; -},{"../../plots/gl3d":1332,"../heatmap/colorbar":1425,"./attributes":1458,"./convert":1459,"./defaults":1460}],1462:[function(require,module,exports){ +},{"../../plots/gl3d":681,"../heatmap/colorbar":774,"./attributes":807,"./convert":808,"./defaults":809}],811:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165778,9 +151533,9 @@ module.exports = { } }; -},{"../../lib":1261,"../scatter/attributes":1485}],1463:[function(require,module,exports){ +},{"../../lib":610,"../scatter/attributes":834}],812:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165827,9 +151582,9 @@ function handleDirection(traceIn, traceOut, coerce, direction) { coerce(direction + '.line.dash', traceOut.line.dash); } -},{"../../lib":1261,"./attributes":1462,"./direction_defaults":1464,"./helpers":1465,"./ohlc_defaults":1467}],1464:[function(require,module,exports){ +},{"../../lib":610,"./attributes":811,"./direction_defaults":813,"./helpers":814,"./ohlc_defaults":816}],813:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165853,9 +151608,9 @@ module.exports = function handleDirectionDefaults(traceIn, traceOut, coerce, dir coerce(direction + '.name', nameDflt); }; -},{}],1465:[function(require,module,exports){ +},{}],814:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -165965,9 +151720,9 @@ exports.addRangeSlider = function(layout) { if(!layout.xaxis.rangeslider) layout.xaxis.rangeslider = {}; }; -},{"../../lib":1261}],1466:[function(require,module,exports){ +},{"../../lib":610}],815:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166007,9 +151762,9 @@ module.exports = { register(require('../scatter')); register(require('./transform')); -},{"../../plot_api/register":1283,"../../plots/cartesian":1301,"../scatter":1495,"./attributes":1462,"./defaults":1463,"./transform":1468}],1467:[function(require,module,exports){ +},{"../../plot_api/register":632,"../../plots/cartesian":650,"../scatter":844,"./attributes":811,"./defaults":812,"./transform":817}],816:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166049,9 +151804,9 @@ module.exports = function handleOHLC(traceIn, traceOut, coerce, layout) { return len; }; -},{"../../registry":1368}],1468:[function(require,module,exports){ +},{"../../registry":717}],817:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166308,9 +152063,9 @@ function convertTickWidth(gd, xa, trace) { return minDiff * tickWidth; } -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"../../plots/cartesian/axis_ids":1296,"./helpers":1465}],1469:[function(require,module,exports){ +},{"../../lib":610,"../../plots/cartesian/axes":642,"../../plots/cartesian/axis_ids":645,"./helpers":814}],818:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166538,9 +152293,9 @@ module.exports = { } }; -},{"../../components/color/attributes":1160,"../../lib/extend":1254,"../../plots/attributes":1291,"../../plots/font_attributes":1313}],1470:[function(require,module,exports){ +},{"../../components/color/attributes":509,"../../lib/extend":603,"../../plots/attributes":640,"../../plots/font_attributes":662}],819:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166585,9 +152340,9 @@ function getCdModule(calcdata, _module) { return cdModule; } -},{"../../registry":1368}],1471:[function(require,module,exports){ +},{"../../registry":717}],820:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166737,9 +152492,9 @@ function nextDefaultColor(index) { return pieDefaultColors[index % pieDefaultColors.length]; } -},{"../../components/color":1161,"./helpers":1473,"fast-isnumeric":181,"tinycolor2":1129}],1472:[function(require,module,exports){ +},{"../../components/color":510,"./helpers":822,"fast-isnumeric":97,"tinycolor2":985}],821:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166821,9 +152576,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('pull'); }; -},{"../../lib":1261,"./attributes":1469}],1473:[function(require,module,exports){ +},{"../../lib":610,"./attributes":818}],822:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166850,9 +152605,9 @@ exports.formatPieValue = function formatPieValue(v, separators) { return Lib.numSeparate(vRounded, separators); }; -},{"../../lib":1261}],1474:[function(require,module,exports){ +},{"../../lib":610}],823:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166886,9 +152641,9 @@ Pie.meta = { module.exports = Pie; -},{"./attributes":1469,"./base_plot":1470,"./calc":1471,"./defaults":1472,"./layout_attributes":1475,"./layout_defaults":1476,"./plot":1477,"./style":1478,"./style_one":1479}],1475:[function(require,module,exports){ +},{"./attributes":818,"./base_plot":819,"./calc":820,"./defaults":821,"./layout_attributes":824,"./layout_defaults":825,"./plot":826,"./style":827,"./style_one":828}],824:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166906,9 +152661,9 @@ module.exports = { hiddenlabels: {valType: 'data_array'} }; -},{}],1476:[function(require,module,exports){ +},{}],825:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -166928,9 +152683,9 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) { coerce('hiddenlabels'); }; -},{"../../lib":1261,"./layout_attributes":1475}],1477:[function(require,module,exports){ +},{"../../lib":610,"./layout_attributes":824}],826:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -167623,9 +153378,9 @@ function maxExtent(tilt, tiltAxisFraction, depth) { 2 * Math.sqrt(1 - sinTilt * sinTilt * tiltAxisFraction * tiltAxisFraction)); } -},{"../../components/color":1161,"../../components/drawing":1184,"../../lib/svg_text_utils":1276,"../../plots/cartesian/graph_interact":1300,"./helpers":1473,"d3":173}],1478:[function(require,module,exports){ +},{"../../components/color":510,"../../components/drawing":533,"../../lib/svg_text_utils":625,"../../plots/cartesian/graph_interact":649,"./helpers":822,"d3":86}],827:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -167652,9 +153407,9 @@ module.exports = function style(gd) { }); }; -},{"./style_one":1479,"d3":173}],1479:[function(require,module,exports){ +},{"./style_one":828,"d3":86}],828:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -167679,9 +153434,9 @@ module.exports = function styleOne(s, pt, trace) { .call(Color.stroke, lineColor); }; -},{"../../components/color":1161}],1480:[function(require,module,exports){ +},{"../../components/color":510}],829:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -167813,9 +153568,9 @@ module.exports = { } }; -},{"../scattergl/attributes":1521}],1481:[function(require,module,exports){ +},{"../scattergl/attributes":870}],830:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168045,9 +153800,9 @@ function createPointcloud(scene, data) { module.exports = createPointcloud; -},{"../../lib/str2rgbarray":1275,"../scatter/get_trace_color":1493,"gl-pointcloud2d":601}],1482:[function(require,module,exports){ +},{"../../lib/str2rgbarray":624,"../scatter/get_trace_color":842,"gl-pointcloud2d":205}],831:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168090,9 +153845,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor) { coerce('marker.border.arearatio'); }; -},{"../../lib":1261,"./attributes":1480}],1483:[function(require,module,exports){ +},{"../../lib":610,"./attributes":829}],832:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168123,9 +153878,9 @@ pointcloud.meta = { module.exports = pointcloud; -},{"../../plots/gl2d":1329,"../scatter3d/calc":1509,"./attributes":1480,"./convert":1481,"./defaults":1482}],1484:[function(require,module,exports){ +},{"../../plots/gl2d":678,"../scatter3d/calc":858,"./attributes":829,"./convert":830,"./defaults":831}],833:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168139,9 +153894,7 @@ var Lib = require('../../lib'); // arrayOk attributes, merge them into calcdata array -module.exports = function arraysToCalcdata(cd) { - var trace = cd[0].trace, - marker = trace.marker; +module.exports = function arraysToCalcdata(cd, trace) { Lib.mergeArray(trace.text, cd, 'tx'); Lib.mergeArray(trace.textposition, cd, 'tp'); @@ -168151,19 +153904,24 @@ module.exports = function arraysToCalcdata(cd) { Lib.mergeArray(trace.textfont.family, cd, 'tf'); } - if(marker && marker.line) { - var markerLine = marker.line; + var marker = trace.marker; + if(marker) { + Lib.mergeArray(marker.size, cd, 'ms'); Lib.mergeArray(marker.opacity, cd, 'mo'); Lib.mergeArray(marker.symbol, cd, 'mx'); Lib.mergeArray(marker.color, cd, 'mc'); - Lib.mergeArray(markerLine.color, cd, 'mlc'); - Lib.mergeArray(markerLine.width, cd, 'mlw'); + + var markerLine = marker.line; + if(marker.line) { + Lib.mergeArray(markerLine.color, cd, 'mlc'); + Lib.mergeArray(markerLine.width, cd, 'mlw'); + } } }; -},{"../../lib":1261}],1485:[function(require,module,exports){ +},{"../../lib":610}],834:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168527,9 +154285,9 @@ module.exports = { error_x: errorBarAttrs }; -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/color_attributes":1168,"../../components/drawing":1184,"../../components/errorbars/attributes":1186,"../../lib/extend":1254,"./constants":1490}],1486:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/color_attributes":517,"../../components/drawing":533,"../../components/errorbars/attributes":535,"../../lib/extend":603,"./constants":839}],835:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168542,10 +154300,10 @@ module.exports = { var isNumeric = require('fast-isnumeric'); var Axes = require('../../plots/cartesian/axes'); -var Lib = require('../../lib'); var subTypes = require('./subtypes'); var calcColorscale = require('./colorscale_calc'); +var arraysToCalcdata = require('./arrays_to_calcdata'); module.exports = function calc(gd, trace) { @@ -168651,16 +154409,15 @@ module.exports = function calc(gd, trace) { } } - // this has migrated up from arraysToCalcdata as we have a reference to 's' here - if(typeof s !== 'undefined') Lib.mergeArray(s, cd, 'ms'); + arraysToCalcdata(cd, trace); gd.firstscatter = false; return cd; }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"./colorscale_calc":1489,"./subtypes":1505,"fast-isnumeric":181}],1487:[function(require,module,exports){ +},{"../../plots/cartesian/axes":642,"./arrays_to_calcdata":833,"./colorscale_calc":838,"./subtypes":854,"fast-isnumeric":97}],836:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168697,9 +154454,9 @@ module.exports = function cleanData(fullData) { } }; -},{}],1488:[function(require,module,exports){ +},{}],837:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168754,9 +154511,9 @@ module.exports = function colorbar(gd, cd) { .options(marker.colorbar)(); }; -},{"../../components/colorbar/draw":1164,"../../components/colorscale":1175,"../../lib":1261,"../../plots/plots":1353,"fast-isnumeric":181}],1489:[function(require,module,exports){ +},{"../../components/colorbar/draw":513,"../../components/colorscale":524,"../../lib":610,"../../plots/plots":702,"fast-isnumeric":97}],838:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168787,9 +154544,9 @@ module.exports = function calcMarkerColorscale(trace) { } }; -},{"../../components/colorscale/calc":1167,"../../components/colorscale/has_colorscale":1174,"./subtypes":1505}],1490:[function(require,module,exports){ +},{"../../components/colorscale/calc":516,"../../components/colorscale/has_colorscale":523,"./subtypes":854}],839:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168803,9 +154560,9 @@ module.exports = { PTS_LINESONLY: 20 }; -},{}],1491:[function(require,module,exports){ +},{}],840:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168883,9 +154640,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout errorBarsSupplyDefaults(traceIn, traceOut, defaultColor, {axis: 'x', inherit: 'y'}); }; -},{"../../components/errorbars/defaults":1189,"../../lib":1261,"./attributes":1485,"./constants":1490,"./fillcolor_defaults":1492,"./line_defaults":1496,"./line_shape_defaults":1498,"./marker_defaults":1501,"./subtypes":1505,"./text_defaults":1506,"./xy_defaults":1507}],1492:[function(require,module,exports){ +},{"../../components/errorbars/defaults":538,"../../lib":610,"./attributes":834,"./constants":839,"./fillcolor_defaults":841,"./line_defaults":845,"./line_shape_defaults":847,"./marker_defaults":850,"./subtypes":854,"./text_defaults":855,"./xy_defaults":856}],841:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168921,9 +154678,9 @@ module.exports = function fillColorDefaults(traceIn, traceOut, defaultColor, coe )); }; -},{"../../components/color":1161}],1493:[function(require,module,exports){ +},{"../../components/color":510}],842:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -168974,9 +154731,9 @@ module.exports = function getTraceColor(trace, di) { } }; -},{"../../components/color":1161,"./subtypes":1505}],1494:[function(require,module,exports){ +},{"../../components/color":510,"./subtypes":854}],843:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169143,9 +154900,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { } }; -},{"../../components/color":1161,"../../components/errorbars":1190,"../../lib":1261,"../../plots/cartesian/constants":1298,"../../plots/cartesian/graph_interact":1300,"./get_trace_color":1493}],1495:[function(require,module,exports){ +},{"../../components/color":510,"../../components/errorbars":539,"../../lib":610,"../../plots/cartesian/constants":647,"../../plots/cartesian/graph_interact":649,"./get_trace_color":842}],844:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169193,9 +154950,9 @@ Scatter.meta = { module.exports = Scatter; -},{"../../plots/cartesian":1301,"./arrays_to_calcdata":1484,"./attributes":1485,"./calc":1486,"./clean_data":1487,"./colorbar":1488,"./defaults":1491,"./hover":1494,"./plot":1502,"./select":1503,"./style":1504,"./subtypes":1505}],1496:[function(require,module,exports){ +},{"../../plots/cartesian":650,"./arrays_to_calcdata":833,"./attributes":834,"./calc":835,"./clean_data":836,"./colorbar":837,"./defaults":840,"./hover":843,"./plot":851,"./select":852,"./style":853,"./subtypes":854}],845:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169226,9 +154983,9 @@ module.exports = function lineDefaults(traceIn, traceOut, defaultColor, layout, coerce('line.dash'); }; -},{"../../components/colorscale/defaults":1170,"../../components/colorscale/has_colorscale":1174}],1497:[function(require,module,exports){ +},{"../../components/colorscale/defaults":519,"../../components/colorscale/has_colorscale":523}],846:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169399,9 +155156,9 @@ module.exports = function linePoints(d, opts) { return segments; }; -},{"../../constants/numerical":1244}],1498:[function(require,module,exports){ +},{"../../constants/numerical":593}],847:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169418,9 +155175,9 @@ module.exports = function handleLineShapeDefaults(traceIn, traceOut, coerce) { if(shape === 'spline') coerce('line.smoothing'); }; -},{}],1499:[function(require,module,exports){ +},{}],848:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169459,9 +155216,9 @@ module.exports = function linkTraces(gd, plotinfo, cdscatter) { } }; -},{}],1500:[function(require,module,exports){ +},{}],849:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169501,9 +155258,9 @@ module.exports = function makeBubbleSizeFn(trace) { }; }; -},{"fast-isnumeric":181}],1501:[function(require,module,exports){ +},{"fast-isnumeric":97}],850:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169561,9 +155318,9 @@ module.exports = function markerDefaults(traceIn, traceOut, defaultColor, layout } }; -},{"../../components/color":1161,"../../components/colorscale/defaults":1170,"../../components/colorscale/has_colorscale":1174,"./subtypes":1505}],1502:[function(require,module,exports){ +},{"../../components/color":510,"../../components/colorscale/defaults":519,"../../components/colorscale/has_colorscale":523,"./subtypes":854}],851:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -169580,7 +155337,6 @@ var Drawing = require('../../components/drawing'); var ErrorBars = require('../../components/errorbars'); var subTypes = require('./subtypes'); -var arraysToCalcdata = require('./arrays_to_calcdata'); var linePoints = require('./line_points'); var linkTraces = require('./link_traces'); var polygonTester = require('../../lib/polygon').tester; @@ -169743,8 +155499,6 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition // store node for tweaking by selectPoints cdscatter[0].node3 = tr; - arraysToCalcdata(cdscatter); - var prevRevpath = ''; var prevPolygons = []; var prevtrace = trace._prevtrace; @@ -170099,9 +155853,9 @@ function selectMarkers(gd, idx, plotinfo, cdscatter, cdscatterAll) { }); } -},{"../../components/drawing":1184,"../../components/errorbars":1190,"../../lib":1261,"../../lib/polygon":1269,"./arrays_to_calcdata":1484,"./line_points":1497,"./link_traces":1499,"./subtypes":1505,"d3":173}],1503:[function(require,module,exports){ +},{"../../components/drawing":533,"../../components/errorbars":539,"../../lib":610,"../../lib/polygon":618,"./line_points":846,"./link_traces":848,"./subtypes":854,"d3":86}],852:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170171,9 +155925,9 @@ module.exports = function selectPoints(searchInfo, polygon) { return selection; }; -},{"./subtypes":1505}],1504:[function(require,module,exports){ +},{"./subtypes":854}],853:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170213,9 +155967,9 @@ module.exports = function style(gd) { s.call(ErrorBars.style); }; -},{"../../components/drawing":1184,"../../components/errorbars":1190,"d3":173}],1505:[function(require,module,exports){ +},{"../../components/drawing":533,"../../components/errorbars":539,"d3":86}],854:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170249,9 +156003,9 @@ module.exports = { } }; -},{"../../lib":1261}],1506:[function(require,module,exports){ +},{"../../lib":610}],855:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170270,9 +156024,9 @@ module.exports = function(traceIn, traceOut, layout, coerce) { Lib.coerceFont(coerce, 'textfont', layout.font); }; -},{"../../lib":1261}],1507:[function(require,module,exports){ +},{"../../lib":610}],856:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170320,9 +156074,9 @@ module.exports = function handleXYDefaults(traceIn, traceOut, layout, coerce) { return len; }; -},{"../../registry":1368}],1508:[function(require,module,exports){ +},{"../../registry":717}],857:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170480,9 +156234,9 @@ module.exports = { error_z: errorBarAttrs, }; -},{"../../components/colorscale/color_attributes":1168,"../../components/errorbars/attributes":1186,"../../constants/gl_markers":1243,"../../lib/extend":1254,"../scatter/attributes":1485}],1509:[function(require,module,exports){ +},{"../../components/colorscale/color_attributes":517,"../../components/errorbars/attributes":535,"../../constants/gl_markers":592,"../../lib/extend":603,"../scatter/attributes":834}],858:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170503,15 +156257,15 @@ var calcColorscales = require('../scatter/colorscale_calc'); module.exports = function calc(gd, trace) { var cd = [{x: false, y: false, trace: trace, t: {}}]; - arraysToCalcdata(cd); + arraysToCalcdata(cd, trace); calcColorscales(trace); return cd; }; -},{"../scatter/arrays_to_calcdata":1484,"../scatter/colorscale_calc":1489}],1510:[function(require,module,exports){ +},{"../scatter/arrays_to_calcdata":833,"../scatter/colorscale_calc":838}],859:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -170580,9 +156334,9 @@ function calculateErrors(data, scaleFactor) { module.exports = calculateErrors; -},{"../../components/errorbars/compute_error":1188}],1511:[function(require,module,exports){ +},{"../../components/errorbars/compute_error":537}],860:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171042,9 +156796,9 @@ function createLineWithMarkers(scene, data) { module.exports = createLineWithMarkers; -},{"../../constants/gl3d_dashes":1242,"../../constants/gl_markers":1243,"../../lib":1261,"../../lib/gl_format_color":1259,"../../lib/str2rgbarray":1275,"../scatter/make_bubble_size_func":1500,"./calc_errors":1510,"delaunay-triangulate":179,"gl-error3d":188,"gl-line3d":214,"gl-mesh3d":248,"gl-scatter3d":828}],1512:[function(require,module,exports){ +},{"../../constants/gl3d_dashes":591,"../../constants/gl_markers":592,"../../lib":610,"../../lib/gl_format_color":608,"../../lib/str2rgbarray":624,"../scatter/make_bubble_size_func":849,"./calc_errors":859,"delaunay-triangulate":88,"gl-error3d":137,"gl-line3d":147,"gl-mesh3d":180,"gl-scatter3d":223}],861:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171131,9 +156885,9 @@ function handleXYZDefaults(traceIn, traceOut, coerce, layout) { return len; } -},{"../../components/errorbars/defaults":1189,"../../lib":1261,"../../registry":1368,"../scatter/line_defaults":1496,"../scatter/marker_defaults":1501,"../scatter/subtypes":1505,"../scatter/text_defaults":1506,"./attributes":1508}],1513:[function(require,module,exports){ +},{"../../components/errorbars/defaults":538,"../../lib":610,"../../registry":717,"../scatter/line_defaults":845,"../scatter/marker_defaults":850,"../scatter/subtypes":854,"../scatter/text_defaults":855,"./attributes":857}],862:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171169,9 +156923,9 @@ Scatter3D.meta = { module.exports = Scatter3D; -},{"../../constants/gl_markers":1243,"../../plots/gl3d":1332,"../scatter/colorbar":1488,"./attributes":1508,"./calc":1509,"./convert":1511,"./defaults":1512}],1514:[function(require,module,exports){ +},{"../../constants/gl_markers":592,"../../plots/gl3d":681,"../scatter/colorbar":837,"./attributes":857,"./calc":858,"./convert":860,"./defaults":861}],863:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171277,9 +157031,9 @@ module.exports = { }) }; -},{"../../components/colorscale/color_attributes":1168,"../../lib/extend":1254,"../../plots/attributes":1291,"../scatter/attributes":1485}],1515:[function(require,module,exports){ +},{"../../components/colorscale/color_attributes":517,"../../lib/extend":603,"../../plots/attributes":640,"../scatter/attributes":834}],864:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171334,9 +157088,9 @@ module.exports = function calc(gd, trace) { return calcTrace; }; -},{"../scatter/colorscale_calc":1489,"fast-isnumeric":181}],1516:[function(require,module,exports){ +},{"../scatter/colorscale_calc":838,"fast-isnumeric":97}],865:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171414,9 +157168,9 @@ function handleLonLatLocDefaults(traceIn, traceOut, coerce) { return len; } -},{"../../lib":1261,"../scatter/fillcolor_defaults":1492,"../scatter/line_defaults":1496,"../scatter/marker_defaults":1501,"../scatter/subtypes":1505,"../scatter/text_defaults":1506,"./attributes":1514}],1517:[function(require,module,exports){ +},{"../../lib":610,"../scatter/fillcolor_defaults":841,"../scatter/line_defaults":845,"../scatter/marker_defaults":850,"../scatter/subtypes":854,"../scatter/text_defaults":855,"./attributes":863}],866:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171435,9 +157189,9 @@ module.exports = function eventData(out, pt) { return out; }; -},{}],1518:[function(require,module,exports){ +},{}],867:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171540,14 +157294,17 @@ function getExtraText(trace, pt, axis) { else if(hasLon) text.push('lon: ' + format(pt.lonlat[0])); else if(hasLat) text.push('lat: ' + format(pt.lonlat[1])); - if(hasText) text.push(pt.tx || trace.text); + if(hasText) { + var tx = pt.tx || trace.text; + if(!Array.isArray(tx)) text.push(tx); + } return text.join('
'); } -},{"../../plots/cartesian/axes":1293,"../../plots/cartesian/graph_interact":1300,"../scatter/get_trace_color":1493,"./attributes":1514}],1519:[function(require,module,exports){ +},{"../../plots/cartesian/axes":642,"../../plots/cartesian/graph_interact":649,"../scatter/get_trace_color":842,"./attributes":863}],868:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171582,9 +157339,9 @@ ScatterGeo.meta = { module.exports = ScatterGeo; -},{"../../plots/geo":1317,"../scatter/colorbar":1488,"./attributes":1514,"./calc":1515,"./defaults":1516,"./event_data":1517,"./hover":1518,"./plot":1520}],1520:[function(require,module,exports){ +},{"../../plots/geo":666,"../scatter/colorbar":837,"./attributes":863,"./calc":864,"./defaults":865,"./event_data":866,"./hover":867,"./plot":869}],869:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171754,9 +157511,9 @@ function style(geo) { }); } -},{"../../components/color":1161,"../../components/drawing":1184,"../../lib":1261,"../../lib/array_to_calc_item":1249,"../../lib/geo_location_utils":1257,"../../lib/geojson_utils":1258,"../../lib/topojson_utils":1277,"../scatter/subtypes":1505,"d3":173}],1521:[function(require,module,exports){ +},{"../../components/color":510,"../../components/drawing":533,"../../lib":610,"../../lib/array_to_calc_item":598,"../../lib/geo_location_utils":606,"../../lib/geojson_utils":607,"../../lib/topojson_utils":626,"../scatter/subtypes":854,"d3":86}],870:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -171844,9 +157601,9 @@ module.exports = { error_x: scatterAttrs.error_x }; -},{"../../components/colorscale/color_attributes":1168,"../../constants/gl2d_dashes":1241,"../../constants/gl_markers":1243,"../../lib/extend":1254,"../scatter/attributes":1485}],1522:[function(require,module,exports){ +},{"../../components/colorscale/color_attributes":517,"../../constants/gl2d_dashes":590,"../../constants/gl_markers":592,"../../lib/extend":603,"../scatter/attributes":834}],871:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -172459,9 +158216,9 @@ function createLineWithMarkers(scene, data) { module.exports = createLineWithMarkers; -},{"../../components/errorbars":1190,"../../constants/gl2d_dashes":1241,"../../constants/gl_markers":1243,"../../lib":1261,"../../lib/gl_format_color":1259,"../../lib/str2rgbarray":1275,"../../lib/typed_array_truncate":1278,"../../plots/cartesian/axes":1293,"../../plots/cartesian/axis_autotype":1294,"../scatter/get_trace_color":1493,"../scatter/make_bubble_size_func":1500,"../scatter/subtypes":1505,"fast-isnumeric":181,"gl-error2d":182,"gl-line2d":207,"gl-scatter2d":730,"gl-scatter2d-fancy":721}],1523:[function(require,module,exports){ +},{"../../components/errorbars":539,"../../constants/gl2d_dashes":590,"../../constants/gl_markers":592,"../../lib":610,"../../lib/gl_format_color":608,"../../lib/str2rgbarray":624,"../../lib/typed_array_truncate":627,"../../plots/cartesian/axes":642,"../../plots/cartesian/axis_autotype":643,"../scatter/get_trace_color":842,"../scatter/make_bubble_size_func":849,"../scatter/subtypes":854,"fast-isnumeric":97,"gl-error2d":135,"gl-line2d":145,"gl-scatter2d":220,"gl-scatter2d-fancy":215}],872:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -172516,9 +158273,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout errorBarsSupplyDefaults(traceIn, traceOut, defaultColor, {axis: 'x', inherit: 'y'}); }; -},{"../../components/errorbars/defaults":1189,"../../lib":1261,"../scatter/constants":1490,"../scatter/fillcolor_defaults":1492,"../scatter/line_defaults":1496,"../scatter/marker_defaults":1501,"../scatter/subtypes":1505,"../scatter/xy_defaults":1507,"./attributes":1521}],1524:[function(require,module,exports){ +},{"../../components/errorbars/defaults":538,"../../lib":610,"../scatter/constants":839,"../scatter/fillcolor_defaults":841,"../scatter/line_defaults":845,"../scatter/marker_defaults":850,"../scatter/subtypes":854,"../scatter/xy_defaults":856,"./attributes":870}],873:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -172552,9 +158309,9 @@ ScatterGl.meta = { module.exports = ScatterGl; -},{"../../plots/gl2d":1329,"../scatter/colorbar":1488,"../scatter3d/calc":1509,"./attributes":1521,"./convert":1522,"./defaults":1523}],1525:[function(require,module,exports){ +},{"../../plots/gl2d":678,"../scatter/colorbar":837,"../scatter3d/calc":858,"./attributes":870,"./convert":871,"./defaults":872}],874:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -172660,9 +158417,9 @@ module.exports = { }), }; -},{"../../components/colorbar/attributes":1162,"../../lib/extend":1254,"../../plots/attributes":1291,"../../plots/mapbox/layout_attributes":1349,"../scatter/attributes":1485,"../scattergeo/attributes":1514}],1526:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../lib/extend":603,"../../plots/attributes":640,"../../plots/mapbox/layout_attributes":698,"../scatter/attributes":834,"../scattergeo/attributes":863}],875:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -172763,9 +158520,9 @@ module.exports = function calc(gd, trace) { return calcTrace; }; -},{"../../components/colorscale":1175,"../../lib":1261,"../scatter/colorscale_calc":1489,"../scatter/make_bubble_size_func":1500,"../scatter/subtypes":1505,"fast-isnumeric":181}],1527:[function(require,module,exports){ +},{"../../components/colorscale":524,"../../lib":610,"../scatter/colorscale_calc":838,"../scatter/make_bubble_size_func":849,"../scatter/subtypes":854,"fast-isnumeric":97}],876:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173072,9 +158829,9 @@ function getFillFunc(attr) { function blankFillFunc() { return ''; } -},{"../../lib":1261,"../../lib/geojson_utils":1258,"../../plots/mapbox/convert_text_opts":1346,"../scatter/subtypes":1505}],1528:[function(require,module,exports){ +},{"../../lib":610,"../../lib/geojson_utils":607,"../../plots/mapbox/convert_text_opts":695,"../scatter/subtypes":854}],877:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173160,9 +158917,9 @@ function handleLonLatDefaults(traceIn, traceOut, coerce) { return len; } -},{"../../lib":1261,"../scatter/attributes":1485,"../scatter/fillcolor_defaults":1492,"../scatter/line_defaults":1496,"../scatter/marker_defaults":1501,"../scatter/subtypes":1505,"../scatter/text_defaults":1506,"./attributes":1525}],1529:[function(require,module,exports){ +},{"../../lib":610,"../scatter/attributes":834,"../scatter/fillcolor_defaults":841,"../scatter/line_defaults":845,"../scatter/marker_defaults":850,"../scatter/subtypes":854,"../scatter/text_defaults":855,"./attributes":874}],878:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173180,9 +158937,9 @@ module.exports = function eventData(out, pt) { return out; }; -},{}],1530:[function(require,module,exports){ +},{}],879:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173269,15 +159026,16 @@ function getExtraText(trace, di) { else if(hasLat) text.push('lat: ' + format(lonlat[1])); if(isAll || hoverinfo.indexOf('text') !== -1) { - text.push(di.tx || trace.text); + var tx = di.tx || trace.text; + if(!Array.isArray(tx)) text.push(tx); } return text.join('
'); } -},{"../../plots/cartesian/graph_interact":1300,"../scatter/get_trace_color":1493}],1531:[function(require,module,exports){ +},{"../../plots/cartesian/graph_interact":649,"../scatter/get_trace_color":842}],880:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173312,9 +159070,9 @@ ScatterMapbox.meta = { module.exports = ScatterMapbox; -},{"../../plots/mapbox":1347,"../scatter/colorbar":1488,"./attributes":1525,"./calc":1526,"./defaults":1528,"./event_data":1529,"./hover":1530,"./plot":1532}],1532:[function(require,module,exports){ +},{"../../plots/mapbox":696,"../scatter/colorbar":837,"./attributes":874,"./calc":875,"./defaults":877,"./event_data":878,"./hover":879,"./plot":881}],881:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173436,9 +159194,9 @@ module.exports = function createScatterMapbox(mapbox, calcTrace) { return scatterMapbox; }; -},{"./convert":1527}],1533:[function(require,module,exports){ +},{"./convert":876}],882:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173561,9 +159319,9 @@ module.exports = { hoveron: scatterAttrs.hoveron, }; -},{"../../components/colorbar/attributes":1162,"../../components/colorscale/color_attributes":1168,"../../lib/extend":1254,"../../plots/attributes":1291,"../scatter/attributes":1485}],1534:[function(require,module,exports){ +},{"../../components/colorbar/attributes":511,"../../components/colorscale/color_attributes":517,"../../lib/extend":603,"../../plots/attributes":640,"../scatter/attributes":834}],883:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173576,10 +159334,10 @@ module.exports = { var isNumeric = require('fast-isnumeric'); var Axes = require('../../plots/cartesian/axes'); -var Lib = require('../../lib'); var subTypes = require('../scatter/subtypes'); var calcColorscale = require('../scatter/colorscale_calc'); +var arraysToCalcdata = require('../scatter/arrays_to_calcdata'); var dataArrays = ['a', 'b', 'c']; var arraysToFill = {a: ['b', 'c'], b: ['a', 'c'], c: ['a', 'b']}; @@ -173653,16 +159411,14 @@ module.exports = function calc(gd, trace) { } calcColorscale(trace); - - // this has migrated up from arraysToCalcdata as we have a reference to 's' here - if(typeof s !== 'undefined') Lib.mergeArray(s, cd, 'ms'); + arraysToCalcdata(cd, trace); return cd; }; -},{"../../lib":1261,"../../plots/cartesian/axes":1293,"../scatter/colorscale_calc":1489,"../scatter/subtypes":1505,"fast-isnumeric":181}],1535:[function(require,module,exports){ +},{"../../plots/cartesian/axes":642,"../scatter/arrays_to_calcdata":833,"../scatter/colorscale_calc":838,"../scatter/subtypes":854,"fast-isnumeric":97}],884:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173765,9 +159521,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('hoveron', dfltHoverOn.join('+') || 'points'); }; -},{"../../lib":1261,"../scatter/constants":1490,"../scatter/fillcolor_defaults":1492,"../scatter/line_defaults":1496,"../scatter/line_shape_defaults":1498,"../scatter/marker_defaults":1501,"../scatter/subtypes":1505,"../scatter/text_defaults":1506,"./attributes":1533}],1536:[function(require,module,exports){ +},{"../../lib":610,"../scatter/constants":839,"../scatter/fillcolor_defaults":841,"../scatter/line_defaults":845,"../scatter/line_shape_defaults":847,"../scatter/marker_defaults":850,"../scatter/subtypes":854,"../scatter/text_defaults":855,"./attributes":882}],885:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173836,9 +159592,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { return scatterPointData; }; -},{"../../plots/cartesian/axes":1293,"../scatter/hover":1494}],1537:[function(require,module,exports){ +},{"../../plots/cartesian/axes":642,"../scatter/hover":843}],886:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173872,9 +159628,9 @@ ScatterTernary.meta = { module.exports = ScatterTernary; -},{"../../plots/ternary":1361,"../scatter/colorbar":1488,"./attributes":1533,"./calc":1534,"./defaults":1535,"./hover":1536,"./plot":1538,"./select":1539,"./style":1540}],1538:[function(require,module,exports){ +},{"../../plots/ternary":710,"../scatter/colorbar":837,"./attributes":882,"./calc":883,"./defaults":884,"./hover":885,"./plot":887,"./select":888,"./style":889}],887:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173918,9 +159674,9 @@ module.exports = function plot(ternary, data) { scatterPlot(ternary.graphDiv, plotinfo, calcdata); }; -},{"../scatter/plot":1502}],1539:[function(require,module,exports){ +},{"../scatter/plot":851}],888:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173953,9 +159709,9 @@ module.exports = function selectPoints(searchInfo, polygon) { return selection; }; -},{"../scatter/select":1503}],1540:[function(require,module,exports){ +},{"../scatter/select":852}],889:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -173982,9 +159738,9 @@ module.exports = function style(gd) { scatterStyle(gd); }; -},{"../scatter/style":1504}],1541:[function(require,module,exports){ +},{"../scatter/style":853}],890:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -174230,9 +159986,9 @@ module.exports = { } }; -},{"../../components/color":1161,"../../components/colorbar/attributes":1162,"../../components/colorscale/attributes":1166,"../../lib/extend":1254}],1542:[function(require,module,exports){ +},{"../../components/color":510,"../../components/colorbar/attributes":511,"../../components/colorscale/attributes":515,"../../lib/extend":603}],891:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -174254,9 +160010,9 @@ module.exports = function calc(gd, trace) { } }; -},{"../../components/colorscale/calc":1167}],1543:[function(require,module,exports){ +},{"../../components/colorscale/calc":516}],892:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -174306,9 +160062,9 @@ module.exports = function colorbar(gd, cd) { .options(trace.colorbar)(); }; -},{"../../components/colorbar/draw":1164,"../../components/colorscale":1175,"../../lib":1261,"../../plots/plots":1353,"fast-isnumeric":181}],1544:[function(require,module,exports){ +},{"../../components/colorbar/draw":513,"../../components/colorscale":524,"../../lib":610,"../../plots/plots":702,"fast-isnumeric":97}],893:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -174690,9 +160446,9 @@ function createSurfaceTrace(scene, data) { module.exports = createSurfaceTrace; -},{"../../lib/str2rgbarray":1275,"gl-surface3d":900,"ndarray":1118,"ndarray-fill":1096,"ndarray-homography":1112,"ndarray-ops":1113,"tinycolor2":1129}],1545:[function(require,module,exports){ +},{"../../lib/str2rgbarray":624,"gl-surface3d":238,"ndarray":443,"ndarray-fill":433,"ndarray-homography":435,"ndarray-ops":437,"tinycolor2":985}],894:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -174811,9 +160567,9 @@ function mapLegacy(traceIn, oldAttr, newAttr) { } } -},{"../../components/colorscale/defaults":1170,"../../lib":1261,"../../registry":1368,"./attributes":1541}],1546:[function(require,module,exports){ +},{"../../components/colorscale/defaults":519,"../../lib":610,"../../registry":717,"./attributes":890}],895:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -174854,9 +160610,9 @@ Surface.meta = { module.exports = Surface; -},{"../../plots/gl3d":1332,"./attributes":1541,"./calc":1542,"./colorbar":1543,"./convert":1544,"./defaults":1545}],1547:[function(require,module,exports){ +},{"../../plots/gl3d":681,"./attributes":890,"./calc":891,"./colorbar":892,"./convert":893,"./defaults":894}],896:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -175191,9 +160947,9 @@ function getFilterFunc(opts, d2c, targetCalendar) { } } -},{"../lib":1261,"../plot_api/plot_schema":1282,"../plots/cartesian/axis_autotype":1294,"../plots/cartesian/axis_ids":1296,"../plots/cartesian/set_convert":1307,"../registry":1368}],1548:[function(require,module,exports){ +},{"../lib":610,"../plot_api/plot_schema":631,"../plots/cartesian/axis_autotype":643,"../plots/cartesian/axis_ids":645,"../plots/cartesian/set_convert":656,"../registry":717}],897:[function(require,module,exports){ /** -* Copyright 2012-2016, Plotly, Inc. +* Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the @@ -175347,57 +161103,3369 @@ function transformOne(trace, state) { return newData; } -},{"../lib":1261,"../plot_api/plot_schema":1282}],1549:[function(require,module,exports){ +},{"../lib":610,"../plot_api/plot_schema":631}],898:[function(require,module,exports){ (function (Buffer){ -/**! - * contentstream - index.js - * - * Copyright(c) fengmk2 and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ - 'use strict'; -/** - * Module dependencies. - */ +var interlaceUtils = require('./interlace'); -var Readable = require('readable-stream').Readable; -var util = require('util'); - -module.exports = ContentStream; - -function ContentStream(obj, options) { - if (!(this instanceof ContentStream)) { - return new ContentStream(obj, options); +var pixelBppMap = { + 1: { // L + 0: 0, + 1: 0, + 2: 0, + 3: 0xff + }, + 2: { // LA + 0: 0, + 1: 0, + 2: 0, + 3: 1 + }, + 3: { // RGB + 0: 0, + 1: 1, + 2: 2, + 3: 0xff + }, + 4: { // RGBA + 0: 0, + 1: 1, + 2: 2, + 3: 3 } - Readable.call(this, options); - if (obj === null || obj === undefined) { - obj = String(obj); +}; + +function bitRetriever(data, depth) { + + var leftOver = []; + var i = 0; + + function split() { + if (i === data.length) { + throw new Error('Ran out of data'); + } + var byte = data[i]; + i++; + var byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1; + switch (depth) { + default: + throw new Error('unrecognised depth'); + case 16: + byte2 = data[i]; + i++; + leftOver.push(((byte << 8) + byte2)); + break; + case 4: + byte2 = byte & 0x0f; + byte1 = byte >> 4; + leftOver.push(byte1, byte2); + break; + case 2: + byte4 = byte & 3; + byte3 = byte >> 2 & 3; + byte2 = byte >> 4 & 3; + byte1 = byte >> 6 & 3; + leftOver.push(byte1, byte2, byte3, byte4); + break; + case 1: + byte8 = byte & 1; + byte7 = byte >> 1 & 1; + byte6 = byte >> 2 & 1; + byte5 = byte >> 3 & 1; + byte4 = byte >> 4 & 1; + byte3 = byte >> 5 & 1; + byte2 = byte >> 6 & 1; + byte1 = byte >> 7 & 1; + leftOver.push(byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8); + break; + } } - this._obj = obj; + + return { + get: function(count) { + while (leftOver.length < count) { + split(); + } + var returner = leftOver.slice(0, count); + leftOver = leftOver.slice(count); + return returner; + }, + resetAfterLine: function() { + leftOver.length = 0; + }, + end: function() { + if (i !== data.length) { + throw new Error('extra data found'); + } + } + }; } -util.inherits(ContentStream, Readable); +function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) { // eslint-disable-line max-params + var imageWidth = image.width; + var imageHeight = image.height; + var imagePass = image.index; + for (var y = 0; y < imageHeight; y++) { + for (var x = 0; x < imageWidth; x++) { + var pxPos = getPxPos(x, y, imagePass); -ContentStream.prototype._read = function (n) { - var obj = this._obj; - if (typeof obj === 'string') { - this.push(new Buffer(obj)); - } else if (Buffer.isBuffer(obj)) { - this.push(obj); - } else { - this.push(new Buffer(JSON.stringify(obj))); + for (var i = 0; i < 4; i++) { + var idx = pixelBppMap[bpp][i]; + if (i === data.length) { + throw new Error('Ran out of data'); + } + pxData[pxPos + i] = idx !== 0xff ? data[idx + rawPos] : 0xff; + } + rawPos += bpp; //eslint-disable-line no-param-reassign + } } - this.push(null); + return rawPos; +} + +function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) { // eslint-disable-line max-params + var imageWidth = image.width; + var imageHeight = image.height; + var imagePass = image.index; + for (var y = 0; y < imageHeight; y++) { + for (var x = 0; x < imageWidth; x++) { + var pixelData = bits.get(bpp); + var pxPos = getPxPos(x, y, imagePass); + + for (var i = 0; i < 4; i++) { + var idx = pixelBppMap[bpp][i]; + pxData[pxPos + i] = idx !== 0xff ? pixelData[idx] : maxBit; + } + } + bits.resetAfterLine(); + } +} + +exports.dataToBitMap = function(data, bitmapInfo) { + + var width = bitmapInfo.width; + var height = bitmapInfo.height; + var depth = bitmapInfo.depth; + var bpp = bitmapInfo.bpp; + var interlace = bitmapInfo.interlace; + + if (depth !== 8) { + var bits = bitRetriever(data, depth); + } + var pxData; + if (depth <= 8) { + pxData = new Buffer(width * height * 4); + } + else { + pxData = new Uint16Array(width * height * 4); + } + var maxBit = Math.pow(2, depth) - 1; + var rawPos = 0; + var images; + var getPxPos; + + if (interlace) { + images = interlaceUtils.getImagePasses(width, height); + getPxPos = interlaceUtils.getInterlaceIterator(width, height); + } + else { + var nonInterlacedPxPos = 0; + getPxPos = function() { + var returner = nonInterlacedPxPos; + nonInterlacedPxPos += 4; + return returner; + }; + images = [{ width: width, height: height }]; + } + + for (var imageIndex = 0; imageIndex < images.length; imageIndex++) { + if (depth === 8) { + rawPos = mapImage8Bit(images[imageIndex], pxData, getPxPos, bpp, data, rawPos); + } + else { + mapImageCustomBit(images[imageIndex], pxData, getPxPos, bpp, bits, maxBit); + } + } + if (depth === 8) { + if (rawPos !== data.length) { + throw new Error('extra data found'); + } + } + else { + bits.end(); + } + + return pxData; }; }).call(this,require("buffer").Buffer) -},{"buffer":33,"readable-stream":1559,"util":68}],1550:[function(require,module,exports){ +},{"./interlace":908,"buffer":46}],899:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var constants = require('./constants'); + +module.exports = function(data, width, height, options) { + var outHasAlpha = options.colorType === constants.COLORTYPE_COLOR_ALPHA; + if (options.inputHasAlpha && outHasAlpha) { + return data; + } + if (!options.inputHasAlpha && !outHasAlpha) { + return data; + } + + var outBpp = outHasAlpha ? 4 : 3; + var outData = new Buffer(width * height * outBpp); + var inBpp = options.inputHasAlpha ? 4 : 3; + var inIndex = 0; + var outIndex = 0; + + var bgColor = options.bgColor || {}; + if (bgColor.red === undefined) { + bgColor.red = 255; + } + if (bgColor.green === undefined) { + bgColor.green = 255; + } + if (bgColor.blue === undefined) { + bgColor.blue = 255; + } + + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + var red = data[inIndex]; + var green = data[inIndex + 1]; + var blue = data[inIndex + 2]; + + var alpha; + if (options.inputHasAlpha) { + alpha = data[inIndex + 3]; + if (!outHasAlpha) { + alpha /= 255; + red = Math.min(Math.max(Math.round((1 - alpha) * bgColor.red + alpha * red), 0), 255); + green = Math.min(Math.max(Math.round((1 - alpha) * bgColor.green + alpha * green), 0), 255); + blue = Math.min(Math.max(Math.round((1 - alpha) * bgColor.blue + alpha * blue), 0), 255); + } + } + else { + alpha = 255; + } + + outData[outIndex] = red; + outData[outIndex + 1] = green; + outData[outIndex + 2] = blue; + if (outHasAlpha) { + outData[outIndex + 3] = alpha; + } + + inIndex += inBpp; + outIndex += outBpp; + } + } + + return outData; +}; + +}).call(this,require("buffer").Buffer) +},{"./constants":901,"buffer":46}],900:[function(require,module,exports){ +(function (process,Buffer){ +'use strict'; + + +var util = require('util'); +var Stream = require('stream'); + + +var ChunkStream = module.exports = function() { + Stream.call(this); + + this._buffers = []; + this._buffered = 0; + + this._reads = []; + this._paused = false; + + this._encoding = 'utf8'; + this.writable = true; +}; +util.inherits(ChunkStream, Stream); + + +ChunkStream.prototype.read = function(length, callback) { + + this._reads.push({ + length: Math.abs(length), // if length < 0 then at most this length + allowLess: length < 0, + func: callback + }); + + process.nextTick(function() { + this._process(); + + // its paused and there is not enought data then ask for more + if (this._paused && this._reads.length > 0) { + this._paused = false; + + this.emit('drain'); + } + }.bind(this)); +}; + +ChunkStream.prototype.write = function(data, encoding) { + + if (!this.writable) { + this.emit('error', new Error('Stream not writable')); + return false; + } + + var dataBuffer; + if (Buffer.isBuffer(data)) { + dataBuffer = data; + } + else { + dataBuffer = new Buffer(data, encoding || this._encoding); + } + + this._buffers.push(dataBuffer); + this._buffered += dataBuffer.length; + + this._process(); + + // ok if there are no more read requests + if (this._reads && this._reads.length === 0) { + this._paused = true; + } + + return this.writable && !this._paused; +}; + +ChunkStream.prototype.end = function(data, encoding) { + + if (data) { + this.write(data, encoding); + } + + this.writable = false; + + // already destroyed + if (!this._buffers) { + return; + } + + // enqueue or handle end + if (this._buffers.length === 0) { + this._end(); + } + else { + this._buffers.push(null); + this._process(); + } +}; + +ChunkStream.prototype.destroySoon = ChunkStream.prototype.end; + +ChunkStream.prototype._end = function() { + + if (this._reads.length > 0) { + this.emit('error', + new Error('There are some read requests waitng on finished stream') + ); + } + + this.destroy(); +}; + +ChunkStream.prototype.destroy = function() { + + if (!this._buffers) { + return; + } + + this.writable = false; + this._reads = null; + this._buffers = null; + + this.emit('close'); +}; + +ChunkStream.prototype._processReadAllowingLess = function(read) { + // ok there is any data so that we can satisfy this request + this._reads.shift(); // == read + + // first we need to peek into first buffer + var smallerBuf = this._buffers[0]; + + // ok there is more data than we need + if (smallerBuf.length > read.length) { + + this._buffered -= read.length; + this._buffers[0] = smallerBuf.slice(read.length); + + read.func.call(this, smallerBuf.slice(0, read.length)); + + } + else { + // ok this is less than maximum length so use it all + this._buffered -= smallerBuf.length; + this._buffers.shift(); // == smallerBuf + + read.func.call(this, smallerBuf); + } +}; + +ChunkStream.prototype._processRead = function(read) { + this._reads.shift(); // == read + + var pos = 0; + var count = 0; + var data = new Buffer(read.length); + + // create buffer for all data + while (pos < read.length) { + + var buf = this._buffers[count++]; + var len = Math.min(buf.length, read.length - pos); + + buf.copy(data, pos, 0, len); + pos += len; + + // last buffer wasn't used all so just slice it and leave + if (len !== buf.length) { + this._buffers[--count] = buf.slice(len); + } + } + + // remove all used buffers + if (count > 0) { + this._buffers.splice(0, count); + } + + this._buffered -= read.length; + + read.func.call(this, data); +}; + +ChunkStream.prototype._process = function() { + + try { + // as long as there is any data and read requests + while (this._buffered > 0 && this._reads && this._reads.length > 0) { + + var read = this._reads[0]; + + // read any data (but no more than length) + if (read.allowLess) { + this._processReadAllowingLess(read); + + } + else if (this._buffered >= read.length) { + // ok we can meet some expectations + + this._processRead(read); + } + else { + // not enought data to satisfy first request in queue + // so we need to wait for more + break; + } + } + + if (this._buffers && this._buffers.length > 0 && this._buffers[0] === null) { + this._end(); + } + } + catch (ex) { + this.emit('error', ex); + } +}; + +}).call(this,require('_process'),require("buffer").Buffer) +},{"_process":923,"buffer":46,"stream":978,"util":1001}],901:[function(require,module,exports){ +'use strict'; + + +module.exports = { + + PNG_SIGNATURE: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], + + TYPE_IHDR: 0x49484452, + TYPE_IEND: 0x49454e44, + TYPE_IDAT: 0x49444154, + TYPE_PLTE: 0x504c5445, + TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase + TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase + + // color-type bits + COLORTYPE_GRAYSCALE: 0, + COLORTYPE_PALETTE: 1, + COLORTYPE_COLOR: 2, + COLORTYPE_ALPHA: 4, // e.g. grayscale and alpha + + // color-type combinations + COLORTYPE_PALETTE_COLOR: 3, + COLORTYPE_COLOR_ALPHA: 6, + + COLORTYPE_TO_BPP_MAP: { + 0: 1, + 2: 3, + 3: 1, + 4: 2, + 6: 4 + }, + + GAMMA_DIVISION: 100000 +}; + +},{}],902:[function(require,module,exports){ +'use strict'; + +var crcTable = []; + +(function() { + for (var i = 0; i < 256; i++) { + var currentCrc = i; + for (var j = 0; j < 8; j++) { + if (currentCrc & 1) { + currentCrc = 0xedb88320 ^ (currentCrc >>> 1); + } + else { + currentCrc = currentCrc >>> 1; + } + } + crcTable[i] = currentCrc; + } +}()); + +var CrcCalculator = module.exports = function() { + this._crc = -1; +}; + +CrcCalculator.prototype.write = function(data) { + + for (var i = 0; i < data.length; i++) { + this._crc = crcTable[(this._crc ^ data[i]) & 0xff] ^ (this._crc >>> 8); + } + return true; +}; + +CrcCalculator.prototype.crc32 = function() { + return this._crc ^ -1; +}; + + +CrcCalculator.crc32 = function(buf) { + + var crc = -1; + for (var i = 0; i < buf.length; i++) { + crc = crcTable[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8); + } + return crc ^ -1; +}; + +},{}],903:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var paethPredictor = require('./paeth-predictor'); + +function filterNone(pxData, pxPos, byteWidth, rawData, rawPos) { + pxData.copy(rawData, rawPos, pxPos, pxPos + byteWidth); +} + +function filterSumNone(pxData, pxPos, byteWidth) { + + var sum = 0; + var length = pxPos + byteWidth; + + for (var i = pxPos; i < length; i++) { + sum += Math.abs(pxData[i]); + } + return sum; +} + +function filterSub(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { + + for (var x = 0; x < byteWidth; x++) { + + var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; + var val = pxData[pxPos + x] - left; + + rawData[rawPos + x] = val; + } +} + +function filterSumSub(pxData, pxPos, byteWidth, bpp) { + + var sum = 0; + for (var x = 0; x < byteWidth; x++) { + + var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; + var val = pxData[pxPos + x] - left; + + sum += Math.abs(val); + } + + return sum; +} + +function filterUp(pxData, pxPos, byteWidth, rawData, rawPos) { + + for (var x = 0; x < byteWidth; x++) { + + var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; + var val = pxData[pxPos + x] - up; + + rawData[rawPos + x] = val; + } +} + +function filterSumUp(pxData, pxPos, byteWidth) { + + var sum = 0; + var length = pxPos + byteWidth; + for (var x = pxPos; x < length; x++) { + + var up = pxPos > 0 ? pxData[x - byteWidth] : 0; + var val = pxData[x] - up; + + sum += Math.abs(val); + } + + return sum; +} + +function filterAvg(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { + + for (var x = 0; x < byteWidth; x++) { + + var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; + var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; + var val = pxData[pxPos + x] - ((left + up) >> 1); + + rawData[rawPos + x] = val; + } +} + +function filterSumAvg(pxData, pxPos, byteWidth, bpp) { + + var sum = 0; + for (var x = 0; x < byteWidth; x++) { + + var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; + var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; + var val = pxData[pxPos + x] - ((left + up) >> 1); + + sum += Math.abs(val); + } + + return sum; +} + +function filterPaeth(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { + + for (var x = 0; x < byteWidth; x++) { + + var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; + var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; + var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; + var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); + + rawData[rawPos + x] = val; + } +} + +function filterSumPaeth(pxData, pxPos, byteWidth, bpp) { + var sum = 0; + for (var x = 0; x < byteWidth; x++) { + + var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; + var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; + var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; + var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); + + sum += Math.abs(val); + } + + return sum; +} + +var filters = { + 0: filterNone, + 1: filterSub, + 2: filterUp, + 3: filterAvg, + 4: filterPaeth +}; + +var filterSums = { + 0: filterSumNone, + 1: filterSumSub, + 2: filterSumUp, + 3: filterSumAvg, + 4: filterSumPaeth +}; + +module.exports = function(pxData, width, height, options, bpp) { + + var filterTypes; + if (!('filterType' in options) || options.filterType === -1) { + filterTypes = [0, 1, 2, 3, 4]; + } + else if (typeof options.filterType === 'number') { + filterTypes = [options.filterType]; + } + else { + throw new Error('unrecognised filter types'); + } + + var byteWidth = width * bpp; + var rawPos = 0; + var pxPos = 0; + var rawData = new Buffer((byteWidth + 1) * height); + var sel = filterTypes[0]; + + for (var y = 0; y < height; y++) { + + if (filterTypes.length > 1) { + // find best filter for this line (with lowest sum of values) + var min = Infinity; + + for (var i = 0; i < filterTypes.length; i++) { + var sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth, bpp); + if (sum < min) { + sel = filterTypes[i]; + min = sum; + } + } + } + + rawData[rawPos] = sel; + rawPos++; + filters[sel](pxData, pxPos, byteWidth, rawData, rawPos, bpp); + rawPos += byteWidth; + pxPos += byteWidth; + } + return rawData; +}; + +}).call(this,require("buffer").Buffer) +},{"./paeth-predictor":912,"buffer":46}],904:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var util = require('util'); +var ChunkStream = require('./chunkstream'); +var Filter = require('./filter-parse'); + + +var FilterAsync = module.exports = function(bitmapInfo) { + ChunkStream.call(this); + + var buffers = []; + var that = this; + this._filter = new Filter(bitmapInfo, { + read: this.read.bind(this), + write: function(buffer) { + buffers.push(buffer); + }, + complete: function() { + that.emit('complete', Buffer.concat(buffers)); + } + }); + + this._filter.start(); +}; +util.inherits(FilterAsync, ChunkStream); + +}).call(this,require("buffer").Buffer) +},{"./chunkstream":900,"./filter-parse":906,"buffer":46,"util":1001}],905:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var SyncReader = require('./sync-reader'); +var Filter = require('./filter-parse'); + + +exports.process = function(inBuffer, bitmapInfo) { + + var outBuffers = []; + var reader = new SyncReader(inBuffer); + var filter = new Filter(bitmapInfo, { + read: reader.read.bind(reader), + write: function(bufferPart) { + outBuffers.push(bufferPart); + }, + complete: function() { + } + }); + + filter.start(); + reader.process(); + + return Buffer.concat(outBuffers); +}; +}).call(this,require("buffer").Buffer) +},{"./filter-parse":906,"./sync-reader":918,"buffer":46}],906:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var interlaceUtils = require('./interlace'); +var paethPredictor = require('./paeth-predictor'); + +function getByteWidth(width, bpp, depth) { + var byteWidth = width * bpp; + if (depth !== 8) { + byteWidth = Math.ceil(byteWidth / (8 / depth)); + } + return byteWidth; +} + +var Filter = module.exports = function(bitmapInfo, dependencies) { + + var width = bitmapInfo.width; + var height = bitmapInfo.height; + var interlace = bitmapInfo.interlace; + var bpp = bitmapInfo.bpp; + var depth = bitmapInfo.depth; + + this.read = dependencies.read; + this.write = dependencies.write; + this.complete = dependencies.complete; + + this._imageIndex = 0; + this._images = []; + if (interlace) { + var passes = interlaceUtils.getImagePasses(width, height); + for (var i = 0; i < passes.length; i++) { + this._images.push({ + byteWidth: getByteWidth(passes[i].width, bpp, depth), + height: passes[i].height, + lineIndex: 0 + }); + } + } + else { + this._images.push({ + byteWidth: getByteWidth(width, bpp, depth), + height: height, + lineIndex: 0 + }); + } + + // when filtering the line we look at the pixel to the left + // the spec also says it is done on a byte level regardless of the number of pixels + // so if the depth is byte compatible (8 or 16) we subtract the bpp in order to compare back + // a pixel rather than just a different byte part. However if we are sub byte, we ignore. + if (depth === 8) { + this._xComparison = bpp; + } + else if (depth === 16) { + this._xComparison = bpp * 2; + } + else { + this._xComparison = 1; + } +}; + +Filter.prototype.start = function() { + this.read(this._images[this._imageIndex].byteWidth + 1, this._reverseFilterLine.bind(this)); +}; + +Filter.prototype._unFilterType1 = function(rawData, unfilteredLine, byteWidth) { + + var xComparison = this._xComparison; + var xBiggerThan = xComparison - 1; + + for (var x = 0; x < byteWidth; x++) { + var rawByte = rawData[1 + x]; + var f1Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; + unfilteredLine[x] = rawByte + f1Left; + } +}; + +Filter.prototype._unFilterType2 = function(rawData, unfilteredLine, byteWidth) { + + var lastLine = this._lastLine; + + for (var x = 0; x < byteWidth; x++) { + var rawByte = rawData[1 + x]; + var f2Up = lastLine ? lastLine[x] : 0; + unfilteredLine[x] = rawByte + f2Up; + } +}; + +Filter.prototype._unFilterType3 = function(rawData, unfilteredLine, byteWidth) { + + var xComparison = this._xComparison; + var xBiggerThan = xComparison - 1; + var lastLine = this._lastLine; + + for (var x = 0; x < byteWidth; x++) { + var rawByte = rawData[1 + x]; + var f3Up = lastLine ? lastLine[x] : 0; + var f3Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; + var f3Add = Math.floor((f3Left + f3Up) / 2); + unfilteredLine[x] = rawByte + f3Add; + } +}; + +Filter.prototype._unFilterType4 = function(rawData, unfilteredLine, byteWidth) { + + var xComparison = this._xComparison; + var xBiggerThan = xComparison - 1; + var lastLine = this._lastLine; + + for (var x = 0; x < byteWidth; x++) { + var rawByte = rawData[1 + x]; + var f4Up = lastLine ? lastLine[x] : 0; + var f4Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; + var f4UpLeft = x > xBiggerThan && lastLine ? lastLine[x - xComparison] : 0; + var f4Add = paethPredictor(f4Left, f4Up, f4UpLeft); + unfilteredLine[x] = rawByte + f4Add; + } +}; + +Filter.prototype._reverseFilterLine = function(rawData) { + + var filter = rawData[0]; + var unfilteredLine; + var currentImage = this._images[this._imageIndex]; + var byteWidth = currentImage.byteWidth; + + if (filter === 0) { + unfilteredLine = rawData.slice(1, byteWidth + 1); + } + else { + + unfilteredLine = new Buffer(byteWidth); + + switch (filter) { + case 1: + this._unFilterType1(rawData, unfilteredLine, byteWidth); + break; + case 2: + this._unFilterType2(rawData, unfilteredLine, byteWidth); + break; + case 3: + this._unFilterType3(rawData, unfilteredLine, byteWidth); + break; + case 4: + this._unFilterType4(rawData, unfilteredLine, byteWidth); + break; + default: + throw new Error('Unrecognised filter type - ' + filter); + } + } + + this.write(unfilteredLine); + + currentImage.lineIndex++; + if (currentImage.lineIndex >= currentImage.height) { + this._lastLine = null; + this._imageIndex++; + currentImage = this._images[this._imageIndex]; + } + else { + this._lastLine = unfilteredLine; + } + + if (currentImage) { + // read, using the byte width that may be from the new current image + this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this)); + } + else { + this._lastLine = null; + this.complete(); + } +}; + +}).call(this,require("buffer").Buffer) +},{"./interlace":908,"./paeth-predictor":912,"buffer":46}],907:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +function dePalette(indata, outdata, width, height, palette) { + var pxPos = 0; + // use values from palette + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + var color = palette[indata[pxPos]]; + + if (!color) { + throw new Error('index ' + indata[pxPos] + ' not in palette'); + } + + for (var i = 0; i < 4; i++) { + outdata[pxPos + i] = color[i]; + } + pxPos += 4; + } + } +} + +function replaceTransparentColor(indata, outdata, width, height, transColor) { + var pxPos = 0; + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + var makeTrans = false; + + if (transColor.length === 1) { + if (transColor[0] === indata[pxPos]) { + makeTrans = true; + } + } + else if (transColor[0] === indata[pxPos] && transColor[1] === indata[pxPos + 1] && transColor[2] === indata[pxPos + 2]) { + makeTrans = true; + } + if (makeTrans) { + for (var i = 0; i < 4; i++) { + outdata[pxPos + i] = 0; + } + } + pxPos += 4; + } + } +} + +function scaleDepth(indata, outdata, width, height, depth) { + var maxOutSample = 255; + var maxInSample = Math.pow(2, depth) - 1; + var pxPos = 0; + + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + for (var i = 0; i < 4; i++) { + outdata[pxPos + i] = Math.floor((indata[pxPos + i] * maxOutSample) / maxInSample + 0.5); + } + pxPos += 4; + } + } +} + +module.exports = function(indata, imageData) { + + var depth = imageData.depth; + var width = imageData.width; + var height = imageData.height; + var colorType = imageData.colorType; + var transColor = imageData.transColor; + var palette = imageData.palette; + + var outdata = indata; // only different for 16 bits + + if (colorType === 3) { // paletted + dePalette(indata, outdata, width, height, palette); + } + else { + if (transColor) { + replaceTransparentColor(indata, outdata, width, height, transColor); + } + // if it needs scaling + if (depth !== 8) { + // if we need to change the buffer size + if (depth === 16) { + outdata = new Buffer(width * height * 4); + } + scaleDepth(indata, outdata, width, height, depth); + } + } + return outdata; +}; + +}).call(this,require("buffer").Buffer) +},{"buffer":46}],908:[function(require,module,exports){ +'use strict'; + +// Adam 7 +// 0 1 2 3 4 5 6 7 +// 0 x 6 4 6 x 6 4 6 +// 1 7 7 7 7 7 7 7 7 +// 2 5 6 5 6 5 6 5 6 +// 3 7 7 7 7 7 7 7 7 +// 4 3 6 4 6 3 6 4 6 +// 5 7 7 7 7 7 7 7 7 +// 6 5 6 5 6 5 6 5 6 +// 7 7 7 7 7 7 7 7 7 + + +var imagePasses = [ + { // pass 1 - 1px + x: [0], + y: [0] + }, + { // pass 2 - 1px + x: [4], + y: [0] + }, + { // pass 3 - 2px + x: [0, 4], + y: [4] + }, + { // pass 4 - 4px + x: [2, 6], + y: [0, 4] + }, + { // pass 5 - 8px + x: [0, 2, 4, 6], + y: [2, 6] + }, + { // pass 6 - 16px + x: [1, 3, 5, 7], + y: [0, 2, 4, 6] + }, + { // pass 7 - 32px + x: [0, 1, 2, 3, 4, 5, 6, 7], + y: [1, 3, 5, 7] + } +]; + +exports.getImagePasses = function(width, height) { + var images = []; + var xLeftOver = width % 8; + var yLeftOver = height % 8; + var xRepeats = (width - xLeftOver) / 8; + var yRepeats = (height - yLeftOver) / 8; + for (var i = 0; i < imagePasses.length; i++) { + var pass = imagePasses[i]; + var passWidth = xRepeats * pass.x.length; + var passHeight = yRepeats * pass.y.length; + for (var j = 0; j < pass.x.length; j++) { + if (pass.x[j] < xLeftOver) { + passWidth++; + } + else { + break; + } + } + for (j = 0; j < pass.y.length; j++) { + if (pass.y[j] < yLeftOver) { + passHeight++; + } + else { + break; + } + } + if (passWidth > 0 && passHeight > 0) { + images.push({ width: passWidth, height: passHeight, index: i }); + } + } + return images; +}; + +exports.getInterlaceIterator = function(width) { + return function(x, y, pass) { + var outerXLeftOver = x % imagePasses[pass].x.length; + var outerX = (((x - outerXLeftOver) / imagePasses[pass].x.length) * 8) + imagePasses[pass].x[outerXLeftOver]; + var outerYLeftOver = y % imagePasses[pass].y.length; + var outerY = (((y - outerYLeftOver) / imagePasses[pass].y.length) * 8) + imagePasses[pass].y[outerYLeftOver]; + return (outerX * 4) + (outerY * width * 4); + }; +}; +},{}],909:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var util = require('util'); +var Stream = require('stream'); +var constants = require('./constants'); +var Packer = require('./packer'); + +var PackerAsync = module.exports = function(opt) { + Stream.call(this); + + var options = opt || {}; + + this._packer = new Packer(options); + this._deflate = this._packer.createDeflate(); + + this.readable = true; +}; +util.inherits(PackerAsync, Stream); + + +PackerAsync.prototype.pack = function(data, width, height, gamma) { + // Signature + this.emit('data', new Buffer(constants.PNG_SIGNATURE)); + this.emit('data', this._packer.packIHDR(width, height)); + + if (gamma) { + this.emit('data', this._packer.packGAMA(gamma)); + } + + var filteredData = this._packer.filterData(data, width, height); + + // compress it + this._deflate.on('error', this.emit.bind(this, 'error')); + + this._deflate.on('data', function(compressedData) { + this.emit('data', this._packer.packIDAT(compressedData)); + }.bind(this)); + + this._deflate.on('end', function() { + this.emit('data', this._packer.packIEND()); + this.emit('end'); + }.bind(this)); + + this._deflate.end(filteredData); +}; + +}).call(this,require("buffer").Buffer) +},{"./constants":901,"./packer":911,"buffer":46,"stream":978,"util":1001}],910:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var hasSyncZlib = true; +var zlib = require('zlib'); +if (!zlib.deflateSync) { + hasSyncZlib = false; +} +var constants = require('./constants'); +var Packer = require('./packer'); + +module.exports = function(metaData, opt) { + + if (!hasSyncZlib) { + throw new Error('To use the sync capability of this library in old node versions, please also add a dependency on node-zlb-backport'); + } + + var options = opt || {}; + + var packer = new Packer(options); + + var chunks = []; + + // Signature + chunks.push(new Buffer(constants.PNG_SIGNATURE)); + + // Header + chunks.push(packer.packIHDR(metaData.width, metaData.height)); + + if (metaData.gamma) { + chunks.push(packer.packGAMA(metaData.gamma)); + } + + var filteredData = packer.filterData(metaData.data, metaData.width, metaData.height); + + // compress it + var compressedData = zlib.deflateSync(filteredData, packer.getDeflateOptions()); + filteredData = null; + + if (!compressedData || !compressedData.length) { + throw new Error('bad png - invalid compressed data response'); + } + chunks.push(packer.packIDAT(compressedData)); + + // End + chunks.push(packer.packIEND()); + + return Buffer.concat(chunks); +}; + +}).call(this,require("buffer").Buffer) +},{"./constants":901,"./packer":911,"buffer":46,"zlib":45}],911:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var constants = require('./constants'); +var CrcStream = require('./crc'); +var bitPacker = require('./bitpacker'); +var filter = require('./filter-pack'); +var zlib = require('zlib'); + +var Packer = module.exports = function(options) { + this._options = options; + + options.deflateChunkSize = options.deflateChunkSize || 32 * 1024; + options.deflateLevel = options.deflateLevel != null ? options.deflateLevel : 9; + options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3; + options.inputHasAlpha = options.inputHasAlpha != null ? options.inputHasAlpha : true; + options.deflateFactory = options.deflateFactory || zlib.createDeflate; + options.bitDepth = options.bitDepth || 8; + options.colorType = (typeof options.colorType === 'number') ? options.colorType : constants.COLORTYPE_COLOR_ALPHA; + + if (options.colorType !== constants.COLORTYPE_COLOR && options.colorType !== constants.COLORTYPE_COLOR_ALPHA) { + throw new Error('option color type:' + options.colorType + ' is not supported at present'); + } + if (options.bitDepth !== 8) { + throw new Error('option bit depth:' + options.bitDepth + ' is not supported at present'); + } +}; + +Packer.prototype.getDeflateOptions = function() { + return { + chunkSize: this._options.deflateChunkSize, + level: this._options.deflateLevel, + strategy: this._options.deflateStrategy + }; +}; + +Packer.prototype.createDeflate = function() { + return this._options.deflateFactory(this.getDeflateOptions()); +}; + +Packer.prototype.filterData = function(data, width, height) { + // convert to correct format for filtering (e.g. right bpp and bit depth) + var packedData = bitPacker(data, width, height, this._options); + + // filter pixel data + var bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType]; + var filteredData = filter(packedData, width, height, this._options, bpp); + return filteredData; +}; + +Packer.prototype._packChunk = function(type, data) { + + var len = (data ? data.length : 0); + var buf = new Buffer(len + 12); + + buf.writeUInt32BE(len, 0); + buf.writeUInt32BE(type, 4); + + if (data) { + data.copy(buf, 8); + } + + buf.writeInt32BE(CrcStream.crc32(buf.slice(4, buf.length - 4)), buf.length - 4); + return buf; +}; + +Packer.prototype.packGAMA = function(gamma) { + var buf = new Buffer(4); + buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0); + return this._packChunk(constants.TYPE_gAMA, buf); +}; + +Packer.prototype.packIHDR = function(width, height) { + + var buf = new Buffer(13); + buf.writeUInt32BE(width, 0); + buf.writeUInt32BE(height, 4); + buf[8] = this._options.bitDepth; // Bit depth + buf[9] = this._options.colorType; // colorType + buf[10] = 0; // compression + buf[11] = 0; // filter + buf[12] = 0; // interlace + + return this._packChunk(constants.TYPE_IHDR, buf); +}; + +Packer.prototype.packIDAT = function(data) { + return this._packChunk(constants.TYPE_IDAT, data); +}; + +Packer.prototype.packIEND = function() { + return this._packChunk(constants.TYPE_IEND, null); +}; +}).call(this,require("buffer").Buffer) +},{"./bitpacker":899,"./constants":901,"./crc":902,"./filter-pack":903,"buffer":46,"zlib":45}],912:[function(require,module,exports){ +'use strict'; + +module.exports = function paethPredictor(left, above, upLeft) { + + var paeth = left + above - upLeft; + var pLeft = Math.abs(paeth - left); + var pAbove = Math.abs(paeth - above); + var pUpLeft = Math.abs(paeth - upLeft); + + if (pLeft <= pAbove && pLeft <= pUpLeft) { + return left; + } + if (pAbove <= pUpLeft) { + return above; + } + return upLeft; +}; +},{}],913:[function(require,module,exports){ +'use strict'; + +var util = require('util'); +var zlib = require('zlib'); +var ChunkStream = require('./chunkstream'); +var FilterAsync = require('./filter-parse-async'); +var Parser = require('./parser'); +var bitmapper = require('./bitmapper'); +var formatNormaliser = require('./format-normaliser'); + +var ParserAsync = module.exports = function(options) { + ChunkStream.call(this); + + this._parser = new Parser(options, { + read: this.read.bind(this), + error: this._handleError.bind(this), + metadata: this._handleMetaData.bind(this), + gamma: this.emit.bind(this, 'gamma'), + palette: this._handlePalette.bind(this), + transColor: this._handleTransColor.bind(this), + finished: this._finished.bind(this), + inflateData: this._inflateData.bind(this) + }); + this._options = options; + this.writable = true; + + this._parser.start(); +}; +util.inherits(ParserAsync, ChunkStream); + + +ParserAsync.prototype._handleError = function(err) { + + this.emit('error', err); + + this.writable = false; + + this.destroy(); + + if (this._inflate && this._inflate.destroy) { + this._inflate.destroy(); + } + + this.errord = true; +}; + +ParserAsync.prototype._inflateData = function(data) { + if (!this._inflate) { + this._inflate = zlib.createInflate(); + + this._inflate.on('error', this.emit.bind(this, 'error')); + this._filter.on('complete', this._complete.bind(this)); + + this._inflate.pipe(this._filter); + } + this._inflate.write(data); +}; + +ParserAsync.prototype._handleMetaData = function(metaData) { + + this.emit('metadata', metaData); + + this._bitmapInfo = Object.create(metaData); + + this._filter = new FilterAsync(this._bitmapInfo); +}; + +ParserAsync.prototype._handleTransColor = function(transColor) { + this._bitmapInfo.transColor = transColor; +}; + +ParserAsync.prototype._handlePalette = function(palette) { + this._bitmapInfo.palette = palette; +}; + + +ParserAsync.prototype._finished = function() { + if (this.errord) { + return; + } + + if (!this._inflate) { + this.emit('error', 'No Inflate block'); + } + else { + // no more data to inflate + this._inflate.end(); + } + this.destroySoon(); +}; + +ParserAsync.prototype._complete = function(filteredData) { + + if (this.errord) { + return; + } + + try { + var bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo); + + var normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo); + bitmapData = null; + } + catch (ex) { + this._handleError(ex); + return; + } + + this.emit('parsed', normalisedBitmapData); +}; + +},{"./bitmapper":898,"./chunkstream":900,"./filter-parse-async":904,"./format-normaliser":907,"./parser":915,"util":1001,"zlib":45}],914:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var hasSyncZlib = true; +var zlib = require('zlib'); +if (!zlib.deflateSync) { + hasSyncZlib = false; +} +var SyncReader = require('./sync-reader'); +var FilterSync = require('./filter-parse-sync'); +var Parser = require('./parser'); +var bitmapper = require('./bitmapper'); +var formatNormaliser = require('./format-normaliser'); + + +module.exports = function(buffer, options) { + + if (!hasSyncZlib) { + throw new Error('To use the sync capability of this library in old node versions, please also add a dependency on node-zlb-backport'); + } + + var err; + function handleError(_err_) { + err = _err_; + } + + var metaData; + function handleMetaData(_metaData_) { + metaData = _metaData_; + } + + function handleTransColor(transColor) { + metaData.transColor = transColor; + } + + function handlePalette(palette) { + metaData.palette = palette; + } + + var gamma; + function handleGamma(_gamma_) { + gamma = _gamma_; + } + + var inflateDataList = []; + function handleInflateData(inflatedData) { + inflateDataList.push(inflatedData); + } + + var reader = new SyncReader(buffer); + + var parser = new Parser(options, { + read: reader.read.bind(reader), + error: handleError, + metadata: handleMetaData, + gamma: handleGamma, + palette: handlePalette, + transColor: handleTransColor, + inflateData: handleInflateData + }); + + parser.start(); + reader.process(); + + if (err) { + throw err; + } + + //join together the inflate datas + var inflateData = Buffer.concat(inflateDataList); + inflateDataList.length = 0; + + var inflatedData = zlib.inflateSync(inflateData); + inflateData = null; + + if (!inflatedData || !inflatedData.length) { + throw new Error('bad png - invalid inflate data response'); + } + + var unfilteredData = FilterSync.process(inflatedData, metaData); + inflateData = null; + + var bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData); + unfilteredData = null; + + var normalisedBitmapData = formatNormaliser(bitmapData, metaData); + + metaData.data = normalisedBitmapData; + metaData.gamma = gamma || 0; + + return metaData; +}; + +}).call(this,require("buffer").Buffer) +},{"./bitmapper":898,"./filter-parse-sync":905,"./format-normaliser":907,"./parser":915,"./sync-reader":918,"buffer":46,"zlib":45}],915:[function(require,module,exports){ +(function (Buffer){ +'use strict'; + +var constants = require('./constants'); +var CrcCalculator = require('./crc'); + + +var Parser = module.exports = function(options, dependencies) { + + this._options = options; + options.checkCRC = options.checkCRC !== false; + + this._hasIHDR = false; + this._hasIEND = false; + + // input flags/metadata + this._palette = []; + this._colorType = 0; + + this._chunks = {}; + this._chunks[constants.TYPE_IHDR] = this._handleIHDR.bind(this); + this._chunks[constants.TYPE_IEND] = this._handleIEND.bind(this); + this._chunks[constants.TYPE_IDAT] = this._handleIDAT.bind(this); + this._chunks[constants.TYPE_PLTE] = this._handlePLTE.bind(this); + this._chunks[constants.TYPE_tRNS] = this._handleTRNS.bind(this); + this._chunks[constants.TYPE_gAMA] = this._handleGAMA.bind(this); + + this.read = dependencies.read; + this.error = dependencies.error; + this.metadata = dependencies.metadata; + this.gamma = dependencies.gamma; + this.transColor = dependencies.transColor; + this.palette = dependencies.palette; + this.parsed = dependencies.parsed; + this.inflateData = dependencies.inflateData; + this.inflateData = dependencies.inflateData; + this.finished = dependencies.finished; +}; + +Parser.prototype.start = function() { + this.read(constants.PNG_SIGNATURE.length, + this._parseSignature.bind(this) + ); +}; + +Parser.prototype._parseSignature = function(data) { + + var signature = constants.PNG_SIGNATURE; + + for (var i = 0; i < signature.length; i++) { + if (data[i] !== signature[i]) { + this.error(new Error('Invalid file signature')); + return; + } + } + this.read(8, this._parseChunkBegin.bind(this)); +}; + +Parser.prototype._parseChunkBegin = function(data) { + + // chunk content length + var length = data.readUInt32BE(0); + + // chunk type + var type = data.readUInt32BE(4); + var name = ''; + for (var i = 4; i < 8; i++) { + name += String.fromCharCode(data[i]); + } + + //console.log('chunk ', name, length); + + // chunk flags + var ancillary = Boolean(data[4] & 0x20); // or critical +// priv = Boolean(data[5] & 0x20), // or public +// safeToCopy = Boolean(data[7] & 0x20); // or unsafe + + if (!this._hasIHDR && type !== constants.TYPE_IHDR) { + this.error(new Error('Expected IHDR on beggining')); + return; + } + + this._crc = new CrcCalculator(); + this._crc.write(new Buffer(name)); + + if (this._chunks[type]) { + return this._chunks[type](length); + } + + if (!ancillary) { + this.error(new Error('Unsupported critical chunk type ' + name)); + return; + } + + this.read(length + 4, this._skipChunk.bind(this)); +}; + +Parser.prototype._skipChunk = function(/*data*/) { + this.read(8, this._parseChunkBegin.bind(this)); +}; + +Parser.prototype._handleChunkEnd = function() { + this.read(4, this._parseChunkEnd.bind(this)); +}; + +Parser.prototype._parseChunkEnd = function(data) { + + var fileCrc = data.readInt32BE(0); + var calcCrc = this._crc.crc32(); + + // check CRC + if (this._options.checkCRC && calcCrc !== fileCrc) { + this.error(new Error('Crc error - ' + fileCrc + ' - ' + calcCrc)); + return; + } + + if (!this._hasIEND) { + this.read(8, this._parseChunkBegin.bind(this)); + } +}; + +Parser.prototype._handleIHDR = function(length) { + this.read(length, this._parseIHDR.bind(this)); +}; +Parser.prototype._parseIHDR = function(data) { + + this._crc.write(data); + + var width = data.readUInt32BE(0); + var height = data.readUInt32BE(4); + var depth = data[8]; + var colorType = data[9]; // bits: 1 palette, 2 color, 4 alpha + var compr = data[10]; + var filter = data[11]; + var interlace = data[12]; + + // console.log(' width', width, 'height', height, + // 'depth', depth, 'colorType', colorType, + // 'compr', compr, 'filter', filter, 'interlace', interlace + // ); + + if (depth !== 8 && depth !== 4 && depth !== 2 && depth !== 1 && depth !== 16) { + this.error(new Error('Unsupported bit depth ' + depth)); + return; + } + if (!(colorType in constants.COLORTYPE_TO_BPP_MAP)) { + this.error(new Error('Unsupported color type')); + return; + } + if (compr !== 0) { + this.error(new Error('Unsupported compression method')); + return; + } + if (filter !== 0) { + this.error(new Error('Unsupported filter method')); + return; + } + if (interlace !== 0 && interlace !== 1) { + this.error(new Error('Unsupported interlace method')); + return; + } + + this._colorType = colorType; + + var bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType]; + + this._hasIHDR = true; + + this.metadata({ + width: width, + height: height, + depth: depth, + interlace: Boolean(interlace), + palette: Boolean(colorType & constants.COLORTYPE_PALETTE), + color: Boolean(colorType & constants.COLORTYPE_COLOR), + alpha: Boolean(colorType & constants.COLORTYPE_ALPHA), + bpp: bpp, + colorType: colorType + }); + + this._handleChunkEnd(); +}; + + +Parser.prototype._handlePLTE = function(length) { + this.read(length, this._parsePLTE.bind(this)); +}; +Parser.prototype._parsePLTE = function(data) { + + this._crc.write(data); + + var entries = Math.floor(data.length / 3); + // console.log('Palette:', entries); + + for (var i = 0; i < entries; i++) { + this._palette.push([ + data[i * 3], + data[i * 3 + 1], + data[i * 3 + 2], + 0xff + ]); + } + + this.palette(this._palette); + + this._handleChunkEnd(); +}; + +Parser.prototype._handleTRNS = function(length) { + this.read(length, this._parseTRNS.bind(this)); +}; +Parser.prototype._parseTRNS = function(data) { + + this._crc.write(data); + + // palette + if (this._colorType === constants.COLORTYPE_PALETTE_COLOR) { + if (this._palette.length === 0) { + this.error(new Error('Transparency chunk must be after palette')); + return; + } + if (data.length > this._palette.length) { + this.error(new Error('More transparent colors than palette size')); + return; + } + for (var i = 0; i < data.length; i++) { + this._palette[i][3] = data[i]; + } + this.palette(this._palette); + } + + // for colorType 0 (grayscale) and 2 (rgb) + // there might be one gray/color defined as transparent + if (this._colorType === constants.COLORTYPE_GRAYSCALE) { + // grey, 2 bytes + this.transColor([data.readUInt16BE(0)]); + } + if (this._colorType === constants.COLORTYPE_COLOR) { + this.transColor([data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)]); + } + + this._handleChunkEnd(); +}; + +Parser.prototype._handleGAMA = function(length) { + this.read(length, this._parseGAMA.bind(this)); +}; +Parser.prototype._parseGAMA = function(data) { + + this._crc.write(data); + this.gamma(data.readUInt32BE(0) / constants.GAMMA_DIVISION); + + this._handleChunkEnd(); +}; + +Parser.prototype._handleIDAT = function(length) { + this.read(-length, this._parseIDAT.bind(this, length)); +}; +Parser.prototype._parseIDAT = function(length, data) { + + this._crc.write(data); + + if (this._colorType === constants.COLORTYPE_PALETTE_COLOR && this._palette.length === 0) { + throw new Error('Expected palette not found'); + } + + this.inflateData(data); + var leftOverLength = length - data.length; + + if (leftOverLength > 0) { + this._handleIDAT(leftOverLength); + } + else { + this._handleChunkEnd(); + } +}; + +Parser.prototype._handleIEND = function(length) { + this.read(length, this._parseIEND.bind(this)); +}; +Parser.prototype._parseIEND = function(data) { + + this._crc.write(data); + + this._hasIEND = true; + this._handleChunkEnd(); + + if (this.finished) { + this.finished(); + } +}; + +}).call(this,require("buffer").Buffer) +},{"./constants":901,"./crc":902,"buffer":46}],916:[function(require,module,exports){ +'use strict'; + + +var parse = require('./parser-sync'); +var pack = require('./packer-sync'); + + +exports.read = function(buffer, options) { + + return parse(buffer, options || {}); +}; + +exports.write = function(png) { + + return pack(png); +}; + +},{"./packer-sync":910,"./parser-sync":914}],917:[function(require,module,exports){ +(function (process,Buffer){ +'use strict'; + +var util = require('util'); +var Stream = require('stream'); +var Parser = require('./parser-async'); +var Packer = require('./packer-async'); +var PNGSync = require('./png-sync'); + + +var PNG = exports.PNG = function(options) { + Stream.call(this); + + options = options || {}; // eslint-disable-line no-param-reassign + + this.width = options.width || 0; + this.height = options.height || 0; + + this.data = this.width > 0 && this.height > 0 ? + new Buffer(4 * this.width * this.height) : null; + + if (options.fill && this.data) { + this.data.fill(0); + } + + this.gamma = 0; + this.readable = this.writable = true; + + this._parser = new Parser(options); + + this._parser.on('error', this.emit.bind(this, 'error')); + this._parser.on('close', this._handleClose.bind(this)); + this._parser.on('metadata', this._metadata.bind(this)); + this._parser.on('gamma', this._gamma.bind(this)); + this._parser.on('parsed', function(data) { + this.data = data; + this.emit('parsed', data); + }.bind(this)); + + this._packer = new Packer(options); + this._packer.on('data', this.emit.bind(this, 'data')); + this._packer.on('end', this.emit.bind(this, 'end')); + this._parser.on('close', this._handleClose.bind(this)); + this._packer.on('error', this.emit.bind(this, 'error')); + +}; +util.inherits(PNG, Stream); + +PNG.sync = PNGSync; + +PNG.prototype.pack = function() { + + if (!this.data || !this.data.length) { + this.emit('error', 'No data provided'); + return this; + } + + process.nextTick(function() { + this._packer.pack(this.data, this.width, this.height, this.gamma); + }.bind(this)); + + return this; +}; + + +PNG.prototype.parse = function(data, callback) { + + if (callback) { + var onParsed, onError; + + onParsed = function(parsedData) { + this.removeListener('error', onError); + + this.data = parsedData; + callback(null, this); + }.bind(this); + + onError = function(err) { + this.removeListener('parsed', onParsed); + + callback(err, null); + }.bind(this); + + this.once('parsed', onParsed); + this.once('error', onError); + } + + this.end(data); + return this; +}; + +PNG.prototype.write = function(data) { + this._parser.write(data); + return true; +}; + +PNG.prototype.end = function(data) { + this._parser.end(data); +}; + +PNG.prototype._metadata = function(metadata) { + this.width = metadata.width; + this.height = metadata.height; + + this.emit('metadata', metadata); +}; + +PNG.prototype._gamma = function(gamma) { + this.gamma = gamma; +}; + +PNG.prototype._handleClose = function() { + if (!this._parser.writable && !this._packer.readable) { + this.emit('close'); + } +}; + + +PNG.bitblt = function(src, dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params + + if (srcX > src.width || srcY > src.height || srcX + width > src.width || srcY + height > src.height) { + throw new Error('bitblt reading outside image'); + } + + if (deltaX > dst.width || deltaY > dst.height || deltaX + width > dst.width || deltaY + height > dst.height) { + throw new Error('bitblt writing outside image'); + } + + for (var y = 0; y < height; y++) { + src.data.copy(dst.data, + ((deltaY + y) * dst.width + deltaX) << 2, + ((srcY + y) * src.width + srcX) << 2, + ((srcY + y) * src.width + srcX + width) << 2 + ); + } +}; + + +PNG.prototype.bitblt = function(dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params + + PNG.bitblt(this, dst, srcX, srcY, width, height, deltaX, deltaY); + return this; +}; + +PNG.adjustGamma = function(src) { + if (src.gamma) { + for (var y = 0; y < src.height; y++) { + for (var x = 0; x < src.width; x++) { + var idx = (src.width * y + x) << 2; + + for (var i = 0; i < 3; i++) { + var sample = src.data[idx + i] / 255; + sample = Math.pow(sample, 1 / 2.2 / src.gamma); + src.data[idx + i] = Math.round(sample * 255); + } + } + } + src.gamma = 0; + } +}; + +PNG.prototype.adjustGamma = function() { + PNG.adjustGamma(this); +}; + +}).call(this,require('_process'),require("buffer").Buffer) +},{"./packer-async":909,"./parser-async":913,"./png-sync":916,"_process":923,"buffer":46,"stream":978,"util":1001}],918:[function(require,module,exports){ +'use strict'; + +var SyncReader = module.exports = function(buffer) { + + this._buffer = buffer; + this._reads = []; +}; + +SyncReader.prototype.read = function(length, callback) { + + this._reads.push({ + length: Math.abs(length), // if length < 0 then at most this length + allowLess: length < 0, + func: callback + }); +}; + +SyncReader.prototype.process = function() { + + // as long as there is any data and read requests + while (this._reads.length > 0 && this._buffer.length) { + + var read = this._reads[0]; + + if (this._buffer.length && (this._buffer.length >= read.length || read.allowLess)) { + + // ok there is any data so that we can satisfy this request + this._reads.shift(); // == read + + var buf = this._buffer; + + this._buffer = buf.slice(read.length); + + read.func.call(this, buf.slice(0, read.length)); + + } + else { + break; + } + + } + + if (this._reads.length > 0) { + return new Error('There are some read requests waitng on finished stream'); + } + + if (this._buffer.length > 0) { + return new Error('unrecognised content at end of stream'); + } + +}; + +},{}],919:[function(require,module,exports){ +'use strict'; + +module.exports = Point; + +function Point(x, y) { + this.x = x; + this.y = y; +} + +Point.prototype = { + clone: function() { return new Point(this.x, this.y); }, + + add: function(p) { return this.clone()._add(p); }, + sub: function(p) { return this.clone()._sub(p); }, + mult: function(k) { return this.clone()._mult(k); }, + div: function(k) { return this.clone()._div(k); }, + rotate: function(a) { return this.clone()._rotate(a); }, + matMult: function(m) { return this.clone()._matMult(m); }, + unit: function() { return this.clone()._unit(); }, + perp: function() { return this.clone()._perp(); }, + round: function() { return this.clone()._round(); }, + + mag: function() { + return Math.sqrt(this.x * this.x + this.y * this.y); + }, + + equals: function(p) { + return this.x === p.x && + this.y === p.y; + }, + + dist: function(p) { + return Math.sqrt(this.distSqr(p)); + }, + + distSqr: function(p) { + var dx = p.x - this.x, + dy = p.y - this.y; + return dx * dx + dy * dy; + }, + + angle: function() { + return Math.atan2(this.y, this.x); + }, + + angleTo: function(b) { + return Math.atan2(this.y - b.y, this.x - b.x); + }, + + angleWith: function(b) { + return this.angleWithSep(b.x, b.y); + }, + + // Find the angle of the two vectors, solving the formula for the cross product a x b = |a||b|sin(θ) for θ. + angleWithSep: function(x, y) { + return Math.atan2( + this.x * y - this.y * x, + this.x * x + this.y * y); + }, + + _matMult: function(m) { + var x = m[0] * this.x + m[1] * this.y, + y = m[2] * this.x + m[3] * this.y; + this.x = x; + this.y = y; + return this; + }, + + _add: function(p) { + this.x += p.x; + this.y += p.y; + return this; + }, + + _sub: function(p) { + this.x -= p.x; + this.y -= p.y; + return this; + }, + + _mult: function(k) { + this.x *= k; + this.y *= k; + return this; + }, + + _div: function(k) { + this.x /= k; + this.y /= k; + return this; + }, + + _unit: function() { + this._div(this.mag()); + return this; + }, + + _perp: function() { + var y = this.y; + this.y = this.x; + this.x = -y; + return this; + }, + + _rotate: function(angle) { + var cos = Math.cos(angle), + sin = Math.sin(angle), + x = cos * this.x - sin * this.y, + y = sin * this.x + cos * this.y; + this.x = x; + this.y = y; + return this; + }, + + _round: function() { + this.x = Math.round(this.x); + this.y = Math.round(this.y); + return this; + } +}; + +// constructs Point from an array if necessary +Point.convert = function (a) { + if (a instanceof Point) { + return a; + } + if (Array.isArray(a)) { + return new Point(a[0], a[1]); + } + return a; +}; + +},{}],920:[function(require,module,exports){ +module.exports = preprocessPolygon + +var orient = require('robust-orientation')[3] +var makeSlabs = require('slab-decomposition') +var makeIntervalTree = require('interval-tree-1d') +var bsearch = require('binary-search-bounds') + +function visitInterval() { + return true +} + +function intervalSearch(table) { + return function(x, y) { + var tree = table[x] + if(tree) { + return !!tree.queryPoint(y, visitInterval) + } + return false + } +} + +function buildVerticalIndex(segments) { + var table = {} + for(var i=0; i 0 && coordinates[bucket] === p[0]) { + root = slabs[bucket-1] + } else { + return 1 + } + } + var lastOrientation = 1 + while(root) { + var s = root.key + var o = orient(p, s[0], s[1]) + if(s[0][0] < s[1][0]) { + if(o < 0) { + root = root.left + } else if(o > 0) { + lastOrientation = -1 + root = root.right + } else { + return 0 + } + } else { + if(o > 0) { + root = root.left + } else if(o < 0) { + lastOrientation = 1 + root = root.right + } else { + return 0 + } + } + } + return lastOrientation + } +} + +function classifyEmpty(p) { + return 1 +} + +function createClassifyVertical(testVertical) { + return function classify(p) { + if(testVertical(p[0], p[1])) { + return 0 + } + return 1 + } +} + +function createClassifyPointDegen(testVertical, testNormal) { + return function classify(p) { + if(testVertical(p[0], p[1])) { + return 0 + } + return testNormal(p) + } +} + +function preprocessPolygon(loops) { + //Compute number of loops + var numLoops = loops.length + + //Unpack segments + var segments = [] + var vsegments = [] + var ptr = 0 + for(var i=0; i= a00) { + s = 1.0; + sqrDistance = a00 + 2.0*b0 + c; + } else { + s = -b0/a00; + sqrDistance = b0*s + c; + } + } else { + s = 0; + if (b1 >= 0) { + t = 0; + sqrDistance = c; + } else if (-b1 >= a11) { + t = 1; + sqrDistance = a11 + 2.0*b1 + c; + } else { + t = -b1/a11; + sqrDistance = b1*t + c; + } + } + } else { // region 3 + s = 0; + if (b1 >= 0) { + t = 0; + sqrDistance = c; + } else if (-b1 >= a11) { + t = 1; + sqrDistance = a11 + 2.0*b1 + c; + } else { + t = -b1/a11; + sqrDistance = b1*t + c; + } + } + } else if (t < 0) { // region 5 + t = 0; + if (b0 >= 0) { + s = 0; + sqrDistance = c; + } else if (-b0 >= a00) { + s = 1; + sqrDistance = a00 + 2.0*b0 + c; + } else { + s = -b0/a00; + sqrDistance = b0*s + c; + } + } else { // region 0 + // minimum at interior point + var invDet = 1.0 / det; + s *= invDet; + t *= invDet; + sqrDistance = s*(a00*s + a01*t + 2.0*b0) + t*(a01*s + a11*t + 2.0*b1) + c; + } + } else { + var tmp0, tmp1, numer, denom; + + if (s < 0) { // region 2 + tmp0 = a01 + b0; + tmp1 = a11 + b1; + if (tmp1 > tmp0) { + numer = tmp1 - tmp0; + denom = a00 - 2.0*a01 + a11; + if (numer >= denom) { + s = 1; + t = 0; + sqrDistance = a00 + 2.0*b0 + c; + } else { + s = numer/denom; + t = 1 - s; + sqrDistance = s*(a00*s + a01*t + 2.0*b0) + + t*(a01*s + a11*t + 2.0*b1) + c; + } + } else { + s = 0; + if (tmp1 <= 0) { + t = 1; + sqrDistance = a11 + 2.0*b1 + c; + } else if (b1 >= 0) { + t = 0; + sqrDistance = c; + } else { + t = -b1/a11; + sqrDistance = b1*t + c; + } + } + } else if (t < 0) { // region 6 + tmp0 = a01 + b1; + tmp1 = a00 + b0; + if (tmp1 > tmp0) { + numer = tmp1 - tmp0; + denom = a00 - 2.0*a01 + a11; + if (numer >= denom) { + t = 1; + s = 0; + sqrDistance = a11 + 2.0*b1 + c; + } else { + t = numer/denom; + s = 1 - t; + sqrDistance = s*(a00*s + a01*t + 2.0*b0) + + t*(a01*s + a11*t + 2.0*b1) + c; + } + } else { + t = 0; + if (tmp1 <= 0) { + s = 1; + sqrDistance = a00 + 2.0*b0 + c; + } else if (b0 >= 0) { + s = 0; + sqrDistance = c; + } else { + s = -b0/a00; + sqrDistance = b0*s + c; + } + } + } else { // region 1 + numer = a11 + b1 - a01 - b0; + if (numer <= 0) { + s = 0; + t = 1; + sqrDistance = a11 + 2.0*b1 + c; + } else { + denom = a00 - 2.0*a01 + a11; + if (numer >= denom) { + s = 1; + t = 0; + sqrDistance = a00 + 2.0*b0 + c; + } else { + s = numer/denom; + t = 1 - s; + sqrDistance = s*(a00*s + a01*t + 2.0*b0) + + t*(a01*s + a11*t + 2.0*b1) + c; + } + } + } + } + var u = 1.0 - s - t; + for(var i=0; i 1) { + for (var i = 1; i < arguments.length; i++) { + args[i - 1] = arguments[i]; + } + } + queue.push(new Item(fun, args)); + if (queue.length === 1 && !draining) { + runTimeout(drainQueue); + } +}; + +// v8 likes predictible objects +function Item(fun, array) { + this.fun = fun; + this.array = array; +} +Item.prototype.run = function () { + this.fun.apply(null, this.array); +}; +process.title = 'browser'; +process.browser = true; +process.env = {}; +process.argv = []; +process.version = ''; // empty string to avoid regexp issues +process.versions = {}; + +function noop() {} + +process.on = noop; +process.addListener = noop; +process.once = noop; +process.off = noop; +process.removeListener = noop; +process.removeAllListeners = noop; +process.emit = noop; + +process.binding = function (name) { + throw new Error('process.binding is not supported'); +}; + +process.cwd = function () { return '/' }; +process.chdir = function (dir) { + throw new Error('process.chdir is not supported'); +}; +process.umask = function() { return 0; }; + +},{}],924:[function(require,module,exports){ +(function (global){ +/*! https://mths.be/punycode v1.4.1 by @mathias */ +;(function(root) { + + /** Detect free variables */ + var freeExports = typeof exports == 'object' && exports && + !exports.nodeType && exports; + var freeModule = typeof module == 'object' && module && + !module.nodeType && module; + var freeGlobal = typeof global == 'object' && global; + if ( + freeGlobal.global === freeGlobal || + freeGlobal.window === freeGlobal || + freeGlobal.self === freeGlobal + ) { + root = freeGlobal; + } + + /** + * The `punycode` object. + * @name punycode + * @type Object + */ + var punycode, + + /** Highest positive signed 32-bit float value */ + maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1 + + /** Bootstring parameters */ + base = 36, + tMin = 1, + tMax = 26, + skew = 38, + damp = 700, + initialBias = 72, + initialN = 128, // 0x80 + delimiter = '-', // '\x2D' + + /** Regular expressions */ + regexPunycode = /^xn--/, + regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars + regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators + + /** Error messages */ + errors = { + 'overflow': 'Overflow: input needs wider integers to process', + 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', + 'invalid-input': 'Invalid input' + }, + + /** Convenience shortcuts */ + baseMinusTMin = base - tMin, + floor = Math.floor, + stringFromCharCode = String.fromCharCode, + + /** Temporary variable */ + key; + + /*--------------------------------------------------------------------------*/ + + /** + * A generic error utility function. + * @private + * @param {String} type The error type. + * @returns {Error} Throws a `RangeError` with the applicable error message. + */ + function error(type) { + throw new RangeError(errors[type]); + } + + /** + * A generic `Array#map` utility function. + * @private + * @param {Array} array The array to iterate over. + * @param {Function} callback The function that gets called for every array + * item. + * @returns {Array} A new array of values returned by the callback function. + */ + function map(array, fn) { + var length = array.length; + var result = []; + while (length--) { + result[length] = fn(array[length]); + } + return result; + } + + /** + * A simple `Array#map`-like wrapper to work with domain name strings or email + * addresses. + * @private + * @param {String} domain The domain name or email address. + * @param {Function} callback The function that gets called for every + * character. + * @returns {Array} A new string of characters returned by the callback + * function. + */ + function mapDomain(string, fn) { + var parts = string.split('@'); + var result = ''; + if (parts.length > 1) { + // In email addresses, only the domain name should be punycoded. Leave + // the local part (i.e. everything up to `@`) intact. + result = parts[0] + '@'; + string = parts[1]; + } + // Avoid `split(regex)` for IE8 compatibility. See #17. + string = string.replace(regexSeparators, '\x2E'); + var labels = string.split('.'); + var encoded = map(labels, fn).join('.'); + return result + encoded; + } + + /** + * Creates an array containing the numeric code points of each Unicode + * character in the string. While JavaScript uses UCS-2 internally, + * this function will convert a pair of surrogate halves (each of which + * UCS-2 exposes as separate characters) into a single code point, + * matching UTF-16. + * @see `punycode.ucs2.encode` + * @see + * @memberOf punycode.ucs2 + * @name decode + * @param {String} string The Unicode input string (UCS-2). + * @returns {Array} The new array of code points. + */ + function ucs2decode(string) { + var output = [], + counter = 0, + length = string.length, + value, + extra; + while (counter < length) { + value = string.charCodeAt(counter++); + if (value >= 0xD800 && value <= 0xDBFF && counter < length) { + // high surrogate, and there is a next character + extra = string.charCodeAt(counter++); + if ((extra & 0xFC00) == 0xDC00) { // low surrogate + output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); + } else { + // unmatched surrogate; only append this code unit, in case the next + // code unit is the high surrogate of a surrogate pair + output.push(value); + counter--; + } + } else { + output.push(value); + } + } + return output; + } + + /** + * Creates a string based on an array of numeric code points. + * @see `punycode.ucs2.decode` + * @memberOf punycode.ucs2 + * @name encode + * @param {Array} codePoints The array of numeric code points. + * @returns {String} The new Unicode string (UCS-2). + */ + function ucs2encode(array) { + return map(array, function(value) { + var output = ''; + if (value > 0xFFFF) { + value -= 0x10000; + output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); + value = 0xDC00 | value & 0x3FF; + } + output += stringFromCharCode(value); + return output; + }).join(''); + } + + /** + * Converts a basic code point into a digit/integer. + * @see `digitToBasic()` + * @private + * @param {Number} codePoint The basic numeric code point value. + * @returns {Number} The numeric value of a basic code point (for use in + * representing integers) in the range `0` to `base - 1`, or `base` if + * the code point does not represent a value. + */ + function basicToDigit(codePoint) { + if (codePoint - 48 < 10) { + return codePoint - 22; + } + if (codePoint - 65 < 26) { + return codePoint - 65; + } + if (codePoint - 97 < 26) { + return codePoint - 97; + } + return base; + } + + /** + * Converts a digit/integer into a basic code point. + * @see `basicToDigit()` + * @private + * @param {Number} digit The numeric value of a basic code point. + * @returns {Number} The basic code point whose value (when used for + * representing integers) is `digit`, which needs to be in the range + * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is + * used; else, the lowercase form is used. The behavior is undefined + * if `flag` is non-zero and `digit` has no uppercase form. + */ + function digitToBasic(digit, flag) { + // 0..25 map to ASCII a..z or A..Z + // 26..35 map to ASCII 0..9 + return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5); + } + + /** + * Bias adaptation function as per section 3.4 of RFC 3492. + * https://tools.ietf.org/html/rfc3492#section-3.4 + * @private + */ + function adapt(delta, numPoints, firstTime) { + var k = 0; + delta = firstTime ? floor(delta / damp) : delta >> 1; + delta += floor(delta / numPoints); + for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) { + delta = floor(delta / baseMinusTMin); + } + return floor(k + (baseMinusTMin + 1) * delta / (delta + skew)); + } + + /** + * Converts a Punycode string of ASCII-only symbols to a string of Unicode + * symbols. + * @memberOf punycode + * @param {String} input The Punycode string of ASCII-only symbols. + * @returns {String} The resulting string of Unicode symbols. + */ + function decode(input) { + // Don't use UCS-2 + var output = [], + inputLength = input.length, + out, + i = 0, + n = initialN, + bias = initialBias, + basic, + j, + index, + oldi, + w, + k, + digit, + t, + /** Cached calculation results */ + baseMinusT; + + // Handle the basic code points: let `basic` be the number of input code + // points before the last delimiter, or `0` if there is none, then copy + // the first basic code points to the output. + + basic = input.lastIndexOf(delimiter); + if (basic < 0) { + basic = 0; + } + + for (j = 0; j < basic; ++j) { + // if it's not a basic code point + if (input.charCodeAt(j) >= 0x80) { + error('not-basic'); + } + output.push(input.charCodeAt(j)); + } + + // Main decoding loop: start just after the last delimiter if any basic code + // points were copied; start at the beginning otherwise. + + for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) { + + // `index` is the index of the next character to be consumed. + // Decode a generalized variable-length integer into `delta`, + // which gets added to `i`. The overflow checking is easier + // if we increase `i` as we go, then subtract off its starting + // value at the end to obtain `delta`. + for (oldi = i, w = 1, k = base; /* no condition */; k += base) { + + if (index >= inputLength) { + error('invalid-input'); + } + + digit = basicToDigit(input.charCodeAt(index++)); + + if (digit >= base || digit > floor((maxInt - i) / w)) { + error('overflow'); + } + + i += digit * w; + t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + + if (digit < t) { + break; + } + + baseMinusT = base - t; + if (w > floor(maxInt / baseMinusT)) { + error('overflow'); + } + + w *= baseMinusT; + + } + + out = output.length + 1; + bias = adapt(i - oldi, out, oldi == 0); + + // `i` was supposed to wrap around from `out` to `0`, + // incrementing `n` each time, so we'll fix that now: + if (floor(i / out) > maxInt - n) { + error('overflow'); + } + + n += floor(i / out); + i %= out; + + // Insert `n` at position `i` of the output + output.splice(i++, 0, n); + + } + + return ucs2encode(output); + } + + /** + * Converts a string of Unicode symbols (e.g. a domain name label) to a + * Punycode string of ASCII-only symbols. + * @memberOf punycode + * @param {String} input The string of Unicode symbols. + * @returns {String} The resulting Punycode string of ASCII-only symbols. + */ + function encode(input) { + var n, + delta, + handledCPCount, + basicLength, + bias, + j, + m, + q, + k, + t, + currentValue, + output = [], + /** `inputLength` will hold the number of code points in `input`. */ + inputLength, + /** Cached calculation results */ + handledCPCountPlusOne, + baseMinusT, + qMinusT; + + // Convert the input in UCS-2 to Unicode + input = ucs2decode(input); + + // Cache the length + inputLength = input.length; + + // Initialize the state + n = initialN; + delta = 0; + bias = initialBias; + + // Handle the basic code points + for (j = 0; j < inputLength; ++j) { + currentValue = input[j]; + if (currentValue < 0x80) { + output.push(stringFromCharCode(currentValue)); + } + } + + handledCPCount = basicLength = output.length; + + // `handledCPCount` is the number of code points that have been handled; + // `basicLength` is the number of basic code points. + + // Finish the basic string - if it is not empty - with a delimiter + if (basicLength) { + output.push(delimiter); + } + + // Main encoding loop: + while (handledCPCount < inputLength) { + + // All non-basic code points < n have been handled already. Find the next + // larger one: + for (m = maxInt, j = 0; j < inputLength; ++j) { + currentValue = input[j]; + if (currentValue >= n && currentValue < m) { + m = currentValue; + } + } + + // Increase `delta` enough to advance the decoder's state to , + // but guard against overflow + handledCPCountPlusOne = handledCPCount + 1; + if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { + error('overflow'); + } + + delta += (m - n) * handledCPCountPlusOne; + n = m; + + for (j = 0; j < inputLength; ++j) { + currentValue = input[j]; + + if (currentValue < n && ++delta > maxInt) { + error('overflow'); + } + + if (currentValue == n) { + // Represent delta as a generalized variable-length integer + for (q = delta, k = base; /* no condition */; k += base) { + t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + if (q < t) { + break; + } + qMinusT = q - t; + baseMinusT = base - t; + output.push( + stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)) + ); + q = floor(qMinusT / baseMinusT); + } + + output.push(stringFromCharCode(digitToBasic(q, 0))); + bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength); + delta = 0; + ++handledCPCount; + } + } + + ++delta; + ++n; + + } + return output.join(''); + } + + /** + * Converts a Punycode string representing a domain name or an email address + * to Unicode. Only the Punycoded parts of the input will be converted, i.e. + * it doesn't matter if you call it on a string that has already been + * converted to Unicode. + * @memberOf punycode + * @param {String} input The Punycoded domain name or email address to + * convert to Unicode. + * @returns {String} The Unicode representation of the given Punycode + * string. + */ + function toUnicode(input) { + return mapDomain(input, function(string) { + return regexPunycode.test(string) + ? decode(string.slice(4).toLowerCase()) + : string; + }); + } + + /** + * Converts a Unicode string representing a domain name or an email address to + * Punycode. Only the non-ASCII parts of the domain name will be converted, + * i.e. it doesn't matter if you call it with a domain that's already in + * ASCII. + * @memberOf punycode + * @param {String} input The domain name or email address to convert, as a + * Unicode string. + * @returns {String} The Punycode representation of the given domain name or + * email address. + */ + function toASCII(input) { + return mapDomain(input, function(string) { + return regexNonASCII.test(string) + ? 'xn--' + encode(string) + : string; + }); + } + + /*--------------------------------------------------------------------------*/ + + /** Define the public API */ + punycode = { + /** + * A string representing the current Punycode.js version number. + * @memberOf punycode + * @type String + */ + 'version': '1.4.1', + /** + * An object of methods to convert from JavaScript's internal character + * representation (UCS-2) to Unicode code points, and back. + * @see + * @memberOf punycode + * @type Object + */ + 'ucs2': { + 'decode': ucs2decode, + 'encode': ucs2encode + }, + 'decode': decode, + 'encode': encode, + 'toASCII': toASCII, + 'toUnicode': toUnicode + }; + + /** Expose `punycode` */ + // Some AMD build optimizers, like r.js, check for specific condition patterns + // like the following: + if ( + typeof define == 'function' && + typeof define.amd == 'object' && + define.amd + ) { + define('punycode', function() { + return punycode; + }); + } else if (freeExports && freeModule) { + if (module.exports == freeExports) { + // in Node.js, io.js, or RingoJS v0.8.0+ + freeModule.exports = punycode; + } else { + // in Narwhal or RingoJS v0.7.0- + for (key in punycode) { + punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); + } + } + } else { + // in Rhino or a web browser + root.punycode = punycode; + } + +}(this)); + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{}],925:[function(require,module,exports){ +module.exports = require('gl-quat/slerp') +},{"gl-quat/slerp":206}],926:[function(require,module,exports){ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -175419,22 +164487,317 @@ ContentStream.prototype._read = function (n) { // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +'use strict'; + +// If obj.hasOwnProperty has been overridden, then calling +// obj.hasOwnProperty(prop) will break. +// See: https://github.com/joyent/node/issues/1707 +function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} + +module.exports = function(qs, sep, eq, options) { + sep = sep || '&'; + eq = eq || '='; + var obj = {}; + + if (typeof qs !== 'string' || qs.length === 0) { + return obj; + } + + var regexp = /\+/g; + qs = qs.split(sep); + + var maxKeys = 1000; + if (options && typeof options.maxKeys === 'number') { + maxKeys = options.maxKeys; + } + + var len = qs.length; + // maxKeys <= 0 means that we should not limit keys count + if (maxKeys > 0 && len > maxKeys) { + len = maxKeys; + } + + for (var i = 0; i < len; ++i) { + var x = qs[i].replace(regexp, '%20'), + idx = x.indexOf(eq), + kstr, vstr, k, v; + + if (idx >= 0) { + kstr = x.substr(0, idx); + vstr = x.substr(idx + 1); + } else { + kstr = x; + vstr = ''; + } + + k = decodeURIComponent(kstr); + v = decodeURIComponent(vstr); + + if (!hasOwnProperty(obj, k)) { + obj[k] = v; + } else if (isArray(obj[k])) { + obj[k].push(v); + } else { + obj[k] = [obj[k], v]; + } + } + + return obj; +}; + +var isArray = Array.isArray || function (xs) { + return Object.prototype.toString.call(xs) === '[object Array]'; +}; + +},{}],927:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +var stringifyPrimitive = function(v) { + switch (typeof v) { + case 'string': + return v; + + case 'boolean': + return v ? 'true' : 'false'; + + case 'number': + return isFinite(v) ? v : ''; + + default: + return ''; + } +}; + +module.exports = function(obj, sep, eq, name) { + sep = sep || '&'; + eq = eq || '='; + if (obj === null) { + obj = undefined; + } + + if (typeof obj === 'object') { + return map(objectKeys(obj), function(k) { + var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; + if (isArray(obj[k])) { + return map(obj[k], function(v) { + return ks + encodeURIComponent(stringifyPrimitive(v)); + }).join(sep); + } else { + return ks + encodeURIComponent(stringifyPrimitive(obj[k])); + } + }).join(sep); + + } + + if (!name) return ''; + return encodeURIComponent(stringifyPrimitive(name)) + eq + + encodeURIComponent(stringifyPrimitive(obj)); +}; + +var isArray = Array.isArray || function (xs) { + return Object.prototype.toString.call(xs) === '[object Array]'; +}; + +function map (xs, f) { + if (xs.map) return xs.map(f); + var res = []; + for (var i = 0; i < xs.length; i++) { + res.push(f(xs[i], i)); + } + return res; +} + +var objectKeys = Object.keys || function (obj) { + var res = []; + for (var key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key); + } + return res; +}; + +},{}],928:[function(require,module,exports){ +'use strict'; + +exports.decode = exports.parse = require('./decode'); +exports.encode = exports.stringify = require('./encode'); + +},{"./decode":926,"./encode":927}],929:[function(require,module,exports){ +'use strict'; + +module.exports = partialSort; + +// Floyd-Rivest selection algorithm: +// Rearrange items so that all items in the [left, k] range are smaller than all items in (k, right]; +// The k-th element will have the (k - left + 1)th smallest value in [left, right] + +function partialSort(arr, k, left, right, compare) { + left = left || 0; + right = right || (arr.length - 1); + compare = compare || defaultCompare; + + while (right > left) { + if (right - left > 600) { + var n = right - left + 1; + var m = k - left + 1; + var z = Math.log(n); + var s = 0.5 * Math.exp(2 * z / 3); + var sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1); + var newLeft = Math.max(left, Math.floor(k - m * s / n + sd)); + var newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd)); + partialSort(arr, k, newLeft, newRight, compare); + } + + var t = arr[k]; + var i = left; + var j = right; + + swap(arr, left, k); + if (compare(arr[right], t) > 0) swap(arr, left, right); + + while (i < j) { + swap(arr, i, j); + i++; + j--; + while (compare(arr[i], t) < 0) i++; + while (compare(arr[j], t) > 0) j--; + } + + if (compare(arr[left], t) === 0) swap(arr, left, j); + else { + j++; + swap(arr, j, right); + } + + if (j <= k) left = j + 1; + if (k <= j) right = j - 1; + } +} + +function swap(arr, i, j) { + var tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; +} + +function defaultCompare(a, b) { + return a < b ? -1 : a > b ? 1 : 0; +} + +},{}],930:[function(require,module,exports){ +'use strict' + +var bnadd = require('big-rat/add') + +module.exports = add + +function add (a, b) { + var n = a.length + var r = new Array(n) + for (var i=0; i*/ + +var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) { + keys.push(key); + }return keys; +}; +/**/ + module.exports = Duplex; /**/ -var objectKeys = Object.keys || function (obj) { - var keys = []; - for (var key in obj) keys.push(key); - return keys; -} +var processNextTick = require('process-nextick-args'); /**/ - /**/ var util = require('core-util-is'); util.inherits = require('inherits'); @@ -175445,27 +164808,24 @@ var Writable = require('./_stream_writable'); util.inherits(Duplex, Readable); -forEach(objectKeys(Writable.prototype), function(method) { - if (!Duplex.prototype[method]) - Duplex.prototype[method] = Writable.prototype[method]; -}); +var keys = objectKeys(Writable.prototype); +for (var v = 0; v < keys.length; v++) { + var method = keys[v]; + if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; +} function Duplex(options) { - if (!(this instanceof Duplex)) - return new Duplex(options); + if (!(this instanceof Duplex)) return new Duplex(options); Readable.call(this, options); Writable.call(this, options); - if (options && options.readable === false) - this.readable = false; + if (options && options.readable === false) this.readable = false; - if (options && options.writable === false) - this.writable = false; + if (options && options.writable === false) this.writable = false; this.allowHalfOpen = true; - if (options && options.allowHalfOpen === false) - this.allowHalfOpen = false; + if (options && options.allowHalfOpen === false) this.allowHalfOpen = false; this.once('end', onend); } @@ -175474,47 +164834,29 @@ function Duplex(options) { function onend() { // if we allow half-open state, or if the writable side ended, // then we're ok. - if (this.allowHalfOpen || this._writableState.ended) - return; + if (this.allowHalfOpen || this._writableState.ended) return; // no more data can be written. // But allow more writes to happen in this tick. - process.nextTick(this.end.bind(this)); + processNextTick(onEndNT, this); } -function forEach (xs, f) { +function onEndNT(self) { + self.end(); +} + +function forEach(xs, f) { for (var i = 0, l = xs.length; i < l; i++) { f(xs[i], i); } } - -}).call(this,require('_process')) -},{"./_stream_readable":1552,"./_stream_writable":1554,"_process":41,"core-util-is":1555,"inherits":1556}],1551:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - +},{"./_stream_readable":937,"./_stream_writable":939,"core-util-is":78,"inherits":266,"process-nextick-args":922}],936:[function(require,module,exports){ // a passthrough stream. // basically just the most minimal sort of Transform stream. // Every written chunk gets output as-is. +'use strict'; + module.exports = PassThrough; var Transform = require('./_stream_transform'); @@ -175527,2944 +164869,116 @@ util.inherits = require('inherits'); util.inherits(PassThrough, Transform); function PassThrough(options) { - if (!(this instanceof PassThrough)) - return new PassThrough(options); + if (!(this instanceof PassThrough)) return new PassThrough(options); Transform.call(this, options); } -PassThrough.prototype._transform = function(chunk, encoding, cb) { +PassThrough.prototype._transform = function (chunk, encoding, cb) { cb(null, chunk); }; - -},{"./_stream_transform":1553,"core-util-is":1555,"inherits":1556}],1552:[function(require,module,exports){ +},{"./_stream_transform":938,"core-util-is":78,"inherits":266}],937:[function(require,module,exports){ (function (process){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. +'use strict'; module.exports = Readable; +/**/ +var processNextTick = require('process-nextick-args'); +/**/ + /**/ var isArray = require('isarray'); /**/ - /**/ -var Buffer = require('buffer').Buffer; +var Duplex; /**/ Readable.ReadableState = ReadableState; +/**/ var EE = require('events').EventEmitter; -/**/ -if (!EE.listenerCount) EE.listenerCount = function(emitter, type) { +var EElistenerCount = function (emitter, type) { return emitter.listeners(type).length; }; /**/ -var Stream = require('stream'); - /**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); +var Stream; +(function () { + try { + Stream = require('st' + 'ream'); + } catch (_) {} finally { + if (!Stream) Stream = require('events').EventEmitter; + } +})(); /**/ -var StringDecoder; - -util.inherits(Readable, Stream); - -function ReadableState(options, stream) { - options = options || {}; - - // the point at which it stops calling _read() to fill the buffer - // Note: 0 is a valid value, means "don't call _read preemptively ever" - var hwm = options.highWaterMark; - this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; - - // cast to ints. - this.highWaterMark = ~~this.highWaterMark; - - this.buffer = []; - this.length = 0; - this.pipes = null; - this.pipesCount = 0; - this.flowing = false; - this.ended = false; - this.endEmitted = false; - this.reading = false; - - // In streams that never have any data, and do push(null) right away, - // the consumer can miss the 'end' event if they do some I/O before - // consuming the stream. So, we don't emit('end') until some reading - // happens. - this.calledRead = false; - - // a flag to be able to tell if the onwrite cb is called immediately, - // or on a later tick. We set this to true at first, becuase any - // actions that shouldn't happen until "later" should generally also - // not happen before the first write call. - this.sync = true; - - // whenever we return null, then we set a flag to say - // that we're awaiting a 'readable' event emission. - this.needReadable = false; - this.emittedReadable = false; - this.readableListening = false; - - - // object stream flag. Used to make read(n) ignore n and to - // make all the buffer merging and length checks go away - this.objectMode = !!options.objectMode; - - // Crypto is kind of old and crusty. Historically, its default string - // encoding is 'binary' so we have to make this configurable. - // Everything else in the universe uses 'utf8', though. - this.defaultEncoding = options.defaultEncoding || 'utf8'; - - // when piping, we only care about 'readable' events that happen - // after read()ing all the bytes and not getting any pushback. - this.ranOut = false; - - // the number of writers that are awaiting a drain event in .pipe()s - this.awaitDrain = 0; - - // if true, a maybeReadMore has been scheduled - this.readingMore = false; - - this.decoder = null; - this.encoding = null; - if (options.encoding) { - if (!StringDecoder) - StringDecoder = require('string_decoder/').StringDecoder; - this.decoder = new StringDecoder(options.encoding); - this.encoding = options.encoding; - } -} - -function Readable(options) { - if (!(this instanceof Readable)) - return new Readable(options); - - this._readableState = new ReadableState(options, this); - - // legacy - this.readable = true; - - Stream.call(this); -} - -// Manually shove something into the read() buffer. -// This returns true if the highWaterMark has not been hit yet, -// similar to how Writable.write() returns true if you should -// write() some more. -Readable.prototype.push = function(chunk, encoding) { - var state = this._readableState; - - if (typeof chunk === 'string' && !state.objectMode) { - encoding = encoding || state.defaultEncoding; - if (encoding !== state.encoding) { - chunk = new Buffer(chunk, encoding); - encoding = ''; - } - } - - return readableAddChunk(this, state, chunk, encoding, false); -}; - -// Unshift should *always* be something directly out of read() -Readable.prototype.unshift = function(chunk) { - var state = this._readableState; - return readableAddChunk(this, state, chunk, '', true); -}; - -function readableAddChunk(stream, state, chunk, encoding, addToFront) { - var er = chunkInvalid(state, chunk); - if (er) { - stream.emit('error', er); - } else if (chunk === null || chunk === undefined) { - state.reading = false; - if (!state.ended) - onEofChunk(stream, state); - } else if (state.objectMode || chunk && chunk.length > 0) { - if (state.ended && !addToFront) { - var e = new Error('stream.push() after EOF'); - stream.emit('error', e); - } else if (state.endEmitted && addToFront) { - var e = new Error('stream.unshift() after end event'); - stream.emit('error', e); - } else { - if (state.decoder && !addToFront && !encoding) - chunk = state.decoder.write(chunk); - - // update the buffer info. - state.length += state.objectMode ? 1 : chunk.length; - if (addToFront) { - state.buffer.unshift(chunk); - } else { - state.reading = false; - state.buffer.push(chunk); - } - - if (state.needReadable) - emitReadable(stream); - - maybeReadMore(stream, state); - } - } else if (!addToFront) { - state.reading = false; - } - - return needMoreData(state); -} - - - -// if it's past the high water mark, we can push in some more. -// Also, if we have no data yet, we can stand some -// more bytes. This is to work around cases where hwm=0, -// such as the repl. Also, if the push() triggered a -// readable event, and the user called read(largeNumber) such that -// needReadable was set, then we ought to push more, so that another -// 'readable' event will be triggered. -function needMoreData(state) { - return !state.ended && - (state.needReadable || - state.length < state.highWaterMark || - state.length === 0); -} - -// backwards compatibility. -Readable.prototype.setEncoding = function(enc) { - if (!StringDecoder) - StringDecoder = require('string_decoder/').StringDecoder; - this._readableState.decoder = new StringDecoder(enc); - this._readableState.encoding = enc; -}; - -// Don't raise the hwm > 128MB -var MAX_HWM = 0x800000; -function roundUpToNextPowerOf2(n) { - if (n >= MAX_HWM) { - n = MAX_HWM; - } else { - // Get the next highest power of 2 - n--; - for (var p = 1; p < 32; p <<= 1) n |= n >> p; - n++; - } - return n; -} - -function howMuchToRead(n, state) { - if (state.length === 0 && state.ended) - return 0; - - if (state.objectMode) - return n === 0 ? 0 : 1; - - if (n === null || isNaN(n)) { - // only flow one buffer at a time - if (state.flowing && state.buffer.length) - return state.buffer[0].length; - else - return state.length; - } - - if (n <= 0) - return 0; - - // If we're asking for more than the target buffer level, - // then raise the water mark. Bump up to the next highest - // power of 2, to prevent increasing it excessively in tiny - // amounts. - if (n > state.highWaterMark) - state.highWaterMark = roundUpToNextPowerOf2(n); - - // don't have that much. return null, unless we've ended. - if (n > state.length) { - if (!state.ended) { - state.needReadable = true; - return 0; - } else - return state.length; - } - - return n; -} - -// you can override either this method, or the async _read(n) below. -Readable.prototype.read = function(n) { - var state = this._readableState; - state.calledRead = true; - var nOrig = n; - var ret; - - if (typeof n !== 'number' || n > 0) - state.emittedReadable = false; - - // if we're doing read(0) to trigger a readable event, but we - // already have a bunch of data in the buffer, then just trigger - // the 'readable' event and move on. - if (n === 0 && - state.needReadable && - (state.length >= state.highWaterMark || state.ended)) { - emitReadable(this); - return null; - } - - n = howMuchToRead(n, state); - - // if we've ended, and we're now clear, then finish it up. - if (n === 0 && state.ended) { - ret = null; - - // In cases where the decoder did not receive enough data - // to produce a full chunk, then immediately received an - // EOF, state.buffer will contain [, ]. - // howMuchToRead will see this and coerce the amount to - // read to zero (because it's looking at the length of the - // first in state.buffer), and we'll end up here. - // - // This can only happen via state.decoder -- no other venue - // exists for pushing a zero-length chunk into state.buffer - // and triggering this behavior. In this case, we return our - // remaining data and end the stream, if appropriate. - if (state.length > 0 && state.decoder) { - ret = fromList(n, state); - state.length -= ret.length; - } - - if (state.length === 0) - endReadable(this); - - return ret; - } - - // All the actual chunk generation logic needs to be - // *below* the call to _read. The reason is that in certain - // synthetic stream cases, such as passthrough streams, _read - // may be a completely synchronous operation which may change - // the state of the read buffer, providing enough data when - // before there was *not* enough. - // - // So, the steps are: - // 1. Figure out what the state of things will be after we do - // a read from the buffer. - // - // 2. If that resulting state will trigger a _read, then call _read. - // Note that this may be asynchronous, or synchronous. Yes, it is - // deeply ugly to write APIs this way, but that still doesn't mean - // that the Readable class should behave improperly, as streams are - // designed to be sync/async agnostic. - // Take note if the _read call is sync or async (ie, if the read call - // has returned yet), so that we know whether or not it's safe to emit - // 'readable' etc. - // - // 3. Actually pull the requested chunks out of the buffer and return. - - // if we need a readable event, then we need to do some reading. - var doRead = state.needReadable; - - // if we currently have less than the highWaterMark, then also read some - if (state.length - n <= state.highWaterMark) - doRead = true; - - // however, if we've ended, then there's no point, and if we're already - // reading, then it's unnecessary. - if (state.ended || state.reading) - doRead = false; - - if (doRead) { - state.reading = true; - state.sync = true; - // if the length is currently zero, then we *need* a readable event. - if (state.length === 0) - state.needReadable = true; - // call internal read method - this._read(state.highWaterMark); - state.sync = false; - } - - // If _read called its callback synchronously, then `reading` - // will be false, and we need to re-evaluate how much data we - // can return to the user. - if (doRead && !state.reading) - n = howMuchToRead(nOrig, state); - - if (n > 0) - ret = fromList(n, state); - else - ret = null; - - if (ret === null) { - state.needReadable = true; - n = 0; - } - - state.length -= n; - - // If we have nothing in the buffer, then we want to know - // as soon as we *do* get something into the buffer. - if (state.length === 0 && !state.ended) - state.needReadable = true; - - // If we happened to read() exactly the remaining amount in the - // buffer, and the EOF has been seen at this point, then make sure - // that we emit 'end' on the very next tick. - if (state.ended && !state.endEmitted && state.length === 0) - endReadable(this); - - return ret; -}; - -function chunkInvalid(state, chunk) { - var er = null; - if (!Buffer.isBuffer(chunk) && - 'string' !== typeof chunk && - chunk !== null && - chunk !== undefined && - !state.objectMode) { - er = new TypeError('Invalid non-string/buffer chunk'); - } - return er; -} - - -function onEofChunk(stream, state) { - if (state.decoder && !state.ended) { - var chunk = state.decoder.end(); - if (chunk && chunk.length) { - state.buffer.push(chunk); - state.length += state.objectMode ? 1 : chunk.length; - } - } - state.ended = true; - - // if we've ended and we have some data left, then emit - // 'readable' now to make sure it gets picked up. - if (state.length > 0) - emitReadable(stream); - else - endReadable(stream); -} - -// Don't emit readable right away in sync mode, because this can trigger -// another read() call => stack overflow. This way, it might trigger -// a nextTick recursion warning, but that's not so bad. -function emitReadable(stream) { - var state = stream._readableState; - state.needReadable = false; - if (state.emittedReadable) - return; - - state.emittedReadable = true; - if (state.sync) - process.nextTick(function() { - emitReadable_(stream); - }); - else - emitReadable_(stream); -} - -function emitReadable_(stream) { - stream.emit('readable'); -} - - -// at this point, the user has presumably seen the 'readable' event, -// and called read() to consume some data. that may have triggered -// in turn another _read(n) call, in which case reading = true if -// it's in progress. -// However, if we're not ended, or reading, and the length < hwm, -// then go ahead and try to read some more preemptively. -function maybeReadMore(stream, state) { - if (!state.readingMore) { - state.readingMore = true; - process.nextTick(function() { - maybeReadMore_(stream, state); - }); - } -} - -function maybeReadMore_(stream, state) { - var len = state.length; - while (!state.reading && !state.flowing && !state.ended && - state.length < state.highWaterMark) { - stream.read(0); - if (len === state.length) - // didn't get any data, stop spinning. - break; - else - len = state.length; - } - state.readingMore = false; -} - -// abstract method. to be overridden in specific implementation classes. -// call cb(er, data) where data is <= n in length. -// for virtual (non-string, non-buffer) streams, "length" is somewhat -// arbitrary, and perhaps not very meaningful. -Readable.prototype._read = function(n) { - this.emit('error', new Error('not implemented')); -}; - -Readable.prototype.pipe = function(dest, pipeOpts) { - var src = this; - var state = this._readableState; - - switch (state.pipesCount) { - case 0: - state.pipes = dest; - break; - case 1: - state.pipes = [state.pipes, dest]; - break; - default: - state.pipes.push(dest); - break; - } - state.pipesCount += 1; - - var doEnd = (!pipeOpts || pipeOpts.end !== false) && - dest !== process.stdout && - dest !== process.stderr; - - var endFn = doEnd ? onend : cleanup; - if (state.endEmitted) - process.nextTick(endFn); - else - src.once('end', endFn); - - dest.on('unpipe', onunpipe); - function onunpipe(readable) { - if (readable !== src) return; - cleanup(); - } - - function onend() { - dest.end(); - } - - // when the dest drains, it reduces the awaitDrain counter - // on the source. This would be more elegant with a .once() - // handler in flow(), but adding and removing repeatedly is - // too slow. - var ondrain = pipeOnDrain(src); - dest.on('drain', ondrain); - - function cleanup() { - // cleanup event handlers once the pipe is broken - dest.removeListener('close', onclose); - dest.removeListener('finish', onfinish); - dest.removeListener('drain', ondrain); - dest.removeListener('error', onerror); - dest.removeListener('unpipe', onunpipe); - src.removeListener('end', onend); - src.removeListener('end', cleanup); - - // if the reader is waiting for a drain event from this - // specific writer, then it would cause it to never start - // flowing again. - // So, if this is awaiting a drain, then we just call it now. - // If we don't know, then assume that we are waiting for one. - if (!dest._writableState || dest._writableState.needDrain) - ondrain(); - } - - // if the dest has an error, then stop piping into it. - // however, don't suppress the throwing behavior for this. - function onerror(er) { - unpipe(); - dest.removeListener('error', onerror); - if (EE.listenerCount(dest, 'error') === 0) - dest.emit('error', er); - } - // This is a brutally ugly hack to make sure that our error handler - // is attached before any userland ones. NEVER DO THIS. - if (!dest._events || !dest._events.error) - dest.on('error', onerror); - else if (isArray(dest._events.error)) - dest._events.error.unshift(onerror); - else - dest._events.error = [onerror, dest._events.error]; - - - - // Both close and finish should trigger unpipe, but only once. - function onclose() { - dest.removeListener('finish', onfinish); - unpipe(); - } - dest.once('close', onclose); - function onfinish() { - dest.removeListener('close', onclose); - unpipe(); - } - dest.once('finish', onfinish); - - function unpipe() { - src.unpipe(dest); - } - - // tell the dest that it's being piped to - dest.emit('pipe', src); - - // start the flow if it hasn't been started already. - if (!state.flowing) { - // the handler that waits for readable events after all - // the data gets sucked out in flow. - // This would be easier to follow with a .once() handler - // in flow(), but that is too slow. - this.on('readable', pipeOnReadable); - - state.flowing = true; - process.nextTick(function() { - flow(src); - }); - } - - return dest; -}; - -function pipeOnDrain(src) { - return function() { - var dest = this; - var state = src._readableState; - state.awaitDrain--; - if (state.awaitDrain === 0) - flow(src); - }; -} - -function flow(src) { - var state = src._readableState; - var chunk; - state.awaitDrain = 0; - - function write(dest, i, list) { - var written = dest.write(chunk); - if (false === written) { - state.awaitDrain++; - } - } - - while (state.pipesCount && null !== (chunk = src.read())) { - - if (state.pipesCount === 1) - write(state.pipes, 0, null); - else - forEach(state.pipes, write); - - src.emit('data', chunk); - - // if anyone needs a drain, then we have to wait for that. - if (state.awaitDrain > 0) - return; - } - - // if every destination was unpiped, either before entering this - // function, or in the while loop, then stop flowing. - // - // NB: This is a pretty rare edge case. - if (state.pipesCount === 0) { - state.flowing = false; - - // if there were data event listeners added, then switch to old mode. - if (EE.listenerCount(src, 'data') > 0) - emitDataEvents(src); - return; - } - - // at this point, no one needed a drain, so we just ran out of data - // on the next readable event, start it over again. - state.ranOut = true; -} - -function pipeOnReadable() { - if (this._readableState.ranOut) { - this._readableState.ranOut = false; - flow(this); - } -} - - -Readable.prototype.unpipe = function(dest) { - var state = this._readableState; - - // if we're not piping anywhere, then do nothing. - if (state.pipesCount === 0) - return this; - - // just one destination. most common case. - if (state.pipesCount === 1) { - // passed in one, but it's not the right one. - if (dest && dest !== state.pipes) - return this; - - if (!dest) - dest = state.pipes; - - // got a match. - state.pipes = null; - state.pipesCount = 0; - this.removeListener('readable', pipeOnReadable); - state.flowing = false; - if (dest) - dest.emit('unpipe', this); - return this; - } - - // slow case. multiple pipe destinations. - - if (!dest) { - // remove all. - var dests = state.pipes; - var len = state.pipesCount; - state.pipes = null; - state.pipesCount = 0; - this.removeListener('readable', pipeOnReadable); - state.flowing = false; - - for (var i = 0; i < len; i++) - dests[i].emit('unpipe', this); - return this; - } - - // try to find the right one. - var i = indexOf(state.pipes, dest); - if (i === -1) - return this; - - state.pipes.splice(i, 1); - state.pipesCount -= 1; - if (state.pipesCount === 1) - state.pipes = state.pipes[0]; - - dest.emit('unpipe', this); - - return this; -}; - -// set up data events if they are asked for -// Ensure readable listeners eventually get something -Readable.prototype.on = function(ev, fn) { - var res = Stream.prototype.on.call(this, ev, fn); - - if (ev === 'data' && !this._readableState.flowing) - emitDataEvents(this); - - if (ev === 'readable' && this.readable) { - var state = this._readableState; - if (!state.readableListening) { - state.readableListening = true; - state.emittedReadable = false; - state.needReadable = true; - if (!state.reading) { - this.read(0); - } else if (state.length) { - emitReadable(this, state); - } - } - } - - return res; -}; -Readable.prototype.addListener = Readable.prototype.on; - -// pause() and resume() are remnants of the legacy readable stream API -// If the user uses them, then switch into old mode. -Readable.prototype.resume = function() { - emitDataEvents(this); - this.read(0); - this.emit('resume'); -}; - -Readable.prototype.pause = function() { - emitDataEvents(this, true); - this.emit('pause'); -}; - -function emitDataEvents(stream, startPaused) { - var state = stream._readableState; - - if (state.flowing) { - // https://github.com/isaacs/readable-stream/issues/16 - throw new Error('Cannot switch to old mode now.'); - } - - var paused = startPaused || false; - var readable = false; - - // convert to an old-style stream. - stream.readable = true; - stream.pipe = Stream.prototype.pipe; - stream.on = stream.addListener = Stream.prototype.on; - - stream.on('readable', function() { - readable = true; - - var c; - while (!paused && (null !== (c = stream.read()))) - stream.emit('data', c); - - if (c === null) { - readable = false; - stream._readableState.needReadable = true; - } - }); - - stream.pause = function() { - paused = true; - this.emit('pause'); - }; - - stream.resume = function() { - paused = false; - if (readable) - process.nextTick(function() { - stream.emit('readable'); - }); - else - this.read(0); - this.emit('resume'); - }; - - // now make it start, just in case it hadn't already. - stream.emit('readable'); -} - -// wrap an old-style stream as the async data source. -// This is *not* part of the readable stream interface. -// It is an ugly unfortunate mess of history. -Readable.prototype.wrap = function(stream) { - var state = this._readableState; - var paused = false; - - var self = this; - stream.on('end', function() { - if (state.decoder && !state.ended) { - var chunk = state.decoder.end(); - if (chunk && chunk.length) - self.push(chunk); - } - - self.push(null); - }); - - stream.on('data', function(chunk) { - if (state.decoder) - chunk = state.decoder.write(chunk); - - // don't skip over falsy values in objectMode - //if (state.objectMode && util.isNullOrUndefined(chunk)) - if (state.objectMode && (chunk === null || chunk === undefined)) - return; - else if (!state.objectMode && (!chunk || !chunk.length)) - return; - - var ret = self.push(chunk); - if (!ret) { - paused = true; - stream.pause(); - } - }); - - // proxy all the other methods. - // important when wrapping filters and duplexes. - for (var i in stream) { - if (typeof stream[i] === 'function' && - typeof this[i] === 'undefined') { - this[i] = function(method) { return function() { - return stream[method].apply(stream, arguments); - }}(i); - } - } - - // proxy certain important events. - var events = ['error', 'close', 'destroy', 'pause', 'resume']; - forEach(events, function(ev) { - stream.on(ev, self.emit.bind(self, ev)); - }); - - // when we try to consume some more bytes, simply unpause the - // underlying stream. - self._read = function(n) { - if (paused) { - paused = false; - stream.resume(); - } - }; - - return self; -}; - - - -// exposed for testing purposes only. -Readable._fromList = fromList; - -// Pluck off n bytes from an array of buffers. -// Length is the combined lengths of all the buffers in the list. -function fromList(n, state) { - var list = state.buffer; - var length = state.length; - var stringMode = !!state.decoder; - var objectMode = !!state.objectMode; - var ret; - - // nothing in the list, definitely empty. - if (list.length === 0) - return null; - - if (length === 0) - ret = null; - else if (objectMode) - ret = list.shift(); - else if (!n || n >= length) { - // read it all, truncate the array. - if (stringMode) - ret = list.join(''); - else - ret = Buffer.concat(list, length); - list.length = 0; - } else { - // read just some of it. - if (n < list[0].length) { - // just take a part of the first list item. - // slice is the same for buffers and strings. - var buf = list[0]; - ret = buf.slice(0, n); - list[0] = buf.slice(n); - } else if (n === list[0].length) { - // first list is a perfect match - ret = list.shift(); - } else { - // complex case. - // we have enough to cover it, but it spans past the first buffer. - if (stringMode) - ret = ''; - else - ret = new Buffer(n); - - var c = 0; - for (var i = 0, l = list.length; i < l && c < n; i++) { - var buf = list[0]; - var cpy = Math.min(n - c, buf.length); - - if (stringMode) - ret += buf.slice(0, cpy); - else - buf.copy(ret, c, 0, cpy); - - if (cpy < buf.length) - list[0] = buf.slice(cpy); - else - list.shift(); - - c += cpy; - } - } - } - - return ret; -} - -function endReadable(stream) { - var state = stream._readableState; - - // If we get here before consuming all the bytes, then that is a - // bug in node. Should never happen. - if (state.length > 0) - throw new Error('endReadable called on non-empty stream'); - - if (!state.endEmitted && state.calledRead) { - state.ended = true; - process.nextTick(function() { - // Check that we didn't get one last unshift. - if (!state.endEmitted && state.length === 0) { - state.endEmitted = true; - stream.readable = false; - stream.emit('end'); - } - }); - } -} - -function forEach (xs, f) { - for (var i = 0, l = xs.length; i < l; i++) { - f(xs[i], i); - } -} - -function indexOf (xs, x) { - for (var i = 0, l = xs.length; i < l; i++) { - if (xs[i] === x) return i; - } - return -1; -} - -}).call(this,require('_process')) -},{"_process":41,"buffer":33,"core-util-is":1555,"events":37,"inherits":1556,"isarray":1557,"stream":62,"string_decoder/":1558}],1553:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - - -// a transform stream is a readable/writable stream where you do -// something with the data. Sometimes it's called a "filter", -// but that's not a great name for it, since that implies a thing where -// some bits pass through, and others are simply ignored. (That would -// be a valid example of a transform, of course.) -// -// While the output is causally related to the input, it's not a -// necessarily symmetric or synchronous transformation. For example, -// a zlib stream might take multiple plain-text writes(), and then -// emit a single compressed chunk some time in the future. -// -// Here's how this works: -// -// The Transform stream has all the aspects of the readable and writable -// stream classes. When you write(chunk), that calls _write(chunk,cb) -// internally, and returns false if there's a lot of pending writes -// buffered up. When you call read(), that calls _read(n) until -// there's enough pending readable data buffered up. -// -// In a transform stream, the written data is placed in a buffer. When -// _read(n) is called, it transforms the queued up data, calling the -// buffered _write cb's as it consumes chunks. If consuming a single -// written chunk would result in multiple output chunks, then the first -// outputted bit calls the readcb, and subsequent chunks just go into -// the read buffer, and will cause it to emit 'readable' if necessary. -// -// This way, back-pressure is actually determined by the reading side, -// since _read has to be called to start processing a new chunk. However, -// a pathological inflate type of transform can cause excessive buffering -// here. For example, imagine a stream where every byte of input is -// interpreted as an integer from 0-255, and then results in that many -// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in -// 1kb of data being output. In this case, you could write a very small -// amount of input, and end up with a very large amount of output. In -// such a pathological inflating mechanism, there'd be no way to tell -// the system to stop doing the transform. A single 4MB write could -// cause the system to run out of memory. -// -// However, even in such a pathological case, only a single written chunk -// would be consumed, and then the rest would wait (un-transformed) until -// the results of the previous transformed chunk were consumed. - -module.exports = Transform; - -var Duplex = require('./_stream_duplex'); - -/**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); -/**/ - -util.inherits(Transform, Duplex); - - -function TransformState(options, stream) { - this.afterTransform = function(er, data) { - return afterTransform(stream, er, data); - }; - - this.needTransform = false; - this.transforming = false; - this.writecb = null; - this.writechunk = null; -} - -function afterTransform(stream, er, data) { - var ts = stream._transformState; - ts.transforming = false; - - var cb = ts.writecb; - - if (!cb) - return stream.emit('error', new Error('no writecb in Transform class')); - - ts.writechunk = null; - ts.writecb = null; - - if (data !== null && data !== undefined) - stream.push(data); - - if (cb) - cb(er); - - var rs = stream._readableState; - rs.reading = false; - if (rs.needReadable || rs.length < rs.highWaterMark) { - stream._read(rs.highWaterMark); - } -} - - -function Transform(options) { - if (!(this instanceof Transform)) - return new Transform(options); - - Duplex.call(this, options); - - var ts = this._transformState = new TransformState(options, this); - - // when the writable side finishes, then flush out anything remaining. - var stream = this; - - // start out asking for a readable event once data is transformed. - this._readableState.needReadable = true; - - // we have implemented the _read method, and done the other things - // that Readable wants before the first _read call, so unset the - // sync guard flag. - this._readableState.sync = false; - - this.once('finish', function() { - if ('function' === typeof this._flush) - this._flush(function(er) { - done(stream, er); - }); - else - done(stream); - }); -} - -Transform.prototype.push = function(chunk, encoding) { - this._transformState.needTransform = false; - return Duplex.prototype.push.call(this, chunk, encoding); -}; - -// This is the part where you do stuff! -// override this function in implementation classes. -// 'chunk' is an input chunk. -// -// Call `push(newChunk)` to pass along transformed output -// to the readable side. You may call 'push' zero or more times. -// -// Call `cb(err)` when you are done with this chunk. If you pass -// an error, then that'll put the hurt on the whole operation. If you -// never call cb(), then you'll never get another chunk. -Transform.prototype._transform = function(chunk, encoding, cb) { - throw new Error('not implemented'); -}; - -Transform.prototype._write = function(chunk, encoding, cb) { - var ts = this._transformState; - ts.writecb = cb; - ts.writechunk = chunk; - ts.writeencoding = encoding; - if (!ts.transforming) { - var rs = this._readableState; - if (ts.needTransform || - rs.needReadable || - rs.length < rs.highWaterMark) - this._read(rs.highWaterMark); - } -}; - -// Doesn't matter what the args are here. -// _transform does all the work. -// That we got here means that the readable side wants more data. -Transform.prototype._read = function(n) { - var ts = this._transformState; - - if (ts.writechunk !== null && ts.writecb && !ts.transforming) { - ts.transforming = true; - this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); - } else { - // mark that we need a transform, so that any data that comes in - // will get processed, now that we've asked for it. - ts.needTransform = true; - } -}; - - -function done(stream, er) { - if (er) - return stream.emit('error', er); - - // if there's nothing in the write buffer, then that means - // that nothing more will ever be provided - var ws = stream._writableState; - var rs = stream._readableState; - var ts = stream._transformState; - - if (ws.length) - throw new Error('calling transform done when ws.length != 0'); - - if (ts.transforming) - throw new Error('calling transform done when still transforming'); - - return stream.push(null); -} - -},{"./_stream_duplex":1550,"core-util-is":1555,"inherits":1556}],1554:[function(require,module,exports){ -(function (process){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -// A bit simpler than readable streams. -// Implement an async ._write(chunk, cb), and it'll handle all -// the drain event emission and buffering. - -module.exports = Writable; - -/**/ var Buffer = require('buffer').Buffer; +/**/ +var bufferShim = require('buffer-shims'); /**/ -Writable.WritableState = WritableState; - - /**/ var util = require('core-util-is'); util.inherits = require('inherits'); /**/ -var Stream = require('stream'); - -util.inherits(Writable, Stream); - -function WriteReq(chunk, encoding, cb) { - this.chunk = chunk; - this.encoding = encoding; - this.callback = cb; -} - -function WritableState(options, stream) { - options = options || {}; - - // the point at which write() starts returning false - // Note: 0 is a valid value, means that we always return false if - // the entire buffer is not flushed immediately on write() - var hwm = options.highWaterMark; - this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; - - // object stream flag to indicate whether or not this stream - // contains buffers or objects. - this.objectMode = !!options.objectMode; - - // cast to ints. - this.highWaterMark = ~~this.highWaterMark; - - this.needDrain = false; - // at the start of calling end() - this.ending = false; - // when end() has been called, and returned - this.ended = false; - // when 'finish' is emitted - this.finished = false; - - // should we decode strings into buffers before passing to _write? - // this is here so that some node-core streams can optimize string - // handling at a lower level. - var noDecode = options.decodeStrings === false; - this.decodeStrings = !noDecode; - - // Crypto is kind of old and crusty. Historically, its default string - // encoding is 'binary' so we have to make this configurable. - // Everything else in the universe uses 'utf8', though. - this.defaultEncoding = options.defaultEncoding || 'utf8'; - - // not an actual buffer we keep track of, but a measurement - // of how much we're waiting to get pushed to some underlying - // socket or file. - this.length = 0; - - // a flag to see when we're in the middle of a write. - this.writing = false; - - // a flag to be able to tell if the onwrite cb is called immediately, - // or on a later tick. We set this to true at first, becuase any - // actions that shouldn't happen until "later" should generally also - // not happen before the first write call. - this.sync = true; - - // a flag to know if we're processing previously buffered items, which - // may call the _write() callback in the same tick, so that we don't - // end up in an overlapped onwrite situation. - this.bufferProcessing = false; - - // the callback that's passed to _write(chunk,cb) - this.onwrite = function(er) { - onwrite(stream, er); - }; - - // the callback that the user supplies to write(chunk,encoding,cb) - this.writecb = null; - - // the amount that is being written when _write is called. - this.writelen = 0; - - this.buffer = []; - - // True if the error was already emitted and should not be thrown again - this.errorEmitted = false; -} - -function Writable(options) { - var Duplex = require('./_stream_duplex'); - - // Writable ctor is applied to Duplexes, though they're not - // instanceof Writable, they're instanceof Readable. - if (!(this instanceof Writable) && !(this instanceof Duplex)) - return new Writable(options); - - this._writableState = new WritableState(options, this); - - // legacy. - this.writable = true; - - Stream.call(this); -} - -// Otherwise people can pipe Writable streams, which is just wrong. -Writable.prototype.pipe = function() { - this.emit('error', new Error('Cannot pipe. Not readable.')); -}; - - -function writeAfterEnd(stream, state, cb) { - var er = new Error('write after end'); - // TODO: defer error events consistently everywhere, not just the cb - stream.emit('error', er); - process.nextTick(function() { - cb(er); - }); -} - -// If we get something that is not a buffer, string, null, or undefined, -// and we're not in objectMode, then that's an error. -// Otherwise stream chunks are all considered to be of length=1, and the -// watermarks determine how many objects to keep in the buffer, rather than -// how many bytes or characters. -function validChunk(stream, state, chunk, cb) { - var valid = true; - if (!Buffer.isBuffer(chunk) && - 'string' !== typeof chunk && - chunk !== null && - chunk !== undefined && - !state.objectMode) { - var er = new TypeError('Invalid non-string/buffer chunk'); - stream.emit('error', er); - process.nextTick(function() { - cb(er); - }); - valid = false; - } - return valid; -} - -Writable.prototype.write = function(chunk, encoding, cb) { - var state = this._writableState; - var ret = false; - - if (typeof encoding === 'function') { - cb = encoding; - encoding = null; - } - - if (Buffer.isBuffer(chunk)) - encoding = 'buffer'; - else if (!encoding) - encoding = state.defaultEncoding; - - if (typeof cb !== 'function') - cb = function() {}; - - if (state.ended) - writeAfterEnd(this, state, cb); - else if (validChunk(this, state, chunk, cb)) - ret = writeOrBuffer(this, state, chunk, encoding, cb); - - return ret; -}; - -function decodeChunk(state, chunk, encoding) { - if (!state.objectMode && - state.decodeStrings !== false && - typeof chunk === 'string') { - chunk = new Buffer(chunk, encoding); - } - return chunk; -} - -// if we're already writing something, then just put this -// in the queue, and wait our turn. Otherwise, call _write -// If we return false, then we need a drain event, so set that flag. -function writeOrBuffer(stream, state, chunk, encoding, cb) { - chunk = decodeChunk(state, chunk, encoding); - if (Buffer.isBuffer(chunk)) - encoding = 'buffer'; - var len = state.objectMode ? 1 : chunk.length; - - state.length += len; - - var ret = state.length < state.highWaterMark; - // we must ensure that previous needDrain will not be reset to false. - if (!ret) - state.needDrain = true; - - if (state.writing) - state.buffer.push(new WriteReq(chunk, encoding, cb)); - else - doWrite(stream, state, len, chunk, encoding, cb); - - return ret; -} - -function doWrite(stream, state, len, chunk, encoding, cb) { - state.writelen = len; - state.writecb = cb; - state.writing = true; - state.sync = true; - stream._write(chunk, encoding, state.onwrite); - state.sync = false; -} - -function onwriteError(stream, state, sync, er, cb) { - if (sync) - process.nextTick(function() { - cb(er); - }); - else - cb(er); - - stream._writableState.errorEmitted = true; - stream.emit('error', er); -} - -function onwriteStateUpdate(state) { - state.writing = false; - state.writecb = null; - state.length -= state.writelen; - state.writelen = 0; -} - -function onwrite(stream, er) { - var state = stream._writableState; - var sync = state.sync; - var cb = state.writecb; - - onwriteStateUpdate(state); - - if (er) - onwriteError(stream, state, sync, er, cb); - else { - // Check if we're actually ready to finish, but don't emit yet - var finished = needFinish(stream, state); - - if (!finished && !state.bufferProcessing && state.buffer.length) - clearBuffer(stream, state); - - if (sync) { - process.nextTick(function() { - afterWrite(stream, state, finished, cb); - }); - } else { - afterWrite(stream, state, finished, cb); - } - } -} - -function afterWrite(stream, state, finished, cb) { - if (!finished) - onwriteDrain(stream, state); - cb(); - if (finished) - finishMaybe(stream, state); -} - -// Must force callback to be called on nextTick, so that we don't -// emit 'drain' before the write() consumer gets the 'false' return -// value, and has a chance to attach a 'drain' listener. -function onwriteDrain(stream, state) { - if (state.length === 0 && state.needDrain) { - state.needDrain = false; - stream.emit('drain'); - } -} - - -// if there's something in the buffer waiting, then process it -function clearBuffer(stream, state) { - state.bufferProcessing = true; - - for (var c = 0; c < state.buffer.length; c++) { - var entry = state.buffer[c]; - var chunk = entry.chunk; - var encoding = entry.encoding; - var cb = entry.callback; - var len = state.objectMode ? 1 : chunk.length; - - doWrite(stream, state, len, chunk, encoding, cb); - - // if we didn't call the onwrite immediately, then - // it means that we need to wait until it does. - // also, that means that the chunk and cb are currently - // being processed, so move the buffer counter past them. - if (state.writing) { - c++; - break; - } - } - - state.bufferProcessing = false; - if (c < state.buffer.length) - state.buffer = state.buffer.slice(c); - else - state.buffer.length = 0; -} - -Writable.prototype._write = function(chunk, encoding, cb) { - cb(new Error('not implemented')); -}; - -Writable.prototype.end = function(chunk, encoding, cb) { - var state = this._writableState; - - if (typeof chunk === 'function') { - cb = chunk; - chunk = null; - encoding = null; - } else if (typeof encoding === 'function') { - cb = encoding; - encoding = null; - } - - if (typeof chunk !== 'undefined' && chunk !== null) - this.write(chunk, encoding); - - // ignore unnecessary end() calls. - if (!state.ending && !state.finished) - endWritable(this, state, cb); -}; - - -function needFinish(stream, state) { - return (state.ending && - state.length === 0 && - !state.finished && - !state.writing); -} - -function finishMaybe(stream, state) { - var need = needFinish(stream, state); - if (need) { - state.finished = true; - stream.emit('finish'); - } - return need; -} - -function endWritable(stream, state, cb) { - state.ending = true; - finishMaybe(stream, state); - if (cb) { - if (state.finished) - process.nextTick(cb); - else - stream.once('finish', cb); - } - state.ended = true; -} - -}).call(this,require('_process')) -},{"./_stream_duplex":1550,"_process":41,"buffer":33,"core-util-is":1555,"inherits":1556,"stream":62}],1555:[function(require,module,exports){ -(function (Buffer){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -// NOTE: These type checking functions intentionally don't use `instanceof` -// because it is fragile and can be easily faked with `Object.create()`. - -function isArray(arg) { - if (Array.isArray) { - return Array.isArray(arg); - } - return objectToString(arg) === '[object Array]'; -} -exports.isArray = isArray; - -function isBoolean(arg) { - return typeof arg === 'boolean'; -} -exports.isBoolean = isBoolean; - -function isNull(arg) { - return arg === null; -} -exports.isNull = isNull; - -function isNullOrUndefined(arg) { - return arg == null; -} -exports.isNullOrUndefined = isNullOrUndefined; - -function isNumber(arg) { - return typeof arg === 'number'; -} -exports.isNumber = isNumber; - -function isString(arg) { - return typeof arg === 'string'; -} -exports.isString = isString; - -function isSymbol(arg) { - return typeof arg === 'symbol'; -} -exports.isSymbol = isSymbol; - -function isUndefined(arg) { - return arg === void 0; -} -exports.isUndefined = isUndefined; - -function isRegExp(re) { - return objectToString(re) === '[object RegExp]'; -} -exports.isRegExp = isRegExp; - -function isObject(arg) { - return typeof arg === 'object' && arg !== null; -} -exports.isObject = isObject; - -function isDate(d) { - return objectToString(d) === '[object Date]'; -} -exports.isDate = isDate; - -function isError(e) { - return (objectToString(e) === '[object Error]' || e instanceof Error); -} -exports.isError = isError; - -function isFunction(arg) { - return typeof arg === 'function'; -} -exports.isFunction = isFunction; - -function isPrimitive(arg) { - return arg === null || - typeof arg === 'boolean' || - typeof arg === 'number' || - typeof arg === 'string' || - typeof arg === 'symbol' || // ES6 symbol - typeof arg === 'undefined'; -} -exports.isPrimitive = isPrimitive; - -exports.isBuffer = Buffer.isBuffer; - -function objectToString(o) { - return Object.prototype.toString.call(o); -} - -}).call(this,{"isBuffer":require("../../../../../../../../browserify/node_modules/insert-module-globals/node_modules/is-buffer/index.js")}) -},{"../../../../../../../../browserify/node_modules/insert-module-globals/node_modules/is-buffer/index.js":39}],1556:[function(require,module,exports){ -arguments[4][12][0].apply(exports,arguments) -},{"dup":12}],1557:[function(require,module,exports){ -module.exports = Array.isArray || function (arr) { - return Object.prototype.toString.call(arr) == '[object Array]'; -}; - -},{}],1558:[function(require,module,exports){ -arguments[4][15][0].apply(exports,arguments) -},{"buffer":33,"dup":15}],1559:[function(require,module,exports){ -(function (process){ -var Stream = require('stream'); // hack to fix a circular dependency issue when used with browserify -exports = module.exports = require('./lib/_stream_readable.js'); -exports.Stream = Stream; -exports.Readable = exports; -exports.Writable = require('./lib/_stream_writable.js'); -exports.Duplex = require('./lib/_stream_duplex.js'); -exports.Transform = require('./lib/_stream_transform.js'); -exports.PassThrough = require('./lib/_stream_passthrough.js'); -if (!process.browser && process.env.READABLE_STREAM === 'disable') { - module.exports = require('stream'); -} - -}).call(this,require('_process')) -},{"./lib/_stream_duplex.js":1550,"./lib/_stream_passthrough.js":1551,"./lib/_stream_readable.js":1552,"./lib/_stream_transform.js":1553,"./lib/_stream_writable.js":1554,"_process":41,"stream":62}],1560:[function(require,module,exports){ -(function (Buffer){ -/* - GIFEncoder.js - - Authors - Kevin Weiner (original Java version - kweiner@fmsware.com) - Thibault Imbert (AS3 version - bytearray.org) - Johan Nordberg (JS version - code@johan-nordberg.com) - Todd Wolfson (Implemented streams - todd@twolfson.com) -*/ - -var assert = require('assert'); -var EventEmitter = require('events').EventEmitter; -var ReadableStream = require('readable-stream'); -var util = require('util'); - -var NeuQuant = require('./TypedNeuQuant.js'); -var LZWEncoder = require('./LZWEncoder.js'); - -// DEV: By using a capacitor, we prevent creating a data event for every byte written -function ByteCapacitor(options) { - // Inherit from ReadableStream - ReadableStream.call(this, options); - - // Start with an empty buffer and allow writes - this.okayToPush = true; - this.resetData(); -} -util.inherits(ByteCapacitor, ReadableStream); - -ByteCapacitor.prototype._read = function () { - // The output is controlled by the input provided by methods. - // If we exceed the highwater mark, we will raise an error. - this.okayToPush = true; -}; - -ByteCapacitor.prototype.resetData = function () { - this.data = []; -}; - -ByteCapacitor.prototype.flushData = function () { - // If we are not okay to push, emit an error - if (!this.okayToPush) { - var err = new Error('GIF memory limit exceeded. Please `read` from GIF before writing additional frames/information.'); - return this.emit('error', err); - } - - // Otherwise, push out the new buffer - var buff = new Buffer(this.data); - this.resetData(); - this.okayToPush = this.push(buff); -}; - -ByteCapacitor.prototype.writeByte = function (val) { - this.data.push(val); -}; - -ByteCapacitor.prototype.writeUTFBytes = function (string) { - for (var l = string.length, i = 0; i < l; i++) { - this.writeByte(string.charCodeAt(i)); - } -}; - -ByteCapacitor.prototype.writeBytes = function (array, offset, length) { - for (var l = length || array.length, i = offset || 0; i < l; i++) { - this.writeByte(array[i]); - } -}; - -function GIFEncoder(width, height, options) { - // Fallback options - options = options || {}; - - // Inherit from ByteCapacitor immediately - // https://github.com/isaacs/readable-stream/blob/v1.1.9/lib/_stream_readable.js#L60-L63 - var hwm = options.highWaterMark; - ByteCapacitor.call(this, { - // Allow for up to 64kB of GIFfy-goodness - highWaterMark: (hwm || hwm === 0) ? hwm : 64 * 1024 - }); - - // image size - this.width = ~~width; - this.height = ~~height; - - // transparent color if given - this.transparent = null; - - // transparent index in color table - this.transIndex = 0; - - // -1 = no repeat, 0 = forever. anything else is repeat count - this.repeat = -1; - - // frame delay (hundredths) - this.delay = 0; - - this.pixels = null; // BGR byte array from frame - this.indexedPixels = null; // converted frame indexed to palette - this.colorDepth = null; // number of bit planes - this.colorTab = null; // RGB palette - this.usedEntry = []; // active palette entries - this.palSize = 7; // color table size (bits-1) - this.dispose = -1; // disposal code (-1 = use default) - this.firstFrame = true; - this.sample = 10; // default sample interval for quantizer - - // When we encounter a header, new frame, or stop, emit data - var that = this; - function flushData() { - that.flushData(); - } - this.on('writeHeader#stop', flushData); - this.on('frame#stop', flushData); - this.on('finish#stop', function finishGif () { - // Flush the data - flushData(); - - // Close the gif - that.push(null); - }); -} -util.inherits(GIFEncoder, ByteCapacitor); - -/* - Sets the delay time between each frame, or changes it for subsequent frames - (applies to last frame added) -*/ -GIFEncoder.prototype.setDelay = function(milliseconds) { - this.delay = Math.round(milliseconds / 10); -}; - -/* - Sets frame rate in frames per second. -*/ -GIFEncoder.prototype.setFrameRate = function(fps) { - this.delay = Math.round(100 / fps); -}; - -/* - Sets the GIF frame disposal code for the last added frame and any - subsequent frames. - - Default is 0 if no transparent color has been set, otherwise 2. -*/ -GIFEncoder.prototype.setDispose = function(disposalCode) { - if (disposalCode >= 0) this.dispose = disposalCode; -}; - -/* - Sets the number of times the set of GIF frames should be played. - - -1 = play once - 0 = repeat indefinitely - - Default is -1 - - Must be invoked before the first image is added -*/ - -GIFEncoder.prototype.setRepeat = function(repeat) { - this.repeat = repeat; -}; - -/* - Sets the transparent color for the last added frame and any subsequent - frames. Since all colors are subject to modification in the quantization - process, the color in the final palette for each frame closest to the given - color becomes the transparent color for that frame. May be set to null to - indicate no transparent color. -*/ -GIFEncoder.prototype.setTransparent = function(color) { - this.transparent = color; -}; - -// Custom methods for performance hacks around streaming GIF data pieces without re-analyzing/loading -GIFEncoder.prototype.analyzeImage = function (imageData) { - // convert to correct format if necessary - this.setImagePixels(this.removeAlphaChannel(imageData)); - this.analyzePixels(); // build color table & map pixels -}; - -GIFEncoder.prototype.writeImageInfo = function () { - if (this.firstFrame) { - this.writeLSD(); // logical screen descriptior - this.writePalette(); // global color table - if (this.repeat >= 0) { - // use NS app extension to indicate reps - this.writeNetscapeExt(); - } - } - - this.writeGraphicCtrlExt(); // write graphic control extension - this.writeImageDesc(); // image descriptor - if (!this.firstFrame) this.writePalette(); // local color table - - // DEV: This was originally after outputImage but it does not affect order it seems - this.firstFrame = false; -}; - -GIFEncoder.prototype.outputImage = function () { - this.writePixels(); // encode and write pixel data -}; - -/* - Adds next GIF frame. The frame is not written immediately, but is - actually deferred until the next frame is received so that timing - data can be inserted. Invoking finish() flushes all frames. -*/ -GIFEncoder.prototype.addFrame = function(imageData) { - this.emit('frame#start'); - - this.analyzeImage(imageData); - this.writeImageInfo(); - this.outputImage(); - - this.emit('frame#stop'); -}; - -/* - Adds final trailer to the GIF stream, if you don't call the finish method - the GIF stream will not be valid. -*/ -GIFEncoder.prototype.finish = function() { - this.emit('finish#start'); - this.writeByte(0x3b); // gif trailer - this.emit('finish#stop'); -}; - -/* - Sets quality of color quantization (conversion of images to the maximum 256 - colors allowed by the GIF specification). Lower values (minimum = 1) - produce better colors, but slow processing significantly. 10 is the - default, and produces good color mapping at reasonable speeds. Values - greater than 20 do not yield significant improvements in speed. -*/ -GIFEncoder.prototype.setQuality = function(quality) { - if (quality < 1) quality = 1; - this.sample = quality; -}; - -/* - Writes GIF file header -*/ -GIFEncoder.prototype.writeHeader = function() { - this.emit('writeHeader#start'); - this.writeUTFBytes("GIF89a"); - this.emit('writeHeader#stop'); -}; - -/* - Analyzes current frame colors and creates color map. -*/ -GIFEncoder.prototype.analyzePixels = function() { - var len = this.pixels.length; - var nPix = len / 3; - - // TODO: Re-use indexedPixels - this.indexedPixels = new Uint8Array(nPix); - - var imgq = new NeuQuant(this.pixels, this.sample); - imgq.buildColormap(); // create reduced palette - this.colorTab = imgq.getColormap(); - - // map image pixels to new palette - var k = 0; - for (var j = 0; j < nPix; j++) { - var index = imgq.lookupRGB( - this.pixels[k++] & 0xff, - this.pixels[k++] & 0xff, - this.pixels[k++] & 0xff - ); - this.usedEntry[index] = true; - this.indexedPixels[j] = index; - } - - this.pixels = null; - this.colorDepth = 8; - this.palSize = 7; - - // get closest match to transparent color if specified - if (this.transparent !== null) { - this.transIndex = this.findClosest(this.transparent); - } -}; - -/* - Returns index of palette color closest to c -*/ -GIFEncoder.prototype.findClosest = function(c) { - if (this.colorTab === null) return -1; - - var r = (c & 0xFF0000) >> 16; - var g = (c & 0x00FF00) >> 8; - var b = (c & 0x0000FF); - var minpos = 0; - var dmin = 256 * 256 * 256; - var len = this.colorTab.length; - - for (var i = 0; i < len;) { - var dr = r - (this.colorTab[i++] & 0xff); - var dg = g - (this.colorTab[i++] & 0xff); - var db = b - (this.colorTab[i] & 0xff); - var d = dr * dr + dg * dg + db * db; - var index = i / 3; - if (this.usedEntry[index] && (d < dmin)) { - dmin = d; - minpos = index; - } - i++; - } - - return minpos; -}; - -/* - Extracts image pixels into byte array pixels - (removes alphachannel from canvas imagedata) -*/ -GIFEncoder.prototype.removeAlphaChannel = function (data) { - var w = this.width; - var h = this.height; - var pixels = new Uint8Array(w * h * 3); - - var count = 0; - - for (var i = 0; i < h; i++) { - for (var j = 0; j < w; j++) { - var b = (i * w * 4) + j * 4; - pixels[count++] = data[b]; - pixels[count++] = data[b+1]; - pixels[count++] = data[b+2]; - } - } - - return pixels; -}; - -GIFEncoder.prototype.setImagePixels = function(pixels) { - this.pixels = pixels; -}; - -/* - Writes Graphic Control Extension -*/ -GIFEncoder.prototype.writeGraphicCtrlExt = function() { - this.writeByte(0x21); // extension introducer - this.writeByte(0xf9); // GCE label - this.writeByte(4); // data block size - - var transp, disp; - if (this.transparent === null) { - transp = 0; - disp = 0; // dispose = no action - } else { - transp = 1; - disp = 2; // force clear if using transparent color - } - - if (this.dispose >= 0) { - disp = dispose & 7; // user override - } - disp <<= 2; - - // packed fields - this.writeByte( - 0 | // 1:3 reserved - disp | // 4:6 disposal - 0 | // 7 user input - 0 = none - transp // 8 transparency flag - ); - - this.writeShort(this.delay); // delay x 1/100 sec - this.writeByte(this.transIndex); // transparent color index - this.writeByte(0); // block terminator -}; - -/* - Writes Image Descriptor -*/ -GIFEncoder.prototype.writeImageDesc = function() { - this.writeByte(0x2c); // image separator - this.writeShort(0); // image position x,y = 0,0 - this.writeShort(0); - this.writeShort(this.width); // image size - this.writeShort(this.height); - - // packed fields - if (this.firstFrame) { - // no LCT - GCT is used for first (or only) frame - this.writeByte(0); - } else { - // specify normal LCT - this.writeByte( - 0x80 | // 1 local color table 1=yes - 0 | // 2 interlace - 0=no - 0 | // 3 sorted - 0=no - 0 | // 4-5 reserved - this.palSize // 6-8 size of color table - ); - } -}; - -/* - Writes Logical Screen Descriptor -*/ -GIFEncoder.prototype.writeLSD = function() { - // logical screen size - this.writeShort(this.width); - this.writeShort(this.height); - - // packed fields - this.writeByte( - 0x80 | // 1 : global color table flag = 1 (gct used) - 0x70 | // 2-4 : color resolution = 7 - 0x00 | // 5 : gct sort flag = 0 - this.palSize // 6-8 : gct size - ); - - this.writeByte(0); // background color index - this.writeByte(0); // pixel aspect ratio - assume 1:1 -}; - -/* - Writes Netscape application extension to define repeat count. -*/ -GIFEncoder.prototype.writeNetscapeExt = function() { - this.writeByte(0x21); // extension introducer - this.writeByte(0xff); // app extension label - this.writeByte(11); // block size - this.writeUTFBytes('NETSCAPE2.0'); // app id + auth code - this.writeByte(3); // sub-block size - this.writeByte(1); // loop sub-block id - this.writeShort(this.repeat); // loop count (extra iterations, 0=repeat forever) - this.writeByte(0); // block terminator -}; - -/* - Writes color table -*/ -GIFEncoder.prototype.writePalette = function() { - this.writeBytes(this.colorTab); - var n = (3 * 256) - this.colorTab.length; - for (var i = 0; i < n; i++) - this.writeByte(0); -}; - -GIFEncoder.prototype.writeShort = function(pValue) { - this.writeByte(pValue & 0xFF); - this.writeByte((pValue >> 8) & 0xFF); -}; - -/* - Encodes and writes pixel data -*/ -GIFEncoder.prototype.writePixels = function() { - var enc = new LZWEncoder(this.width, this.height, this.indexedPixels, this.colorDepth); - enc.encode(this); -}; - -/* - Retrieves the GIF stream -*/ -GIFEncoder.prototype.stream = function() { - return this; -}; - -GIFEncoder.ByteCapacitor = ByteCapacitor; - -module.exports = GIFEncoder; - -}).call(this,require("buffer").Buffer) -},{"./LZWEncoder.js":1561,"./TypedNeuQuant.js":1562,"assert":18,"buffer":33,"events":37,"readable-stream":1572,"util":68}],1561:[function(require,module,exports){ -/* - LZWEncoder.js - - Authors - Kevin Weiner (original Java version - kweiner@fmsware.com) - Thibault Imbert (AS3 version - bytearray.org) - Johan Nordberg (JS version - code@johan-nordberg.com) - - Acknowledgements - GIFCOMPR.C - GIF Image compression routines - Lempel-Ziv compression based on 'compress'. GIF modifications by - David Rowley (mgardi@watdcsu.waterloo.edu) - GIF Image compression - modified 'compress' - Based on: compress.c - File compression ala IEEE Computer, June 1984. - By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) - Jim McKie (decvax!mcvax!jim) - Steve Davies (decvax!vax135!petsd!peora!srd) - Ken Turkowski (decvax!decwrl!turtlevax!ken) - James A. Woods (decvax!ihnp4!ames!jaw) - Joe Orost (decvax!vax135!petsd!joe) -*/ - -var EOF = -1; -var BITS = 12; -var HSIZE = 5003; // 80% occupancy -var masks = [0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, - 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, - 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF]; - -function LZWEncoder(width, height, pixels, colorDepth) { - var initCodeSize = Math.max(2, colorDepth); - - var accum = new Uint8Array(256); - var htab = new Int32Array(HSIZE); - var codetab = new Int32Array(HSIZE); - - var cur_accum, cur_bits = 0; - var a_count; - var free_ent = 0; // first unused entry - var maxcode; - var remaining; - var curPixel; - var n_bits; - - // block compression parameters -- after all codes are used up, - // and compression rate changes, start over. - var clear_flg = false; - - // Algorithm: use open addressing double hashing (no chaining) on the - // prefix code / next character combination. We do a variant of Knuth's - // algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime - // secondary probe. Here, the modular division first probe is gives way - // to a faster exclusive-or manipulation. Also do block compression with - // an adaptive reset, whereby the code table is cleared when the compression - // ratio decreases, but after the table fills. The variable-length output - // codes are re-sized at this point, and a special CLEAR code is generated - // for the decompressor. Late addition: construct the table according to - // file size for noticeable speed improvement on small files. Please direct - // questions about this implementation to ames!jaw. - var g_init_bits, ClearCode, EOFCode; - - // Add a character to the end of the current packet, and if it is 254 - // characters, flush the packet to disk. - function char_out(c, outs) { - accum[a_count++] = c; - if (a_count >= 254) flush_char(outs); - } - - // Clear out the hash table - // table clear for block compress - function cl_block(outs) { - cl_hash(HSIZE); - free_ent = ClearCode + 2; - clear_flg = true; - output(ClearCode, outs); - } - - // Reset code table - function cl_hash(hsize) { - for (var i = 0; i < hsize; ++i) htab[i] = -1; - } - - function compress(init_bits, outs) { - var fcode, c, i, ent, disp, hsize_reg, hshift; - - // Set up the globals: g_init_bits - initial number of bits - g_init_bits = init_bits; - - // Set up the necessary values - clear_flg = false; - n_bits = g_init_bits; - maxcode = MAXCODE(n_bits); - - ClearCode = 1 << (init_bits - 1); - EOFCode = ClearCode + 1; - free_ent = ClearCode + 2; - - a_count = 0; // clear packet - - ent = nextPixel(); - - hshift = 0; - for (fcode = HSIZE; fcode < 65536; fcode *= 2) ++hshift; - hshift = 8 - hshift; // set hash code range bound - hsize_reg = HSIZE; - cl_hash(hsize_reg); // clear hash table - - output(ClearCode, outs); - - outer_loop: while ((c = nextPixel()) != EOF) { - fcode = (c << BITS) + ent; - i = (c << hshift) ^ ent; // xor hashing - if (htab[i] === fcode) { - ent = codetab[i]; - continue; - } else if (htab[i] >= 0) { // non-empty slot - disp = hsize_reg - i; // secondary hash (after G. Knott) - if (i === 0) disp = 1; - do { - if ((i -= disp) < 0) i += hsize_reg; - if (htab[i] === fcode) { - ent = codetab[i]; - continue outer_loop; - } - } while (htab[i] >= 0); - } - output(ent, outs); - ent = c; - if (free_ent < 1 << BITS) { - codetab[i] = free_ent++; // code -> hashtable - htab[i] = fcode; - } else { - cl_block(outs); - } - } - - // Put out the final code. - output(ent, outs); - output(EOFCode, outs); - } - - function encode(outs) { - outs.writeByte(initCodeSize); // write "initial code size" byte - remaining = width * height; // reset navigation variables - curPixel = 0; - compress(initCodeSize + 1, outs); // compress and write the pixel data - outs.writeByte(0); // write block terminator - } - - // Flush the packet to disk, and reset the accumulator - function flush_char(outs) { - if (a_count > 0) { - outs.writeByte(a_count); - outs.writeBytes(accum, 0, a_count); - a_count = 0; - } - } - - function MAXCODE(n_bits) { - return (1 << n_bits) - 1; - } - - // Return the next pixel from the image - function nextPixel() { - if (remaining === 0) return EOF; - --remaining; - var pix = pixels[curPixel++]; - return pix & 0xff; - } - - function output(code, outs) { - cur_accum &= masks[cur_bits]; - - if (cur_bits > 0) cur_accum |= (code << cur_bits); - else cur_accum = code; - - cur_bits += n_bits; - - while (cur_bits >= 8) { - char_out((cur_accum & 0xff), outs); - cur_accum >>= 8; - cur_bits -= 8; - } - - // If the next entry is going to be too big for the code size, - // then increase it, if possible. - if (free_ent > maxcode || clear_flg) { - if (clear_flg) { - maxcode = MAXCODE(n_bits = g_init_bits); - clear_flg = false; - } else { - ++n_bits; - if (n_bits == BITS) maxcode = 1 << BITS; - else maxcode = MAXCODE(n_bits); - } - } - - if (code == EOFCode) { - // At EOF, write the rest of the buffer. - while (cur_bits > 0) { - char_out((cur_accum & 0xff), outs); - cur_accum >>= 8; - cur_bits -= 8; - } - flush_char(outs); - } - } - - this.encode = encode; -} - -module.exports = LZWEncoder; - -},{}],1562:[function(require,module,exports){ -/* NeuQuant Neural-Net Quantization Algorithm - * ------------------------------------------ - * - * Copyright (c) 1994 Anthony Dekker - * - * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. - * See "Kohonen neural networks for optimal colour quantization" - * in "Network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367. - * for a discussion of the algorithm. - * See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML - * - * Any party obtaining a copy of these files from the author, directly or - * indirectly, is granted, free of charge, a full and unrestricted irrevocable, - * world-wide, paid up, royalty-free, nonexclusive right and license to deal - * in this software and documentation files (the "Software"), including without - * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons who receive - * copies from any such party to do so, with the only requirement being - * that this copyright notice remain intact. - * - * (JavaScript port 2012 by Johan Nordberg) - */ - -var ncycles = 100; // number of learning cycles -var netsize = 256; // number of colors used -var maxnetpos = netsize - 1; - -// defs for freq and bias -var netbiasshift = 4; // bias for colour values -var intbiasshift = 16; // bias for fractions -var intbias = (1 << intbiasshift); -var gammashift = 10; -var gamma = (1 << gammashift); -var betashift = 10; -var beta = (intbias >> betashift); /* beta = 1/1024 */ -var betagamma = (intbias << (gammashift - betashift)); - -// defs for decreasing radius factor -var initrad = (netsize >> 3); // for 256 cols, radius starts -var radiusbiasshift = 6; // at 32.0 biased by 6 bits -var radiusbias = (1 << radiusbiasshift); -var initradius = (initrad * radiusbias); //and decreases by a -var radiusdec = 30; // factor of 1/30 each cycle - -// defs for decreasing alpha factor -var alphabiasshift = 10; // alpha starts at 1.0 -var initalpha = (1 << alphabiasshift); -var alphadec; // biased by 10 bits - -/* radbias and alpharadbias used for radpower calculation */ -var radbiasshift = 8; -var radbias = (1 << radbiasshift); -var alpharadbshift = (alphabiasshift + radbiasshift); -var alpharadbias = (1 << alpharadbshift); - -// four primes near 500 - assume no image has a length so large that it is -// divisible by all four primes -var prime1 = 499; -var prime2 = 491; -var prime3 = 487; -var prime4 = 503; -var minpicturebytes = (3 * prime4); - -/* - Constructor: NeuQuant - - Arguments: - - pixels - array of pixels in RGB format - samplefac - sampling factor 1 to 30 where lower is better quality - - > - > pixels = [r, g, b, r, g, b, r, g, b, ..] - > -*/ -function NeuQuant(pixels, samplefac) { - var network; // int[netsize][4] - var netindex; // for network lookup - really 256 - - // bias and freq arrays for learning - var bias; - var freq; - var radpower; - - /* - Private Method: init - - sets up arrays - */ - function init() { - network = []; - netindex = new Int32Array(256); - bias = new Int32Array(netsize); - freq = new Int32Array(netsize); - radpower = new Int32Array(netsize >> 3); - - var i, v; - for (i = 0; i < netsize; i++) { - v = (i << (netbiasshift + 8)) / netsize; - network[i] = new Float64Array([v, v, v, 0]); - //network[i] = [v, v, v, 0] - freq[i] = intbias / netsize; - bias[i] = 0; - } - } - - /* - Private Method: unbiasnet - - unbiases network to give byte values 0..255 and record position i to prepare for sort - */ - function unbiasnet() { - for (var i = 0; i < netsize; i++) { - network[i][0] >>= netbiasshift; - network[i][1] >>= netbiasshift; - network[i][2] >>= netbiasshift; - network[i][3] = i; // record color number - } - } - - /* - Private Method: altersingle - - moves neuron *i* towards biased (b,g,r) by factor *alpha* - */ - function altersingle(alpha, i, b, g, r) { - network[i][0] -= (alpha * (network[i][0] - b)) / initalpha; - network[i][1] -= (alpha * (network[i][1] - g)) / initalpha; - network[i][2] -= (alpha * (network[i][2] - r)) / initalpha; - } - - /* - Private Method: alterneigh - - moves neurons in *radius* around index *i* towards biased (b,g,r) by factor *alpha* - */ - function alterneigh(radius, i, b, g, r) { - var lo = Math.abs(i - radius); - var hi = Math.min(i + radius, netsize); - - var j = i + 1; - var k = i - 1; - var m = 1; - - var p, a; - while ((j < hi) || (k > lo)) { - a = radpower[m++]; - - if (j < hi) { - p = network[j++]; - p[0] -= (a * (p[0] - b)) / alpharadbias; - p[1] -= (a * (p[1] - g)) / alpharadbias; - p[2] -= (a * (p[2] - r)) / alpharadbias; - } - - if (k > lo) { - p = network[k--]; - p[0] -= (a * (p[0] - b)) / alpharadbias; - p[1] -= (a * (p[1] - g)) / alpharadbias; - p[2] -= (a * (p[2] - r)) / alpharadbias; - } - } - } - - /* - Private Method: contest - - searches for biased BGR values - */ - function contest(b, g, r) { - /* - finds closest neuron (min dist) and updates freq - finds best neuron (min dist-bias) and returns position - for frequently chosen neurons, freq[i] is high and bias[i] is negative - bias[i] = gamma * ((1 / netsize) - freq[i]) - */ - - var bestd = ~(1 << 31); - var bestbiasd = bestd; - var bestpos = -1; - var bestbiaspos = bestpos; - - var i, n, dist, biasdist, betafreq; - for (i = 0; i < netsize; i++) { - n = network[i]; - - dist = Math.abs(n[0] - b) + Math.abs(n[1] - g) + Math.abs(n[2] - r); - if (dist < bestd) { - bestd = dist; - bestpos = i; - } - - biasdist = dist - ((bias[i]) >> (intbiasshift - netbiasshift)); - if (biasdist < bestbiasd) { - bestbiasd = biasdist; - bestbiaspos = i; - } - - betafreq = (freq[i] >> betashift); - freq[i] -= betafreq; - bias[i] += (betafreq << gammashift); - } - - freq[bestpos] += beta; - bias[bestpos] -= betagamma; - - return bestbiaspos; - } - - /* - Private Method: inxbuild - - sorts network and builds netindex[0..255] - */ - function inxbuild() { - var i, j, p, q, smallpos, smallval, previouscol = 0, startpos = 0; - for (i = 0; i < netsize; i++) { - p = network[i]; - smallpos = i; - smallval = p[1]; // index on g - // find smallest in i..netsize-1 - for (j = i + 1; j < netsize; j++) { - q = network[j]; - if (q[1] < smallval) { // index on g - smallpos = j; - smallval = q[1]; // index on g - } - } - q = network[smallpos]; - // swap p (i) and q (smallpos) entries - if (i != smallpos) { - j = q[0]; q[0] = p[0]; p[0] = j; - j = q[1]; q[1] = p[1]; p[1] = j; - j = q[2]; q[2] = p[2]; p[2] = j; - j = q[3]; q[3] = p[3]; p[3] = j; - } - // smallval entry is now in position i - - if (smallval != previouscol) { - netindex[previouscol] = (startpos + i) >> 1; - for (j = previouscol + 1; j < smallval; j++) - netindex[j] = i; - previouscol = smallval; - startpos = i; - } - } - netindex[previouscol] = (startpos + maxnetpos) >> 1; - for (j = previouscol + 1; j < 256; j++) - netindex[j] = maxnetpos; // really 256 - } - - /* - Private Method: inxsearch - - searches for BGR values 0..255 and returns a color index - */ - function inxsearch(b, g, r) { - var a, p, dist; - - var bestd = 1000; // biggest possible dist is 256*3 - var best = -1; - - var i = netindex[g]; // index on g - var j = i - 1; // start at netindex[g] and work outwards - - while ((i < netsize) || (j >= 0)) { - if (i < netsize) { - p = network[i]; - dist = p[1] - g; // inx key - if (dist >= bestd) i = netsize; // stop iter - else { - i++; - if (dist < 0) dist = -dist; - a = p[0] - b; if (a < 0) a = -a; - dist += a; - if (dist < bestd) { - a = p[2] - r; if (a < 0) a = -a; - dist += a; - if (dist < bestd) { - bestd = dist; - best = p[3]; - } - } - } - } - if (j >= 0) { - p = network[j]; - dist = g - p[1]; // inx key - reverse dif - if (dist >= bestd) j = -1; // stop iter - else { - j--; - if (dist < 0) dist = -dist; - a = p[0] - b; if (a < 0) a = -a; - dist += a; - if (dist < bestd) { - a = p[2] - r; if (a < 0) a = -a; - dist += a; - if (dist < bestd) { - bestd = dist; - best = p[3]; - } - } - } - } - } - - return best; - } - - /* - Private Method: learn - - "Main Learning Loop" - */ - function learn() { - var i; - - var lengthcount = pixels.length; - var alphadec = 30 + ((samplefac - 1) / 3); - var samplepixels = lengthcount / (3 * samplefac); - var delta = ~~(samplepixels / ncycles); - var alpha = initalpha; - var radius = initradius; - - var rad = radius >> radiusbiasshift; - - if (rad <= 1) rad = 0; - for (i = 0; i < rad; i++) - radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad)); - - var step; - if (lengthcount < minpicturebytes) { - samplefac = 1; - step = 3; - } else if ((lengthcount % prime1) !== 0) { - step = 3 * prime1; - } else if ((lengthcount % prime2) !== 0) { - step = 3 * prime2; - } else if ((lengthcount % prime3) !== 0) { - step = 3 * prime3; - } else { - step = 3 * prime4; - } - - var b, g, r, j; - var pix = 0; // current pixel - - i = 0; - while (i < samplepixels) { - b = (pixels[pix] & 0xff) << netbiasshift; - g = (pixels[pix + 1] & 0xff) << netbiasshift; - r = (pixels[pix + 2] & 0xff) << netbiasshift; - - j = contest(b, g, r); - - altersingle(alpha, j, b, g, r); - if (rad !== 0) alterneigh(rad, j, b, g, r); // alter neighbours - - pix += step; - if (pix >= lengthcount) pix -= lengthcount; - - i++; - - if (delta === 0) delta = 1; - if (i % delta === 0) { - alpha -= alpha / alphadec; - radius -= radius / radiusdec; - rad = radius >> radiusbiasshift; - - if (rad <= 1) rad = 0; - for (j = 0; j < rad; j++) - radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad)); - } - } - } - - /* - Method: buildColormap - - 1. initializes network - 2. trains it - 3. removes misconceptions - 4. builds colorindex - */ - function buildColormap() { - init(); - learn(); - unbiasnet(); - inxbuild(); - } - this.buildColormap = buildColormap; - - /* - Method: getColormap - - builds colormap from the index - - returns array in the format: - - > - > [r, g, b, r, g, b, r, g, b, ..] - > - */ - function getColormap() { - var map = []; - var index = []; - - for (var i = 0; i < netsize; i++) - index[network[i][3]] = i; - - var k = 0; - for (var l = 0; l < netsize; l++) { - var j = index[l]; - map[k++] = (network[j][0]); - map[k++] = (network[j][1]); - map[k++] = (network[j][2]); - } - return map; - } - this.getColormap = getColormap; - - /* - Method: lookupRGB - - looks for the closest *r*, *g*, *b* color in the map and - returns its index - */ - this.lookupRGB = inxsearch; -} - -module.exports = NeuQuant; - -},{}],1563:[function(require,module,exports){ -arguments[4][1550][0].apply(exports,arguments) -},{"./_stream_readable":1565,"./_stream_writable":1567,"_process":41,"core-util-is":1568,"dup":1550,"inherits":1569}],1564:[function(require,module,exports){ -arguments[4][1551][0].apply(exports,arguments) -},{"./_stream_transform":1566,"core-util-is":1568,"dup":1551,"inherits":1569}],1565:[function(require,module,exports){ -(function (process){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -module.exports = Readable; - /**/ -var isArray = require('isarray'); -/**/ - - -/**/ -var Buffer = require('buffer').Buffer; -/**/ - -Readable.ReadableState = ReadableState; - -var EE = require('events').EventEmitter; - -/**/ -if (!EE.listenerCount) EE.listenerCount = function(emitter, type) { - return emitter.listeners(type).length; -}; -/**/ - -var Stream = require('stream'); - -/**/ -var util = require('core-util-is'); -util.inherits = require('inherits'); -/**/ - -var StringDecoder; - - -/**/ -var debug = require('util'); -if (debug && debug.debuglog) { - debug = debug.debuglog('stream'); +var debugUtil = require('util'); +var debug = void 0; +if (debugUtil && debugUtil.debuglog) { + debug = debugUtil.debuglog('stream'); } else { debug = function () {}; } /**/ +var BufferList = require('./internal/streams/BufferList'); +var StringDecoder; util.inherits(Readable, Stream); +function prependListener(emitter, event, fn) { + // Sadly this is not cacheable as some libraries bundle their own + // event emitter implementation with them. + if (typeof emitter.prependListener === 'function') { + return emitter.prependListener(event, fn); + } else { + // This is a hack to make sure that our error handler is attached before any + // userland ones. NEVER DO THIS. This is here only because this code needs + // to continue to work with older versions of Node.js that do not include + // the prependListener() method. The goal is to eventually remove this hack. + if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; + } +} + function ReadableState(options, stream) { - var Duplex = require('./_stream_duplex'); + Duplex = Duplex || require('./_stream_duplex'); options = options || {}; + // object stream flag. Used to make read(n) ignore n and to + // make all the buffer merging and length checks go away + this.objectMode = !!options.objectMode; + + if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode; + // the point at which it stops calling _read() to fill the buffer // Note: 0 is a valid value, means "don't call _read preemptively ever" var hwm = options.highWaterMark; - var defaultHwm = options.objectMode ? 16 : 16 * 1024; - this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; // cast to ints. - this.highWaterMark = ~~this.highWaterMark; + this.highWaterMark = ~ ~this.highWaterMark; - this.buffer = []; + // A linked list is used to store data chunks instead of an array because the + // linked list can remove elements from the beginning faster than + // array.shift() + this.buffer = new BufferList(); this.length = 0; this.pipes = null; this.pipesCount = 0; @@ -178484,14 +164998,7 @@ function ReadableState(options, stream) { this.needReadable = false; this.emittedReadable = false; this.readableListening = false; - - - // object stream flag. Used to make read(n) ignore n and to - // make all the buffer merging and length checks go away - this.objectMode = !!options.objectMode; - - if (stream instanceof Duplex) - this.objectMode = this.objectMode || !!options.readableObjectMode; + this.resumeScheduled = false; // Crypto is kind of old and crusty. Historically, its default string // encoding is 'binary' so we have to make this configurable. @@ -178511,24 +165018,24 @@ function ReadableState(options, stream) { this.decoder = null; this.encoding = null; if (options.encoding) { - if (!StringDecoder) - StringDecoder = require('string_decoder/').StringDecoder; + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; this.decoder = new StringDecoder(options.encoding); this.encoding = options.encoding; } } function Readable(options) { - var Duplex = require('./_stream_duplex'); + Duplex = Duplex || require('./_stream_duplex'); - if (!(this instanceof Readable)) - return new Readable(options); + if (!(this instanceof Readable)) return new Readable(options); this._readableState = new ReadableState(options, this); // legacy this.readable = true; + if (options && typeof options.read === 'function') this._read = options.read; + Stream.call(this); } @@ -178536,13 +165043,13 @@ function Readable(options) { // This returns true if the highWaterMark has not been hit yet, // similar to how Writable.write() returns true if you should // write() some more. -Readable.prototype.push = function(chunk, encoding) { +Readable.prototype.push = function (chunk, encoding) { var state = this._readableState; - if (util.isString(chunk) && !state.objectMode) { + if (!state.objectMode && typeof chunk === 'string') { encoding = encoding || state.defaultEncoding; if (encoding !== state.encoding) { - chunk = new Buffer(chunk, encoding); + chunk = bufferShim.from(chunk, encoding); encoding = ''; } } @@ -178551,47 +165058,52 @@ Readable.prototype.push = function(chunk, encoding) { }; // Unshift should *always* be something directly out of read() -Readable.prototype.unshift = function(chunk) { +Readable.prototype.unshift = function (chunk) { var state = this._readableState; return readableAddChunk(this, state, chunk, '', true); }; +Readable.prototype.isPaused = function () { + return this._readableState.flowing === false; +}; + function readableAddChunk(stream, state, chunk, encoding, addToFront) { var er = chunkInvalid(state, chunk); if (er) { stream.emit('error', er); - } else if (util.isNullOrUndefined(chunk)) { + } else if (chunk === null) { state.reading = false; - if (!state.ended) - onEofChunk(stream, state); + onEofChunk(stream, state); } else if (state.objectMode || chunk && chunk.length > 0) { if (state.ended && !addToFront) { var e = new Error('stream.push() after EOF'); stream.emit('error', e); } else if (state.endEmitted && addToFront) { - var e = new Error('stream.unshift() after end event'); - stream.emit('error', e); + var _e = new Error('stream.unshift() after end event'); + stream.emit('error', _e); } else { - if (state.decoder && !addToFront && !encoding) + var skipAdd; + if (state.decoder && !addToFront && !encoding) { chunk = state.decoder.write(chunk); + skipAdd = !state.objectMode && chunk.length === 0; + } - if (!addToFront) - state.reading = false; + if (!addToFront) state.reading = false; - // if we want the data now, just emit it. - if (state.flowing && state.length === 0 && !state.sync) { - stream.emit('data', chunk); - stream.read(0); - } else { - // update the buffer info. - state.length += state.objectMode ? 1 : chunk.length; - if (addToFront) - state.buffer.unshift(chunk); - else - state.buffer.push(chunk); + // Don't add to the buffer if we've decoded to an empty string chunk and + // we're not in object mode + if (!skipAdd) { + // if we want the data now, just emit it. + if (state.flowing && state.length === 0 && !state.sync) { + stream.emit('data', chunk); + stream.read(0); + } else { + // update the buffer info. + state.length += state.objectMode ? 1 : chunk.length; + if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); - if (state.needReadable) - emitReadable(stream); + if (state.needReadable) emitReadable(stream); + } } maybeReadMore(stream, state); @@ -178603,8 +165115,6 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) { return needMoreData(state); } - - // if it's past the high water mark, we can push in some more. // Also, if we have no data yet, we can stand some // more bytes. This is to work around cases where hwm=0, @@ -178613,92 +165123,71 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) { // needReadable was set, then we ought to push more, so that another // 'readable' event will be triggered. function needMoreData(state) { - return !state.ended && - (state.needReadable || - state.length < state.highWaterMark || - state.length === 0); + return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); } // backwards compatibility. -Readable.prototype.setEncoding = function(enc) { - if (!StringDecoder) - StringDecoder = require('string_decoder/').StringDecoder; +Readable.prototype.setEncoding = function (enc) { + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; this._readableState.decoder = new StringDecoder(enc); this._readableState.encoding = enc; return this; }; -// Don't raise the hwm > 128MB +// Don't raise the hwm > 8MB var MAX_HWM = 0x800000; -function roundUpToNextPowerOf2(n) { +function computeNewHighWaterMark(n) { if (n >= MAX_HWM) { n = MAX_HWM; } else { - // Get the next highest power of 2 + // Get the next highest power of 2 to prevent increasing hwm excessively in + // tiny amounts n--; - for (var p = 1; p < 32; p <<= 1) n |= n >> p; + n |= n >>> 1; + n |= n >>> 2; + n |= n >>> 4; + n |= n >>> 8; + n |= n >>> 16; n++; } return n; } +// This function is designed to be inlinable, so please take care when making +// changes to the function body. function howMuchToRead(n, state) { - if (state.length === 0 && state.ended) - return 0; - - if (state.objectMode) - return n === 0 ? 0 : 1; - - if (isNaN(n) || util.isNull(n)) { - // only flow one buffer at a time - if (state.flowing && state.buffer.length) - return state.buffer[0].length; - else - return state.length; + if (n <= 0 || state.length === 0 && state.ended) return 0; + if (state.objectMode) return 1; + if (n !== n) { + // Only flow one buffer at a time + if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; } - - if (n <= 0) + // If we're asking for more than the current hwm, then raise the hwm. + if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); + if (n <= state.length) return n; + // Don't have enough + if (!state.ended) { + state.needReadable = true; return 0; - - // If we're asking for more than the target buffer level, - // then raise the water mark. Bump up to the next highest - // power of 2, to prevent increasing it excessively in tiny - // amounts. - if (n > state.highWaterMark) - state.highWaterMark = roundUpToNextPowerOf2(n); - - // don't have that much. return null, unless we've ended. - if (n > state.length) { - if (!state.ended) { - state.needReadable = true; - return 0; - } else - return state.length; } - - return n; + return state.length; } // you can override either this method, or the async _read(n) below. -Readable.prototype.read = function(n) { +Readable.prototype.read = function (n) { debug('read', n); + n = parseInt(n, 10); var state = this._readableState; var nOrig = n; - if (!util.isNumber(n) || n > 0) - state.emittedReadable = false; + if (n !== 0) state.emittedReadable = false; // if we're doing read(0) to trigger a readable event, but we // already have a bunch of data in the buffer, then just trigger // the 'readable' event and move on. - if (n === 0 && - state.needReadable && - (state.length >= state.highWaterMark || state.ended)) { + if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { debug('read: emitReadable', state.length, state.ended); - if (state.length === 0 && state.ended) - endReadable(this); - else - emitReadable(this); + if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this); return null; } @@ -178706,8 +165195,7 @@ Readable.prototype.read = function(n) { // if we've ended, and we're now clear, then finish it up. if (n === 0 && state.ended) { - if (state.length === 0) - endReadable(this); + if (state.length === 0) endReadable(this); return null; } @@ -178748,67 +165236,55 @@ Readable.prototype.read = function(n) { if (state.ended || state.reading) { doRead = false; debug('reading or ended', doRead); - } - - if (doRead) { + } else if (doRead) { debug('do read'); state.reading = true; state.sync = true; // if the length is currently zero, then we *need* a readable event. - if (state.length === 0) - state.needReadable = true; + if (state.length === 0) state.needReadable = true; // call internal read method this._read(state.highWaterMark); state.sync = false; + // If _read pushed data synchronously, then `reading` will be false, + // and we need to re-evaluate how much data we can return to the user. + if (!state.reading) n = howMuchToRead(nOrig, state); } - // If _read pushed data synchronously, then `reading` will be false, - // and we need to re-evaluate how much data we can return to the user. - if (doRead && !state.reading) - n = howMuchToRead(nOrig, state); - var ret; - if (n > 0) - ret = fromList(n, state); - else - ret = null; + if (n > 0) ret = fromList(n, state);else ret = null; - if (util.isNull(ret)) { + if (ret === null) { state.needReadable = true; n = 0; + } else { + state.length -= n; } - state.length -= n; + if (state.length === 0) { + // If we have nothing in the buffer, then we want to know + // as soon as we *do* get something into the buffer. + if (!state.ended) state.needReadable = true; - // If we have nothing in the buffer, then we want to know - // as soon as we *do* get something into the buffer. - if (state.length === 0 && !state.ended) - state.needReadable = true; + // If we tried to read() past the EOF, then emit end on the next tick. + if (nOrig !== n && state.ended) endReadable(this); + } - // If we tried to read() past the EOF, then emit end on the next tick. - if (nOrig !== n && state.ended && state.length === 0) - endReadable(this); - - if (!util.isNull(ret)) - this.emit('data', ret); + if (ret !== null) this.emit('data', ret); return ret; }; function chunkInvalid(state, chunk) { var er = null; - if (!util.isBuffer(chunk) && - !util.isString(chunk) && - !util.isNullOrUndefined(chunk) && - !state.objectMode) { + if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) { er = new TypeError('Invalid non-string/buffer chunk'); } return er; } - function onEofChunk(stream, state) { - if (state.decoder && !state.ended) { + if (state.ended) return; + if (state.decoder) { var chunk = state.decoder.end(); if (chunk && chunk.length) { state.buffer.push(chunk); @@ -178830,12 +165306,7 @@ function emitReadable(stream) { if (!state.emittedReadable) { debug('emitReadable', state.flowing); state.emittedReadable = true; - if (state.sync) - process.nextTick(function() { - emitReadable_(stream); - }); - else - emitReadable_(stream); + if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream); } } @@ -178845,7 +165316,6 @@ function emitReadable_(stream) { flow(stream); } - // at this point, the user has presumably seen the 'readable' event, // and called read() to consume some data. that may have triggered // in turn another _read(n) call, in which case reading = true if @@ -178855,23 +165325,18 @@ function emitReadable_(stream) { function maybeReadMore(stream, state) { if (!state.readingMore) { state.readingMore = true; - process.nextTick(function() { - maybeReadMore_(stream, state); - }); + processNextTick(maybeReadMore_, stream, state); } } function maybeReadMore_(stream, state) { var len = state.length; - while (!state.reading && !state.flowing && !state.ended && - state.length < state.highWaterMark) { + while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { debug('maybeReadMore read 0'); stream.read(0); if (len === state.length) // didn't get any data, stop spinning. - break; - else - len = state.length; + break;else len = state.length; } state.readingMore = false; } @@ -178880,11 +165345,11 @@ function maybeReadMore_(stream, state) { // call cb(er, data) where data is <= n in length. // for virtual (non-string, non-buffer) streams, "length" is somewhat // arbitrary, and perhaps not very meaningful. -Readable.prototype._read = function(n) { - this.emit('error', new Error('not implemented')); +Readable.prototype._read = function (n) { + this.emit('error', new Error('_read() is not implemented')); }; -Readable.prototype.pipe = function(dest, pipeOpts) { +Readable.prototype.pipe = function (dest, pipeOpts) { var src = this; var state = this._readableState; @@ -178902,15 +165367,10 @@ Readable.prototype.pipe = function(dest, pipeOpts) { state.pipesCount += 1; debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); - var doEnd = (!pipeOpts || pipeOpts.end !== false) && - dest !== process.stdout && - dest !== process.stderr; + var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; var endFn = doEnd ? onend : cleanup; - if (state.endEmitted) - process.nextTick(endFn); - else - src.once('end', endFn); + if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn); dest.on('unpipe', onunpipe); function onunpipe(readable) { @@ -178932,6 +165392,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { var ondrain = pipeOnDrain(src); dest.on('drain', ondrain); + var cleanedUp = false; function cleanup() { debug('cleanup'); // cleanup event handlers once the pipe is broken @@ -178944,24 +165405,36 @@ Readable.prototype.pipe = function(dest, pipeOpts) { src.removeListener('end', cleanup); src.removeListener('data', ondata); + cleanedUp = true; + // if the reader is waiting for a drain event from this // specific writer, then it would cause it to never start // flowing again. // So, if this is awaiting a drain, then we just call it now. // If we don't know, then assume that we are waiting for one. - if (state.awaitDrain && - (!dest._writableState || dest._writableState.needDrain)) - ondrain(); + if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); } + // If the user pushes more data while we're writing to dest then we'll end up + // in ondata again. However, we only want to increase awaitDrain once because + // dest will only emit one 'drain' event for the multiple writes. + // => Introduce a guard on increasing awaitDrain. + var increasedAwaitDrain = false; src.on('data', ondata); function ondata(chunk) { debug('ondata'); + increasedAwaitDrain = false; var ret = dest.write(chunk); - if (false === ret) { - debug('false write response, pause', - src._readableState.awaitDrain); - src._readableState.awaitDrain++; + if (false === ret && !increasedAwaitDrain) { + // If the user unpiped during `dest.write()`, it is possible + // to get stuck in a permanently paused state if that write + // also returned false. + // => Check whether `dest` is still a piping destination. + if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { + debug('false write response, pause', src._readableState.awaitDrain); + src._readableState.awaitDrain++; + increasedAwaitDrain = true; + } src.pause(); } } @@ -178972,19 +165445,11 @@ Readable.prototype.pipe = function(dest, pipeOpts) { debug('onerror', er); unpipe(); dest.removeListener('error', onerror); - if (EE.listenerCount(dest, 'error') === 0) - dest.emit('error', er); + if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); } - // This is a brutally ugly hack to make sure that our error handler - // is attached before any userland ones. NEVER DO THIS. - if (!dest._events || !dest._events.error) - dest.on('error', onerror); - else if (isArray(dest._events.error)) - dest._events.error.unshift(onerror); - else - dest._events.error = [onerror, dest._events.error]; - + // Make sure our error handler is attached before userland ones. + prependListener(dest, 'error', onerror); // Both close and finish should trigger unpipe, but only once. function onclose() { @@ -179017,41 +165482,35 @@ Readable.prototype.pipe = function(dest, pipeOpts) { }; function pipeOnDrain(src) { - return function() { + return function () { var state = src._readableState; debug('pipeOnDrain', state.awaitDrain); - if (state.awaitDrain) - state.awaitDrain--; - if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) { + if (state.awaitDrain) state.awaitDrain--; + if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { state.flowing = true; flow(src); } }; } - -Readable.prototype.unpipe = function(dest) { +Readable.prototype.unpipe = function (dest) { var state = this._readableState; // if we're not piping anywhere, then do nothing. - if (state.pipesCount === 0) - return this; + if (state.pipesCount === 0) return this; // just one destination. most common case. if (state.pipesCount === 1) { // passed in one, but it's not the right one. - if (dest && dest !== state.pipes) - return this; + if (dest && dest !== state.pipes) return this; - if (!dest) - dest = state.pipes; + if (!dest) dest = state.pipes; // got a match. state.pipes = null; state.pipesCount = 0; state.flowing = false; - if (dest) - dest.emit('unpipe', this); + if (dest) dest.emit('unpipe', this); return this; } @@ -179065,20 +165524,18 @@ Readable.prototype.unpipe = function(dest) { state.pipesCount = 0; state.flowing = false; - for (var i = 0; i < len; i++) + for (var i = 0; i < len; i++) { dests[i].emit('unpipe', this); - return this; + }return this; } // try to find the right one. - var i = indexOf(state.pipes, dest); - if (i === -1) - return this; + var index = indexOf(state.pipes, dest); + if (index === -1) return this; - state.pipes.splice(i, 1); + state.pipes.splice(index, 1); state.pipesCount -= 1; - if (state.pipesCount === 1) - state.pipes = state.pipes[0]; + if (state.pipesCount === 1) state.pipes = state.pipes[0]; dest.emit('unpipe', this); @@ -179087,27 +165544,19 @@ Readable.prototype.unpipe = function(dest) { // set up data events if they are asked for // Ensure readable listeners eventually get something -Readable.prototype.on = function(ev, fn) { +Readable.prototype.on = function (ev, fn) { var res = Stream.prototype.on.call(this, ev, fn); - // If listening to data, and it has not explicitly been paused, - // then call resume to start the flow of data on the next tick. - if (ev === 'data' && false !== this._readableState.flowing) { - this.resume(); - } - - if (ev === 'readable' && this.readable) { + if (ev === 'data') { + // Start flowing on next tick if stream isn't explicitly paused + if (this._readableState.flowing !== false) this.resume(); + } else if (ev === 'readable') { var state = this._readableState; - if (!state.readableListening) { - state.readableListening = true; + if (!state.endEmitted && !state.readableListening) { + state.readableListening = state.needReadable = true; state.emittedReadable = false; - state.needReadable = true; if (!state.reading) { - var self = this; - process.nextTick(function() { - debug('readable nexttick read 0'); - self.read(0); - }); + processNextTick(nReadingNextTick, this); } else if (state.length) { emitReadable(this, state); } @@ -179118,17 +165567,18 @@ Readable.prototype.on = function(ev, fn) { }; Readable.prototype.addListener = Readable.prototype.on; +function nReadingNextTick(self) { + debug('readable nexttick read 0'); + self.read(0); +} + // pause() and resume() are remnants of the legacy readable stream API // If the user uses them, then switch into old mode. -Readable.prototype.resume = function() { +Readable.prototype.resume = function () { var state = this._readableState; if (!state.flowing) { debug('resume'); state.flowing = true; - if (!state.reading) { - debug('resume read 0'); - this.read(0); - } resume(this, state); } return this; @@ -179137,21 +165587,24 @@ Readable.prototype.resume = function() { function resume(stream, state) { if (!state.resumeScheduled) { state.resumeScheduled = true; - process.nextTick(function() { - resume_(stream, state); - }); + processNextTick(resume_, stream, state); } } function resume_(stream, state) { + if (!state.reading) { + debug('resume read 0'); + stream.read(0); + } + state.resumeScheduled = false; + state.awaitDrain = 0; stream.emit('resume'); flow(stream); - if (state.flowing && !state.reading) - stream.read(0); + if (state.flowing && !state.reading) stream.read(0); } -Readable.prototype.pause = function() { +Readable.prototype.pause = function () { debug('call pause flowing=%j', this._readableState.flowing); if (false !== this._readableState.flowing) { debug('pause'); @@ -179164,38 +165617,33 @@ Readable.prototype.pause = function() { function flow(stream) { var state = stream._readableState; debug('flow', state.flowing); - if (state.flowing) { - do { - var chunk = stream.read(); - } while (null !== chunk && state.flowing); - } + while (state.flowing && stream.read() !== null) {} } // wrap an old-style stream as the async data source. // This is *not* part of the readable stream interface. // It is an ugly unfortunate mess of history. -Readable.prototype.wrap = function(stream) { +Readable.prototype.wrap = function (stream) { var state = this._readableState; var paused = false; var self = this; - stream.on('end', function() { + stream.on('end', function () { debug('wrapped end'); if (state.decoder && !state.ended) { var chunk = state.decoder.end(); - if (chunk && chunk.length) - self.push(chunk); + if (chunk && chunk.length) self.push(chunk); } self.push(null); }); - stream.on('data', function(chunk) { + stream.on('data', function (chunk) { debug('wrapped data'); - if (state.decoder) - chunk = state.decoder.write(chunk); - if (!chunk || !state.objectMode && !chunk.length) - return; + if (state.decoder) chunk = state.decoder.write(chunk); + + // don't skip over falsy values in objectMode + if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; var ret = self.push(chunk); if (!ret) { @@ -179207,22 +165655,24 @@ Readable.prototype.wrap = function(stream) { // proxy all the other methods. // important when wrapping filters and duplexes. for (var i in stream) { - if (util.isFunction(stream[i]) && util.isUndefined(this[i])) { - this[i] = function(method) { return function() { - return stream[method].apply(stream, arguments); - }}(i); + if (this[i] === undefined && typeof stream[i] === 'function') { + this[i] = function (method) { + return function () { + return stream[method].apply(stream, arguments); + }; + }(i); } } // proxy certain important events. var events = ['error', 'close', 'destroy', 'pause', 'resume']; - forEach(events, function(ev) { + forEach(events, function (ev) { stream.on(ev, self.emit.bind(self, ev)); }); // when we try to consume some more bytes, simply unpause the // underlying stream. - self._read = function(n) { + self._read = function (n) { debug('wrapped _read', n); if (paused) { paused = false; @@ -179233,135 +165683,145 @@ Readable.prototype.wrap = function(stream) { return self; }; - - // exposed for testing purposes only. Readable._fromList = fromList; // Pluck off n bytes from an array of buffers. // Length is the combined lengths of all the buffers in the list. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. function fromList(n, state) { - var list = state.buffer; - var length = state.length; - var stringMode = !!state.decoder; - var objectMode = !!state.objectMode; + // nothing buffered + if (state.length === 0) return null; + var ret; - - // nothing in the list, definitely empty. - if (list.length === 0) - return null; - - if (length === 0) - ret = null; - else if (objectMode) - ret = list.shift(); - else if (!n || n >= length) { - // read it all, truncate the array. - if (stringMode) - ret = list.join(''); - else - ret = Buffer.concat(list, length); - list.length = 0; + if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { + // read it all, truncate the list + if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length); + state.buffer.clear(); } else { - // read just some of it. - if (n < list[0].length) { - // just take a part of the first list item. - // slice is the same for buffers and strings. - var buf = list[0]; - ret = buf.slice(0, n); - list[0] = buf.slice(n); - } else if (n === list[0].length) { - // first list is a perfect match - ret = list.shift(); - } else { - // complex case. - // we have enough to cover it, but it spans past the first buffer. - if (stringMode) - ret = ''; - else - ret = new Buffer(n); - - var c = 0; - for (var i = 0, l = list.length; i < l && c < n; i++) { - var buf = list[0]; - var cpy = Math.min(n - c, buf.length); - - if (stringMode) - ret += buf.slice(0, cpy); - else - buf.copy(ret, c, 0, cpy); - - if (cpy < buf.length) - list[0] = buf.slice(cpy); - else - list.shift(); - - c += cpy; - } - } + // read part of list + ret = fromListPartial(n, state.buffer, state.decoder); } return ret; } +// Extracts only enough buffered data to satisfy the amount requested. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function fromListPartial(n, list, hasStrings) { + var ret; + if (n < list.head.data.length) { + // slice is the same for buffers and strings + ret = list.head.data.slice(0, n); + list.head.data = list.head.data.slice(n); + } else if (n === list.head.data.length) { + // first chunk is a perfect match + ret = list.shift(); + } else { + // result spans more than one buffer + ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); + } + return ret; +} + +// Copies a specified amount of characters from the list of buffered data +// chunks. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function copyFromBufferString(n, list) { + var p = list.head; + var c = 1; + var ret = p.data; + n -= ret.length; + while (p = p.next) { + var str = p.data; + var nb = n > str.length ? str.length : n; + if (nb === str.length) ret += str;else ret += str.slice(0, n); + n -= nb; + if (n === 0) { + if (nb === str.length) { + ++c; + if (p.next) list.head = p.next;else list.head = list.tail = null; + } else { + list.head = p; + p.data = str.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; +} + +// Copies a specified amount of bytes from the list of buffered data chunks. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function copyFromBuffer(n, list) { + var ret = bufferShim.allocUnsafe(n); + var p = list.head; + var c = 1; + p.data.copy(ret); + n -= p.data.length; + while (p = p.next) { + var buf = p.data; + var nb = n > buf.length ? buf.length : n; + buf.copy(ret, ret.length - n, 0, nb); + n -= nb; + if (n === 0) { + if (nb === buf.length) { + ++c; + if (p.next) list.head = p.next;else list.head = list.tail = null; + } else { + list.head = p; + p.data = buf.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; +} + function endReadable(stream) { var state = stream._readableState; // If we get here before consuming all the bytes, then that is a // bug in node. Should never happen. - if (state.length > 0) - throw new Error('endReadable called on non-empty stream'); + if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); if (!state.endEmitted) { state.ended = true; - process.nextTick(function() { - // Check that we didn't get one last unshift. - if (!state.endEmitted && state.length === 0) { - state.endEmitted = true; - stream.readable = false; - stream.emit('end'); - } - }); + processNextTick(endReadableNT, state, stream); } } -function forEach (xs, f) { +function endReadableNT(state, stream) { + // Check that we didn't get one last unshift. + if (!state.endEmitted && state.length === 0) { + state.endEmitted = true; + stream.readable = false; + stream.emit('end'); + } +} + +function forEach(xs, f) { for (var i = 0, l = xs.length; i < l; i++) { f(xs[i], i); } } -function indexOf (xs, x) { +function indexOf(xs, x) { for (var i = 0, l = xs.length; i < l; i++) { if (xs[i] === x) return i; } return -1; } - }).call(this,require('_process')) -},{"./_stream_duplex":1563,"_process":41,"buffer":33,"core-util-is":1568,"events":37,"inherits":1569,"isarray":1570,"stream":62,"string_decoder/":1571,"util":19}],1566:[function(require,module,exports){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - - +},{"./_stream_duplex":935,"./internal/streams/BufferList":940,"_process":923,"buffer":46,"buffer-shims":48,"core-util-is":78,"events":95,"inherits":266,"isarray":272,"process-nextick-args":922,"string_decoder/":979,"util":43}],938:[function(require,module,exports){ // a transform stream is a readable/writable stream where you do // something with the data. Sometimes it's called a "filter", // but that's not a great name for it, since that implies a thing where @@ -179404,6 +165864,8 @@ function indexOf (xs, x) { // would be consumed, and then the rest would wait (un-transformed) until // the results of the previous transformed chunk were consumed. +'use strict'; + module.exports = Transform; var Duplex = require('./_stream_duplex'); @@ -179415,9 +165877,8 @@ util.inherits = require('inherits'); util.inherits(Transform, Duplex); - -function TransformState(options, stream) { - this.afterTransform = function(er, data) { +function TransformState(stream) { + this.afterTransform = function (er, data) { return afterTransform(stream, er, data); }; @@ -179425,6 +165886,7 @@ function TransformState(options, stream) { this.transforming = false; this.writecb = null; this.writechunk = null; + this.writeencoding = null; } function afterTransform(stream, er, data) { @@ -179433,17 +165895,14 @@ function afterTransform(stream, er, data) { var cb = ts.writecb; - if (!cb) - return stream.emit('error', new Error('no writecb in Transform class')); + if (!cb) return stream.emit('error', new Error('no writecb in Transform class')); ts.writechunk = null; ts.writecb = null; - if (!util.isNullOrUndefined(data)) - stream.push(data); + if (data !== null && data !== undefined) stream.push(data); - if (cb) - cb(er); + cb(er); var rs = stream._readableState; rs.reading = false; @@ -179452,16 +165911,13 @@ function afterTransform(stream, er, data) { } } - function Transform(options) { - if (!(this instanceof Transform)) - return new Transform(options); + if (!(this instanceof Transform)) return new Transform(options); Duplex.call(this, options); - this._transformState = new TransformState(options, this); + this._transformState = new TransformState(this); - // when the writable side finishes, then flush out anything remaining. var stream = this; // start out asking for a readable event once data is transformed. @@ -179472,17 +165928,21 @@ function Transform(options) { // sync guard flag. this._readableState.sync = false; - this.once('prefinish', function() { - if (util.isFunction(this._flush)) - this._flush(function(er) { - done(stream, er); - }); - else - done(stream); + if (options) { + if (typeof options.transform === 'function') this._transform = options.transform; + + if (typeof options.flush === 'function') this._flush = options.flush; + } + + // When the writable side finishes, then flush out anything remaining. + this.once('prefinish', function () { + if (typeof this._flush === 'function') this._flush(function (er, data) { + done(stream, er, data); + });else done(stream); }); } -Transform.prototype.push = function(chunk, encoding) { +Transform.prototype.push = function (chunk, encoding) { this._transformState.needTransform = false; return Duplex.prototype.push.call(this, chunk, encoding); }; @@ -179497,31 +165957,28 @@ Transform.prototype.push = function(chunk, encoding) { // Call `cb(err)` when you are done with this chunk. If you pass // an error, then that'll put the hurt on the whole operation. If you // never call cb(), then you'll never get another chunk. -Transform.prototype._transform = function(chunk, encoding, cb) { - throw new Error('not implemented'); +Transform.prototype._transform = function (chunk, encoding, cb) { + throw new Error('_transform() is not implemented'); }; -Transform.prototype._write = function(chunk, encoding, cb) { +Transform.prototype._write = function (chunk, encoding, cb) { var ts = this._transformState; ts.writecb = cb; ts.writechunk = chunk; ts.writeencoding = encoding; if (!ts.transforming) { var rs = this._readableState; - if (ts.needTransform || - rs.needReadable || - rs.length < rs.highWaterMark) - this._read(rs.highWaterMark); + if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); } }; // Doesn't matter what the args are here. // _transform does all the work. // That we got here means that the readable side wants more data. -Transform.prototype._read = function(n) { +Transform.prototype._read = function (n) { var ts = this._transformState; - if (!util.isNull(ts.writechunk) && ts.writecb && !ts.transforming) { + if (ts.writechunk !== null && ts.writecb && !ts.transforming) { ts.transforming = true; this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); } else { @@ -179531,98 +165988,106 @@ Transform.prototype._read = function(n) { } }; +function done(stream, er, data) { + if (er) return stream.emit('error', er); -function done(stream, er) { - if (er) - return stream.emit('error', er); + if (data !== null && data !== undefined) stream.push(data); // if there's nothing in the write buffer, then that means // that nothing more will ever be provided var ws = stream._writableState; var ts = stream._transformState; - if (ws.length) - throw new Error('calling transform done when ws.length != 0'); + if (ws.length) throw new Error('Calling transform done when ws.length != 0'); - if (ts.transforming) - throw new Error('calling transform done when still transforming'); + if (ts.transforming) throw new Error('Calling transform done when still transforming'); return stream.push(null); } - -},{"./_stream_duplex":1563,"core-util-is":1568,"inherits":1569}],1567:[function(require,module,exports){ +},{"./_stream_duplex":935,"core-util-is":78,"inherits":266}],939:[function(require,module,exports){ (function (process){ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - // A bit simpler than readable streams. -// Implement an async ._write(chunk, cb), and it'll handle all +// Implement an async ._write(chunk, encoding, cb), and it'll handle all // the drain event emission and buffering. +'use strict'; + module.exports = Writable; /**/ -var Buffer = require('buffer').Buffer; +var processNextTick = require('process-nextick-args'); +/**/ + +/**/ +var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : processNextTick; +/**/ + +/**/ +var Duplex; /**/ Writable.WritableState = WritableState; - /**/ var util = require('core-util-is'); util.inherits = require('inherits'); /**/ -var Stream = require('stream'); +/**/ +var internalUtil = { + deprecate: require('util-deprecate') +}; +/**/ + +/**/ +var Stream; +(function () { + try { + Stream = require('st' + 'ream'); + } catch (_) {} finally { + if (!Stream) Stream = require('events').EventEmitter; + } +})(); +/**/ + +var Buffer = require('buffer').Buffer; +/**/ +var bufferShim = require('buffer-shims'); +/**/ util.inherits(Writable, Stream); +function nop() {} + function WriteReq(chunk, encoding, cb) { this.chunk = chunk; this.encoding = encoding; this.callback = cb; + this.next = null; } function WritableState(options, stream) { - var Duplex = require('./_stream_duplex'); + Duplex = Duplex || require('./_stream_duplex'); options = options || {}; - // the point at which write() starts returning false - // Note: 0 is a valid value, means that we always return false if - // the entire buffer is not flushed immediately on write() - var hwm = options.highWaterMark; - var defaultHwm = options.objectMode ? 16 : 16 * 1024; - this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; - // object stream flag to indicate whether or not this stream // contains buffers or objects. this.objectMode = !!options.objectMode; - if (stream instanceof Duplex) - this.objectMode = this.objectMode || !!options.writableObjectMode; + if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode; + + // the point at which write() starts returning false + // Note: 0 is a valid value, means that we always return false if + // the entire buffer is not flushed immediately on write() + var hwm = options.highWaterMark; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; // cast to ints. - this.highWaterMark = ~~this.highWaterMark; + this.highWaterMark = ~ ~this.highWaterMark; + // drain event flag. this.needDrain = false; // at the start of calling end() this.ending = false; @@ -179665,7 +166130,7 @@ function WritableState(options, stream) { this.bufferProcessing = false; // the callback that's passed to _write(chunk,cb) - this.onwrite = function(er) { + this.onwrite = function (er) { onwrite(stream, er); }; @@ -179675,7 +166140,8 @@ function WritableState(options, stream) { // the amount that is being written when _write is called. this.writelen = 0; - this.buffer = []; + this.bufferedRequest = null; + this.lastBufferedRequest = null; // number of pending user-supplied write callbacks // this must be 0 before 'finish' can be emitted @@ -179687,37 +166153,91 @@ function WritableState(options, stream) { // True if the error was already emitted and should not be thrown again this.errorEmitted = false; + + // count buffered requests + this.bufferedRequestCount = 0; + + // allocate the first CorkedRequest, there is always + // one allocated and free to use, and we maintain at most two + this.corkedRequestsFree = new CorkedRequest(this); +} + +WritableState.prototype.getBuffer = function getBuffer() { + var current = this.bufferedRequest; + var out = []; + while (current) { + out.push(current); + current = current.next; + } + return out; +}; + +(function () { + try { + Object.defineProperty(WritableState.prototype, 'buffer', { + get: internalUtil.deprecate(function () { + return this.getBuffer(); + }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.') + }); + } catch (_) {} +})(); + +// Test _writableState for inheritance to account for Duplex streams, +// whose prototype chain only points to Readable. +var realHasInstance; +if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { + realHasInstance = Function.prototype[Symbol.hasInstance]; + Object.defineProperty(Writable, Symbol.hasInstance, { + value: function (object) { + if (realHasInstance.call(this, object)) return true; + + return object && object._writableState instanceof WritableState; + } + }); +} else { + realHasInstance = function (object) { + return object instanceof this; + }; } function Writable(options) { - var Duplex = require('./_stream_duplex'); + Duplex = Duplex || require('./_stream_duplex'); - // Writable ctor is applied to Duplexes, though they're not - // instanceof Writable, they're instanceof Readable. - if (!(this instanceof Writable) && !(this instanceof Duplex)) + // Writable ctor is applied to Duplexes, too. + // `realHasInstance` is necessary because using plain `instanceof` + // would return false, as no `_writableState` property is attached. + + // Trying to use the custom `instanceof` for Writable here will also break the + // Node.js LazyTransform implementation, which has a non-trivial getter for + // `_writableState` that would lead to infinite recursion. + if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) { return new Writable(options); + } this._writableState = new WritableState(options, this); // legacy. this.writable = true; + if (options) { + if (typeof options.write === 'function') this._write = options.write; + + if (typeof options.writev === 'function') this._writev = options.writev; + } + Stream.call(this); } // Otherwise people can pipe Writable streams, which is just wrong. -Writable.prototype.pipe = function() { - this.emit('error', new Error('Cannot pipe. Not readable.')); +Writable.prototype.pipe = function () { + this.emit('error', new Error('Cannot pipe, not readable')); }; - -function writeAfterEnd(stream, state, cb) { +function writeAfterEnd(stream, cb) { var er = new Error('write after end'); // TODO: defer error events consistently everywhere, not just the cb stream.emit('error', er); - process.nextTick(function() { - cb(er); - }); + processNextTick(cb, er); } // If we get something that is not a buffer, string, null, or undefined, @@ -179727,40 +166247,37 @@ function writeAfterEnd(stream, state, cb) { // how many bytes or characters. function validChunk(stream, state, chunk, cb) { var valid = true; - if (!util.isBuffer(chunk) && - !util.isString(chunk) && - !util.isNullOrUndefined(chunk) && - !state.objectMode) { - var er = new TypeError('Invalid non-string/buffer chunk'); + var er = false; + // Always throw error if a null is written + // if we are not in object mode then throw + // if it is not a buffer, string, or undefined. + if (chunk === null) { + er = new TypeError('May not write null values to stream'); + } else if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + if (er) { stream.emit('error', er); - process.nextTick(function() { - cb(er); - }); + processNextTick(cb, er); valid = false; } return valid; } -Writable.prototype.write = function(chunk, encoding, cb) { +Writable.prototype.write = function (chunk, encoding, cb) { var state = this._writableState; var ret = false; - if (util.isFunction(encoding)) { + if (typeof encoding === 'function') { cb = encoding; encoding = null; } - if (util.isBuffer(chunk)) - encoding = 'buffer'; - else if (!encoding) - encoding = state.defaultEncoding; + if (Buffer.isBuffer(chunk)) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding; - if (!util.isFunction(cb)) - cb = function() {}; + if (typeof cb !== 'function') cb = nop; - if (state.ended) - writeAfterEnd(this, state, cb); - else if (validChunk(this, state, chunk, cb)) { + if (state.ended) writeAfterEnd(this, cb);else if (validChunk(this, state, chunk, cb)) { state.pendingcb++; ret = writeOrBuffer(this, state, chunk, encoding, cb); } @@ -179768,32 +166285,33 @@ Writable.prototype.write = function(chunk, encoding, cb) { return ret; }; -Writable.prototype.cork = function() { +Writable.prototype.cork = function () { var state = this._writableState; state.corked++; }; -Writable.prototype.uncork = function() { +Writable.prototype.uncork = function () { var state = this._writableState; if (state.corked) { state.corked--; - if (!state.writing && - !state.corked && - !state.finished && - !state.bufferProcessing && - state.buffer.length) - clearBuffer(this, state); + if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); } }; +Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { + // node::ParseEncoding() requires lower case. + if (typeof encoding === 'string') encoding = encoding.toLowerCase(); + if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding); + this._writableState.defaultEncoding = encoding; + return this; +}; + function decodeChunk(state, chunk, encoding) { - if (!state.objectMode && - state.decodeStrings !== false && - util.isString(chunk)) { - chunk = new Buffer(chunk, encoding); + if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { + chunk = bufferShim.from(chunk, encoding); } return chunk; } @@ -179803,21 +166321,28 @@ function decodeChunk(state, chunk, encoding) { // If we return false, then we need a drain event, so set that flag. function writeOrBuffer(stream, state, chunk, encoding, cb) { chunk = decodeChunk(state, chunk, encoding); - if (util.isBuffer(chunk)) - encoding = 'buffer'; + + if (Buffer.isBuffer(chunk)) encoding = 'buffer'; var len = state.objectMode ? 1 : chunk.length; state.length += len; var ret = state.length < state.highWaterMark; // we must ensure that previous needDrain will not be reset to false. - if (!ret) - state.needDrain = true; + if (!ret) state.needDrain = true; - if (state.writing || state.corked) - state.buffer.push(new WriteReq(chunk, encoding, cb)); - else + if (state.writing || state.corked) { + var last = state.lastBufferedRequest; + state.lastBufferedRequest = new WriteReq(chunk, encoding, cb); + if (last) { + last.next = state.lastBufferedRequest; + } else { + state.bufferedRequest = state.lastBufferedRequest; + } + state.bufferedRequestCount += 1; + } else { doWrite(stream, state, false, len, chunk, encoding, cb); + } return ret; } @@ -179827,23 +166352,13 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) { state.writecb = cb; state.writing = true; state.sync = true; - if (writev) - stream._writev(chunk, state.onwrite); - else - stream._write(chunk, encoding, state.onwrite); + if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite); state.sync = false; } function onwriteError(stream, state, sync, er, cb) { - if (sync) - process.nextTick(function() { - state.pendingcb--; - cb(er); - }); - else { - state.pendingcb--; - cb(er); - } + --state.pendingcb; + if (sync) processNextTick(cb, er);else cb(er); stream._writableState.errorEmitted = true; stream.emit('error', er); @@ -179863,32 +166378,26 @@ function onwrite(stream, er) { onwriteStateUpdate(state); - if (er) - onwriteError(stream, state, sync, er, cb); - else { + if (er) onwriteError(stream, state, sync, er, cb);else { // Check if we're actually ready to finish, but don't emit yet - var finished = needFinish(stream, state); + var finished = needFinish(state); - if (!finished && - !state.corked && - !state.bufferProcessing && - state.buffer.length) { + if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { clearBuffer(stream, state); } if (sync) { - process.nextTick(function() { - afterWrite(stream, state, finished, cb); - }); + /**/ + asyncWrite(afterWrite, stream, state, finished, cb); + /**/ } else { - afterWrite(stream, state, finished, cb); - } + afterWrite(stream, state, finished, cb); + } } } function afterWrite(stream, state, finished, cb) { - if (!finished) - onwriteDrain(stream, state); + if (!finished) onwriteDrain(stream, state); state.pendingcb--; cb(); finishMaybe(stream, state); @@ -179904,80 +166413,83 @@ function onwriteDrain(stream, state) { } } - // if there's something in the buffer waiting, then process it function clearBuffer(stream, state) { state.bufferProcessing = true; + var entry = state.bufferedRequest; - if (stream._writev && state.buffer.length > 1) { + if (stream._writev && entry && entry.next) { // Fast case, write everything using _writev() - var cbs = []; - for (var c = 0; c < state.buffer.length; c++) - cbs.push(state.buffer[c].callback); + var l = state.bufferedRequestCount; + var buffer = new Array(l); + var holder = state.corkedRequestsFree; + holder.entry = entry; - // count the one we are adding, as well. - // TODO(isaacs) clean this up + var count = 0; + while (entry) { + buffer[count] = entry; + entry = entry.next; + count += 1; + } + + doWrite(stream, state, true, state.length, buffer, '', holder.finish); + + // doWrite is almost always async, defer these to save a bit of time + // as the hot path ends with doWrite state.pendingcb++; - doWrite(stream, state, true, state.length, state.buffer, '', function(err) { - for (var i = 0; i < cbs.length; i++) { - state.pendingcb--; - cbs[i](err); - } - }); - - // Clear buffer - state.buffer = []; + state.lastBufferedRequest = null; + if (holder.next) { + state.corkedRequestsFree = holder.next; + holder.next = null; + } else { + state.corkedRequestsFree = new CorkedRequest(state); + } } else { // Slow case, write chunks one-by-one - for (var c = 0; c < state.buffer.length; c++) { - var entry = state.buffer[c]; + while (entry) { var chunk = entry.chunk; var encoding = entry.encoding; var cb = entry.callback; var len = state.objectMode ? 1 : chunk.length; doWrite(stream, state, false, len, chunk, encoding, cb); - + entry = entry.next; // if we didn't call the onwrite immediately, then // it means that we need to wait until it does. // also, that means that the chunk and cb are currently // being processed, so move the buffer counter past them. if (state.writing) { - c++; break; } } - if (c < state.buffer.length) - state.buffer = state.buffer.slice(c); - else - state.buffer.length = 0; + if (entry === null) state.lastBufferedRequest = null; } + state.bufferedRequestCount = 0; + state.bufferedRequest = entry; state.bufferProcessing = false; } -Writable.prototype._write = function(chunk, encoding, cb) { - cb(new Error('not implemented')); - +Writable.prototype._write = function (chunk, encoding, cb) { + cb(new Error('_write() is not implemented')); }; Writable.prototype._writev = null; -Writable.prototype.end = function(chunk, encoding, cb) { +Writable.prototype.end = function (chunk, encoding, cb) { var state = this._writableState; - if (util.isFunction(chunk)) { + if (typeof chunk === 'function') { cb = chunk; chunk = null; encoding = null; - } else if (util.isFunction(encoding)) { + } else if (typeof encoding === 'function') { cb = encoding; encoding = null; } - if (!util.isNullOrUndefined(chunk)) - this.write(chunk, encoding); + if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); // .end() fully uncorks if (state.corked) { @@ -179986,16 +166498,11 @@ Writable.prototype.end = function(chunk, encoding, cb) { } // ignore unnecessary end() calls. - if (!state.ending && !state.finished) - endWritable(this, state, cb); + if (!state.ending && !state.finished) endWritable(this, state, cb); }; - -function needFinish(stream, state) { - return (state.ending && - state.length === 0 && - !state.finished && - !state.writing); +function needFinish(state) { + return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; } function prefinish(stream, state) { @@ -180006,14 +166513,15 @@ function prefinish(stream, state) { } function finishMaybe(stream, state) { - var need = needFinish(stream, state); + var need = needFinish(state); if (need) { if (state.pendingcb === 0) { prefinish(stream, state); state.finished = true; stream.emit('finish'); - } else + } else { prefinish(stream, state); + } } return need; } @@ -180022,38 +166530,1325 @@ function endWritable(stream, state, cb) { state.ending = true; finishMaybe(stream, state); if (cb) { - if (state.finished) - process.nextTick(cb); - else - stream.once('finish', cb); + if (state.finished) processNextTick(cb);else stream.once('finish', cb); } state.ended = true; + stream.writable = false; } +// It seems a linked list but it is not +// there will be only 2 of these for each stream +function CorkedRequest(state) { + var _this = this; + + this.next = null; + this.entry = null; + + this.finish = function (err) { + var entry = _this.entry; + _this.entry = null; + while (entry) { + var cb = entry.callback; + state.pendingcb--; + cb(err); + entry = entry.next; + } + if (state.corkedRequestsFree) { + state.corkedRequestsFree.next = _this; + } else { + state.corkedRequestsFree = _this; + } + }; +} }).call(this,require('_process')) -},{"./_stream_duplex":1563,"_process":41,"buffer":33,"core-util-is":1568,"inherits":1569,"stream":62}],1568:[function(require,module,exports){ -arguments[4][1555][0].apply(exports,arguments) -},{"../../../../../../../../browserify/node_modules/insert-module-globals/node_modules/is-buffer/index.js":39,"dup":1555}],1569:[function(require,module,exports){ -arguments[4][12][0].apply(exports,arguments) -},{"dup":12}],1570:[function(require,module,exports){ -arguments[4][1557][0].apply(exports,arguments) -},{"dup":1557}],1571:[function(require,module,exports){ -arguments[4][15][0].apply(exports,arguments) -},{"buffer":33,"dup":15}],1572:[function(require,module,exports){ +},{"./_stream_duplex":935,"_process":923,"buffer":46,"buffer-shims":48,"core-util-is":78,"events":95,"inherits":266,"process-nextick-args":922,"util-deprecate":998}],940:[function(require,module,exports){ +'use strict'; + +var Buffer = require('buffer').Buffer; +/**/ +var bufferShim = require('buffer-shims'); +/**/ + +module.exports = BufferList; + +function BufferList() { + this.head = null; + this.tail = null; + this.length = 0; +} + +BufferList.prototype.push = function (v) { + var entry = { data: v, next: null }; + if (this.length > 0) this.tail.next = entry;else this.head = entry; + this.tail = entry; + ++this.length; +}; + +BufferList.prototype.unshift = function (v) { + var entry = { data: v, next: this.head }; + if (this.length === 0) this.tail = entry; + this.head = entry; + ++this.length; +}; + +BufferList.prototype.shift = function () { + if (this.length === 0) return; + var ret = this.head.data; + if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next; + --this.length; + return ret; +}; + +BufferList.prototype.clear = function () { + this.head = this.tail = null; + this.length = 0; +}; + +BufferList.prototype.join = function (s) { + if (this.length === 0) return ''; + var p = this.head; + var ret = '' + p.data; + while (p = p.next) { + ret += s + p.data; + }return ret; +}; + +BufferList.prototype.concat = function (n) { + if (this.length === 0) return bufferShim.alloc(0); + if (this.length === 1) return this.head.data; + var ret = bufferShim.allocUnsafe(n >>> 0); + var p = this.head; + var i = 0; + while (p) { + p.data.copy(ret, i); + i += p.data.length; + p = p.next; + } + return ret; +}; +},{"buffer":46,"buffer-shims":48}],941:[function(require,module,exports){ +module.exports = require("./lib/_stream_passthrough.js") + +},{"./lib/_stream_passthrough.js":936}],942:[function(require,module,exports){ (function (process){ +var Stream = (function (){ + try { + return require('st' + 'ream'); // hack to fix a circular dependency issue when used with browserify + } catch(_){} +}()); exports = module.exports = require('./lib/_stream_readable.js'); -exports.Stream = require('stream'); +exports.Stream = Stream || exports; exports.Readable = exports; exports.Writable = require('./lib/_stream_writable.js'); exports.Duplex = require('./lib/_stream_duplex.js'); exports.Transform = require('./lib/_stream_transform.js'); exports.PassThrough = require('./lib/_stream_passthrough.js'); -if (!process.browser && process.env.READABLE_STREAM === 'disable') { - module.exports = require('stream'); + +if (!process.browser && process.env.READABLE_STREAM === 'disable' && Stream) { + module.exports = Stream; } }).call(this,require('_process')) -},{"./lib/_stream_duplex.js":1563,"./lib/_stream_passthrough.js":1564,"./lib/_stream_readable.js":1565,"./lib/_stream_transform.js":1566,"./lib/_stream_writable.js":1567,"_process":41,"stream":62}],1573:[function(require,module,exports){ +},{"./lib/_stream_duplex.js":935,"./lib/_stream_passthrough.js":936,"./lib/_stream_readable.js":937,"./lib/_stream_transform.js":938,"./lib/_stream_writable.js":939,"_process":923}],943:[function(require,module,exports){ +module.exports = require("./lib/_stream_transform.js") + +},{"./lib/_stream_transform.js":938}],944:[function(require,module,exports){ +module.exports = require("./lib/_stream_writable.js") + +},{"./lib/_stream_writable.js":939}],945:[function(require,module,exports){ +'use strict' + +var compareCell = require('compare-cell') +var compareOrientedCell = require('compare-oriented-cell') +var orientation = require('cell-orientation') + +module.exports = reduceCellComplex + +function reduceCellComplex(cells) { + cells.sort(compareOrientedCell) + var n = cells.length + var ptr = 0 + for(var i=0; i 0) { + var f = cells[ptr-1] + if(compareCell(c, f) === 0 && + orientation(f) !== o) { + ptr -= 1 + continue + } + } + cells[ptr++] = c + } + cells.length = ptr + return cells +} + +},{"cell-orientation":55,"compare-cell":64,"compare-oriented-cell":65}],946:[function(require,module,exports){ +/*! + * repeat-string + * + * Copyright (c) 2014-2015, Jon Schlinkert. + * Licensed under the MIT License. + */ + +'use strict'; + +/** + * Results cache + */ + +var res = ''; +var cache; + +/** + * Expose `repeat` + */ + +module.exports = repeat; + +/** + * Repeat the given `string` the specified `number` + * of times. + * + * **Example:** + * + * ```js + * var repeat = require('repeat-string'); + * repeat('A', 5); + * //=> AAAAA + * ``` + * + * @param {String} `string` The string to repeat + * @param {Number} `number` The number of times to repeat the string + * @return {String} Repeated string + * @api public + */ + +function repeat(str, num) { + if (typeof str !== 'string') { + throw new TypeError('expected a string'); + } + + // cover common, quick use cases + if (num === 1) return str; + if (num === 2) return str + str; + + var max = str.length * num; + if (cache !== str || typeof cache === 'undefined') { + cache = str; + res = ''; + } else if (res.length >= max) { + return res.substr(0, max); + } + + while (max > res.length && num > 1) { + if (num & 1) { + res += str; + } + + num >>= 1; + str += str; + } + + res += str; + res = res.substr(0, max); + return res; +} + +},{}],947:[function(require,module,exports){ +// Copyright 2014 Simon Lydell +// X11 (“MIT”) Licensed. (See LICENSE.) + +void (function(root, factory) { + if (typeof define === "function" && define.amd) { + define(factory) + } else if (typeof exports === "object") { + module.exports = factory() + } else { + root.resolveUrl = factory() + } +}(this, function() { + + function resolveUrl(/* ...urls */) { + var numUrls = arguments.length + + if (numUrls === 0) { + throw new Error("resolveUrl requires at least one argument; got none.") + } + + var base = document.createElement("base") + base.href = arguments[0] + + if (numUrls === 1) { + return base.href + } + + var head = document.getElementsByTagName("head")[0] + head.insertBefore(base, head.firstChild) + + var a = document.createElement("a") + var resolved + + for (var index = 1; index < numUrls; index++) { + a.href = arguments[index] + resolved = a.href + base.href = resolved + } + + head.removeChild(base) + + return resolved + } + + return resolveUrl + +})); + +},{}],948:[function(require,module,exports){ +(function (global){ +module.exports = + global.performance && + global.performance.now ? function now() { + return performance.now() + } : Date.now || function now() { + return +new Date + } + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{}],949:[function(require,module,exports){ +"use strict" + +module.exports = compressExpansion + +function compressExpansion(e) { + var m = e.length + var Q = e[e.length-1] + var bottom = m + for(var i=m-2; i>=0; --i) { + var a = Q + var b = e[i] + Q = a + b + var bv = Q - a + var q = b - bv + if(q) { + e[--bottom] = Q + Q = q + } + } + var top = 0 + for(var i=bottom; i>1 + return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") + } +} + +function determinant(m) { + if(m.length === 2) { + return ["sum(prod(", m[0][0], ",", m[1][1], "),prod(-", m[0][1], ",", m[1][0], "))"].join("") + } else { + var expr = [] + for(var i=0; i>1 + return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") + } +} + +function makeProduct(a, b) { + if(a.charAt(0) === "m") { + if(b.charAt(0) === "w") { + var toks = a.split("[") + return ["w", b.substr(1), "m", toks[0].substr(1)].join("") + } else { + return ["prod(", a, ",", b, ")"].join("") + } + } else { + return makeProduct(b, a) + } +} + +function sign(s) { + if(s & 1 !== 0) { + return "-" + } + return "" +} + +function determinant(m) { + if(m.length === 2) { + return [["diff(", makeProduct(m[0][0], m[1][1]), ",", makeProduct(m[1][0], m[0][1]), ")"].join("")] + } else { + var expr = [] + for(var i=0; i 0) { + code.push(",") + } + code.push("[") + for(var k=0; k 0) { + code.push(",") + } + if(k === i) { + code.push("+b[", j, "]") + } else { + code.push("+A[", j, "][", k, "]") + } + } + code.push("]") + } + code.push("]),") + } + code.push("det(A)]}return ", funcName) + var proc = new Function("det", code.join("")) + if(n < 6) { + return proc(determinant[n]) + } + return proc(determinant) +} + +function robustLinearSolve0d() { + return [ 0 ] +} + +function robustLinearSolve1d(A, b) { + return [ [ b[0] ], [ A[0][0] ] ] +} + +var CACHE = [ + robustLinearSolve0d, + robustLinearSolve1d +] + +function generateDispatch() { + while(CACHE.length < NUM_EXPAND) { + CACHE.push(generateSolver(CACHE.length)) + } + var procArgs = [] + var code = ["function dispatchLinearSolve(A,b){switch(A.length){"] + for(var i=0; i>1 + return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") + } +} + +function determinant(m) { + if(m.length === 2) { + return [["sum(prod(", m[0][0], ",", m[1][1], "),prod(-", m[0][1], ",", m[1][0], "))"].join("")] + } else { + var expr = [] + for(var i=0; i 0) { + if(r <= 0) { + return det + } else { + s = l + r + } + } else if(l < 0) { + if(r >= 0) { + return det + } else { + s = -(l + r) + } + } else { + return det + } + var tol = ERRBOUND3 * s + if(det >= tol || det <= -tol) { + return det + } + return orientation3Exact(a, b, c) + }, + function orientation4(a,b,c,d) { + var adx = a[0] - d[0] + var bdx = b[0] - d[0] + var cdx = c[0] - d[0] + var ady = a[1] - d[1] + var bdy = b[1] - d[1] + var cdy = c[1] - d[1] + var adz = a[2] - d[2] + var bdz = b[2] - d[2] + var cdz = c[2] - d[2] + var bdxcdy = bdx * cdy + var cdxbdy = cdx * bdy + var cdxady = cdx * ady + var adxcdy = adx * cdy + var adxbdy = adx * bdy + var bdxady = bdx * ady + var det = adz * (bdxcdy - cdxbdy) + + bdz * (cdxady - adxcdy) + + cdz * (adxbdy - bdxady) + var permanent = (Math.abs(bdxcdy) + Math.abs(cdxbdy)) * Math.abs(adz) + + (Math.abs(cdxady) + Math.abs(adxcdy)) * Math.abs(bdz) + + (Math.abs(adxbdy) + Math.abs(bdxady)) * Math.abs(cdz) + var tol = ERRBOUND4 * permanent + if ((det > tol) || (-det > tol)) { + return det + } + return orientation4Exact(a,b,c,d) + } +] + +function slowOrient(args) { + var proc = CACHED[args.length] + if(!proc) { + proc = CACHED[args.length] = orientation(args.length) + } + return proc.apply(undefined, args) +} + +function generateOrientationProc() { + while(CACHED.length <= NUM_EXPAND) { + CACHED.push(orientation(CACHED.length)) + } + var args = [] + var procArgs = ["slow"] + for(var i=0; i<=NUM_EXPAND; ++i) { + args.push("a" + i) + procArgs.push("o" + i) + } + var code = [ + "function getOrientation(", args.join(), "){switch(arguments.length){case 0:case 1:return 0;" + ] + for(var i=2; i<=NUM_EXPAND; ++i) { + code.push("case ", i, ":return o", i, "(", args.slice(0, i).join(), ");") + } + code.push("}var s=new Array(arguments.length);for(var i=0;i 0 && y0 > 0) || (x0 < 0 && y0 < 0)) { + return false + } + + var x1 = orient(b0, a0, a1) + var y1 = orient(b1, a0, a1) + if((x1 > 0 && y1 > 0) || (x1 < 0 && y1 < 0)) { + return false + } + + //Check for degenerate collinear case + if(x0 === 0 && y0 === 0 && x1 === 0 && y1 === 0) { + return checkCollinear(a0, a1, b0, b1) + } + + return true +} +},{"robust-orientation":954}],958:[function(require,module,exports){ +"use strict" + +module.exports = robustSubtract + +//Easy case: Add two scalars +function scalarScalar(a, b) { + var x = a + b + var bv = x - a + var av = x - bv + var br = b - bv + var ar = a - av + var y = ar + br + if(y) { + return [y, x] + } + return [x] +} + +function robustSubtract(e, f) { + var ne = e.length|0 + var nf = f.length|0 + if(ne === 1 && nf === 1) { + return scalarScalar(e[0], -f[0]) + } + var n = ne + nf + var g = new Array(n) + var count = 0 + var eptr = 0 + var fptr = 0 + var abs = Math.abs + var ei = e[eptr] + var ea = abs(ei) + var fi = -f[fptr] + var fa = abs(fi) + var a, b + if(ea < fa) { + b = ei + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + ea = abs(ei) + } + } else { + b = fi + fptr += 1 + if(fptr < nf) { + fi = -f[fptr] + fa = abs(fi) + } + } + if((eptr < ne && ea < fa) || (fptr >= nf)) { + a = ei + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + ea = abs(ei) + } + } else { + a = fi + fptr += 1 + if(fptr < nf) { + fi = -f[fptr] + fa = abs(fi) + } + } + var x = a + b + var bv = x - a + var y = b - bv + var q0 = y + var q1 = x + var _x, _bv, _av, _br, _ar + while(eptr < ne && fptr < nf) { + if(ea < fa) { + a = ei + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + ea = abs(ei) + } + } else { + a = fi + fptr += 1 + if(fptr < nf) { + fi = -f[fptr] + fa = abs(fi) + } + } + b = q0 + x = a + b + bv = x - a + y = b - bv + if(y) { + g[count++] = y + } + _x = q1 + x + _bv = _x - q1 + _av = _x - _bv + _br = x - _bv + _ar = q1 - _av + q0 = _ar + _br + q1 = _x + } + while(eptr < ne) { + a = ei + b = q0 + x = a + b + bv = x - a + y = b - bv + if(y) { + g[count++] = y + } + _x = q1 + x + _bv = _x - q1 + _av = _x - _bv + _br = x - _bv + _ar = q1 - _av + q0 = _ar + _br + q1 = _x + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + } + } + while(fptr < nf) { + a = fi + b = q0 + x = a + b + bv = x - a + y = b - bv + if(y) { + g[count++] = y + } + _x = q1 + x + _bv = _x - q1 + _av = _x - _bv + _br = x - _bv + _ar = q1 - _av + q0 = _ar + _br + q1 = _x + fptr += 1 + if(fptr < nf) { + fi = -f[fptr] + } + } + if(q0) { + g[count++] = q0 + } + if(q1) { + g[count++] = q1 + } + if(!count) { + g[count++] = 0.0 + } + g.length = count + return g +} +},{}],959:[function(require,module,exports){ +"use strict" + +module.exports = linearExpansionSum + +//Easy case: Add two scalars +function scalarScalar(a, b) { + var x = a + b + var bv = x - a + var av = x - bv + var br = b - bv + var ar = a - av + var y = ar + br + if(y) { + return [y, x] + } + return [x] +} + +function linearExpansionSum(e, f) { + var ne = e.length|0 + var nf = f.length|0 + if(ne === 1 && nf === 1) { + return scalarScalar(e[0], f[0]) + } + var n = ne + nf + var g = new Array(n) + var count = 0 + var eptr = 0 + var fptr = 0 + var abs = Math.abs + var ei = e[eptr] + var ea = abs(ei) + var fi = f[fptr] + var fa = abs(fi) + var a, b + if(ea < fa) { + b = ei + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + ea = abs(ei) + } + } else { + b = fi + fptr += 1 + if(fptr < nf) { + fi = f[fptr] + fa = abs(fi) + } + } + if((eptr < ne && ea < fa) || (fptr >= nf)) { + a = ei + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + ea = abs(ei) + } + } else { + a = fi + fptr += 1 + if(fptr < nf) { + fi = f[fptr] + fa = abs(fi) + } + } + var x = a + b + var bv = x - a + var y = b - bv + var q0 = y + var q1 = x + var _x, _bv, _av, _br, _ar + while(eptr < ne && fptr < nf) { + if(ea < fa) { + a = ei + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + ea = abs(ei) + } + } else { + a = fi + fptr += 1 + if(fptr < nf) { + fi = f[fptr] + fa = abs(fi) + } + } + b = q0 + x = a + b + bv = x - a + y = b - bv + if(y) { + g[count++] = y + } + _x = q1 + x + _bv = _x - q1 + _av = _x - _bv + _br = x - _bv + _ar = q1 - _av + q0 = _ar + _br + q1 = _x + } + while(eptr < ne) { + a = ei + b = q0 + x = a + b + bv = x - a + y = b - bv + if(y) { + g[count++] = y + } + _x = q1 + x + _bv = _x - q1 + _av = _x - _bv + _br = x - _bv + _ar = q1 - _av + q0 = _ar + _br + q1 = _x + eptr += 1 + if(eptr < ne) { + ei = e[eptr] + } + } + while(fptr < nf) { + a = fi + b = q0 + x = a + b + bv = x - a + y = b - bv + if(y) { + g[count++] = y + } + _x = q1 + x + _bv = _x - q1 + _av = _x - _bv + _br = x - _bv + _ar = q1 - _av + q0 = _ar + _br + q1 = _x + fptr += 1 + if(fptr < nf) { + fi = f[fptr] + } + } + if(q0) { + g[count++] = q0 + } + if(q1) { + g[count++] = q1 + } + if(!count) { + g[count++] = 0.0 + } + g.length = count + return g +} +},{}],960:[function(require,module,exports){ var encode = require('./lib/encoder'), decode = require('./lib/decoder'); @@ -180062,7 +167857,7 @@ module.exports = { decode: decode }; -},{"./lib/decoder":1574,"./lib/encoder":1575}],1574:[function(require,module,exports){ +},{"./lib/decoder":961,"./lib/encoder":962}],961:[function(require,module,exports){ (function (Buffer){ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ @@ -181045,7 +168840,7 @@ function decode(jpegData) { } }).call(this,require("buffer").Buffer) -},{"buffer":33}],1575:[function(require,module,exports){ +},{"buffer":46}],962:[function(require,module,exports){ (function (Buffer){ /* Copyright (c) 2008, Adobe Systems Incorporated @@ -181815,2136 +169610,7 @@ function getImageDataFromImage(idOrElement){ } }).call(this,require("buffer").Buffer) -},{"buffer":33}],1576:[function(require,module,exports){ -arguments[4][1113][0].apply(exports,arguments) -},{"cwise-compiler":1577,"dup":1113}],1577:[function(require,module,exports){ -arguments[4][73][0].apply(exports,arguments) -},{"./lib/thunk.js":1579,"dup":73}],1578:[function(require,module,exports){ -arguments[4][74][0].apply(exports,arguments) -},{"dup":74,"uniq":1580}],1579:[function(require,module,exports){ -arguments[4][75][0].apply(exports,arguments) -},{"./compile.js":1578,"dup":75}],1580:[function(require,module,exports){ -arguments[4][76][0].apply(exports,arguments) -},{"dup":76}],1581:[function(require,module,exports){ -arguments[4][77][0].apply(exports,arguments) -},{"dup":77,"iota-array":1582,"is-buffer":1583}],1582:[function(require,module,exports){ -arguments[4][78][0].apply(exports,arguments) -},{"dup":78}],1583:[function(require,module,exports){ -arguments[4][39][0].apply(exports,arguments) -},{"dup":39}],1584:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var interlaceUtils = require('./interlace'); - -var pixelBppMap = { - 1: { // L - 0: 0, - 1: 0, - 2: 0, - 3: 0xff - }, - 2: { // LA - 0: 0, - 1: 0, - 2: 0, - 3: 1 - }, - 3: { // RGB - 0: 0, - 1: 1, - 2: 2, - 3: 0xff - }, - 4: { // RGBA - 0: 0, - 1: 1, - 2: 2, - 3: 3 - } -}; - -function bitRetriever(data, depth) { - - var leftOver = []; - var i = 0; - - function split() { - if (i === data.length) { - throw new Error('Ran out of data'); - } - var byte = data[i]; - i++; - var byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1; - switch (depth) { - default: - throw new Error('unrecognised depth'); - case 16: - byte2 = data[i]; - i++; - leftOver.push(((byte << 8) + byte2)); - break; - case 4: - byte2 = byte & 0x0f; - byte1 = byte >> 4; - leftOver.push(byte1, byte2); - break; - case 2: - byte4 = byte & 3; - byte3 = byte >> 2 & 3; - byte2 = byte >> 4 & 3; - byte1 = byte >> 6 & 3; - leftOver.push(byte1, byte2, byte3, byte4); - break; - case 1: - byte8 = byte & 1; - byte7 = byte >> 1 & 1; - byte6 = byte >> 2 & 1; - byte5 = byte >> 3 & 1; - byte4 = byte >> 4 & 1; - byte3 = byte >> 5 & 1; - byte2 = byte >> 6 & 1; - byte1 = byte >> 7 & 1; - leftOver.push(byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8); - break; - } - } - - return { - get: function(count) { - while (leftOver.length < count) { - split(); - } - var returner = leftOver.slice(0, count); - leftOver = leftOver.slice(count); - return returner; - }, - resetAfterLine: function() { - leftOver.length = 0; - }, - end: function() { - if (i !== data.length) { - throw new Error('extra data found'); - } - } - }; -} - -function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) { // eslint-disable-line max-params - var imageWidth = image.width; - var imageHeight = image.height; - var imagePass = image.index; - for (var y = 0; y < imageHeight; y++) { - for (var x = 0; x < imageWidth; x++) { - var pxPos = getPxPos(x, y, imagePass); - - for (var i = 0; i < 4; i++) { - var idx = pixelBppMap[bpp][i]; - if (i === data.length) { - throw new Error('Ran out of data'); - } - pxData[pxPos + i] = idx !== 0xff ? data[idx + rawPos] : 0xff; - } - rawPos += bpp; //eslint-disable-line no-param-reassign - } - } - return rawPos; -} - -function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) { // eslint-disable-line max-params - var imageWidth = image.width; - var imageHeight = image.height; - var imagePass = image.index; - for (var y = 0; y < imageHeight; y++) { - for (var x = 0; x < imageWidth; x++) { - var pixelData = bits.get(bpp); - var pxPos = getPxPos(x, y, imagePass); - - for (var i = 0; i < 4; i++) { - var idx = pixelBppMap[bpp][i]; - pxData[pxPos + i] = idx !== 0xff ? pixelData[idx] : maxBit; - } - } - bits.resetAfterLine(); - } -} - -exports.dataToBitMap = function(data, bitmapInfo) { - - var width = bitmapInfo.width; - var height = bitmapInfo.height; - var depth = bitmapInfo.depth; - var bpp = bitmapInfo.bpp; - var interlace = bitmapInfo.interlace; - - if (depth !== 8) { - var bits = bitRetriever(data, depth); - } - var pxData; - if (depth <= 8) { - pxData = new Buffer(width * height * 4); - } - else { - pxData = new Uint16Array(width * height * 4); - } - var maxBit = Math.pow(2, depth) - 1; - var rawPos = 0; - var images; - var getPxPos; - - if (interlace) { - images = interlaceUtils.getImagePasses(width, height); - getPxPos = interlaceUtils.getInterlaceIterator(width, height); - } - else { - var nonInterlacedPxPos = 0; - getPxPos = function() { - var returner = nonInterlacedPxPos; - nonInterlacedPxPos += 4; - return returner; - }; - images = [{ width: width, height: height }]; - } - - for (var imageIndex = 0; imageIndex < images.length; imageIndex++) { - if (depth === 8) { - rawPos = mapImage8Bit(images[imageIndex], pxData, getPxPos, bpp, data, rawPos); - } - else { - mapImageCustomBit(images[imageIndex], pxData, getPxPos, bpp, bits, maxBit); - } - } - if (depth === 8) { - if (rawPos !== data.length) { - throw new Error('extra data found'); - } - } - else { - bits.end(); - } - - return pxData; -}; - -}).call(this,require("buffer").Buffer) -},{"./interlace":1594,"buffer":33}],1585:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var constants = require('./constants'); - -module.exports = function(data, width, height, options) { - var outHasAlpha = options.colorType === constants.COLORTYPE_COLOR_ALPHA; - if (options.inputHasAlpha && outHasAlpha) { - return data; - } - if (!options.inputHasAlpha && !outHasAlpha) { - return data; - } - - var outBpp = outHasAlpha ? 4 : 3; - var outData = new Buffer(width * height * outBpp); - var inBpp = options.inputHasAlpha ? 4 : 3; - var inIndex = 0; - var outIndex = 0; - - var bgColor = options.bgColor || {}; - if (bgColor.red === undefined) { - bgColor.red = 255; - } - if (bgColor.green === undefined) { - bgColor.green = 255; - } - if (bgColor.blue === undefined) { - bgColor.blue = 255; - } - - for (var y = 0; y < height; y++) { - for (var x = 0; x < width; x++) { - var red = data[inIndex]; - var green = data[inIndex + 1]; - var blue = data[inIndex + 2]; - - var alpha; - if (options.inputHasAlpha) { - alpha = data[inIndex + 3]; - if (!outHasAlpha) { - alpha /= 255; - red = Math.min(Math.max(Math.round((1 - alpha) * bgColor.red + alpha * red), 0), 255); - green = Math.min(Math.max(Math.round((1 - alpha) * bgColor.green + alpha * green), 0), 255); - blue = Math.min(Math.max(Math.round((1 - alpha) * bgColor.blue + alpha * blue), 0), 255); - } - } - else { - alpha = 255; - } - - outData[outIndex] = red; - outData[outIndex + 1] = green; - outData[outIndex + 2] = blue; - if (outHasAlpha) { - outData[outIndex + 3] = alpha; - } - - inIndex += inBpp; - outIndex += outBpp; - } - } - - return outData; -}; - -}).call(this,require("buffer").Buffer) -},{"./constants":1587,"buffer":33}],1586:[function(require,module,exports){ -(function (process,Buffer){ -'use strict'; - - -var util = require('util'); -var Stream = require('stream'); - - -var ChunkStream = module.exports = function() { - Stream.call(this); - - this._buffers = []; - this._buffered = 0; - - this._reads = []; - this._paused = false; - - this._encoding = 'utf8'; - this.writable = true; -}; -util.inherits(ChunkStream, Stream); - - -ChunkStream.prototype.read = function(length, callback) { - - this._reads.push({ - length: Math.abs(length), // if length < 0 then at most this length - allowLess: length < 0, - func: callback - }); - - process.nextTick(function() { - this._process(); - - // its paused and there is not enought data then ask for more - if (this._paused && this._reads.length > 0) { - this._paused = false; - - this.emit('drain'); - } - }.bind(this)); -}; - -ChunkStream.prototype.write = function(data, encoding) { - - if (!this.writable) { - this.emit('error', new Error('Stream not writable')); - return false; - } - - var dataBuffer; - if (Buffer.isBuffer(data)) { - dataBuffer = data; - } - else { - dataBuffer = new Buffer(data, encoding || this._encoding); - } - - this._buffers.push(dataBuffer); - this._buffered += dataBuffer.length; - - this._process(); - - // ok if there are no more read requests - if (this._reads && this._reads.length === 0) { - this._paused = true; - } - - return this.writable && !this._paused; -}; - -ChunkStream.prototype.end = function(data, encoding) { - - if (data) { - this.write(data, encoding); - } - - this.writable = false; - - // already destroyed - if (!this._buffers) { - return; - } - - // enqueue or handle end - if (this._buffers.length === 0) { - this._end(); - } - else { - this._buffers.push(null); - this._process(); - } -}; - -ChunkStream.prototype.destroySoon = ChunkStream.prototype.end; - -ChunkStream.prototype._end = function() { - - if (this._reads.length > 0) { - this.emit('error', - new Error('There are some read requests waitng on finished stream') - ); - } - - this.destroy(); -}; - -ChunkStream.prototype.destroy = function() { - - if (!this._buffers) { - return; - } - - this.writable = false; - this._reads = null; - this._buffers = null; - - this.emit('close'); -}; - -ChunkStream.prototype._processReadAllowingLess = function(read) { - // ok there is any data so that we can satisfy this request - this._reads.shift(); // == read - - // first we need to peek into first buffer - var smallerBuf = this._buffers[0]; - - // ok there is more data than we need - if (smallerBuf.length > read.length) { - - this._buffered -= read.length; - this._buffers[0] = smallerBuf.slice(read.length); - - read.func.call(this, smallerBuf.slice(0, read.length)); - - } - else { - // ok this is less than maximum length so use it all - this._buffered -= smallerBuf.length; - this._buffers.shift(); // == smallerBuf - - read.func.call(this, smallerBuf); - } -}; - -ChunkStream.prototype._processRead = function(read) { - this._reads.shift(); // == read - - var pos = 0; - var count = 0; - var data = new Buffer(read.length); - - // create buffer for all data - while (pos < read.length) { - - var buf = this._buffers[count++]; - var len = Math.min(buf.length, read.length - pos); - - buf.copy(data, pos, 0, len); - pos += len; - - // last buffer wasn't used all so just slice it and leave - if (len !== buf.length) { - this._buffers[--count] = buf.slice(len); - } - } - - // remove all used buffers - if (count > 0) { - this._buffers.splice(0, count); - } - - this._buffered -= read.length; - - read.func.call(this, data); -}; - -ChunkStream.prototype._process = function() { - - try { - // as long as there is any data and read requests - while (this._buffered > 0 && this._reads && this._reads.length > 0) { - - var read = this._reads[0]; - - // read any data (but no more than length) - if (read.allowLess) { - this._processReadAllowingLess(read); - - } - else if (this._buffered >= read.length) { - // ok we can meet some expectations - - this._processRead(read); - } - else { - // not enought data to satisfy first request in queue - // so we need to wait for more - break; - } - } - - if (this._buffers && this._buffers.length > 0 && this._buffers[0] === null) { - this._end(); - } - } - catch (ex) { - this.emit('error', ex); - } -}; - -}).call(this,require('_process'),require("buffer").Buffer) -},{"_process":41,"buffer":33,"stream":62,"util":68}],1587:[function(require,module,exports){ -'use strict'; - - -module.exports = { - - PNG_SIGNATURE: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], - - TYPE_IHDR: 0x49484452, - TYPE_IEND: 0x49454e44, - TYPE_IDAT: 0x49444154, - TYPE_PLTE: 0x504c5445, - TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase - TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase - - // color-type bits - COLORTYPE_GRAYSCALE: 0, - COLORTYPE_PALETTE: 1, - COLORTYPE_COLOR: 2, - COLORTYPE_ALPHA: 4, // e.g. grayscale and alpha - - // color-type combinations - COLORTYPE_PALETTE_COLOR: 3, - COLORTYPE_COLOR_ALPHA: 6, - - COLORTYPE_TO_BPP_MAP: { - 0: 1, - 2: 3, - 3: 1, - 4: 2, - 6: 4 - }, - - GAMMA_DIVISION: 100000 -}; - -},{}],1588:[function(require,module,exports){ -'use strict'; - -var crcTable = []; - -(function() { - for (var i = 0; i < 256; i++) { - var currentCrc = i; - for (var j = 0; j < 8; j++) { - if (currentCrc & 1) { - currentCrc = 0xedb88320 ^ (currentCrc >>> 1); - } - else { - currentCrc = currentCrc >>> 1; - } - } - crcTable[i] = currentCrc; - } -}()); - -var CrcCalculator = module.exports = function() { - this._crc = -1; -}; - -CrcCalculator.prototype.write = function(data) { - - for (var i = 0; i < data.length; i++) { - this._crc = crcTable[(this._crc ^ data[i]) & 0xff] ^ (this._crc >>> 8); - } - return true; -}; - -CrcCalculator.prototype.crc32 = function() { - return this._crc ^ -1; -}; - - -CrcCalculator.crc32 = function(buf) { - - var crc = -1; - for (var i = 0; i < buf.length; i++) { - crc = crcTable[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8); - } - return crc ^ -1; -}; - -},{}],1589:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var paethPredictor = require('./paeth-predictor'); - -function filterNone(pxData, pxPos, byteWidth, rawData, rawPos) { - pxData.copy(rawData, rawPos, pxPos, pxPos + byteWidth); -} - -function filterSumNone(pxData, pxPos, byteWidth) { - - var sum = 0; - var length = pxPos + byteWidth; - - for (var i = pxPos; i < length; i++) { - sum += Math.abs(pxData[i]); - } - return sum; -} - -function filterSub(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { - - for (var x = 0; x < byteWidth; x++) { - - var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; - var val = pxData[pxPos + x] - left; - - rawData[rawPos + x] = val; - } -} - -function filterSumSub(pxData, pxPos, byteWidth, bpp) { - - var sum = 0; - for (var x = 0; x < byteWidth; x++) { - - var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; - var val = pxData[pxPos + x] - left; - - sum += Math.abs(val); - } - - return sum; -} - -function filterUp(pxData, pxPos, byteWidth, rawData, rawPos) { - - for (var x = 0; x < byteWidth; x++) { - - var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; - var val = pxData[pxPos + x] - up; - - rawData[rawPos + x] = val; - } -} - -function filterSumUp(pxData, pxPos, byteWidth) { - - var sum = 0; - var length = pxPos + byteWidth; - for (var x = pxPos; x < length; x++) { - - var up = pxPos > 0 ? pxData[x - byteWidth] : 0; - var val = pxData[x] - up; - - sum += Math.abs(val); - } - - return sum; -} - -function filterAvg(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { - - for (var x = 0; x < byteWidth; x++) { - - var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; - var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; - var val = pxData[pxPos + x] - ((left + up) >> 1); - - rawData[rawPos + x] = val; - } -} - -function filterSumAvg(pxData, pxPos, byteWidth, bpp) { - - var sum = 0; - for (var x = 0; x < byteWidth; x++) { - - var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; - var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; - var val = pxData[pxPos + x] - ((left + up) >> 1); - - sum += Math.abs(val); - } - - return sum; -} - -function filterPaeth(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { - - for (var x = 0; x < byteWidth; x++) { - - var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; - var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; - var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; - var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); - - rawData[rawPos + x] = val; - } -} - -function filterSumPaeth(pxData, pxPos, byteWidth, bpp) { - var sum = 0; - for (var x = 0; x < byteWidth; x++) { - - var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; - var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; - var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; - var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); - - sum += Math.abs(val); - } - - return sum; -} - -var filters = { - 0: filterNone, - 1: filterSub, - 2: filterUp, - 3: filterAvg, - 4: filterPaeth -}; - -var filterSums = { - 0: filterSumNone, - 1: filterSumSub, - 2: filterSumUp, - 3: filterSumAvg, - 4: filterSumPaeth -}; - -module.exports = function(pxData, width, height, options, bpp) { - - var filterTypes; - if (!('filterType' in options) || options.filterType === -1) { - filterTypes = [0, 1, 2, 3, 4]; - } - else if (typeof options.filterType === 'number') { - filterTypes = [options.filterType]; - } - else { - throw new Error('unrecognised filter types'); - } - - var byteWidth = width * bpp; - var rawPos = 0; - var pxPos = 0; - var rawData = new Buffer((byteWidth + 1) * height); - var sel = filterTypes[0]; - - for (var y = 0; y < height; y++) { - - if (filterTypes.length > 1) { - // find best filter for this line (with lowest sum of values) - var min = Infinity; - - for (var i = 0; i < filterTypes.length; i++) { - var sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth, bpp); - if (sum < min) { - sel = filterTypes[i]; - min = sum; - } - } - } - - rawData[rawPos] = sel; - rawPos++; - filters[sel](pxData, pxPos, byteWidth, rawData, rawPos, bpp); - rawPos += byteWidth; - pxPos += byteWidth; - } - return rawData; -}; - -}).call(this,require("buffer").Buffer) -},{"./paeth-predictor":1598,"buffer":33}],1590:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var util = require('util'); -var ChunkStream = require('./chunkstream'); -var Filter = require('./filter-parse'); - - -var FilterAsync = module.exports = function(bitmapInfo) { - ChunkStream.call(this); - - var buffers = []; - var that = this; - this._filter = new Filter(bitmapInfo, { - read: this.read.bind(this), - write: function(buffer) { - buffers.push(buffer); - }, - complete: function() { - that.emit('complete', Buffer.concat(buffers)); - } - }); - - this._filter.start(); -}; -util.inherits(FilterAsync, ChunkStream); - -}).call(this,require("buffer").Buffer) -},{"./chunkstream":1586,"./filter-parse":1592,"buffer":33,"util":68}],1591:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var SyncReader = require('./sync-reader'); -var Filter = require('./filter-parse'); - - -exports.process = function(inBuffer, bitmapInfo) { - - var outBuffers = []; - var reader = new SyncReader(inBuffer); - var filter = new Filter(bitmapInfo, { - read: reader.read.bind(reader), - write: function(bufferPart) { - outBuffers.push(bufferPart); - }, - complete: function() { - } - }); - - filter.start(); - reader.process(); - - return Buffer.concat(outBuffers); -}; -}).call(this,require("buffer").Buffer) -},{"./filter-parse":1592,"./sync-reader":1604,"buffer":33}],1592:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var interlaceUtils = require('./interlace'); -var paethPredictor = require('./paeth-predictor'); - -function getByteWidth(width, bpp, depth) { - var byteWidth = width * bpp; - if (depth !== 8) { - byteWidth = Math.ceil(byteWidth / (8 / depth)); - } - return byteWidth; -} - -var Filter = module.exports = function(bitmapInfo, dependencies) { - - var width = bitmapInfo.width; - var height = bitmapInfo.height; - var interlace = bitmapInfo.interlace; - var bpp = bitmapInfo.bpp; - var depth = bitmapInfo.depth; - - this.read = dependencies.read; - this.write = dependencies.write; - this.complete = dependencies.complete; - - this._imageIndex = 0; - this._images = []; - if (interlace) { - var passes = interlaceUtils.getImagePasses(width, height); - for (var i = 0; i < passes.length; i++) { - this._images.push({ - byteWidth: getByteWidth(passes[i].width, bpp, depth), - height: passes[i].height, - lineIndex: 0 - }); - } - } - else { - this._images.push({ - byteWidth: getByteWidth(width, bpp, depth), - height: height, - lineIndex: 0 - }); - } - - // when filtering the line we look at the pixel to the left - // the spec also says it is done on a byte level regardless of the number of pixels - // so if the depth is byte compatible (8 or 16) we subtract the bpp in order to compare back - // a pixel rather than just a different byte part. However if we are sub byte, we ignore. - if (depth === 8) { - this._xComparison = bpp; - } - else if (depth === 16) { - this._xComparison = bpp * 2; - } - else { - this._xComparison = 1; - } -}; - -Filter.prototype.start = function() { - this.read(this._images[this._imageIndex].byteWidth + 1, this._reverseFilterLine.bind(this)); -}; - -Filter.prototype._unFilterType1 = function(rawData, unfilteredLine, byteWidth) { - - var xComparison = this._xComparison; - var xBiggerThan = xComparison - 1; - - for (var x = 0; x < byteWidth; x++) { - var rawByte = rawData[1 + x]; - var f1Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; - unfilteredLine[x] = rawByte + f1Left; - } -}; - -Filter.prototype._unFilterType2 = function(rawData, unfilteredLine, byteWidth) { - - var lastLine = this._lastLine; - - for (var x = 0; x < byteWidth; x++) { - var rawByte = rawData[1 + x]; - var f2Up = lastLine ? lastLine[x] : 0; - unfilteredLine[x] = rawByte + f2Up; - } -}; - -Filter.prototype._unFilterType3 = function(rawData, unfilteredLine, byteWidth) { - - var xComparison = this._xComparison; - var xBiggerThan = xComparison - 1; - var lastLine = this._lastLine; - - for (var x = 0; x < byteWidth; x++) { - var rawByte = rawData[1 + x]; - var f3Up = lastLine ? lastLine[x] : 0; - var f3Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; - var f3Add = Math.floor((f3Left + f3Up) / 2); - unfilteredLine[x] = rawByte + f3Add; - } -}; - -Filter.prototype._unFilterType4 = function(rawData, unfilteredLine, byteWidth) { - - var xComparison = this._xComparison; - var xBiggerThan = xComparison - 1; - var lastLine = this._lastLine; - - for (var x = 0; x < byteWidth; x++) { - var rawByte = rawData[1 + x]; - var f4Up = lastLine ? lastLine[x] : 0; - var f4Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0; - var f4UpLeft = x > xBiggerThan && lastLine ? lastLine[x - xComparison] : 0; - var f4Add = paethPredictor(f4Left, f4Up, f4UpLeft); - unfilteredLine[x] = rawByte + f4Add; - } -}; - -Filter.prototype._reverseFilterLine = function(rawData) { - - var filter = rawData[0]; - var unfilteredLine; - var currentImage = this._images[this._imageIndex]; - var byteWidth = currentImage.byteWidth; - - if (filter === 0) { - unfilteredLine = rawData.slice(1, byteWidth + 1); - } - else { - - unfilteredLine = new Buffer(byteWidth); - - switch (filter) { - case 1: - this._unFilterType1(rawData, unfilteredLine, byteWidth); - break; - case 2: - this._unFilterType2(rawData, unfilteredLine, byteWidth); - break; - case 3: - this._unFilterType3(rawData, unfilteredLine, byteWidth); - break; - case 4: - this._unFilterType4(rawData, unfilteredLine, byteWidth); - break; - default: - throw new Error('Unrecognised filter type - ' + filter); - } - } - - this.write(unfilteredLine); - - currentImage.lineIndex++; - if (currentImage.lineIndex >= currentImage.height) { - this._lastLine = null; - this._imageIndex++; - currentImage = this._images[this._imageIndex]; - } - else { - this._lastLine = unfilteredLine; - } - - if (currentImage) { - // read, using the byte width that may be from the new current image - this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this)); - } - else { - this._lastLine = null; - this.complete(); - } -}; - -}).call(this,require("buffer").Buffer) -},{"./interlace":1594,"./paeth-predictor":1598,"buffer":33}],1593:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -function dePalette(indata, outdata, width, height, palette) { - var pxPos = 0; - // use values from palette - for (var y = 0; y < height; y++) { - for (var x = 0; x < width; x++) { - var color = palette[indata[pxPos]]; - - if (!color) { - throw new Error('index ' + indata[pxPos] + ' not in palette'); - } - - for (var i = 0; i < 4; i++) { - outdata[pxPos + i] = color[i]; - } - pxPos += 4; - } - } -} - -function replaceTransparentColor(indata, outdata, width, height, transColor) { - var pxPos = 0; - for (var y = 0; y < height; y++) { - for (var x = 0; x < width; x++) { - var makeTrans = false; - - if (transColor.length === 1) { - if (transColor[0] === indata[pxPos]) { - makeTrans = true; - } - } - else if (transColor[0] === indata[pxPos] && transColor[1] === indata[pxPos + 1] && transColor[2] === indata[pxPos + 2]) { - makeTrans = true; - } - if (makeTrans) { - for (var i = 0; i < 4; i++) { - outdata[pxPos + i] = 0; - } - } - pxPos += 4; - } - } -} - -function scaleDepth(indata, outdata, width, height, depth) { - var maxOutSample = 255; - var maxInSample = Math.pow(2, depth) - 1; - var pxPos = 0; - - for (var y = 0; y < height; y++) { - for (var x = 0; x < width; x++) { - for (var i = 0; i < 4; i++) { - outdata[pxPos + i] = Math.floor((indata[pxPos + i] * maxOutSample) / maxInSample + 0.5); - } - pxPos += 4; - } - } -} - -module.exports = function(indata, imageData) { - - var depth = imageData.depth; - var width = imageData.width; - var height = imageData.height; - var colorType = imageData.colorType; - var transColor = imageData.transColor; - var palette = imageData.palette; - - var outdata = indata; // only different for 16 bits - - if (colorType === 3) { // paletted - dePalette(indata, outdata, width, height, palette); - } - else { - if (transColor) { - replaceTransparentColor(indata, outdata, width, height, transColor); - } - // if it needs scaling - if (depth !== 8) { - // if we need to change the buffer size - if (depth === 16) { - outdata = new Buffer(width * height * 4); - } - scaleDepth(indata, outdata, width, height, depth); - } - } - return outdata; -}; - -}).call(this,require("buffer").Buffer) -},{"buffer":33}],1594:[function(require,module,exports){ -'use strict'; - -// Adam 7 -// 0 1 2 3 4 5 6 7 -// 0 x 6 4 6 x 6 4 6 -// 1 7 7 7 7 7 7 7 7 -// 2 5 6 5 6 5 6 5 6 -// 3 7 7 7 7 7 7 7 7 -// 4 3 6 4 6 3 6 4 6 -// 5 7 7 7 7 7 7 7 7 -// 6 5 6 5 6 5 6 5 6 -// 7 7 7 7 7 7 7 7 7 - - -var imagePasses = [ - { // pass 1 - 1px - x: [0], - y: [0] - }, - { // pass 2 - 1px - x: [4], - y: [0] - }, - { // pass 3 - 2px - x: [0, 4], - y: [4] - }, - { // pass 4 - 4px - x: [2, 6], - y: [0, 4] - }, - { // pass 5 - 8px - x: [0, 2, 4, 6], - y: [2, 6] - }, - { // pass 6 - 16px - x: [1, 3, 5, 7], - y: [0, 2, 4, 6] - }, - { // pass 7 - 32px - x: [0, 1, 2, 3, 4, 5, 6, 7], - y: [1, 3, 5, 7] - } -]; - -exports.getImagePasses = function(width, height) { - var images = []; - var xLeftOver = width % 8; - var yLeftOver = height % 8; - var xRepeats = (width - xLeftOver) / 8; - var yRepeats = (height - yLeftOver) / 8; - for (var i = 0; i < imagePasses.length; i++) { - var pass = imagePasses[i]; - var passWidth = xRepeats * pass.x.length; - var passHeight = yRepeats * pass.y.length; - for (var j = 0; j < pass.x.length; j++) { - if (pass.x[j] < xLeftOver) { - passWidth++; - } - else { - break; - } - } - for (j = 0; j < pass.y.length; j++) { - if (pass.y[j] < yLeftOver) { - passHeight++; - } - else { - break; - } - } - if (passWidth > 0 && passHeight > 0) { - images.push({ width: passWidth, height: passHeight, index: i }); - } - } - return images; -}; - -exports.getInterlaceIterator = function(width) { - return function(x, y, pass) { - var outerXLeftOver = x % imagePasses[pass].x.length; - var outerX = (((x - outerXLeftOver) / imagePasses[pass].x.length) * 8) + imagePasses[pass].x[outerXLeftOver]; - var outerYLeftOver = y % imagePasses[pass].y.length; - var outerY = (((y - outerYLeftOver) / imagePasses[pass].y.length) * 8) + imagePasses[pass].y[outerYLeftOver]; - return (outerX * 4) + (outerY * width * 4); - }; -}; -},{}],1595:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var util = require('util'); -var Stream = require('stream'); -var constants = require('./constants'); -var Packer = require('./packer'); - -var PackerAsync = module.exports = function(opt) { - Stream.call(this); - - var options = opt || {}; - - this._packer = new Packer(options); - this._deflate = this._packer.createDeflate(); - - this.readable = true; -}; -util.inherits(PackerAsync, Stream); - - -PackerAsync.prototype.pack = function(data, width, height, gamma) { - // Signature - this.emit('data', new Buffer(constants.PNG_SIGNATURE)); - this.emit('data', this._packer.packIHDR(width, height)); - - if (gamma) { - this.emit('data', this._packer.packGAMA(gamma)); - } - - var filteredData = this._packer.filterData(data, width, height); - - // compress it - this._deflate.on('error', this.emit.bind(this, 'error')); - - this._deflate.on('data', function(compressedData) { - this.emit('data', this._packer.packIDAT(compressedData)); - }.bind(this)); - - this._deflate.on('end', function() { - this.emit('data', this._packer.packIEND()); - this.emit('end'); - }.bind(this)); - - this._deflate.end(filteredData); -}; - -}).call(this,require("buffer").Buffer) -},{"./constants":1587,"./packer":1597,"buffer":33,"stream":62,"util":68}],1596:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var hasSyncZlib = true; -var zlib = require('zlib'); -if (!zlib.deflateSync) { - hasSyncZlib = false; -} -var constants = require('./constants'); -var Packer = require('./packer'); - -module.exports = function(metaData, opt) { - - if (!hasSyncZlib) { - throw new Error('To use the sync capability of this library in old node versions, please also add a dependency on node-zlb-backport'); - } - - var options = opt || {}; - - var packer = new Packer(options); - - var chunks = []; - - // Signature - chunks.push(new Buffer(constants.PNG_SIGNATURE)); - - // Header - chunks.push(packer.packIHDR(metaData.width, metaData.height)); - - if (metaData.gamma) { - chunks.push(packer.packGAMA(metaData.gamma)); - } - - var filteredData = packer.filterData(metaData.data, metaData.width, metaData.height); - - // compress it - var compressedData = zlib.deflateSync(filteredData, packer.getDeflateOptions()); - filteredData = null; - - if (!compressedData || !compressedData.length) { - throw new Error('bad png - invalid compressed data response'); - } - chunks.push(packer.packIDAT(compressedData)); - - // End - chunks.push(packer.packIEND()); - - return Buffer.concat(chunks); -}; - -}).call(this,require("buffer").Buffer) -},{"./constants":1587,"./packer":1597,"buffer":33,"zlib":32}],1597:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var constants = require('./constants'); -var CrcStream = require('./crc'); -var bitPacker = require('./bitpacker'); -var filter = require('./filter-pack'); -var zlib = require('zlib'); - -var Packer = module.exports = function(options) { - this._options = options; - - options.deflateChunkSize = options.deflateChunkSize || 32 * 1024; - options.deflateLevel = options.deflateLevel != null ? options.deflateLevel : 9; - options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3; - options.inputHasAlpha = options.inputHasAlpha != null ? options.inputHasAlpha : true; - options.deflateFactory = options.deflateFactory || zlib.createDeflate; - options.bitDepth = options.bitDepth || 8; - options.colorType = (typeof options.colorType === 'number') ? options.colorType : constants.COLORTYPE_COLOR_ALPHA; - - if (options.colorType !== constants.COLORTYPE_COLOR && options.colorType !== constants.COLORTYPE_COLOR_ALPHA) { - throw new Error('option color type:' + options.colorType + ' is not supported at present'); - } - if (options.bitDepth !== 8) { - throw new Error('option bit depth:' + options.bitDepth + ' is not supported at present'); - } -}; - -Packer.prototype.getDeflateOptions = function() { - return { - chunkSize: this._options.deflateChunkSize, - level: this._options.deflateLevel, - strategy: this._options.deflateStrategy - }; -}; - -Packer.prototype.createDeflate = function() { - return this._options.deflateFactory(this.getDeflateOptions()); -}; - -Packer.prototype.filterData = function(data, width, height) { - // convert to correct format for filtering (e.g. right bpp and bit depth) - var packedData = bitPacker(data, width, height, this._options); - - // filter pixel data - var bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType]; - var filteredData = filter(packedData, width, height, this._options, bpp); - return filteredData; -}; - -Packer.prototype._packChunk = function(type, data) { - - var len = (data ? data.length : 0); - var buf = new Buffer(len + 12); - - buf.writeUInt32BE(len, 0); - buf.writeUInt32BE(type, 4); - - if (data) { - data.copy(buf, 8); - } - - buf.writeInt32BE(CrcStream.crc32(buf.slice(4, buf.length - 4)), buf.length - 4); - return buf; -}; - -Packer.prototype.packGAMA = function(gamma) { - var buf = new Buffer(4); - buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0); - return this._packChunk(constants.TYPE_gAMA, buf); -}; - -Packer.prototype.packIHDR = function(width, height) { - - var buf = new Buffer(13); - buf.writeUInt32BE(width, 0); - buf.writeUInt32BE(height, 4); - buf[8] = this._options.bitDepth; // Bit depth - buf[9] = this._options.colorType; // colorType - buf[10] = 0; // compression - buf[11] = 0; // filter - buf[12] = 0; // interlace - - return this._packChunk(constants.TYPE_IHDR, buf); -}; - -Packer.prototype.packIDAT = function(data) { - return this._packChunk(constants.TYPE_IDAT, data); -}; - -Packer.prototype.packIEND = function() { - return this._packChunk(constants.TYPE_IEND, null); -}; -}).call(this,require("buffer").Buffer) -},{"./bitpacker":1585,"./constants":1587,"./crc":1588,"./filter-pack":1589,"buffer":33,"zlib":32}],1598:[function(require,module,exports){ -'use strict'; - -module.exports = function paethPredictor(left, above, upLeft) { - - var paeth = left + above - upLeft; - var pLeft = Math.abs(paeth - left); - var pAbove = Math.abs(paeth - above); - var pUpLeft = Math.abs(paeth - upLeft); - - if (pLeft <= pAbove && pLeft <= pUpLeft) { - return left; - } - if (pAbove <= pUpLeft) { - return above; - } - return upLeft; -}; -},{}],1599:[function(require,module,exports){ -'use strict'; - -var util = require('util'); -var zlib = require('zlib'); -var ChunkStream = require('./chunkstream'); -var FilterAsync = require('./filter-parse-async'); -var Parser = require('./parser'); -var bitmapper = require('./bitmapper'); -var formatNormaliser = require('./format-normaliser'); - -var ParserAsync = module.exports = function(options) { - ChunkStream.call(this); - - this._parser = new Parser(options, { - read: this.read.bind(this), - error: this._handleError.bind(this), - metadata: this._handleMetaData.bind(this), - gamma: this.emit.bind(this, 'gamma'), - palette: this._handlePalette.bind(this), - transColor: this._handleTransColor.bind(this), - finished: this._finished.bind(this), - inflateData: this._inflateData.bind(this) - }); - this._options = options; - this.writable = true; - - this._parser.start(); -}; -util.inherits(ParserAsync, ChunkStream); - - -ParserAsync.prototype._handleError = function(err) { - - this.emit('error', err); - - this.writable = false; - - this.destroy(); - - if (this._inflate && this._inflate.destroy) { - this._inflate.destroy(); - } - - this.errord = true; -}; - -ParserAsync.prototype._inflateData = function(data) { - if (!this._inflate) { - this._inflate = zlib.createInflate(); - - this._inflate.on('error', this.emit.bind(this, 'error')); - this._filter.on('complete', this._complete.bind(this)); - - this._inflate.pipe(this._filter); - } - this._inflate.write(data); -}; - -ParserAsync.prototype._handleMetaData = function(metaData) { - - this.emit('metadata', metaData); - - this._bitmapInfo = Object.create(metaData); - - this._filter = new FilterAsync(this._bitmapInfo); -}; - -ParserAsync.prototype._handleTransColor = function(transColor) { - this._bitmapInfo.transColor = transColor; -}; - -ParserAsync.prototype._handlePalette = function(palette) { - this._bitmapInfo.palette = palette; -}; - - -ParserAsync.prototype._finished = function() { - if (this.errord) { - return; - } - - if (!this._inflate) { - this.emit('error', 'No Inflate block'); - } - else { - // no more data to inflate - this._inflate.end(); - } - this.destroySoon(); -}; - -ParserAsync.prototype._complete = function(filteredData) { - - if (this.errord) { - return; - } - - try { - var bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo); - - var normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo); - bitmapData = null; - } - catch (ex) { - this._handleError(ex); - return; - } - - this.emit('parsed', normalisedBitmapData); -}; - -},{"./bitmapper":1584,"./chunkstream":1586,"./filter-parse-async":1590,"./format-normaliser":1593,"./parser":1601,"util":68,"zlib":32}],1600:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var hasSyncZlib = true; -var zlib = require('zlib'); -if (!zlib.deflateSync) { - hasSyncZlib = false; -} -var SyncReader = require('./sync-reader'); -var FilterSync = require('./filter-parse-sync'); -var Parser = require('./parser'); -var bitmapper = require('./bitmapper'); -var formatNormaliser = require('./format-normaliser'); - - -module.exports = function(buffer, options) { - - if (!hasSyncZlib) { - throw new Error('To use the sync capability of this library in old node versions, please also add a dependency on node-zlb-backport'); - } - - var err; - function handleError(_err_) { - err = _err_; - } - - var metaData; - function handleMetaData(_metaData_) { - metaData = _metaData_; - } - - function handleTransColor(transColor) { - metaData.transColor = transColor; - } - - function handlePalette(palette) { - metaData.palette = palette; - } - - var gamma; - function handleGamma(_gamma_) { - gamma = _gamma_; - } - - var inflateDataList = []; - function handleInflateData(inflatedData) { - inflateDataList.push(inflatedData); - } - - var reader = new SyncReader(buffer); - - var parser = new Parser(options, { - read: reader.read.bind(reader), - error: handleError, - metadata: handleMetaData, - gamma: handleGamma, - palette: handlePalette, - transColor: handleTransColor, - inflateData: handleInflateData - }); - - parser.start(); - reader.process(); - - if (err) { - throw err; - } - - //join together the inflate datas - var inflateData = Buffer.concat(inflateDataList); - inflateDataList.length = 0; - - var inflatedData = zlib.inflateSync(inflateData); - inflateData = null; - - if (!inflatedData || !inflatedData.length) { - throw new Error('bad png - invalid inflate data response'); - } - - var unfilteredData = FilterSync.process(inflatedData, metaData); - inflateData = null; - - var bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData); - unfilteredData = null; - - var normalisedBitmapData = formatNormaliser(bitmapData, metaData); - - metaData.data = normalisedBitmapData; - metaData.gamma = gamma || 0; - - return metaData; -}; - -}).call(this,require("buffer").Buffer) -},{"./bitmapper":1584,"./filter-parse-sync":1591,"./format-normaliser":1593,"./parser":1601,"./sync-reader":1604,"buffer":33,"zlib":32}],1601:[function(require,module,exports){ -(function (Buffer){ -'use strict'; - -var constants = require('./constants'); -var CrcCalculator = require('./crc'); - - -var Parser = module.exports = function(options, dependencies) { - - this._options = options; - options.checkCRC = options.checkCRC !== false; - - this._hasIHDR = false; - this._hasIEND = false; - - // input flags/metadata - this._palette = []; - this._colorType = 0; - - this._chunks = {}; - this._chunks[constants.TYPE_IHDR] = this._handleIHDR.bind(this); - this._chunks[constants.TYPE_IEND] = this._handleIEND.bind(this); - this._chunks[constants.TYPE_IDAT] = this._handleIDAT.bind(this); - this._chunks[constants.TYPE_PLTE] = this._handlePLTE.bind(this); - this._chunks[constants.TYPE_tRNS] = this._handleTRNS.bind(this); - this._chunks[constants.TYPE_gAMA] = this._handleGAMA.bind(this); - - this.read = dependencies.read; - this.error = dependencies.error; - this.metadata = dependencies.metadata; - this.gamma = dependencies.gamma; - this.transColor = dependencies.transColor; - this.palette = dependencies.palette; - this.parsed = dependencies.parsed; - this.inflateData = dependencies.inflateData; - this.inflateData = dependencies.inflateData; - this.finished = dependencies.finished; -}; - -Parser.prototype.start = function() { - this.read(constants.PNG_SIGNATURE.length, - this._parseSignature.bind(this) - ); -}; - -Parser.prototype._parseSignature = function(data) { - - var signature = constants.PNG_SIGNATURE; - - for (var i = 0; i < signature.length; i++) { - if (data[i] !== signature[i]) { - this.error(new Error('Invalid file signature')); - return; - } - } - this.read(8, this._parseChunkBegin.bind(this)); -}; - -Parser.prototype._parseChunkBegin = function(data) { - - // chunk content length - var length = data.readUInt32BE(0); - - // chunk type - var type = data.readUInt32BE(4); - var name = ''; - for (var i = 4; i < 8; i++) { - name += String.fromCharCode(data[i]); - } - - //console.log('chunk ', name, length); - - // chunk flags - var ancillary = Boolean(data[4] & 0x20); // or critical -// priv = Boolean(data[5] & 0x20), // or public -// safeToCopy = Boolean(data[7] & 0x20); // or unsafe - - if (!this._hasIHDR && type !== constants.TYPE_IHDR) { - this.error(new Error('Expected IHDR on beggining')); - return; - } - - this._crc = new CrcCalculator(); - this._crc.write(new Buffer(name)); - - if (this._chunks[type]) { - return this._chunks[type](length); - } - - if (!ancillary) { - this.error(new Error('Unsupported critical chunk type ' + name)); - return; - } - - this.read(length + 4, this._skipChunk.bind(this)); -}; - -Parser.prototype._skipChunk = function(/*data*/) { - this.read(8, this._parseChunkBegin.bind(this)); -}; - -Parser.prototype._handleChunkEnd = function() { - this.read(4, this._parseChunkEnd.bind(this)); -}; - -Parser.prototype._parseChunkEnd = function(data) { - - var fileCrc = data.readInt32BE(0); - var calcCrc = this._crc.crc32(); - - // check CRC - if (this._options.checkCRC && calcCrc !== fileCrc) { - this.error(new Error('Crc error - ' + fileCrc + ' - ' + calcCrc)); - return; - } - - if (!this._hasIEND) { - this.read(8, this._parseChunkBegin.bind(this)); - } -}; - -Parser.prototype._handleIHDR = function(length) { - this.read(length, this._parseIHDR.bind(this)); -}; -Parser.prototype._parseIHDR = function(data) { - - this._crc.write(data); - - var width = data.readUInt32BE(0); - var height = data.readUInt32BE(4); - var depth = data[8]; - var colorType = data[9]; // bits: 1 palette, 2 color, 4 alpha - var compr = data[10]; - var filter = data[11]; - var interlace = data[12]; - - // console.log(' width', width, 'height', height, - // 'depth', depth, 'colorType', colorType, - // 'compr', compr, 'filter', filter, 'interlace', interlace - // ); - - if (depth !== 8 && depth !== 4 && depth !== 2 && depth !== 1 && depth !== 16) { - this.error(new Error('Unsupported bit depth ' + depth)); - return; - } - if (!(colorType in constants.COLORTYPE_TO_BPP_MAP)) { - this.error(new Error('Unsupported color type')); - return; - } - if (compr !== 0) { - this.error(new Error('Unsupported compression method')); - return; - } - if (filter !== 0) { - this.error(new Error('Unsupported filter method')); - return; - } - if (interlace !== 0 && interlace !== 1) { - this.error(new Error('Unsupported interlace method')); - return; - } - - this._colorType = colorType; - - var bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType]; - - this._hasIHDR = true; - - this.metadata({ - width: width, - height: height, - depth: depth, - interlace: Boolean(interlace), - palette: Boolean(colorType & constants.COLORTYPE_PALETTE), - color: Boolean(colorType & constants.COLORTYPE_COLOR), - alpha: Boolean(colorType & constants.COLORTYPE_ALPHA), - bpp: bpp, - colorType: colorType - }); - - this._handleChunkEnd(); -}; - - -Parser.prototype._handlePLTE = function(length) { - this.read(length, this._parsePLTE.bind(this)); -}; -Parser.prototype._parsePLTE = function(data) { - - this._crc.write(data); - - var entries = Math.floor(data.length / 3); - // console.log('Palette:', entries); - - for (var i = 0; i < entries; i++) { - this._palette.push([ - data[i * 3], - data[i * 3 + 1], - data[i * 3 + 2], - 0xff - ]); - } - - this.palette(this._palette); - - this._handleChunkEnd(); -}; - -Parser.prototype._handleTRNS = function(length) { - this.read(length, this._parseTRNS.bind(this)); -}; -Parser.prototype._parseTRNS = function(data) { - - this._crc.write(data); - - // palette - if (this._colorType === constants.COLORTYPE_PALETTE_COLOR) { - if (this._palette.length === 0) { - this.error(new Error('Transparency chunk must be after palette')); - return; - } - if (data.length > this._palette.length) { - this.error(new Error('More transparent colors than palette size')); - return; - } - for (var i = 0; i < data.length; i++) { - this._palette[i][3] = data[i]; - } - this.palette(this._palette); - } - - // for colorType 0 (grayscale) and 2 (rgb) - // there might be one gray/color defined as transparent - if (this._colorType === constants.COLORTYPE_GRAYSCALE) { - // grey, 2 bytes - this.transColor([data.readUInt16BE(0)]); - } - if (this._colorType === constants.COLORTYPE_COLOR) { - this.transColor([data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)]); - } - - this._handleChunkEnd(); -}; - -Parser.prototype._handleGAMA = function(length) { - this.read(length, this._parseGAMA.bind(this)); -}; -Parser.prototype._parseGAMA = function(data) { - - this._crc.write(data); - this.gamma(data.readUInt32BE(0) / constants.GAMMA_DIVISION); - - this._handleChunkEnd(); -}; - -Parser.prototype._handleIDAT = function(length) { - this.read(-length, this._parseIDAT.bind(this, length)); -}; -Parser.prototype._parseIDAT = function(length, data) { - - this._crc.write(data); - - if (this._colorType === constants.COLORTYPE_PALETTE_COLOR && this._palette.length === 0) { - throw new Error('Expected palette not found'); - } - - this.inflateData(data); - var leftOverLength = length - data.length; - - if (leftOverLength > 0) { - this._handleIDAT(leftOverLength); - } - else { - this._handleChunkEnd(); - } -}; - -Parser.prototype._handleIEND = function(length) { - this.read(length, this._parseIEND.bind(this)); -}; -Parser.prototype._parseIEND = function(data) { - - this._crc.write(data); - - this._hasIEND = true; - this._handleChunkEnd(); - - if (this.finished) { - this.finished(); - } -}; - -}).call(this,require("buffer").Buffer) -},{"./constants":1587,"./crc":1588,"buffer":33}],1602:[function(require,module,exports){ -'use strict'; - - -var parse = require('./parser-sync'); -var pack = require('./packer-sync'); - - -exports.read = function(buffer, options) { - - return parse(buffer, options || {}); -}; - -exports.write = function(png) { - - return pack(png); -}; - -},{"./packer-sync":1596,"./parser-sync":1600}],1603:[function(require,module,exports){ -(function (process,Buffer){ -'use strict'; - -var util = require('util'); -var Stream = require('stream'); -var Parser = require('./parser-async'); -var Packer = require('./packer-async'); -var PNGSync = require('./png-sync'); - - -var PNG = exports.PNG = function(options) { - Stream.call(this); - - options = options || {}; // eslint-disable-line no-param-reassign - - this.width = options.width || 0; - this.height = options.height || 0; - - this.data = this.width > 0 && this.height > 0 ? - new Buffer(4 * this.width * this.height) : null; - - if (options.fill && this.data) { - this.data.fill(0); - } - - this.gamma = 0; - this.readable = this.writable = true; - - this._parser = new Parser(options); - - this._parser.on('error', this.emit.bind(this, 'error')); - this._parser.on('close', this._handleClose.bind(this)); - this._parser.on('metadata', this._metadata.bind(this)); - this._parser.on('gamma', this._gamma.bind(this)); - this._parser.on('parsed', function(data) { - this.data = data; - this.emit('parsed', data); - }.bind(this)); - - this._packer = new Packer(options); - this._packer.on('data', this.emit.bind(this, 'data')); - this._packer.on('end', this.emit.bind(this, 'end')); - this._parser.on('close', this._handleClose.bind(this)); - this._packer.on('error', this.emit.bind(this, 'error')); - -}; -util.inherits(PNG, Stream); - -PNG.sync = PNGSync; - -PNG.prototype.pack = function() { - - if (!this.data || !this.data.length) { - this.emit('error', 'No data provided'); - return this; - } - - process.nextTick(function() { - this._packer.pack(this.data, this.width, this.height, this.gamma); - }.bind(this)); - - return this; -}; - - -PNG.prototype.parse = function(data, callback) { - - if (callback) { - var onParsed, onError; - - onParsed = function(parsedData) { - this.removeListener('error', onError); - - this.data = parsedData; - callback(null, this); - }.bind(this); - - onError = function(err) { - this.removeListener('parsed', onParsed); - - callback(err, null); - }.bind(this); - - this.once('parsed', onParsed); - this.once('error', onError); - } - - this.end(data); - return this; -}; - -PNG.prototype.write = function(data) { - this._parser.write(data); - return true; -}; - -PNG.prototype.end = function(data) { - this._parser.end(data); -}; - -PNG.prototype._metadata = function(metadata) { - this.width = metadata.width; - this.height = metadata.height; - - this.emit('metadata', metadata); -}; - -PNG.prototype._gamma = function(gamma) { - this.gamma = gamma; -}; - -PNG.prototype._handleClose = function() { - if (!this._parser.writable && !this._packer.readable) { - this.emit('close'); - } -}; - - -PNG.bitblt = function(src, dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params - - if (srcX > src.width || srcY > src.height || srcX + width > src.width || srcY + height > src.height) { - throw new Error('bitblt reading outside image'); - } - - if (deltaX > dst.width || deltaY > dst.height || deltaX + width > dst.width || deltaY + height > dst.height) { - throw new Error('bitblt writing outside image'); - } - - for (var y = 0; y < height; y++) { - src.data.copy(dst.data, - ((deltaY + y) * dst.width + deltaX) << 2, - ((srcY + y) * src.width + srcX) << 2, - ((srcY + y) * src.width + srcX + width) << 2 - ); - } -}; - - -PNG.prototype.bitblt = function(dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params - - PNG.bitblt(this, dst, srcX, srcY, width, height, deltaX, deltaY); - return this; -}; - -PNG.adjustGamma = function(src) { - if (src.gamma) { - for (var y = 0; y < src.height; y++) { - for (var x = 0; x < src.width; x++) { - var idx = (src.width * y + x) << 2; - - for (var i = 0; i < 3; i++) { - var sample = src.data[idx + i] / 255; - sample = Math.pow(sample, 1 / 2.2 / src.gamma); - src.data[idx + i] = Math.round(sample * 255); - } - } - } - src.gamma = 0; - } -}; - -PNG.prototype.adjustGamma = function() { - PNG.adjustGamma(this); -}; - -}).call(this,require('_process'),require("buffer").Buffer) -},{"./packer-async":1595,"./parser-async":1599,"./png-sync":1602,"_process":41,"buffer":33,"stream":62,"util":68}],1604:[function(require,module,exports){ -'use strict'; - -var SyncReader = module.exports = function(buffer) { - - this._buffer = buffer; - this._reads = []; -}; - -SyncReader.prototype.read = function(length, callback) { - - this._reads.push({ - length: Math.abs(length), // if length < 0 then at most this length - allowLess: length < 0, - func: callback - }); -}; - -SyncReader.prototype.process = function() { - - // as long as there is any data and read requests - while (this._reads.length > 0 && this._buffer.length) { - - var read = this._reads[0]; - - if (this._buffer.length && (this._buffer.length >= read.length || read.allowLess)) { - - // ok there is any data so that we can satisfy this request - this._reads.shift(); // == read - - var buf = this._buffer; - - this._buffer = buf.slice(read.length); - - read.func.call(this, buf.slice(0, read.length)); - - } - else { - break; - } - - } - - if (this._reads.length > 0) { - return new Error('There are some read requests waitng on finished stream'); - } - - if (this._buffer.length > 0) { - return new Error('unrecognised content at end of stream'); - } - -}; - -},{}],1605:[function(require,module,exports){ -arguments[4][81][0].apply(exports,arguments) -},{"_process":41,"dup":81,"stream":62}],1606:[function(require,module,exports){ +},{"buffer":46}],963:[function(require,module,exports){ (function (Buffer){ 'use strict' @@ -184089,7 +169755,14395 @@ module.exports = function savePixels (array, type, options) { } }).call(this,require("buffer").Buffer) -},{"buffer":33,"contentstream":1549,"gif-encoder":1560,"jpeg-js":1573,"ndarray":1581,"ndarray-ops":1576,"pngjs-nozlib":1603,"through":1605}],1607:[function(require,module,exports){ +},{"buffer":46,"contentstream":66,"gif-encoder":114,"jpeg-js":960,"ndarray":443,"ndarray-ops":437,"pngjs-nozlib":917,"through":984}],964:[function(require,module,exports){ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global.ShelfPack = factory()); +}(this, function () { + +/** + * Create a new ShelfPack bin allocator. + * + * Uses the Shelf Best Height Fit algorithm from + * http://clb.demon.fi/files/RectangleBinPack.pdf + * + * @class ShelfPack + * @param {number} [w=64] Initial width of the sprite + * @param {number} [h=64] Initial width of the sprite + * @param {Object} [options] + * @param {boolean} [options.autoResize=false] If `true`, the sprite will automatically grow + * @example + * var sprite = new ShelfPack(64, 64, { autoResize: false }); + */ +function ShelfPack(w, h, options) { + options = options || {}; + this.w = w || 64; + this.h = h || 64; + this.autoResize = !!options.autoResize; + this.shelves = []; + this.stats = {}; + this.count = function(h) { + this.stats[h] = (this.stats[h] | 0) + 1; + }; +} + +/** + * Batch pack multiple bins into the sprite. + * + * @param {Array} bins Array of requested bins - each object should have `width`, `height` (or `w`, `h`) properties + * @param {Object} [options] + * @param {boolean} [options.inPlace=false] If `true`, the supplied bin objects will be updated inplace with `x` and `y` properties + * @returns {Array} Array of allocated bins - each bin is an object with `x`, `y`, `w`, `h` properties + * @example + * var bins = [ + * { id: 'a', width: 12, height: 12 }, + * { id: 'b', width: 12, height: 16 }, + * { id: 'c', width: 12, height: 24 } + * ]; + * var results = sprite.pack(bins, { inPlace: false }); + */ +ShelfPack.prototype.pack = function(bins, options) { + bins = [].concat(bins); + options = options || {}; + + var results = [], + w, h, allocation; + + for (var i = 0; i < bins.length; i++) { + w = bins[i].w || bins[i].width; + h = bins[i].h || bins[i].height; + if (w && h) { + allocation = this.packOne(w, h); + if (!allocation) { + continue; + } + if (options.inPlace) { + bins[i].x = allocation.x; + bins[i].y = allocation.y; + } + results.push(allocation); + } + } + + // Shrink the width/height of the sprite to the bare minimum. + // Since shelf-pack doubles first width, then height when running out of shelf space + // this can result in fairly large unused space both in width and height if that happens + // towards the end of bin packing. + if (this.shelves.length > 0) { + var w2 = 0; + var h2 = 0; + + for (var j = 0; j < this.shelves.length; j++) { + var shelf = this.shelves[j]; + h2 += shelf.h; + w2 = Math.max(shelf.w - shelf.free, w2); + } + + this.resize(w2, h2); + } + + return results; +}; + +/** + * Pack a single bin into the sprite. + * + * @param {number} w Width of the bin to allocate + * @param {number} h Height of the bin to allocate + * @returns {Object} Allocated bin object with `x`, `y`, `w`, `h` properties, or `null` if allocation failed + * @example + * var results = sprite.packOne(12, 16); + */ +ShelfPack.prototype.packOne = function(w, h) { + var y = 0, + best = { shelf: -1, waste: Infinity }, + shelf, waste; + + // find the best shelf + for (var i = 0; i < this.shelves.length; i++) { + shelf = this.shelves[i]; + y += shelf.h; + + // exactly the right height with width to spare, pack it.. + if (h === shelf.h && w <= shelf.free) { + this.count(h); + return shelf.alloc(w, h); + } + // not enough height or width, skip it.. + if (h > shelf.h || w > shelf.free) { + continue; + } + // maybe enough height or width, minimize waste.. + if (h < shelf.h && w <= shelf.free) { + waste = shelf.h - h; + if (waste < best.waste) { + best.waste = waste; + best.shelf = i; + } + } + } + + if (best.shelf !== -1) { + shelf = this.shelves[best.shelf]; + this.count(h); + return shelf.alloc(w, h); + } + + // add shelf.. + if (h <= (this.h - y) && w <= this.w) { + shelf = new Shelf(y, this.w, h); + this.shelves.push(shelf); + this.count(h); + return shelf.alloc(w, h); + } + + // no more space.. + // If `autoResize` option is set, grow the sprite as follows: + // * double whichever sprite dimension is smaller (`w1` or `h1`) + // * if sprite dimensions are equal, grow width before height + // * accomodate very large bin requests (big `w` or `h`) + if (this.autoResize) { + var h1, h2, w1, w2; + + h1 = h2 = this.h; + w1 = w2 = this.w; + + if (w1 <= h1 || w > w1) { // grow width.. + w2 = Math.max(w, w1) * 2; + } + if (h1 < w1 || h > h1) { // grow height.. + h2 = Math.max(h, h1) * 2; + } + + this.resize(w2, h2); + return this.packOne(w, h); // retry + } + + return null; +}; + +/** + * Clear the sprite. + * + * @example + * sprite.clear(); + */ +ShelfPack.prototype.clear = function() { + this.shelves = []; + this.stats = {}; +}; + +/** + * Resize the sprite. + * + * @param {number} w Requested new sprite width + * @param {number} h Requested new sprite height + * @returns {boolean} `true` if resize succeeded, `false` if failed + * @example + * sprite.resize(256, 256); + */ +ShelfPack.prototype.resize = function(w, h) { + this.w = w; + this.h = h; + for (var i = 0; i < this.shelves.length; i++) { + this.shelves[i].resize(w); + } + return true; +}; + + + +/** + * Create a new Shelf. + * + * @private + * @class Shelf + * @param {number} y Top coordinate of the new shelf + * @param {number} w Width of the new shelf + * @param {number} h Height of the new shelf + * @example + * var shelf = new Shelf(64, 512, 24); + */ +function Shelf(y, w, h) { + this.x = 0; + this.y = y; + this.w = this.free = w; + this.h = h; +} + +/** + * Allocate a single bin into the shelf. + * + * @private + * @param {number} w Width of the bin to allocate + * @param {number} h Height of the bin to allocate + * @returns {Object} Allocated bin object with `x`, `y`, `w`, `h` properties, or `null` if allocation failed + * @example + * shelf.alloc(12, 16); + */ +Shelf.prototype.alloc = function(w, h) { + if (w > this.free || h > this.h) { + return null; + } + var x = this.x; + this.x += w; + this.free -= w; + return { x: x, y: this.y, w: w, h: h, width: w, height: h }; +}; + +/** + * Resize the shelf. + * + * @private + * @param {number} w Requested new width of the shelf + * @returns {boolean} true if resize succeeded, false if failed + * @example + * shelf.resize(512); + */ +Shelf.prototype.resize = function(w) { + this.free += (w - this.w); + this.w = w; + return true; +}; + +return ShelfPack; + +})); +},{}],965:[function(require,module,exports){ +"use strict" + +module.exports = function signum(x) { + if(x < 0) { return -1 } + if(x > 0) { return 1 } + return 0.0 +} +},{}],966:[function(require,module,exports){ +'use strict' + +module.exports = boundary + +var bnd = require('boundary-cells') +var reduce = require('reduce-simplicial-complex') + +function boundary(cells) { + return reduce(bnd(cells)) +} + +},{"boundary-cells":35,"reduce-simplicial-complex":945}],967:[function(require,module,exports){ +'use strict' + +module.exports = extractContour + +var ndarray = require('ndarray') +var pool = require('typedarray-pool') +var ndsort = require('ndarray-sort') + +var contourAlgorithm = require('./lib/codegen') + +function getDimension(cells) { + var numCells = cells.length + var d = 0 + for(var i=0; i>1,v=E[2*m+1];', + 'if(v===b){return m}', + 'if(b 0) { + code.push(',') + } + code.push('[') + for(var j=0; j 0) { + code.push(',') + } + code.push('B(C,E,c[', f[0], '],c[', f[1], '])') + } + code.push(']') + } + code.push(');') + } + + for(var i=d+1; i>1; --i) { + if(i < d+1) { + code.push('else ') + } + code.push('if(l===', i, '){') + + //Generate mask + var maskStr = [] + for(var j=0; j> 1 + , s = compareCells(cells[mid], c) + if(s <= 0) { + if(s === 0) { + r = mid + } + lo = mid + 1 + } else if(s > 0) { + hi = mid - 1 + } + } + return r +} +exports.findCell = findCell; + +//Builds an index for an n-cell. This is more general than dual, but less efficient +function incidence(from_cells, to_cells) { + var index = new Array(from_cells.length) + for(var i=0, il=index.length; i= from_cells.length || compareCells(from_cells[idx], b) !== 0) { + break + } + } + } + } + return index +} +exports.incidence = incidence + +//Computes the dual of the mesh. This is basically an optimized version of buildIndex for the situation where from_cells is just the list of vertices +function dual(cells, vertex_count) { + if(!vertex_count) { + return incidence(unique(skeleton(cells, 0)), cells, 0) + } + var res = new Array(vertex_count) + for(var i=0; i>> k) & 1) { + b.push(c[k]) + } + } + result.push(b) + } + } + return normalize(result) +} +exports.explode = explode + +//Enumerates all of the n-cells of a cell complex +function skeleton(cells, n) { + if(n < 0) { + return [] + } + var result = [] + , k0 = (1<<(n+1))-1 + for(var i=0; i> 1 + } + return (i >> 1) - 1 + } + + //Bubble element i down the heap + function heapDown(i) { + var w = heapWeight(i) + while(true) { + var tw = w + var left = 2*i + 1 + var right = 2*(i + 1) + var next = i + if(left < heapCount) { + var lw = heapWeight(left) + if(lw < tw) { + next = left + tw = lw + } + } + if(right < heapCount) { + var rw = heapWeight(right) + if(rw < tw) { + next = right + } + } + if(next === i) { + return i + } + heapSwap(i, next) + i = next + } + } + + //Bubbles element i up the heap + function heapUp(i) { + var w = heapWeight(i) + while(i > 0) { + var parent = heapParent(i) + if(parent >= 0) { + var pw = heapWeight(parent) + if(w < pw) { + heapSwap(i, parent) + i = parent + continue + } + } + return i + } + } + + //Pop minimum element + function heapPop() { + if(heapCount > 0) { + var head = heap[0] + heapSwap(0, heapCount-1) + heapCount -= 1 + heapDown(0) + return head + } + return -1 + } + + //Update heap item i + function heapUpdate(i, w) { + var a = heap[i] + if(weights[a] === w) { + return i + } + weights[a] = -Infinity + heapUp(i) + heapPop() + weights[a] = w + heapCount += 1 + return heapUp(heapCount-1) + } + + //Kills a vertex (assume vertex already removed from heap) + function kill(i) { + if(dead[i]) { + return + } + //Kill vertex + dead[i] = true + //Fixup topology + var s = inv[i] + var t = outv[i] + if(inv[t] >= 0) { + inv[t] = s + } + if(outv[s] >= 0) { + outv[s] = t + } + + //Update weights on s and t + if(index[s] >= 0) { + heapUpdate(index[s], computeWeight(s)) + } + if(index[t] >= 0) { + heapUpdate(index[t], computeWeight(t)) + } + } + + //Initialize weights and heap + var heap = [] + var index = new Array(n) + for(var i=0; i>1; i>=0; --i) { + heapDown(i) + } + + //Kill vertices + while(true) { + var hmin = heapPop() + if((hmin < 0) || (weights[hmin] > minArea)) { + break + } + kill(hmin) + } + + //Build collapsed vertex table + var npositions = [] + for(var i=0; i= 0 && tout >= 0 && tin !== tout) { + var cin = index[tin] + var cout = index[tout] + if(cin !== cout) { + ncells.push([ cin, cout ]) + } + } + }) + + //Normalize result + sc.unique(sc.normalize(ncells)) + + //Return final list of cells + return { + positions: npositions, + edges: ncells + } +} +},{"robust-orientation":954,"simplicial-complex":971}],974:[function(require,module,exports){ +"use strict" + +module.exports = orderSegments + +var orient = require("robust-orientation") + +function horizontalOrder(a, b) { + var bl, br + if(b[0][0] < b[1][0]) { + bl = b[0] + br = b[1] + } else if(b[0][0] > b[1][0]) { + bl = b[1] + br = b[0] + } else { + var alo = Math.min(a[0][1], a[1][1]) + var ahi = Math.max(a[0][1], a[1][1]) + var blo = Math.min(b[0][1], b[1][1]) + var bhi = Math.max(b[0][1], b[1][1]) + if(ahi < blo) { + return ahi - blo + } + if(alo > bhi) { + return alo - bhi + } + return ahi - bhi + } + var al, ar + if(a[0][1] < a[1][1]) { + al = a[0] + ar = a[1] + } else { + al = a[1] + ar = a[0] + } + var d = orient(br, bl, al) + if(d) { + return d + } + d = orient(br, bl, ar) + if(d) { + return d + } + return ar - br +} + +function orderSegments(b, a) { + var al, ar + if(a[0][0] < a[1][0]) { + al = a[0] + ar = a[1] + } else if(a[0][0] > a[1][0]) { + al = a[1] + ar = a[0] + } else { + return horizontalOrder(a, b) + } + var bl, br + if(b[0][0] < b[1][0]) { + bl = b[0] + br = b[1] + } else if(b[0][0] > b[1][0]) { + bl = b[1] + br = b[0] + } else { + return -horizontalOrder(b, a) + } + var d1 = orient(al, ar, br) + var d2 = orient(al, ar, bl) + if(d1 < 0) { + if(d2 <= 0) { + return d1 + } + } else if(d1 > 0) { + if(d2 >= 0) { + return d1 + } + } else if(d2) { + return d2 + } + d1 = orient(br, bl, ar) + d2 = orient(br, bl, al) + if(d1 < 0) { + if(d2 <= 0) { + return d1 + } + } else if(d1 > 0) { + if(d2 >= 0) { + return d1 + } + } else if(d2) { + return d2 + } + return ar[0] - br[0] +} +},{"robust-orientation":954}],975:[function(require,module,exports){ +"use strict" + +module.exports = createSlabDecomposition + +var bounds = require("binary-search-bounds") +var createRBTree = require("functional-red-black-tree") +var orient = require("robust-orientation") +var orderSegments = require("./lib/order-segments") + +function SlabDecomposition(slabs, coordinates, horizontal) { + this.slabs = slabs + this.coordinates = coordinates + this.horizontal = horizontal +} + +var proto = SlabDecomposition.prototype + +function compareHorizontal(e, y) { + return e.y - y +} + +function searchBucket(root, p) { + var lastNode = null + while(root) { + var seg = root.key + var l, r + if(seg[0][0] < seg[1][0]) { + l = seg[0] + r = seg[1] + } else { + l = seg[1] + r = seg[0] + } + var o = orient(l, r, p) + if(o < 0) { + root = root.left + } else if(o > 0) { + if(p[0] !== seg[1][0]) { + lastNode = root + root = root.right + } else { + var val = searchBucket(root.right, p) + if(val) { + return val + } + root = root.left + } + } else { + if(p[0] !== seg[1][0]) { + return root + } else { + var val = searchBucket(root.right, p) + if(val) { + return val + } + root = root.left + } + } + } + return lastNode +} + +proto.castUp = function(p) { + var bucket = bounds.le(this.coordinates, p[0]) + if(bucket < 0) { + return -1 + } + var root = this.slabs[bucket] + var hitNode = searchBucket(this.slabs[bucket], p) + var lastHit = -1 + if(hitNode) { + lastHit = hitNode.value + } + //Edge case: need to handle horizontal segments (sucks) + if(this.coordinates[bucket] === p[0]) { + var lastSegment = null + if(hitNode) { + lastSegment = hitNode.key + } + if(bucket > 0) { + var otherHitNode = searchBucket(this.slabs[bucket-1], p) + if(otherHitNode) { + if(lastSegment) { + if(orderSegments(otherHitNode.key, lastSegment) > 0) { + lastSegment = otherHitNode.key + lastHit = otherHitNode.value + } + } else { + lastHit = otherHitNode.value + lastSegment = otherHitNode.key + } + } + } + var horiz = this.horizontal[bucket] + if(horiz.length > 0) { + var hbucket = bounds.ge(horiz, p[1], compareHorizontal) + if(hbucket < horiz.length) { + var e = horiz[hbucket] + if(p[1] === e.y) { + if(e.closed) { + return e.index + } else { + while(hbucket < horiz.length-1 && horiz[hbucket+1].y === p[1]) { + hbucket = hbucket+1 + e = horiz[hbucket] + if(e.closed) { + return e.index + } + } + if(e.y === p[1] && !e.start) { + hbucket = hbucket+1 + if(hbucket >= horiz.length) { + return lastHit + } + e = horiz[hbucket] + } + } + } + //Check if e is above/below last segment + if(e.start) { + if(lastSegment) { + var o = orient(lastSegment[0], lastSegment[1], [p[0], e.y]) + if(lastSegment[0][0] > lastSegment[1][0]) { + o = -o + } + if(o > 0) { + lastHit = e.index + } + } else { + lastHit = e.index + } + } else if(e.y !== p[1]) { + lastHit = e.index + } + } + } + } + return lastHit +} + +function IntervalSegment(y, index, start, closed) { + this.y = y + this.index = index + this.start = start + this.closed = closed +} + +function Event(x, segment, create, index) { + this.x = x + this.segment = segment + this.create = create + this.index = index +} + + +function createSlabDecomposition(segments) { + var numSegments = segments.length + var numEvents = 2 * numSegments + var events = new Array(numEvents) + for(var i=0; i 1.0) { + t = 1.0 + } + var ti = 1.0 - t + var n = a.length + var r = new Array(n) + for(var i=0; i 0) || (a > 0 && b < 0)) { + var p = lerpW(s, b, t, a) + pos.push(p) + neg.push(p.slice()) + } + if(b < 0) { + neg.push(t.slice()) + } else if(b > 0) { + pos.push(t.slice()) + } else { + pos.push(t.slice()) + neg.push(t.slice()) + } + a = b + } + return { positive: pos, negative: neg } +} + +function positive(points, plane) { + var pos = [] + var a = planeT(points[points.length-1], plane) + for(var s=points[points.length-1], t=points[0], i=0; i 0) || (a > 0 && b < 0)) { + pos.push(lerpW(s, b, t, a)) + } + if(b >= 0) { + pos.push(t.slice()) + } + a = b + } + return pos +} + +function negative(points, plane) { + var neg = [] + var a = planeT(points[points.length-1], plane) + for(var s=points[points.length-1], t=points[0], i=0; i 0) || (a > 0 && b < 0)) { + neg.push(lerpW(s, b, t, a)) + } + if(b <= 0) { + neg.push(t.slice()) + } + a = b + } + return neg +} +},{"robust-dot-product":951,"robust-sum":959}],977:[function(require,module,exports){ +(function(window) { + var re = { + not_string: /[^s]/, + number: /[diefg]/, + json: /[j]/, + not_json: /[^j]/, + text: /^[^\x25]+/, + modulo: /^\x25{2}/, + placeholder: /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijosuxX])/, + key: /^([a-z_][a-z_\d]*)/i, + key_access: /^\.([a-z_][a-z_\d]*)/i, + index_access: /^\[(\d+)\]/, + sign: /^[\+\-]/ + } + + function sprintf() { + var key = arguments[0], cache = sprintf.cache + if (!(cache[key] && cache.hasOwnProperty(key))) { + cache[key] = sprintf.parse(key) + } + return sprintf.format.call(null, cache[key], arguments) + } + + sprintf.format = function(parse_tree, argv) { + var cursor = 1, tree_length = parse_tree.length, node_type = "", arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = "" + for (i = 0; i < tree_length; i++) { + node_type = get_type(parse_tree[i]) + if (node_type === "string") { + output[output.length] = parse_tree[i] + } + else if (node_type === "array") { + match = parse_tree[i] // convenience purposes only + if (match[2]) { // keyword argument + arg = argv[cursor] + for (k = 0; k < match[2].length; k++) { + if (!arg.hasOwnProperty(match[2][k])) { + throw new Error(sprintf("[sprintf] property '%s' does not exist", match[2][k])) + } + arg = arg[match[2][k]] + } + } + else if (match[1]) { // positional argument (explicit) + arg = argv[match[1]] + } + else { // positional argument (implicit) + arg = argv[cursor++] + } + + if (get_type(arg) == "function") { + arg = arg() + } + + if (re.not_string.test(match[8]) && re.not_json.test(match[8]) && (get_type(arg) != "number" && isNaN(arg))) { + throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg))) + } + + if (re.number.test(match[8])) { + is_positive = arg >= 0 + } + + switch (match[8]) { + case "b": + arg = arg.toString(2) + break + case "c": + arg = String.fromCharCode(arg) + break + case "d": + case "i": + arg = parseInt(arg, 10) + break + case "j": + arg = JSON.stringify(arg, null, match[6] ? parseInt(match[6]) : 0) + break + case "e": + arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential() + break + case "f": + arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg) + break + case "g": + arg = match[7] ? parseFloat(arg).toPrecision(match[7]) : parseFloat(arg) + break + case "o": + arg = arg.toString(8) + break + case "s": + arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg) + break + case "u": + arg = arg >>> 0 + break + case "x": + arg = arg.toString(16) + break + case "X": + arg = arg.toString(16).toUpperCase() + break + } + if (re.json.test(match[8])) { + output[output.length] = arg + } + else { + if (re.number.test(match[8]) && (!is_positive || match[3])) { + sign = is_positive ? "+" : "-" + arg = arg.toString().replace(re.sign, "") + } + else { + sign = "" + } + pad_character = match[4] ? match[4] === "0" ? "0" : match[4].charAt(1) : " " + pad_length = match[6] - (sign + arg).length + pad = match[6] ? (pad_length > 0 ? str_repeat(pad_character, pad_length) : "") : "" + output[output.length] = match[5] ? sign + arg + pad : (pad_character === "0" ? sign + pad + arg : pad + sign + arg) + } + } + } + return output.join("") + } + + sprintf.cache = {} + + sprintf.parse = function(fmt) { + var _fmt = fmt, match = [], parse_tree = [], arg_names = 0 + while (_fmt) { + if ((match = re.text.exec(_fmt)) !== null) { + parse_tree[parse_tree.length] = match[0] + } + else if ((match = re.modulo.exec(_fmt)) !== null) { + parse_tree[parse_tree.length] = "%" + } + else if ((match = re.placeholder.exec(_fmt)) !== null) { + if (match[2]) { + arg_names |= 1 + var field_list = [], replacement_field = match[2], field_match = [] + if ((field_match = re.key.exec(replacement_field)) !== null) { + field_list[field_list.length] = field_match[1] + while ((replacement_field = replacement_field.substring(field_match[0].length)) !== "") { + if ((field_match = re.key_access.exec(replacement_field)) !== null) { + field_list[field_list.length] = field_match[1] + } + else if ((field_match = re.index_access.exec(replacement_field)) !== null) { + field_list[field_list.length] = field_match[1] + } + else { + throw new SyntaxError("[sprintf] failed to parse named argument key") + } + } + } + else { + throw new SyntaxError("[sprintf] failed to parse named argument key") + } + match[2] = field_list + } + else { + arg_names |= 2 + } + if (arg_names === 3) { + throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported") + } + parse_tree[parse_tree.length] = match + } + else { + throw new SyntaxError("[sprintf] unexpected placeholder") + } + _fmt = _fmt.substring(match[0].length) + } + return parse_tree + } + + var vsprintf = function(fmt, argv, _argv) { + _argv = (argv || []).slice(0) + _argv.splice(0, 0, fmt) + return sprintf.apply(null, _argv) + } + + /** + * helpers + */ + function get_type(variable) { + return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase() + } + + function str_repeat(input, multiplier) { + return Array(multiplier + 1).join(input) + } + + /** + * export to either browser or node.js + */ + if (typeof exports !== "undefined") { + exports.sprintf = sprintf + exports.vsprintf = vsprintf + } + else { + window.sprintf = sprintf + window.vsprintf = vsprintf + + if (typeof define === "function" && define.amd) { + define(function() { + return { + sprintf: sprintf, + vsprintf: vsprintf + } + }) + } + } +})(typeof window === "undefined" ? this : window); + +},{}],978:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +module.exports = Stream; + +var EE = require('events').EventEmitter; +var inherits = require('inherits'); + +inherits(Stream, EE); +Stream.Readable = require('readable-stream/readable.js'); +Stream.Writable = require('readable-stream/writable.js'); +Stream.Duplex = require('readable-stream/duplex.js'); +Stream.Transform = require('readable-stream/transform.js'); +Stream.PassThrough = require('readable-stream/passthrough.js'); + +// Backwards-compat with node 0.4.x +Stream.Stream = Stream; + + + +// old-style streams. Note that the pipe method (the only relevant +// part of this class) is overridden in the Readable class. + +function Stream() { + EE.call(this); +} + +Stream.prototype.pipe = function(dest, options) { + var source = this; + + function ondata(chunk) { + if (dest.writable) { + if (false === dest.write(chunk) && source.pause) { + source.pause(); + } + } + } + + source.on('data', ondata); + + function ondrain() { + if (source.readable && source.resume) { + source.resume(); + } + } + + dest.on('drain', ondrain); + + // If the 'end' option is not supplied, dest.end() will be called when + // source gets the 'end' or 'close' events. Only dest.end() once. + if (!dest._isStdio && (!options || options.end !== false)) { + source.on('end', onend); + source.on('close', onclose); + } + + var didOnEnd = false; + function onend() { + if (didOnEnd) return; + didOnEnd = true; + + dest.end(); + } + + + function onclose() { + if (didOnEnd) return; + didOnEnd = true; + + if (typeof dest.destroy === 'function') dest.destroy(); + } + + // don't leave dangling pipes when there are errors. + function onerror(er) { + cleanup(); + if (EE.listenerCount(this, 'error') === 0) { + throw er; // Unhandled stream error in pipe. + } + } + + source.on('error', onerror); + dest.on('error', onerror); + + // remove all the event listeners that were added. + function cleanup() { + source.removeListener('data', ondata); + dest.removeListener('drain', ondrain); + + source.removeListener('end', onend); + source.removeListener('close', onclose); + + source.removeListener('error', onerror); + dest.removeListener('error', onerror); + + source.removeListener('end', cleanup); + source.removeListener('close', cleanup); + + dest.removeListener('close', cleanup); + } + + source.on('end', cleanup); + source.on('close', cleanup); + + dest.on('close', cleanup); + + dest.emit('pipe', source); + + // Allow for unix-like usage: A.pipe(B).pipe(C) + return dest; +}; + +},{"events":95,"inherits":266,"readable-stream/duplex.js":934,"readable-stream/passthrough.js":941,"readable-stream/readable.js":942,"readable-stream/transform.js":943,"readable-stream/writable.js":944}],979:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var Buffer = require('buffer').Buffer; + +var isBufferEncoding = Buffer.isEncoding + || function(encoding) { + switch (encoding && encoding.toLowerCase()) { + case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': case 'raw': return true; + default: return false; + } + } + + +function assertEncoding(encoding) { + if (encoding && !isBufferEncoding(encoding)) { + throw new Error('Unknown encoding: ' + encoding); + } +} + +// StringDecoder provides an interface for efficiently splitting a series of +// buffers into a series of JS strings without breaking apart multi-byte +// characters. CESU-8 is handled as part of the UTF-8 encoding. +// +// @TODO Handling all encodings inside a single object makes it very difficult +// to reason about this code, so it should be split up in the future. +// @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code +// points as used by CESU-8. +var StringDecoder = exports.StringDecoder = function(encoding) { + this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); + assertEncoding(encoding); + switch (this.encoding) { + case 'utf8': + // CESU-8 represents each of Surrogate Pair by 3-bytes + this.surrogateSize = 3; + break; + case 'ucs2': + case 'utf16le': + // UTF-16 represents each of Surrogate Pair by 2-bytes + this.surrogateSize = 2; + this.detectIncompleteChar = utf16DetectIncompleteChar; + break; + case 'base64': + // Base-64 stores 3 bytes in 4 chars, and pads the remainder. + this.surrogateSize = 3; + this.detectIncompleteChar = base64DetectIncompleteChar; + break; + default: + this.write = passThroughWrite; + return; + } + + // Enough space to store all bytes of a single character. UTF-8 needs 4 + // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate). + this.charBuffer = new Buffer(6); + // Number of bytes received for the current incomplete multi-byte character. + this.charReceived = 0; + // Number of bytes expected for the current incomplete multi-byte character. + this.charLength = 0; +}; + + +// write decodes the given buffer and returns it as JS string that is +// guaranteed to not contain any partial multi-byte characters. Any partial +// character found at the end of the buffer is buffered up, and will be +// returned when calling write again with the remaining bytes. +// +// Note: Converting a Buffer containing an orphan surrogate to a String +// currently works, but converting a String to a Buffer (via `new Buffer`, or +// Buffer#write) will replace incomplete surrogates with the unicode +// replacement character. See https://codereview.chromium.org/121173009/ . +StringDecoder.prototype.write = function(buffer) { + var charStr = ''; + // if our last write ended with an incomplete multibyte character + while (this.charLength) { + // determine how many remaining bytes this buffer has to offer for this char + var available = (buffer.length >= this.charLength - this.charReceived) ? + this.charLength - this.charReceived : + buffer.length; + + // add the new bytes to the char buffer + buffer.copy(this.charBuffer, this.charReceived, 0, available); + this.charReceived += available; + + if (this.charReceived < this.charLength) { + // still not enough chars in this buffer? wait for more ... + return ''; + } + + // remove bytes belonging to the current character from the buffer + buffer = buffer.slice(available, buffer.length); + + // get the character that was split + charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding); + + // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character + var charCode = charStr.charCodeAt(charStr.length - 1); + if (charCode >= 0xD800 && charCode <= 0xDBFF) { + this.charLength += this.surrogateSize; + charStr = ''; + continue; + } + this.charReceived = this.charLength = 0; + + // if there are no more bytes in this buffer, just emit our char + if (buffer.length === 0) { + return charStr; + } + break; + } + + // determine and set charLength / charReceived + this.detectIncompleteChar(buffer); + + var end = buffer.length; + if (this.charLength) { + // buffer the incomplete character bytes we got + buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end); + end -= this.charReceived; + } + + charStr += buffer.toString(this.encoding, 0, end); + + var end = charStr.length - 1; + var charCode = charStr.charCodeAt(end); + // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character + if (charCode >= 0xD800 && charCode <= 0xDBFF) { + var size = this.surrogateSize; + this.charLength += size; + this.charReceived += size; + this.charBuffer.copy(this.charBuffer, size, 0, size); + buffer.copy(this.charBuffer, 0, 0, size); + return charStr.substring(0, end); + } + + // or just emit the charStr + return charStr; +}; + +// detectIncompleteChar determines if there is an incomplete UTF-8 character at +// the end of the given buffer. If so, it sets this.charLength to the byte +// length that character, and sets this.charReceived to the number of bytes +// that are available for this character. +StringDecoder.prototype.detectIncompleteChar = function(buffer) { + // determine how many bytes we have to check at the end of this buffer + var i = (buffer.length >= 3) ? 3 : buffer.length; + + // Figure out if one of the last i bytes of our buffer announces an + // incomplete char. + for (; i > 0; i--) { + var c = buffer[buffer.length - i]; + + // See http://en.wikipedia.org/wiki/UTF-8#Description + + // 110XXXXX + if (i == 1 && c >> 5 == 0x06) { + this.charLength = 2; + break; + } + + // 1110XXXX + if (i <= 2 && c >> 4 == 0x0E) { + this.charLength = 3; + break; + } + + // 11110XXX + if (i <= 3 && c >> 3 == 0x1E) { + this.charLength = 4; + break; + } + } + this.charReceived = i; +}; + +StringDecoder.prototype.end = function(buffer) { + var res = ''; + if (buffer && buffer.length) + res = this.write(buffer); + + if (this.charReceived) { + var cr = this.charReceived; + var buf = this.charBuffer; + var enc = this.encoding; + res += buf.slice(0, cr).toString(enc); + } + + return res; +}; + +function passThroughWrite(buffer) { + return buffer.toString(this.encoding); +} + +function utf16DetectIncompleteChar(buffer) { + this.charReceived = buffer.length % 2; + this.charLength = this.charReceived ? 2 : 0; +} + +function base64DetectIncompleteChar(buffer) { + this.charReceived = buffer.length % 3; + this.charLength = this.charReceived ? 3 : 0; +} + +},{"buffer":46}],980:[function(require,module,exports){ +'use strict'; + +var kdbush = require('kdbush'); + +module.exports = supercluster; + +function supercluster(options) { + return new SuperCluster(options); +} + +function SuperCluster(options) { + this.options = extend(Object.create(this.options), options); + this.trees = new Array(this.options.maxZoom + 1); +} + +SuperCluster.prototype = { + options: { + minZoom: 0, // min zoom to generate clusters on + maxZoom: 16, // max zoom level to cluster the points on + radius: 40, // cluster radius in pixels + extent: 512, // tile extent (radius is calculated relative to it) + nodeSize: 64, // size of the KD-tree leaf node, affects performance + log: false, // whether to log timing info + + // a reduce function for calculating custom cluster properties + reduce: null, // function (accumulated, props) { accumulated.sum += props.sum; } + + // initial properties of a cluster (before running the reducer) + initial: function () { return {}; }, // function () { return {sum: 0}; }, + + // properties to use for individual points when running the reducer + map: function (props) { return props; } // function (props) { return {sum: props.my_value}; }, + }, + + load: function (points) { + var log = this.options.log; + + if (log) console.time('total time'); + + var timerId = 'prepare ' + points.length + ' points'; + if (log) console.time(timerId); + + this.points = points; + + // generate a cluster object for each point + var clusters = points.map(createPointCluster); + if (log) console.timeEnd(timerId); + + // cluster points on max zoom, then cluster the results on previous zoom, etc.; + // results in a cluster hierarchy across zoom levels + for (var z = this.options.maxZoom; z >= this.options.minZoom; z--) { + var now = +Date.now(); + + // index input points into a KD-tree + this.trees[z + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); + + clusters = this._cluster(clusters, z); // create a new set of clusters for the zoom + + if (log) console.log('z%d: %d clusters in %dms', z, clusters.length, +Date.now() - now); + } + + // index top-level clusters + this.trees[this.options.minZoom] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); + + if (log) console.timeEnd('total time'); + + return this; + }, + + getClusters: function (bbox, zoom) { + var tree = this.trees[this._limitZoom(zoom)]; + var ids = tree.range(lngX(bbox[0]), latY(bbox[3]), lngX(bbox[2]), latY(bbox[1])); + var clusters = []; + for (var i = 0; i < ids.length; i++) { + var c = tree.points[ids[i]]; + clusters.push(c.numPoints ? getClusterJSON(c) : this.points[c.id]); + } + return clusters; + }, + + getChildren: function (clusterId, clusterZoom) { + var origin = this.trees[clusterZoom + 1].points[clusterId]; + var r = this.options.radius / (this.options.extent * Math.pow(2, clusterZoom)); + var points = this.trees[clusterZoom + 1].within(origin.x, origin.y, r); + var children = []; + for (var i = 0; i < points.length; i++) { + var c = this.trees[clusterZoom + 1].points[points[i]]; + if (c.parentId === clusterId) { + children.push(c.numPoints ? getClusterJSON(c) : this.points[c.id]); + } + } + return children; + }, + + getLeaves: function (clusterId, clusterZoom, limit, offset) { + limit = limit || 10; + offset = offset || 0; + + var leaves = []; + this._appendLeaves(leaves, clusterId, clusterZoom, limit, offset, 0); + + return leaves; + }, + + getTile: function (z, x, y) { + var tree = this.trees[this._limitZoom(z)]; + var z2 = Math.pow(2, z); + var extent = this.options.extent; + var r = this.options.radius; + var p = r / extent; + var top = (y - p) / z2; + var bottom = (y + 1 + p) / z2; + + var tile = { + features: [] + }; + + this._addTileFeatures( + tree.range((x - p) / z2, top, (x + 1 + p) / z2, bottom), + tree.points, x, y, z2, tile); + + if (x === 0) { + this._addTileFeatures( + tree.range(1 - p / z2, top, 1, bottom), + tree.points, z2, y, z2, tile); + } + if (x === z2 - 1) { + this._addTileFeatures( + tree.range(0, top, p / z2, bottom), + tree.points, -1, y, z2, tile); + } + + return tile.features.length ? tile : null; + }, + + getClusterExpansionZoom: function (clusterId, clusterZoom) { + while (clusterZoom < this.options.maxZoom) { + var children = this.getChildren(clusterId, clusterZoom); + clusterZoom++; + if (children.length !== 1) break; + clusterId = children[0].properties.cluster_id; + } + return clusterZoom; + }, + + _appendLeaves: function (result, clusterId, clusterZoom, limit, offset, skipped) { + var children = this.getChildren(clusterId, clusterZoom); + + for (var i = 0; i < children.length; i++) { + var props = children[i].properties; + + if (props.cluster) { + if (skipped + props.point_count <= offset) { + // skip the whole cluster + skipped += props.point_count; + } else { + // enter the cluster + skipped = this._appendLeaves( + result, props.cluster_id, clusterZoom + 1, limit, offset, skipped); + // exit the cluster + } + } else if (skipped < offset) { + // skip a single point + skipped++; + } else { + // add a single point + result.push(children[i]); + } + if (result.length === limit) break; + } + + return skipped; + }, + + _addTileFeatures: function (ids, points, x, y, z2, tile) { + for (var i = 0; i < ids.length; i++) { + var c = points[ids[i]]; + tile.features.push({ + type: 1, + geometry: [[ + Math.round(this.options.extent * (c.x * z2 - x)), + Math.round(this.options.extent * (c.y * z2 - y)) + ]], + tags: c.numPoints ? getClusterProperties(c) : this.points[c.id].properties + }); + } + }, + + _limitZoom: function (z) { + return Math.max(this.options.minZoom, Math.min(z, this.options.maxZoom + 1)); + }, + + _cluster: function (points, zoom) { + var clusters = []; + var r = this.options.radius / (this.options.extent * Math.pow(2, zoom)); + + // loop through each point + for (var i = 0; i < points.length; i++) { + var p = points[i]; + // if we've already visited the point at this zoom level, skip it + if (p.zoom <= zoom) continue; + p.zoom = zoom; + + // find all nearby points + var tree = this.trees[zoom + 1]; + var neighborIds = tree.within(p.x, p.y, r); + + var numPoints = p.numPoints || 1; + var wx = p.x * numPoints; + var wy = p.y * numPoints; + + var clusterProperties = null; + + if (this.options.reduce) { + clusterProperties = this.options.initial(); + this._accumulate(clusterProperties, p); + } + + for (var j = 0; j < neighborIds.length; j++) { + var b = tree.points[neighborIds[j]]; + // filter out neighbors that are too far or already processed + if (zoom < b.zoom) { + var numPoints2 = b.numPoints || 1; + b.zoom = zoom; // save the zoom (so it doesn't get processed twice) + wx += b.x * numPoints2; // accumulate coordinates for calculating weighted center + wy += b.y * numPoints2; + numPoints += numPoints2; + b.parentId = i; + + if (this.options.reduce) { + this._accumulate(clusterProperties, b); + } + } + } + + if (numPoints === 1) { + clusters.push(p); + } else { + p.parentId = i; + clusters.push(createCluster(wx / numPoints, wy / numPoints, numPoints, i, clusterProperties)); + } + } + + return clusters; + }, + + _accumulate: function (clusterProperties, point) { + var properties = point.numPoints ? + point.properties : + this.options.map(this.points[point.id].properties); + + this.options.reduce(clusterProperties, properties); + } +}; + +function createCluster(x, y, numPoints, id, properties) { + return { + x: x, // weighted cluster center + y: y, + zoom: Infinity, // the last zoom the cluster was processed at + id: id, // index of the first child of the cluster in the zoom level tree + properties: properties, + parentId: -1, // parent cluster id + numPoints: numPoints + }; +} + +function createPointCluster(p, id) { + var coords = p.geometry.coordinates; + return { + x: lngX(coords[0]), // projected point coordinates + y: latY(coords[1]), + zoom: Infinity, // the last zoom the point was processed at + id: id, // index of the source feature in the original input array + parentId: -1 // parent cluster id + }; +} + +function getClusterJSON(cluster) { + return { + type: 'Feature', + properties: getClusterProperties(cluster), + geometry: { + type: 'Point', + coordinates: [xLng(cluster.x), yLat(cluster.y)] + } + }; +} + +function getClusterProperties(cluster) { + var count = cluster.numPoints; + var abbrev = count >= 10000 ? Math.round(count / 1000) + 'k' : + count >= 1000 ? (Math.round(count / 100) / 10) + 'k' : count; + return extend(extend({}, cluster.properties), { + cluster: true, + cluster_id: cluster.id, + point_count: count, + point_count_abbreviated: abbrev + }); +} + +// longitude/latitude to spherical mercator in [0..1] range +function lngX(lng) { + return lng / 360 + 0.5; +} +function latY(lat) { + var sin = Math.sin(lat * Math.PI / 180), + y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI); + return y < 0 ? 0 : + y > 1 ? 1 : y; +} + +// spherical mercator to longitude/latitude +function xLng(x) { + return (x - 0.5) * 360; +} +function yLat(y) { + var y2 = (180 - y * 360) * Math.PI / 180; + return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90; +} + +function extend(dest, src) { + for (var id in src) dest[id] = src[id]; + return dest; +} + +function getX(p) { + return p.x; +} +function getY(p) { + return p.y; +} + +},{"kdbush":274}],981:[function(require,module,exports){ +'use strict' + +module.exports = toSuperScript + +var SUPERSCRIPTS = { + ' ': ' ', + '0': '⁰', + '1': '¹', + '2': '²', + '3': '³', + '4': '⁴', + '5': '⁵', + '6': '⁶', + '7': '⁷', + '8': '⁸', + '9': '⁹', + '+': '⁺', + '-': '⁻', + 'a': 'ᵃ', + 'b': 'ᵇ', + 'c': 'ᶜ', + 'd': 'ᵈ', + 'e': 'ᵉ', + 'f': 'ᶠ', + 'g': 'ᵍ', + 'h': 'ʰ', + 'i': 'ⁱ', + 'j': 'ʲ', + 'k': 'ᵏ', + 'l': 'ˡ', + 'm': 'ᵐ', + 'n': 'ⁿ', + 'o': 'ᵒ', + 'p': 'ᵖ', + 'r': 'ʳ', + 's': 'ˢ', + 't': 'ᵗ', + 'u': 'ᵘ', + 'v': 'ᵛ', + 'w': 'ʷ', + 'x': 'ˣ', + 'y': 'ʸ', + 'z': 'ᶻ' +} + +function toSuperScript(x) { + return x.split('').map(function(c) { + if(c in SUPERSCRIPTS) { + return SUPERSCRIPTS[c] + } + return '' + }).join('') +} + +},{}],982:[function(require,module,exports){ +"use strict" + +module.exports = surfaceNets + +var generateContourExtractor = require("ndarray-extract-contour") +var triangulateCube = require("triangulate-hypercube") +var zeroCrossings = require("zero-crossings") + +function buildSurfaceNets(order, dtype) { + var dimension = order.length + var code = ["'use strict';"] + var funcName = "surfaceNets" + order.join("_") + "d" + dtype + + //Contour extraction function + code.push( + "var contour=genContour({", + "order:[", order.join(), "],", + "scalarArguments: 3,", + "phase:function phaseFunc(p,a,b,c) { return (p > c)|0 },") + if(dtype === "generic") { + code.push("getters:[0],") + } + + //Generate vertex function + var cubeArgs = [] + var extraArgs = [] + for(var i=0; i>>7){") + } + for(var i=0; i<1<<(1< 128) { + if((i%128)===0) { + if(extraFuncs.length > 0) { + currentFunc.push("}}") + } + var efName = "vExtra" + extraFuncs.length + code.push("case ", (i>>>7), ":", efName, "(m&0x7f,", extraArgs.join(), ");break;") + currentFunc = [ + "function ", efName, "(m,", extraArgs.join(), "){switch(m){" + ] + extraFuncs.push(currentFunc) + } + } + currentFunc.push("case ", (i&0x7f), ":") + var crossings = new Array(dimension) + var denoms = new Array(dimension) + var crossingCount = new Array(dimension) + var bias = new Array(dimension) + var totalCrossings = 0 + for(var j=0; j j) { + continue + } + if(!(i&(1< 0) { + cStr = "+" + crossingCount[k] + "*c" + } + var weight = 0.5 * (crossings[k].length / totalCrossings) + var shift = 0.5 + 0.5 * (bias[k] / totalCrossings) + vertexStr.push("d" + k + "-" + shift + "-" + weight + "*(" + crossings[k].join("+") + cStr + ")/(" + denoms[k].join("+") + ")") + + } + } + currentFunc.push("a.push([", vertexStr.join(), "]);", + "break;") + } + code.push("}},") + if(extraFuncs.length > 0) { + currentFunc.push("}}") + } + + //Create face function + var faceArgs = [] + for(var i=0; i<(1<<(dimension-1)); ++i) { + faceArgs.push("v" + i) + } + faceArgs.push("c0", "c1", "p0", "p1", "a", "b", "c") + code.push("cell:function cellFunc(", faceArgs.join(), "){") + + var facets = triangulateCube(dimension-1) + code.push("if(p0){b.push(", + facets.map(function(f) { + return "[" + f.map(function(v) { + return "v" + v + }) + "]" + }).join(), ")}else{b.push(", + facets.map(function(f) { + var e = f.slice() + e.reverse() + return "[" + e.map(function(v) { + return "v" + v + }) + "]" + }).join(), + ")}}});function ", funcName, "(array,level){var verts=[],cells=[];contour(array,verts,cells,level);return {positions:verts,cells:cells};} return ", funcName, ";") + + for(var i=0; i0) { + shapeX += 0.02 + } + } + + var data = new Float32Array(bufferSize) + var ptr = 0 + var xOffset = -0.5 * shapeX + for(var i=0; i= 0; + var needsAlphaFormat = !formatSet && hasAlpha && (format === "hex" || format === "hex6" || format === "hex3" || format === "hex4" || format === "hex8" || format === "name"); + + if (needsAlphaFormat) { + // Special case for "transparent", all other non-alpha formats + // will return rgba when there is transparency. + if (format === "name" && this._a === 0) { + return this.toName(); + } + return this.toRgbString(); + } + if (format === "rgb") { + formattedString = this.toRgbString(); + } + if (format === "prgb") { + formattedString = this.toPercentageRgbString(); + } + if (format === "hex" || format === "hex6") { + formattedString = this.toHexString(); + } + if (format === "hex3") { + formattedString = this.toHexString(true); + } + if (format === "hex4") { + formattedString = this.toHex8String(true); + } + if (format === "hex8") { + formattedString = this.toHex8String(); + } + if (format === "name") { + formattedString = this.toName(); + } + if (format === "hsl") { + formattedString = this.toHslString(); + } + if (format === "hsv") { + formattedString = this.toHsvString(); + } + + return formattedString || this.toHexString(); + }, + clone: function() { + return tinycolor(this.toString()); + }, + + _applyModification: function(fn, args) { + var color = fn.apply(null, [this].concat([].slice.call(args))); + this._r = color._r; + this._g = color._g; + this._b = color._b; + this.setAlpha(color._a); + return this; + }, + lighten: function() { + return this._applyModification(lighten, arguments); + }, + brighten: function() { + return this._applyModification(brighten, arguments); + }, + darken: function() { + return this._applyModification(darken, arguments); + }, + desaturate: function() { + return this._applyModification(desaturate, arguments); + }, + saturate: function() { + return this._applyModification(saturate, arguments); + }, + greyscale: function() { + return this._applyModification(greyscale, arguments); + }, + spin: function() { + return this._applyModification(spin, arguments); + }, + + _applyCombination: function(fn, args) { + return fn.apply(null, [this].concat([].slice.call(args))); + }, + analogous: function() { + return this._applyCombination(analogous, arguments); + }, + complement: function() { + return this._applyCombination(complement, arguments); + }, + monochromatic: function() { + return this._applyCombination(monochromatic, arguments); + }, + splitcomplement: function() { + return this._applyCombination(splitcomplement, arguments); + }, + triad: function() { + return this._applyCombination(triad, arguments); + }, + tetrad: function() { + return this._applyCombination(tetrad, arguments); + } +}; + +// If input is an object, force 1 into "1.0" to handle ratios properly +// String input requires "1.0" as input, so 1 will be treated as 1 +tinycolor.fromRatio = function(color, opts) { + if (typeof color == "object") { + var newColor = {}; + for (var i in color) { + if (color.hasOwnProperty(i)) { + if (i === "a") { + newColor[i] = color[i]; + } + else { + newColor[i] = convertToPercentage(color[i]); + } + } + } + color = newColor; + } + + return tinycolor(color, opts); +}; + +// Given a string or object, convert that input to RGB +// Possible string inputs: +// +// "red" +// "#f00" or "f00" +// "#ff0000" or "ff0000" +// "#ff000000" or "ff000000" +// "rgb 255 0 0" or "rgb (255, 0, 0)" +// "rgb 1.0 0 0" or "rgb (1, 0, 0)" +// "rgba (255, 0, 0, 1)" or "rgba 255, 0, 0, 1" +// "rgba (1.0, 0, 0, 1)" or "rgba 1.0, 0, 0, 1" +// "hsl(0, 100%, 50%)" or "hsl 0 100% 50%" +// "hsla(0, 100%, 50%, 1)" or "hsla 0 100% 50%, 1" +// "hsv(0, 100%, 100%)" or "hsv 0 100% 100%" +// +function inputToRGB(color) { + + var rgb = { r: 0, g: 0, b: 0 }; + var a = 1; + var s = null; + var v = null; + var l = null; + var ok = false; + var format = false; + + if (typeof color == "string") { + color = stringInputToObject(color); + } + + if (typeof color == "object") { + if (isValidCSSUnit(color.r) && isValidCSSUnit(color.g) && isValidCSSUnit(color.b)) { + rgb = rgbToRgb(color.r, color.g, color.b); + ok = true; + format = String(color.r).substr(-1) === "%" ? "prgb" : "rgb"; + } + else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.v)) { + s = convertToPercentage(color.s); + v = convertToPercentage(color.v); + rgb = hsvToRgb(color.h, s, v); + ok = true; + format = "hsv"; + } + else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.l)) { + s = convertToPercentage(color.s); + l = convertToPercentage(color.l); + rgb = hslToRgb(color.h, s, l); + ok = true; + format = "hsl"; + } + + if (color.hasOwnProperty("a")) { + a = color.a; + } + } + + a = boundAlpha(a); + + return { + ok: ok, + format: color.format || format, + r: mathMin(255, mathMax(rgb.r, 0)), + g: mathMin(255, mathMax(rgb.g, 0)), + b: mathMin(255, mathMax(rgb.b, 0)), + a: a + }; +} + + +// Conversion Functions +// -------------------- + +// `rgbToHsl`, `rgbToHsv`, `hslToRgb`, `hsvToRgb` modified from: +// + +// `rgbToRgb` +// Handle bounds / percentage checking to conform to CSS color spec +// +// *Assumes:* r, g, b in [0, 255] or [0, 1] +// *Returns:* { r, g, b } in [0, 255] +function rgbToRgb(r, g, b){ + return { + r: bound01(r, 255) * 255, + g: bound01(g, 255) * 255, + b: bound01(b, 255) * 255 + }; +} + +// `rgbToHsl` +// Converts an RGB color value to HSL. +// *Assumes:* r, g, and b are contained in [0, 255] or [0, 1] +// *Returns:* { h, s, l } in [0,1] +function rgbToHsl(r, g, b) { + + r = bound01(r, 255); + g = bound01(g, 255); + b = bound01(b, 255); + + var max = mathMax(r, g, b), min = mathMin(r, g, b); + var h, s, l = (max + min) / 2; + + if(max == min) { + h = s = 0; // achromatic + } + else { + var d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + switch(max) { + case r: h = (g - b) / d + (g < b ? 6 : 0); break; + case g: h = (b - r) / d + 2; break; + case b: h = (r - g) / d + 4; break; + } + + h /= 6; + } + + return { h: h, s: s, l: l }; +} + +// `hslToRgb` +// Converts an HSL color value to RGB. +// *Assumes:* h is contained in [0, 1] or [0, 360] and s and l are contained [0, 1] or [0, 100] +// *Returns:* { r, g, b } in the set [0, 255] +function hslToRgb(h, s, l) { + var r, g, b; + + h = bound01(h, 360); + s = bound01(s, 100); + l = bound01(l, 100); + + function hue2rgb(p, q, t) { + if(t < 0) t += 1; + if(t > 1) t -= 1; + if(t < 1/6) return p + (q - p) * 6 * t; + if(t < 1/2) return q; + if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; + return p; + } + + if(s === 0) { + r = g = b = l; // achromatic + } + else { + var q = l < 0.5 ? l * (1 + s) : l + s - l * s; + var p = 2 * l - q; + r = hue2rgb(p, q, h + 1/3); + g = hue2rgb(p, q, h); + b = hue2rgb(p, q, h - 1/3); + } + + return { r: r * 255, g: g * 255, b: b * 255 }; +} + +// `rgbToHsv` +// Converts an RGB color value to HSV +// *Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1] +// *Returns:* { h, s, v } in [0,1] +function rgbToHsv(r, g, b) { + + r = bound01(r, 255); + g = bound01(g, 255); + b = bound01(b, 255); + + var max = mathMax(r, g, b), min = mathMin(r, g, b); + var h, s, v = max; + + var d = max - min; + s = max === 0 ? 0 : d / max; + + if(max == min) { + h = 0; // achromatic + } + else { + switch(max) { + case r: h = (g - b) / d + (g < b ? 6 : 0); break; + case g: h = (b - r) / d + 2; break; + case b: h = (r - g) / d + 4; break; + } + h /= 6; + } + return { h: h, s: s, v: v }; +} + +// `hsvToRgb` +// Converts an HSV color value to RGB. +// *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100] +// *Returns:* { r, g, b } in the set [0, 255] + function hsvToRgb(h, s, v) { + + h = bound01(h, 360) * 6; + s = bound01(s, 100); + v = bound01(v, 100); + + var i = Math.floor(h), + f = h - i, + p = v * (1 - s), + q = v * (1 - f * s), + t = v * (1 - (1 - f) * s), + mod = i % 6, + r = [v, q, p, p, t, v][mod], + g = [t, v, v, q, p, p][mod], + b = [p, p, t, v, v, q][mod]; + + return { r: r * 255, g: g * 255, b: b * 255 }; +} + +// `rgbToHex` +// Converts an RGB color to hex +// Assumes r, g, and b are contained in the set [0, 255] +// Returns a 3 or 6 character hex +function rgbToHex(r, g, b, allow3Char) { + + var hex = [ + pad2(mathRound(r).toString(16)), + pad2(mathRound(g).toString(16)), + pad2(mathRound(b).toString(16)) + ]; + + // Return a 3 character hex if possible + if (allow3Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1)) { + return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0); + } + + return hex.join(""); +} + +// `rgbaToHex` +// Converts an RGBA color plus alpha transparency to hex +// Assumes r, g, b are contained in the set [0, 255] and +// a in [0, 1]. Returns a 4 or 8 character rgba hex +function rgbaToHex(r, g, b, a, allow4Char) { + + var hex = [ + pad2(mathRound(r).toString(16)), + pad2(mathRound(g).toString(16)), + pad2(mathRound(b).toString(16)), + pad2(convertDecimalToHex(a)) + ]; + + // Return a 4 character hex if possible + if (allow4Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1) && hex[3].charAt(0) == hex[3].charAt(1)) { + return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0) + hex[3].charAt(0); + } + + return hex.join(""); +} + +// `rgbaToArgbHex` +// Converts an RGBA color to an ARGB Hex8 string +// Rarely used, but required for "toFilter()" +function rgbaToArgbHex(r, g, b, a) { + + var hex = [ + pad2(convertDecimalToHex(a)), + pad2(mathRound(r).toString(16)), + pad2(mathRound(g).toString(16)), + pad2(mathRound(b).toString(16)) + ]; + + return hex.join(""); +} + +// `equals` +// Can be called with any tinycolor input +tinycolor.equals = function (color1, color2) { + if (!color1 || !color2) { return false; } + return tinycolor(color1).toRgbString() == tinycolor(color2).toRgbString(); +}; + +tinycolor.random = function() { + return tinycolor.fromRatio({ + r: mathRandom(), + g: mathRandom(), + b: mathRandom() + }); +}; + + +// Modification Functions +// ---------------------- +// Thanks to less.js for some of the basics here +// + +function desaturate(color, amount) { + amount = (amount === 0) ? 0 : (amount || 10); + var hsl = tinycolor(color).toHsl(); + hsl.s -= amount / 100; + hsl.s = clamp01(hsl.s); + return tinycolor(hsl); +} + +function saturate(color, amount) { + amount = (amount === 0) ? 0 : (amount || 10); + var hsl = tinycolor(color).toHsl(); + hsl.s += amount / 100; + hsl.s = clamp01(hsl.s); + return tinycolor(hsl); +} + +function greyscale(color) { + return tinycolor(color).desaturate(100); +} + +function lighten (color, amount) { + amount = (amount === 0) ? 0 : (amount || 10); + var hsl = tinycolor(color).toHsl(); + hsl.l += amount / 100; + hsl.l = clamp01(hsl.l); + return tinycolor(hsl); +} + +function brighten(color, amount) { + amount = (amount === 0) ? 0 : (amount || 10); + var rgb = tinycolor(color).toRgb(); + rgb.r = mathMax(0, mathMin(255, rgb.r - mathRound(255 * - (amount / 100)))); + rgb.g = mathMax(0, mathMin(255, rgb.g - mathRound(255 * - (amount / 100)))); + rgb.b = mathMax(0, mathMin(255, rgb.b - mathRound(255 * - (amount / 100)))); + return tinycolor(rgb); +} + +function darken (color, amount) { + amount = (amount === 0) ? 0 : (amount || 10); + var hsl = tinycolor(color).toHsl(); + hsl.l -= amount / 100; + hsl.l = clamp01(hsl.l); + return tinycolor(hsl); +} + +// Spin takes a positive or negative amount within [-360, 360] indicating the change of hue. +// Values outside of this range will be wrapped into this range. +function spin(color, amount) { + var hsl = tinycolor(color).toHsl(); + var hue = (hsl.h + amount) % 360; + hsl.h = hue < 0 ? 360 + hue : hue; + return tinycolor(hsl); +} + +// Combination Functions +// --------------------- +// Thanks to jQuery xColor for some of the ideas behind these +// + +function complement(color) { + var hsl = tinycolor(color).toHsl(); + hsl.h = (hsl.h + 180) % 360; + return tinycolor(hsl); +} + +function triad(color) { + var hsl = tinycolor(color).toHsl(); + var h = hsl.h; + return [ + tinycolor(color), + tinycolor({ h: (h + 120) % 360, s: hsl.s, l: hsl.l }), + tinycolor({ h: (h + 240) % 360, s: hsl.s, l: hsl.l }) + ]; +} + +function tetrad(color) { + var hsl = tinycolor(color).toHsl(); + var h = hsl.h; + return [ + tinycolor(color), + tinycolor({ h: (h + 90) % 360, s: hsl.s, l: hsl.l }), + tinycolor({ h: (h + 180) % 360, s: hsl.s, l: hsl.l }), + tinycolor({ h: (h + 270) % 360, s: hsl.s, l: hsl.l }) + ]; +} + +function splitcomplement(color) { + var hsl = tinycolor(color).toHsl(); + var h = hsl.h; + return [ + tinycolor(color), + tinycolor({ h: (h + 72) % 360, s: hsl.s, l: hsl.l}), + tinycolor({ h: (h + 216) % 360, s: hsl.s, l: hsl.l}) + ]; +} + +function analogous(color, results, slices) { + results = results || 6; + slices = slices || 30; + + var hsl = tinycolor(color).toHsl(); + var part = 360 / slices; + var ret = [tinycolor(color)]; + + for (hsl.h = ((hsl.h - (part * results >> 1)) + 720) % 360; --results; ) { + hsl.h = (hsl.h + part) % 360; + ret.push(tinycolor(hsl)); + } + return ret; +} + +function monochromatic(color, results) { + results = results || 6; + var hsv = tinycolor(color).toHsv(); + var h = hsv.h, s = hsv.s, v = hsv.v; + var ret = []; + var modification = 1 / results; + + while (results--) { + ret.push(tinycolor({ h: h, s: s, v: v})); + v = (v + modification) % 1; + } + + return ret; +} + +// Utility Functions +// --------------------- + +tinycolor.mix = function(color1, color2, amount) { + amount = (amount === 0) ? 0 : (amount || 50); + + var rgb1 = tinycolor(color1).toRgb(); + var rgb2 = tinycolor(color2).toRgb(); + + var p = amount / 100; + + var rgba = { + r: ((rgb2.r - rgb1.r) * p) + rgb1.r, + g: ((rgb2.g - rgb1.g) * p) + rgb1.g, + b: ((rgb2.b - rgb1.b) * p) + rgb1.b, + a: ((rgb2.a - rgb1.a) * p) + rgb1.a + }; + + return tinycolor(rgba); +}; + + +// Readability Functions +// --------------------- +// false +// tinycolor.isReadable("#000", "#111",{level:"AA",size:"large"}) => false +tinycolor.isReadable = function(color1, color2, wcag2) { + var readability = tinycolor.readability(color1, color2); + var wcag2Parms, out; + + out = false; + + wcag2Parms = validateWCAG2Parms(wcag2); + switch (wcag2Parms.level + wcag2Parms.size) { + case "AAsmall": + case "AAAlarge": + out = readability >= 4.5; + break; + case "AAlarge": + out = readability >= 3; + break; + case "AAAsmall": + out = readability >= 7; + break; + } + return out; + +}; + +// `mostReadable` +// Given a base color and a list of possible foreground or background +// colors for that base, returns the most readable color. +// Optionally returns Black or White if the most readable color is unreadable. +// *Example* +// tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:false}).toHexString(); // "#112255" +// tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:true}).toHexString(); // "#ffffff" +// tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"large"}).toHexString(); // "#faf3f3" +// tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"small"}).toHexString(); // "#ffffff" +tinycolor.mostReadable = function(baseColor, colorList, args) { + var bestColor = null; + var bestScore = 0; + var readability; + var includeFallbackColors, level, size ; + args = args || {}; + includeFallbackColors = args.includeFallbackColors ; + level = args.level; + size = args.size; + + for (var i= 0; i < colorList.length ; i++) { + readability = tinycolor.readability(baseColor, colorList[i]); + if (readability > bestScore) { + bestScore = readability; + bestColor = tinycolor(colorList[i]); + } + } + + if (tinycolor.isReadable(baseColor, bestColor, {"level":level,"size":size}) || !includeFallbackColors) { + return bestColor; + } + else { + args.includeFallbackColors=false; + return tinycolor.mostReadable(baseColor,["#fff", "#000"],args); + } +}; + + +// Big List of Colors +// ------------------ +// +var names = tinycolor.names = { + aliceblue: "f0f8ff", + antiquewhite: "faebd7", + aqua: "0ff", + aquamarine: "7fffd4", + azure: "f0ffff", + beige: "f5f5dc", + bisque: "ffe4c4", + black: "000", + blanchedalmond: "ffebcd", + blue: "00f", + blueviolet: "8a2be2", + brown: "a52a2a", + burlywood: "deb887", + burntsienna: "ea7e5d", + cadetblue: "5f9ea0", + chartreuse: "7fff00", + chocolate: "d2691e", + coral: "ff7f50", + cornflowerblue: "6495ed", + cornsilk: "fff8dc", + crimson: "dc143c", + cyan: "0ff", + darkblue: "00008b", + darkcyan: "008b8b", + darkgoldenrod: "b8860b", + darkgray: "a9a9a9", + darkgreen: "006400", + darkgrey: "a9a9a9", + darkkhaki: "bdb76b", + darkmagenta: "8b008b", + darkolivegreen: "556b2f", + darkorange: "ff8c00", + darkorchid: "9932cc", + darkred: "8b0000", + darksalmon: "e9967a", + darkseagreen: "8fbc8f", + darkslateblue: "483d8b", + darkslategray: "2f4f4f", + darkslategrey: "2f4f4f", + darkturquoise: "00ced1", + darkviolet: "9400d3", + deeppink: "ff1493", + deepskyblue: "00bfff", + dimgray: "696969", + dimgrey: "696969", + dodgerblue: "1e90ff", + firebrick: "b22222", + floralwhite: "fffaf0", + forestgreen: "228b22", + fuchsia: "f0f", + gainsboro: "dcdcdc", + ghostwhite: "f8f8ff", + gold: "ffd700", + goldenrod: "daa520", + gray: "808080", + green: "008000", + greenyellow: "adff2f", + grey: "808080", + honeydew: "f0fff0", + hotpink: "ff69b4", + indianred: "cd5c5c", + indigo: "4b0082", + ivory: "fffff0", + khaki: "f0e68c", + lavender: "e6e6fa", + lavenderblush: "fff0f5", + lawngreen: "7cfc00", + lemonchiffon: "fffacd", + lightblue: "add8e6", + lightcoral: "f08080", + lightcyan: "e0ffff", + lightgoldenrodyellow: "fafad2", + lightgray: "d3d3d3", + lightgreen: "90ee90", + lightgrey: "d3d3d3", + lightpink: "ffb6c1", + lightsalmon: "ffa07a", + lightseagreen: "20b2aa", + lightskyblue: "87cefa", + lightslategray: "789", + lightslategrey: "789", + lightsteelblue: "b0c4de", + lightyellow: "ffffe0", + lime: "0f0", + limegreen: "32cd32", + linen: "faf0e6", + magenta: "f0f", + maroon: "800000", + mediumaquamarine: "66cdaa", + mediumblue: "0000cd", + mediumorchid: "ba55d3", + mediumpurple: "9370db", + mediumseagreen: "3cb371", + mediumslateblue: "7b68ee", + mediumspringgreen: "00fa9a", + mediumturquoise: "48d1cc", + mediumvioletred: "c71585", + midnightblue: "191970", + mintcream: "f5fffa", + mistyrose: "ffe4e1", + moccasin: "ffe4b5", + navajowhite: "ffdead", + navy: "000080", + oldlace: "fdf5e6", + olive: "808000", + olivedrab: "6b8e23", + orange: "ffa500", + orangered: "ff4500", + orchid: "da70d6", + palegoldenrod: "eee8aa", + palegreen: "98fb98", + paleturquoise: "afeeee", + palevioletred: "db7093", + papayawhip: "ffefd5", + peachpuff: "ffdab9", + peru: "cd853f", + pink: "ffc0cb", + plum: "dda0dd", + powderblue: "b0e0e6", + purple: "800080", + rebeccapurple: "663399", + red: "f00", + rosybrown: "bc8f8f", + royalblue: "4169e1", + saddlebrown: "8b4513", + salmon: "fa8072", + sandybrown: "f4a460", + seagreen: "2e8b57", + seashell: "fff5ee", + sienna: "a0522d", + silver: "c0c0c0", + skyblue: "87ceeb", + slateblue: "6a5acd", + slategray: "708090", + slategrey: "708090", + snow: "fffafa", + springgreen: "00ff7f", + steelblue: "4682b4", + tan: "d2b48c", + teal: "008080", + thistle: "d8bfd8", + tomato: "ff6347", + turquoise: "40e0d0", + violet: "ee82ee", + wheat: "f5deb3", + white: "fff", + whitesmoke: "f5f5f5", + yellow: "ff0", + yellowgreen: "9acd32" +}; + +// Make it easy to access colors via `hexNames[hex]` +var hexNames = tinycolor.hexNames = flip(names); + + +// Utilities +// --------- + +// `{ 'name1': 'val1' }` becomes `{ 'val1': 'name1' }` +function flip(o) { + var flipped = { }; + for (var i in o) { + if (o.hasOwnProperty(i)) { + flipped[o[i]] = i; + } + } + return flipped; +} + +// Return a valid alpha value [0,1] with all invalid values being set to 1 +function boundAlpha(a) { + a = parseFloat(a); + + if (isNaN(a) || a < 0 || a > 1) { + a = 1; + } + + return a; +} + +// Take input from [0, n] and return it as [0, 1] +function bound01(n, max) { + if (isOnePointZero(n)) { n = "100%"; } + + var processPercent = isPercentage(n); + n = mathMin(max, mathMax(0, parseFloat(n))); + + // Automatically convert percentage into number + if (processPercent) { + n = parseInt(n * max, 10) / 100; + } + + // Handle floating point rounding errors + if ((Math.abs(n - max) < 0.000001)) { + return 1; + } + + // Convert into [0, 1] range if it isn't already + return (n % max) / parseFloat(max); +} + +// Force a number between 0 and 1 +function clamp01(val) { + return mathMin(1, mathMax(0, val)); +} + +// Parse a base-16 hex value into a base-10 integer +function parseIntFromHex(val) { + return parseInt(val, 16); +} + +// Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1 +// +function isOnePointZero(n) { + return typeof n == "string" && n.indexOf('.') != -1 && parseFloat(n) === 1; +} + +// Check to see if string passed in is a percentage +function isPercentage(n) { + return typeof n === "string" && n.indexOf('%') != -1; +} + +// Force a hex value to have 2 characters +function pad2(c) { + return c.length == 1 ? '0' + c : '' + c; +} + +// Replace a decimal with it's percentage value +function convertToPercentage(n) { + if (n <= 1) { + n = (n * 100) + "%"; + } + + return n; +} + +// Converts a decimal to a hex value +function convertDecimalToHex(d) { + return Math.round(parseFloat(d) * 255).toString(16); +} +// Converts a hex value to a decimal +function convertHexToDecimal(h) { + return (parseIntFromHex(h) / 255); +} + +var matchers = (function() { + + // + var CSS_INTEGER = "[-\\+]?\\d+%?"; + + // + var CSS_NUMBER = "[-\\+]?\\d*\\.\\d+%?"; + + // Allow positive/negative integer/number. Don't capture the either/or, just the entire outcome. + var CSS_UNIT = "(?:" + CSS_NUMBER + ")|(?:" + CSS_INTEGER + ")"; + + // Actual matching. + // Parentheses and commas are optional, but not required. + // Whitespace can take the place of commas or opening paren + var PERMISSIVE_MATCH3 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?"; + var PERMISSIVE_MATCH4 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?"; + + return { + CSS_UNIT: new RegExp(CSS_UNIT), + rgb: new RegExp("rgb" + PERMISSIVE_MATCH3), + rgba: new RegExp("rgba" + PERMISSIVE_MATCH4), + hsl: new RegExp("hsl" + PERMISSIVE_MATCH3), + hsla: new RegExp("hsla" + PERMISSIVE_MATCH4), + hsv: new RegExp("hsv" + PERMISSIVE_MATCH3), + hsva: new RegExp("hsva" + PERMISSIVE_MATCH4), + hex3: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, + hex6: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/, + hex4: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, + hex8: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/ + }; +})(); + +// `isValidCSSUnit` +// Take in a single string / number and check to see if it looks like a CSS unit +// (see `matchers` above for definition). +function isValidCSSUnit(color) { + return !!matchers.CSS_UNIT.exec(color); +} + +// `stringInputToObject` +// Permissive string parsing. Take in a number of formats, and output an object +// based on detected format. Returns `{ r, g, b }` or `{ h, s, l }` or `{ h, s, v}` +function stringInputToObject(color) { + + color = color.replace(trimLeft,'').replace(trimRight, '').toLowerCase(); + var named = false; + if (names[color]) { + color = names[color]; + named = true; + } + else if (color == 'transparent') { + return { r: 0, g: 0, b: 0, a: 0, format: "name" }; + } + + // Try to match string input using regular expressions. + // Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360] + // Just return an object and let the conversion functions handle that. + // This way the result will be the same whether the tinycolor is initialized with string or object. + var match; + if ((match = matchers.rgb.exec(color))) { + return { r: match[1], g: match[2], b: match[3] }; + } + if ((match = matchers.rgba.exec(color))) { + return { r: match[1], g: match[2], b: match[3], a: match[4] }; + } + if ((match = matchers.hsl.exec(color))) { + return { h: match[1], s: match[2], l: match[3] }; + } + if ((match = matchers.hsla.exec(color))) { + return { h: match[1], s: match[2], l: match[3], a: match[4] }; + } + if ((match = matchers.hsv.exec(color))) { + return { h: match[1], s: match[2], v: match[3] }; + } + if ((match = matchers.hsva.exec(color))) { + return { h: match[1], s: match[2], v: match[3], a: match[4] }; + } + if ((match = matchers.hex8.exec(color))) { + return { + r: parseIntFromHex(match[1]), + g: parseIntFromHex(match[2]), + b: parseIntFromHex(match[3]), + a: convertHexToDecimal(match[4]), + format: named ? "name" : "hex8" + }; + } + if ((match = matchers.hex6.exec(color))) { + return { + r: parseIntFromHex(match[1]), + g: parseIntFromHex(match[2]), + b: parseIntFromHex(match[3]), + format: named ? "name" : "hex" + }; + } + if ((match = matchers.hex4.exec(color))) { + return { + r: parseIntFromHex(match[1] + '' + match[1]), + g: parseIntFromHex(match[2] + '' + match[2]), + b: parseIntFromHex(match[3] + '' + match[3]), + a: convertHexToDecimal(match[4] + '' + match[4]), + format: named ? "name" : "hex8" + }; + } + if ((match = matchers.hex3.exec(color))) { + return { + r: parseIntFromHex(match[1] + '' + match[1]), + g: parseIntFromHex(match[2] + '' + match[2]), + b: parseIntFromHex(match[3] + '' + match[3]), + format: named ? "name" : "hex" + }; + } + + return false; +} + +function validateWCAG2Parms(parms) { + // return valid WCAG2 parms for isReadable. + // If input parms are invalid, return {"level":"AA", "size":"small"} + var level, size; + parms = parms || {"level":"AA", "size":"small"}; + level = (parms.level || "AA").toUpperCase(); + size = (parms.size || "small").toLowerCase(); + if (level !== "AA" && level !== "AAA") { + level = "AA"; + } + if (size !== "small" && size !== "large") { + size = "small"; + } + return {"level":level, "size":size}; +} + +// Node: Export function +if (typeof module !== "undefined" && module.exports) { + module.exports = tinycolor; +} +// AMD/requirejs: Define the module +else if (typeof define === 'function' && define.amd) { + define(function () {return tinycolor;}); +} +// Browser: Expose to window +else { + window.tinycolor = tinycolor; +} + +})(Math); + +},{}],986:[function(require,module,exports){ +'use strict' + +var parseUnit = require('parse-unit') + +module.exports = toPX + +var PIXELS_PER_INCH = 96 + +function getPropertyInPX(element, prop) { + var parts = parseUnit(getComputedStyle(element).getPropertyValue(prop)) + return parts[0] * toPX(parts[1], element) +} + +//This brutal hack is needed +function getSizeBrutal(unit, element) { + var testDIV = document.createElement('div') + testDIV.style['font-size'] = '128' + unit + element.appendChild(testDIV) + var size = getPropertyInPX(testDIV, 'font-size') / 128 + element.removeChild(testDIV) + return size +} + +function toPX(str, element) { + element = element || document.body + str = (str || 'px').trim().toLowerCase() + if(element === window || element === document) { + element = document.body + } + switch(str) { + case '%': //Ambiguous, not sure if we should use width or height + return element.clientHeight / 100.0 + case 'ch': + case 'ex': + return getSizeBrutal(str, element) + case 'em': + return getPropertyInPX(element, 'font-size') + case 'rem': + return getPropertyInPX(document.body, 'font-size') + case 'vw': + return window.innerWidth/100 + case 'vh': + return window.innerHeight/100 + case 'vmin': + return Math.min(window.innerWidth, window.innerHeight) / 100 + case 'vmax': + return Math.max(window.innerWidth, window.innerHeight) / 100 + case 'in': + return PIXELS_PER_INCH + case 'cm': + return PIXELS_PER_INCH / 2.54 + case 'mm': + return PIXELS_PER_INCH / 25.4 + case 'pt': + return PIXELS_PER_INCH / 72 + case 'pc': + return PIXELS_PER_INCH / 6 + } + return 1 +} +},{"parse-unit":462}],987:[function(require,module,exports){ +// https://github.com/topojson/topojson-client Version 2.1.0. Copyright 2016 Mike Bostock. +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.topojson = global.topojson || {}))); +}(this, (function (exports) { 'use strict'; + +var identity = function(x) { + return x; +}; + +var transform = function(topology) { + if ((transform = topology.transform) == null) return identity; + var transform, + x0, + y0, + kx = transform.scale[0], + ky = transform.scale[1], + dx = transform.translate[0], + dy = transform.translate[1]; + return function(point, i) { + if (!i) x0 = y0 = 0; + point[0] = (x0 += point[0]) * kx + dx; + point[1] = (y0 += point[1]) * ky + dy; + return point; + }; +}; + +var bbox = function(topology) { + var bbox = topology.bbox; + + function bboxPoint(p0) { + p1[0] = p0[0], p1[1] = p0[1], t(p1); + if (p1[0] < x0) x0 = p1[0]; + if (p1[0] > x1) x1 = p1[0]; + if (p1[1] < y0) y0 = p1[1]; + if (p1[1] > y1) y1 = p1[1]; + } + + function bboxGeometry(o) { + switch (o.type) { + case "GeometryCollection": o.geometries.forEach(bboxGeometry); break; + case "Point": bboxPoint(o.coordinates); break; + case "MultiPoint": o.coordinates.forEach(bboxPoint); break; + } + } + + if (!bbox) { + var t = transform(topology), p0, p1 = new Array(2), name, + x0 = Infinity, y0 = x0, x1 = -x0, y1 = -x0; + + topology.arcs.forEach(function(arc) { + var i = -1, n = arc.length; + while (++i < n) { + p0 = arc[i], p1[0] = p0[0], p1[1] = p0[1], t(p1, i); + if (p1[0] < x0) x0 = p1[0]; + if (p1[0] > x1) x1 = p1[0]; + if (p1[1] < y0) y0 = p1[1]; + if (p1[1] > y1) y1 = p1[1]; + } + }); + + for (name in topology.objects) { + bboxGeometry(topology.objects[name]); + } + + bbox = topology.bbox = [x0, y0, x1, y1]; + } + + return bbox; +}; + +var reverse = function(array, n) { + var t, j = array.length, i = j - n; + while (i < --j) t = array[i], array[i++] = array[j], array[j] = t; +}; + +var feature = function(topology, o) { + return o.type === "GeometryCollection" + ? {type: "FeatureCollection", features: o.geometries.map(function(o) { return feature$1(topology, o); })} + : feature$1(topology, o); +}; + +function feature$1(topology, o) { + var id = o.id, + bbox = o.bbox, + properties = o.properties == null ? {} : o.properties, + geometry = object(topology, o); + return id == null && bbox == null ? {type: "Feature", properties: properties, geometry: geometry} + : bbox == null ? {type: "Feature", id: id, properties: properties, geometry: geometry} + : {type: "Feature", id: id, bbox: bbox, properties: properties, geometry: geometry}; +} + +function object(topology, o) { + var transformPoint = transform(topology), + arcs = topology.arcs; + + function arc(i, points) { + if (points.length) points.pop(); + for (var a = arcs[i < 0 ? ~i : i], k = 0, n = a.length; k < n; ++k) { + points.push(transformPoint(a[k].slice(), k)); + } + if (i < 0) reverse(points, n); + } + + function point(p) { + return transformPoint(p.slice()); + } + + function line(arcs) { + var points = []; + for (var i = 0, n = arcs.length; i < n; ++i) arc(arcs[i], points); + if (points.length < 2) points.push(points[0].slice()); + return points; + } + + function ring(arcs) { + var points = line(arcs); + while (points.length < 4) points.push(points[0].slice()); + return points; + } + + function polygon(arcs) { + return arcs.map(ring); + } + + function geometry(o) { + var type = o.type, coordinates; + switch (type) { + case "GeometryCollection": return {type: type, geometries: o.geometries.map(geometry)}; + case "Point": coordinates = point(o.coordinates); break; + case "MultiPoint": coordinates = o.coordinates.map(point); break; + case "LineString": coordinates = line(o.arcs); break; + case "MultiLineString": coordinates = o.arcs.map(line); break; + case "Polygon": coordinates = polygon(o.arcs); break; + case "MultiPolygon": coordinates = o.arcs.map(polygon); break; + default: return null; + } + return {type: type, coordinates: coordinates}; + } + + return geometry(o); +} + +var stitch = function(topology, arcs) { + var stitchedArcs = {}, + fragmentByStart = {}, + fragmentByEnd = {}, + fragments = [], + emptyIndex = -1; + + // Stitch empty arcs first, since they may be subsumed by other arcs. + arcs.forEach(function(i, j) { + var arc = topology.arcs[i < 0 ? ~i : i], t; + if (arc.length < 3 && !arc[1][0] && !arc[1][1]) { + t = arcs[++emptyIndex], arcs[emptyIndex] = i, arcs[j] = t; + } + }); + + arcs.forEach(function(i) { + var e = ends(i), + start = e[0], + end = e[1], + f, g; + + if (f = fragmentByEnd[start]) { + delete fragmentByEnd[f.end]; + f.push(i); + f.end = end; + if (g = fragmentByStart[end]) { + delete fragmentByStart[g.start]; + var fg = g === f ? f : f.concat(g); + fragmentByStart[fg.start = f.start] = fragmentByEnd[fg.end = g.end] = fg; + } else { + fragmentByStart[f.start] = fragmentByEnd[f.end] = f; + } + } else if (f = fragmentByStart[end]) { + delete fragmentByStart[f.start]; + f.unshift(i); + f.start = start; + if (g = fragmentByEnd[start]) { + delete fragmentByEnd[g.end]; + var gf = g === f ? f : g.concat(f); + fragmentByStart[gf.start = g.start] = fragmentByEnd[gf.end = f.end] = gf; + } else { + fragmentByStart[f.start] = fragmentByEnd[f.end] = f; + } + } else { + f = [i]; + fragmentByStart[f.start = start] = fragmentByEnd[f.end = end] = f; + } + }); + + function ends(i) { + var arc = topology.arcs[i < 0 ? ~i : i], p0 = arc[0], p1; + if (topology.transform) p1 = [0, 0], arc.forEach(function(dp) { p1[0] += dp[0], p1[1] += dp[1]; }); + else p1 = arc[arc.length - 1]; + return i < 0 ? [p1, p0] : [p0, p1]; + } + + function flush(fragmentByEnd, fragmentByStart) { + for (var k in fragmentByEnd) { + var f = fragmentByEnd[k]; + delete fragmentByStart[f.start]; + delete f.start; + delete f.end; + f.forEach(function(i) { stitchedArcs[i < 0 ? ~i : i] = 1; }); + fragments.push(f); + } + } + + flush(fragmentByEnd, fragmentByStart); + flush(fragmentByStart, fragmentByEnd); + arcs.forEach(function(i) { if (!stitchedArcs[i < 0 ? ~i : i]) fragments.push([i]); }); + + return fragments; +}; + +var mesh = function(topology) { + return object(topology, meshArcs.apply(this, arguments)); +}; + +function meshArcs(topology, object$$1, filter) { + var arcs, i, n; + if (arguments.length > 1) arcs = extractArcs(topology, object$$1, filter); + else for (i = 0, arcs = new Array(n = topology.arcs.length); i < n; ++i) arcs[i] = i; + return {type: "MultiLineString", arcs: stitch(topology, arcs)}; +} + +function extractArcs(topology, object$$1, filter) { + var arcs = [], + geomsByArc = [], + geom; + + function extract0(i) { + var j = i < 0 ? ~i : i; + (geomsByArc[j] || (geomsByArc[j] = [])).push({i: i, g: geom}); + } + + function extract1(arcs) { + arcs.forEach(extract0); + } + + function extract2(arcs) { + arcs.forEach(extract1); + } + + function extract3(arcs) { + arcs.forEach(extract2); + } + + function geometry(o) { + switch (geom = o, o.type) { + case "GeometryCollection": o.geometries.forEach(geometry); break; + case "LineString": extract1(o.arcs); break; + case "MultiLineString": case "Polygon": extract2(o.arcs); break; + case "MultiPolygon": extract3(o.arcs); break; + } + } + + geometry(object$$1); + + geomsByArc.forEach(filter == null + ? function(geoms) { arcs.push(geoms[0].i); } + : function(geoms) { if (filter(geoms[0].g, geoms[geoms.length - 1].g)) arcs.push(geoms[0].i); }); + + return arcs; +} + +function planarRingArea(ring) { + var i = -1, n = ring.length, a, b = ring[n - 1], area = 0; + while (++i < n) a = b, b = ring[i], area += a[0] * b[1] - a[1] * b[0]; + return Math.abs(area); // Note: doubled area! +} + +var merge = function(topology) { + return object(topology, mergeArcs.apply(this, arguments)); +}; + +function mergeArcs(topology, objects) { + var polygonsByArc = {}, + polygons = [], + groups = []; + + objects.forEach(geometry); + + function geometry(o) { + switch (o.type) { + case "GeometryCollection": o.geometries.forEach(geometry); break; + case "Polygon": extract(o.arcs); break; + case "MultiPolygon": o.arcs.forEach(extract); break; + } + } + + function extract(polygon) { + polygon.forEach(function(ring) { + ring.forEach(function(arc) { + (polygonsByArc[arc = arc < 0 ? ~arc : arc] || (polygonsByArc[arc] = [])).push(polygon); + }); + }); + polygons.push(polygon); + } + + function area(ring) { + return planarRingArea(object(topology, {type: "Polygon", arcs: [ring]}).coordinates[0]); + } + + polygons.forEach(function(polygon) { + if (!polygon._) { + var group = [], + neighbors = [polygon]; + polygon._ = 1; + groups.push(group); + while (polygon = neighbors.pop()) { + group.push(polygon); + polygon.forEach(function(ring) { + ring.forEach(function(arc) { + polygonsByArc[arc < 0 ? ~arc : arc].forEach(function(polygon) { + if (!polygon._) { + polygon._ = 1; + neighbors.push(polygon); + } + }); + }); + }); + } + } + }); + + polygons.forEach(function(polygon) { + delete polygon._; + }); + + return { + type: "MultiPolygon", + arcs: groups.map(function(polygons) { + var arcs = [], n; + + // Extract the exterior (unique) arcs. + polygons.forEach(function(polygon) { + polygon.forEach(function(ring) { + ring.forEach(function(arc) { + if (polygonsByArc[arc < 0 ? ~arc : arc].length < 2) { + arcs.push(arc); + } + }); + }); + }); + + // Stitch the arcs into one or more rings. + arcs = stitch(topology, arcs); + + // If more than one ring is returned, + // at most one of these rings can be the exterior; + // choose the one with the greatest absolute area. + if ((n = arcs.length) > 1) { + for (var i = 1, k = area(arcs[0]), ki, t; i < n; ++i) { + if ((ki = area(arcs[i])) > k) { + t = arcs[0], arcs[0] = arcs[i], arcs[i] = t, k = ki; + } + } + } + + return arcs; + }) + }; +} + +var bisect = function(a, x) { + var lo = 0, hi = a.length; + while (lo < hi) { + var mid = lo + hi >>> 1; + if (a[mid] < x) lo = mid + 1; + else hi = mid; + } + return lo; +}; + +var neighbors = function(objects) { + var indexesByArc = {}, // arc index -> array of object indexes + neighbors = objects.map(function() { return []; }); + + function line(arcs, i) { + arcs.forEach(function(a) { + if (a < 0) a = ~a; + var o = indexesByArc[a]; + if (o) o.push(i); + else indexesByArc[a] = [i]; + }); + } + + function polygon(arcs, i) { + arcs.forEach(function(arc) { line(arc, i); }); + } + + function geometry(o, i) { + if (o.type === "GeometryCollection") o.geometries.forEach(function(o) { geometry(o, i); }); + else if (o.type in geometryType) geometryType[o.type](o.arcs, i); + } + + var geometryType = { + LineString: line, + MultiLineString: polygon, + Polygon: polygon, + MultiPolygon: function(arcs, i) { arcs.forEach(function(arc) { polygon(arc, i); }); } + }; + + objects.forEach(geometry); + + for (var i in indexesByArc) { + for (var indexes = indexesByArc[i], m = indexes.length, j = 0; j < m; ++j) { + for (var k = j + 1; k < m; ++k) { + var ij = indexes[j], ik = indexes[k], n; + if ((n = neighbors[ij])[i = bisect(n, ik)] !== ik) n.splice(i, 0, ik); + if ((n = neighbors[ik])[i = bisect(n, ij)] !== ij) n.splice(i, 0, ij); + } + } + } + + return neighbors; +}; + +var quantize = function(topology, n) { + if (!((n = Math.floor(n)) >= 2)) throw new Error("n must be ≥2"); + if (topology.transform) throw new Error("already quantized"); + var bb = bbox(topology), name, + dx = bb[0], kx = (bb[2] - dx) / (n - 1) || 1, + dy = bb[1], ky = (bb[3] - dy) / (n - 1) || 1; + + function quantizePoint(p) { + p[0] = Math.round((p[0] - dx) / kx); + p[1] = Math.round((p[1] - dy) / ky); + } + + function quantizeGeometry(o) { + switch (o.type) { + case "GeometryCollection": o.geometries.forEach(quantizeGeometry); break; + case "Point": quantizePoint(o.coordinates); break; + case "MultiPoint": o.coordinates.forEach(quantizePoint); break; + } + } + + topology.arcs.forEach(function(arc) { + var i = 1, + j = 1, + n = arc.length, + pi = arc[0], + x0 = pi[0] = Math.round((pi[0] - dx) / kx), + y0 = pi[1] = Math.round((pi[1] - dy) / ky), + pj, + x1, + y1; + + for (; i < n; ++i) { + pi = arc[i]; + x1 = Math.round((pi[0] - dx) / kx); + y1 = Math.round((pi[1] - dy) / ky); + if (x1 !== x0 || y1 !== y0) { + pj = arc[j++]; + pj[0] = x1 - x0, x0 = x1; + pj[1] = y1 - y0, y0 = y1; + } + } + + if (j < 2) { + pj = arc[j++]; + pj[0] = 0; + pj[1] = 0; + } + + arc.length = j; + }); + + for (name in topology.objects) { + quantizeGeometry(topology.objects[name]); + } + + topology.transform = { + scale: [kx, ky], + translate: [dx, dy] + }; + + return topology; +}; + +var untransform = function(topology) { + if ((transform = topology.transform) == null) return identity; + var transform, + x0, + y0, + kx = transform.scale[0], + ky = transform.scale[1], + dx = transform.translate[0], + dy = transform.translate[1]; + return function(point, i) { + if (!i) x0 = y0 = 0; + var x1 = Math.round((point[0] - dx) / kx), + y1 = Math.round((point[1] - dy) / ky); + point[0] = x1 - x0, x0 = x1; + point[1] = y1 - y0, y0 = y1; + return point; + }; +}; + +exports.bbox = bbox; +exports.feature = feature; +exports.mesh = mesh; +exports.meshArcs = meshArcs; +exports.merge = merge; +exports.mergeArcs = mergeArcs; +exports.neighbors = neighbors; +exports.quantize = quantize; +exports.transform = transform; +exports.untransform = untransform; + +Object.defineProperty(exports, '__esModule', { value: true }); + +}))); + +},{}],988:[function(require,module,exports){ +"use strict" + +module.exports = triangulateCube + +var perm = require("permutation-rank") +var sgn = require("permutation-parity") +var gamma = require("gamma") + +function triangulateCube(dimension) { + if(dimension < 0) { + return [ ] + } + if(dimension === 0) { + return [ [0] ] + } + var dfactorial = Math.round(gamma(dimension+1))|0 + var result = [] + for(var i=0; i Math.max(vy, vz)) { + u[2] = 1 + } else if(vy > Math.max(vx, vz)) { + u[0] = 1 + } else { + u[1] = 1 + } + + var vv = 0 + var uv = 0 + for(var i=0; i<3; ++i ) { + vv += v[i] * v[i] + uv += u[i] * v[i] + } + for(var i=0; i<3; ++i) { + u[i] -= (uv / vv) * v[i] + } + normalize3(u, u) + return u +} + +function TurntableController(zoomMin, zoomMax, center, up, right, radius, theta, phi) { + this.center = filterVector(center) + this.up = filterVector(up) + this.right = filterVector(right) + this.radius = filterVector([radius]) + this.angle = filterVector([theta, phi]) + this.angle.bounds = [[-Infinity,-Math.PI/2], [Infinity,Math.PI/2]] + this.setDistanceLimits(zoomMin, zoomMax) + + this.computedCenter = this.center.curve(0) + this.computedUp = this.up.curve(0) + this.computedRight = this.right.curve(0) + this.computedRadius = this.radius.curve(0) + this.computedAngle = this.angle.curve(0) + this.computedToward = [0,0,0] + this.computedEye = [0,0,0] + this.computedMatrix = new Array(16) + for(var i=0; i<16; ++i) { + this.computedMatrix[i] = 0.5 + } + + this.recalcMatrix(0) +} + +var proto = TurntableController.prototype + +proto.setDistanceLimits = function(minDist, maxDist) { + if(minDist > 0) { + minDist = Math.log(minDist) + } else { + minDist = -Infinity + } + if(maxDist > 0) { + maxDist = Math.log(maxDist) + } else { + maxDist = Infinity + } + maxDist = Math.max(maxDist, minDist) + this.radius.bounds[0][0] = minDist + this.radius.bounds[1][0] = maxDist +} + +proto.getDistanceLimits = function(out) { + var bounds = this.radius.bounds[0] + if(out) { + out[0] = Math.exp(bounds[0][0]) + out[1] = Math.exp(bounds[1][0]) + return out + } + return [ Math.exp(bounds[0][0]), Math.exp(bounds[1][0]) ] +} + +proto.recalcMatrix = function(t) { + //Recompute curves + this.center.curve(t) + this.up.curve(t) + this.right.curve(t) + this.radius.curve(t) + this.angle.curve(t) + + //Compute frame for camera matrix + var up = this.computedUp + var right = this.computedRight + var uu = 0.0 + var ur = 0.0 + for(var i=0; i<3; ++i) { + ur += up[i] * right[i] + uu += up[i] * up[i] + } + var ul = Math.sqrt(uu) + var rr = 0.0 + for(var i=0; i<3; ++i) { + right[i] -= up[i] * ur / uu + rr += right[i] * right[i] + up[i] /= ul + } + var rl = Math.sqrt(rr) + for(var i=0; i<3; ++i) { + right[i] /= rl + } + + //Compute toward vector + var toward = this.computedToward + cross(toward, up, right) + normalize3(toward, toward) + + //Compute angular parameters + var radius = Math.exp(this.computedRadius[0]) + var theta = this.computedAngle[0] + var phi = this.computedAngle[1] + + var ctheta = Math.cos(theta) + var stheta = Math.sin(theta) + var cphi = Math.cos(phi) + var sphi = Math.sin(phi) + + var center = this.computedCenter + + var wx = ctheta * cphi + var wy = stheta * cphi + var wz = sphi + + var sx = -ctheta * sphi + var sy = -stheta * sphi + var sz = cphi + + var eye = this.computedEye + var mat = this.computedMatrix + for(var i=0; i<3; ++i) { + var x = wx * right[i] + wy * toward[i] + wz * up[i] + mat[4*i+1] = sx * right[i] + sy * toward[i] + sz * up[i] + mat[4*i+2] = x + mat[4*i+3] = 0.0 + } + + var ax = mat[1] + var ay = mat[5] + var az = mat[9] + var bx = mat[2] + var by = mat[6] + var bz = mat[10] + var cx = ay * bz - az * by + var cy = az * bx - ax * bz + var cz = ax * by - ay * bx + var cl = len3(cx, cy, cz) + cx /= cl + cy /= cl + cz /= cl + mat[0] = cx + mat[4] = cy + mat[8] = cz + + for(var i=0; i<3; ++i) { + eye[i] = center[i] + mat[2+4*i]*radius + } + + for(var i=0; i<3; ++i) { + var rr = 0.0 + for(var j=0; j<3; ++j) { + rr += mat[i+4*j] * eye[j] + } + mat[12+i] = -rr + } + mat[15] = 1.0 +} + +proto.getMatrix = function(t, result) { + this.recalcMatrix(t) + var mat = this.computedMatrix + if(result) { + for(var i=0; i<16; ++i) { + result[i] = mat[i] + } + return result + } + return mat +} + +var zAxis = [0,0,0] +proto.rotate = function(t, dtheta, dphi, droll) { + this.angle.move(t, dtheta, dphi) + if(droll) { + this.recalcMatrix(t) + + var mat = this.computedMatrix + zAxis[0] = mat[2] + zAxis[1] = mat[6] + zAxis[2] = mat[10] + + var up = this.computedUp + var right = this.computedRight + var toward = this.computedToward + + for(var i=0; i<3; ++i) { + mat[4*i] = up[i] + mat[4*i+1] = right[i] + mat[4*i+2] = toward[i] + } + rotateM(mat, mat, droll, zAxis) + for(var i=0; i<3; ++i) { + up[i] = mat[4*i] + right[i] = mat[4*i+1] + } + + this.up.set(t, up[0], up[1], up[2]) + this.right.set(t, right[0], right[1], right[2]) + } +} + +proto.pan = function(t, dx, dy, dz) { + dx = dx || 0.0 + dy = dy || 0.0 + dz = dz || 0.0 + + this.recalcMatrix(t) + var mat = this.computedMatrix + + var dist = Math.exp(this.computedRadius[0]) + + var ux = mat[1] + var uy = mat[5] + var uz = mat[9] + var ul = len3(ux, uy, uz) + ux /= ul + uy /= ul + uz /= ul + + var rx = mat[0] + var ry = mat[4] + var rz = mat[8] + var ru = rx * ux + ry * uy + rz * uz + rx -= ux * ru + ry -= uy * ru + rz -= uz * ru + var rl = len3(rx, ry, rz) + rx /= rl + ry /= rl + rz /= rl + + var vx = rx * dx + ux * dy + var vy = ry * dx + uy * dy + var vz = rz * dx + uz * dy + this.center.move(t, vx, vy, vz) + + //Update z-component of radius + var radius = Math.exp(this.computedRadius[0]) + radius = Math.max(1e-4, radius + dz) + this.radius.set(t, Math.log(radius)) +} + +proto.translate = function(t, dx, dy, dz) { + this.center.move(t, + dx||0.0, + dy||0.0, + dz||0.0) +} + +//Recenters the coordinate axes +proto.setMatrix = function(t, mat, axes, noSnap) { + + //Get the axes for tare + var ushift = 1 + if(typeof axes === 'number') { + ushift = (axes)|0 + } + if(ushift < 0 || ushift > 3) { + ushift = 1 + } + var vshift = (ushift + 2) % 3 + var fshift = (ushift + 1) % 3 + + //Recompute state for new t value + if(!mat) { + this.recalcMatrix(t) + mat = this.computedMatrix + } + + //Get right and up vectors + var ux = mat[ushift] + var uy = mat[ushift+4] + var uz = mat[ushift+8] + if(!noSnap) { + var ul = len3(ux, uy, uz) + ux /= ul + uy /= ul + uz /= ul + } else { + var ax = Math.abs(ux) + var ay = Math.abs(uy) + var az = Math.abs(uz) + var am = Math.max(ax,ay,az) + if(ax === am) { + ux = (ux < 0) ? -1 : 1 + uy = uz = 0 + } else if(az === am) { + uz = (uz < 0) ? -1 : 1 + ux = uy = 0 + } else { + uy = (uy < 0) ? -1 : 1 + ux = uz = 0 + } + } + + var rx = mat[vshift] + var ry = mat[vshift+4] + var rz = mat[vshift+8] + var ru = rx * ux + ry * uy + rz * uz + rx -= ux * ru + ry -= uy * ru + rz -= uz * ru + var rl = len3(rx, ry, rz) + rx /= rl + ry /= rl + rz /= rl + + var fx = uy * rz - uz * ry + var fy = uz * rx - ux * rz + var fz = ux * ry - uy * rx + var fl = len3(fx, fy, fz) + fx /= fl + fy /= fl + fz /= fl + + this.center.jump(t, ex, ey, ez) + this.radius.idle(t) + this.up.jump(t, ux, uy, uz) + this.right.jump(t, rx, ry, rz) + + var phi, theta + if(ushift === 2) { + var cx = mat[1] + var cy = mat[5] + var cz = mat[9] + var cr = cx * rx + cy * ry + cz * rz + var cf = cx * fx + cy * fy + cz * fz + if(tu < 0) { + phi = -Math.PI/2 + } else { + phi = Math.PI/2 + } + theta = Math.atan2(cf, cr) + } else { + var tx = mat[2] + var ty = mat[6] + var tz = mat[10] + var tu = tx * ux + ty * uy + tz * uz + var tr = tx * rx + ty * ry + tz * rz + var tf = tx * fx + ty * fy + tz * fz + + phi = Math.asin(clamp1(tu)) + theta = Math.atan2(tf, tr) + } + + this.angle.jump(t, theta, phi) + + this.recalcMatrix(t) + var dx = mat[2] + var dy = mat[6] + var dz = mat[10] + + var imat = this.computedMatrix + invert44(imat, mat) + var w = imat[15] + var ex = imat[12] / w + var ey = imat[13] / w + var ez = imat[14] / w + + var gs = Math.exp(this.computedRadius[0]) + this.center.jump(t, ex-dx*gs, ey-dy*gs, ez-dz*gs) +} + +proto.lastT = function() { + return Math.max( + this.center.lastT(), + this.up.lastT(), + this.right.lastT(), + this.radius.lastT(), + this.angle.lastT()) +} + +proto.idle = function(t) { + this.center.idle(t) + this.up.idle(t) + this.right.idle(t) + this.radius.idle(t) + this.angle.idle(t) +} + +proto.flush = function(t) { + this.center.flush(t) + this.up.flush(t) + this.right.flush(t) + this.radius.flush(t) + this.angle.flush(t) +} + +proto.setDistance = function(t, d) { + if(d > 0) { + this.radius.set(t, Math.log(d)) + } +} + +proto.lookAt = function(t, eye, center, up) { + this.recalcMatrix(t) + + eye = eye || this.computedEye + center = center || this.computedCenter + up = up || this.computedUp + + var ux = up[0] + var uy = up[1] + var uz = up[2] + var ul = len3(ux, uy, uz) + if(ul < 1e-6) { + return + } + ux /= ul + uy /= ul + uz /= ul + + var tx = eye[0] - center[0] + var ty = eye[1] - center[1] + var tz = eye[2] - center[2] + var tl = len3(tx, ty, tz) + if(tl < 1e-6) { + return + } + tx /= tl + ty /= tl + tz /= tl + + var right = this.computedRight + var rx = right[0] + var ry = right[1] + var rz = right[2] + var ru = ux*rx + uy*ry + uz*rz + rx -= ru * ux + ry -= ru * uy + rz -= ru * uz + var rl = len3(rx, ry, rz) + + if(rl < 0.01) { + rx = uy * tz - uz * ty + ry = uz * tx - ux * tz + rz = ux * ty - uy * tx + rl = len3(rx, ry, rz) + if(rl < 1e-6) { + return + } + } + rx /= rl + ry /= rl + rz /= rl + + this.up.set(t, ux, uy, uz) + this.right.set(t, rx, ry, rz) + this.center.set(t, center[0], center[1], center[2]) + this.radius.set(t, Math.log(tl)) + + var fx = uy * rz - uz * ry + var fy = uz * rx - ux * rz + var fz = ux * ry - uy * rx + var fl = len3(fx, fy, fz) + fx /= fl + fy /= fl + fz /= fl + + var tu = ux*tx + uy*ty + uz*tz + var tr = rx*tx + ry*ty + rz*tz + var tf = fx*tx + fy*ty + fz*tz + + var phi = Math.asin(clamp1(tu)) + var theta = Math.atan2(tf, tr) + + var angleState = this.angle._state + var lastTheta = angleState[angleState.length-1] + var lastPhi = angleState[angleState.length-2] + lastTheta = lastTheta % (2.0 * Math.PI) + var dp = Math.abs(lastTheta + 2.0 * Math.PI - theta) + var d0 = Math.abs(lastTheta - theta) + var dn = Math.abs(lastTheta - 2.0 * Math.PI - theta) + if(dp < d0) { + lastTheta += 2.0 * Math.PI + } + if(dn < d0) { + lastTheta -= 2.0 * Math.PI + } + + this.angle.jump(this.angle.lastT(), lastTheta, lastPhi) + this.angle.set(t, theta, phi) +} + +function createTurntableController(options) { + options = options || {} + + var center = options.center || [0,0,0] + var up = options.up || [0,1,0] + var right = options.right || findOrthoPair(up) + var radius = options.radius || 1.0 + var theta = options.theta || 0.0 + var phi = options.phi || 0.0 + + center = [].slice.call(center, 0, 3) + + up = [].slice.call(up, 0, 3) + normalize3(up, up) + + right = [].slice.call(right, 0, 3) + normalize3(right, right) + + if('eye' in options) { + var eye = options.eye + var toward = [ + eye[0]-center[0], + eye[1]-center[1], + eye[2]-center[2] + ] + cross(right, toward, up) + if(len3(right[0], right[1], right[2]) < 1e-6) { + right = findOrthoPair(up) + } else { + normalize3(right, right) + } + + radius = len3(toward[0], toward[1], toward[2]) + + var ut = dot3(up, toward) / radius + var rt = dot3(right, toward) / radius + phi = Math.acos(ut) + theta = Math.acos(rt) + } + + //Use logarithmic coordinates for radius + radius = Math.log(radius) + + //Return the controller + return new TurntableController( + options.zoomMin, + options.zoomMax, + center, + up, + right, + radius, + theta, + phi) +} +},{"filtered-vector":99,"gl-mat4/invert":156,"gl-mat4/rotate":160,"gl-vec3/cross":244,"gl-vec3/dot":245,"gl-vec3/normalize":248}],990:[function(require,module,exports){ +"use strict" + +module.exports = twoProduct + +var SPLITTER = +(Math.pow(2, 27) + 1.0) + +function twoProduct(a, b, result) { + var x = a * b + + var c = SPLITTER * a + var abig = c - a + var ahi = c - abig + var alo = a - ahi + + var d = SPLITTER * b + var bbig = d - b + var bhi = d - bbig + var blo = b - bhi + + var err1 = x - (ahi * bhi) + var err2 = err1 - (alo * bhi) + var err3 = err2 - (ahi * blo) + + var y = alo * blo - err3 + + if(result) { + result[0] = y + result[1] = x + return result + } + + return [ y, x ] +} +},{}],991:[function(require,module,exports){ +"use strict" + +module.exports = fastTwoSum + +function fastTwoSum(a, b, result) { + var x = a + b + var bv = x - a + var av = x - bv + var br = b - bv + var ar = a - av + if(result) { + result[0] = ar + br + result[1] = x + return result + } + return [ar+br, x] +} +},{}],992:[function(require,module,exports){ +(function (global,Buffer){ +'use strict' + +var bits = require('bit-twiddle') +var dup = require('dup') + +//Legacy pool support +if(!global.__TYPEDARRAY_POOL) { + global.__TYPEDARRAY_POOL = { + UINT8 : dup([32, 0]) + , UINT16 : dup([32, 0]) + , UINT32 : dup([32, 0]) + , INT8 : dup([32, 0]) + , INT16 : dup([32, 0]) + , INT32 : dup([32, 0]) + , FLOAT : dup([32, 0]) + , DOUBLE : dup([32, 0]) + , DATA : dup([32, 0]) + , UINT8C : dup([32, 0]) + , BUFFER : dup([32, 0]) + } +} + +var hasUint8C = (typeof Uint8ClampedArray) !== 'undefined' +var POOL = global.__TYPEDARRAY_POOL + +//Upgrade pool +if(!POOL.UINT8C) { + POOL.UINT8C = dup([32, 0]) +} +if(!POOL.BUFFER) { + POOL.BUFFER = dup([32, 0]) +} + +//New technique: Only allocate from ArrayBufferView and Buffer +var DATA = POOL.DATA + , BUFFER = POOL.BUFFER + +exports.free = function free(array) { + if(Buffer.isBuffer(array)) { + BUFFER[bits.log2(array.length)].push(array) + } else { + if(Object.prototype.toString.call(array) !== '[object ArrayBuffer]') { + array = array.buffer + } + if(!array) { + return + } + var n = array.length || array.byteLength + var log_n = bits.log2(n)|0 + DATA[log_n].push(array) + } +} + +function freeArrayBuffer(buffer) { + if(!buffer) { + return + } + var n = buffer.length || buffer.byteLength + var log_n = bits.log2(n) + DATA[log_n].push(buffer) +} + +function freeTypedArray(array) { + freeArrayBuffer(array.buffer) +} + +exports.freeUint8 = +exports.freeUint16 = +exports.freeUint32 = +exports.freeInt8 = +exports.freeInt16 = +exports.freeInt32 = +exports.freeFloat32 = +exports.freeFloat = +exports.freeFloat64 = +exports.freeDouble = +exports.freeUint8Clamped = +exports.freeDataView = freeTypedArray + +exports.freeArrayBuffer = freeArrayBuffer + +exports.freeBuffer = function freeBuffer(array) { + BUFFER[bits.log2(array.length)].push(array) +} + +exports.malloc = function malloc(n, dtype) { + if(dtype === undefined || dtype === 'arraybuffer') { + return mallocArrayBuffer(n) + } else { + switch(dtype) { + case 'uint8': + return mallocUint8(n) + case 'uint16': + return mallocUint16(n) + case 'uint32': + return mallocUint32(n) + case 'int8': + return mallocInt8(n) + case 'int16': + return mallocInt16(n) + case 'int32': + return mallocInt32(n) + case 'float': + case 'float32': + return mallocFloat(n) + case 'double': + case 'float64': + return mallocDouble(n) + case 'uint8_clamped': + return mallocUint8Clamped(n) + case 'buffer': + return mallocBuffer(n) + case 'data': + case 'dataview': + return mallocDataView(n) + + default: + return null + } + } + return null +} + +function mallocArrayBuffer(n) { + var n = bits.nextPow2(n) + var log_n = bits.log2(n) + var d = DATA[log_n] + if(d.length > 0) { + return d.pop() + } + return new ArrayBuffer(n) +} +exports.mallocArrayBuffer = mallocArrayBuffer + +function mallocUint8(n) { + return new Uint8Array(mallocArrayBuffer(n), 0, n) +} +exports.mallocUint8 = mallocUint8 + +function mallocUint16(n) { + return new Uint16Array(mallocArrayBuffer(2*n), 0, n) +} +exports.mallocUint16 = mallocUint16 + +function mallocUint32(n) { + return new Uint32Array(mallocArrayBuffer(4*n), 0, n) +} +exports.mallocUint32 = mallocUint32 + +function mallocInt8(n) { + return new Int8Array(mallocArrayBuffer(n), 0, n) +} +exports.mallocInt8 = mallocInt8 + +function mallocInt16(n) { + return new Int16Array(mallocArrayBuffer(2*n), 0, n) +} +exports.mallocInt16 = mallocInt16 + +function mallocInt32(n) { + return new Int32Array(mallocArrayBuffer(4*n), 0, n) +} +exports.mallocInt32 = mallocInt32 + +function mallocFloat(n) { + return new Float32Array(mallocArrayBuffer(4*n), 0, n) +} +exports.mallocFloat32 = exports.mallocFloat = mallocFloat + +function mallocDouble(n) { + return new Float64Array(mallocArrayBuffer(8*n), 0, n) +} +exports.mallocFloat64 = exports.mallocDouble = mallocDouble + +function mallocUint8Clamped(n) { + if(hasUint8C) { + return new Uint8ClampedArray(mallocArrayBuffer(n), 0, n) + } else { + return mallocUint8(n) + } +} +exports.mallocUint8Clamped = mallocUint8Clamped + +function mallocDataView(n) { + return new DataView(mallocArrayBuffer(n), 0, n) +} +exports.mallocDataView = mallocDataView + +function mallocBuffer(n) { + n = bits.nextPow2(n) + var log_n = bits.log2(n) + var cache = BUFFER[log_n] + if(cache.length > 0) { + return cache.pop() + } + return new Buffer(n) +} +exports.mallocBuffer = mallocBuffer + +exports.clearCache = function clearCache() { + for(var i=0; i<32; ++i) { + POOL.UINT8[i].length = 0 + POOL.UINT16[i].length = 0 + POOL.UINT32[i].length = 0 + POOL.INT8[i].length = 0 + POOL.INT16[i].length = 0 + POOL.INT32[i].length = 0 + POOL.FLOAT[i].length = 0 + POOL.DOUBLE[i].length = 0 + POOL.UINT8C[i].length = 0 + DATA[i].length = 0 + BUFFER[i].length = 0 + } +} +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) +},{"bit-twiddle":33,"buffer":46,"dup":90}],993:[function(require,module,exports){ +"use strict"; "use restrict"; + +module.exports = UnionFind; + +function UnionFind(count) { + this.roots = new Array(count); + this.ranks = new Array(count); + + for(var i=0; i t1) return t1; + + while (t0 < t1) { + + x2 = this.sampleCurveX(t2); + if (Math.abs(x2 - x) < epsilon) return t2; + + if (x > x2) { + t0 = t2; + } else { + t1 = t2; + } + + t2 = (t1 - t0) * 0.5 + t0; + } + + // Failure. + return t2; +}; + +UnitBezier.prototype.solve = function(x, epsilon) { + return this.sampleCurveY(this.solveCurveX(x, epsilon)); +}; + +},{}],996:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +var punycode = require('punycode'); +var util = require('./util'); + +exports.parse = urlParse; +exports.resolve = urlResolve; +exports.resolveObject = urlResolveObject; +exports.format = urlFormat; + +exports.Url = Url; + +function Url() { + this.protocol = null; + this.slashes = null; + this.auth = null; + this.host = null; + this.port = null; + this.hostname = null; + this.hash = null; + this.search = null; + this.query = null; + this.pathname = null; + this.path = null; + this.href = null; +} + +// Reference: RFC 3986, RFC 1808, RFC 2396 + +// define these here so at least they only have to be +// compiled once on the first module load. +var protocolPattern = /^([a-z0-9.+-]+:)/i, + portPattern = /:[0-9]*$/, + + // Special case for a simple path URL + simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/, + + // RFC 2396: characters reserved for delimiting URLs. + // We actually just auto-escape these. + delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'], + + // RFC 2396: characters not allowed for various reasons. + unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims), + + // Allowed by RFCs, but cause of XSS attacks. Always escape these. + autoEscape = ['\''].concat(unwise), + // Characters that are never ever allowed in a hostname. + // Note that any invalid chars are also handled, but these + // are the ones that are *expected* to be seen, so we fast-path + // them. + nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape), + hostEndingChars = ['/', '?', '#'], + hostnameMaxLen = 255, + hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/, + hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/, + // protocols that can allow "unsafe" and "unwise" chars. + unsafeProtocol = { + 'javascript': true, + 'javascript:': true + }, + // protocols that never have a hostname. + hostlessProtocol = { + 'javascript': true, + 'javascript:': true + }, + // protocols that always contain a // bit. + slashedProtocol = { + 'http': true, + 'https': true, + 'ftp': true, + 'gopher': true, + 'file': true, + 'http:': true, + 'https:': true, + 'ftp:': true, + 'gopher:': true, + 'file:': true + }, + querystring = require('querystring'); + +function urlParse(url, parseQueryString, slashesDenoteHost) { + if (url && util.isObject(url) && url instanceof Url) return url; + + var u = new Url; + u.parse(url, parseQueryString, slashesDenoteHost); + return u; +} + +Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { + if (!util.isString(url)) { + throw new TypeError("Parameter 'url' must be a string, not " + typeof url); + } + + // Copy chrome, IE, opera backslash-handling behavior. + // Back slashes before the query string get converted to forward slashes + // See: https://code.google.com/p/chromium/issues/detail?id=25916 + var queryIndex = url.indexOf('?'), + splitter = + (queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#', + uSplit = url.split(splitter), + slashRegex = /\\/g; + uSplit[0] = uSplit[0].replace(slashRegex, '/'); + url = uSplit.join(splitter); + + var rest = url; + + // trim before proceeding. + // This is to support parse stuff like " http://foo.com \n" + rest = rest.trim(); + + if (!slashesDenoteHost && url.split('#').length === 1) { + // Try fast path regexp + var simplePath = simplePathPattern.exec(rest); + if (simplePath) { + this.path = rest; + this.href = rest; + this.pathname = simplePath[1]; + if (simplePath[2]) { + this.search = simplePath[2]; + if (parseQueryString) { + this.query = querystring.parse(this.search.substr(1)); + } else { + this.query = this.search.substr(1); + } + } else if (parseQueryString) { + this.search = ''; + this.query = {}; + } + return this; + } + } + + var proto = protocolPattern.exec(rest); + if (proto) { + proto = proto[0]; + var lowerProto = proto.toLowerCase(); + this.protocol = lowerProto; + rest = rest.substr(proto.length); + } + + // figure out if it's got a host + // user@server is *always* interpreted as a hostname, and url + // resolution will treat //foo/bar as host=foo,path=bar because that's + // how the browser resolves relative URLs. + if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) { + var slashes = rest.substr(0, 2) === '//'; + if (slashes && !(proto && hostlessProtocol[proto])) { + rest = rest.substr(2); + this.slashes = true; + } + } + + if (!hostlessProtocol[proto] && + (slashes || (proto && !slashedProtocol[proto]))) { + + // there's a hostname. + // the first instance of /, ?, ;, or # ends the host. + // + // If there is an @ in the hostname, then non-host chars *are* allowed + // to the left of the last @ sign, unless some host-ending character + // comes *before* the @-sign. + // URLs are obnoxious. + // + // ex: + // http://a@b@c/ => user:a@b host:c + // http://a@b?@c => user:a host:c path:/?@c + + // v0.12 TODO(isaacs): This is not quite how Chrome does things. + // Review our test case against browsers more comprehensively. + + // find the first instance of any hostEndingChars + var hostEnd = -1; + for (var i = 0; i < hostEndingChars.length; i++) { + var hec = rest.indexOf(hostEndingChars[i]); + if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) + hostEnd = hec; + } + + // at this point, either we have an explicit point where the + // auth portion cannot go past, or the last @ char is the decider. + var auth, atSign; + if (hostEnd === -1) { + // atSign can be anywhere. + atSign = rest.lastIndexOf('@'); + } else { + // atSign must be in auth portion. + // http://a@b/c@d => host:b auth:a path:/c@d + atSign = rest.lastIndexOf('@', hostEnd); + } + + // Now we have a portion which is definitely the auth. + // Pull that off. + if (atSign !== -1) { + auth = rest.slice(0, atSign); + rest = rest.slice(atSign + 1); + this.auth = decodeURIComponent(auth); + } + + // the host is the remaining to the left of the first non-host char + hostEnd = -1; + for (var i = 0; i < nonHostChars.length; i++) { + var hec = rest.indexOf(nonHostChars[i]); + if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) + hostEnd = hec; + } + // if we still have not hit it, then the entire thing is a host. + if (hostEnd === -1) + hostEnd = rest.length; + + this.host = rest.slice(0, hostEnd); + rest = rest.slice(hostEnd); + + // pull out port. + this.parseHost(); + + // we've indicated that there is a hostname, + // so even if it's empty, it has to be present. + this.hostname = this.hostname || ''; + + // if hostname begins with [ and ends with ] + // assume that it's an IPv6 address. + var ipv6Hostname = this.hostname[0] === '[' && + this.hostname[this.hostname.length - 1] === ']'; + + // validate a little. + if (!ipv6Hostname) { + var hostparts = this.hostname.split(/\./); + for (var i = 0, l = hostparts.length; i < l; i++) { + var part = hostparts[i]; + if (!part) continue; + if (!part.match(hostnamePartPattern)) { + var newpart = ''; + for (var j = 0, k = part.length; j < k; j++) { + if (part.charCodeAt(j) > 127) { + // we replace non-ASCII char with a temporary placeholder + // we need this to make sure size of hostname is not + // broken by replacing non-ASCII by nothing + newpart += 'x'; + } else { + newpart += part[j]; + } + } + // we test again with ASCII char only + if (!newpart.match(hostnamePartPattern)) { + var validParts = hostparts.slice(0, i); + var notHost = hostparts.slice(i + 1); + var bit = part.match(hostnamePartStart); + if (bit) { + validParts.push(bit[1]); + notHost.unshift(bit[2]); + } + if (notHost.length) { + rest = '/' + notHost.join('.') + rest; + } + this.hostname = validParts.join('.'); + break; + } + } + } + } + + if (this.hostname.length > hostnameMaxLen) { + this.hostname = ''; + } else { + // hostnames are always lower case. + this.hostname = this.hostname.toLowerCase(); + } + + if (!ipv6Hostname) { + // IDNA Support: Returns a punycoded representation of "domain". + // It only converts parts of the domain name that + // have non-ASCII characters, i.e. it doesn't matter if + // you call it with a domain that already is ASCII-only. + this.hostname = punycode.toASCII(this.hostname); + } + + var p = this.port ? ':' + this.port : ''; + var h = this.hostname || ''; + this.host = h + p; + this.href += this.host; + + // strip [ and ] from the hostname + // the host field still retains them, though + if (ipv6Hostname) { + this.hostname = this.hostname.substr(1, this.hostname.length - 2); + if (rest[0] !== '/') { + rest = '/' + rest; + } + } + } + + // now rest is set to the post-host stuff. + // chop off any delim chars. + if (!unsafeProtocol[lowerProto]) { + + // First, make 100% sure that any "autoEscape" chars get + // escaped, even if encodeURIComponent doesn't think they + // need to be. + for (var i = 0, l = autoEscape.length; i < l; i++) { + var ae = autoEscape[i]; + if (rest.indexOf(ae) === -1) + continue; + var esc = encodeURIComponent(ae); + if (esc === ae) { + esc = escape(ae); + } + rest = rest.split(ae).join(esc); + } + } + + + // chop off from the tail first. + var hash = rest.indexOf('#'); + if (hash !== -1) { + // got a fragment string. + this.hash = rest.substr(hash); + rest = rest.slice(0, hash); + } + var qm = rest.indexOf('?'); + if (qm !== -1) { + this.search = rest.substr(qm); + this.query = rest.substr(qm + 1); + if (parseQueryString) { + this.query = querystring.parse(this.query); + } + rest = rest.slice(0, qm); + } else if (parseQueryString) { + // no query string, but parseQueryString still requested + this.search = ''; + this.query = {}; + } + if (rest) this.pathname = rest; + if (slashedProtocol[lowerProto] && + this.hostname && !this.pathname) { + this.pathname = '/'; + } + + //to support http.request + if (this.pathname || this.search) { + var p = this.pathname || ''; + var s = this.search || ''; + this.path = p + s; + } + + // finally, reconstruct the href based on what has been validated. + this.href = this.format(); + return this; +}; + +// format a parsed object into a url string +function urlFormat(obj) { + // ensure it's an object, and not a string url. + // If it's an obj, this is a no-op. + // this way, you can call url_format() on strings + // to clean up potentially wonky urls. + if (util.isString(obj)) obj = urlParse(obj); + if (!(obj instanceof Url)) return Url.prototype.format.call(obj); + return obj.format(); +} + +Url.prototype.format = function() { + var auth = this.auth || ''; + if (auth) { + auth = encodeURIComponent(auth); + auth = auth.replace(/%3A/i, ':'); + auth += '@'; + } + + var protocol = this.protocol || '', + pathname = this.pathname || '', + hash = this.hash || '', + host = false, + query = ''; + + if (this.host) { + host = auth + this.host; + } else if (this.hostname) { + host = auth + (this.hostname.indexOf(':') === -1 ? + this.hostname : + '[' + this.hostname + ']'); + if (this.port) { + host += ':' + this.port; + } + } + + if (this.query && + util.isObject(this.query) && + Object.keys(this.query).length) { + query = querystring.stringify(this.query); + } + + var search = this.search || (query && ('?' + query)) || ''; + + if (protocol && protocol.substr(-1) !== ':') protocol += ':'; + + // only the slashedProtocols get the //. Not mailto:, xmpp:, etc. + // unless they had them to begin with. + if (this.slashes || + (!protocol || slashedProtocol[protocol]) && host !== false) { + host = '//' + (host || ''); + if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname; + } else if (!host) { + host = ''; + } + + if (hash && hash.charAt(0) !== '#') hash = '#' + hash; + if (search && search.charAt(0) !== '?') search = '?' + search; + + pathname = pathname.replace(/[?#]/g, function(match) { + return encodeURIComponent(match); + }); + search = search.replace('#', '%23'); + + return protocol + host + pathname + search + hash; +}; + +function urlResolve(source, relative) { + return urlParse(source, false, true).resolve(relative); +} + +Url.prototype.resolve = function(relative) { + return this.resolveObject(urlParse(relative, false, true)).format(); +}; + +function urlResolveObject(source, relative) { + if (!source) return relative; + return urlParse(source, false, true).resolveObject(relative); +} + +Url.prototype.resolveObject = function(relative) { + if (util.isString(relative)) { + var rel = new Url(); + rel.parse(relative, false, true); + relative = rel; + } + + var result = new Url(); + var tkeys = Object.keys(this); + for (var tk = 0; tk < tkeys.length; tk++) { + var tkey = tkeys[tk]; + result[tkey] = this[tkey]; + } + + // hash is always overridden, no matter what. + // even href="" will remove it. + result.hash = relative.hash; + + // if the relative url is empty, then there's nothing left to do here. + if (relative.href === '') { + result.href = result.format(); + return result; + } + + // hrefs like //foo/bar always cut to the protocol. + if (relative.slashes && !relative.protocol) { + // take everything except the protocol from relative + var rkeys = Object.keys(relative); + for (var rk = 0; rk < rkeys.length; rk++) { + var rkey = rkeys[rk]; + if (rkey !== 'protocol') + result[rkey] = relative[rkey]; + } + + //urlParse appends trailing / to urls like http://www.example.com + if (slashedProtocol[result.protocol] && + result.hostname && !result.pathname) { + result.path = result.pathname = '/'; + } + + result.href = result.format(); + return result; + } + + if (relative.protocol && relative.protocol !== result.protocol) { + // if it's a known url protocol, then changing + // the protocol does weird things + // first, if it's not file:, then we MUST have a host, + // and if there was a path + // to begin with, then we MUST have a path. + // if it is file:, then the host is dropped, + // because that's known to be hostless. + // anything else is assumed to be absolute. + if (!slashedProtocol[relative.protocol]) { + var keys = Object.keys(relative); + for (var v = 0; v < keys.length; v++) { + var k = keys[v]; + result[k] = relative[k]; + } + result.href = result.format(); + return result; + } + + result.protocol = relative.protocol; + if (!relative.host && !hostlessProtocol[relative.protocol]) { + var relPath = (relative.pathname || '').split('/'); + while (relPath.length && !(relative.host = relPath.shift())); + if (!relative.host) relative.host = ''; + if (!relative.hostname) relative.hostname = ''; + if (relPath[0] !== '') relPath.unshift(''); + if (relPath.length < 2) relPath.unshift(''); + result.pathname = relPath.join('/'); + } else { + result.pathname = relative.pathname; + } + result.search = relative.search; + result.query = relative.query; + result.host = relative.host || ''; + result.auth = relative.auth; + result.hostname = relative.hostname || relative.host; + result.port = relative.port; + // to support http.request + if (result.pathname || result.search) { + var p = result.pathname || ''; + var s = result.search || ''; + result.path = p + s; + } + result.slashes = result.slashes || relative.slashes; + result.href = result.format(); + return result; + } + + var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'), + isRelAbs = ( + relative.host || + relative.pathname && relative.pathname.charAt(0) === '/' + ), + mustEndAbs = (isRelAbs || isSourceAbs || + (result.host && relative.pathname)), + removeAllDots = mustEndAbs, + srcPath = result.pathname && result.pathname.split('/') || [], + relPath = relative.pathname && relative.pathname.split('/') || [], + psychotic = result.protocol && !slashedProtocol[result.protocol]; + + // if the url is a non-slashed url, then relative + // links like ../.. should be able + // to crawl up to the hostname, as well. This is strange. + // result.protocol has already been set by now. + // Later on, put the first path part into the host field. + if (psychotic) { + result.hostname = ''; + result.port = null; + if (result.host) { + if (srcPath[0] === '') srcPath[0] = result.host; + else srcPath.unshift(result.host); + } + result.host = ''; + if (relative.protocol) { + relative.hostname = null; + relative.port = null; + if (relative.host) { + if (relPath[0] === '') relPath[0] = relative.host; + else relPath.unshift(relative.host); + } + relative.host = null; + } + mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === ''); + } + + if (isRelAbs) { + // it's absolute. + result.host = (relative.host || relative.host === '') ? + relative.host : result.host; + result.hostname = (relative.hostname || relative.hostname === '') ? + relative.hostname : result.hostname; + result.search = relative.search; + result.query = relative.query; + srcPath = relPath; + // fall through to the dot-handling below. + } else if (relPath.length) { + // it's relative + // throw away the existing file, and take the new path instead. + if (!srcPath) srcPath = []; + srcPath.pop(); + srcPath = srcPath.concat(relPath); + result.search = relative.search; + result.query = relative.query; + } else if (!util.isNullOrUndefined(relative.search)) { + // just pull out the search. + // like href='?foo'. + // Put this after the other two cases because it simplifies the booleans + if (psychotic) { + result.hostname = result.host = srcPath.shift(); + //occationaly the auth can get stuck only in host + //this especially happens in cases like + //url.resolveObject('mailto:local1@domain1', 'local2@domain2') + var authInHost = result.host && result.host.indexOf('@') > 0 ? + result.host.split('@') : false; + if (authInHost) { + result.auth = authInHost.shift(); + result.host = result.hostname = authInHost.shift(); + } + } + result.search = relative.search; + result.query = relative.query; + //to support http.request + if (!util.isNull(result.pathname) || !util.isNull(result.search)) { + result.path = (result.pathname ? result.pathname : '') + + (result.search ? result.search : ''); + } + result.href = result.format(); + return result; + } + + if (!srcPath.length) { + // no path at all. easy. + // we've already handled the other stuff above. + result.pathname = null; + //to support http.request + if (result.search) { + result.path = '/' + result.search; + } else { + result.path = null; + } + result.href = result.format(); + return result; + } + + // if a url ENDs in . or .., then it must get a trailing slash. + // however, if it ends in anything else non-slashy, + // then it must NOT get a trailing slash. + var last = srcPath.slice(-1)[0]; + var hasTrailingSlash = ( + (result.host || relative.host || srcPath.length > 1) && + (last === '.' || last === '..') || last === ''); + + // strip single dots, resolve double dots to parent dir + // if the path tries to go above the root, `up` ends up > 0 + var up = 0; + for (var i = srcPath.length; i >= 0; i--) { + last = srcPath[i]; + if (last === '.') { + srcPath.splice(i, 1); + } else if (last === '..') { + srcPath.splice(i, 1); + up++; + } else if (up) { + srcPath.splice(i, 1); + up--; + } + } + + // if the path is allowed to go above the root, restore leading ..s + if (!mustEndAbs && !removeAllDots) { + for (; up--; up) { + srcPath.unshift('..'); + } + } + + if (mustEndAbs && srcPath[0] !== '' && + (!srcPath[0] || srcPath[0].charAt(0) !== '/')) { + srcPath.unshift(''); + } + + if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) { + srcPath.push(''); + } + + var isAbsolute = srcPath[0] === '' || + (srcPath[0] && srcPath[0].charAt(0) === '/'); + + // put the host back + if (psychotic) { + result.hostname = result.host = isAbsolute ? '' : + srcPath.length ? srcPath.shift() : ''; + //occationaly the auth can get stuck only in host + //this especially happens in cases like + //url.resolveObject('mailto:local1@domain1', 'local2@domain2') + var authInHost = result.host && result.host.indexOf('@') > 0 ? + result.host.split('@') : false; + if (authInHost) { + result.auth = authInHost.shift(); + result.host = result.hostname = authInHost.shift(); + } + } + + mustEndAbs = mustEndAbs || (result.host && srcPath.length); + + if (mustEndAbs && !isAbsolute) { + srcPath.unshift(''); + } + + if (!srcPath.length) { + result.pathname = null; + result.path = null; + } else { + result.pathname = srcPath.join('/'); + } + + //to support request.http + if (!util.isNull(result.pathname) || !util.isNull(result.search)) { + result.path = (result.pathname ? result.pathname : '') + + (result.search ? result.search : ''); + } + result.auth = relative.auth || result.auth; + result.slashes = result.slashes || relative.slashes; + result.href = result.format(); + return result; +}; + +Url.prototype.parseHost = function() { + var host = this.host; + var port = portPattern.exec(host); + if (port) { + port = port[0]; + if (port !== ':') { + this.port = port.substr(1); + } + host = host.substr(0, host.length - port.length); + } + if (host) this.hostname = host; +}; + +},{"./util":997,"punycode":924,"querystring":928}],997:[function(require,module,exports){ +'use strict'; + +module.exports = { + isString: function(arg) { + return typeof(arg) === 'string'; + }, + isObject: function(arg) { + return typeof(arg) === 'object' && arg !== null; + }, + isNull: function(arg) { + return arg === null; + }, + isNullOrUndefined: function(arg) { + return arg == null; + } +}; + +},{}],998:[function(require,module,exports){ +(function (global){ + +/** + * Module exports. + */ + +module.exports = deprecate; + +/** + * Mark that a method should not be used. + * Returns a modified function which warns once by default. + * + * If `localStorage.noDeprecation = true` is set, then it is a no-op. + * + * If `localStorage.throwDeprecation = true` is set, then deprecated functions + * will throw an Error when invoked. + * + * If `localStorage.traceDeprecation = true` is set, then deprecated functions + * will invoke `console.trace()` instead of `console.error()`. + * + * @param {Function} fn - the function to deprecate + * @param {String} msg - the string to print to the console when `fn` is invoked + * @returns {Function} a new "deprecated" version of `fn` + * @api public + */ + +function deprecate (fn, msg) { + if (config('noDeprecation')) { + return fn; + } + + var warned = false; + function deprecated() { + if (!warned) { + if (config('throwDeprecation')) { + throw new Error(msg); + } else if (config('traceDeprecation')) { + console.trace(msg); + } else { + console.warn(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; +} + +/** + * Checks `localStorage` for boolean values for the given `name`. + * + * @param {String} name + * @returns {Boolean} + * @api private + */ + +function config (name) { + // accessing global.localStorage can trigger a DOMException in sandboxed iframes + try { + if (!global.localStorage) return false; + } catch (_) { + return false; + } + var val = global.localStorage[name]; + if (null == val) return false; + return String(val).toLowerCase() === 'true'; +} + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{}],999:[function(require,module,exports){ +arguments[4][266][0].apply(exports,arguments) +},{"dup":266}],1000:[function(require,module,exports){ +module.exports = function isBuffer(arg) { + return arg && typeof arg === 'object' + && typeof arg.copy === 'function' + && typeof arg.fill === 'function' + && typeof arg.readUInt8 === 'function'; +} +},{}],1001:[function(require,module,exports){ +(function (process,global){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var formatRegExp = /%[sdj%]/g; +exports.format = function(f) { + if (!isString(f)) { + var objects = []; + for (var i = 0; i < arguments.length; i++) { + objects.push(inspect(arguments[i])); + } + return objects.join(' '); + } + + var i = 1; + var args = arguments; + var len = args.length; + var str = String(f).replace(formatRegExp, function(x) { + if (x === '%%') return '%'; + if (i >= len) return x; + switch (x) { + case '%s': return String(args[i++]); + case '%d': return Number(args[i++]); + case '%j': + try { + return JSON.stringify(args[i++]); + } catch (_) { + return '[Circular]'; + } + default: + return x; + } + }); + for (var x = args[i]; i < len; x = args[++i]) { + if (isNull(x) || !isObject(x)) { + str += ' ' + x; + } else { + str += ' ' + inspect(x); + } + } + return str; +}; + + +// Mark that a method should not be used. +// Returns a modified function which warns once by default. +// If --no-deprecation is set, then it is a no-op. +exports.deprecate = function(fn, msg) { + // Allow for deprecating things in the process of starting up. + if (isUndefined(global.process)) { + return function() { + return exports.deprecate(fn, msg).apply(this, arguments); + }; + } + + if (process.noDeprecation === true) { + return fn; + } + + var warned = false; + function deprecated() { + if (!warned) { + if (process.throwDeprecation) { + throw new Error(msg); + } else if (process.traceDeprecation) { + console.trace(msg); + } else { + console.error(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; +}; + + +var debugs = {}; +var debugEnviron; +exports.debuglog = function(set) { + if (isUndefined(debugEnviron)) + debugEnviron = process.env.NODE_DEBUG || ''; + set = set.toUpperCase(); + if (!debugs[set]) { + if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { + var pid = process.pid; + debugs[set] = function() { + var msg = exports.format.apply(exports, arguments); + console.error('%s %d: %s', set, pid, msg); + }; + } else { + debugs[set] = function() {}; + } + } + return debugs[set]; +}; + + +/** + * Echos the value of a value. Trys to print the value out + * in the best way possible given the different types. + * + * @param {Object} obj The object to print out. + * @param {Object} opts Optional options object that alters the output. + */ +/* legacy: obj, showHidden, depth, colors*/ +function inspect(obj, opts) { + // default options + var ctx = { + seen: [], + stylize: stylizeNoColor + }; + // legacy... + if (arguments.length >= 3) ctx.depth = arguments[2]; + if (arguments.length >= 4) ctx.colors = arguments[3]; + if (isBoolean(opts)) { + // legacy... + ctx.showHidden = opts; + } else if (opts) { + // got an "options" object + exports._extend(ctx, opts); + } + // set default options + if (isUndefined(ctx.showHidden)) ctx.showHidden = false; + if (isUndefined(ctx.depth)) ctx.depth = 2; + if (isUndefined(ctx.colors)) ctx.colors = false; + if (isUndefined(ctx.customInspect)) ctx.customInspect = true; + if (ctx.colors) ctx.stylize = stylizeWithColor; + return formatValue(ctx, obj, ctx.depth); +} +exports.inspect = inspect; + + +// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics +inspect.colors = { + 'bold' : [1, 22], + 'italic' : [3, 23], + 'underline' : [4, 24], + 'inverse' : [7, 27], + 'white' : [37, 39], + 'grey' : [90, 39], + 'black' : [30, 39], + 'blue' : [34, 39], + 'cyan' : [36, 39], + 'green' : [32, 39], + 'magenta' : [35, 39], + 'red' : [31, 39], + 'yellow' : [33, 39] +}; + +// Don't use 'blue' not visible on cmd.exe +inspect.styles = { + 'special': 'cyan', + 'number': 'yellow', + 'boolean': 'yellow', + 'undefined': 'grey', + 'null': 'bold', + 'string': 'green', + 'date': 'magenta', + // "name": intentionally not styling + 'regexp': 'red' +}; + + +function stylizeWithColor(str, styleType) { + var style = inspect.styles[styleType]; + + if (style) { + return '\u001b[' + inspect.colors[style][0] + 'm' + str + + '\u001b[' + inspect.colors[style][1] + 'm'; + } else { + return str; + } +} + + +function stylizeNoColor(str, styleType) { + return str; +} + + +function arrayToHash(array) { + var hash = {}; + + array.forEach(function(val, idx) { + hash[val] = true; + }); + + return hash; +} + + +function formatValue(ctx, value, recurseTimes) { + // Provide a hook for user-specified inspect functions. + // Check that value is an object with an inspect function on it + if (ctx.customInspect && + value && + isFunction(value.inspect) && + // Filter out the util module, it's inspect function is special + value.inspect !== exports.inspect && + // Also filter out any prototype objects using the circular check. + !(value.constructor && value.constructor.prototype === value)) { + var ret = value.inspect(recurseTimes, ctx); + if (!isString(ret)) { + ret = formatValue(ctx, ret, recurseTimes); + } + return ret; + } + + // Primitive types cannot have properties + var primitive = formatPrimitive(ctx, value); + if (primitive) { + return primitive; + } + + // Look up the keys of the object. + var keys = Object.keys(value); + var visibleKeys = arrayToHash(keys); + + if (ctx.showHidden) { + keys = Object.getOwnPropertyNames(value); + } + + // IE doesn't make error fields non-enumerable + // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx + if (isError(value) + && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { + return formatError(value); + } + + // Some type of object without properties can be shortcutted. + if (keys.length === 0) { + if (isFunction(value)) { + var name = value.name ? ': ' + value.name : ''; + return ctx.stylize('[Function' + name + ']', 'special'); + } + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } + if (isDate(value)) { + return ctx.stylize(Date.prototype.toString.call(value), 'date'); + } + if (isError(value)) { + return formatError(value); + } + } + + var base = '', array = false, braces = ['{', '}']; + + // Make Array say that they are Array + if (isArray(value)) { + array = true; + braces = ['[', ']']; + } + + // Make functions say that they are functions + if (isFunction(value)) { + var n = value.name ? ': ' + value.name : ''; + base = ' [Function' + n + ']'; + } + + // Make RegExps say that they are RegExps + if (isRegExp(value)) { + base = ' ' + RegExp.prototype.toString.call(value); + } + + // Make dates with properties first say the date + if (isDate(value)) { + base = ' ' + Date.prototype.toUTCString.call(value); + } + + // Make error with message first say the error + if (isError(value)) { + base = ' ' + formatError(value); + } + + if (keys.length === 0 && (!array || value.length == 0)) { + return braces[0] + base + braces[1]; + } + + if (recurseTimes < 0) { + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } else { + return ctx.stylize('[Object]', 'special'); + } + } + + ctx.seen.push(value); + + var output; + if (array) { + output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); + } else { + output = keys.map(function(key) { + return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); + }); + } + + ctx.seen.pop(); + + return reduceToSingleString(output, base, braces); +} + + +function formatPrimitive(ctx, value) { + if (isUndefined(value)) + return ctx.stylize('undefined', 'undefined'); + if (isString(value)) { + var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') + .replace(/'/g, "\\'") + .replace(/\\"/g, '"') + '\''; + return ctx.stylize(simple, 'string'); + } + if (isNumber(value)) + return ctx.stylize('' + value, 'number'); + if (isBoolean(value)) + return ctx.stylize('' + value, 'boolean'); + // For some reason typeof null is "object", so special case here. + if (isNull(value)) + return ctx.stylize('null', 'null'); +} + + +function formatError(value) { + return '[' + Error.prototype.toString.call(value) + ']'; +} + + +function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { + var output = []; + for (var i = 0, l = value.length; i < l; ++i) { + if (hasOwnProperty(value, String(i))) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + String(i), true)); + } else { + output.push(''); + } + } + keys.forEach(function(key) { + if (!key.match(/^\d+$/)) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + key, true)); + } + }); + return output; +} + + +function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { + var name, str, desc; + desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; + if (desc.get) { + if (desc.set) { + str = ctx.stylize('[Getter/Setter]', 'special'); + } else { + str = ctx.stylize('[Getter]', 'special'); + } + } else { + if (desc.set) { + str = ctx.stylize('[Setter]', 'special'); + } + } + if (!hasOwnProperty(visibleKeys, key)) { + name = '[' + key + ']'; + } + if (!str) { + if (ctx.seen.indexOf(desc.value) < 0) { + if (isNull(recurseTimes)) { + str = formatValue(ctx, desc.value, null); + } else { + str = formatValue(ctx, desc.value, recurseTimes - 1); + } + if (str.indexOf('\n') > -1) { + if (array) { + str = str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n').substr(2); + } else { + str = '\n' + str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n'); + } + } + } else { + str = ctx.stylize('[Circular]', 'special'); + } + } + if (isUndefined(name)) { + if (array && key.match(/^\d+$/)) { + return str; + } + name = JSON.stringify('' + key); + if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { + name = name.substr(1, name.length - 2); + name = ctx.stylize(name, 'name'); + } else { + name = name.replace(/'/g, "\\'") + .replace(/\\"/g, '"') + .replace(/(^"|"$)/g, "'"); + name = ctx.stylize(name, 'string'); + } + } + + return name + ': ' + str; +} + + +function reduceToSingleString(output, base, braces) { + var numLinesEst = 0; + var length = output.reduce(function(prev, cur) { + numLinesEst++; + if (cur.indexOf('\n') >= 0) numLinesEst++; + return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; + }, 0); + + if (length > 60) { + return braces[0] + + (base === '' ? '' : base + '\n ') + + ' ' + + output.join(',\n ') + + ' ' + + braces[1]; + } + + return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; +} + + +// NOTE: These type checking functions intentionally don't use `instanceof` +// because it is fragile and can be easily faked with `Object.create()`. +function isArray(ar) { + return Array.isArray(ar); +} +exports.isArray = isArray; + +function isBoolean(arg) { + return typeof arg === 'boolean'; +} +exports.isBoolean = isBoolean; + +function isNull(arg) { + return arg === null; +} +exports.isNull = isNull; + +function isNullOrUndefined(arg) { + return arg == null; +} +exports.isNullOrUndefined = isNullOrUndefined; + +function isNumber(arg) { + return typeof arg === 'number'; +} +exports.isNumber = isNumber; + +function isString(arg) { + return typeof arg === 'string'; +} +exports.isString = isString; + +function isSymbol(arg) { + return typeof arg === 'symbol'; +} +exports.isSymbol = isSymbol; + +function isUndefined(arg) { + return arg === void 0; +} +exports.isUndefined = isUndefined; + +function isRegExp(re) { + return isObject(re) && objectToString(re) === '[object RegExp]'; +} +exports.isRegExp = isRegExp; + +function isObject(arg) { + return typeof arg === 'object' && arg !== null; +} +exports.isObject = isObject; + +function isDate(d) { + return isObject(d) && objectToString(d) === '[object Date]'; +} +exports.isDate = isDate; + +function isError(e) { + return isObject(e) && + (objectToString(e) === '[object Error]' || e instanceof Error); +} +exports.isError = isError; + +function isFunction(arg) { + return typeof arg === 'function'; +} +exports.isFunction = isFunction; + +function isPrimitive(arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; +} +exports.isPrimitive = isPrimitive; + +exports.isBuffer = require('./support/isBuffer'); + +function objectToString(o) { + return Object.prototype.toString.call(o); +} + + +function pad(n) { + return n < 10 ? '0' + n.toString(10) : n.toString(10); +} + + +var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', + 'Oct', 'Nov', 'Dec']; + +// 26 Feb 16:19:34 +function timestamp() { + var d = new Date(); + var time = [pad(d.getHours()), + pad(d.getMinutes()), + pad(d.getSeconds())].join(':'); + return [d.getDate(), months[d.getMonth()], time].join(' '); +} + + +// log is just a thin wrapper to console.log that prepends a timestamp +exports.log = function() { + console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); +}; + + +/** + * Inherit the prototype methods from one constructor into another. + * + * The Function.prototype.inherits from lang.js rewritten as a standalone + * function (not on Function.prototype). NOTE: If this file is to be loaded + * during bootstrapping this function needs to be rewritten using some native + * functions as prototype setup using normal JavaScript does not work as + * expected during bootstrapping (see mirror.js in r114903). + * + * @param {function} ctor Constructor function which needs to inherit the + * prototype. + * @param {function} superCtor Constructor function to inherit prototype from. + */ +exports.inherits = require('inherits'); + +exports._extend = function(origin, add) { + // Don't do anything if add isn't an object + if (!add || !isObject(add)) return origin; + + var keys = Object.keys(add); + var i = keys.length; + while (i--) { + origin[keys[i]] = add[keys[i]]; + } + return origin; +}; + +function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} + +}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"./support/isBuffer":1000,"_process":923,"inherits":999}],1002:[function(require,module,exports){ +module.exports.VectorTile = require('./lib/vectortile.js'); +module.exports.VectorTileFeature = require('./lib/vectortilefeature.js'); +module.exports.VectorTileLayer = require('./lib/vectortilelayer.js'); + +},{"./lib/vectortile.js":1003,"./lib/vectortilefeature.js":1004,"./lib/vectortilelayer.js":1005}],1003:[function(require,module,exports){ +'use strict'; + +var VectorTileLayer = require('./vectortilelayer'); + +module.exports = VectorTile; + +function VectorTile(pbf, end) { + this.layers = pbf.readFields(readTile, {}, end); +} + +function readTile(tag, layers, pbf) { + if (tag === 3) { + var layer = new VectorTileLayer(pbf, pbf.readVarint() + pbf.pos); + if (layer.length) layers[layer.name] = layer; + } +} + + +},{"./vectortilelayer":1005}],1004:[function(require,module,exports){ +'use strict'; + +var Point = require('point-geometry'); + +module.exports = VectorTileFeature; + +function VectorTileFeature(pbf, end, extent, keys, values) { + // Public + this.properties = {}; + this.extent = extent; + this.type = 0; + + // Private + this._pbf = pbf; + this._geometry = -1; + this._keys = keys; + this._values = values; + + pbf.readFields(readFeature, this, end); +} + +function readFeature(tag, feature, pbf) { + if (tag == 1) feature.id = pbf.readVarint(); + else if (tag == 2) readTag(pbf, feature); + else if (tag == 3) feature.type = pbf.readVarint(); + else if (tag == 4) feature._geometry = pbf.pos; +} + +function readTag(pbf, feature) { + var end = pbf.readVarint() + pbf.pos; + + while (pbf.pos < end) { + var key = feature._keys[pbf.readVarint()], + value = feature._values[pbf.readVarint()]; + feature.properties[key] = value; + } +} + +VectorTileFeature.types = ['Unknown', 'Point', 'LineString', 'Polygon']; + +VectorTileFeature.prototype.loadGeometry = function() { + var pbf = this._pbf; + pbf.pos = this._geometry; + + var end = pbf.readVarint() + pbf.pos, + cmd = 1, + length = 0, + x = 0, + y = 0, + lines = [], + line; + + while (pbf.pos < end) { + if (!length) { + var cmdLen = pbf.readVarint(); + cmd = cmdLen & 0x7; + length = cmdLen >> 3; + } + + length--; + + if (cmd === 1 || cmd === 2) { + x += pbf.readSVarint(); + y += pbf.readSVarint(); + + if (cmd === 1) { // moveTo + if (line) lines.push(line); + line = []; + } + + line.push(new Point(x, y)); + + } else if (cmd === 7) { + + // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90 + if (line) { + line.push(line[0].clone()); // closePolygon + } + + } else { + throw new Error('unknown command ' + cmd); + } + } + + if (line) lines.push(line); + + return lines; +}; + +VectorTileFeature.prototype.bbox = function() { + var pbf = this._pbf; + pbf.pos = this._geometry; + + var end = pbf.readVarint() + pbf.pos, + cmd = 1, + length = 0, + x = 0, + y = 0, + x1 = Infinity, + x2 = -Infinity, + y1 = Infinity, + y2 = -Infinity; + + while (pbf.pos < end) { + if (!length) { + var cmdLen = pbf.readVarint(); + cmd = cmdLen & 0x7; + length = cmdLen >> 3; + } + + length--; + + if (cmd === 1 || cmd === 2) { + x += pbf.readSVarint(); + y += pbf.readSVarint(); + if (x < x1) x1 = x; + if (x > x2) x2 = x; + if (y < y1) y1 = y; + if (y > y2) y2 = y; + + } else if (cmd !== 7) { + throw new Error('unknown command ' + cmd); + } + } + + return [x1, y1, x2, y2]; +}; + +VectorTileFeature.prototype.toGeoJSON = function(x, y, z) { + var size = this.extent * Math.pow(2, z), + x0 = this.extent * x, + y0 = this.extent * y, + coords = this.loadGeometry(), + type = VectorTileFeature.types[this.type], + i, j; + + function project(line) { + for (var j = 0; j < line.length; j++) { + var p = line[j], y2 = 180 - (p.y + y0) * 360 / size; + line[j] = [ + (p.x + x0) * 360 / size - 180, + 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90 + ]; + } + } + + switch (this.type) { + case 1: + var points = []; + for (i = 0; i < coords.length; i++) { + points[i] = coords[i][0]; + } + coords = points; + project(coords); + break; + + case 2: + for (i = 0; i < coords.length; i++) { + project(coords[i]); + } + break; + + case 3: + coords = classifyRings(coords); + for (i = 0; i < coords.length; i++) { + for (j = 0; j < coords[i].length; j++) { + project(coords[i][j]); + } + } + break; + } + + if (coords.length === 1) { + coords = coords[0]; + } else { + type = 'Multi' + type; + } + + var result = { + type: "Feature", + geometry: { + type: type, + coordinates: coords + }, + properties: this.properties + }; + + if ('id' in this) { + result.id = this.id; + } + + return result; +}; + +// classifies an array of rings into polygons with outer rings and holes + +function classifyRings(rings) { + var len = rings.length; + + if (len <= 1) return [rings]; + + var polygons = [], + polygon, + ccw; + + for (var i = 0; i < len; i++) { + var area = signedArea(rings[i]); + if (area === 0) continue; + + if (ccw === undefined) ccw = area < 0; + + if (ccw === area < 0) { + if (polygon) polygons.push(polygon); + polygon = [rings[i]]; + + } else { + polygon.push(rings[i]); + } + } + if (polygon) polygons.push(polygon); + + return polygons; +} + +function signedArea(ring) { + var sum = 0; + for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) { + p1 = ring[i]; + p2 = ring[j]; + sum += (p2.x - p1.x) * (p1.y + p2.y); + } + return sum; +} + +},{"point-geometry":919}],1005:[function(require,module,exports){ +'use strict'; + +var VectorTileFeature = require('./vectortilefeature.js'); + +module.exports = VectorTileLayer; + +function VectorTileLayer(pbf, end) { + // Public + this.version = 1; + this.name = null; + this.extent = 4096; + this.length = 0; + + // Private + this._pbf = pbf; + this._keys = []; + this._values = []; + this._features = []; + + pbf.readFields(readLayer, this, end); + + this.length = this._features.length; +} + +function readLayer(tag, layer, pbf) { + if (tag === 15) layer.version = pbf.readVarint(); + else if (tag === 1) layer.name = pbf.readString(); + else if (tag === 5) layer.extent = pbf.readVarint(); + else if (tag === 2) layer._features.push(pbf.pos); + else if (tag === 3) layer._keys.push(pbf.readString()); + else if (tag === 4) layer._values.push(readValueMessage(pbf)); +} + +function readValueMessage(pbf) { + var value = null, + end = pbf.readVarint() + pbf.pos; + + while (pbf.pos < end) { + var tag = pbf.readVarint() >> 3; + + value = tag === 1 ? pbf.readString() : + tag === 2 ? pbf.readFloat() : + tag === 3 ? pbf.readDouble() : + tag === 4 ? pbf.readVarint64() : + tag === 5 ? pbf.readVarint() : + tag === 6 ? pbf.readSVarint() : + tag === 7 ? pbf.readBoolean() : null; + } + + return value; +} + +// return feature `i` from this layer as a `VectorTileFeature` +VectorTileLayer.prototype.feature = function(i) { + if (i < 0 || i >= this._features.length) throw new Error('feature index out of bounds'); + + this._pbf.pos = this._features[i]; + + var end = this._pbf.readVarint() + this._pbf.pos; + return new VectorTileFeature(this._pbf, end, this.extent, this._keys, this._values); +}; + +},{"./vectortilefeature.js":1004}],1006:[function(require,module,exports){ +"use strict" + +module.exports = createText + +var vectorizeText = require("./lib/vtext") +var defaultCanvas = null +var defaultContext = null + +if(typeof document !== 'undefined') { + defaultCanvas = document.createElement('canvas') + defaultCanvas.width = 8192 + defaultCanvas.height = 1024 + defaultContext = defaultCanvas.getContext("2d") +} + +function createText(str, options) { + if((typeof options !== "object") || (options === null)) { + options = {} + } + return vectorizeText( + str, + options.canvas || defaultCanvas, + options.context || defaultContext, + options) +} + +},{"./lib/vtext":1007}],1007:[function(require,module,exports){ +"use strict" + +module.exports = vectorizeText +module.exports.processPixels = processPixels + +var surfaceNets = require('surface-nets') +var ndarray = require('ndarray') +var simplify = require('simplify-planar-graph') +var cleanPSLG = require('clean-pslg') +var cdt2d = require('cdt2d') +var toPolygonCrappy = require('planar-graph-to-polyline') + +function transformPositions(positions, options, size) { + var align = options.textAlign || "start" + var baseline = options.textBaseline || "alphabetic" + + var lo = [1<<30, 1<<30] + var hi = [0,0] + var n = positions.length + for(var i=0; i 8192) { + throw new Error("vectorize-text: String too long (sorry, this will get fixed later)") + } + var height = 3 * size + if(canvas.height < height) { + canvas.height = height + } + + context.fillStyle = "#000" + context.fillRect(0, 0, canvas.width, canvas.height) + + context.fillStyle = "#fff" + context.fillText(str, size, 2*size) + + //Cut pixels from image + var pixelData = context.getImageData(0, 0, width, height) + var pixels = ndarray(pixelData.data, [height, width, 4]) + + return pixels.pick(-1,-1,0).transpose(1,0) +} + +function getContour(pixels, doSimplify) { + var contour = surfaceNets(pixels, 128) + if(doSimplify) { + return simplify(contour.cells, contour.positions, 0.25) + } + return { + edges: contour.cells, + positions: contour.positions + } +} + +function processPixelsImpl(pixels, options, size, simplify) { + //Extract contour + var contour = getContour(pixels, simplify) + + //Apply warp to positions + var positions = transformPositions(contour.positions, options, size) + var edges = contour.edges + var flip = "ccw" === options.orientation + + //Clean up the PSLG, resolve self intersections, etc. + cleanPSLG(positions, edges) + + //If triangulate flag passed, triangulate the result + if(options.polygons || options.polygon || options.polyline) { + var result = toPolygonCrappy(edges, positions) + var nresult = new Array(result.length) + for(var i=0; i> 31) +} + +/** + * Encode a polygon's geometry into an array ready to be serialized + * to mapbox vector tile specified geometry data. + * + * @param {Array} Rings, each being an array of [x, y] tile-space coordinates + * @return {Array} encoded geometry + */ +function encodeGeometry (geometry) { + var encoded = [] + var x = 0 + var y = 0 + var rings = geometry.length + for (var r = 0; r < rings; r++) { + var ring = geometry[r] + encoded.push(command(1, 1)) // moveto + for (var i = 0; i < ring.length; i++) { + if (i === 1) { + encoded.push(command(2, ring.length - 1)) // lineto + } + var dx = ring[i].x - x + var dy = ring[i].y - y + encoded.push(zigzag(dx), zigzag(dy)) + x += dx + y += dy + } + } + + return encoded +} + +/** + * Wrap a property value according to its type. The returned object + * is of the form { xxxx_value: primitiveValue }, which is what the generated + * protobuf serializer expects. + */ +function wrapValue (value) { + var result + var type = typeof value + if (type === 'string') { + result = { string_value: value } + } else if (type === 'boolean') { + result = { bool_value: value } + } else if (type === 'number') { + if (value % 1 !== 0) { + result = { double_value: value } + } else if (value < 0) { + result = { sint_value: value } + } else { + result = { uint_value: value } + } + } else { + value = JSON.stringify(value) + result = { string_value: value } + } + + result.key = type + ':' + value + return result +} + +},{"./lib/geojson_wrapper":1009,"./vector-tile-pb":1010,"pbf":465}],1009:[function(require,module,exports){ +'use strict' + +var Point = require('point-geometry') +var VectorTileFeature = require('vector-tile').VectorTileFeature + +module.exports = GeoJSONWrapper + +// conform to vectortile api +function GeoJSONWrapper (features) { + this.features = features + this.length = features.length +} + +GeoJSONWrapper.prototype.feature = function (i) { + return new FeatureWrapper(this.features[i]) +} + +function FeatureWrapper (feature) { + this.id = typeof feature.id === 'number' ? feature.id : undefined + this.type = feature.type + this.rawGeometry = feature.type === 1 ? [feature.geometry] : feature.geometry + this.properties = feature.tags + this.extent = 4096 +} + +FeatureWrapper.prototype.loadGeometry = function () { + var rings = this.rawGeometry + this.geometry = [] + + for (var i = 0; i < rings.length; i++) { + var ring = rings[i] + var newRing = [] + for (var j = 0; j < ring.length; j++) { + newRing.push(new Point(ring[j][0], ring[j][1])) + } + this.geometry.push(newRing) + } + return this.geometry +} + +FeatureWrapper.prototype.bbox = function () { + if (!this.geometry) this.loadGeometry() + + var rings = this.geometry + var x1 = Infinity + var x2 = -Infinity + var y1 = Infinity + var y2 = -Infinity + + for (var i = 0; i < rings.length; i++) { + var ring = rings[i] + + for (var j = 0; j < ring.length; j++) { + var coord = ring[j] + + x1 = Math.min(x1, coord.x) + x2 = Math.max(x2, coord.x) + y1 = Math.min(y1, coord.y) + y2 = Math.max(y2, coord.y) + } + } + + return [x1, y1, x2, y2] +} + +FeatureWrapper.prototype.toGeoJSON = VectorTileFeature.prototype.toGeoJSON + +},{"point-geometry":919,"vector-tile":1002}],1010:[function(require,module,exports){ +'use strict'; + +// tile ======================================== + +var tile = exports.tile = {read: readTile, write: writeTile}; + +tile.GeomType = { + "Unknown": 0, + "Point": 1, + "LineString": 2, + "Polygon": 3 +}; + +function readTile(pbf, end) { + return pbf.readFields(readTileField, {"layers": []}, end); +} + +function readTileField(tag, tile, pbf) { + if (tag === 3) tile.layers.push(readLayer(pbf, pbf.readVarint() + pbf.pos)); +} + +function writeTile(tile, pbf) { + var i; + if (tile.layers !== undefined) for (i = 0; i < tile.layers.length; i++) pbf.writeMessage(3, writeLayer, tile.layers[i]); +} + +// value ======================================== + +tile.value = {read: readValue, write: writeValue}; + +function readValue(pbf, end) { + return pbf.readFields(readValueField, {}, end); +} + +function readValueField(tag, value, pbf) { + if (tag === 1) value.string_value = pbf.readString(); + else if (tag === 2) value.float_value = pbf.readFloat(); + else if (tag === 3) value.double_value = pbf.readDouble(); + else if (tag === 4) value.int_value = pbf.readVarint(); + else if (tag === 5) value.uint_value = pbf.readVarint(); + else if (tag === 6) value.sint_value = pbf.readSVarint(); + else if (tag === 7) value.bool_value = pbf.readBoolean(); +} + +function writeValue(value, pbf) { + if (value.string_value !== undefined) pbf.writeStringField(1, value.string_value); + if (value.float_value !== undefined) pbf.writeFloatField(2, value.float_value); + if (value.double_value !== undefined) pbf.writeDoubleField(3, value.double_value); + if (value.int_value !== undefined) pbf.writeVarintField(4, value.int_value); + if (value.uint_value !== undefined) pbf.writeVarintField(5, value.uint_value); + if (value.sint_value !== undefined) pbf.writeSVarintField(6, value.sint_value); + if (value.bool_value !== undefined) pbf.writeBooleanField(7, value.bool_value); +} + +// feature ======================================== + +tile.feature = {read: readFeature, write: writeFeature}; + +function readFeature(pbf, end) { + var feature = pbf.readFields(readFeatureField, {}, end); + if (feature.type === undefined) feature.type = "Unknown"; + return feature; +} + +function readFeatureField(tag, feature, pbf) { + if (tag === 1) feature.id = pbf.readVarint(); + else if (tag === 2) feature.tags = pbf.readPackedVarint(); + else if (tag === 3) feature.type = pbf.readVarint(); + else if (tag === 4) feature.geometry = pbf.readPackedVarint(); +} + +function writeFeature(feature, pbf) { + if (feature.id !== undefined) pbf.writeVarintField(1, feature.id); + if (feature.tags !== undefined) pbf.writePackedVarint(2, feature.tags); + if (feature.type !== undefined) pbf.writeVarintField(3, feature.type); + if (feature.geometry !== undefined) pbf.writePackedVarint(4, feature.geometry); +} + +// layer ======================================== + +tile.layer = {read: readLayer, write: writeLayer}; + +function readLayer(pbf, end) { + return pbf.readFields(readLayerField, {"features": [], "keys": [], "values": []}, end); +} + +function readLayerField(tag, layer, pbf) { + if (tag === 15) layer.version = pbf.readVarint(); + else if (tag === 1) layer.name = pbf.readString(); + else if (tag === 2) layer.features.push(readFeature(pbf, pbf.readVarint() + pbf.pos)); + else if (tag === 3) layer.keys.push(pbf.readString()); + else if (tag === 4) layer.values.push(readValue(pbf, pbf.readVarint() + pbf.pos)); + else if (tag === 5) layer.extent = pbf.readVarint(); +} + +function writeLayer(layer, pbf) { + if (layer.version !== undefined) pbf.writeVarintField(15, layer.version); + if (layer.name !== undefined) pbf.writeStringField(1, layer.name); + var i; + if (layer.features !== undefined) for (i = 0; i < layer.features.length; i++) pbf.writeMessage(2, writeFeature, layer.features[i]); + if (layer.keys !== undefined) for (i = 0; i < layer.keys.length; i++) pbf.writeStringField(3, layer.keys[i]); + if (layer.values !== undefined) for (i = 0; i < layer.values.length; i++) pbf.writeMessage(4, writeValue, layer.values[i]); + if (layer.extent !== undefined) pbf.writeVarintField(5, layer.extent); +} + +},{}],1011:[function(require,module,exports){ +// Copyright (C) 2011 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview Install a leaky WeakMap emulation on platforms that + * don't provide a built-in one. + * + *

Assumes that an ES5 platform where, if {@code WeakMap} is + * already present, then it conforms to the anticipated ES6 + * specification. To run this file on an ES5 or almost ES5 + * implementation where the {@code WeakMap} specification does not + * quite conform, run repairES5.js first. + * + *

Even though WeakMapModule is not global, the linter thinks it + * is, which is why it is in the overrides list below. + * + *

NOTE: Before using this WeakMap emulation in a non-SES + * environment, see the note below about hiddenRecord. + * + * @author Mark S. Miller + * @requires crypto, ArrayBuffer, Uint8Array, navigator, console + * @overrides WeakMap, ses, Proxy + * @overrides WeakMapModule + */ + +/** + * This {@code WeakMap} emulation is observably equivalent to the + * ES-Harmony WeakMap, but with leakier garbage collection properties. + * + *

As with true WeakMaps, in this emulation, a key does not + * retain maps indexed by that key and (crucially) a map does not + * retain the keys it indexes. A map by itself also does not retain + * the values associated with that map. + * + *

However, the values associated with a key in some map are + * retained so long as that key is retained and those associations are + * not overridden. For example, when used to support membranes, all + * values exported from a given membrane will live for the lifetime + * they would have had in the absence of an interposed membrane. Even + * when the membrane is revoked, all objects that would have been + * reachable in the absence of revocation will still be reachable, as + * far as the GC can tell, even though they will no longer be relevant + * to ongoing computation. + * + *

The API implemented here is approximately the API as implemented + * in FF6.0a1 and agreed to by MarkM, Andreas Gal, and Dave Herman, + * rather than the offially approved proposal page. TODO(erights): + * upgrade the ecmascript WeakMap proposal page to explain this API + * change and present to EcmaScript committee for their approval. + * + *

The first difference between the emulation here and that in + * FF6.0a1 is the presence of non enumerable {@code get___, has___, + * set___, and delete___} methods on WeakMap instances to represent + * what would be the hidden internal properties of a primitive + * implementation. Whereas the FF6.0a1 WeakMap.prototype methods + * require their {@code this} to be a genuine WeakMap instance (i.e., + * an object of {@code [[Class]]} "WeakMap}), since there is nothing + * unforgeable about the pseudo-internal method names used here, + * nothing prevents these emulated prototype methods from being + * applied to non-WeakMaps with pseudo-internal methods of the same + * names. + * + *

Another difference is that our emulated {@code + * WeakMap.prototype} is not itself a WeakMap. A problem with the + * current FF6.0a1 API is that WeakMap.prototype is itself a WeakMap + * providing ambient mutability and an ambient communications + * channel. Thus, if a WeakMap is already present and has this + * problem, repairES5.js wraps it in a safe wrappper in order to + * prevent access to this channel. (See + * PATCH_MUTABLE_FROZEN_WEAKMAP_PROTO in repairES5.js). + */ + +/** + * If this is a full secureable ES5 platform and the ES-Harmony {@code WeakMap} is + * absent, install an approximate emulation. + * + *

If WeakMap is present but cannot store some objects, use our approximate + * emulation as a wrapper. + * + *

If this is almost a secureable ES5 platform, then WeakMap.js + * should be run after repairES5.js. + * + *

See {@code WeakMap} for documentation of the garbage collection + * properties of this WeakMap emulation. + */ +(function WeakMapModule() { + "use strict"; + + if (typeof ses !== 'undefined' && ses.ok && !ses.ok()) { + // already too broken, so give up + return; + } + + /** + * In some cases (current Firefox), we must make a choice betweeen a + * WeakMap which is capable of using all varieties of host objects as + * keys and one which is capable of safely using proxies as keys. See + * comments below about HostWeakMap and DoubleWeakMap for details. + * + * This function (which is a global, not exposed to guests) marks a + * WeakMap as permitted to do what is necessary to index all host + * objects, at the cost of making it unsafe for proxies. + * + * Do not apply this function to anything which is not a genuine + * fresh WeakMap. + */ + function weakMapPermitHostObjects(map) { + // identity of function used as a secret -- good enough and cheap + if (map.permitHostObjects___) { + map.permitHostObjects___(weakMapPermitHostObjects); + } + } + if (typeof ses !== 'undefined') { + ses.weakMapPermitHostObjects = weakMapPermitHostObjects; + } + + // IE 11 has no Proxy but has a broken WeakMap such that we need to patch + // it using DoubleWeakMap; this flag tells DoubleWeakMap so. + var doubleWeakMapCheckSilentFailure = false; + + // Check if there is already a good-enough WeakMap implementation, and if so + // exit without replacing it. + if (typeof WeakMap === 'function') { + var HostWeakMap = WeakMap; + // There is a WeakMap -- is it good enough? + if (typeof navigator !== 'undefined' && + /Firefox/.test(navigator.userAgent)) { + // We're now *assuming not*, because as of this writing (2013-05-06) + // Firefox's WeakMaps have a miscellany of objects they won't accept, and + // we don't want to make an exhaustive list, and testing for just one + // will be a problem if that one is fixed alone (as they did for Event). + + // If there is a platform that we *can* reliably test on, here's how to + // do it: + // var problematic = ... ; + // var testHostMap = new HostWeakMap(); + // try { + // testHostMap.set(problematic, 1); // Firefox 20 will throw here + // if (testHostMap.get(problematic) === 1) { + // return; + // } + // } catch (e) {} + + } else { + // IE 11 bug: WeakMaps silently fail to store frozen objects. + var testMap = new HostWeakMap(); + var testObject = Object.freeze({}); + testMap.set(testObject, 1); + if (testMap.get(testObject) !== 1) { + doubleWeakMapCheckSilentFailure = true; + // Fall through to installing our WeakMap. + } else { + module.exports = WeakMap; + return; + } + } + } + + var hop = Object.prototype.hasOwnProperty; + var gopn = Object.getOwnPropertyNames; + var defProp = Object.defineProperty; + var isExtensible = Object.isExtensible; + + /** + * Security depends on HIDDEN_NAME being both unguessable and + * undiscoverable by untrusted code. + * + *

Given the known weaknesses of Math.random() on existing + * browsers, it does not generate unguessability we can be confident + * of. + * + *

It is the monkey patching logic in this file that is intended + * to ensure undiscoverability. The basic idea is that there are + * three fundamental means of discovering properties of an object: + * The for/in loop, Object.keys(), and Object.getOwnPropertyNames(), + * as well as some proposed ES6 extensions that appear on our + * whitelist. The first two only discover enumerable properties, and + * we only use HIDDEN_NAME to name a non-enumerable property, so the + * only remaining threat should be getOwnPropertyNames and some + * proposed ES6 extensions that appear on our whitelist. We monkey + * patch them to remove HIDDEN_NAME from the list of properties they + * returns. + * + *

TODO(erights): On a platform with built-in Proxies, proxies + * could be used to trap and thereby discover the HIDDEN_NAME, so we + * need to monkey patch Proxy.create, Proxy.createFunction, etc, in + * order to wrap the provided handler with the real handler which + * filters out all traps using HIDDEN_NAME. + * + *

TODO(erights): Revisit Mike Stay's suggestion that we use an + * encapsulated function at a not-necessarily-secret name, which + * uses the Stiegler shared-state rights amplification pattern to + * reveal the associated value only to the WeakMap in which this key + * is associated with that value. Since only the key retains the + * function, the function can also remember the key without causing + * leakage of the key, so this doesn't violate our general gc + * goals. In addition, because the name need not be a guarded + * secret, we could efficiently handle cross-frame frozen keys. + */ + var HIDDEN_NAME_PREFIX = 'weakmap:'; + var HIDDEN_NAME = HIDDEN_NAME_PREFIX + 'ident:' + Math.random() + '___'; + + if (typeof crypto !== 'undefined' && + typeof crypto.getRandomValues === 'function' && + typeof ArrayBuffer === 'function' && + typeof Uint8Array === 'function') { + var ab = new ArrayBuffer(25); + var u8s = new Uint8Array(ab); + crypto.getRandomValues(u8s); + HIDDEN_NAME = HIDDEN_NAME_PREFIX + 'rand:' + + Array.prototype.map.call(u8s, function(u8) { + return (u8 % 36).toString(36); + }).join('') + '___'; + } + + function isNotHiddenName(name) { + return !( + name.substr(0, HIDDEN_NAME_PREFIX.length) == HIDDEN_NAME_PREFIX && + name.substr(name.length - 3) === '___'); + } + + /** + * Monkey patch getOwnPropertyNames to avoid revealing the + * HIDDEN_NAME. + * + *

The ES5.1 spec requires each name to appear only once, but as + * of this writing, this requirement is controversial for ES6, so we + * made this code robust against this case. If the resulting extra + * search turns out to be expensive, we can probably relax this once + * ES6 is adequately supported on all major browsers, iff no browser + * versions we support at that time have relaxed this constraint + * without providing built-in ES6 WeakMaps. + */ + defProp(Object, 'getOwnPropertyNames', { + value: function fakeGetOwnPropertyNames(obj) { + return gopn(obj).filter(isNotHiddenName); + } + }); + + /** + * getPropertyNames is not in ES5 but it is proposed for ES6 and + * does appear in our whitelist, so we need to clean it too. + */ + if ('getPropertyNames' in Object) { + var originalGetPropertyNames = Object.getPropertyNames; + defProp(Object, 'getPropertyNames', { + value: function fakeGetPropertyNames(obj) { + return originalGetPropertyNames(obj).filter(isNotHiddenName); + } + }); + } + + /** + *

To treat objects as identity-keys with reasonable efficiency + * on ES5 by itself (i.e., without any object-keyed collections), we + * need to add a hidden property to such key objects when we + * can. This raises several issues: + *

    + *
  • Arranging to add this property to objects before we lose the + * chance, and + *
  • Hiding the existence of this new property from most + * JavaScript code. + *
  • Preventing certification theft, where one object is + * created falsely claiming to be the key of an association + * actually keyed by another object. + *
  • Preventing value theft, where untrusted code with + * access to a key object but not a weak map nevertheless + * obtains access to the value associated with that key in that + * weak map. + *
+ * We do so by + *
    + *
  • Making the name of the hidden property unguessable, so "[]" + * indexing, which we cannot intercept, cannot be used to access + * a property without knowing the name. + *
  • Making the hidden property non-enumerable, so we need not + * worry about for-in loops or {@code Object.keys}, + *
  • monkey patching those reflective methods that would + * prevent extensions, to add this hidden property first, + *
  • monkey patching those methods that would reveal this + * hidden property. + *
+ * Unfortunately, because of same-origin iframes, we cannot reliably + * add this hidden property before an object becomes + * non-extensible. Instead, if we encounter a non-extensible object + * without a hidden record that we can detect (whether or not it has + * a hidden record stored under a name secret to us), then we just + * use the key object itself to represent its identity in a brute + * force leaky map stored in the weak map, losing all the advantages + * of weakness for these. + */ + function getHiddenRecord(key) { + if (key !== Object(key)) { + throw new TypeError('Not an object: ' + key); + } + var hiddenRecord = key[HIDDEN_NAME]; + if (hiddenRecord && hiddenRecord.key === key) { return hiddenRecord; } + if (!isExtensible(key)) { + // Weak map must brute force, as explained in doc-comment above. + return void 0; + } + + // The hiddenRecord and the key point directly at each other, via + // the "key" and HIDDEN_NAME properties respectively. The key + // field is for quickly verifying that this hidden record is an + // own property, not a hidden record from up the prototype chain. + // + // NOTE: Because this WeakMap emulation is meant only for systems like + // SES where Object.prototype is frozen without any numeric + // properties, it is ok to use an object literal for the hiddenRecord. + // This has two advantages: + // * It is much faster in a performance critical place + // * It avoids relying on Object.create(null), which had been + // problematic on Chrome 28.0.1480.0. See + // https://code.google.com/p/google-caja/issues/detail?id=1687 + hiddenRecord = { key: key }; + + // When using this WeakMap emulation on platforms where + // Object.prototype might not be frozen and Object.create(null) is + // reliable, use the following two commented out lines instead. + // hiddenRecord = Object.create(null); + // hiddenRecord.key = key; + + // Please contact us if you need this to work on platforms where + // Object.prototype might not be frozen and + // Object.create(null) might not be reliable. + + try { + defProp(key, HIDDEN_NAME, { + value: hiddenRecord, + writable: false, + enumerable: false, + configurable: false + }); + return hiddenRecord; + } catch (error) { + // Under some circumstances, isExtensible seems to misreport whether + // the HIDDEN_NAME can be defined. + // The circumstances have not been isolated, but at least affect + // Node.js v0.10.26 on TravisCI / Linux, but not the same version of + // Node.js on OS X. + return void 0; + } + } + + /** + * Monkey patch operations that would make their argument + * non-extensible. + * + *

The monkey patched versions throw a TypeError if their + * argument is not an object, so it should only be done to functions + * that should throw a TypeError anyway if their argument is not an + * object. + */ + (function(){ + var oldFreeze = Object.freeze; + defProp(Object, 'freeze', { + value: function identifyingFreeze(obj) { + getHiddenRecord(obj); + return oldFreeze(obj); + } + }); + var oldSeal = Object.seal; + defProp(Object, 'seal', { + value: function identifyingSeal(obj) { + getHiddenRecord(obj); + return oldSeal(obj); + } + }); + var oldPreventExtensions = Object.preventExtensions; + defProp(Object, 'preventExtensions', { + value: function identifyingPreventExtensions(obj) { + getHiddenRecord(obj); + return oldPreventExtensions(obj); + } + }); + })(); + + function constFunc(func) { + func.prototype = null; + return Object.freeze(func); + } + + var calledAsFunctionWarningDone = false; + function calledAsFunctionWarning() { + // Future ES6 WeakMap is currently (2013-09-10) expected to reject WeakMap() + // but we used to permit it and do it ourselves, so warn only. + if (!calledAsFunctionWarningDone && typeof console !== 'undefined') { + calledAsFunctionWarningDone = true; + console.warn('WeakMap should be invoked as new WeakMap(), not ' + + 'WeakMap(). This will be an error in the future.'); + } + } + + var nextId = 0; + + var OurWeakMap = function() { + if (!(this instanceof OurWeakMap)) { // approximate test for new ...() + calledAsFunctionWarning(); + } + + // We are currently (12/25/2012) never encountering any prematurely + // non-extensible keys. + var keys = []; // brute force for prematurely non-extensible keys. + var values = []; // brute force for corresponding values. + var id = nextId++; + + function get___(key, opt_default) { + var index; + var hiddenRecord = getHiddenRecord(key); + if (hiddenRecord) { + return id in hiddenRecord ? hiddenRecord[id] : opt_default; + } else { + index = keys.indexOf(key); + return index >= 0 ? values[index] : opt_default; + } + } + + function has___(key) { + var hiddenRecord = getHiddenRecord(key); + if (hiddenRecord) { + return id in hiddenRecord; + } else { + return keys.indexOf(key) >= 0; + } + } + + function set___(key, value) { + var index; + var hiddenRecord = getHiddenRecord(key); + if (hiddenRecord) { + hiddenRecord[id] = value; + } else { + index = keys.indexOf(key); + if (index >= 0) { + values[index] = value; + } else { + // Since some browsers preemptively terminate slow turns but + // then continue computing with presumably corrupted heap + // state, we here defensively get keys.length first and then + // use it to update both the values and keys arrays, keeping + // them in sync. + index = keys.length; + values[index] = value; + // If we crash here, values will be one longer than keys. + keys[index] = key; + } + } + return this; + } + + function delete___(key) { + var hiddenRecord = getHiddenRecord(key); + var index, lastIndex; + if (hiddenRecord) { + return id in hiddenRecord && delete hiddenRecord[id]; + } else { + index = keys.indexOf(key); + if (index < 0) { + return false; + } + // Since some browsers preemptively terminate slow turns but + // then continue computing with potentially corrupted heap + // state, we here defensively get keys.length first and then use + // it to update both the keys and the values array, keeping + // them in sync. We update the two with an order of assignments, + // such that any prefix of these assignments will preserve the + // key/value correspondence, either before or after the delete. + // Note that this needs to work correctly when index === lastIndex. + lastIndex = keys.length - 1; + keys[index] = void 0; + // If we crash here, there's a void 0 in the keys array, but + // no operation will cause a "keys.indexOf(void 0)", since + // getHiddenRecord(void 0) will always throw an error first. + values[index] = values[lastIndex]; + // If we crash here, values[index] cannot be found here, + // because keys[index] is void 0. + keys[index] = keys[lastIndex]; + // If index === lastIndex and we crash here, then keys[index] + // is still void 0, since the aliasing killed the previous key. + keys.length = lastIndex; + // If we crash here, keys will be one shorter than values. + values.length = lastIndex; + return true; + } + } + + return Object.create(OurWeakMap.prototype, { + get___: { value: constFunc(get___) }, + has___: { value: constFunc(has___) }, + set___: { value: constFunc(set___) }, + delete___: { value: constFunc(delete___) } + }); + }; + + OurWeakMap.prototype = Object.create(Object.prototype, { + get: { + /** + * Return the value most recently associated with key, or + * opt_default if none. + */ + value: function get(key, opt_default) { + return this.get___(key, opt_default); + }, + writable: true, + configurable: true + }, + + has: { + /** + * Is there a value associated with key in this WeakMap? + */ + value: function has(key) { + return this.has___(key); + }, + writable: true, + configurable: true + }, + + set: { + /** + * Associate value with key in this WeakMap, overwriting any + * previous association if present. + */ + value: function set(key, value) { + return this.set___(key, value); + }, + writable: true, + configurable: true + }, + + 'delete': { + /** + * Remove any association for key in this WeakMap, returning + * whether there was one. + * + *

Note that the boolean return here does not work like the + * {@code delete} operator. The {@code delete} operator returns + * whether the deletion succeeds at bringing about a state in + * which the deleted property is absent. The {@code delete} + * operator therefore returns true if the property was already + * absent, whereas this {@code delete} method returns false if + * the association was already absent. + */ + value: function remove(key) { + return this.delete___(key); + }, + writable: true, + configurable: true + } + }); + + if (typeof HostWeakMap === 'function') { + (function() { + // If we got here, then the platform has a WeakMap but we are concerned + // that it may refuse to store some key types. Therefore, make a map + // implementation which makes use of both as possible. + + // In this mode we are always using double maps, so we are not proxy-safe. + // This combination does not occur in any known browser, but we had best + // be safe. + if (doubleWeakMapCheckSilentFailure && typeof Proxy !== 'undefined') { + Proxy = undefined; + } + + function DoubleWeakMap() { + if (!(this instanceof OurWeakMap)) { // approximate test for new ...() + calledAsFunctionWarning(); + } + + // Preferable, truly weak map. + var hmap = new HostWeakMap(); + + // Our hidden-property-based pseudo-weak-map. Lazily initialized in the + // 'set' implementation; thus we can avoid performing extra lookups if + // we know all entries actually stored are entered in 'hmap'. + var omap = undefined; + + // Hidden-property maps are not compatible with proxies because proxies + // can observe the hidden name and either accidentally expose it or fail + // to allow the hidden property to be set. Therefore, we do not allow + // arbitrary WeakMaps to switch to using hidden properties, but only + // those which need the ability, and unprivileged code is not allowed + // to set the flag. + // + // (Except in doubleWeakMapCheckSilentFailure mode in which case we + // disable proxies.) + var enableSwitching = false; + + function dget(key, opt_default) { + if (omap) { + return hmap.has(key) ? hmap.get(key) + : omap.get___(key, opt_default); + } else { + return hmap.get(key, opt_default); + } + } + + function dhas(key) { + return hmap.has(key) || (omap ? omap.has___(key) : false); + } + + var dset; + if (doubleWeakMapCheckSilentFailure) { + dset = function(key, value) { + hmap.set(key, value); + if (!hmap.has(key)) { + if (!omap) { omap = new OurWeakMap(); } + omap.set(key, value); + } + return this; + }; + } else { + dset = function(key, value) { + if (enableSwitching) { + try { + hmap.set(key, value); + } catch (e) { + if (!omap) { omap = new OurWeakMap(); } + omap.set___(key, value); + } + } else { + hmap.set(key, value); + } + return this; + }; + } + + function ddelete(key) { + var result = !!hmap['delete'](key); + if (omap) { return omap.delete___(key) || result; } + return result; + } + + return Object.create(OurWeakMap.prototype, { + get___: { value: constFunc(dget) }, + has___: { value: constFunc(dhas) }, + set___: { value: constFunc(dset) }, + delete___: { value: constFunc(ddelete) }, + permitHostObjects___: { value: constFunc(function(token) { + if (token === weakMapPermitHostObjects) { + enableSwitching = true; + } else { + throw new Error('bogus call to permitHostObjects___'); + } + })} + }); + } + DoubleWeakMap.prototype = OurWeakMap.prototype; + module.exports = DoubleWeakMap; + + // define .constructor to hide OurWeakMap ctor + Object.defineProperty(WeakMap.prototype, 'constructor', { + value: WeakMap, + enumerable: false, // as default .constructor is + configurable: true, + writable: true + }); + })(); + } else { + // There is no host WeakMap, so we must use the emulation. + + // Emulated WeakMaps are incompatible with native proxies (because proxies + // can observe the hidden name), so we must disable Proxy usage (in + // ArrayLike and Domado, currently). + if (typeof Proxy !== 'undefined') { + Proxy = undefined; + } + + module.exports = OurWeakMap; + } +})(); + +},{}],1012:[function(require,module,exports){ +var hiddenStore = require('./hidden-store.js'); + +module.exports = createStore; + +function createStore() { + var key = {}; + + return function (obj) { + if ((typeof obj !== 'object' || obj === null) && + typeof obj !== 'function' + ) { + throw new Error('Weakmap-shim: Key must be object') + } + + var store = obj.valueOf(key); + return store && store.identity === key ? + store : hiddenStore(obj, key); + }; +} + +},{"./hidden-store.js":1013}],1013:[function(require,module,exports){ +module.exports = hiddenStore; + +function hiddenStore(obj, key) { + var store = { identity: key }; + var valueOf = obj.valueOf; + + Object.defineProperty(obj, "valueOf", { + value: function (value) { + return value !== key ? + valueOf.apply(this, arguments) : store; + }, + writable: true + }); + + return store; +} + +},{}],1014:[function(require,module,exports){ +// Original - @Gozola. +// https://gist.github.com/Gozala/1269991 +// This is a reimplemented version (with a few bug fixes). + +var createStore = require('./create-store.js'); + +module.exports = weakMap; + +function weakMap() { + var privates = createStore(); + + return { + 'get': function (key, fallback) { + var store = privates(key) + return store.hasOwnProperty('value') ? + store.value : fallback + }, + 'set': function (key, value) { + privates(key).value = value; + return this; + }, + 'has': function(key) { + return 'value' in privates(key); + }, + 'delete': function (key) { + return delete privates(key).value; + } + } +} + +},{"./create-store.js":1012}],1015:[function(require,module,exports){ +var getContext = require('get-canvas-context') + +module.exports = function getWebGLContext (opt) { + return getContext('webgl', opt) +} + +},{"get-canvas-context":112}],1016:[function(require,module,exports){ +var bundleFn = arguments[3]; +var sources = arguments[4]; +var cache = arguments[5]; + +var stringify = JSON.stringify; + +module.exports = function (fn, options) { + var wkey; + var cacheKeys = Object.keys(cache); + + for (var i = 0, l = cacheKeys.length; i < l; i++) { + var key = cacheKeys[i]; + var exp = cache[key].exports; + // Using babel as a transpiler to use esmodule, the export will always + // be an object with the default export as a property of it. To ensure + // the existing api and babel esmodule exports are both supported we + // check for both + if (exp === fn || exp && exp.default === fn) { + wkey = key; + break; + } + } + + if (!wkey) { + wkey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16); + var wcache = {}; + for (var i = 0, l = cacheKeys.length; i < l; i++) { + var key = cacheKeys[i]; + wcache[key] = key; + } + sources[wkey] = [ + Function(['require','module','exports'], '(' + fn + ')(self)'), + wcache + ]; + } + var skey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16); + + var scache = {}; scache[wkey] = wkey; + sources[skey] = [ + Function(['require'], ( + // try to call default if defined to also support babel esmodule + // exports + 'var f = require(' + stringify(wkey) + ');' + + '(f.default ? f.default : f)(self);' + )), + scache + ]; + + var workerSources = {}; + resolveSources(skey); + + function resolveSources(key) { + workerSources[key] = true; + + for (var depPath in sources[key][1]) { + var depKey = sources[key][1][depPath]; + if (!workerSources[depKey]) { + resolveSources(depKey); + } + } + } + + var src = '(' + bundleFn + ')({' + + Object.keys(workerSources).map(function (key) { + return stringify(key) + ':[' + + sources[key][0] + + ',' + stringify(sources[key][1]) + ']' + ; + }).join(',') + + '},{},[' + stringify(skey) + '])' + ; + + var URL = window.URL || window.webkitURL || window.mozURL || window.msURL; + + var blob = new Blob([src], { type: 'text/javascript' }); + if (options && options.bare) { return blob; } + var workerUrl = URL.createObjectURL(blob); + var worker = new Worker(workerUrl); + worker.objectURL = workerUrl; + return worker; +}; + +},{}],1017:[function(require,module,exports){ +module.exports.RADIUS = 6378137; +module.exports.FLATTENING = 1/298.257223563; +module.exports.POLAR_RADIUS = 6356752.3142; + +},{}],1018:[function(require,module,exports){ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.WhooTS = global.WhooTS || {}))); +}(this, function (exports) { + +/** + * getURL + * + * @param {String} baseUrl Base url of the WMS server + * @param {String} layer Layer name + * @param {Number} x Tile coordinate x + * @param {Number} y Tile coordinate y + * @param {Number} z Tile zoom + * @param {Object} [options] + * @param {String} [options.format='image/png'] + * @param {String} [options.service='WMS'] + * @param {String} [options.version='1.1.1'] + * @param {String} [options.request='GetMap'] + * @param {String} [options.srs='EPSG:3857'] + * @param {Number} [options.width='256'] + * @param {Number} [options.height='256'] + * @returns {String} url + * @example + * var baseUrl = 'http://geodata.state.nj.us/imagerywms/Natural2015'; + * var layer = 'Natural2015'; + * var url = whoots.getURL(baseUrl, layer, 154308, 197167, 19); + */ +function getURL(baseUrl, layer, x, y, z, options) { + options = options || {}; + + var url = baseUrl + '?' + [ + 'bbox=' + getTileBBox(x, y, z), + 'format=' + (options.format || 'image/png'), + 'service=' + (options.service || 'WMS'), + 'version=' + (options.version || '1.1.1'), + 'request=' + (options.request || 'GetMap'), + 'srs=' + (options.srs || 'EPSG:3857'), + 'width=' + (options.width || 256), + 'height=' + (options.height || 256), + 'layers=' + layer + ].join('&'); + + return url; +} + + +/** + * getTileBBox + * + * @param {Number} x Tile coordinate x + * @param {Number} y Tile coordinate y + * @param {Number} z Tile zoom + * @returns {String} String of the bounding box + */ +function getTileBBox(x, y, z) { + // for Google/OSM tile scheme we need to alter the y + y = (Math.pow(2, z) - y - 1); + + var min = getMercCoords(x * 256, y * 256, z), + max = getMercCoords((x + 1) * 256, (y + 1) * 256, z); + + return min[0] + ',' + min[1] + ',' + max[0] + ',' + max[1]; +} + + +/** + * getMercCoords + * + * @param {Number} x Pixel coordinate x + * @param {Number} y Pixel coordinate y + * @param {Number} z Tile zoom + * @returns {Array} [x, y] + */ +function getMercCoords(x, y, z) { + var resolution = (2 * Math.PI * 6378137 / 256) / Math.pow(2, z), + merc_x = (x * resolution - 2 * Math.PI * 6378137 / 2.0), + merc_y = (y * resolution - 2 * Math.PI * 6378137 / 2.0); + + return [merc_x, merc_y]; +} + +exports.getURL = getURL; +exports.getTileBBox = getTileBBox; +exports.getMercCoords = getMercCoords; + +Object.defineProperty(exports, '__esModule', { value: true }); + +})); +},{}],1019:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Traditional Chinese calendar for jQuery v2.0.2. + Written by Nicolas Riesco (enquiries@nicolasriesco.net) December 2016. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +var gregorianCalendar = main.instance(); + +/** Implementation of the traditional Chinese calendar. + Source of calendar tables https://github.com/isee15/Lunar-Solar-Calendar-Converter . + @class ChineseCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function ChineseCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +ChineseCalendar.prototype = new main.baseCalendar; + +assign(ChineseCalendar.prototype, { + /** The calendar name. + @memberof ChineseCalendar */ + name: 'Chinese', + /** Julian date of start of Gregorian epoch: 1 January 0001 CE. + @memberof GregorianCalendar */ + jdEpoch: 1721425.5, + /** true if has a year zero, false if not. + @memberof ChineseCalendar */ + hasYearZero: false, + /** The minimum month number. + This calendar uses month indices to account for intercalary months. + @memberof ChineseCalendar */ + minMonth: 0, + /** The first month in the year. + This calendar uses month indices to account for intercalary months. + @memberof ChineseCalendar */ + firstMonth: 0, + /** The minimum day number. + @memberof ChineseCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof ChineseCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Chinese', + epochs: ['BEC', 'EC'], + monthNumbers: function(date, padded) { + if (typeof date === 'string') { + var match = date.match(MONTH_NUMBER_REGEXP); + return (match) ? match[0] : ''; + } + + var year = this._validateYear(date); + var monthIndex = date.month(); + + var month = '' + this.toChineseMonth(year, monthIndex); + + if (padded && month.length < 2) { + month = "0" + month; + } + + if (this.isIntercalaryMonth(year, monthIndex)) { + month += 'i'; + } + + return month; + }, + monthNames: function(date) { + if (typeof date === 'string') { + var match = date.match(MONTH_NAME_REGEXP); + return (match) ? match[0] : ''; + } + + var year = this._validateYear(date); + var monthIndex = date.month(); + + var month = this.toChineseMonth(year, monthIndex); + + var monthName = ['一月','二月','三月','四月','五月','六月', + '七月','八月','九月','十月','十一月','十二月'][month - 1]; + + if (this.isIntercalaryMonth(year, monthIndex)) { + monthName = '闰' + monthName; + } + + return monthName; + }, + monthNamesShort: function(date) { + if (typeof date === 'string') { + var match = date.match(MONTH_SHORT_NAME_REGEXP); + return (match) ? match[0] : ''; + } + + var year = this._validateYear(date); + var monthIndex = date.month(); + + var month = this.toChineseMonth(year, monthIndex); + + var monthName = ['一','二','三','四','五','六', + '七','八','九','十','十一','十二'][month - 1]; + + if (this.isIntercalaryMonth(year, monthIndex)) { + monthName = '闰' + monthName; + } + + return monthName; + }, + parseMonth: function(year, monthString) { + year = this._validateYear(year); + var month = parseInt(monthString); + var isIntercalary; + + if (!isNaN(month)) { + var i = monthString[monthString.length - 1]; + isIntercalary = (i === 'i' || i === 'I'); + } else { + if (monthString[0] === '闰') { + isIntercalary = true; + monthString = monthString.substring(1); + } + if (monthString[monthString.length - 1] === '月') { + monthString = monthString.substring(0, monthString.length - 1); + } + month = 1 + + ['一','二','三','四','五','六', + '七','八','九','十','十一','十二'].indexOf(monthString); + } + + var monthIndex = this.toMonthIndex(year, month, isIntercalary); + return monthIndex; + }, + dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], + digits: null, + dateFormat: 'yyyy/mm/dd', + firstDay: 1, + isRTL: false + } + }, + + /** Check that a candidate date is from the same calendar and is valid. + @memberof BaseCalendar + @private + @param year {CDate|number} The date or the year to validate. + @param error {string} Error message if invalid. + @return {number} The year. + @throws Error if year out of range. */ + _validateYear: function(year, error) { + if (year.year) { + year = year.year(); + } + + if (typeof year !== 'number' || year < 1888 || year > 2111) { + throw error.replace(/\{0\}/, this.local.name); + } + + return year; + }, + + /** Retrieve the month index (i.e. accounting for intercalary months). + @memberof ChineseCalendar + @param year {number} The year. + @param month {number} The month (1 for first month). + @param [isIntercalary=false] {boolean} If month is intercalary. + @return {number} The month index (0 for first month). + @throws Error if an invalid month/year or a different calendar used. */ + toMonthIndex: function(year, month, isIntercalary) { + // compute intercalary month in the year (0 if none) + var intercalaryMonth = this.intercalaryMonth(year); + + // validate month + var invalidIntercalaryMonth = + (isIntercalary && month !== intercalaryMonth); + if (invalidIntercalaryMonth || month < 1 || month > 12) { + throw main.local.invalidMonth + .replace(/\{0\}/, this.local.name); + } + + // compute month index + var monthIndex; + + if (!intercalaryMonth) { + monthIndex = month - 1; + } else if(!isIntercalary && month <= intercalaryMonth) { + monthIndex = month - 1; + } else { + monthIndex = month; + } + + return monthIndex; + }, + + /** Retrieve the month (i.e. accounting for intercalary months). + @memberof ChineseCalendar + @param year {CDate|number} The date or the year to examine. + @param monthIndex {number} The month index (0 for first month). + @return {number} The month (1 for first month). + @throws Error if an invalid month/year or a different calendar used. */ + toChineseMonth: function(year, monthIndex) { + if (year.year) { + year = year.year(); + monthIndex = year.month(); + } + + // compute intercalary month in the year (0 if none) + var intercalaryMonth = this.intercalaryMonth(year); + + // validate month + var maxMonthIndex = (intercalaryMonth) ? 12 : 11; + if (monthIndex < 0 || monthIndex > maxMonthIndex) { + throw main.local.invalidMonth + .replace(/\{0\}/, this.local.name); + } + + // compute Chinese month + var month; + + if (!intercalaryMonth) { + month = monthIndex + 1; + } else if(monthIndex < intercalaryMonth) { + month = monthIndex + 1; + } else { + month = monthIndex; + } + + return month; + }, + + /** Determine the intercalary month of a year (if any). + @memberof ChineseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The intercalary month number, or 0 if none. + @throws Error if an invalid year or a different calendar used. */ + intercalaryMonth: function(year) { + year = this._validateYear(year); + + var monthDaysTable = LUNAR_MONTH_DAYS[year - LUNAR_MONTH_DAYS[0]]; + var intercalaryMonth = monthDaysTable >> 13; + + return intercalaryMonth; + }, + + /** Determine whether this date is an intercalary month. + @memberof ChineseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [monthIndex] {number} The month index to examine. + @return {boolean} true if this is an intercalary month, false if not. + @throws Error if an invalid year or a different calendar used. */ + isIntercalaryMonth: function(year, monthIndex) { + if (year.year) { + year = year.year(); + monthIndex = year.month(); + } + + var intercalaryMonth = this.intercalaryMonth(year); + + return !!intercalaryMonth && intercalaryMonth === monthIndex; + }, + + /** Determine whether this date is in a leap year. + @memberof ChineseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + return (this.intercalaryMonth(year) !== 0); + }, + + /** Determine the week of the year for a date - ISO 8601. + @memberof ChineseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [monthIndex] {number} The month index to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, monthIndex, day) { + // compute Chinese new year + var validatedYear = + this._validateYear(year, main.local.invalidyear); + var packedDate = + CHINESE_NEW_YEAR[validatedYear - CHINESE_NEW_YEAR[0]]; + + var y = (packedDate >> 9) & 0xFFF; + var m = (packedDate >> 5) & 0x0F; + var d = packedDate & 0x1F; + + // find first Thrusday of the year + var firstThursday; + firstThursday = gregorianCalendar.newDate(y, m, d); + firstThursday.add(4 - (firstThursday.dayOfWeek() || 7), 'd'); + + // compute days from first Thursday + var offset = + this.toJD(year, monthIndex, day) - firstThursday.toJD(); + return 1 + Math.floor(offset / 7); + }, + + /** Retrieve the number of months in a year. + @memberof ChineseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of months. + @throws Error if an invalid year or a different calendar used. */ + monthsInYear: function(year) { + return (this.leapYear(year)) ? 13 : 12; + }, + + /** Retrieve the number of days in a month. + @memberof ChineseCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [monthIndex] {number} The month index. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, monthIndex) { + if (year.year) { + monthIndex = year.month(); + year = year.year(); + } + + year = this._validateYear(year); + + var monthDaysTable = LUNAR_MONTH_DAYS[year - LUNAR_MONTH_DAYS[0]]; + + var intercalaryMonth = monthDaysTable >> 13; + var maxMonthIndex = (intercalaryMonth) ? 12 : 11; + if (monthIndex > maxMonthIndex) { + throw main.local.invalidMonth + .replace(/\{0\}/, this.local.name); + } + + var daysInMonth = (monthDaysTable & (1 << (12 - monthIndex))) ? + 30 : 29; + + return daysInMonth; + }, + + /** Determine whether this date is a week day. + @memberof ChineseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [monthIndex] {number} The month index to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, monthIndex, day) { + return (this.dayOfWeek(year, monthIndex, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof ChineseCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [monthIndex] {number} The month index to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, monthIndex, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = this._validateYear(date.year()); + monthIndex = date.month(); + day = date.day(); + + var isIntercalary = this.isIntercalaryMonth(year, monthIndex); + var month = this.toChineseMonth(year, monthIndex); + + var solar = toSolar(year, month, day, isIntercalary); + + return gregorianCalendar.toJD(solar.year, solar.month, solar.day); + }, + + /** Create a new date from a Julian date. + @memberof ChineseCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + var date = gregorianCalendar.fromJD(jd); + var lunar = toLunar(date.year(), date.month(), date.day()); + var monthIndex = this.toMonthIndex( + lunar.year, lunar.month, lunar.isIntercalary); + return this.newDate(lunar.year, monthIndex, lunar.day); + }, + + /** Create a new date from a string. + @memberof ChineseCalendar + @param dateString {string} String representing a Chinese date + @return {CDate} The new date. + @throws Error if an invalid date. */ + fromString: function(dateString) { + var match = dateString.match(DATE_REGEXP); + + var year = this._validateYear(+match[1]); + + var month = +match[2]; + var isIntercalary = !!match[3]; + var monthIndex = this.toMonthIndex(year, month, isIntercalary); + + var day = +match[4]; + + return this.newDate(year, monthIndex, day); + }, + + /** Add period(s) to a date. + Cater for no year zero. + @memberof ChineseCalendar + @param date {CDate} The starting date. + @param offset {number} The number of periods to adjust by. + @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. + @return {CDate} The updated date. + @throws Error if a different calendar used. */ + add: function(date, offset, period) { + var year = date.year(); + var monthIndex = date.month(); + var isIntercalary = this.isIntercalaryMonth(year, monthIndex); + var month = this.toChineseMonth(year, monthIndex); + + var cdate = Object.getPrototypeOf(ChineseCalendar.prototype) + .add.call(this, date, offset, period); + + if (period === 'y') { + // Resync month + var resultYear = cdate.year(); + var resultMonthIndex = cdate.month(); + + // Using the fact the month index of an intercalary month + // equals its month number: + var resultCanBeIntercalaryMonth = + this.isIntercalaryMonth(resultYear, month); + + var correctedMonthIndex = + (isIntercalary && resultCanBeIntercalaryMonth) ? + this.toMonthIndex(resultYear, month, true) : + this.toMonthIndex(resultYear, month, false); + + if (correctedMonthIndex !== resultMonthIndex) { + cdate.month(correctedMonthIndex); + } + } + + return cdate; + }, +}); + +// Used by ChineseCalendar.prototype.fromString +var DATE_REGEXP = /^\s*(-?\d\d\d\d|\d\d)[-/](\d?\d)([iI]?)[-/](\d?\d)/m; +var MONTH_NUMBER_REGEXP = /^\d?\d[iI]?/m; +var MONTH_NAME_REGEXP = /^闰?十?[一二三四五六七八九]?月/m; +var MONTH_SHORT_NAME_REGEXP = /^闰?十?[一二三四五六七八九]?/m; + +// Chinese calendar implementation +main.calendars.chinese = ChineseCalendar; + +// Chinese calendar tables from year 1888 to 2111 +// +// Source: +// https://github.com/isee15/Lunar-Solar-Calendar-Converter.git + +// Table of intercalary months and days per month from year 1888 to 2111 +// +// bit (12 - i): days in the i^th month +// (= 0 if i^th lunar month has 29 days) +// (= 1 if i^th lunar month has 30 days) +// (first month in lunar year is i = 0) +// bits (13,14,15,16): intercalary month +// (= 0 if lunar year has no intercalary month) +var LUNAR_MONTH_DAYS = [1887, 0x1694, 0x16aa, 0x4ad5, + 0xab6, 0xc4b7, 0x4ae, 0xa56, 0xb52a, 0x1d2a, 0xd54, 0x75aa, 0x156a, + 0x1096d, 0x95c, 0x14ae, 0xaa4d, 0x1a4c, 0x1b2a, 0x8d55, 0xad4, + 0x135a, 0x495d, 0x95c, 0xd49b, 0x149a, 0x1a4a, 0xbaa5, 0x16a8, + 0x1ad4, 0x52da, 0x12b6, 0xe937, 0x92e, 0x1496, 0xb64b, 0xd4a, + 0xda8, 0x95b5, 0x56c, 0x12ae, 0x492f, 0x92e, 0xcc96, 0x1a94, + 0x1d4a, 0xada9, 0xb5a, 0x56c, 0x726e, 0x125c, 0xf92d, 0x192a, + 0x1a94, 0xdb4a, 0x16aa, 0xad4, 0x955b, 0x4ba, 0x125a, 0x592b, + 0x152a, 0xf695, 0xd94, 0x16aa, 0xaab5, 0x9b4, 0x14b6, 0x6a57, + 0xa56, 0x1152a, 0x1d2a, 0xd54, 0xd5aa, 0x156a, 0x96c, 0x94ae, + 0x14ae, 0xa4c, 0x7d26, 0x1b2a, 0xeb55, 0xad4, 0x12da, 0xa95d, + 0x95a, 0x149a, 0x9a4d, 0x1a4a, 0x11aa5, 0x16a8, 0x16d4, 0xd2da, + 0x12b6, 0x936, 0x9497, 0x1496, 0x1564b, 0xd4a, 0xda8, 0xd5b4, + 0x156c, 0x12ae, 0xa92f, 0x92e, 0xc96, 0x6d4a, 0x1d4a, 0x10d65, + 0xb58, 0x156c, 0xb26d, 0x125c, 0x192c, 0x9a95, 0x1a94, 0x1b4a, + 0x4b55, 0xad4, 0xf55b, 0x4ba, 0x125a, 0xb92b, 0x152a, 0x1694, + 0x96aa, 0x15aa, 0x12ab5, 0x974, 0x14b6, 0xca57, 0xa56, 0x1526, + 0x8e95, 0xd54, 0x15aa, 0x49b5, 0x96c, 0xd4ae, 0x149c, 0x1a4c, + 0xbd26, 0x1aa6, 0xb54, 0x6d6a, 0x12da, 0x1695d, 0x95a, 0x149a, + 0xda4b, 0x1a4a, 0x1aa4, 0xbb54, 0x16b4, 0xada, 0x495b, 0x936, + 0xf497, 0x1496, 0x154a, 0xb6a5, 0xda4, 0x15b4, 0x6ab6, 0x126e, + 0x1092f, 0x92e, 0xc96, 0xcd4a, 0x1d4a, 0xd64, 0x956c, 0x155c, + 0x125c, 0x792e, 0x192c, 0xfa95, 0x1a94, 0x1b4a, 0xab55, 0xad4, + 0x14da, 0x8a5d, 0xa5a, 0x1152b, 0x152a, 0x1694, 0xd6aa, 0x15aa, + 0xab4, 0x94ba, 0x14b6, 0xa56, 0x7527, 0xd26, 0xee53, 0xd54, 0x15aa, + 0xa9b5, 0x96c, 0x14ae, 0x8a4e, 0x1a4c, 0x11d26, 0x1aa4, 0x1b54, + 0xcd6a, 0xada, 0x95c, 0x949d, 0x149a, 0x1a2a, 0x5b25, 0x1aa4, + 0xfb52, 0x16b4, 0xaba, 0xa95b, 0x936, 0x1496, 0x9a4b, 0x154a, + 0x136a5, 0xda4, 0x15ac]; + +// Table of Chinese New Years from year 1888 to 2111 +// +// bits (0 to 4): solar day +// bits (5 to 8): solar month +// bits (9 to 20): solar year +var CHINESE_NEW_YEAR = [1887, 0xec04c, 0xec23f, 0xec435, 0xec649, + 0xec83e, 0xeca51, 0xecc46, 0xece3a, 0xed04d, 0xed242, 0xed436, + 0xed64a, 0xed83f, 0xeda53, 0xedc48, 0xede3d, 0xee050, 0xee244, + 0xee439, 0xee64d, 0xee842, 0xeea36, 0xeec4a, 0xeee3e, 0xef052, + 0xef246, 0xef43a, 0xef64e, 0xef843, 0xefa37, 0xefc4b, 0xefe41, + 0xf0054, 0xf0248, 0xf043c, 0xf0650, 0xf0845, 0xf0a38, 0xf0c4d, + 0xf0e42, 0xf1037, 0xf124a, 0xf143e, 0xf1651, 0xf1846, 0xf1a3a, + 0xf1c4e, 0xf1e44, 0xf2038, 0xf224b, 0xf243f, 0xf2653, 0xf2848, + 0xf2a3b, 0xf2c4f, 0xf2e45, 0xf3039, 0xf324d, 0xf3442, 0xf3636, + 0xf384a, 0xf3a3d, 0xf3c51, 0xf3e46, 0xf403b, 0xf424e, 0xf4443, + 0xf4638, 0xf484c, 0xf4a3f, 0xf4c52, 0xf4e48, 0xf503c, 0xf524f, + 0xf5445, 0xf5639, 0xf584d, 0xf5a42, 0xf5c35, 0xf5e49, 0xf603e, + 0xf6251, 0xf6446, 0xf663b, 0xf684f, 0xf6a43, 0xf6c37, 0xf6e4b, + 0xf703f, 0xf7252, 0xf7447, 0xf763c, 0xf7850, 0xf7a45, 0xf7c39, + 0xf7e4d, 0xf8042, 0xf8254, 0xf8449, 0xf863d, 0xf8851, 0xf8a46, + 0xf8c3b, 0xf8e4f, 0xf9044, 0xf9237, 0xf944a, 0xf963f, 0xf9853, + 0xf9a47, 0xf9c3c, 0xf9e50, 0xfa045, 0xfa238, 0xfa44c, 0xfa641, + 0xfa836, 0xfaa49, 0xfac3d, 0xfae52, 0xfb047, 0xfb23a, 0xfb44e, + 0xfb643, 0xfb837, 0xfba4a, 0xfbc3f, 0xfbe53, 0xfc048, 0xfc23c, + 0xfc450, 0xfc645, 0xfc839, 0xfca4c, 0xfcc41, 0xfce36, 0xfd04a, + 0xfd23d, 0xfd451, 0xfd646, 0xfd83a, 0xfda4d, 0xfdc43, 0xfde37, + 0xfe04b, 0xfe23f, 0xfe453, 0xfe648, 0xfe83c, 0xfea4f, 0xfec44, + 0xfee38, 0xff04c, 0xff241, 0xff436, 0xff64a, 0xff83e, 0xffa51, + 0xffc46, 0xffe3a, 0x10004e, 0x100242, 0x100437, 0x10064b, 0x100841, + 0x100a53, 0x100c48, 0x100e3c, 0x10104f, 0x101244, 0x101438, + 0x10164c, 0x101842, 0x101a35, 0x101c49, 0x101e3d, 0x102051, + 0x102245, 0x10243a, 0x10264e, 0x102843, 0x102a37, 0x102c4b, + 0x102e3f, 0x103053, 0x103247, 0x10343b, 0x10364f, 0x103845, + 0x103a38, 0x103c4c, 0x103e42, 0x104036, 0x104249, 0x10443d, + 0x104651, 0x104846, 0x104a3a, 0x104c4e, 0x104e43, 0x105038, + 0x10524a, 0x10543e, 0x105652, 0x105847, 0x105a3b, 0x105c4f, + 0x105e45, 0x106039, 0x10624c, 0x106441, 0x106635, 0x106849, + 0x106a3d, 0x106c51, 0x106e47, 0x10703c, 0x10724f, 0x107444, + 0x107638, 0x10784c, 0x107a3f, 0x107c53, 0x107e48]; + +function toLunar(yearOrDate, monthOrResult, day, result) { + var solarDate; + var lunarDate; + + if(typeof yearOrDate === 'object') { + solarDate = yearOrDate; + lunarDate = monthOrResult || {}; + + } else { + var isValidYear = (typeof yearOrDate === 'number') && + (yearOrDate >= 1888) && (yearOrDate <= 2111); + if(!isValidYear) + throw new Error("Solar year outside range 1888-2111"); + + var isValidMonth = (typeof monthOrResult === 'number') && + (monthOrResult >= 1) && (monthOrResult <= 12); + if(!isValidMonth) + throw new Error("Solar month outside range 1 - 12"); + + var isValidDay = (typeof day === 'number') && (day >= 1) && (day <= 31); + if(!isValidDay) + throw new Error("Solar day outside range 1 - 31"); + + solarDate = { + year: yearOrDate, + month: monthOrResult, + day: day, + }; + lunarDate = result || {}; + } + + // Compute Chinese new year and lunar year + var chineseNewYearPackedDate = + CHINESE_NEW_YEAR[solarDate.year - CHINESE_NEW_YEAR[0]]; + + var packedDate = (solarDate.year << 9) | (solarDate.month << 5) + | solarDate.day; + + lunarDate.year = (packedDate >= chineseNewYearPackedDate) ? + solarDate.year : + solarDate.year - 1; + + chineseNewYearPackedDate = + CHINESE_NEW_YEAR[lunarDate.year - CHINESE_NEW_YEAR[0]]; + + var y = (chineseNewYearPackedDate >> 9) & 0xFFF; + var m = (chineseNewYearPackedDate >> 5) & 0x0F; + var d = chineseNewYearPackedDate & 0x1F; + + // Compute days from new year + var daysFromNewYear; + + var chineseNewYearJSDate = new Date(y, m -1, d); + var jsDate = new Date(solarDate.year, solarDate.month - 1, solarDate.day); + + daysFromNewYear = Math.round( + (jsDate - chineseNewYearJSDate) / (24 * 3600 * 1000)); + + // Compute lunar month and day + var monthDaysTable = LUNAR_MONTH_DAYS[lunarDate.year - LUNAR_MONTH_DAYS[0]]; + + var i; + for(i = 0; i < 13; i++) { + var daysInMonth = (monthDaysTable & (1 << (12 - i))) ? 30 : 29; + + if (daysFromNewYear < daysInMonth) { + break; + } + + daysFromNewYear -= daysInMonth; + } + + var intercalaryMonth = monthDaysTable >> 13; + if (!intercalaryMonth || i < intercalaryMonth) { + lunarDate.isIntercalary = false; + lunarDate.month = 1 + i; + } else if (i === intercalaryMonth) { + lunarDate.isIntercalary = true; + lunarDate.month = i; + } else { + lunarDate.isIntercalary = false; + lunarDate.month = i; + } + + lunarDate.day = 1 + daysFromNewYear; + + return lunarDate; +} + +function toSolar(yearOrDate, monthOrResult, day, isIntercalaryOrResult, result) { + var solarDate; + var lunarDate; + + if(typeof yearOrDate === 'object') { + lunarDate = yearOrDate; + solarDate = monthOrResult || {}; + + } else { + var isValidYear = (typeof yearOrDate === 'number') && + (yearOrDate >= 1888) && (yearOrDate <= 2111); + if(!isValidYear) + throw new Error("Lunar year outside range 1888-2111"); + + var isValidMonth = (typeof monthOrResult === 'number') && + (monthOrResult >= 1) && (monthOrResult <= 12); + if(!isValidMonth) + throw new Error("Lunar month outside range 1 - 12"); + + var isValidDay = (typeof day === 'number') && (day >= 1) && (day <= 30); + if(!isValidDay) + throw new Error("Lunar day outside range 1 - 30"); + + var isIntercalary; + if(typeof isIntercalaryOrResult === 'object') { + isIntercalary = false; + solarDate = isIntercalaryOrResult; + } else { + isIntercalary = !!isIntercalaryOrResult; + solarDate = result || {}; + } + + lunarDate = { + year: yearOrDate, + month: monthOrResult, + day: day, + isIntercalary: isIntercalary, + }; + } + + // Compute days from new year + var daysFromNewYear; + + daysFromNewYear = lunarDate.day - 1; + + var monthDaysTable = LUNAR_MONTH_DAYS[lunarDate.year - LUNAR_MONTH_DAYS[0]]; + var intercalaryMonth = monthDaysTable >> 13; + + var monthsFromNewYear; + if (!intercalaryMonth) { + monthsFromNewYear = lunarDate.month - 1; + } else if (lunarDate.month > intercalaryMonth) { + monthsFromNewYear = lunarDate.month; + } else if (lunarDate.isIntercalary) { + monthsFromNewYear = lunarDate.month; + } else { + monthsFromNewYear = lunarDate.month - 1; + } + + for(var i = 0; i < monthsFromNewYear; i++) { + var daysInMonth = (monthDaysTable & (1 << (12 - i))) ? 30 : 29; + daysFromNewYear += daysInMonth; + } + + // Compute Chinese new year + var packedDate = CHINESE_NEW_YEAR[lunarDate.year - CHINESE_NEW_YEAR[0]]; + + var y = (packedDate >> 9) & 0xFFF; + var m = (packedDate >> 5) & 0x0F; + var d = packedDate & 0x1F; + + // Compute solar date + var jsDate = new Date(y, m - 1, d + daysFromNewYear); + + solarDate.year = jsDate.getFullYear(); + solarDate.month = 1 + jsDate.getMonth(); + solarDate.day = jsDate.getDate(); + + return solarDate; +} + + +},{"../main":1033,"object-assign":446}],1020:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Coptic calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Coptic calendar. + See http://en.wikipedia.org/wiki/Coptic_calendar. + See also Calendrical Calculations: The Millennium Edition + (http://emr.cs.iit.edu/home/reingold/calendar-book/index.shtml). + @class CopticCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function CopticCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +CopticCalendar.prototype = new main.baseCalendar; + +assign(CopticCalendar.prototype, { + /** The calendar name. + @memberof CopticCalendar */ + name: 'Coptic', + /** Julian date of start of Coptic epoch: 29 August 284 CE (Gregorian). + @memberof CopticCalendar */ + jdEpoch: 1825029.5, + /** Days per month in a common year. + @memberof CopticCalendar */ + daysPerMonth: [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 5], + /** true if has a year zero, false if not. + @memberof CopticCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof CopticCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof CopticCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof CopticCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof CopticCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Coptic', + epochs: ['BAM', 'AM'], + monthNames: ['Thout', 'Paopi', 'Hathor', 'Koiak', 'Tobi', 'Meshir', + 'Paremhat', 'Paremoude', 'Pashons', 'Paoni', 'Epip', 'Mesori', 'Pi Kogi Enavot'], + monthNamesShort: ['Tho', 'Pao', 'Hath', 'Koi', 'Tob', 'Mesh', + 'Pat', 'Pad', 'Pash', 'Pao', 'Epi', 'Meso', 'PiK'], + dayNames: ['Tkyriaka', 'Pesnau', 'Pshoment', 'Peftoou', 'Ptiou', 'Psoou', 'Psabbaton'], + dayNamesShort: ['Tky', 'Pes', 'Psh', 'Pef', 'Pti', 'Pso', 'Psa'], + dayNamesMin: ['Tk', 'Pes', 'Psh', 'Pef', 'Pt', 'Pso', 'Psa'], + digits: null, + dateFormat: 'dd/mm/yyyy', + firstDay: 0, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof CopticCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + var year = date.year() + (date.year() < 0 ? 1 : 0); // No year zero + return year % 4 === 3 || year % 4 === -1; + }, + + /** Retrieve the number of months in a year. + @memberof CopticCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of months. + @throws Error if an invalid year or a different calendar used. */ + monthsInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, + main.local.invalidYear || main.regionalOptions[''].invalidYear); + return 13; + }, + + /** Determine the week of the year for a date. + @memberof CopticCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number) the month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Sunday of this week starting on Sunday + var checkDate = this.newDate(year, month, day); + checkDate.add(-checkDate.dayOfWeek(), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a month. + @memberof CopticCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 13 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof CopticCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param month {number} The month to examine. + @param day {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return (this.dayOfWeek(year, month, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof CopticCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number) the month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = date.year(); + if (year < 0) { year++; } // No year zero + return date.day() + (date.month() - 1) * 30 + + (year - 1) * 365 + Math.floor(year / 4) + this.jdEpoch - 1; + }, + + /** Create a new date from a Julian date. + @memberof CopticCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + var c = Math.floor(jd) + 0.5 - this.jdEpoch; + var year = Math.floor((c - Math.floor((c + 366) / 1461)) / 365) + 1; + if (year <= 0) { year--; } // No year zero + c = Math.floor(jd) + 0.5 - this.newDate(year, 1, 1).toJD(); + var month = Math.floor(c / 30) + 1; + var day = c - (month - 1) * 30 + 1; + return this.newDate(year, month, day); + } +}); + +// Coptic calendar implementation +main.calendars.coptic = CopticCalendar; + + +},{"../main":1033,"object-assign":446}],1021:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Discworld calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) January 2016. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Discworld calendar - Unseen University version. + See also http://wiki.lspace.org/mediawiki/Discworld_calendar + and http://discworld.wikia.com/wiki/Discworld_calendar. + @class DiscworldCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function DiscworldCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +DiscworldCalendar.prototype = new main.baseCalendar; + +assign(DiscworldCalendar.prototype, { + /** The calendar name. + @memberof DiscworldCalendar */ + name: 'Discworld', + /** Julian date of start of Discworld epoch: 1 January 0001 CE. + @memberof DiscworldCalendar */ + jdEpoch: 1721425.5, + /** Days per month in a common year. + @memberof DiscworldCalendar */ + daysPerMonth: [16, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32], + /** true if has a year zero, false if not. + @memberof DiscworldCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof DiscworldCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof DiscworldCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof DiscworldCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof DiscworldCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Discworld', + epochs: ['BUC', 'UC'], + monthNames: ['Ick', 'Offle', 'February', 'March', 'April', 'May', 'June', + 'Grune', 'August', 'Spune', 'Sektober', 'Ember', 'December'], + monthNamesShort: ['Ick', 'Off', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Gru', 'Aug', 'Spu', 'Sek', 'Emb', 'Dec'], + dayNames: ['Sunday', 'Octeday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + dayNamesShort: ['Sun', 'Oct', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + dayNamesMin: ['Su', 'Oc', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], + digits: null, + dateFormat: 'yyyy/mm/dd', + firstDay: 2, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return false; + }, + + /** Retrieve the number of months in a year. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of months. + @throws Error if an invalid year or a different calendar used. */ + monthsInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return 13; + }, + + /** Retrieve the number of days in a year. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of days. + @throws Error if an invalid year or a different calendar used. */ + daysInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return 400; + }, + + /** Determine the week of the year for a date. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Sunday of this week starting on Sunday + var checkDate = this.newDate(year, month, day); + checkDate.add(-checkDate.dayOfWeek(), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 8) + 1; + }, + + /** Retrieve the number of days in a month. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1]; + }, + + /** Retrieve the number of days in a week. + @memberof DiscworldCalendar + @return {number} The number of days. */ + daysInWeek: function() { + return 8; + }, + + /** Retrieve the day of the week for a date. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The day of the week: 0 to number of days - 1. + @throws Error if an invalid date or a different calendar used. */ + dayOfWeek: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + return (date.day() + 1) % 8; + }, + + /** Determine whether this date is a week day. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + var dow = this.dayOfWeek(year, month, day); + return (dow >= 2 && dow <= 6); + }, + + /** Retrieve additional information about a date. + @memberof DiscworldCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {object} Additional information - contents depends on calendar. + @throws Error if an invalid date or a different calendar used. */ + extraInfo: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + return {century: centuries[Math.floor((date.year() - 1) / 100) + 1] || ''}; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof DiscworldCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = date.year() + (date.year() < 0 ? 1 : 0); + month = date.month(); + day = date.day(); + return day + (month > 1 ? 16 : 0) + (month > 2 ? (month - 2) * 32 : 0) + + (year - 1) * 400 + this.jdEpoch - 1; + }, + + /** Create a new date from a Julian date. + @memberof DiscworldCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + jd = Math.floor(jd + 0.5) - Math.floor(this.jdEpoch) - 1; + var year = Math.floor(jd / 400) + 1; + jd -= (year - 1) * 400; + jd += (jd > 15 ? 16 : 0); + var month = Math.floor(jd / 32) + 1; + var day = jd - (month - 1) * 32 + 1; + return this.newDate(year <= 0 ? year - 1 : year, month, day); + } +}); + +// Names of the centuries +var centuries = { + 20: 'Fruitbat', + 21: 'Anchovy' +}; + +// Discworld calendar implementation +main.calendars.discworld = DiscworldCalendar; + + +},{"../main":1033,"object-assign":446}],1022:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Ethiopian calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Ethiopian calendar. + See http://en.wikipedia.org/wiki/Ethiopian_calendar. + See also Calendrical Calculations: The Millennium Edition + (http://emr.cs.iit.edu/home/reingold/calendar-book/index.shtml). + @class EthiopianCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function EthiopianCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +EthiopianCalendar.prototype = new main.baseCalendar; + +assign(EthiopianCalendar.prototype, { + /** The calendar name. + @memberof EthiopianCalendar */ + name: 'Ethiopian', + /** Julian date of start of Ethiopian epoch: 27 August 8 CE (Gregorian). + @memberof EthiopianCalendar */ + jdEpoch: 1724220.5, + /** Days per month in a common year. + @memberof EthiopianCalendar */ + daysPerMonth: [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 5], + /** true if has a year zero, false if not. + @memberof EthiopianCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof EthiopianCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof EthiopianCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof EthiopianCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof EthiopianCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Ethiopian', + epochs: ['BEE', 'EE'], + monthNames: ['Meskerem', 'Tikemet', 'Hidar', 'Tahesas', 'Tir', 'Yekatit', + 'Megabit', 'Miazia', 'Genbot', 'Sene', 'Hamle', 'Nehase', 'Pagume'], + monthNamesShort: ['Mes', 'Tik', 'Hid', 'Tah', 'Tir', 'Yek', + 'Meg', 'Mia', 'Gen', 'Sen', 'Ham', 'Neh', 'Pag'], + dayNames: ['Ehud', 'Segno', 'Maksegno', 'Irob', 'Hamus', 'Arb', 'Kidame'], + dayNamesShort: ['Ehu', 'Seg', 'Mak', 'Iro', 'Ham', 'Arb', 'Kid'], + dayNamesMin: ['Eh', 'Se', 'Ma', 'Ir', 'Ha', 'Ar', 'Ki'], + digits: null, + dateFormat: 'dd/mm/yyyy', + firstDay: 0, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof EthiopianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + var year = date.year() + (date.year() < 0 ? 1 : 0); // No year zero + return year % 4 === 3 || year % 4 === -1; + }, + + /** Retrieve the number of months in a year. + @memberof EthiopianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of months. + @throws Error if an invalid year or a different calendar used. */ + monthsInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, + main.local.invalidYear || main.regionalOptions[''].invalidYear); + return 13; + }, + + /** Determine the week of the year for a date. + @memberof EthiopianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Sunday of this week starting on Sunday + var checkDate = this.newDate(year, month, day); + checkDate.add(-checkDate.dayOfWeek(), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a month. + @memberof EthiopianCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 13 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof EthiopianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return (this.dayOfWeek(year, month, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof EthiopianCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = date.year(); + if (year < 0) { year++; } // No year zero + return date.day() + (date.month() - 1) * 30 + + (year - 1) * 365 + Math.floor(year / 4) + this.jdEpoch - 1; + }, + + /** Create a new date from a Julian date. + @memberof EthiopianCalendar + @param jd {number} the Julian date to convert. + @return {CDate} the equivalent date. */ + fromJD: function(jd) { + var c = Math.floor(jd) + 0.5 - this.jdEpoch; + var year = Math.floor((c - Math.floor((c + 366) / 1461)) / 365) + 1; + if (year <= 0) { year--; } // No year zero + c = Math.floor(jd) + 0.5 - this.newDate(year, 1, 1).toJD(); + var month = Math.floor(c / 30) + 1; + var day = c - (month - 1) * 30 + 1; + return this.newDate(year, month, day); + } +}); + +// Ethiopian calendar implementation +main.calendars.ethiopian = EthiopianCalendar; + + +},{"../main":1033,"object-assign":446}],1023:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Hebrew calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Hebrew civil calendar. + Based on code from http://www.fourmilab.ch/documents/calendar/. + See also http://en.wikipedia.org/wiki/Hebrew_calendar. + @class HebrewCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function HebrewCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +HebrewCalendar.prototype = new main.baseCalendar; + +assign(HebrewCalendar.prototype, { + /** The calendar name. + @memberof HebrewCalendar */ + name: 'Hebrew', + /** Julian date of start of Hebrew epoch: 7 October 3761 BCE. + @memberof HebrewCalendar */ + jdEpoch: 347995.5, + /** Days per month in a common year. + @memberof HebrewCalendar */ + daysPerMonth: [30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 29], + /** true if has a year zero, false if not. + @memberof HebrewCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof HebrewCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof HebrewCalendar */ + firstMonth: 7, + /** The minimum day number. + @memberof HebrewCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof HebrewCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Hebrew', + epochs: ['BAM', 'AM'], + monthNames: ['Nisan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul', + 'Tishrei', 'Cheshvan', 'Kislev', 'Tevet', 'Shevat', 'Adar', 'Adar II'], + monthNamesShort: ['Nis', 'Iya', 'Siv', 'Tam', 'Av', 'Elu', 'Tis', 'Che', 'Kis', 'Tev', 'She', 'Ada', 'Ad2'], + dayNames: ['Yom Rishon', 'Yom Sheni', 'Yom Shlishi', 'Yom Revi\'i', 'Yom Chamishi', 'Yom Shishi', 'Yom Shabbat'], + dayNamesShort: ['Ris', 'She', 'Shl', 'Rev', 'Cha', 'Shi', 'Sha'], + dayNamesMin: ['Ri','She','Shl','Re','Ch','Shi','Sha'], + digits: null, + dateFormat: 'dd/mm/yyyy', + firstDay: 0, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof HebrewCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return this._leapYear(date.year()); + }, + + /** Determine whether this date is in a leap year. + @memberof HebrewCalendar + @private + @param year {number} The year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + _leapYear: function(year) { + year = (year < 0 ? year + 1 : year); + return mod(year * 7 + 1, 19) < 7; + }, + + /** Retrieve the number of months in a year. + @memberof HebrewCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of months. + @throws Error if an invalid year or a different calendar used. */ + monthsInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return this._leapYear(year.year ? year.year() : year) ? 13 : 12; + }, + + /** Determine the week of the year for a date. + @memberof HebrewCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Sunday of this week starting on Sunday + var checkDate = this.newDate(year, month, day); + checkDate.add(-checkDate.dayOfWeek(), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a year. + @memberof HebrewCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of days. + @throws Error if an invalid year or a different calendar used. */ + daysInYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + year = date.year(); + return this.toJD((year === -1 ? +1 : year + 1), 7, 1) - this.toJD(year, 7, 1); + }, + + /** Retrieve the number of days in a month. + @memberof HebrewCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + if (year.year) { + month = year.month(); + year = year.year(); + } + this._validate(year, month, this.minDay, main.local.invalidMonth); + return (month === 12 && this.leapYear(year) ? 30 : // Adar I + (month === 8 && mod(this.daysInYear(year), 10) === 5 ? 30 : // Cheshvan in shlemah year + (month === 9 && mod(this.daysInYear(year), 10) === 3 ? 29 : // Kislev in chaserah year + this.daysPerMonth[month - 1]))); + }, + + /** Determine whether this date is a week day. + @memberof HebrewCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return this.dayOfWeek(year, month, day) !== 6; + }, + + /** Retrieve additional information about a date - year type. + @memberof HebrewCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {object} Additional information - contents depends on calendar. + @throws Error if an invalid date or a different calendar used. */ + extraInfo: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + return {yearType: (this.leapYear(date) ? 'embolismic' : 'common') + ' ' + + ['deficient', 'regular', 'complete'][this.daysInYear(date) % 10 - 3]}; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof HebrewCalendar + @param year {CDate)|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = date.year(); + month = date.month(); + day = date.day(); + var adjYear = (year <= 0 ? year + 1 : year); + var jd = this.jdEpoch + this._delay1(adjYear) + + this._delay2(adjYear) + day + 1; + if (month < 7) { + for (var m = 7; m <= this.monthsInYear(year); m++) { + jd += this.daysInMonth(year, m); + } + for (var m = 1; m < month; m++) { + jd += this.daysInMonth(year, m); + } + } + else { + for (var m = 7; m < month; m++) { + jd += this.daysInMonth(year, m); + } + } + return jd; + }, + + /** Test for delay of start of new year and to avoid + Sunday, Wednesday, or Friday as start of the new year. + @memberof HebrewCalendar + @private + @param year {number} The year to examine. + @return {number} The days to offset by. */ + _delay1: function(year) { + var months = Math.floor((235 * year - 234) / 19); + var parts = 12084 + 13753 * months; + var day = months * 29 + Math.floor(parts / 25920); + if (mod(3 * (day + 1), 7) < 3) { + day++; + } + return day; + }, + + /** Check for delay in start of new year due to length of adjacent years. + @memberof HebrewCalendar + @private + @param year {number} The year to examine. + @return {number} The days to offset by. */ + _delay2: function(year) { + var last = this._delay1(year - 1); + var present = this._delay1(year); + var next = this._delay1(year + 1); + return ((next - present) === 356 ? 2 : ((present - last) === 382 ? 1 : 0)); + }, + + /** Create a new date from a Julian date. + @memberof HebrewCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + jd = Math.floor(jd) + 0.5; + var year = Math.floor(((jd - this.jdEpoch) * 98496.0) / 35975351.0) - 1; + while (jd >= this.toJD((year === -1 ? +1 : year + 1), 7, 1)) { + year++; + } + var month = (jd < this.toJD(year, 1, 1)) ? 7 : 1; + while (jd > this.toJD(year, month, this.daysInMonth(year, month))) { + month++; + } + var day = jd - this.toJD(year, month, 1) + 1; + return this.newDate(year, month, day); + } +}); + +// Modulus function which works for non-integers. +function mod(a, b) { + return a - (b * Math.floor(a / b)); +} + +// Hebrew calendar implementation +main.calendars.hebrew = HebrewCalendar; + + +},{"../main":1033,"object-assign":446}],1024:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Islamic calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Islamic or '16 civil' calendar. + Based on code from http://www.iranchamber.com/calendar/converter/iranian_calendar_converter.php. + See also http://en.wikipedia.org/wiki/Islamic_calendar. + @class IslamicCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function IslamicCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +IslamicCalendar.prototype = new main.baseCalendar; + +assign(IslamicCalendar.prototype, { + /** The calendar name. + @memberof IslamicCalendar */ + name: 'Islamic', + /** Julian date of start of Islamic epoch: 16 July 622 CE. + @memberof IslamicCalendar */ + jdEpoch: 1948439.5, + /** Days per month in a common year. + @memberof IslamicCalendar */ + daysPerMonth: [30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29], + /** true if has a year zero, false if not. + @memberof IslamicCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof IslamicCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof IslamicCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof IslamicCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof IslamicCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Islamic', + epochs: ['BH', 'AH'], + monthNames: ['Muharram', 'Safar', 'Rabi\' al-awwal', 'Rabi\' al-thani', 'Jumada al-awwal', 'Jumada al-thani', + 'Rajab', 'Sha\'aban', 'Ramadan', 'Shawwal', 'Dhu al-Qi\'dah', 'Dhu al-Hijjah'], + monthNamesShort: ['Muh', 'Saf', 'Rab1', 'Rab2', 'Jum1', 'Jum2', 'Raj', 'Sha\'', 'Ram', 'Shaw', 'DhuQ', 'DhuH'], + dayNames: ['Yawm al-ahad', 'Yawm al-ithnayn', 'Yawm ath-thulaathaa\'', + 'Yawm al-arbi\'aa\'', 'Yawm al-khamīs', 'Yawm al-jum\'a', 'Yawm as-sabt'], + dayNamesShort: ['Aha', 'Ith', 'Thu', 'Arb', 'Kha', 'Jum', 'Sab'], + dayNamesMin: ['Ah','It','Th','Ar','Kh','Ju','Sa'], + digits: null, + dateFormat: 'yyyy/mm/dd', + firstDay: 6, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof IslamicCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return (date.year() * 11 + 14) % 30 < 11; + }, + + /** Determine the week of the year for a date. + @memberof IslamicCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Sunday of this week starting on Sunday + var checkDate = this.newDate(year, month, day); + checkDate.add(-checkDate.dayOfWeek(), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a year. + @memberof IslamicCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of days. + @throws Error if an invalid year or a different calendar used. */ + daysInYear: function(year) { + return (this.leapYear(year) ? 355 : 354); + }, + + /** Retrieve the number of days in a month. + @memberof IslamicCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 12 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof IslamicCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return this.dayOfWeek(year, month, day) !== 5; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof IslamicCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = date.year(); + month = date.month(); + day = date.day(); + year = (year <= 0 ? year + 1 : year); + return day + Math.ceil(29.5 * (month - 1)) + (year - 1) * 354 + + Math.floor((3 + (11 * year)) / 30) + this.jdEpoch - 1; + }, + + /** Create a new date from a Julian date. + @memberof IslamicCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + jd = Math.floor(jd) + 0.5; + var year = Math.floor((30 * (jd - this.jdEpoch) + 10646) / 10631); + year = (year <= 0 ? year - 1 : year); + var month = Math.min(12, Math.ceil((jd - 29 - this.toJD(year, 1, 1)) / 29.5) + 1); + var day = jd - this.toJD(year, month, 1) + 1; + return this.newDate(year, month, day); + } +}); + +// Islamic (16 civil) calendar implementation +main.calendars.islamic = IslamicCalendar; + + +},{"../main":1033,"object-assign":446}],1025:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Julian calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Julian calendar. + Based on code from http://www.fourmilab.ch/documents/calendar/. + See also http://en.wikipedia.org/wiki/Julian_calendar. + @class JulianCalendar + @augments BaseCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function JulianCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +JulianCalendar.prototype = new main.baseCalendar; + +assign(JulianCalendar.prototype, { + /** The calendar name. + @memberof JulianCalendar */ + name: 'Julian', + /** Julian date of start of Julian epoch: 1 January 0001 AD = 30 December 0001 BCE. + @memberof JulianCalendar */ + jdEpoch: 1721423.5, + /** Days per month in a common year. + @memberof JulianCalendar */ + daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], + /** true if has a year zero, false if not. + @memberof JulianCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof JulianCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof JulianCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof JulianCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof JulianCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Julian', + epochs: ['BC', 'AD'], + monthNames: ['January', 'February', 'March', 'April', 'May', 'June', + 'July', 'August', 'September', 'October', 'November', 'December'], + monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], + digits: null, + dateFormat: 'mm/dd/yyyy', + firstDay: 0, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof JulianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + var year = (date.year() < 0 ? date.year() + 1 : date.year()); // No year zero + return (year % 4) === 0; + }, + + /** Determine the week of the year for a date - ISO 8601. + @memberof JulianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Thursday of this week starting on Monday + var checkDate = this.newDate(year, month, day); + checkDate.add(4 - (checkDate.dayOfWeek() || 7), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a month. + @memberof JulianCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof JulianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} True if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return (this.dayOfWeek(year, month, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof JulianCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = date.year(); + month = date.month(); + day = date.day(); + if (year < 0) { year++; } // No year zero + // Jean Meeus algorithm, "Astronomical Algorithms", 1991 + if (month <= 2) { + year--; + month += 12; + } + return Math.floor(365.25 * (year + 4716)) + + Math.floor(30.6001 * (month + 1)) + day - 1524.5; + }, + + /** Create a new date from a Julian date. + @memberof JulianCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + // Jean Meeus algorithm, "Astronomical Algorithms", 1991 + var a = Math.floor(jd + 0.5); + var b = a + 1524; + var c = Math.floor((b - 122.1) / 365.25); + var d = Math.floor(365.25 * c); + var e = Math.floor((b - d) / 30.6001); + var month = e - Math.floor(e < 14 ? 1 : 13); + var year = c - Math.floor(month > 2 ? 4716 : 4715); + var day = b - d - Math.floor(30.6001 * e); + if (year <= 0) { year--; } // No year zero + return this.newDate(year, month, day); + } +}); + +// Julian calendar implementation +main.calendars.julian = JulianCalendar; + + +},{"../main":1033,"object-assign":446}],1026:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Mayan calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Mayan Long Count calendar. + See also http://en.wikipedia.org/wiki/Mayan_calendar. + @class MayanCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function MayanCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +MayanCalendar.prototype = new main.baseCalendar; + +assign(MayanCalendar.prototype, { + /** The calendar name. + @memberof MayanCalendar */ + name: 'Mayan', + /** Julian date of start of Mayan epoch: 11 August 3114 BCE. + @memberof MayanCalendar */ + jdEpoch: 584282.5, + /** true if has a year zero, false if not. + @memberof MayanCalendar */ + hasYearZero: true, + /** The minimum month number. + @memberof MayanCalendar */ + minMonth: 0, + /** The first month in the year. + @memberof MayanCalendar */ + firstMonth: 0, + /** The minimum day number. + @memberof MayanCalendar */ + minDay: 0, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof MayanCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. + @property haabMonths {string[]} The names of the Haab months. + @property tzolkinMonths {string[]} The names of the Tzolkin months. */ + regionalOptions: { // Localisations + '': { + name: 'Mayan', + epochs: ['', ''], + monthNames: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '10', '11', '12', '13', '14', '15', '16', '17'], + monthNamesShort: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '10', '11', '12', '13', '14', '15', '16', '17'], + dayNames: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], + dayNamesShort: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], + dayNamesMin: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], + digits: null, + dateFormat: 'YYYY.m.d', + firstDay: 0, + isRTL: false, + haabMonths: ['Pop', 'Uo', 'Zip', 'Zotz', 'Tzec', 'Xul', 'Yaxkin', 'Mol', 'Chen', 'Yax', + 'Zac', 'Ceh', 'Mac', 'Kankin', 'Muan', 'Pax', 'Kayab', 'Cumku', 'Uayeb'], + tzolkinMonths: ['Imix', 'Ik', 'Akbal', 'Kan', 'Chicchan', 'Cimi', 'Manik', 'Lamat', 'Muluc', 'Oc', + 'Chuen', 'Eb', 'Ben', 'Ix', 'Men', 'Cib', 'Caban', 'Etznab', 'Cauac', 'Ahau'] + } + }, + + /** Determine whether this date is in a leap year. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return false; + }, + + /** Format the year, if not a simple sequential number. + @memberof MayanCalendar + @param year {CDate|number} The date to format or the year to format. + @return {string} The formatted year. + @throws Error if an invalid year or a different calendar used. */ + formatYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + year = date.year(); + var baktun = Math.floor(year / 400); + year = year % 400; + year += (year < 0 ? 400 : 0); + var katun = Math.floor(year / 20); + return baktun + '.' + katun + '.' + (year % 20); + }, + + /** Convert from the formatted year back to a single number. + @memberof MayanCalendar + @param years {string} The year as n.n.n. + @return {number} The sequential year. + @throws Error if an invalid value is supplied. */ + forYear: function(years) { + years = years.split('.'); + if (years.length < 3) { + throw 'Invalid Mayan year'; + } + var year = 0; + for (var i = 0; i < years.length; i++) { + var y = parseInt(years[i], 10); + if (Math.abs(y) > 19 || (i > 0 && y < 0)) { + throw 'Invalid Mayan year'; + } + year = year * 20 + y; + } + return year; + }, + + /** Retrieve the number of months in a year. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of months. + @throws Error if an invalid year or a different calendar used. */ + monthsInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return 18; + }, + + /** Determine the week of the year for a date. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + this._validate(year, month, day, main.local.invalidDate); + return 0; + }, + + /** Retrieve the number of days in a year. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of days. + @throws Error if an invalid year or a different calendar used. */ + daysInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return 360; + }, + + /** Retrieve the number of days in a month. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + this._validate(year, month, this.minDay, main.local.invalidMonth); + return 20; + }, + + /** Retrieve the number of days in a week. + @memberof MayanCalendar + @return {number} The number of days. */ + daysInWeek: function() { + return 5; // Just for formatting + }, + + /** Retrieve the day of the week for a date. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The day of the week: 0 to number of days - 1. + @throws Error if an invalid date or a different calendar used. */ + dayOfWeek: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + return date.day(); + }, + + /** Determine whether this date is a week day. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + this._validate(year, month, day, main.local.invalidDate); + return true; + }, + + /** Retrieve additional information about a date - Haab and Tzolkin equivalents. + @memberof MayanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {object} Additional information - contents depends on calendar. + @throws Error if an invalid date or a different calendar used. */ + extraInfo: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + var jd = date.toJD(); + var haab = this._toHaab(jd); + var tzolkin = this._toTzolkin(jd); + return {haabMonthName: this.local.haabMonths[haab[0] - 1], + haabMonth: haab[0], haabDay: haab[1], + tzolkinDayName: this.local.tzolkinMonths[tzolkin[0] - 1], + tzolkinDay: tzolkin[0], tzolkinTrecena: tzolkin[1]}; + }, + + /** Retrieve Haab date from a Julian date. + @memberof MayanCalendar + @private + @param jd {number} The Julian date. + @return {number[]} Corresponding Haab month and day. */ + _toHaab: function(jd) { + jd -= this.jdEpoch; + var day = mod(jd + 8 + ((18 - 1) * 20), 365); + return [Math.floor(day / 20) + 1, mod(day, 20)]; + }, + + /** Retrieve Tzolkin date from a Julian date. + @memberof MayanCalendar + @private + @param jd {number} The Julian date. + @return {number[]} Corresponding Tzolkin day and trecena. */ + _toTzolkin: function(jd) { + jd -= this.jdEpoch; + return [amod(jd + 20, 20), amod(jd + 4, 13)]; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof MayanCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + return date.day() + (date.month() * 20) + (date.year() * 360) + this.jdEpoch; + }, + + /** Create a new date from a Julian date. + @memberof MayanCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + jd = Math.floor(jd) + 0.5 - this.jdEpoch; + var year = Math.floor(jd / 360); + jd = jd % 360; + jd += (jd < 0 ? 360 : 0); + var month = Math.floor(jd / 20); + var day = jd % 20; + return this.newDate(year, month, day); + } +}); + +// Modulus function which works for non-integers. +function mod(a, b) { + return a - (b * Math.floor(a / b)); +} + +// Modulus function which returns numerator if modulus is zero. +function amod(a, b) { + return mod(a - 1, b) + 1; +} + +// Mayan calendar implementation +main.calendars.mayan = MayanCalendar; + + +},{"../main":1033,"object-assign":446}],1027:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Nanakshahi calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) January 2016. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Nanakshahi calendar. + See also https://en.wikipedia.org/wiki/Nanakshahi_calendar. + @class NanakshahiCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function NanakshahiCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +NanakshahiCalendar.prototype = new main.baseCalendar; + +var gregorian = main.instance('gregorian'); + +assign(NanakshahiCalendar.prototype, { + /** The calendar name. + @memberof NanakshahiCalendar */ + name: 'Nanakshahi', + /** Julian date of start of Nanakshahi epoch: 14 March 1469 CE. + @memberof NanakshahiCalendar */ + jdEpoch: 2257673.5, + /** Days per month in a common year. + @memberof NanakshahiCalendar */ + daysPerMonth: [31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30], + /** true if has a year zero, false if not. + @memberof NanakshahiCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof NanakshahiCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof NanakshahiCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof NanakshahiCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof NanakshahiCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Nanakshahi', + epochs: ['BN', 'AN'], + monthNames: ['Chet', 'Vaisakh', 'Jeth', 'Harh', 'Sawan', 'Bhadon', + 'Assu', 'Katak', 'Maghar', 'Poh', 'Magh', 'Phagun'], + monthNamesShort: ['Che', 'Vai', 'Jet', 'Har', 'Saw', 'Bha', 'Ass', 'Kat', 'Mgr', 'Poh', 'Mgh', 'Pha'], + dayNames: ['Somvaar', 'Mangalvar', 'Budhvaar', 'Veervaar', 'Shukarvaar', 'Sanicharvaar', 'Etvaar'], + dayNamesShort: ['Som', 'Mangal', 'Budh', 'Veer', 'Shukar', 'Sanichar', 'Et'], + dayNamesMin: ['So', 'Ma', 'Bu', 'Ve', 'Sh', 'Sa', 'Et'], + digits: null, + dateFormat: 'dd-mm-yyyy', + firstDay: 0, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof NanakshahiCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, + main.local.invalidYear || main.regionalOptions[''].invalidYear); + return gregorian.leapYear(date.year() + (date.year() < 1 ? 1 : 0) + 1469); + }, + + /** Determine the week of the year for a date. + @memberof NanakshahiCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Monday of this week starting on Monday + var checkDate = this.newDate(year, month, day); + checkDate.add(1 - (checkDate.dayOfWeek() || 7), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a month. + @memberof NanakshahiCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 12 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof NanakshahiCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return (this.dayOfWeek(year, month, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof NanakshahiCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidMonth); + var year = date.year(); + if (year < 0) { year++; } // No year zero + var doy = date.day(); + for (var m = 1; m < date.month(); m++) { + doy += this.daysPerMonth[m - 1]; + } + return doy + gregorian.toJD(year + 1468, 3, 13); + }, + + /** Create a new date from a Julian date. + @memberof NanakshahiCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + jd = Math.floor(jd + 0.5); + var year = Math.floor((jd - (this.jdEpoch - 1)) / 366); + while (jd >= this.toJD(year + 1, 1, 1)) { + year++; + } + var day = jd - Math.floor(this.toJD(year, 1, 1) + 0.5) + 1; + var month = 1; + while (day > this.daysInMonth(year, month)) { + day -= this.daysInMonth(year, month); + month++; + } + return this.newDate(year, month, day); + } +}); + +// Nanakshahi calendar implementation +main.calendars.nanakshahi = NanakshahiCalendar; + + +},{"../main":1033,"object-assign":446}],1028:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Nepali calendar for jQuery v2.0.2. + Written by Artur Neumann (ict.projects{at}nepal.inf.org) April 2013. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Nepali civil calendar. + Based on the ideas from + http://codeissue.com/articles/a04e050dea7468f/algorithm-to-convert-english-date-to-nepali-date-using-c-net + and http://birenj2ee.blogspot.com/2011/04/nepali-calendar-in-java.html + See also http://en.wikipedia.org/wiki/Nepali_calendar + and https://en.wikipedia.org/wiki/Bikram_Samwat. + @class NepaliCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function NepaliCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +NepaliCalendar.prototype = new main.baseCalendar; + +assign(NepaliCalendar.prototype, { + /** The calendar name. + @memberof NepaliCalendar */ + name: 'Nepali', + /** Julian date of start of Nepali epoch: 14 April 57 BCE. + @memberof NepaliCalendar */ + jdEpoch: 1700709.5, + /** Days per month in a common year. + @memberof NepaliCalendar */ + daysPerMonth: [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + /** true if has a year zero, false if not. + @memberof NepaliCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof NepaliCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof NepaliCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof NepaliCalendar */ + minDay: 1, + /** The number of days in the year. + @memberof NepaliCalendar */ + daysPerYear: 365, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof NepaliCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Nepali', + epochs: ['BBS', 'ABS'], + monthNames: ['Baisakh', 'Jestha', 'Ashadh', 'Shrawan', 'Bhadra', 'Ashwin', + 'Kartik', 'Mangsir', 'Paush', 'Mangh', 'Falgun', 'Chaitra'], + monthNamesShort: ['Bai', 'Je', 'As', 'Shra', 'Bha', 'Ash', 'Kar', 'Mang', 'Pau', 'Ma', 'Fal', 'Chai'], + dayNames: ['Aaitabaar', 'Sombaar', 'Manglbaar', 'Budhabaar', 'Bihibaar', 'Shukrabaar', 'Shanibaar'], + dayNamesShort: ['Aaita', 'Som', 'Mangl', 'Budha', 'Bihi', 'Shukra', 'Shani'], + dayNamesMin: ['Aai', 'So', 'Man', 'Bu', 'Bi', 'Shu', 'Sha'], + digits: null, + dateFormat: 'dd/mm/yyyy', + firstDay: 1, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof NepaliCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + return this.daysInYear(year) !== this.daysPerYear; + }, + + /** Determine the week of the year for a date. + @memberof NepaliCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Sunday of this week starting on Sunday + var checkDate = this.newDate(year, month, day); + checkDate.add(-checkDate.dayOfWeek(), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a year. + @memberof NepaliCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of days. + @throws Error if an invalid year or a different calendar used. */ + daysInYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + year = date.year(); + if (typeof this.NEPALI_CALENDAR_DATA[year] === 'undefined') { + return this.daysPerYear; + } + var daysPerYear = 0; + for (var month_number = this.minMonth; month_number <= 12; month_number++) { + daysPerYear += this.NEPALI_CALENDAR_DATA[year][month_number]; + } + return daysPerYear; + }, + + /** Retrieve the number of days in a month. + @memberof NepaliCalendar + @param year {CDate|number| The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + if (year.year) { + month = year.month(); + year = year.year(); + } + this._validate(year, month, this.minDay, main.local.invalidMonth); + return (typeof this.NEPALI_CALENDAR_DATA[year] === 'undefined' ? + this.daysPerMonth[month - 1] : this.NEPALI_CALENDAR_DATA[year][month]); + }, + + /** Determine whether this date is a week day. + @memberof NepaliCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return this.dayOfWeek(year, month, day) !== 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof NepaliCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(nepaliYear, nepaliMonth, nepaliDay) { + var date = this._validate(nepaliYear, nepaliMonth, nepaliDay, main.local.invalidDate); + nepaliYear = date.year(); + nepaliMonth = date.month(); + nepaliDay = date.day(); + var gregorianCalendar = main.instance(); + var gregorianDayOfYear = 0; // We will add all the days that went by since + // the 1st. January and then we can get the Gregorian Date + var nepaliMonthToCheck = nepaliMonth; + var nepaliYearToCheck = nepaliYear; + this._createMissingCalendarData(nepaliYear); + // Get the correct year + var gregorianYear = nepaliYear - (nepaliMonthToCheck > 9 || (nepaliMonthToCheck === 9 && + nepaliDay >= this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][0]) ? 56 : 57); + // First we add the amount of days in the actual Nepali month as the day of year in the + // Gregorian one because at least this days are gone since the 1st. Jan. + if (nepaliMonth !== 9) { + gregorianDayOfYear = nepaliDay; + nepaliMonthToCheck--; + } + // Now we loop throw all Nepali month and add the amount of days to gregorianDayOfYear + // we do this till we reach Paush (9th month). 1st. January always falls in this month + while (nepaliMonthToCheck !== 9) { + if (nepaliMonthToCheck <= 0) { + nepaliMonthToCheck = 12; + nepaliYearToCheck--; + } + gregorianDayOfYear += this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][nepaliMonthToCheck]; + nepaliMonthToCheck--; + } + // If the date that has to be converted is in Paush (month no. 9) we have to do some other calculation + if (nepaliMonth === 9) { + // Add the days that are passed since the first day of Paush and substract the + // amount of days that lie between 1st. Jan and 1st Paush + gregorianDayOfYear += nepaliDay - this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][0]; + // For the first days of Paush we are now in negative values, + // because in the end of the gregorian year we substract + // 365 / 366 days (P.S. remember math in school + - gives -) + if (gregorianDayOfYear < 0) { + gregorianDayOfYear += gregorianCalendar.daysInYear(gregorianYear); + } + } + else { + gregorianDayOfYear += this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][9] - + this.NEPALI_CALENDAR_DATA[nepaliYearToCheck][0]; + } + return gregorianCalendar.newDate(gregorianYear, 1 ,1).add(gregorianDayOfYear, 'd').toJD(); + }, + + /** Create a new date from a Julian date. + @memberof NepaliCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + var gregorianCalendar = main.instance(); + var gregorianDate = gregorianCalendar.fromJD(jd); + var gregorianYear = gregorianDate.year(); + var gregorianDayOfYear = gregorianDate.dayOfYear(); + var nepaliYear = gregorianYear + 56; //this is not final, it could be also +57 but +56 is always true for 1st Jan. + this._createMissingCalendarData(nepaliYear); + var nepaliMonth = 9; // Jan 1 always fall in Nepali month Paush which is the 9th month of Nepali calendar. + // Get the Nepali day in Paush (month 9) of 1st January + var dayOfFirstJanInPaush = this.NEPALI_CALENDAR_DATA[nepaliYear][0]; + // Check how many days are left of Paush . + // Days calculated from 1st Jan till the end of the actual Nepali month, + // we use this value to check if the gregorian Date is in the actual Nepali month. + var daysSinceJanFirstToEndOfNepaliMonth = + this.NEPALI_CALENDAR_DATA[nepaliYear][nepaliMonth] - dayOfFirstJanInPaush + 1; + // If the gregorian day-of-year is smaller o equal than the sum of days between the 1st January and + // the end of the actual nepali month we found the correct nepali month. + // Example: + // The 4th February 2011 is the gregorianDayOfYear 35 (31 days of January + 4) + // 1st January 2011 is in the nepali year 2067, where 1st. January is in the 17th day of Paush (9th month) + // In 2067 Paush has 30days, This means (30-17+1=14) there are 14days between 1st January and end of Paush + // (including 17th January) + // The gregorianDayOfYear (35) is bigger than 14, so we check the next month + // The next nepali month (Mangh) has 29 days + // 29+14=43, this is bigger than gregorianDayOfYear(35) so, we found the correct nepali month + while (gregorianDayOfYear > daysSinceJanFirstToEndOfNepaliMonth) { + nepaliMonth++; + if (nepaliMonth > 12) { + nepaliMonth = 1; + nepaliYear++; + } + daysSinceJanFirstToEndOfNepaliMonth += this.NEPALI_CALENDAR_DATA[nepaliYear][nepaliMonth]; + } + // The last step is to calculate the nepali day-of-month + // to continue our example from before: + // we calculated there are 43 days from 1st. January (17 Paush) till end of Mangh (29 days) + // when we subtract from this 43 days the day-of-year of the the Gregorian date (35), + // we know how far the searched day is away from the end of the Nepali month. + // So we simply subtract this number from the amount of days in this month (30) + var nepaliDayOfMonth = this.NEPALI_CALENDAR_DATA[nepaliYear][nepaliMonth] - + (daysSinceJanFirstToEndOfNepaliMonth - gregorianDayOfYear); + return this.newDate(nepaliYear, nepaliMonth, nepaliDayOfMonth); + }, + + /** Creates missing data in the NEPALI_CALENDAR_DATA table. + This data will not be correct but just give an estimated result. Mostly -/+ 1 day + @private + @param nepaliYear {number} The missing year number. */ + _createMissingCalendarData: function(nepaliYear) { + var tmp_calendar_data = this.daysPerMonth.slice(0); + tmp_calendar_data.unshift(17); + for (var nepaliYearToCreate = (nepaliYear - 1); nepaliYearToCreate < (nepaliYear + 2); nepaliYearToCreate++) { + if (typeof this.NEPALI_CALENDAR_DATA[nepaliYearToCreate] === 'undefined') { + this.NEPALI_CALENDAR_DATA[nepaliYearToCreate] = tmp_calendar_data; + } + } + }, + + NEPALI_CALENDAR_DATA: { + // These data are from http://www.ashesh.com.np + 1970: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1971: [18, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], + 1972: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], + 1973: [19, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 1974: [19, 31, 31, 32, 30, 31, 31, 30, 29, 30, 29, 30, 30], + 1975: [18, 31, 31, 32, 32, 30, 31, 30, 29, 30, 29, 30, 30], + 1976: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 1977: [18, 31, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], + 1978: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1979: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 1980: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 1981: [18, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], + 1982: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1983: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 1984: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 1985: [18, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], + 1986: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1987: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 1988: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 1989: [18, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 1990: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1991: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], + // These data are from http://nepalicalendar.rat32.com/index.php + 1992: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 1993: [18, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 1994: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1995: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], + 1996: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 1997: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1998: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 1999: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2000: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2001: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2002: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2003: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2004: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2005: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2006: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2007: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2008: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], + 2009: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2010: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2011: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2012: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], + 2013: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2014: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2015: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2016: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], + 2017: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2018: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2019: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2020: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 2021: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2022: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], + 2023: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2024: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 2025: [18, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2026: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2027: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2028: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2029: [18, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], + 2030: [17, 31, 32, 31, 32, 31, 30, 30, 30, 30, 30, 30, 31], + 2031: [17, 31, 32, 31, 32, 31, 31, 31, 31, 31, 31, 31, 31], + 2032: [17, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32], + 2033: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2034: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2035: [17, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], + 2036: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2037: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2038: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2039: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], + 2040: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2041: [18, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2042: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2043: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], + 2044: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2045: [18, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2046: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2047: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 2048: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2049: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], + 2050: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2051: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 2052: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2053: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], + 2054: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2055: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 29, 30], + 2056: [17, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], + 2057: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2058: [17, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2059: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2060: [17, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2061: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2062: [17, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], + 2063: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2064: [17, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2065: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2066: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], + 2067: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2068: [17, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2069: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2070: [17, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], + 2071: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2072: [17, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2073: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], + 2074: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 2075: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2076: [16, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], + 2077: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], + 2078: [17, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], + 2079: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], + 2080: [16, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], + // These data are from http://www.ashesh.com.np/nepali-calendar/ + 2081: [17, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2082: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2083: [17, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], + 2084: [17, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], + 2085: [17, 31, 32, 31, 32, 31, 31, 30, 30, 29, 30, 30, 30], + 2086: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2087: [16, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], + 2088: [16, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], + 2089: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2090: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2091: [16, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], + 2092: [16, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2093: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2094: [17, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], + 2095: [17, 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30], + 2096: [17, 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], + 2097: [17, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], + 2098: [17, 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 30, 31], + 2099: [17, 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30], + 2100: [17, 31, 32, 31, 32, 30, 31, 30, 29, 30, 29, 30, 30] + } +}); + +// Nepali calendar implementation +main.calendars.nepali = NepaliCalendar; + + +},{"../main":1033,"object-assign":446}],1029:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Persian calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the Persian or Jalali calendar. + Based on code from http://www.iranchamber.com/calendar/converter/iranian_calendar_converter.php. + See also http://en.wikipedia.org/wiki/Iranian_calendar. + @class PersianCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function PersianCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +PersianCalendar.prototype = new main.baseCalendar; + +assign(PersianCalendar.prototype, { + /** The calendar name. + @memberof PersianCalendar */ + name: 'Persian', + /** Julian date of start of Persian epoch: 19 March 622 CE. + @memberof PersianCalendar */ + jdEpoch: 1948320.5, + /** Days per month in a common year. + @memberof PersianCalendar */ + daysPerMonth: [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29], + /** true if has a year zero, false if not. + @memberof PersianCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof PersianCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof PersianCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof PersianCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof PersianCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Persian', + epochs: ['BP', 'AP'], + monthNames: ['Farvardin', 'Ordibehesht', 'Khordad', 'Tir', 'Mordad', 'Shahrivar', + 'Mehr', 'Aban', 'Azar', 'Day', 'Bahman', 'Esfand'], + monthNamesShort: ['Far', 'Ord', 'Kho', 'Tir', 'Mor', 'Sha', 'Meh', 'Aba', 'Aza', 'Day', 'Bah', 'Esf'], + dayNames: ['Yekshambe', 'Doshambe', 'Seshambe', 'Chæharshambe', 'Panjshambe', 'Jom\'e', 'Shambe'], + dayNamesShort: ['Yek', 'Do', 'Se', 'Chæ', 'Panj', 'Jom', 'Sha'], + dayNamesMin: ['Ye','Do','Se','Ch','Pa','Jo','Sh'], + digits: null, + dateFormat: 'yyyy/mm/dd', + firstDay: 6, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof PersianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return (((((date.year() - (date.year() > 0 ? 474 : 473)) % 2820) + + 474 + 38) * 682) % 2816) < 682; + }, + + /** Determine the week of the year for a date. + @memberof PersianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Saturday of this week starting on Saturday + var checkDate = this.newDate(year, month, day); + checkDate.add(-((checkDate.dayOfWeek() + 1) % 7), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a month. + @memberof PersianCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 12 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof PersianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return this.dayOfWeek(year, month, day) !== 5; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof PersianCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + year = date.year(); + month = date.month(); + day = date.day(); + var epBase = year - (year >= 0 ? 474 : 473); + var epYear = 474 + mod(epBase, 2820); + return day + (month <= 7 ? (month - 1) * 31 : (month - 1) * 30 + 6) + + Math.floor((epYear * 682 - 110) / 2816) + (epYear - 1) * 365 + + Math.floor(epBase / 2820) * 1029983 + this.jdEpoch - 1; + }, + + /** Create a new date from a Julian date. + @memberof PersianCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + jd = Math.floor(jd) + 0.5; + var depoch = jd - this.toJD(475, 1, 1); + var cycle = Math.floor(depoch / 1029983); + var cyear = mod(depoch, 1029983); + var ycycle = 2820; + if (cyear !== 1029982) { + var aux1 = Math.floor(cyear / 366); + var aux2 = mod(cyear, 366); + ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1; + } + var year = ycycle + (2820 * cycle) + 474; + year = (year <= 0 ? year - 1 : year); + var yday = jd - this.toJD(year, 1, 1) + 1; + var month = (yday <= 186 ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30)); + var day = jd - this.toJD(year, month, 1) + 1; + return this.newDate(year, month, day); + } +}); + +// Modulus function which works for non-integers. +function mod(a, b) { + return a - (b * Math.floor(a / b)); +} + +// Persian (Jalali) calendar implementation +main.calendars.persian = PersianCalendar; +main.calendars.jalali = PersianCalendar; + + +},{"../main":1033,"object-assign":446}],1030:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Taiwanese (Minguo) calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +var gregorianCalendar = main.instance(); + +/** Implementation of the Taiwanese calendar. + See http://en.wikipedia.org/wiki/Minguo_calendar. + @class TaiwanCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function TaiwanCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +TaiwanCalendar.prototype = new main.baseCalendar; + +assign(TaiwanCalendar.prototype, { + /** The calendar name. + @memberof TaiwanCalendar */ + name: 'Taiwan', + /** Julian date of start of Taiwan epoch: 1 January 1912 CE (Gregorian). + @memberof TaiwanCalendar */ + jdEpoch: 2419402.5, + /** Difference in years between Taiwan and Gregorian calendars. + @memberof TaiwanCalendar */ + yearsOffset: 1911, + /** Days per month in a common year. + @memberof TaiwanCalendar */ + daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], + /** true if has a year zero, false if not. + @memberof TaiwanCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof TaiwanCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof TaiwanCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof TaiwanCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof TaiwanCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Taiwan', + epochs: ['BROC', 'ROC'], + monthNames: ['January', 'February', 'March', 'April', 'May', 'June', + 'July', 'August', 'September', 'October', 'November', 'December'], + monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], + digits: null, + dateFormat: 'yyyy/mm/dd', + firstDay: 1, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof TaiwanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + var year = this._t2gYear(date.year()); + return gregorianCalendar.leapYear(year); + }, + + /** Determine the week of the year for a date - ISO 8601. + @memberof TaiwanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + var year = this._t2gYear(date.year()); + return gregorianCalendar.weekOfYear(year, date.month(), date.day()); + }, + + /** Retrieve the number of days in a month. + @memberof TaiwanCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof TaiwanCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return (this.dayOfWeek(year, month, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof TaiwanCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + var year = this._t2gYear(date.year()); + return gregorianCalendar.toJD(year, date.month(), date.day()); + }, + + /** Create a new date from a Julian date. + @memberof TaiwanCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + var date = gregorianCalendar.fromJD(jd); + var year = this._g2tYear(date.year()); + return this.newDate(year, date.month(), date.day()); + }, + + /** Convert Taiwanese to Gregorian year. + @memberof TaiwanCalendar + @private + @param year {number} The Taiwanese year. + @return {number} The corresponding Gregorian year. */ + _t2gYear: function(year) { + return year + this.yearsOffset + (year >= -this.yearsOffset && year <= -1 ? 1 : 0); + }, + + /** Convert Gregorian to Taiwanese year. + @memberof TaiwanCalendar + @private + @param year {number} The Gregorian year. + @return {number} The corresponding Taiwanese year. */ + _g2tYear: function(year) { + return year - this.yearsOffset - (year >= 1 && year <= this.yearsOffset ? 1 : 0); + } +}); + +// Taiwan calendar implementation +main.calendars.taiwan = TaiwanCalendar; + + +},{"../main":1033,"object-assign":446}],1031:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Thai calendar for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) February 2010. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +var gregorianCalendar = main.instance(); + +/** Implementation of the Thai calendar. + See http://en.wikipedia.org/wiki/Thai_calendar. + @class ThaiCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function ThaiCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +ThaiCalendar.prototype = new main.baseCalendar; + +assign(ThaiCalendar.prototype, { + /** The calendar name. + @memberof ThaiCalendar */ + name: 'Thai', + /** Julian date of start of Thai epoch: 1 January 543 BCE (Gregorian). + @memberof ThaiCalendar */ + jdEpoch: 1523098.5, + /** Difference in years between Thai and Gregorian calendars. + @memberof ThaiCalendar */ + yearsOffset: 543, + /** Days per month in a common year. + @memberof ThaiCalendar */ + daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], + /** true if has a year zero, false if not. + @memberof ThaiCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof ThaiCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof ThaiCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof ThaiCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof ThaiCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Thai', + epochs: ['BBE', 'BE'], + monthNames: ['January', 'February', 'March', 'April', 'May', 'June', + 'July', 'August', 'September', 'October', 'November', 'December'], + monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], + digits: null, + dateFormat: 'dd/mm/yyyy', + firstDay: 0, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof ThaiCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + var year = this._t2gYear(date.year()); + return gregorianCalendar.leapYear(year); + }, + + /** Determine the week of the year for a date - ISO 8601. + @memberof ThaiCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + var year = this._t2gYear(date.year()); + return gregorianCalendar.weekOfYear(year, date.month(), date.day()); + }, + + /** Retrieve the number of days in a month. + @memberof ThaiCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof ThaiCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return (this.dayOfWeek(year, month, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof ThaiCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + var year = this._t2gYear(date.year()); + return gregorianCalendar.toJD(year, date.month(), date.day()); + }, + + /** Create a new date from a Julian date. + @memberof ThaiCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + var date = gregorianCalendar.fromJD(jd); + var year = this._g2tYear(date.year()); + return this.newDate(year, date.month(), date.day()); + }, + + /** Convert Thai to Gregorian year. + @memberof ThaiCalendar + @private + @param year {number} The Thai year. + @return {number} The corresponding Gregorian year. */ + _t2gYear: function(year) { + return year - this.yearsOffset - (year >= 1 && year <= this.yearsOffset ? 1 : 0); + }, + + /** Convert Gregorian to Thai year. + @memberof ThaiCalendar + @private + @param year {number} The Gregorian year. + @return {number} The corresponding Thai year. */ + _g2tYear: function(year) { + return year + this.yearsOffset + (year >= -this.yearsOffset && year <= -1 ? 1 : 0); + } +}); + +// Thai calendar implementation +main.calendars.thai = ThaiCalendar; + + +},{"../main":1033,"object-assign":446}],1032:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + UmmAlQura calendar for jQuery v2.0.2. + Written by Amro Osama March 2013. + Modified by Binnooh.com & www.elm.sa - 2014 - Added dates back to 1276 Hijri year. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var main = require('../main'); +var assign = require('object-assign'); + + +/** Implementation of the UmmAlQura or 'saudi' calendar. + See also http://en.wikipedia.org/wiki/Islamic_calendar#Saudi_Arabia.27s_Umm_al-Qura_calendar. + http://www.ummulqura.org.sa/About.aspx + http://www.staff.science.uu.nl/~gent0113/islam/ummalqura.htm + @class UmmAlQuraCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function UmmAlQuraCalendar(language) { + this.local = this.regionalOptions[language || ''] || this.regionalOptions['']; +} + +UmmAlQuraCalendar.prototype = new main.baseCalendar; + +assign(UmmAlQuraCalendar.prototype, { + /** The calendar name. + @memberof UmmAlQuraCalendar */ + name: 'UmmAlQura', + //jdEpoch: 1948440, // Julian date of start of UmmAlQura epoch: 14 March 1937 CE + //daysPerMonth: // Days per month in a common year, replaced by a method. + /** true if has a year zero, false if not. + @memberof UmmAlQuraCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof UmmAlQuraCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof UmmAlQuraCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof UmmAlQuraCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof UmmAlQuraCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Umm al-Qura', + epochs: ['BH', 'AH'], + monthNames: ['Al-Muharram', 'Safar', 'Rabi\' al-awwal', 'Rabi\' Al-Thani', 'Jumada Al-Awwal', 'Jumada Al-Thani', + 'Rajab', 'Sha\'aban', 'Ramadan', 'Shawwal', 'Dhu al-Qi\'dah', 'Dhu al-Hijjah'], + monthNamesShort: ['Muh', 'Saf', 'Rab1', 'Rab2', 'Jum1', 'Jum2', 'Raj', 'Sha\'', 'Ram', 'Shaw', 'DhuQ', 'DhuH'], + dayNames: ['Yawm al-Ahad', 'Yawm al-Ithnain', 'Yawm al-Thalāthā’', 'Yawm al-Arba‘ā’', 'Yawm al-Khamīs', 'Yawm al-Jum‘a', 'Yawm al-Sabt'], + dayNamesMin: ['Ah', 'Ith', 'Th', 'Ar', 'Kh', 'Ju', 'Sa'], + digits: null, + dateFormat: 'yyyy/mm/dd', + firstDay: 6, + isRTL: true + } + }, + + /** Determine whether this date is in a leap year. + @memberof UmmAlQuraCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function (year) { + var date = this._validate(year, this.minMonth, this.minDay, main.local.invalidYear); + return (this.daysInYear(date.year()) === 355); + }, + + /** Determine the week of the year for a date. + @memberof UmmAlQuraCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function (year, month, day) { + // Find Sunday of this week starting on Sunday + var checkDate = this.newDate(year, month, day); + checkDate.add(-checkDate.dayOfWeek(), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a year. + @memberof UmmAlQuraCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of days. + @throws Error if an invalid year or a different calendar used. */ + daysInYear: function (year) { + var daysCount = 0; + for (var i = 1; i <= 12; i++) { + daysCount += this.daysInMonth(year, i); + } + return daysCount; + }, + + /** Retrieve the number of days in a month. + @memberof UmmAlQuraCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function (year, month) { + var date = this._validate(year, month, this.minDay, main.local.invalidMonth); + var mcjdn = date.toJD() - 2400000 + 0.5; // Modified Chronological Julian Day Number (MCJDN) + // the MCJDN's of the start of the lunations in the Umm al-Qura calendar are stored in the 'ummalqura_dat' array + var index = 0; + for (var i = 0; i < ummalqura_dat.length; i++) { + if (ummalqura_dat[i] > mcjdn) { + return (ummalqura_dat[index] - ummalqura_dat[index - 1]); + } + index++; + } + return 30; // Unknown outside + }, + + /** Determine whether this date is a week day. + @memberof UmmAlQuraCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function (year, month, day) { + return this.dayOfWeek(year, month, day) !== 5; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof UmmAlQuraCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function (year, month, day) { + var date = this._validate(year, month, day, main.local.invalidDate); + var index = (12 * (date.year() - 1)) + date.month() - 15292; + var mcjdn = date.day() + ummalqura_dat[index - 1] - 1; + return mcjdn + 2400000 - 0.5; // Modified Chronological Julian Day Number (MCJDN) + }, + + /** Create a new date from a Julian date. + @memberof UmmAlQuraCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function (jd) { + var mcjdn = jd - 2400000 + 0.5; // Modified Chronological Julian Day Number (MCJDN) + // the MCJDN's of the start of the lunations in the Umm al-Qura calendar + // are stored in the 'ummalqura_dat' array + var index = 0; + for (var i = 0; i < ummalqura_dat.length; i++) { + if (ummalqura_dat[i] > mcjdn) break; + index++; + } + var lunation = index + 15292; //UmmAlQura Lunation Number + var ii = Math.floor((lunation - 1) / 12); + var year = ii + 1; + var month = lunation - 12 * ii; + var day = mcjdn - ummalqura_dat[index - 1] + 1; + return this.newDate(year, month, day); + }, + + /** Determine whether a date is valid for this calendar. + @memberof UmmAlQuraCalendar + @param year {number} The year to examine. + @param month {number} The month to examine. + @param day {number} The day to examine. + @return {boolean} true if a valid date, false if not. */ + isValid: function(year, month, day) { + var valid = main.baseCalendar.prototype.isValid.apply(this, arguments); + if (valid) { + year = (year.year != null ? year.year : year); + valid = (year >= 1276 && year <= 1500); + } + return valid; + }, + + /** Check that a candidate date is from the same calendar and is valid. + @memberof UmmAlQuraCalendar + @private + @param year {CDate|number} The date to validate or the year to validate. + @param month {number} The month to validate. + @param day {number} The day to validate. + @param error {string} Error message if invalid. + @throws Error if different calendars used or invalid date. */ + _validate: function(year, month, day, error) { + var date = main.baseCalendar.prototype._validate.apply(this, arguments); + if (date.year < 1276 || date.year > 1500) { + throw error.replace(/\{0\}/, this.local.name); + } + return date; + } +}); + +// UmmAlQura calendar implementation +main.calendars.ummalqura = UmmAlQuraCalendar; + +var ummalqura_dat = [ + 20, 50, 79, 109, 138, 168, 197, 227, 256, 286, 315, 345, 374, 404, 433, 463, 492, 522, 551, 581, + 611, 641, 670, 700, 729, 759, 788, 818, 847, 877, 906, 936, 965, 995, 1024, 1054, 1083, 1113, 1142, 1172, + 1201, 1231, 1260, 1290, 1320, 1350, 1379, 1409, 1438, 1468, 1497, 1527, 1556, 1586, 1615, 1645, 1674, 1704, 1733, 1763, + 1792, 1822, 1851, 1881, 1910, 1940, 1969, 1999, 2028, 2058, 2087, 2117, 2146, 2176, 2205, 2235, 2264, 2294, 2323, 2353, + 2383, 2413, 2442, 2472, 2501, 2531, 2560, 2590, 2619, 2649, 2678, 2708, 2737, 2767, 2796, 2826, 2855, 2885, 2914, 2944, + 2973, 3003, 3032, 3062, 3091, 3121, 3150, 3180, 3209, 3239, 3268, 3298, 3327, 3357, 3386, 3416, 3446, 3476, 3505, 3535, + 3564, 3594, 3623, 3653, 3682, 3712, 3741, 3771, 3800, 3830, 3859, 3889, 3918, 3948, 3977, 4007, 4036, 4066, 4095, 4125, + 4155, 4185, 4214, 4244, 4273, 4303, 4332, 4362, 4391, 4421, 4450, 4480, 4509, 4539, 4568, 4598, 4627, 4657, 4686, 4716, + 4745, 4775, 4804, 4834, 4863, 4893, 4922, 4952, 4981, 5011, 5040, 5070, 5099, 5129, 5158, 5188, 5218, 5248, 5277, 5307, + 5336, 5366, 5395, 5425, 5454, 5484, 5513, 5543, 5572, 5602, 5631, 5661, 5690, 5720, 5749, 5779, 5808, 5838, 5867, 5897, + 5926, 5956, 5985, 6015, 6044, 6074, 6103, 6133, 6162, 6192, 6221, 6251, 6281, 6311, 6340, 6370, 6399, 6429, 6458, 6488, + 6517, 6547, 6576, 6606, 6635, 6665, 6694, 6724, 6753, 6783, 6812, 6842, 6871, 6901, 6930, 6960, 6989, 7019, 7048, 7078, + 7107, 7137, 7166, 7196, 7225, 7255, 7284, 7314, 7344, 7374, 7403, 7433, 7462, 7492, 7521, 7551, 7580, 7610, 7639, 7669, + 7698, 7728, 7757, 7787, 7816, 7846, 7875, 7905, 7934, 7964, 7993, 8023, 8053, 8083, 8112, 8142, 8171, 8201, 8230, 8260, + 8289, 8319, 8348, 8378, 8407, 8437, 8466, 8496, 8525, 8555, 8584, 8614, 8643, 8673, 8702, 8732, 8761, 8791, 8821, 8850, + 8880, 8909, 8938, 8968, 8997, 9027, 9056, 9086, 9115, 9145, 9175, 9205, 9234, 9264, 9293, 9322, 9352, 9381, 9410, 9440, + 9470, 9499, 9529, 9559, 9589, 9618, 9648, 9677, 9706, 9736, 9765, 9794, 9824, 9853, 9883, 9913, 9943, 9972, 10002, 10032, + 10061, 10090, 10120, 10149, 10178, 10208, 10237, 10267, 10297, 10326, 10356, 10386, 10415, 10445, 10474, 10504, 10533, 10562, 10592, 10621, + 10651, 10680, 10710, 10740, 10770, 10799, 10829, 10858, 10888, 10917, 10947, 10976, 11005, 11035, 11064, 11094, 11124, 11153, 11183, 11213, + 11242, 11272, 11301, 11331, 11360, 11389, 11419, 11448, 11478, 11507, 11537, 11567, 11596, 11626, 11655, 11685, 11715, 11744, 11774, 11803, + 11832, 11862, 11891, 11921, 11950, 11980, 12010, 12039, 12069, 12099, 12128, 12158, 12187, 12216, 12246, 12275, 12304, 12334, 12364, 12393, + 12423, 12453, 12483, 12512, 12542, 12571, 12600, 12630, 12659, 12688, 12718, 12747, 12777, 12807, 12837, 12866, 12896, 12926, 12955, 12984, + 13014, 13043, 13072, 13102, 13131, 13161, 13191, 13220, 13250, 13280, 13310, 13339, 13368, 13398, 13427, 13456, 13486, 13515, 13545, 13574, + 13604, 13634, 13664, 13693, 13723, 13752, 13782, 13811, 13840, 13870, 13899, 13929, 13958, 13988, 14018, 14047, 14077, 14107, 14136, 14166, + 14195, 14224, 14254, 14283, 14313, 14342, 14372, 14401, 14431, 14461, 14490, 14520, 14550, 14579, 14609, 14638, 14667, 14697, 14726, 14756, + 14785, 14815, 14844, 14874, 14904, 14933, 14963, 14993, 15021, 15051, 15081, 15110, 15140, 15169, 15199, 15228, 15258, 15287, 15317, 15347, + 15377, 15406, 15436, 15465, 15494, 15524, 15553, 15582, 15612, 15641, 15671, 15701, 15731, 15760, 15790, 15820, 15849, 15878, 15908, 15937, + 15966, 15996, 16025, 16055, 16085, 16114, 16144, 16174, 16204, 16233, 16262, 16292, 16321, 16350, 16380, 16409, 16439, 16468, 16498, 16528, + 16558, 16587, 16617, 16646, 16676, 16705, 16734, 16764, 16793, 16823, 16852, 16882, 16912, 16941, 16971, 17001, 17030, 17060, 17089, 17118, + 17148, 17177, 17207, 17236, 17266, 17295, 17325, 17355, 17384, 17414, 17444, 17473, 17502, 17532, 17561, 17591, 17620, 17650, 17679, 17709, + 17738, 17768, 17798, 17827, 17857, 17886, 17916, 17945, 17975, 18004, 18034, 18063, 18093, 18122, 18152, 18181, 18211, 18241, 18270, 18300, + 18330, 18359, 18388, 18418, 18447, 18476, 18506, 18535, 18565, 18595, 18625, 18654, 18684, 18714, 18743, 18772, 18802, 18831, 18860, 18890, + 18919, 18949, 18979, 19008, 19038, 19068, 19098, 19127, 19156, 19186, 19215, 19244, 19274, 19303, 19333, 19362, 19392, 19422, 19452, 19481, + 19511, 19540, 19570, 19599, 19628, 19658, 19687, 19717, 19746, 19776, 19806, 19836, 19865, 19895, 19924, 19954, 19983, 20012, 20042, 20071, + 20101, 20130, 20160, 20190, 20219, 20249, 20279, 20308, 20338, 20367, 20396, 20426, 20455, 20485, 20514, 20544, 20573, 20603, 20633, 20662, + 20692, 20721, 20751, 20780, 20810, 20839, 20869, 20898, 20928, 20957, 20987, 21016, 21046, 21076, 21105, 21135, 21164, 21194, 21223, 21253, + 21282, 21312, 21341, 21371, 21400, 21430, 21459, 21489, 21519, 21548, 21578, 21607, 21637, 21666, 21696, 21725, 21754, 21784, 21813, 21843, + 21873, 21902, 21932, 21962, 21991, 22021, 22050, 22080, 22109, 22138, 22168, 22197, 22227, 22256, 22286, 22316, 22346, 22375, 22405, 22434, + 22464, 22493, 22522, 22552, 22581, 22611, 22640, 22670, 22700, 22730, 22759, 22789, 22818, 22848, 22877, 22906, 22936, 22965, 22994, 23024, + 23054, 23083, 23113, 23143, 23173, 23202, 23232, 23261, 23290, 23320, 23349, 23379, 23408, 23438, 23467, 23497, 23527, 23556, 23586, 23616, + 23645, 23674, 23704, 23733, 23763, 23792, 23822, 23851, 23881, 23910, 23940, 23970, 23999, 24029, 24058, 24088, 24117, 24147, 24176, 24206, + 24235, 24265, 24294, 24324, 24353, 24383, 24413, 24442, 24472, 24501, 24531, 24560, 24590, 24619, 24648, 24678, 24707, 24737, 24767, 24796, + 24826, 24856, 24885, 24915, 24944, 24974, 25003, 25032, 25062, 25091, 25121, 25150, 25180, 25210, 25240, 25269, 25299, 25328, 25358, 25387, + 25416, 25446, 25475, 25505, 25534, 25564, 25594, 25624, 25653, 25683, 25712, 25742, 25771, 25800, 25830, 25859, 25888, 25918, 25948, 25977, + 26007, 26037, 26067, 26096, 26126, 26155, 26184, 26214, 26243, 26272, 26302, 26332, 26361, 26391, 26421, 26451, 26480, 26510, 26539, 26568, + 26598, 26627, 26656, 26686, 26715, 26745, 26775, 26805, 26834, 26864, 26893, 26923, 26952, 26982, 27011, 27041, 27070, 27099, 27129, 27159, + 27188, 27218, 27248, 27277, 27307, 27336, 27366, 27395, 27425, 27454, 27484, 27513, 27542, 27572, 27602, 27631, 27661, 27691, 27720, 27750, + 27779, 27809, 27838, 27868, 27897, 27926, 27956, 27985, 28015, 28045, 28074, 28104, 28134, 28163, 28193, 28222, 28252, 28281, 28310, 28340, + 28369, 28399, 28428, 28458, 28488, 28517, 28547, 28577, + // From 1356 + 28607, 28636, 28665, 28695, 28724, 28754, 28783, 28813, 28843, 28872, 28901, 28931, 28960, 28990, 29019, 29049, 29078, 29108, 29137, 29167, + 29196, 29226, 29255, 29285, 29315, 29345, 29375, 29404, 29434, 29463, 29492, 29522, 29551, 29580, 29610, 29640, 29669, 29699, 29729, 29759, + 29788, 29818, 29847, 29876, 29906, 29935, 29964, 29994, 30023, 30053, 30082, 30112, 30141, 30171, 30200, 30230, 30259, 30289, 30318, 30348, + 30378, 30408, 30437, 30467, 30496, 30526, 30555, 30585, 30614, 30644, 30673, 30703, 30732, 30762, 30791, 30821, 30850, 30880, 30909, 30939, + 30968, 30998, 31027, 31057, 31086, 31116, 31145, 31175, 31204, 31234, 31263, 31293, 31322, 31352, 31381, 31411, 31441, 31471, 31500, 31530, + 31559, 31589, 31618, 31648, 31676, 31706, 31736, 31766, 31795, 31825, 31854, 31884, 31913, 31943, 31972, 32002, 32031, 32061, 32090, 32120, + 32150, 32180, 32209, 32239, 32268, 32298, 32327, 32357, 32386, 32416, 32445, 32475, 32504, 32534, 32563, 32593, 32622, 32652, 32681, 32711, + 32740, 32770, 32799, 32829, 32858, 32888, 32917, 32947, 32976, 33006, 33035, 33065, 33094, 33124, 33153, 33183, 33213, 33243, 33272, 33302, + 33331, 33361, 33390, 33420, 33450, 33479, 33509, 33539, 33568, 33598, 33627, 33657, 33686, 33716, 33745, 33775, 33804, 33834, 33863, 33893, + 33922, 33952, 33981, 34011, 34040, 34069, 34099, 34128, 34158, 34187, 34217, 34247, 34277, 34306, 34336, 34365, 34395, 34424, 34454, 34483, + 34512, 34542, 34571, 34601, 34631, 34660, 34690, 34719, 34749, 34778, 34808, 34837, 34867, 34896, 34926, 34955, 34985, 35015, 35044, 35074, + 35103, 35133, 35162, 35192, 35222, 35251, 35280, 35310, 35340, 35370, 35399, 35429, 35458, 35488, 35517, 35547, 35576, 35605, 35635, 35665, + 35694, 35723, 35753, 35782, 35811, 35841, 35871, 35901, 35930, 35960, 35989, 36019, 36048, 36078, 36107, 36136, 36166, 36195, 36225, 36254, + 36284, 36314, 36343, 36373, 36403, 36433, 36462, 36492, 36521, 36551, 36580, 36610, 36639, 36669, 36698, 36728, 36757, 36786, 36816, 36845, + 36875, 36904, 36934, 36963, 36993, 37022, 37052, 37081, 37111, 37141, 37170, 37200, 37229, 37259, 37288, 37318, 37347, 37377, 37406, 37436, + 37465, 37495, 37524, 37554, 37584, 37613, 37643, 37672, 37701, 37731, 37760, 37790, 37819, 37849, 37878, 37908, 37938, 37967, 37997, 38027, + 38056, 38085, 38115, 38144, 38174, 38203, 38233, 38262, 38292, 38322, 38351, 38381, 38410, 38440, 38469, 38499, 38528, 38558, 38587, 38617, + 38646, 38676, 38705, 38735, 38764, 38794, 38823, 38853, 38882, 38912, 38941, 38971, 39001, 39030, 39059, 39089, 39118, 39148, 39178, 39208, + 39237, 39267, 39297, 39326, 39355, 39385, 39414, 39444, 39473, 39503, 39532, 39562, 39592, 39621, 39650, 39680, 39709, 39739, 39768, 39798, + 39827, 39857, 39886, 39916, 39946, 39975, 40005, 40035, 40064, 40094, 40123, 40153, 40182, 40212, 40241, 40271, 40300, 40330, 40359, 40389, + 40418, 40448, 40477, 40507, 40536, 40566, 40595, 40625, 40655, 40685, 40714, 40744, 40773, 40803, 40832, 40862, 40892, 40921, 40951, 40980, + 41009, 41039, 41068, 41098, 41127, 41157, 41186, 41216, 41245, 41275, 41304, 41334, 41364, 41393, 41422, 41452, 41481, 41511, 41540, 41570, + 41599, 41629, 41658, 41688, 41718, 41748, 41777, 41807, 41836, 41865, 41894, 41924, 41953, 41983, 42012, 42042, 42072, 42102, 42131, 42161, + 42190, 42220, 42249, 42279, 42308, 42337, 42367, 42397, 42426, 42456, 42485, 42515, 42545, 42574, 42604, 42633, 42662, 42692, 42721, 42751, + 42780, 42810, 42839, 42869, 42899, 42929, 42958, 42988, 43017, 43046, 43076, 43105, 43135, 43164, 43194, 43223, 43253, 43283, 43312, 43342, + 43371, 43401, 43430, 43460, 43489, 43519, 43548, 43578, 43607, 43637, 43666, 43696, 43726, 43755, 43785, 43814, 43844, 43873, 43903, 43932, + 43962, 43991, 44021, 44050, 44080, 44109, 44139, 44169, 44198, 44228, 44258, 44287, 44317, 44346, 44375, 44405, 44434, 44464, 44493, 44523, + 44553, 44582, 44612, 44641, 44671, 44700, 44730, 44759, 44788, 44818, 44847, 44877, 44906, 44936, 44966, 44996, 45025, 45055, 45084, 45114, + 45143, 45172, 45202, 45231, 45261, 45290, 45320, 45350, 45380, 45409, 45439, 45468, 45498, 45527, 45556, 45586, 45615, 45644, 45674, 45704, + 45733, 45763, 45793, 45823, 45852, 45882, 45911, 45940, 45970, 45999, 46028, 46058, 46088, 46117, 46147, 46177, 46206, 46236, 46265, 46295, + 46324, 46354, 46383, 46413, 46442, 46472, 46501, 46531, 46560, 46590, 46620, 46649, 46679, 46708, 46738, 46767, 46797, 46826, 46856, 46885, + 46915, 46944, 46974, 47003, 47033, 47063, 47092, 47122, 47151, 47181, 47210, 47240, 47269, 47298, 47328, 47357, 47387, 47417, 47446, 47476, + 47506, 47535, 47565, 47594, 47624, 47653, 47682, 47712, 47741, 47771, 47800, 47830, 47860, 47890, 47919, 47949, 47978, 48008, 48037, 48066, + 48096, 48125, 48155, 48184, 48214, 48244, 48273, 48303, 48333, 48362, 48392, 48421, 48450, 48480, 48509, 48538, 48568, 48598, 48627, 48657, + 48687, 48717, 48746, 48776, 48805, 48834, 48864, 48893, 48922, 48952, 48982, 49011, 49041, 49071, 49100, 49130, 49160, 49189, 49218, 49248, + 49277, 49306, 49336, 49365, 49395, 49425, 49455, 49484, 49514, 49543, 49573, 49602, 49632, 49661, 49690, 49720, 49749, 49779, 49809, 49838, + 49868, 49898, 49927, 49957, 49986, 50016, 50045, 50075, 50104, 50133, 50163, 50192, 50222, 50252, 50281, 50311, 50340, 50370, 50400, 50429, + 50459, 50488, 50518, 50547, 50576, 50606, 50635, 50665, 50694, 50724, 50754, 50784, 50813, 50843, 50872, 50902, 50931, 50960, 50990, 51019, + 51049, 51078, 51108, 51138, 51167, 51197, 51227, 51256, 51286, 51315, 51345, 51374, 51403, 51433, 51462, 51492, 51522, 51552, 51582, 51611, + 51641, 51670, 51699, 51729, 51758, 51787, 51816, 51846, 51876, 51906, 51936, 51965, 51995, 52025, 52054, 52083, 52113, 52142, 52171, 52200, + 52230, 52260, 52290, 52319, 52349, 52379, 52408, 52438, 52467, 52497, 52526, 52555, 52585, 52614, 52644, 52673, 52703, 52733, 52762, 52792, + 52822, 52851, 52881, 52910, 52939, 52969, 52998, 53028, 53057, 53087, 53116, 53146, 53176, 53205, 53235, 53264, 53294, 53324, 53353, 53383, + 53412, 53441, 53471, 53500, 53530, 53559, 53589, 53619, 53648, 53678, 53708, 53737, 53767, 53796, 53825, 53855, 53884, 53913, 53943, 53973, + 54003, 54032, 54062, 54092, 54121, 54151, 54180, 54209, 54239, 54268, 54297, 54327, 54357, 54387, 54416, 54446, 54476, 54505, 54535, 54564, + 54593, 54623, 54652, 54681, 54711, 54741, 54770, 54800, 54830, 54859, 54889, 54919, 54948, 54977, 55007, 55036, 55066, 55095, 55125, 55154, + 55184, 55213, 55243, 55273, 55302, 55332, 55361, 55391, 55420, 55450, 55479, 55508, 55538, 55567, 55597, 55627, 55657, 55686, 55716, 55745, + 55775, 55804, 55834, 55863, 55892, 55922, 55951, 55981, 56011, 56040, 56070, 56100, 56129, 56159, 56188, 56218, 56247, 56276, 56306, 56335, + 56365, 56394, 56424, 56454, 56483, 56513, 56543, 56572, 56601, 56631, 56660, 56690, 56719, 56749, 56778, 56808, 56837, 56867, 56897, 56926, + 56956, 56985, 57015, 57044, 57074, 57103, 57133, 57162, 57192, 57221, 57251, 57280, 57310, 57340, 57369, 57399, 57429, 57458, 57487, 57517, + 57546, 57576, 57605, 57634, 57664, 57694, 57723, 57753, 57783, 57813, 57842, 57871, 57901, 57930, 57959, 57989, 58018, 58048, 58077, 58107, + 58137, 58167, 58196, 58226, 58255, 58285, 58314, 58343, 58373, 58402, 58432, 58461, 58491, 58521, 58551, 58580, 58610, 58639, 58669, 58698, + 58727, 58757, 58786, 58816, 58845, 58875, 58905, 58934, 58964, 58994, 59023, 59053, 59082, 59111, 59141, 59170, 59200, 59229, 59259, 59288, + 59318, 59348, 59377, 59407, 59436, 59466, 59495, 59525, 59554, 59584, 59613, 59643, 59672, 59702, 59731, 59761, 59791, 59820, 59850, 59879, + 59909, 59939, 59968, 59997, 60027, 60056, 60086, 60115, 60145, 60174, 60204, 60234, 60264, 60293, 60323, 60352, 60381, 60411, 60440, 60469, + 60499, 60528, 60558, 60588, 60618, 60648, 60677, 60707, 60736, 60765, 60795, 60824, 60853, 60883, 60912, 60942, 60972, 61002, 61031, 61061, + 61090, 61120, 61149, 61179, 61208, 61237, 61267, 61296, 61326, 61356, 61385, 61415, 61445, 61474, 61504, 61533, 61563, 61592, 61621, 61651, + 61680, 61710, 61739, 61769, 61799, 61828, 61858, 61888, 61917, 61947, 61976, 62006, 62035, 62064, 62094, 62123, 62153, 62182, 62212, 62242, + 62271, 62301, 62331, 62360, 62390, 62419, 62448, 62478, 62507, 62537, 62566, 62596, 62625, 62655, 62685, 62715, 62744, 62774, 62803, 62832, + 62862, 62891, 62921, 62950, 62980, 63009, 63039, 63069, 63099, 63128, 63157, 63187, 63216, 63246, 63275, 63305, 63334, 63363, 63393, 63423, + 63453, 63482, 63512, 63541, 63571, 63600, 63630, 63659, 63689, 63718, 63747, 63777, 63807, 63836, 63866, 63895, 63925, 63955, 63984, 64014, + 64043, 64073, 64102, 64131, 64161, 64190, 64220, 64249, 64279, 64309, 64339, 64368, 64398, 64427, 64457, 64486, 64515, 64545, 64574, 64603, + 64633, 64663, 64692, 64722, 64752, 64782, 64811, 64841, 64870, 64899, 64929, 64958, 64987, 65017, 65047, 65076, 65106, 65136, 65166, 65195, + 65225, 65254, 65283, 65313, 65342, 65371, 65401, 65431, 65460, 65490, 65520, 65549, 65579, 65608, 65638, 65667, 65697, 65726, 65755, 65785, + 65815, 65844, 65874, 65903, 65933, 65963, 65992, 66022, 66051, 66081, 66110, 66140, 66169, 66199, 66228, 66258, 66287, 66317, 66346, 66376, + 66405, 66435, 66465, 66494, 66524, 66553, 66583, 66612, 66641, 66671, 66700, 66730, 66760, 66789, 66819, 66849, 66878, 66908, 66937, 66967, + 66996, 67025, 67055, 67084, 67114, 67143, 67173, 67203, 67233, 67262, 67292, 67321, 67351, 67380, 67409, 67439, 67468, 67497, 67527, 67557, + 67587, 67617, 67646, 67676, 67705, 67735, 67764, 67793, 67823, 67852, 67882, 67911, 67941, 67971, 68000, 68030, 68060, 68089, 68119, 68148, + 68177, 68207, 68236, 68266, 68295, 68325, 68354, 68384, 68414, 68443, 68473, 68502, 68532, 68561, 68591, 68620, 68650, 68679, 68708, 68738, + 68768, 68797, 68827, 68857, 68886, 68916, 68946, 68975, 69004, 69034, 69063, 69092, 69122, 69152, 69181, 69211, 69240, 69270, 69300, 69330, + 69359, 69388, 69418, 69447, 69476, 69506, 69535, 69565, 69595, 69624, 69654, 69684, 69713, 69743, 69772, 69802, 69831, 69861, 69890, 69919, + 69949, 69978, 70008, 70038, 70067, 70097, 70126, 70156, 70186, 70215, 70245, 70274, 70303, 70333, 70362, 70392, 70421, 70451, 70481, 70510, + 70540, 70570, 70599, 70629, 70658, 70687, 70717, 70746, 70776, 70805, 70835, 70864, 70894, 70924, 70954, 70983, 71013, 71042, 71071, 71101, + 71130, 71159, 71189, 71218, 71248, 71278, 71308, 71337, 71367, 71397, 71426, 71455, 71485, 71514, 71543, 71573, 71602, 71632, 71662, 71691, + 71721, 71751, 71781, 71810, 71839, 71869, 71898, 71927, 71957, 71986, 72016, 72046, 72075, 72105, 72135, 72164, 72194, 72223, 72253, 72282, + 72311, 72341, 72370, 72400, 72429, 72459, 72489, 72518, 72548, 72577, 72607, 72637, 72666, 72695, 72725, 72754, 72784, 72813, 72843, 72872, + 72902, 72931, 72961, 72991, 73020, 73050, 73080, 73109, 73139, 73168, 73197, 73227, 73256, 73286, 73315, 73345, 73375, 73404, 73434, 73464, + 73493, 73523, 73552, 73581, 73611, 73640, 73669, 73699, 73729, 73758, 73788, 73818, 73848, 73877, 73907, 73936, 73965, 73995, 74024, 74053, + 74083, 74113, 74142, 74172, 74202, 74231, 74261, 74291, 74320, 74349, 74379, 74408, 74437, 74467, 74497, 74526, 74556, 74586, 74615, 74645, + 74675, 74704, 74733, 74763, 74792, 74822, 74851, 74881, 74910, 74940, 74969, 74999, 75029, 75058, 75088, 75117, 75147, 75176, 75206, 75235, + 75264, 75294, 75323, 75353, 75383, 75412, 75442, 75472, 75501, 75531, 75560, 75590, 75619, 75648, 75678, 75707, 75737, 75766, 75796, 75826, + 75856, 75885, 75915, 75944, 75974, 76003, 76032, 76062, 76091, 76121, 76150, 76180, 76210, 76239, 76269, 76299, 76328, 76358, 76387, 76416, + 76446, 76475, 76505, 76534, 76564, 76593, 76623, 76653, 76682, 76712, 76741, 76771, 76801, 76830, 76859, 76889, 76918, 76948, 76977, 77007, + 77036, 77066, 77096, 77125, 77155, 77185, 77214, 77243, 77273, 77302, 77332, 77361, 77390, 77420, 77450, 77479, 77509, 77539, 77569, 77598, + 77627, 77657, 77686, 77715, 77745, 77774, 77804, 77833, 77863, 77893, 77923, 77952, 77982, 78011, 78041, 78070, 78099, 78129, 78158, 78188, + 78217, 78247, 78277, 78307, 78336, 78366, 78395, 78425, 78454, 78483, 78513, 78542, 78572, 78601, 78631, 78661, 78690, 78720, 78750, 78779, + 78808, 78838, 78867, 78897, 78926, 78956, 78985, 79015, 79044, 79074, 79104, 79133, 79163, 79192, 79222, 79251, 79281, 79310, 79340, 79369, + 79399, 79428, 79458, 79487, 79517, 79546, 79576, 79606, 79635, 79665, 79695, 79724, 79753, 79783, 79812, 79841, 79871, 79900, 79930, 79960, + 79990]; + + +},{"../main":1033,"object-assign":446}],1033:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Calendars for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var assign = require('object-assign'); + + +function Calendars() { + this.regionalOptions = []; + this.regionalOptions[''] = { + invalidCalendar: 'Calendar {0} not found', + invalidDate: 'Invalid {0} date', + invalidMonth: 'Invalid {0} month', + invalidYear: 'Invalid {0} year', + differentCalendars: 'Cannot mix {0} and {1} dates' + }; + this.local = this.regionalOptions['']; + this.calendars = {}; + this._localCals = {}; +} + +/** Create the calendars plugin. +

Provides support for various world calendars in a consistent manner.

+ @class Calendars + @example _exports.instance('julian').newDate(2014, 12, 25) */ +assign(Calendars.prototype, { + + /** Obtain a calendar implementation and localisation. + @memberof Calendars + @param [name='gregorian'] {string} The name of the calendar, e.g. 'gregorian', 'persian', 'islamic'. + @param [language=''] {string} The language code to use for localisation (default is English). + @return {Calendar} The calendar and localisation. + @throws Error if calendar not found. */ + instance: function(name, language) { + name = (name || 'gregorian').toLowerCase(); + language = language || ''; + var cal = this._localCals[name + '-' + language]; + if (!cal && this.calendars[name]) { + cal = new this.calendars[name](language); + this._localCals[name + '-' + language] = cal; + } + if (!cal) { + throw (this.local.invalidCalendar || this.regionalOptions[''].invalidCalendar). + replace(/\{0\}/, name); + } + return cal; + }, + + /** Create a new date - for today if no other parameters given. + @memberof Calendars + @param year {CDate|number} The date to copy or the year for the date. + @param [month] {number} The month for the date. + @param [day] {number} The day for the date. + @param [calendar='gregorian'] {BaseCalendar|string} The underlying calendar or the name of the calendar. + @param [language=''] {string} The language to use for localisation (default English). + @return {CDate} The new date. + @throws Error if an invalid date. */ + newDate: function(year, month, day, calendar, language) { + calendar = (year != null && year.year ? year.calendar() : (typeof calendar === 'string' ? + this.instance(calendar, language) : calendar)) || this.instance(); + return calendar.newDate(year, month, day); + }, + + /** A simple digit substitution function for localising numbers via the Calendar digits option. + @member Calendars + @param digits {string[]} The substitute digits, for 0 through 9. + @return {function} The substitution function. */ + substituteDigits: function(digits) { + return function(value) { + return (value + '').replace(/[0-9]/g, function(digit) { + return digits[digit]; + }); + } + }, + + /** Digit substitution function for localising Chinese style numbers via the Calendar digits option. + @member Calendars + @param digits {string[]} The substitute digits, for 0 through 9. + @param powers {string[]} The characters denoting powers of 10, i.e. 1, 10, 100, 1000. + @return {function} The substitution function. */ + substituteChineseDigits: function(digits, powers) { + return function(value) { + var localNumber = ''; + var power = 0; + while (value > 0) { + var units = value % 10; + localNumber = (units === 0 ? '' : digits[units] + powers[power]) + localNumber; + power++; + value = Math.floor(value / 10); + } + if (localNumber.indexOf(digits[1] + powers[1]) === 0) { + localNumber = localNumber.substr(1); + } + return localNumber || digits[0]; + } + } +}); + +/** Generic date, based on a particular calendar. + @class CDate + @param calendar {BaseCalendar} The underlying calendar implementation. + @param year {number} The year for this date. + @param month {number} The month for this date. + @param day {number} The day for this date. + @return {CDate} The date object. + @throws Error if an invalid date. */ +function CDate(calendar, year, month, day) { + this._calendar = calendar; + this._year = year; + this._month = month; + this._day = day; + if (this._calendar._validateLevel === 0 && + !this._calendar.isValid(this._year, this._month, this._day)) { + throw (_exports.local.invalidDate || _exports.regionalOptions[''].invalidDate). + replace(/\{0\}/, this._calendar.local.name); + } +} + +/** Pad a numeric value with leading zeroes. + @private + @param value {number} The number to format. + @param length {number} The minimum length. + @return {string} The formatted number. */ +function pad(value, length) { + value = '' + value; + return '000000'.substring(0, length - value.length) + value; +} + +assign(CDate.prototype, { + + /** Create a new date. + @memberof CDate + @param [year] {CDate|number} The date to copy or the year for the date (default this date). + @param [month] {number} The month for the date. + @param [day] {number} The day for the date. + @return {CDate} The new date. + @throws Error if an invalid date. */ + newDate: function(year, month, day) { + return this._calendar.newDate((year == null ? this : year), month, day); + }, + + /** Set or retrieve the year for this date. + @memberof CDate + @param [year] {number} The year for the date. + @return {number|CDate} The date's year (if no parameter) or the updated date. + @throws Error if an invalid date. */ + year: function(year) { + return (arguments.length === 0 ? this._year : this.set(year, 'y')); + }, + + /** Set or retrieve the month for this date. + @memberof CDate + @param [month] {number} The month for the date. + @return {number|CDate} The date's month (if no parameter) or the updated date. + @throws Error if an invalid date. */ + month: function(month) { + return (arguments.length === 0 ? this._month : this.set(month, 'm')); + }, + + /** Set or retrieve the day for this date. + @memberof CDate + @param [day] {number} The day for the date. + @return {number|CData} The date's day (if no parameter) or the updated date. + @throws Error if an invalid date. */ + day: function(day) { + return (arguments.length === 0 ? this._day : this.set(day, 'd')); + }, + + /** Set new values for this date. + @memberof CDate + @param year {number} The year for the date. + @param month {number} The month for the date. + @param day {number} The day for the date. + @return {CDate} The updated date. + @throws Error if an invalid date. */ + date: function(year, month, day) { + if (!this._calendar.isValid(year, month, day)) { + throw (_exports.local.invalidDate || _exports.regionalOptions[''].invalidDate). + replace(/\{0\}/, this._calendar.local.name); + } + this._year = year; + this._month = month; + this._day = day; + return this; + }, + + /** Determine whether this date is in a leap year. + @memberof CDate + @return {boolean} true if this is a leap year, false if not. */ + leapYear: function() { + return this._calendar.leapYear(this); + }, + + /** Retrieve the epoch designator for this date, e.g. BCE or CE. + @memberof CDate + @return {string} The current epoch. */ + epoch: function() { + return this._calendar.epoch(this); + }, + + /** Format the year, if not a simple sequential number. + @memberof CDate + @return {string} The formatted year. */ + formatYear: function() { + return this._calendar.formatYear(this); + }, + + /** Retrieve the month of the year for this date, + i.e. the month's position within a numbered year. + @memberof CDate + @return {number} The month of the year: minMonth to months per year. */ + monthOfYear: function() { + return this._calendar.monthOfYear(this); + }, + + /** Retrieve the week of the year for this date. + @memberof CDate + @return {number} The week of the year: 1 to weeks per year. */ + weekOfYear: function() { + return this._calendar.weekOfYear(this); + }, + + /** Retrieve the number of days in the year for this date. + @memberof CDate + @return {number} The number of days in this year. */ + daysInYear: function() { + return this._calendar.daysInYear(this); + }, + + /** Retrieve the day of the year for this date. + @memberof CDate + @return {number} The day of the year: 1 to days per year. */ + dayOfYear: function() { + return this._calendar.dayOfYear(this); + }, + + /** Retrieve the number of days in the month for this date. + @memberof CDate + @return {number} The number of days. */ + daysInMonth: function() { + return this._calendar.daysInMonth(this); + }, + + /** Retrieve the day of the week for this date. + @memberof CDate + @return {number} The day of the week: 0 to number of days - 1. */ + dayOfWeek: function() { + return this._calendar.dayOfWeek(this); + }, + + /** Determine whether this date is a week day. + @memberof CDate + @return {boolean} true if a week day, false if not. */ + weekDay: function() { + return this._calendar.weekDay(this); + }, + + /** Retrieve additional information about this date. + @memberof CDate + @return {object} Additional information - contents depends on calendar. */ + extraInfo: function() { + return this._calendar.extraInfo(this); + }, + + /** Add period(s) to a date. + @memberof CDate + @param offset {number} The number of periods to adjust by. + @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. + @return {CDate} The updated date. */ + add: function(offset, period) { + return this._calendar.add(this, offset, period); + }, + + /** Set a portion of the date. + @memberof CDate + @param value {number} The new value for the period. + @param period {string} One of 'y' for year, 'm' for month, 'd' for day. + @return {CDate} The updated date. + @throws Error if not a valid date. */ + set: function(value, period) { + return this._calendar.set(this, value, period); + }, + + /** Compare this date to another date. + @memberof CDate + @param date {CDate} The other date. + @return {number} -1 if this date is before the other date, + 0 if they are equal, or +1 if this date is after the other date. */ + compareTo: function(date) { + if (this._calendar.name !== date._calendar.name) { + throw (_exports.local.differentCalendars || _exports.regionalOptions[''].differentCalendars). + replace(/\{0\}/, this._calendar.local.name).replace(/\{1\}/, date._calendar.local.name); + } + var c = (this._year !== date._year ? this._year - date._year : + this._month !== date._month ? this.monthOfYear() - date.monthOfYear() : + this._day - date._day); + return (c === 0 ? 0 : (c < 0 ? -1 : +1)); + }, + + /** Retrieve the calendar backing this date. + @memberof CDate + @return {BaseCalendar} The calendar implementation. */ + calendar: function() { + return this._calendar; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof CDate + @return {number} The equivalent Julian date. */ + toJD: function() { + return this._calendar.toJD(this); + }, + + /** Create a new date from a Julian date. + @memberof CDate + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + return this._calendar.fromJD(jd); + }, + + /** Convert this date to a standard (Gregorian) JavaScript Date. + @memberof CDate + @return {Date} The equivalent JavaScript date. */ + toJSDate: function() { + return this._calendar.toJSDate(this); + }, + + /** Create a new date from a standard (Gregorian) JavaScript Date. + @memberof CDate + @param jsd {Date} The JavaScript date to convert. + @return {CDate} The equivalent date. */ + fromJSDate: function(jsd) { + return this._calendar.fromJSDate(jsd); + }, + + /** Convert to a string for display. + @memberof CDate + @return {string} This date as a string. */ + toString: function() { + return (this.year() < 0 ? '-' : '') + pad(Math.abs(this.year()), 4) + + '-' + pad(this.month(), 2) + '-' + pad(this.day(), 2); + } +}); + +/** Basic functionality for all calendars. + Other calendars should extend this: +
OtherCalendar.prototype = new BaseCalendar;
+ @class BaseCalendar */ +function BaseCalendar() { + this.shortYearCutoff = '+10'; +} + +assign(BaseCalendar.prototype, { + _validateLevel: 0, // "Stack" to turn validation on/off + + /** Create a new date within this calendar - today if no parameters given. + @memberof BaseCalendar + @param year {CDate|number} The date to duplicate or the year for the date. + @param [month] {number} The month for the date. + @param [day] {number} The day for the date. + @return {CDate} The new date. + @throws Error if not a valid date or a different calendar used. */ + newDate: function(year, month, day) { + if (year == null) { + return this.today(); + } + if (year.year) { + this._validate(year, month, day, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + day = year.day(); + month = year.month(); + year = year.year(); + } + return new CDate(this, year, month, day); + }, + + /** Create a new date for today. + @memberof BaseCalendar + @return {CDate} Today's date. */ + today: function() { + return this.fromJSDate(new Date()); + }, + + /** Retrieve the epoch designator for this date. + @memberof BaseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {string} The current epoch. + @throws Error if an invalid year or a different calendar used. */ + epoch: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, + _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); + return (date.year() < 0 ? this.local.epochs[0] : this.local.epochs[1]); + }, + + /** Format the year, if not a simple sequential number + @memberof BaseCalendar + @param year {CDate|number} The date to format or the year to format. + @return {string} The formatted year. + @throws Error if an invalid year or a different calendar used. */ + formatYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, + _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); + return (date.year() < 0 ? '-' : '') + pad(Math.abs(date.year()), 4) + }, + + /** Retrieve the number of months in a year. + @memberof BaseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of months. + @throws Error if an invalid year or a different calendar used. */ + monthsInYear: function(year) { + this._validate(year, this.minMonth, this.minDay, + _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); + return 12; + }, + + /** Calculate the month's ordinal position within the year - + for those calendars that don't start at month 1! + @memberof BaseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param month {number} The month to examine. + @return {number} The ordinal position, starting from minMonth. + @throws Error if an invalid year/month or a different calendar used. */ + monthOfYear: function(year, month) { + var date = this._validate(year, month, this.minDay, + _exports.local.invalidMonth || _exports.regionalOptions[''].invalidMonth); + return (date.month() + this.monthsInYear(date) - this.firstMonth) % + this.monthsInYear(date) + this.minMonth; + }, + + /** Calculate actual month from ordinal position, starting from minMonth. + @memberof BaseCalendar + @param year {number} The year to examine. + @param ord {number} The month's ordinal position. + @return {number} The month's number. + @throws Error if an invalid year/month. */ + fromMonthOfYear: function(year, ord) { + var m = (ord + this.firstMonth - 2 * this.minMonth) % + this.monthsInYear(year) + this.minMonth; + this._validate(year, m, this.minDay, + _exports.local.invalidMonth || _exports.regionalOptions[''].invalidMonth); + return m; + }, + + /** Retrieve the number of days in a year. + @memberof BaseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {number} The number of days. + @throws Error if an invalid year or a different calendar used. */ + daysInYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, + _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); + return (this.leapYear(date) ? 366 : 365); + }, + + /** Retrieve the day of the year for a date. + @memberof BaseCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The day of the year. + @throws Error if an invalid date or a different calendar used. */ + dayOfYear: function(year, month, day) { + var date = this._validate(year, month, day, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + return date.toJD() - this.newDate(date.year(), + this.fromMonthOfYear(date.year(), this.minMonth), this.minDay).toJD() + 1; + }, + + /** Retrieve the number of days in a week. + @memberof BaseCalendar + @return {number} The number of days. */ + daysInWeek: function() { + return 7; + }, + + /** Retrieve the day of the week for a date. + @memberof BaseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The day of the week: 0 to number of days - 1. + @throws Error if an invalid date or a different calendar used. */ + dayOfWeek: function(year, month, day) { + var date = this._validate(year, month, day, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + return (Math.floor(this.toJD(date)) + 2) % this.daysInWeek(); + }, + + /** Retrieve additional information about a date. + @memberof BaseCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {object} Additional information - contents depends on calendar. + @throws Error if an invalid date or a different calendar used. */ + extraInfo: function(year, month, day) { + this._validate(year, month, day, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + return {}; + }, + + /** Add period(s) to a date. + Cater for no year zero. + @memberof BaseCalendar + @param date {CDate} The starting date. + @param offset {number} The number of periods to adjust by. + @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. + @return {CDate} The updated date. + @throws Error if a different calendar used. */ + add: function(date, offset, period) { + this._validate(date, this.minMonth, this.minDay, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + return this._correctAdd(date, this._add(date, offset, period), offset, period); + }, + + /** Add period(s) to a date. + @memberof BaseCalendar + @private + @param date {CDate} The starting date. + @param offset {number} The number of periods to adjust by. + @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. + @return {CDate} The updated date. */ + _add: function(date, offset, period) { + this._validateLevel++; + if (period === 'd' || period === 'w') { + var jd = date.toJD() + offset * (period === 'w' ? this.daysInWeek() : 1); + var d = date.calendar().fromJD(jd); + this._validateLevel--; + return [d.year(), d.month(), d.day()]; + } + try { + var y = date.year() + (period === 'y' ? offset : 0); + var m = date.monthOfYear() + (period === 'm' ? offset : 0); + var d = date.day();// + (period === 'd' ? offset : 0) + + //(period === 'w' ? offset * this.daysInWeek() : 0); + var resyncYearMonth = function(calendar) { + while (m < calendar.minMonth) { + y--; + m += calendar.monthsInYear(y); + } + var yearMonths = calendar.monthsInYear(y); + while (m > yearMonths - 1 + calendar.minMonth) { + y++; + m -= yearMonths; + yearMonths = calendar.monthsInYear(y); + } + }; + if (period === 'y') { + if (date.month() !== this.fromMonthOfYear(y, m)) { // Hebrew + m = this.newDate(y, date.month(), this.minDay).monthOfYear(); + } + m = Math.min(m, this.monthsInYear(y)); + d = Math.min(d, this.daysInMonth(y, this.fromMonthOfYear(y, m))); + } + else if (period === 'm') { + resyncYearMonth(this); + d = Math.min(d, this.daysInMonth(y, this.fromMonthOfYear(y, m))); + } + var ymd = [y, this.fromMonthOfYear(y, m), d]; + this._validateLevel--; + return ymd; + } + catch (e) { + this._validateLevel--; + throw e; + } + }, + + /** Correct a candidate date after adding period(s) to a date. + Handle no year zero if necessary. + @memberof BaseCalendar + @private + @param date {CDate} The starting date. + @param ymd {number[]} The added date. + @param offset {number} The number of periods to adjust by. + @param period {string} One of 'y' for year, 'm' for month, 'w' for week, 'd' for day. + @return {CDate} The updated date. */ + _correctAdd: function(date, ymd, offset, period) { + if (!this.hasYearZero && (period === 'y' || period === 'm')) { + if (ymd[0] === 0 || // In year zero + (date.year() > 0) !== (ymd[0] > 0)) { // Crossed year zero + var adj = {y: [1, 1, 'y'], m: [1, this.monthsInYear(-1), 'm'], + w: [this.daysInWeek(), this.daysInYear(-1), 'd'], + d: [1, this.daysInYear(-1), 'd']}[period]; + var dir = (offset < 0 ? -1 : +1); + ymd = this._add(date, offset * adj[0] + dir * adj[1], adj[2]); + } + } + return date.date(ymd[0], ymd[1], ymd[2]); + }, + + /** Set a portion of the date. + @memberof BaseCalendar + @param date {CDate} The starting date. + @param value {number} The new value for the period. + @param period {string} One of 'y' for year, 'm' for month, 'd' for day. + @return {CDate} The updated date. + @throws Error if an invalid date or a different calendar used. */ + set: function(date, value, period) { + this._validate(date, this.minMonth, this.minDay, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + var y = (period === 'y' ? value : date.year()); + var m = (period === 'm' ? value : date.month()); + var d = (period === 'd' ? value : date.day()); + if (period === 'y' || period === 'm') { + d = Math.min(d, this.daysInMonth(y, m)); + } + return date.date(y, m, d); + }, + + /** Determine whether a date is valid for this calendar. + @memberof BaseCalendar + @param year {number} The year to examine. + @param month {number} The month to examine. + @param day {number} The day to examine. + @return {boolean} true if a valid date, false if not. */ + isValid: function(year, month, day) { + this._validateLevel++; + var valid = (this.hasYearZero || year !== 0); + if (valid) { + var date = this.newDate(year, month, this.minDay); + valid = (month >= this.minMonth && month - this.minMonth < this.monthsInYear(date)) && + (day >= this.minDay && day - this.minDay < this.daysInMonth(date)); + } + this._validateLevel--; + return valid; + }, + + /** Convert the date to a standard (Gregorian) JavaScript Date. + @memberof BaseCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {Date} The equivalent JavaScript date. + @throws Error if an invalid date or a different calendar used. */ + toJSDate: function(year, month, day) { + var date = this._validate(year, month, day, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + return _exports.instance().fromJD(this.toJD(date)).toJSDate(); + }, + + /** Convert the date from a standard (Gregorian) JavaScript Date. + @memberof BaseCalendar + @param jsd {Date} The JavaScript date. + @return {CDate} The equivalent calendar date. */ + fromJSDate: function(jsd) { + return this.fromJD(_exports.instance().fromJSDate(jsd).toJD()); + }, + + /** Check that a candidate date is from the same calendar and is valid. + @memberof BaseCalendar + @private + @param year {CDate|number} The date to validate or the year to validate. + @param [month] {number} The month to validate. + @param [day] {number} The day to validate. + @param error {string} Rrror message if invalid. + @throws Error if different calendars used or invalid date. */ + _validate: function(year, month, day, error) { + if (year.year) { + if (this._validateLevel === 0 && this.name !== year.calendar().name) { + throw (_exports.local.differentCalendars || _exports.regionalOptions[''].differentCalendars). + replace(/\{0\}/, this.local.name).replace(/\{1\}/, year.calendar().local.name); + } + return year; + } + try { + this._validateLevel++; + if (this._validateLevel === 1 && !this.isValid(year, month, day)) { + throw error.replace(/\{0\}/, this.local.name); + } + var date = this.newDate(year, month, day); + this._validateLevel--; + return date; + } + catch (e) { + this._validateLevel--; + throw e; + } + } +}); + +/** Implementation of the Proleptic Gregorian Calendar. + See http://en.wikipedia.org/wiki/Gregorian_calendar + and http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar. + @class GregorianCalendar + @augments BaseCalendar + @param [language=''] {string} The language code (default English) for localisation. */ +function GregorianCalendar(language) { + this.local = this.regionalOptions[language] || this.regionalOptions['']; +} + +GregorianCalendar.prototype = new BaseCalendar; + +assign(GregorianCalendar.prototype, { + /** The calendar name. + @memberof GregorianCalendar */ + name: 'Gregorian', + /** Julian date of start of Gregorian epoch: 1 January 0001 CE. + @memberof GregorianCalendar */ + jdEpoch: 1721425.5, + /** Days per month in a common year. + @memberof GregorianCalendar */ + daysPerMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], + /** true if has a year zero, false if not. + @memberof GregorianCalendar */ + hasYearZero: false, + /** The minimum month number. + @memberof GregorianCalendar */ + minMonth: 1, + /** The first month in the year. + @memberof GregorianCalendar */ + firstMonth: 1, + /** The minimum day number. + @memberof GregorianCalendar */ + minDay: 1, + + /** Localisations for the plugin. + Entries are objects indexed by the language code ('' being the default US/English). + Each object has the following attributes. + @memberof GregorianCalendar + @property name {string} The calendar name. + @property epochs {string[]} The epoch names. + @property monthNames {string[]} The long names of the months of the year. + @property monthNamesShort {string[]} The short names of the months of the year. + @property dayNames {string[]} The long names of the days of the week. + @property dayNamesShort {string[]} The short names of the days of the week. + @property dayNamesMin {string[]} The minimal names of the days of the week. + @property dateFormat {string} The date format for this calendar. + See the options on formatDate for details. + @property firstDay {number} The number of the first day of the week, starting at 0. + @property isRTL {number} true if this localisation reads right-to-left. */ + regionalOptions: { // Localisations + '': { + name: 'Gregorian', + epochs: ['BCE', 'CE'], + monthNames: ['January', 'February', 'March', 'April', 'May', 'June', + 'July', 'August', 'September', 'October', 'November', 'December'], + monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], + digits: null, + dateFormat: 'mm/dd/yyyy', + firstDay: 0, + isRTL: false + } + }, + + /** Determine whether this date is in a leap year. + @memberof GregorianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @return {boolean} true if this is a leap year, false if not. + @throws Error if an invalid year or a different calendar used. */ + leapYear: function(year) { + var date = this._validate(year, this.minMonth, this.minDay, + _exports.local.invalidYear || _exports.regionalOptions[''].invalidYear); + var year = date.year() + (date.year() < 0 ? 1 : 0); // No year zero + return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); + }, + + /** Determine the week of the year for a date - ISO 8601. + @memberof GregorianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {number} The week of the year, starting from 1. + @throws Error if an invalid date or a different calendar used. */ + weekOfYear: function(year, month, day) { + // Find Thursday of this week starting on Monday + var checkDate = this.newDate(year, month, day); + checkDate.add(4 - (checkDate.dayOfWeek() || 7), 'd'); + return Math.floor((checkDate.dayOfYear() - 1) / 7) + 1; + }, + + /** Retrieve the number of days in a month. + @memberof GregorianCalendar + @param year {CDate|number} The date to examine or the year of the month. + @param [month] {number} The month. + @return {number} The number of days in this month. + @throws Error if an invalid month/year or a different calendar used. */ + daysInMonth: function(year, month) { + var date = this._validate(year, month, this.minDay, + _exports.local.invalidMonth || _exports.regionalOptions[''].invalidMonth); + return this.daysPerMonth[date.month() - 1] + + (date.month() === 2 && this.leapYear(date.year()) ? 1 : 0); + }, + + /** Determine whether this date is a week day. + @memberof GregorianCalendar + @param year {CDate|number} The date to examine or the year to examine. + @param [month] {number} The month to examine. + @param [day] {number} The day to examine. + @return {boolean} true if a week day, false if not. + @throws Error if an invalid date or a different calendar used. */ + weekDay: function(year, month, day) { + return (this.dayOfWeek(year, month, day) || 7) < 6; + }, + + /** Retrieve the Julian date equivalent for this date, + i.e. days since January 1, 4713 BCE Greenwich noon. + @memberof GregorianCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {number} The equivalent Julian date. + @throws Error if an invalid date or a different calendar used. */ + toJD: function(year, month, day) { + var date = this._validate(year, month, day, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + year = date.year(); + month = date.month(); + day = date.day(); + if (year < 0) { year++; } // No year zero + // Jean Meeus algorithm, "Astronomical Algorithms", 1991 + if (month < 3) { + month += 12; + year--; + } + var a = Math.floor(year / 100); + var b = 2 - a + Math.floor(a / 4); + return Math.floor(365.25 * (year + 4716)) + + Math.floor(30.6001 * (month + 1)) + day + b - 1524.5; + }, + + /** Create a new date from a Julian date. + @memberof GregorianCalendar + @param jd {number} The Julian date to convert. + @return {CDate} The equivalent date. */ + fromJD: function(jd) { + // Jean Meeus algorithm, "Astronomical Algorithms", 1991 + var z = Math.floor(jd + 0.5); + var a = Math.floor((z - 1867216.25) / 36524.25); + a = z + 1 + a - Math.floor(a / 4); + var b = a + 1524; + var c = Math.floor((b - 122.1) / 365.25); + var d = Math.floor(365.25 * c); + var e = Math.floor((b - d) / 30.6001); + var day = b - d - Math.floor(e * 30.6001); + var month = e - (e > 13.5 ? 13 : 1); + var year = c - (month > 2.5 ? 4716 : 4715); + if (year <= 0) { year--; } // No year zero + return this.newDate(year, month, day); + }, + + /** Convert this date to a standard (Gregorian) JavaScript Date. + @memberof GregorianCalendar + @param year {CDate|number} The date to convert or the year to convert. + @param [month] {number} The month to convert. + @param [day] {number} The day to convert. + @return {Date} The equivalent JavaScript date. + @throws Error if an invalid date or a different calendar used. */ + toJSDate: function(year, month, day) { + var date = this._validate(year, month, day, + _exports.local.invalidDate || _exports.regionalOptions[''].invalidDate); + var jsd = new Date(date.year(), date.month() - 1, date.day()); + jsd.setHours(0); + jsd.setMinutes(0); + jsd.setSeconds(0); + jsd.setMilliseconds(0); + // Hours may be non-zero on daylight saving cut-over: + // > 12 when midnight changeover, but then cannot generate + // midnight datetime, so jump to 1AM, otherwise reset. + jsd.setHours(jsd.getHours() > 12 ? jsd.getHours() + 2 : 0); + return jsd; + }, + + /** Create a new date from a standard (Gregorian) JavaScript Date. + @memberof GregorianCalendar + @param jsd {Date} The JavaScript date to convert. + @return {CDate} The equivalent date. */ + fromJSDate: function(jsd) { + return this.newDate(jsd.getFullYear(), jsd.getMonth() + 1, jsd.getDate()); + } +}); + +// Singleton manager +var _exports = module.exports = new Calendars(); + +// Date template +_exports.cdate = CDate; + +// Base calendar template +_exports.baseCalendar = BaseCalendar; + +// Gregorian calendar implementation +_exports.calendars.gregorian = GregorianCalendar; + + +},{"object-assign":446}],1034:[function(require,module,exports){ +/* + * World Calendars + * https://github.com/alexcjohnson/world-calendars + * + * Batch-converted from kbwood/calendars + * Many thanks to Keith Wood and all of the contributors to the original project! + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/* http://keith-wood.name/calendars.html + Calendars extras for jQuery v2.0.2. + Written by Keith Wood (wood.keith{at}optusnet.com.au) August 2009. + Available under the MIT (http://keith-wood.name/licence.html) license. + Please attribute the author if you use it. */ + +var assign = require('object-assign'); +var main = require('./main'); + + +assign(main.regionalOptions[''], { + invalidArguments: 'Invalid arguments', + invalidFormat: 'Cannot format a date from another calendar', + missingNumberAt: 'Missing number at position {0}', + unknownNameAt: 'Unknown name at position {0}', + unexpectedLiteralAt: 'Unexpected literal at position {0}', + unexpectedText: 'Additional text found at end' +}); +main.local = main.regionalOptions['']; + +assign(main.cdate.prototype, { + + /** Format this date. + Found in the jquery.calendars.plus.js module. + @memberof CDate + @param [format] {string} The date format to use (see formatDate). + @param [settings] {object} Options for the formatDate function. + @return {string} The formatted date. */ + formatDate: function(format, settings) { + if (typeof format !== 'string') { + settings = format; + format = ''; + } + return this._calendar.formatDate(format || '', this, settings); + } +}); + +assign(main.baseCalendar.prototype, { + + UNIX_EPOCH: main.instance().newDate(1970, 1, 1).toJD(), + SECS_PER_DAY: 24 * 60 * 60, + TICKS_EPOCH: main.instance().jdEpoch, // 1 January 0001 CE + TICKS_PER_DAY: 24 * 60 * 60 * 10000000, + + /** Date form for ATOM (RFC 3339/ISO 8601). + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + ATOM: 'yyyy-mm-dd', + /** Date form for cookies. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + COOKIE: 'D, dd M yyyy', + /** Date form for full date. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + FULL: 'DD, MM d, yyyy', + /** Date form for ISO 8601. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + ISO_8601: 'yyyy-mm-dd', + /** Date form for Julian date. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + JULIAN: 'J', + /** Date form for RFC 822. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + RFC_822: 'D, d M yy', + /** Date form for RFC 850. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + RFC_850: 'DD, dd-M-yy', + /** Date form for RFC 1036. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + RFC_1036: 'D, d M yy', + /** Date form for RFC 1123. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + RFC_1123: 'D, d M yyyy', + /** Date form for RFC 2822. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + RFC_2822: 'D, d M yyyy', + /** Date form for RSS (RFC 822). + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + RSS: 'D, d M yy', + /** Date form for Windows ticks. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + TICKS: '!', + /** Date form for Unix timestamp. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + TIMESTAMP: '@', + /** Date form for W3c (ISO 8601). + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar */ + W3C: 'yyyy-mm-dd', + + /** Format a date object into a string value. + The format can be combinations of the following: +
    +
  • d - day of month (no leading zero)
  • +
  • dd - day of month (two digit)
  • +
  • o - day of year (no leading zeros)
  • +
  • oo - day of year (three digit)
  • +
  • D - day name short
  • +
  • DD - day name long
  • +
  • w - week of year (no leading zero)
  • +
  • ww - week of year (two digit)
  • +
  • m - month of year (no leading zero)
  • +
  • mm - month of year (two digit)
  • +
  • M - month name short
  • +
  • MM - month name long
  • +
  • yy - year (two digit)
  • +
  • yyyy - year (four digit)
  • +
  • YYYY - formatted year
  • +
  • J - Julian date (days since January 1, 4713 BCE Greenwich noon)
  • +
  • @ - Unix timestamp (s since 01/01/1970)
  • +
  • ! - Windows ticks (100ns since 01/01/0001)
  • +
  • '...' - literal text
  • +
  • '' - single quote
  • +
+ Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar + @param [format] {string} The desired format of the date (defaults to calendar format). + @param date {CDate} The date value to format. + @param [settings] {object} Addition options, whose attributes include: + @property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday. + @property [dayNames] {string[]} Names of the days from Sunday. + @property [monthNamesShort] {string[]} Abbreviated names of the months. + @property [monthNames] {string[]} Names of the months. + @property [calculateWeek] {CalendarsPickerCalculateWeek} Function that determines week of the year. + @property [localNumbers=false] {boolean} true to localise numbers (if available), + false to use normal Arabic numerals. + @return {string} The date in the above format. + @throws Errors if the date is from a different calendar. */ + formatDate: function(format, date, settings) { + if (typeof format !== 'string') { + settings = date; + date = format; + format = ''; + } + if (!date) { + return ''; + } + if (date.calendar() !== this) { + throw main.local.invalidFormat || main.regionalOptions[''].invalidFormat; + } + format = format || this.local.dateFormat; + settings = settings || {}; + var dayNamesShort = settings.dayNamesShort || this.local.dayNamesShort; + var dayNames = settings.dayNames || this.local.dayNames; + var monthNumbers = settings.monthNumbers || this.local.monthNumbers; + var monthNamesShort = settings.monthNamesShort || this.local.monthNamesShort; + var monthNames = settings.monthNames || this.local.monthNames; + var calculateWeek = settings.calculateWeek || this.local.calculateWeek; + // Check whether a format character is doubled + var doubled = function(match, step) { + var matches = 1; + while (iFormat + matches < format.length && format.charAt(iFormat + matches) === match) { + matches++; + } + iFormat += matches - 1; + return Math.floor(matches / (step || 1)) > 1; + }; + // Format a number, with leading zeroes if necessary + var formatNumber = function(match, value, len, step) { + var num = '' + value; + if (doubled(match, step)) { + while (num.length < len) { + num = '0' + num; + } + } + return num; + }; + // Format a name, short or long as requested + var formatName = function(match, value, shortNames, longNames) { + return (doubled(match) ? longNames[value] : shortNames[value]); + }; + // Format month number + // (e.g. Chinese calendar needs to account for intercalary months) + var calendar = this; + var formatMonth = function(date) { + return (typeof monthNumbers === 'function') ? + monthNumbers.call(calendar, date, doubled('m')) : + localiseNumbers(formatNumber('m', date.month(), 2)); + }; + // Format a month name, short or long as requested + var formatMonthName = function(date, useLongName) { + if (useLongName) { + return (typeof monthNames === 'function') ? + monthNames.call(calendar, date) : + monthNames[date.month() - calendar.minMonth]; + } else { + return (typeof monthNamesShort === 'function') ? + monthNamesShort.call(calendar, date) : + monthNamesShort[date.month() - calendar.minMonth]; + } + }; + // Localise numbers if requested and available + var digits = this.local.digits; + var localiseNumbers = function(value) { + return (settings.localNumbers && digits ? digits(value) : value); + }; + var output = ''; + var literal = false; + for (var iFormat = 0; iFormat < format.length; iFormat++) { + if (literal) { + if (format.charAt(iFormat) === "'" && !doubled("'")) { + literal = false; + } + else { + output += format.charAt(iFormat); + } + } + else { + switch (format.charAt(iFormat)) { + case 'd': output += localiseNumbers(formatNumber('d', date.day(), 2)); break; + case 'D': output += formatName('D', date.dayOfWeek(), + dayNamesShort, dayNames); break; + case 'o': output += formatNumber('o', date.dayOfYear(), 3); break; + case 'w': output += formatNumber('w', date.weekOfYear(), 2); break; + case 'm': output += formatMonth(date); break; + case 'M': output += formatMonthName(date, doubled('M')); break; + case 'y': + output += (doubled('y', 2) ? date.year() : + (date.year() % 100 < 10 ? '0' : '') + date.year() % 100); + break; + case 'Y': + doubled('Y', 2); + output += date.formatYear(); + break; + case 'J': output += date.toJD(); break; + case '@': output += (date.toJD() - this.UNIX_EPOCH) * this.SECS_PER_DAY; break; + case '!': output += (date.toJD() - this.TICKS_EPOCH) * this.TICKS_PER_DAY; break; + case "'": + if (doubled("'")) { + output += "'"; + } + else { + literal = true; + } + break; + default: + output += format.charAt(iFormat); + } + } + } + return output; + }, + + /** Parse a string value into a date object. + See formatDate for the possible formats, plus: +
    +
  • * - ignore rest of string
  • +
+ Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar + @param format {string} The expected format of the date ('' for default calendar format). + @param value {string} The date in the above format. + @param [settings] {object} Additional options whose attributes include: + @property [shortYearCutoff] {number} The cutoff year for determining the century. + @property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday. + @property [dayNames] {string[]} Names of the days from Sunday. + @property [monthNamesShort] {string[]} Abbreviated names of the months. + @property [monthNames] {string[]} Names of the months. + @return {CDate} The extracted date value or null if value is blank. + @throws Errors if the format and/or value are missing, + if the value doesn't match the format, or if the date is invalid. */ + parseDate: function(format, value, settings) { + if (value == null) { + throw main.local.invalidArguments || main.regionalOptions[''].invalidArguments; + } + value = (typeof value === 'object' ? value.toString() : value + ''); + if (value === '') { + return null; + } + format = format || this.local.dateFormat; + settings = settings || {}; + var shortYearCutoff = settings.shortYearCutoff || this.shortYearCutoff; + shortYearCutoff = (typeof shortYearCutoff !== 'string' ? shortYearCutoff : + this.today().year() % 100 + parseInt(shortYearCutoff, 10)); + var dayNamesShort = settings.dayNamesShort || this.local.dayNamesShort; + var dayNames = settings.dayNames || this.local.dayNames; + var parseMonth = settings.parseMonth || this.local.parseMonth; + var monthNumbers = settings.monthNumbers || this.local.monthNumbers; + var monthNamesShort = settings.monthNamesShort || this.local.monthNamesShort; + var monthNames = settings.monthNames || this.local.monthNames; + var jd = -1; + var year = -1; + var month = -1; + var day = -1; + var doy = -1; + var shortYear = false; + var literal = false; + // Check whether a format character is doubled + var doubled = function(match, step) { + var matches = 1; + while (iFormat + matches < format.length && format.charAt(iFormat + matches) === match) { + matches++; + } + iFormat += matches - 1; + return Math.floor(matches / (step || 1)) > 1; + }; + // Extract a number from the string value + var getNumber = function(match, step) { + var isDoubled = doubled(match, step); + var size = [2, 3, isDoubled ? 4 : 2, isDoubled ? 4 : 2, 10, 11, 20]['oyYJ@!'.indexOf(match) + 1]; + var digits = new RegExp('^-?\\d{1,' + size + '}'); + var num = value.substring(iValue).match(digits); + if (!num) { + throw (main.local.missingNumberAt || main.regionalOptions[''].missingNumberAt). + replace(/\{0\}/, iValue); + } + iValue += num[0].length; + return parseInt(num[0], 10); + }; + // Extract a month number from the string value + var calendar = this; + var getMonthNumber = function() { + if (typeof monthNumbers === 'function') { + doubled('m'); // update iFormat + var month = monthNumbers.call(calendar, value.substring(iValue)); + iValue += month.length; + return month; + } + + return getNumber('m'); + }; + // Extract a name from the string value and convert to an index + var getName = function(match, shortNames, longNames, step) { + var names = (doubled(match, step) ? longNames : shortNames); + for (var i = 0; i < names.length; i++) { + if (value.substr(iValue, names[i].length).toLowerCase() === names[i].toLowerCase()) { + iValue += names[i].length; + return i + calendar.minMonth; + } + } + throw (main.local.unknownNameAt || main.regionalOptions[''].unknownNameAt). + replace(/\{0\}/, iValue); + }; + // Extract a month number from the string value + var getMonthName = function() { + if (typeof monthNames === 'function') { + var month = doubled('M') ? + monthNames.call(calendar, value.substring(iValue)) : + monthNamesShort.call(calendar, value.substring(iValue)); + iValue += month.length; + return month; + } + + return getName('M', monthNamesShort, monthNames); + }; + // Confirm that a literal character matches the string value + var checkLiteral = function() { + if (value.charAt(iValue) !== format.charAt(iFormat)) { + throw (main.local.unexpectedLiteralAt || + main.regionalOptions[''].unexpectedLiteralAt).replace(/\{0\}/, iValue); + } + iValue++; + }; + var iValue = 0; + for (var iFormat = 0; iFormat < format.length; iFormat++) { + if (literal) { + if (format.charAt(iFormat) === "'" && !doubled("'")) { + literal = false; + } + else { + checkLiteral(); + } + } + else { + switch (format.charAt(iFormat)) { + case 'd': day = getNumber('d'); break; + case 'D': getName('D', dayNamesShort, dayNames); break; + case 'o': doy = getNumber('o'); break; + case 'w': getNumber('w'); break; + case 'm': month = getMonthNumber(); break; + case 'M': month = getMonthName(); break; + case 'y': + var iSave = iFormat; + shortYear = !doubled('y', 2); + iFormat = iSave; + year = getNumber('y', 2); + break; + case 'Y': year = getNumber('Y', 2); break; + case 'J': + jd = getNumber('J') + 0.5; + if (value.charAt(iValue) === '.') { + iValue++; + getNumber('J'); + } + break; + case '@': jd = getNumber('@') / this.SECS_PER_DAY + this.UNIX_EPOCH; break; + case '!': jd = getNumber('!') / this.TICKS_PER_DAY + this.TICKS_EPOCH; break; + case '*': iValue = value.length; break; + case "'": + if (doubled("'")) { + checkLiteral(); + } + else { + literal = true; + } + break; + default: checkLiteral(); + } + } + } + if (iValue < value.length) { + throw main.local.unexpectedText || main.regionalOptions[''].unexpectedText; + } + if (year === -1) { + year = this.today().year(); + } + else if (year < 100 && shortYear) { + year += (shortYearCutoff === -1 ? 1900 : this.today().year() - + this.today().year() % 100 - (year <= shortYearCutoff ? 0 : 100)); + } + if (typeof month === 'string') { + month = parseMonth.call(this, year, month); + } + if (doy > -1) { + month = 1; + day = doy; + for (var dim = this.daysInMonth(year, month); day > dim; dim = this.daysInMonth(year, month)) { + month++; + day -= dim; + } + } + return (jd > -1 ? this.fromJD(jd) : this.newDate(year, month, day)); + }, + + /** A date may be specified as an exact value or a relative one. + Found in the jquery.calendars.plus.js module. + @memberof BaseCalendar + @param dateSpec {CDate|number|string} The date as an object or string in the given format or + an offset - numeric days from today, or string amounts and periods, e.g. '+1m +2w'. + @param defaultDate {CDate} The date to use if no other supplied, may be null. + @param currentDate {CDate} The current date as a possible basis for relative dates, + if null today is used (optional) + @param [dateFormat] {string} The expected date format - see formatDate. + @param [settings] {object} Additional options whose attributes include: + @property [shortYearCutoff] {number} The cutoff year for determining the century. + @property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday. + @property [dayNames] {string[]} Names of the days from Sunday. + @property [monthNamesShort] {string[]} Abbreviated names of the months. + @property [monthNames] {string[]} Names of the months. + @return {CDate} The decoded date. */ + determineDate: function(dateSpec, defaultDate, currentDate, dateFormat, settings) { + if (currentDate && typeof currentDate !== 'object') { + settings = dateFormat; + dateFormat = currentDate; + currentDate = null; + } + if (typeof dateFormat !== 'string') { + settings = dateFormat; + dateFormat = ''; + } + var calendar = this; + var offsetString = function(offset) { + try { + return calendar.parseDate(dateFormat, offset, settings); + } + catch (e) { + // Ignore + } + offset = offset.toLowerCase(); + var date = (offset.match(/^c/) && currentDate ? + currentDate.newDate() : null) || calendar.today(); + var pattern = /([+-]?[0-9]+)\s*(d|w|m|y)?/g; + var matches = pattern.exec(offset); + while (matches) { + date.add(parseInt(matches[1], 10), matches[2] || 'd'); + matches = pattern.exec(offset); + } + return date; + }; + defaultDate = (defaultDate ? defaultDate.newDate() : null); + dateSpec = (dateSpec == null ? defaultDate : + (typeof dateSpec === 'string' ? offsetString(dateSpec) : (typeof dateSpec === 'number' ? + (isNaN(dateSpec) || dateSpec === Infinity || dateSpec === -Infinity ? defaultDate : + calendar.today().add(dateSpec, 'd')) : calendar.newDate(dateSpec)))); + return dateSpec; + } +}); + + +},{"./main":1033,"object-assign":446}],1035:[function(require,module,exports){ +module.exports = require('cwise-compiler')({ + args: ['array', { + offset: [1], + array: 0 + }, 'scalar', 'scalar', 'index'], + pre: { + "body": "{}", + "args": [], + "thisVars": [], + "localVars": [] + }, + post: { + "body": "{}", + "args": [], + "thisVars": [], + "localVars": [] + }, + body: { + "body": "{\n var _inline_1_da = _inline_1_arg0_ - _inline_1_arg3_\n var _inline_1_db = _inline_1_arg1_ - _inline_1_arg3_\n if((_inline_1_da >= 0) !== (_inline_1_db >= 0)) {\n _inline_1_arg2_.push(_inline_1_arg4_[0] + 0.5 + 0.5 * (_inline_1_da + _inline_1_db) / (_inline_1_da - _inline_1_db))\n }\n }", + "args": [{ + "name": "_inline_1_arg0_", + "lvalue": false, + "rvalue": true, + "count": 1 + }, { + "name": "_inline_1_arg1_", + "lvalue": false, + "rvalue": true, + "count": 1 + }, { + "name": "_inline_1_arg2_", + "lvalue": false, + "rvalue": true, + "count": 1 + }, { + "name": "_inline_1_arg3_", + "lvalue": false, + "rvalue": true, + "count": 2 + }, { + "name": "_inline_1_arg4_", + "lvalue": false, + "rvalue": true, + "count": 1 + }], + "thisVars": [], + "localVars": ["_inline_1_da", "_inline_1_db"] + }, + funcName: 'zeroCrossings' +}) + +},{"cwise-compiler":82}],1036:[function(require,module,exports){ +"use strict" + +module.exports = findZeroCrossings + +var core = require("./lib/zc-core") + +function findZeroCrossings(array, level) { + var cross = [] + level = +level || 0.0 + core(array.hi(array.shape[0]-1), cross, level) + return cross +} +},{"./lib/zc-core":1035}],1037:[function(require,module,exports){ if (typeof window !== 'undefined') window.$ = window.jQuery = require('jquery'); ImageSequencer = function ImageSequencer(options) { @@ -184152,7 +184206,7 @@ ImageSequencer = function ImageSequencer(options) { } - // Pre-set the initial output behavior of the final step, + // Pre-set the initial output behavior of the final step, // which will be changed if an additional step is added. module.options.output = function output(image) { if (module.options.ui && module.options.ui.display) module.options.ui.display(image); @@ -184160,7 +184214,7 @@ ImageSequencer = function ImageSequencer(options) { } - // passed image is optional but you can pass a + // passed image is optional but you can pass a // non-stored image through the whole steps chain function run(image) { if (image) steps[1].draw(image); @@ -184199,7 +184253,7 @@ ImageSequencer = function ImageSequencer(options) { module.exports = ImageSequencer; -},{"./Modules":1608,"./UserInterface":1609,"jquery":89}],1608:[function(require,module,exports){ +},{"./Modules":1038,"./UserInterface":1039,"jquery":273}],1038:[function(require,module,exports){ /* * Core modules */ @@ -184209,11 +184263,11 @@ module.exports = { 'green-channel': require('./modules/GreenChannel'), 'ndvi-red': require('./modules/NdviRed'), 'plot': require('./modules/Plot'), - 'image-threshold': require('./modules/ImageThreshold') - + 'image-threshold': require('./modules/ImageThreshold'), + 'crop': require('./modules/Crop') } -},{"./modules/GreenChannel":1610,"./modules/ImageSelect":1611,"./modules/ImageThreshold":1612,"./modules/NdviRed":1613,"./modules/Plot":1615}],1609:[function(require,module,exports){ +},{"./modules/Crop":1040,"./modules/GreenChannel":1041,"./modules/ImageSelect":1042,"./modules/ImageThreshold":1043,"./modules/NdviRed":1044,"./modules/Plot":1046}],1039:[function(require,module,exports){ /* * Default UI for each image-sequencer module */ @@ -184253,7 +184307,70 @@ module.exports = function UserInterface(options) { } -},{}],1610:[function(require,module,exports){ +},{}],1040:[function(require,module,exports){ +/* + * Image Cropping module + * Usage: + * Expected Inputs: + * options.x : x-coordinate of image where the modules starts cropping | default : 0 + * options.y : y-coordinate of image where the modules starts cropping | default : 0 + * options.w : width of the resulting cropped image | default : 50% of input image width + * options.h : height of the resulting cropped image | default : 50% of input image height + * Output: + * The cropped image, which is essentially a rectangle bounded by the lines: + * x = options.x + * x = options.x + options.w + * y = options.y + * y = options.y + options.h + */ + module.exports = function Crop(options){ + + options = options || {}; + options.title = "Crop Image"; + options.format = options.format || "png"; + + function draw(image) { + var getPixels = require("get-pixels"), + savePixels = require("save-pixels"), + base64 = require('base64-stream'); + + getPixels(image.src,function(err,pixels){ + var newdata = []; + var ox = options.x || 0; + var oy = options.y || 0; + var w = options.w || Math.floor(0.5*pixels.shape[0]); + var h = options.h || Math.floor(0.5*pixels.shape[1]); + var iw = pixels.shape[0]; //Width of Original Image + newarray = new Uint8Array(4*w*h); + for (var n = oy; n < oy + h; n++) { + newarray.set(pixels.data.slice(n*4*iw + ox, n*4*iw + ox + 4*w),4*w*(n-oy)); + } + pixels.data = newarray; + pixels.shape = [w,h,4]; + pixels.stride[1] = 4*w; + + var buffer = base64.encode(); + savePixels(pixels, options.format) + .on('end', function() { + + var img = new Image(); + + img.src = 'data:image/' + options.format + ';base64,' + buffer.read().toString(); + + if (options.output) options.output(img); + + }).pipe(buffer); + + }); + } + + return { + options: options, + draw: draw + } + } + +},{"base64-stream":13,"get-pixels":113,"save-pixels":963}],1041:[function(require,module,exports){ /* * Display only the green channel */ @@ -184282,7 +184399,7 @@ module.exports = function GreenChannel(options) { } } -},{"./PixelManipulation.js":1614}],1611:[function(require,module,exports){ +},{"./PixelManipulation.js":1045}],1042:[function(require,module,exports){ /* * Special module to kick off the sequence * -- depends on jQuery for interface setup & drag & drop @@ -184364,12 +184481,11 @@ module.exports = function ImageSelect(options) { } -},{"jquery":89}],1612:[function(require,module,exports){ +},{"jquery":273}],1043:[function(require,module,exports){ /* * Image thresholding with 'image-filter-threshold' */ module.exports = function ImageThreshold(options) { - options = options || {}; options.title = "Threshold image"; options.threshold = options.threshold || 30; @@ -184413,7 +184529,7 @@ module.exports = function ImageThreshold(options) { } } -},{"image-filter-core":85,"image-filter-threshold":86}],1613:[function(require,module,exports){ +},{"image-filter-core":261,"image-filter-threshold":262}],1044:[function(require,module,exports){ /* * NDVI with red filter (blue channel is infrared) */ @@ -184442,7 +184558,7 @@ module.exports = function NdviRed(options) { } } -},{"./PixelManipulation.js":1614}],1614:[function(require,module,exports){ +},{"./PixelManipulation.js":1045}],1045:[function(require,module,exports){ /* * General purpose per-pixel manipulation * accepting a changePixel() method to remix a pixel's channels @@ -184505,7 +184621,7 @@ module.exports = function PixelManipulation(image, options) { } -},{"base64-stream":1,"get-pixels":69,"save-pixels":1606}],1615:[function(require,module,exports){ +},{"base64-stream":13,"get-pixels":113,"save-pixels":963}],1046:[function(require,module,exports){ /* * Plot image on a graph with color bar */ @@ -184580,4 +184696,4 @@ module.exports = function Plot(options) { } } -},{"get-pixels":69,"plotly.js":106}]},{},[1607]); +},{"get-pixels":113,"plotly.js":487}]},{},[1037]); diff --git a/index.html b/index.html index 90bacebe..61b7a5e9 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - + Image Sequencer @@ -48,7 +48,7 @@ - +

@@ -69,6 +69,7 @@ sequencer.addStep('ndvi-red'); sequencer.addStep('image-threshold'); + sequencer.addStep('crop'); //sequencer.addStep('plot'); $('.add-step').click(function(e) { diff --git a/src/ImageSequencer.js b/src/ImageSequencer.js index 901c612c..828eaf9e 100644 --- a/src/ImageSequencer.js +++ b/src/ImageSequencer.js @@ -60,7 +60,7 @@ ImageSequencer = function ImageSequencer(options) { } - // Pre-set the initial output behavior of the final step, + // Pre-set the initial output behavior of the final step, // which will be changed if an additional step is added. module.options.output = function output(image) { if (module.options.ui && module.options.ui.display) module.options.ui.display(image); @@ -68,7 +68,7 @@ ImageSequencer = function ImageSequencer(options) { } - // passed image is optional but you can pass a + // passed image is optional but you can pass a // non-stored image through the whole steps chain function run(image) { if (image) steps[1].draw(image); diff --git a/src/Modules.js b/src/Modules.js index 0c344a06..f7c6f145 100644 --- a/src/Modules.js +++ b/src/Modules.js @@ -7,6 +7,6 @@ module.exports = { 'green-channel': require('./modules/GreenChannel'), 'ndvi-red': require('./modules/NdviRed'), 'plot': require('./modules/Plot'), - 'image-threshold': require('./modules/ImageThreshold') - + 'image-threshold': require('./modules/ImageThreshold'), + 'crop': require('./modules/Crop') } diff --git a/src/modules/Crop.js b/src/modules/Crop.js new file mode 100644 index 00000000..c98dc63e --- /dev/null +++ b/src/modules/Crop.js @@ -0,0 +1,61 @@ +/* + * Image Cropping module + * Usage: + * Expected Inputs: + * options.x : x-coordinate of image where the modules starts cropping | default : 0 + * options.y : y-coordinate of image where the modules starts cropping | default : 0 + * options.w : width of the resulting cropped image | default : 50% of input image width + * options.h : height of the resulting cropped image | default : 50% of input image height + * Output: + * The cropped image, which is essentially a rectangle bounded by the lines: + * x = options.x + * x = options.x + options.w + * y = options.y + * y = options.y + options.h + */ + module.exports = function Crop(options){ + + options = options || {}; + options.title = "Crop Image"; + options.format = options.format || "png"; + + function draw(image) { + var getPixels = require("get-pixels"), + savePixels = require("save-pixels"), + base64 = require('base64-stream'); + + getPixels(image.src,function(err,pixels){ + var newdata = []; + var ox = options.x || 0; + var oy = options.y || 0; + var w = options.w || Math.floor(0.5*pixels.shape[0]); + var h = options.h || Math.floor(0.5*pixels.shape[1]); + var iw = pixels.shape[0]; //Width of Original Image + newarray = new Uint8Array(4*w*h); + for (var n = oy; n < oy + h; n++) { + newarray.set(pixels.data.slice(n*4*iw + ox, n*4*iw + ox + 4*w),4*w*(n-oy)); + } + pixels.data = newarray; + pixels.shape = [w,h,4]; + pixels.stride[1] = 4*w; + + var buffer = base64.encode(); + savePixels(pixels, options.format) + .on('end', function() { + + var img = new Image(); + + img.src = 'data:image/' + options.format + ';base64,' + buffer.read().toString(); + + if (options.output) options.output(img); + + }).pipe(buffer); + + }); + } + + return { + options: options, + draw: draw + } + } diff --git a/src/modules/ImageThreshold.js b/src/modules/ImageThreshold.js index 65b304ef..e5ffb41b 100644 --- a/src/modules/ImageThreshold.js +++ b/src/modules/ImageThreshold.js @@ -2,7 +2,6 @@ * Image thresholding with 'image-filter-threshold' */ module.exports = function ImageThreshold(options) { - options = options || {}; options.title = "Threshold image"; options.threshold = options.threshold || 30; From 7f447928912a02e8c993ab6b94c32164f7bc133f Mon Sep 17 00:00:00 2001 From: Chinmay Pandhare Date: Tue, 14 Mar 2017 14:27:36 +0530 Subject: [PATCH 2/5] Merging Crop.js --- dist/image-sequencer.js | 20 ++++++++++++++++---- src/modules/Crop.js | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/dist/image-sequencer.js b/dist/image-sequencer.js index f7fe0d77..fd1964a0 100644 --- a/dist/image-sequencer.js +++ b/dist/image-sequencer.js @@ -184315,6 +184315,18 @@ module.exports = function UserInterface(options) { },{}],1040:[function(require,module,exports){ /* * Image Cropping module + * Usage: + * Expected Inputs: + * options.x : x-coordinate of image where the modules starts cropping | default : 0 + * options.y : y-coordinate of image where the modules starts cropping | default : 0 + * options.w : width of the resulting cropped image | default : 50% of input image width + * options.h : height of the resulting cropped image | default : 50% of input image height + * Output: + * The cropped image, which is essentially a rectangle bounded by the lines: + * x = options.x + * x = options.x + options.w + * y = options.y + * y = options.y + options.h */ module.exports = function Crop(options){ @@ -184329,10 +184341,10 @@ module.exports = function UserInterface(options) { getPixels(image.src,function(err,pixels){ var newdata = []; - var ox = options.cropX || 0; //Where to begin the crop on X axis - var oy = options.cropY || 0; //Where to begin the crop on Y axis - var w = options.cropL || Math.floor(0.5*pixels.shape[0]); //Length of crop - var h = options.cropH || Math.floor(0.5*pixels.shape[1]); //Height of crop + var ox = options.x || 0; + var oy = options.y || 0; + var w = options.w || Math.floor(0.5*pixels.shape[0]); + var h = options.h || Math.floor(0.5*pixels.shape[1]); var iw = pixels.shape[0]; //Width of Original Image newarray = new Uint8Array(4*w*h); for (var n = oy; n < oy + h; n++) { diff --git a/src/modules/Crop.js b/src/modules/Crop.js index bde46030..c98dc63e 100644 --- a/src/modules/Crop.js +++ b/src/modules/Crop.js @@ -1,5 +1,17 @@ /* * Image Cropping module + * Usage: + * Expected Inputs: + * options.x : x-coordinate of image where the modules starts cropping | default : 0 + * options.y : y-coordinate of image where the modules starts cropping | default : 0 + * options.w : width of the resulting cropped image | default : 50% of input image width + * options.h : height of the resulting cropped image | default : 50% of input image height + * Output: + * The cropped image, which is essentially a rectangle bounded by the lines: + * x = options.x + * x = options.x + options.w + * y = options.y + * y = options.y + options.h */ module.exports = function Crop(options){ @@ -14,10 +26,10 @@ getPixels(image.src,function(err,pixels){ var newdata = []; - var ox = options.x || 0; //Where to begin the crop on X axis - var oy = options.y || 0; //Where to begin the crop on Y axis - var w = options.l || Math.floor(0.5*pixels.shape[0]); //Length of crop - var h = options.h || Math.floor(0.5*pixels.shape[1]); //Height of crop + var ox = options.x || 0; + var oy = options.y || 0; + var w = options.w || Math.floor(0.5*pixels.shape[0]); + var h = options.h || Math.floor(0.5*pixels.shape[1]); var iw = pixels.shape[0]; //Width of Original Image newarray = new Uint8Array(4*w*h); for (var n = oy; n < oy + h; n++) { From b1bdbd1574494ed33f3da0a21c1e9da8d6f6e573 Mon Sep 17 00:00:00 2001 From: Chinmay Pandhare Date: Wed, 15 Mar 2017 00:34:54 +0530 Subject: [PATCH 3/5] Merging Crop.js --- dist/image-sequencer.js | 1 - src/ImageSequencer.js | 1 - 2 files changed, 2 deletions(-) diff --git a/dist/image-sequencer.js b/dist/image-sequencer.js index fd1964a0..f452972e 100644 --- a/dist/image-sequencer.js +++ b/dist/image-sequencer.js @@ -184151,7 +184151,6 @@ ImageSequencer = function ImageSequencer(options) { options = options || {}; options.inBrowser = options.inBrowser || typeof window !== 'undefined'; if (options.inBrowser) options.ui = options.ui || require('./UserInterface'); - options.initialImage = ""; var image, steps = [], diff --git a/src/ImageSequencer.js b/src/ImageSequencer.js index fa34eacb..c26e59f3 100644 --- a/src/ImageSequencer.js +++ b/src/ImageSequencer.js @@ -5,7 +5,6 @@ ImageSequencer = function ImageSequencer(options) { options = options || {}; options.inBrowser = options.inBrowser || typeof window !== 'undefined'; if (options.inBrowser) options.ui = options.ui || require('./UserInterface'); - options.initialImage = ""; var image, steps = [], From 90da255d51310442f952b740ba61476f9a3ba9bb Mon Sep 17 00:00:00 2001 From: Chinmay Pandhare Date: Wed, 15 Mar 2017 00:41:58 +0530 Subject: [PATCH 4/5] Merging Crop.js --- src/ImageSequencer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSequencer.js b/src/ImageSequencer.js index c26e59f3..a48096cf 100644 --- a/src/ImageSequencer.js +++ b/src/ImageSequencer.js @@ -86,7 +86,6 @@ ImageSequencer = function ImageSequencer(options) { // load default starting image // i.e. from parameter // this could send the image to ImageSelect, or something? -// not currently working function loadImage(src, callback) { image = new Image(); image.onload = function() { From 38a569b958363159ff447b3149dd5091a71a0708 Mon Sep 17 00:00:00 2001 From: "Chinmay Pandhare (CCD)" <1000011C1010000@gmail.com> Date: Wed, 15 Mar 2017 02:03:55 +0530 Subject: [PATCH 5/5] Fix for Issue9 : Error while adding a new step from HTML UI, A small fix for #7 (#16) * Fix : add a step in the demo * Change global variable to document.sequencer_image * Change global variable to document.sequencer_image * Small fix for FireFox; Ref: Issue #7 * Change document.sequencer_image to options.image * Change document.sequencer_image to options.initial_image * Completed the addStep issue. * Completed the addStep issue. * Change options.initial_image to options.initialImage * Added Crop Module * lowercase parameters * Merging Crop.js * Merging Crop.js * Merging Crop.js --- dist/image-sequencer.js | 9 +++++++-- index.html | 3 +-- src/ImageSequencer.js | 5 ++++- src/modules/ImageSelect.js | 5 +++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dist/image-sequencer.js b/dist/image-sequencer.js index a879db02..f452972e 100644 --- a/dist/image-sequencer.js +++ b/dist/image-sequencer.js @@ -184197,6 +184197,9 @@ ImageSequencer = function ImageSequencer(options) { if (previousStep) { // connect output of last step to input of this step previousStep.options.output = function output(image) { + if (sequencer.steps[0].options.initialImage) { + options.initialImage = sequencer.steps[0].options.initialImage; + } log('running module "' + name + '"'); // display the image in any available ui if (previousStep.options.ui && previousStep.options.ui.display) previousStep.options.ui.display(image); @@ -184235,6 +184238,7 @@ ImageSequencer = function ImageSequencer(options) { image.onload = function() { run(image); if (callback) callback(image); + options.initialImage = image; } image.src = src; } @@ -184443,8 +184447,8 @@ module.exports = function ImageSelect(options) { // we should trigger "load" event here image = new Image(); - image.src = event.target.result; - + image.src = e.target.result; + options.initialImage = image; el.html(image); // may be redundant // this is done once per image: @@ -184470,6 +184474,7 @@ module.exports = function ImageSelect(options) { // this module is unique because it creates the image function draw(image) { el.html(image); + options.initialImage = image; if (options.output) options.output(image); } diff --git a/index.html b/index.html index 61b7a5e9..7b410446 100644 --- a/index.html +++ b/index.html @@ -73,10 +73,9 @@ //sequencer.addStep('plot'); $('.add-step').click(function(e) { - e.preventDefault(); sequencer.addStep($('.select-module').val()); - sequencer.run(); // later we might only run this step, if we can fetch the image output from the previous + sequencer.run(sequencer.options.initialImage); // later we might only run this step, if we can fetch the image output from the previous }); diff --git a/src/ImageSequencer.js b/src/ImageSequencer.js index 828eaf9e..a48096cf 100644 --- a/src/ImageSequencer.js +++ b/src/ImageSequencer.js @@ -51,6 +51,9 @@ ImageSequencer = function ImageSequencer(options) { if (previousStep) { // connect output of last step to input of this step previousStep.options.output = function output(image) { + if (sequencer.steps[0].options.initialImage) { + options.initialImage = sequencer.steps[0].options.initialImage; + } log('running module "' + name + '"'); // display the image in any available ui if (previousStep.options.ui && previousStep.options.ui.display) previousStep.options.ui.display(image); @@ -83,12 +86,12 @@ ImageSequencer = function ImageSequencer(options) { // load default starting image // i.e. from parameter // this could send the image to ImageSelect, or something? -// not currently working function loadImage(src, callback) { image = new Image(); image.onload = function() { run(image); if (callback) callback(image); + options.initialImage = image; } image.src = src; } diff --git a/src/modules/ImageSelect.js b/src/modules/ImageSelect.js index d375fd79..a4d14251 100644 --- a/src/modules/ImageSelect.js +++ b/src/modules/ImageSelect.js @@ -41,8 +41,8 @@ module.exports = function ImageSelect(options) { // we should trigger "load" event here image = new Image(); - image.src = event.target.result; - + image.src = e.target.result; + options.initialImage = image; el.html(image); // may be redundant // this is done once per image: @@ -68,6 +68,7 @@ module.exports = function ImageSelect(options) { // this module is unique because it creates the image function draw(image) { el.html(image); + options.initialImage = image; if (options.output) options.output(image); }