From 5937b25aca3a2cf20dee9dac670ddf8346a8e087 Mon Sep 17 00:00:00 2001 From: Jan Marsch Date: Fri, 1 Apr 2016 02:50:41 +0200 Subject: [PATCH] build setup --- Gruntfile.js | 182 +--- LICENSE.md | 79 +- README.md | 330 ------- dist/Triangulate.debug.js | 1225 ++++++++++++++++++++++++++ dist/Triangulate.js | 1 + package.json | 3 +- Triangulate.js => src/Triangulate.js | 0 7 files changed, 1249 insertions(+), 571 deletions(-) create mode 100644 dist/Triangulate.debug.js create mode 100644 dist/Triangulate.js rename Triangulate.js => src/Triangulate.js (100%) diff --git a/Gruntfile.js b/Gruntfile.js index 4a4b48b..fa5b470 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,196 +1,54 @@ -var fs = require('fs'); - module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), - concat: { - glx: { - options: { - separator: "\n", - banner: "(function(global) {", - footer: "}(this));", - sourceMap: true - }, - src: [ - "src/glx/index.js", - "src/glx/prefix.js", - "src/glx/util.js", - "src/glx/Buffer.js", - "src/glx/Framebuffer.js", - "src/glx/Shader.js", - "src/glx/Matrix.js", - "src/glx/Texture.js", - "src/glx/texture/index.js", - "src/glx/texture/Image.js", - "src/glx/texture/Data.js", - "src/glx/mesh/index.js", - "src/glx/mesh/Triangle.js", - "src/glx/mesh/Plane.js", - "src/glx/mesh/Cube.js", - "src/glx/suffix.js" - ], - dest: 'lib/GLX.debug.js' + jshint: { + options: { + globals: {} }, + all: 'src/Triangulate.js' + }, - 'osmb-basemap': { + concat: { + default: { options: { separator: "\n", banner: "(function(global) {", - footer: "}(this));", - sourceMap: true + footer: "}(this));" }, src: [ - 'src/engines/Basemap/index.js', - 'src/engines/Basemap/Pointer.js', - 'src/engines/Basemap/Layers.js', - grunt.file.readJSON('config.json').lib, - grunt.file.readJSON('config.json').src + "node_modules/earcut/dist/earcut.dev.js", + "node_modules/Color/dist/Color.debug.js", + "src/Triangulate.js" ], - dest: 'dist/OSMBuildings/<%=pkg.name%>.debug.js' - } - }, - - copy: { - 'assets': { - src: 'src/skydome.jpg', - dest: 'dist/OSMBuildings/skydome.jpg' - }, - 'css': { - src: 'src/engines/Basemap/style.css', - dest: 'dist/OSMBuildings/<%=pkg.name%>.css' + dest: 'dist/<%=pkg.name%>.debug.js' } }, uglify: { - 'osmb-basemap': { - options: { - sourceMap: true - }, - src: 'dist/OSMBuildings/<%=pkg.name%>.debug.js', - dest: 'dist/OSMBuildings/<%=pkg.name%>.js' - } - }, - - shaders: { - dist: { - src: 'src/shader', - dest: 'src/Shaders.min.js', - names: grunt.file.readJSON('config.json').shaders - } - }, - - version: { - dist: { - src: './dist/OSMBuildings/<%=pkg.name%>.debug.js', - mapping: { - '{{VERSION}}': '<%=pkg.version%>' - } - } - }, - - clean: { - dist: ['./dist/OSMBuildings/<%=pkg.name%>.pack.js'] - }, - - jshint: { - options: { - globals: {} - }, - all: grunt.file.readJSON('config.json').src - }, - - compress: { - main: { - options: { - level: 5, - archive: 'dist/<%=pkg.name%>-<%=pkg.version%>.zip' - }, - files: [ - { expand: true, cwd: 'dist/', src: ['<%=pkg.name%>/*', 'index.html'] } - ] - } - }, - - // just testing, whether wepack *would* work - webpack: { - test: { - entry: './dist/OSMBuildings/<%=pkg.name%>.debug.js', - output: { - path: './dist/OSMBuildings', - filename: '<%=pkg.name%>.pack.js', - }, - stats: false, // the stats output - progress: false, // show progress - failOnError: true, // don't report error to grunt if webpack find errors - watch: false, - keepalive: true // don't finish the grunt task + default: { + options: {}, + src: 'dist/<%=pkg.name%>.debug.js', + dest: 'dist/<%=pkg.name%>.js' } } }); grunt.loadNpmTasks('grunt-contrib-concat'); - grunt.loadNpmTasks('grunt-contrib-copy'); - grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-jshint'); - grunt.loadNpmTasks('grunt-contrib-compress'); - grunt.loadNpmTasks('grunt-webpack'); - - grunt.registerMultiTask('version', 'Set version number', function() { - var config = this.data; - - var content = '' + fs.readFileSync(config.src); - - for (var tag in config.mapping) { - content = content.replace(tag, config.mapping[tag]); - } - - fs.writeFileSync(config.src, content); - }); - - grunt.registerMultiTask('shaders', 'Build shaders', function() { - // grunt.log.writeln(JSON.stringify(this.data)); - var config = this.data; - - var src, name, Shaders = {}; - for (var i = 0; i < config.names.length; i++) { - name = config.names[i]; - Shaders[name] = {}; - - src = fs.readFileSync(config.src + '/' + name + '.vs', 'ascii'); - Shaders[name].vertex = src.replace(/'/g, "\'").replace(/[\r\n]+/g, '\n'); - - src = fs.readFileSync(config.src + '/' + name + '.fs', 'ascii'); - Shaders[name].fragment = src.replace(/'/g, "\'").replace(/[\r\n]+/g, '\n'); - } - - fs.writeFileSync(config.dest, 'var Shaders = '+ JSON.stringify(Shaders) +';\n'); - }); + grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', 'Development build', function() { grunt.log.writeln('\033[1;36m'+ grunt.template.date(new Date(), 'yyyy-mm-dd HH:MM:ss') +'\033[0m'); - grunt.task.run('shaders'); - grunt.task.run('concat:osmb-basemap'); - grunt.task.run('version'); + grunt.task.run('concat'); + grunt.task.run('uglify'); }); grunt.registerTask('release', 'Release', function() { grunt.log.writeln('\033[1;36m'+ grunt.template.date(new Date(), 'yyyy-mm-dd HH:MM:ss') +'\033[0m'); - grunt.task.run('jshint'); - - grunt.task.run('concat:glx'); - grunt.task.run('shaders'); - grunt.task.run('concat:osmb-basemap'); - grunt.task.run('version'); - grunt.task.run('uglify:osmb-basemap'); - - grunt.task.run('copy:assets'); - grunt.task.run('copy:css'); - - grunt.task.run('compress'); - grunt.task.run('webpack'); + grunt.task.run('default'); }); }; diff --git a/LICENSE.md b/LICENSE.md index 9ef2dfd..2ccc3c9 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ -OSM Buildings -https://github.com/OSMBuildings/OSMBuildings +Triangulate +https://github.com/OSMBuildings/Triangulates Copyright (c) 2016, Jan Marsch, OSM Buildings Color.js @@ -48,78 +48,3 @@ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -=============================================================================== - -Suncalc -https://github.com/mourner/suncalc/ -Copyright (c) 2014, Vladimir Agafonkin - -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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE -COPYRIGHT HOLDER 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. - -=============================================================================== - -Clockwise winding check -https://github.com/Turfjs/turf-rewind -Abel Vázquez - -Uses Shoelace Formula (http://en.wikipedia.org/wiki/Shoelace_formula) - -The MIT License (MIT) - -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. - -=============================================================================== - -several code fragments from osmstreetview -https://github.com/rbuch703/osmstreetview/ -Robert Buchholz - -Copyright 2014, Robert Buchholz . - -It is licensed under the GNU General Public License version 3. - -=============================================================================== - -Skydome image - - -Taken from http://blenderartists.org/forum/showthread.php?24038-Free-high-res-skymaps-%28Massive-07-update!%29 - -"There are no restrictions on using these textures, you can use these for -commercial work, and you don't have to name me if you don't want to. the only -restriction is that you can't sell them. that's all." diff --git a/README.md b/README.md index ed335e1..e69de29 100644 --- a/README.md +++ b/README.md @@ -1,330 +0,0 @@ - -# OSM Buildings - - - -OSM Buildings is a JavaScript library for visualizing OpenStreetMap building geometry on 2D and 3D maps. - -**Please note: we are a very small team with little time for the project and limited funds. -You could help us a lot in advancing the project with spreading the word, donations, code contributions and testing.** - -The library version in this repository is a WebGL only variant of OSM Buildings. -At some point it will fully integrate the Classic 2.5D version. - -[Example](http://osmbuildings.org/gl/?lat=40.70614&lon=-74.01039&zoom=17.00&rotation=0&tilt=40) - -For the latest information about the project [follow us on Twitter](https://twitter.com/osmbuildings), read [our blog](http://blog.osmbuildings.org), or just mail us at mail@osmbuildings.org. - -**Not sure which version to use?** - -## Classic 2.5D - -Source: https://github.com/kekscom/osmbuildings - -Best for: -- great device compatibility -- good performance on older hardware -- shadow simulation -- full integration with Leaflet or OpenLayers 2 - -## Modern 3D - -Best for: -- great performance on modern graphics hardware -- huge amounts of objects -- combining various data sources - -This version uses GLMap for any events and layers logic. - -## Get the files ## - -Checking in built versions causes a lot of trouble during development. So we decided to use the Github release system instead. - -Just pick the latest version from here: https://github.com/OSMBuildings/OSMBuildings/releases - -## Documentation - -All geo coordinates are in EPSG:4326. - -### Quick integration - -Link all required libraries in your HTML head section. Files are provided in folder `/dist`. - -~~~ html - - - - - - -
-~~~ - -In a script section initialize the map and add a map tile layer. - -~~~ javascript - var map = new GLMap('map', { - position: { latitude:52.52000, longitude:13.41000 }, - zoom: 16 - }); - - -// add OSM Buildings to the map and let it load data tiles. - - var osmb = new OSMBuildings({ - minZoom: 15, - maxZoom: 22 - }).addTo(map); - - osmb.addMapTiles( - 'https://{s}.tiles.mapbox.com/v3/osmbuildings.kbpalbpk/{z}/{x}/{y}.png', - { - attribution: '© Data OpenStreetMap · © Map MapBox' - } - ); - - osmb.addGeoJSONTiles('http://{s}.data.osmbuildings.org/0.2/anonymous/tile/{z}/{x}/{y}.json'); -~~~ - -### GLMap Options - -option | value | description ---- | --- | --- -position | object | geo position of map center -zoom | float | map zoom -rotation | float | map rotation -tilt | float | map tilt -bend | float | map bend -disabled | boolean | disables user input, default false -minZoom | float | minimum allowed zoom -maxZoom | float | maximum allowed zoom -bounds | object | n, e, s, w coordinates of bounds where the map can be moved within -attribution | string | attribution, optional -state | boolean | stores map position/rotation in url, default false - - -### GLMap methods - -method | parameters | description ---- | --- | --- -on | type, function | add an event listener, types are: change, resize, pointerdown, pointermove, pointerup -off | type, fn | remove event listener -setDisabled | boolean | disables any user input -isDisabled | | check wheether user input is disabled -getBounds | | returns coordinates of current map view, respects tilt and rotation but ignores perspective -setZoom | float | sets current zoom -getZoom | | gets current zoom -setPosition | object | sets current geo position of map center -getPosition | | gets current geo position of map center -setSize | object | {width,height} sets current map size in pixels -getSize | | gets current map size in pixels -setRotation | float | sets current rotation -getRotation | | gets current rotation -setTilt | float | sets current tilt -getTilt | | gets current tilt - - -### OSM Buildings options - -option | value | description ---- | --- | --- -baseURL | string | for locating assets, this is relative to calling page -minZoom | float | minimum allowed zoom -maxZoom | float | maximum allowed zoom -attribution | string | attribution, optional -showBackfaces | boolean | render front and backsides of polygons. false increases performance, true might be needed for bad geometries, default false -fogColor | string | color to be used for sky gradients and distance fog. -backgroundColor | string | overall background color -fastMode | boolean | enables faster rendering at cost of image quality, consider also removing any effects -effects | date | date for shadow calculation -project | latitude, longitude, elevation | transforms a geo coordinate + elevation to screen position -unproject | x, y | transforms a screen position into a geo coordinate with elevation 0 - - -### OSM Buildings methods - -method | parameters | description ---- | --- | --- -addTo | map | adds it as a layer to a GLMap instance -addOBJ | url, position, options | adds an OBJ file, specify a geo position and options {scale, rotation, elevation, id, color} -addGeoJSON | url, options | add a GeoJSON file or object and specify options {scale, rotation, elevation, id, color} -addGeoJSONTiles | url, options | add a GeoJSON tile set and specify options {bounds, scale, rotation, elevation, id, color} -addTileLayer | url, options | add a map tile set and specify options {bounds} -getTarget | x, y, function | get a building id at position. You need to provide a callback function do receive the data. -highlight | id, color | highlight a given building by id, this can only be one, set color = null in order to un-highlight -show | function, duration | shows buildings according to a selector function. That function receives parameters id, data of an item -hide | function, duration | hides buildings according to a selector function. That function receives parameters id, data of an item -screenshot | function | creates a screenshot from current view and returns it as data url. You need to provide a callback function do receive the data. -setDate | date | sets a date for shadow calculations - - -### OSM Buildings server - -There is also documentation of OSM Buildings Server side. See https://github.com/OSMBuildings/OSMBuildings/blob/master/docs/server.md - - -## Examples - -### Moving label - -This label moves virtually in space. - -~~~ html -
-~~~ - -~~~ javascript -var label = document.getElementById('label'); -map.on('change', function() { - var pos = osmb.project(52.52, 13.37, 50); - label.style.left = Math.round(pos.x) + 'px'; - label.style.top = Math.round(pos.y) + 'px'; -}); -~~~ - -### Highlight buildings - -~~~ javascript -map.on('pointermove', function(e) { - var id = osmb.getTarget(e.x, e.y, function(id) { - if (id) { - osmb.highlight(id, '#f08000'); - } else { - osmb.highlight(null); - } - }); -}); -~~~ - -### Map control buttons - -~~~ html -
- - -
- -
- - -
- -
- - -
- -
- - -
-~~~ - -~~~ javascript -var controlButtons = document.querySelectorAll('.control button'); - -for (var i = 0; i < controlButtons.length; i++) { - controlButtons[i].addEventListener('click', function(e) { - var button = this; - var parentClassList = button.parentNode.classList; - var direction = button.classList.contains('inc') ? 1 : -1; - var increment; - var property; - - if (parentClassList.contains('tilt')) { - property = 'Tilt'; - increment = direction*10; - } - if (parentClassList.contains('rotation')) { - property = 'Rotation'; - increment = direction*10; - } - if (parentClassList.contains('zoom')) { - property = 'Zoom'; - increment = direction*1; - } - if (parentClassList.contains('bend')) { - property = 'Bend'; - increment = direction*1; - } - if (property) { - map['set'+ property](map['get'+ property]()+increment); - } - }); -} -~~~ - -### Add GeoJSON - -~~~ javascript -var geojson = { - type: 'FeatureCollection', - features: [{ - type: 'Feature', - properties: { - color: '#ff0000', - roofColor: '#cc0000', - height: 50, - minHeight: 0 - }, - geometry: { - type: 'Polygon', - coordinates: [ - [ - [13.37000, 52.52000], - [13.37010, 52.52000], - [13.37010, 52.52010], - [13.37000, 52.52010], - [13.37000, 52.52000] - ] - ] - } - }] -}; -osmb.addGeoJSON(geojson); -~~~ - -### Position a map object - -~~~javascript -var obj = osmb.addGeoJSON(geojson); - -/* - * ## Key codes for object positioning ## - * Cursor keys: move - * +/- : scale - * w/s : elevate - * a/d : rotate - * - * Pressing Alt the same time accelerates - */ -document.addEventListener('keydown', function(e) { - var transInc = e.altKey ? 0.0002 : 0.00002; - var scaleInc = e.altKey ? 0.1 : 0.01; - var rotaInc = e.altKey ? 10 : 1; - var eleInc = e.altKey ? 10 : 1; - - switch (e.which) { - case 37: obj.position.longitude -= transInc; break; - case 39: obj.position.longitude += transInc; break; - case 38: obj.position.latitude += transInc; break; - case 40: obj.position.latitude -= transInc; break; - case 187: obj.scale += scaleInc; break; - case 189: obj.scale -= scaleInc; break; - case 65: obj.rotation += rotaInc; break; - case 68: obj.rotation -= rotaInc; break; - case 87: obj.elevation += eleInc; break; - case 83: obj.elevation -= eleInc; break; - default: return; - } - console.log(JSON.stringify({ - position:{ - latitude:parseFloat(obj.position.latitude.toFixed(5)), - longitude:parseFloat(obj.position.longitude.toFixed(5)) - }, - elevation:parseFloat(obj.elevation.toFixed(2)), - scale:parseFloat(obj.scale.toFixed(2)), - rotation:parseInt(obj.rotation, 10) - })); -}); -~~~ diff --git a/dist/Triangulate.debug.js b/dist/Triangulate.debug.js new file mode 100644 index 0000000..cba526b --- /dev/null +++ b/dist/Triangulate.debug.js @@ -0,0 +1,1225 @@ +(function(global) {(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.earcut = f()}})(function(){var define,module,exports;return (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 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; +}; + +},{}]},{},[1])(1) +}); +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJzcmMvZWFyY3V0LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0FDQUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0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Vhci5wcmV2LFxuICAgICAgICBiID0gZWFyLFxuICAgICAgICBjID0gZWFyLm5leHQ7XG5cbiAgICBpZiAoYXJlYShhLCBiLCBjKSA+PSAwKSByZXR1cm4gZmFsc2U7IC8vIHJlZmxleCwgY2FuJ3QgYmUgYW4gZWFyXG5cbiAgICAvLyBub3cgbWFrZSBzdXJlIHdlIGRvbid0IGhhdmUgb3RoZXIgcG9pbnRzIGluc2lkZSB0aGUgcG90ZW50aWFsIGVhclxuICAgIHZhciBwID0gZWFyLm5leHQubmV4dDtcblxuICAgIHdoaWxlIChwICE9PSBlYXIucHJldikge1xuICAgICAgICBpZiAocG9pbnRJblRyaWFuZ2xlKGEueCwgYS55LCBiLngsIGIueSwgYy54LCBjLnksIHAueCwgcC55KSAmJlxuICAgICAgICAgICAgYXJlYShwLnByZXYsIHAsIHAubmV4dCkgPj0gMCkgcmV0dXJuIGZhbHNlO1xuICAgICAgICBwID0gcC5uZXh0O1xuICAgIH1cblxuICAgIHJldHVybiB0cnVlO1xufVxuXG5mdW5jdGlvbiBpc0Vhckhhc2hlZChlYXIsIG1pblgsIG1pblksIHNpemUpIHtcbiAgICB2YXIgYSA9IGVhci5wcmV2LFxuICAgICAgICBiID0gZWFyLFxuICAgICAgICBjID0gZWFyLm5leHQ7XG5cbiAgICBpZiAoYXJlYShhLCBiLCBjKSA+PSAwKSByZXR1cm4gZmFsc2U7IC8vIHJlZmxleCwgY2FuJ3QgYmUgYW4gZWFyXG5cbiAgICAvLyB0cmlhbmdsZSBiYm94OyBtaW4gJiBtYXggYXJlIGNhbGN1bGF0ZWQgbGlrZSB0aGlzIGZvciBzcGVlZFxuICAgIHZhciBtaW5UWCA9IGEueCA8IGIueCA/IChhLnggPCBjLnggPyBhLnggOiBjLngpIDogKGIueCA8IGMueCA/IGIueCA6IGMueCksXG4gICAgICAgIG1pblRZID0gYS55IDwgYi55ID8gKGEueSA8IGMueSA/IGEueSA6IGMueSkgOiAoYi55IDwgYy55ID8gYi55IDogYy55KSxcbiAgICAgICAgbWF4VFggPSBhLnggPiBiLnggPyAoYS54ID4gYy54ID8gYS54IDogYy54KSA6IChiLnggPiBjLnggPyBiLnggOiBjLngpLFxuICAgICAgICBtYXhUWSA9IGEueSA+IGIueSA/IChhLnkgPiBjLnkgPyBhLnkgOiBjLnkpIDogKGIueSA+IGMueSA/IGIueSA6IGMueSk7XG5cbiAgICAvLyB6LW9yZGVyIHJhbmdlIGZvciB0aGUgY3VycmVudCB0cmlhbmdsZSBiYm94O1xuICAgIHZhciBtaW5aID0gek9yZGVyKG1pblRYLCBtaW5UWSwgbWluWCwgbWluWSwgc2l6ZSksXG4gICAgICAgIG1heFogPSB6T3JkZXIobWF4VFgsIG1heFRZLCBtaW5YLCBtaW5ZLCBzaXplKTtcblxuICAgIC8vIGZpcnN0IGxvb2sgZm9yIHBvaW50cyBpbnNpZGUgdGhlIHRyaWFuZ2xlIGluIGluY3JlYXNpbmcgei1vcmRlclxuICAgIHZhciBwID0gZWFyLm5leHRaO1xuXG4gICAgd2hpbGUgKHAgJiYgcC56IDw9IG1heFopIHtcbiAgICAgICAgaWYgKHAgIT09IGVhci5wcmV2ICYmIHAgIT09IGVhci5uZXh0ICYmXG4gICAgICAgICAgICBwb2ludEluVHJpYW5nbGUoYS54LCBhLnksIGIueCwgYi55LCBjLngsIGMueSwgcC54LCBwLnkpICYmXG4gICAgICAgICAgICBhcmVhKHAucHJldiwgcCwgcC5uZXh0KSA+PSAwKSByZXR1cm4gZmFsc2U7XG4gICAgICAgIHAgPSBwLm5leHRaO1xuICAgIH1cblxuICAgIC8vIHRoZW4gbG9vayBmb3IgcG9pbnRzIGluIGRlY3JlYXNpbmcgei1vcmRlclxuICAgIHAgPSBlYXIucHJldlo7XG5cbiAgICB3aGlsZSAocCAmJiBwLnogPj0gbWluWikge1xuICAgICAgICBpZiAocCAhPT0gZWFyLnByZXYgJiYgcCAhPT0gZWFyLm5leHQgJiZcbiAgICAgICAgICAgIHBvaW50SW5UcmlhbmdsZShh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24gaW50byB0d28gYW5kIHRyaWFuZ3VsYXRlIHRoZW0gaW5kZXBlbmRlbnRseVxuZnVuY3Rpb24gc3BsaXRFYXJjdXQoc3RhcnQsIHRyaWFuZ2xlcywgZGltLCBtaW5YLCBtaW5ZLCBzaXplKSB7XG4gICAgLy8gbG9vayBmb3IgYSB2YWxpZCBkaWFnb25hbCB0aGF0IGRpdmlkZXMgdGhlIHBvbHlnb24gaW50byB0d29cbiAgICB2YXIgYSA9IHN0YXJ0O1xuICAgIGRvIHtcbiAgICAgICAgdmFyIGIgPSBhLm5leHQubmV4dDtcbiAgICAgICAgd2hpbGUgKGIgIT09IGEucHJldikge1xuICAgICAgICAgICAgaWYgKGEuaSAhPT0gYi5pICYmIGlzVmFsaWREaWFnb25hbChhLCBiKSkge1xuICAgICAgICAgICAgICAgIC8vIHNwbGl0IHRoZSBwb2x5Z29uIGluIHR3byBieSB0aGUgZGlhZ29uYWxcbiAgICAgICAgICAgICAgICB2YXIgYyA9IHNwbGl0UG9seWdvbihhLCBiKTtcblxuICAgICAgICAgICAgICAgIC8vIGZpbHRlciBjb2xpbmVhciBwb2ludHMgYXJvdW5kIHRoZSBjdXRzXG4gICAgICAgICAgICAgICAgYSA9IGZpbHRlclBvaW50cyhhLCBhLm5leHQpO1xuICAgICAgICAgICAgICAgIGMgPSBmaWx0ZXJQb2ludHMoYywgYy5uZXh0KTtcblxuICAgICAgICAgICAgICAgIC8vIHJ1biBlYXJjdXQgb24gZWFjaCBoYWxmXG4gICAgICAgICAgICAgICAgZWFyY3V0TGlua2VkKGEsIHRyaWFuZ2xlcywgZGltLCBtaW5YLCBtaW5ZLCBzaXplKTtcbiAgICAgICAgICAgICAgICBlYXJjdXRMaW5rZWQoYywgdHJpYW5nbGVzLCBkaW0sIG1pblgsIG1pblksIHNpemUpO1xuICAgICAgICAgICAgICAgIHJldHVybjtcbiAgICAgICAgICAgIH1cbiAgICAgICAgICAgIGIgPSBiLm5leHQ7XG4gICAgICAgIH1cbiAgICAg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ICAgICAgICAgICB2YXIgeCA9IHAueCArIChoeSAtIHAueSkgKiAocC5uZXh0LnggLSBwLngpIC8gKHAubmV4dC55IC0gcC55KTtcbiAgICAgICAgICAgIGlmICh4IDw9IGh4ICYmIHggPiBxeCkge1xuICAgICAgICAgICAgICAgIHF4ID0geDtcbiAgICAgICAgICAgICAgICBpZiAoeCA9PT0gaHgpIHtcbiAgICAgICAgICAgICAgICAgICAgaWYgKGh5ID09PSBwLnkpIHJldHVybiBwO1xuICAgICAgICAgICAgICAgICAgICBpZiAoaHkgPT09IHAubmV4dC55KSByZXR1cm4gcC5uZXh0O1xuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICBtID0gcC54IDwgcC5uZXh0LnggPyBwIDogcC5uZXh0O1xuICAgICAgICAgICAgfVxuICAgICAgICB9XG4gICAgICAgIHAgPSBwLm5leHQ7XG4gICAgfSB3aGlsZSAocCAhPT0gb3V0ZXJOb2RlKTtcblxuICAgIGlmICghbSkgcmV0dXJuIG51bGw7XG5cbiAgICBpZiAoaHggPT09IHF4KSByZXR1cm4gbS5wcmV2OyAvLyBob2xlIHRvdWNoZXMgb3V0ZXIgc2VnbWVudDsgcGljayBsb3dlciBlbmRwb2ludFxuXG4gICAgLy8gbG9vayBmb3IgcG9pbnRzIGluc2lkZSB0aGUgdHJpYW5nbGUgb2YgaG9sZSBwb2ludCwgc2VnbWVudCBpbnRlcnNlY3Rpb24gYW5kIGVuZHBvaW50O1xuICAgIC8vIGlmIHRoZXJlIGFyZSBubyBwb2ludHMgZm91bmQsIHdlIGhhdmUgYSB2YWxpZCBjb25uZWN0aW9uO1xuICAgIC8vIG90aGVyd2lzZSBjaG9vc2UgdGhlIHBvaW50IG9mIHRoZSBtaW5pbXVtIGFuZ2xlIHdpdGggdGhlIHJheSBhcyBjb25uZWN0aW9uIHBvaW50XG5cbiAgICB2YXIgc3RvcCA9IG0sXG4gICAgICAgIG14ID0gbS54LFxuICAgICAgICBteSA9IG0ueSxcbiAgICAgICAgdGFuTWluID0gSW5maW5pdHksXG4gICAgICAgIHRhbjtcblxuICAgIHAgPSBtLm5leHQ7XG5cbiAgICB3aGlsZSAocCAhPT0gc3RvcCkge1xuICAgICAgICBpZiAoaHggPj0gcC54ICYmIHAueCA+PSBteCAmJlxuICAgICAgICAgICAgICAgIHBvaW50SW5UcmlhbmdsZShoeSA8IG15ID8gaHggOiBxeCwgaHksIG14LCBteSwgaHkgPCBteSA/IHF4IDogaHgsIGh5LCBwLngsIHAueSkpIHtcblxuICAgICAgICAgICAgdGFuID0gTWF0aC5hYnMoaHkgLSBwLnkpIC8gKGh4IC0gcC54KTsgLy8gdGFuZ2VudGlhbFxuXG4gICAgICAgICAgICBpZiAoKHRhbiA8IHRhbk1pbiB8fCAodGFuID09PSB0YW5NaW4gJiYgcC54ID4gbS54KSkgJiYgbG9jYWxseUluc2lkZShwLCBob2xlKSkge1xuICAgICAgICAgICAgICAgIG0gPSBwO1xuICAgICAgICAgICAgICAgIHRhbk1pbiA9IHRhbjtcbiAgICAgICAgICAgIH1cbiAgICAgICAgfVxuXG4gICAgICAgIHAgPSBwLm5leHQ7XG4gICAgfVxuXG4gICAgcmV0dXJuIG07XG59XG5cbi8vIGludGVybGluayBwb2x5Z29uIG5vZGVzIGluIHotb3JkZXJcbmZ1bmN0aW9uIGluZGV4Q3VydmUoc3RhcnQsIG1pblgsIG1pblksIHNpemUpIHtcbiAgICB2YXIgcCA9IHN0YXJ0O1xuICAgIGRvIHtcbiAgICAgICAgaWYgKHAueiA9PT0gbnVsbCkgcC56ID0gek9yZGVyKHAueCwgcC55LCBtaW5YLCBtaW5ZLCBzaXplKTtcbiAgICAgICAgcC5wcmV2WiA9IHAucHJldjtcbiAgICAgICAgcC5uZXh0WiA9IHAubmV4dDtcbiAgICAgICAgcCA9IHAubmV4dDtcbiAgICB9IHdoaWxlIChwICE9PSBzdGFydCk7XG5cbiAgICBwLnByZXZaLm5leHRaID0gbnVsbDtcbiAgICBwLnByZXZaID0gbnVsbDtcblxuICAgIHNvcnRMaW5rZWQocCk7XG59XG5cbi8vIFNpbW9uIFRhdGhhbSdzIGxpbmtlZCBsaXN0IG1lcmdlIHNvcnQgYWxnb3JpdGhtXG4vLyBodHRwOi8vd3d3LmNoaWFyay5ncmVlbmVuZC5vcmcudWsvfnNndGF0aGFtL2FsZ29yaXRobXMvbGlzdHNvcnQuaHRtbFxuZnVuY3Rpb24gc29ydExpbmtlZChsaXN0KSB7XG4gICAgdmFyIGksIHAsIHEsIGUsIHRhaWwsIG51bU1lcmdlcywgcFNpemUsIHFTaXplLFxuICAgICAgICBpblNpemUgPSAxO1xuXG4gICAgZG8ge1xuICAgICAgICBwID0gbGlzdDtcbiAgICAgICAgbGlzdCA9IG51bGw7XG4gICAgICAgIHRhaWwgPSBudWxsO1xuICAgICAgICBudW1NZXJnZXMgPSAwO1xuXG4gICAgICAgIHdoaWxlIChwKSB7XG4gICAgICAgICAgICBudW1NZXJnZXMrKztcbiAgICAgICAgICAgIHEgPSBwO1xuICAgICAgICAgICAgcFNpemUgPSAwO1xuICAgICAgICAgICAgZm9yIChpID0gMDsgaSA8IGluU2l6ZTsgaSsrKSB7XG4gICAgICAgICAgICAgICAgcFNpemUrKztcbiAgICAgICAgICAgICAgICBxID0gcS5uZXh0WjtcbiAgICAgICAgICAgICAgICBpZiAoIXEpIGJyZWFrO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICBxU2l6ZSA9IGluU2l6ZTtcblxuICAgICAgICAgICAgd2hpbGUgKHBTaXplID4gMCB8fCAocVNpemUgPiAwICYmIHEpKSB7XG5cbiAgICAgICAgICAgICAgICBpZiAocFNpemUgPT09IDApIHtcbiAgICAgICAgICAgICAgICAgICAgZSA9IHE7XG4gICAgICAgICAgICAgICAgICAgIHEgPSBxLm5leHRaO1xuICAgICAgICAgICAgICAgICAgICBxU2l6ZS0tO1xuICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAocVNpemUgPT09IDAgfHwgIXEpIHtcbiAgICAgICAgICAgICAgICAgICAgZSA9IHA7XG4gICAgICAgICAgICAgICAgICAgIHAgPSBwLm5leHRaO1xuICAgICAgICAgICAgICAgICAgICBwU2l6ZS0tO1xuICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAocC56IDw9IHEueikge1xuICAgICAgICAgICAgICAgICAgICBlID0gcDtcbiAgICAgICAgICAgICAgICAgICAgcCA9IHAubmV4dFo7XG4gICAgICAgICAgICAgICAgICAgIHBTaXplLS07XG4gICAgICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICAgICAgZSA9IHE7XG4gICAgICAgICAgICAgICAgICAgIHEgPSBxLm5leHRaO1xuICAgICAgICAgICAgICAgICAgICBxU2l6ZS0tO1xuICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgIGlmICh0YWlsKSB0YWlsLm5leHRaID0gZTtcbiAgICAgICAgICAgICAgICBlbHNlIGxpc3QgPSBlO1xuXG4gICAgICAgICAgICAgICAgZS5wcmV2WiA9IHRhaWw7XG4gICAgICAgICAgICAgICAgdGFpbCA9IGU7XG4gICAgICAgICAgICB9XG5cbiAgICAgICAgICAgIHAgPSBxO1xuICAgICAgICB9XG5cbiAgICAgICAgdGFpbC5uZXh0WiA9IG51bGw7XG4gICAgICAgIGluU2l6ZSAqPSAyO1xuXG4gICAgfSB3aGlsZSAobnVtTWVyZ2VzID4gMSk7XG5cbiAgICByZXR1cm4gbGlzdDtcbn1cblxuLy8gei1vcmRlciBvZiBhIHBvaW50IGdpdmVuIGNvb3JkcyBhbmQgc2l6ZSBvZiB0aGUgZGF0YSBib3VuZGluZyBib3hcbmZ1bmN0aW9uIHpPcmRlcih4LCB5LCBtaW5YLCBtaW5ZLCBzaXplKSB7XG4gICAgLy8gY29vcmRzIGFyZSB0cmFuc2Zvcm1lZCBpbnRvIG5vbi1uZWdhdGl2ZSAxNS1iaXQgaW50ZWdlciByYW5nZVxuICAgIHggPSAzMjc2NyAqICh4IC0gbWluWCkgLyBzaXplO1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gICBqID0gaTtcbiAgICB9XG4gICAgcmV0dXJuIHN1bTtcbn1cblxuLy8gdHVybiBhIHBvbHlnb24gaW4gYSBtdWx0aS1kaW1lbnNpb25hbCBhcnJheSBmb3JtIChlLmcuIGFzIGluIEdlb0pTT04pIGludG8gYSBmb3JtIEVhcmN1dCBhY2NlcHRzXG5lYXJjdXQuZmxhdHRlbiA9IGZ1bmN0aW9uIChkYXRhKSB7XG4gICAgdmFyIGRpbSA9IGRhdGFbMF1bMF0ubGVuZ3RoLFxuICAgICAgICByZXN1bHQgPSB7dmVydGljZXM6IFtdLCBob2xlczogW10sIGRpbWVuc2lvbnM6IGRpbX0sXG4gICAgICAgIGhvbGVJbmRleCA9IDA7XG5cbiAgICBmb3IgKHZhciBpID0gMDsgaSA8IGRhdGEubGVuZ3RoOyBpKyspIHtcbiAgICAgICAgZm9yICh2YXIgaiA9IDA7IGogPCBkYXRhW2ldLmxlbmd0aDsgaisrKSB7XG4gICAgICAgICAgICBmb3IgKHZhciBkID0gMDsgZCA8IGRpbTsgZCsrKSByZXN1bHQudmVydGljZXMucHVzaChkYXRhW2ldW2pdW2RdKTtcbiAgICAgICAgfVxuICAgICAgICBpZiAoaSA+IDApIHtcbiAgICAgICAgICAgIGhvbGVJbmRleCArPSBkYXRhW2kgLSAxXS5sZW5ndGg7XG4gICAgICAgICAgICByZXN1bHQuaG9sZXMucHVzaChob2xlSW5kZXgpO1xuICAgICAgICB9XG4gICAgfVxuICAgIHJldHVybiByZXN1bHQ7XG59O1xuIl19 + +var Color = (function(window) { + + +var w3cColors = { +aliceblue: '#f0f8ff', +antiquewhite: '#faebd7', +aqua: '#00ffff', +aquamarine: '#7fffd4', +azure: '#f0ffff', +beige: '#f5f5dc', +bisque: '#ffe4c4', +black: '#000000', +blanchedalmond: '#ffebcd', +blue: '#0000ff', +blueviolet: '#8a2be2', +brown: '#a52a2a', +burlywood: '#deb887', +cadetblue: '#5f9ea0', +chartreuse: '#7fff00', +chocolate: '#d2691e', +coral: '#ff7f50', +cornflowerblue: '#6495ed', +cornsilk: '#fff8dc', +crimson: '#dc143c', +cyan: '#00ffff', +darkblue: '#00008b', +darkcyan: '#008b8b', +darkgoldenrod: '#b8860b', +darkgray: '#a9a9a9', +darkgrey: '#a9a9a9', +darkgreen: '#006400', +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: '#ff00ff', +gainsboro: '#dcdcdc', +ghostwhite: '#f8f8ff', +gold: '#ffd700', +goldenrod: '#daa520', +gray: '#808080', +grey: '#808080', +green: '#008000', +greenyellow: '#adff2f', +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', +lightgrey: '#d3d3d3', +lightgreen: '#90ee90', +lightpink: '#ffb6c1', +lightsalmon: '#ffa07a', +lightseagreen: '#20b2aa', +lightskyblue: '#87cefa', +lightslategray: '#778899', +lightslategrey: '#778899', +lightsteelblue: '#b0c4de', +lightyellow: '#ffffe0', +lime: '#00ff00', +limegreen: '#32cd32', +linen: '#faf0e6', +magenta: '#ff00ff', +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: '#ff0000', +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: '#ffffff', +whitesmoke: '#f5f5f5', +yellow: '#ffff00', +yellowgreen: '#9acd32' +}; + +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; +} + +function clamp(v, max) { + return Math.min(max, Math.max(0, v || 0)); +} + +/** + * @param str, object can be in any of these: 'red', '#0099ff', 'rgb(64, 128, 255)', 'rgba(64, 128, 255, 0.5)', { r:0.2, g:0.3, b:0.9, a:1 } + */ +var Color = function(str) { + str = str || ''; + + if (typeof str === 'object') { + var rgba = str; + this.R = clamp(rgba.r, max); + this.G = clamp(rgba.g, max); + this.B = clamp(rgba.b, max); + this.A = (rgba.a !== undefined ? clamp(rgba.a, 1) : 1); + this.isValid = true; + } else if (typeof str === 'string') { + str = str.toLowerCase(); + str = w3cColors[str] || str; + var m; + if ((m = str.match(/^#?(\w{2})(\w{2})(\w{2})$/))) { + this.R = parseInt(m[1], 16) / 255; + this.G = parseInt(m[2], 16) / 255; + this.B = parseInt(m[3], 16) / 255; + this.A = 1; + this.isValid = true; + } else if ((m = str.match(/rgba?\((\d+)\D+(\d+)\D+(\d+)(\D+([\d.]+))?\)/))) { + this.R = parseInt(m[1], 10) / 255; + this.G = parseInt(m[2], 10) / 255; + this.B = parseInt(m[3], 10) / 255; + this.A = m[4] ? parseFloat(m[5]) : 1; + this.isValid = true; + } + } +}; + +Color.prototype = { + + toHSL: function() { + var + max = Math.max(this.R, this.G, this.B), + min = Math.min(this.R, this.G, this.B), + h, s, l = (max+min) / 2, + d = max-min; + + if (!d) { + h = s = 0; // achromatic + } else { + s = l > 0.5 ? d / (2-max-min) : d / (max+min); + switch (max) { + case this.R: h = (this.G-this.B) / d + (this.G < this.B ? 6 : 0); break; + case this.G: h = (this.B-this.R) / d + 2; break; + case this.B: h = (this.R-this.G) / d + 4; break; + } + h *= 60; + } + + return { h:h, s:s, l:l }; + }, + + fromHSL: function(hsl) { + // h = clamp(hsl.h, 360), + // s = clamp(hsl.s, 1), + // l = clamp(hsl.l, 1), + + // achromatic + if (hsl.s === 0) { + this.R = hsl.l; + this.G = hsl.l; + this.B = hsl.l; + } else { + var + q = hsl.l < 0.5 ? hsl.l * (1+hsl.s) : hsl.l + hsl.s - hsl.l*hsl.s, + p = 2 * hsl.l-q; + hsl.h /= 360; + this.R = hue2rgb(p, q, hsl.h + 1/3); + this.G = hue2rgb(p, q, hsl.h); + this.B = hue2rgb(p, q, hsl.h - 1/3); + } + + return this; + }, + + toString: function() { + if (this.A === 1) { + return '#' + ((1 <<24) + (Math.round(this.R*255) <<16) + (Math.round(this.G*255) <<8) + Math.round(this.B*255)).toString(16).slice(1, 7); + } + return 'rgba(' + [Math.round(this.R*255), Math.round(this.G*255), Math.round(this.B*255), this.A.toFixed(2)].join(',') + ')'; + }, + + toArray: function() { + return [this.R, this.G, this.B]; + }, + + hue: function(h) { + var hsl = this.toHSL(); + hsl.h *= h; + this.fromHSL(hsl); + return this; + }, + + saturation: function(s) { + debugger + var hsl = this.toHSL(); + hsl.s *= s; + this.fromHSL(hsl); + return this; + }, + + lightness: function(l) { + var hsl = this.toHSL(); + hsl.l *= l; + this.fromHSL(hsl); + return this; + }, + + alpha: function(a) { + this.A *= a; + return this; + } +}; +return Color; }(this)); + +var Triangulate = { + + NUM_Y_SEGMENTS: 24, + NUM_X_SEGMENTS: 32, + + //function isVertical(a, b, c) { + // return Math.abs(normal(a, b, c)[2]) < 1/5000; + //} + + quad: function(data, a, b, c, d, color) { + this.triangle(data, a, b, c, color); + this.triangle(data, c, d, a, color); + }, + + triangle: function(data, a, b, c, color) { + var n = normal(a, b, c); + [].push.apply(data.vertices, [].concat(a, c, b)); + [].push.apply(data.normals, [].concat(n, n, n)); + [].push.apply(data.colors, [].concat(color, color, color)); + data.texCoords.push(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); + }, + + circle: function(data, center, radius, Z, color) { + Z = Z || 0; + var u, v; + for (var i = 0; i < this.NUM_X_SEGMENTS; i++) { + u = i/this.NUM_X_SEGMENTS; + v = (i+1)/this.NUM_X_SEGMENTS; + this.triangle( + data, + [ center[0] + radius * Math.sin(u*Math.PI*2), center[1] + radius * Math.cos(u*Math.PI*2), Z ], + [ center[0], center[1], Z ], + [ center[0] + radius * Math.sin(v*Math.PI*2), center[1] + radius * Math.cos(v*Math.PI*2), Z ], + color + ); + } + }, + + polygon: function(data, rings, Z, color) { + Z = Z || 0; + // flatten data + var + inVertices = [], inHoleIndex = [], + index = 0, + i, il; + for (i = 0, il = rings.length; i < il; i++) { + for (var j = 0; j < rings[i].length; j++) { + inVertices.push(rings[i][j][0], rings[i][j][1]); + } + if (i) { + index += rings[i - 1].length; + inHoleIndex.push(index); + } + } + + var vertices = earcut(inVertices, inHoleIndex, 2); + + for (i = 0, il = vertices.length-2; i < il; i+=3) { + this.triangle( + data, + [ inVertices[ vertices[i ]*2 ], inVertices[ vertices[i ]*2+1 ], Z ], + [ inVertices[ vertices[i+1]*2 ], inVertices[ vertices[i+1]*2+1 ], Z ], + [ inVertices[ vertices[i+2]*2 ], inVertices[ vertices[i+2]*2+1 ], Z ], + color + ); + } + }, + + //polygon3d: function(data, rings, color) { + // var ring = rings[0]; + // var ringLength = ring.length; + // var vertices, t, tl; + // +//// { r:255, g:0, b:0 } +// + // if (ringLength <= 4) { // 3: a triangle + // this.triangle( + // data, + // ring[0], + // ring[2], + // ring[1], color + // ); + // + // if (ringLength === 4) { // 4: a quad (2 triangles) + // this.triangle( + // data, + // ring[0], + // ring[3], + // ring[2], color + // ); + // } +// return; + // } + // + // if (isVertical(ring[0], ring[1], ring[2])) { + // for (var i = 0, il = rings[0].length; i < il; i++) { + // rings[0][i] = [ + // rings[0][i][2], + // rings[0][i][1], + // rings[0][i][0] + // ]; + // } + // + // vertices = earcut(rings); + // for (t = 0, tl = vertices.length-2; t < tl; t+=3) { + // this.triangle( + // data, + // [ vertices[t ][2], vertices[t ][1], vertices[t ][0] ], + // [ vertices[t+1][2], vertices[t+1][1], vertices[t+1][0] ], + // [ vertices[t+2][2], vertices[t+2][1], vertices[t+2][0] ], color + // ); + // } +// return; + // } + // + // vertices = earcut(rings); + // for (t = 0, tl = vertices.length-2; t < tl; t+=3) { + // this.triangle( + // data, + // [ vertices[t ][0], vertices[t ][1], vertices[t ][2] ], + // [ vertices[t+1][0], vertices[t+1][1], vertices[t+1][2] ], + // [ vertices[t+2][0], vertices[t+2][1], vertices[t+2][2] ], color + // ); + // } + //}, + + cube: function(data, sizeX, sizeY, sizeZ, X, Y, Z, color) { + X = X || 0; + Y = Y || 0; + Z = Z || 0; + + var a = [X, Y, Z]; + var b = [X+sizeX, Y, Z]; + var c = [X+sizeX, Y+sizeY, Z]; + var d = [X, Y+sizeY, Z]; + + var A = [X, Y, Z+sizeZ]; + var B = [X+sizeX, Y, Z+sizeZ]; + var C = [X+sizeX, Y+sizeY, Z+sizeZ]; + var D = [X, Y+sizeY, Z+sizeZ]; + + this.quad(data, b, a, d, c, color); + this.quad(data, A, B, C, D, color); + this.quad(data, a, b, B, A, color); + this.quad(data, b, c, C, B, color); + this.quad(data, c, d, D, C, color); + this.quad(data, d, a, A, D, color); + }, + + cylinder: function(data, center, radius1, radius2, height, Z, color) { + Z = Z || 0; + var + currAngle, nextAngle, + currSin, currCos, + nextSin, nextCos, + num = this.NUM_X_SEGMENTS, + doublePI = Math.PI*2; + + for (var i = 0; i < num; i++) { + currAngle = ( i /num) * doublePI; + nextAngle = ((i+1)/num) * doublePI; + + currSin = Math.sin(currAngle); + currCos = Math.cos(currAngle); + + nextSin = Math.sin(nextAngle); + nextCos = Math.cos(nextAngle); + + this.triangle( + data, + [ center[0] + radius1*currSin, center[1] + radius1*currCos, Z ], + [ center[0] + radius2*nextSin, center[1] + radius2*nextCos, Z+height ], + [ center[0] + radius1*nextSin, center[1] + radius1*nextCos, Z ], + color + ); + + if (radius2 !== 0) { + this.triangle( + data, + [ center[0] + radius2*currSin, center[1] + radius2*currCos, Z+height ], + [ center[0] + radius2*nextSin, center[1] + radius2*nextCos, Z+height ], + [ center[0] + radius1*currSin, center[1] + radius1*currCos, Z ], + color + ); + } + } + }, + + dome: function(data, center, radius, height, Z, color) { + Z = Z || 0; + var + currAngle, nextAngle, + currSin, currCos, + nextSin, nextCos, + currRadius, nextRadius, + nextHeight, nextZ, + num = this.NUM_Y_SEGMENTS/2, + halfPI = Math.PI/2; + + for (var i = 0; i < num; i++) { + currAngle = ( i /num) * halfPI - halfPI; + nextAngle = ((i+1)/num) * halfPI - halfPI; + + currSin = Math.sin(currAngle); + currCos = Math.cos(currAngle); + + nextSin = Math.sin(nextAngle); + nextCos = Math.cos(nextAngle); + + currRadius = currCos*radius; + nextRadius = nextCos*radius; + + nextHeight = (nextSin-currSin)*height; + nextZ = Z - nextSin*height; + + this.cylinder(data, center, nextRadius, currRadius, nextHeight, nextZ, color); + } + }, + + // TODO + sphere: function(data, center, radius, height, Z, color) { + Z = Z || 0; + return this.cylinder(data, center, radius, radius, height, Z, color); + }, + + pyramid: function(data, polygon, center, height, Z, color) { + Z = Z || 0; + polygon = polygon[0]; + for (var i = 0, il = polygon.length-1; i < il; i++) { + this.triangle( + data, + [ polygon[i ][0], polygon[i ][1], Z ], + [ polygon[i+1][0], polygon[i+1][1], Z ], + [ center[0], center[1], Z+height ], + color + ); + } + }, + + extrusion: function(data, polygon, height, Z, color, tx) { + Z = Z || 0; + var + ring, last, a, b, + L, + v0, v1, v2, v3, n, + tx1, tx2, + ty1 = tx[2]*height, ty2 = tx[3]*height; + + for (var i = 0, il = polygon.length; i < il; i++) { + ring = polygon[i]; + last = ring.length-1; + + if (ring[0][0] !== ring[last][0] || ring[0][1] !== ring[last][1]) { + ring.push(ring[0]); + last++; + } + + for (var r = 0; r < last; r++) { + a = ring[r]; + b = ring[r+1]; + L = len2(sub2(a, b)); + + tx1 = (tx[0]*L) <<0; + tx2 = (tx[1]*L) <<0; + + v0 = [ a[0], a[1], Z]; + v1 = [ b[0], b[1], Z]; + v2 = [ b[0], b[1], Z+height]; + v3 = [ a[0], a[1], Z+height]; + + n = normal(v0, v1, v2); + [].push.apply(data.vertices, [].concat(v0, v2, v1, v0, v3, v2)); + [].push.apply(data.normals, [].concat(n, n, n, n, n, n)); + [].push.apply(data.colors, [].concat(color, color, color, color, color, color)); + + data.texCoords.push( + tx1, ty2, + tx2, ty1, + tx2, ty2, + tx1, ty2, + tx1, ty1, + tx2, ty1 + ); + } + } + } +}; +}(this)); \ No newline at end of file diff --git a/dist/Triangulate.js b/dist/Triangulate.js new file mode 100644 index 0000000..6a03998 --- /dev/null +++ b/dist/Triangulate.js @@ -0,0 +1 @@ +!function(a){!function(b){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=b();else if("function"==typeof define&&define.amd)define([],b);else{var c;c="undefined"!=typeof window?window:"undefined"!=typeof a?a:"undefined"!=typeof self?self:this,c.earcut=b()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g80*c){j=m=a[0],k=n=a[1];for(var r=c;f>r;r+=c)o=a[r],p=a[r+1],j>o&&(j=o),k>p&&(k=p),o>m&&(m=o),p>n&&(n=p);q=Math.max(m-j,n-k)}return g(h,i,c,j,k,q),i}function e(a,b,c,d,e){var f,g;if(e===F(a,b,c,d)>0)for(f=b;c>f;f+=d)g=C(f,a[f],a[f+1],g);else for(f=c-d;f>=b;f-=d)g=C(f,a[f],a[f+1],g);return g&&w(g,g.next)&&(D(g),g=g.next),g}function f(a,b){if(!a)return a;b||(b=a);var c,d=a;do if(c=!1,d.steiner||!w(d,d.next)&&0!==v(d.prev,d,d.next))d=d.next;else{if(D(d),d=b=d.prev,d===d.next)return null;c=!0}while(c||d!==b);return b}function g(a,b,c,d,e,l,m){if(a){!m&&l&&p(a,d,e,l);for(var n,o,q=a;a.prev!==a.next;)if(n=a.prev,o=a.next,l?i(a,d,e,l):h(a))b.push(n.i/c),b.push(a.i/c),b.push(o.i/c),D(a),a=o.next,q=o.next;else if(a=o,a===q){m?1===m?(a=j(a,b,c),g(a,b,c,d,e,l,2)):2===m&&k(a,b,c,d,e,l):g(f(a),b,c,d,e,l,1);break}}}function h(a){var b=a.prev,c=a,d=a.next;if(v(b,c,d)>=0)return!1;for(var e=a.next.next;e!==a.prev;){if(t(b.x,b.y,c.x,c.y,d.x,d.y,e.x,e.y)&&v(e.prev,e,e.next)>=0)return!1;e=e.next}return!0}function i(a,b,c,d){var e=a.prev,f=a,g=a.next;if(v(e,f,g)>=0)return!1;for(var h=e.xf.x?e.x>g.x?e.x:g.x:f.x>g.x?f.x:g.x,k=e.y>f.y?e.y>g.y?e.y:g.y:f.y>g.y?f.y:g.y,l=r(h,i,b,c,d),m=r(j,k,b,c,d),n=a.nextZ;n&&n.z<=m;){if(n!==a.prev&&n!==a.next&&t(e.x,e.y,f.x,f.y,g.x,g.y,n.x,n.y)&&v(n.prev,n,n.next)>=0)return!1;n=n.nextZ}for(n=a.prevZ;n&&n.z>=l;){if(n!==a.prev&&n!==a.next&&t(e.x,e.y,f.x,f.y,g.x,g.y,n.x,n.y)&&v(n.prev,n,n.next)>=0)return!1;n=n.prevZ}return!0}function j(a,b,c){var d=a;do{var e=d.prev,f=d.next.next;!w(e,f)&&x(e,d,d.next,f)&&z(e,f)&&z(f,e)&&(b.push(e.i/c),b.push(d.i/c),b.push(f.i/c),D(d),D(d.next),d=a=f),d=d.next}while(d!==a);return d}function k(a,b,c,d,e,h){var i=a;do{for(var j=i.next.next;j!==i.prev;){if(i.i!==j.i&&u(i,j)){var k=B(i,j);return i=f(i,i.next),k=f(k,k.next),g(i,b,c,d,e,h),void g(k,b,c,d,e,h)}j=j.next}i=i.next}while(i!==a)}function l(a,b,c,d){var g,h,i,j,k,l=[];for(g=0,h=b.length;h>g;g++)i=b[g]*d,j=h-1>g?b[g+1]*d:a.length,k=e(a,i,j,d,!1),k===k.next&&(k.steiner=!0),l.push(s(k));for(l.sort(m),g=0;g=d.next.y){var h=d.x+(f-d.y)*(d.next.x-d.x)/(d.next.y-d.y);if(e>=h&&h>g){if(g=h,h===e){if(f===d.y)return d;if(f===d.next.y)return d.next}c=d.x=d.x&&d.x>=k&&t(l>f?e:g,f,k,l,l>f?g:e,f,d.x,d.y)&&(i=Math.abs(f-d.y)/(e-d.x),(m>i||i===m&&d.x>c.x)&&z(d,a)&&(c=d,m=i)),d=d.next;return c}function p(a,b,c,d){var e=a;do null===e.z&&(e.z=r(e.x,e.y,b,c,d)),e.prevZ=e.prev,e.nextZ=e.next,e=e.next;while(e!==a);e.prevZ.nextZ=null,e.prevZ=null,q(e)}function q(a){var b,c,d,e,f,g,h,i,j=1;do{for(c=a,a=null,f=null,g=0;c;){for(g++,d=c,h=0,b=0;j>b&&(h++,d=d.nextZ,d);b++);for(i=j;h>0||i>0&&d;)0===h?(e=d,d=d.nextZ,i--):0!==i&&d?c.z<=d.z?(e=c,c=c.nextZ,h--):(e=d,d=d.nextZ,i--):(e=c,c=c.nextZ,h--),f?f.nextZ=e:a=e,e.prevZ=f,f=e;c=d}f.nextZ=null,j*=2}while(g>1);return a}function r(a,b,c,d,e){return a=32767*(a-c)/e,b=32767*(b-d)/e,a=16711935&(a|a<<8),a=252645135&(a|a<<4),a=858993459&(a|a<<2),a=1431655765&(a|a<<1),b=16711935&(b|b<<8),b=252645135&(b|b<<4),b=858993459&(b|b<<2),b=1431655765&(b|b<<1),a|b<<1}function s(a){var b=a,c=a;do b.x=0&&(a-g)*(d-h)-(c-g)*(b-h)>=0&&(c-g)*(f-h)-(e-g)*(d-h)>=0}function u(a,b){return a.next.i!==b.i&&a.prev.i!==b.i&&!y(a,b)&&z(a,b)&&z(b,a)&&A(a,b)}function v(a,b,c){return(b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y)}function w(a,b){return a.x===b.x&&a.y===b.y}function x(a,b,c,d){return w(a,b)&&w(c,d)||w(a,d)&&w(c,b)?!0:v(a,b,c)>0!=v(a,b,d)>0&&v(c,d,a)>0!=v(c,d,b)>0}function y(a,b){var c=a;do{if(c.i!==a.i&&c.next.i!==a.i&&c.i!==b.i&&c.next.i!==b.i&&x(c,c.next,a,b))return!0;c=c.next}while(c!==a);return!1}function z(a,b){return v(a.prev,a,a.next)<0?v(a,b,a.next)>=0&&v(a,a.prev,b)>=0:v(a,b,a.prev)<0||v(a,a.next,b)<0}function A(a,b){var c=a,d=!1,e=(a.x+b.x)/2,f=(a.y+b.y)/2;do c.y>f!=c.next.y>f&&e<(c.next.x-c.x)*(f-c.y)/(c.next.y-c.y)+c.x&&(d=!d),c=c.next;while(c!==a);return d}function B(a,b){var c=new E(a.i,a.x,a.y),d=new E(b.i,b.x,b.y),e=a.next,f=b.prev;return a.next=b,b.prev=a,c.next=e,e.prev=c,d.next=c,c.prev=d,f.next=d,d.prev=f,d}function C(a,b,c,d){var e=new E(a,b,c);return d?(e.next=d.next,e.prev=d,d.next.prev=e,d.next=e):(e.prev=e,e.next=e),e}function D(a){a.next.prev=a.prev,a.prev.next=a.next,a.prevZ&&(a.prevZ.nextZ=a.nextZ),a.nextZ&&(a.nextZ.prevZ=a.prevZ)}function E(a,b,c){this.i=a,this.x=b,this.y=c,this.prev=null,this.next=null,this.z=null,this.prevZ=null,this.nextZ=null,this.steiner=!1}function F(a,b,c,d){for(var e=0,f=b,g=c-d;c>f;f+=d)e+=(a[g]-a[f])*(a[f+1]+a[g+1]),g=f;return e}b.exports=d,d.deviation=function(a,b,c,d){var e=b&&b.length,f=e?b[0]*c:a.length,g=Math.abs(F(a,0,f,c));if(e)for(var h=0,i=b.length;i>h;h++){var j=b[h]*c,k=i-1>h?b[h+1]*c:a.length;g-=Math.abs(F(a,j,k,c))}var l=0;for(h=0;hg;g++)c.vertices.push(a[e][f][g]);e>0&&(d+=a[e-1].length,c.holes.push(d))}return c}},{}]},{},[1])(1)});(function(a){function b(a,b,c){return 0>c&&(c+=1),c>1&&(c-=1),1/6>c?a+6*(b-a)*c:.5>c?b:2/3>c?a+(b-a)*(2/3-c)*6:a}function c(a,b){return Math.min(b,Math.max(0,a||0))}var d={aliceblue:"#f0f8ff",antiquewhite:"#faebd7",aqua:"#00ffff",aquamarine:"#7fffd4",azure:"#f0ffff",beige:"#f5f5dc",bisque:"#ffe4c4",black:"#000000",blanchedalmond:"#ffebcd",blue:"#0000ff",blueviolet:"#8a2be2",brown:"#a52a2a",burlywood:"#deb887",cadetblue:"#5f9ea0",chartreuse:"#7fff00",chocolate:"#d2691e",coral:"#ff7f50",cornflowerblue:"#6495ed",cornsilk:"#fff8dc",crimson:"#dc143c",cyan:"#00ffff",darkblue:"#00008b",darkcyan:"#008b8b",darkgoldenrod:"#b8860b",darkgray:"#a9a9a9",darkgrey:"#a9a9a9",darkgreen:"#006400",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:"#ff00ff",gainsboro:"#dcdcdc",ghostwhite:"#f8f8ff",gold:"#ffd700",goldenrod:"#daa520",gray:"#808080",grey:"#808080",green:"#008000",greenyellow:"#adff2f",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",lightgrey:"#d3d3d3",lightgreen:"#90ee90",lightpink:"#ffb6c1",lightsalmon:"#ffa07a",lightseagreen:"#20b2aa",lightskyblue:"#87cefa",lightslategray:"#778899",lightslategrey:"#778899",lightsteelblue:"#b0c4de",lightyellow:"#ffffe0",lime:"#00ff00",limegreen:"#32cd32",linen:"#faf0e6",magenta:"#ff00ff",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:"#ff0000",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:"#ffffff",whitesmoke:"#f5f5f5",yellow:"#ffff00",yellowgreen:"#9acd32"},e=function(a){if(a=a||"","object"==typeof a){var b=a;this.R=c(b.r,max),this.G=c(b.g,max),this.B=c(b.b,max),this.A=void 0!==b.a?c(b.a,1):1,this.isValid=!0}else if("string"==typeof a){a=a.toLowerCase(),a=d[a]||a;var e;(e=a.match(/^#?(\w{2})(\w{2})(\w{2})$/))?(this.R=parseInt(e[1],16)/255,this.G=parseInt(e[2],16)/255,this.B=parseInt(e[3],16)/255,this.A=1,this.isValid=!0):(e=a.match(/rgba?\((\d+)\D+(\d+)\D+(\d+)(\D+([\d.]+))?\)/))&&(this.R=parseInt(e[1],10)/255,this.G=parseInt(e[2],10)/255,this.B=parseInt(e[3],10)/255,this.A=e[4]?parseFloat(e[5]):1,this.isValid=!0)}};return e.prototype={toHSL:function(){var a,b,c=Math.max(this.R,this.G,this.B),d=Math.min(this.R,this.G,this.B),e=(c+d)/2,f=c-d;if(f){switch(b=e>.5?f/(2-c-d):f/(c+d),c){case this.R:a=(this.G-this.B)/f+(this.G