d3-geo-voronoi
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -1,2 +0,2 @@ | ||
// https://github.com/Fil/d3-geo-voronoi Version 1.1.1. Copyright 2018 Philippe Rivière. | ||
// https://github.com/Fil/d3-geo-voronoi Version 1.1.2. Copyright 2018 Philippe Rivière. | ||
(function (global, factory) { | ||
@@ -128,2 +128,9 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-delaunay'), require('d3-geo'), require('d3-array')) : | ||
function geo_find(neighbors, points) { | ||
function distance2(a,b) { | ||
let x = a[0] - b[0], | ||
y = a[1] - b[1], | ||
z = a[2] - b[2]; | ||
return x * x + y * y + z * z; | ||
} | ||
return function find(x, y, next) { | ||
@@ -134,8 +141,9 @@ if (next === undefined) next = 0; | ||
found = next; | ||
const xyz = cartesian([x, y]); | ||
do { | ||
cell = next; | ||
next = null; | ||
dist = d3Geo.geoDistance([x, y], points[cell]); | ||
dist = distance2(xyz, cartesian(points[cell])); | ||
neighbors[cell].forEach(i => { | ||
let ndist = d3Geo.geoDistance([x, y], points[i]); | ||
let ndist = distance2(xyz, cartesian(points[i])); | ||
if (ndist < dist) { | ||
@@ -142,0 +150,0 @@ dist = ndist; |
@@ -1,1 +0,1 @@ | ||
(function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-delaunay"),require("d3-geo"),require("d3-array")):"function"==typeof define&&define.amd?define(["exports","d3-delaunay","d3-geo","d3-array"],t):t(e.d3=e.d3||{},e.d3,e.d3,e.d3)})(this,function(e,t,n,o){"use strict";var r=Math.PI,a=r/2,u=180/r,i=r/180,l=Math.atan2,s=Math.cos,c=Math.max,f=Math.min,p=Math.sin,d=Math.sign||function(e){return e>0?1:e<0?-1:0},h=Math.sqrt;function y(e,t){return e[0]*t[0]+e[1]*t[1]+e[2]*t[2]}function g(e,t){return[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]]}function m(e,t){return[e[0]+t[0],e[1]+t[1],e[2]+t[2]]}function v(e){var t=h(e[0]*e[0]+e[1]*e[1]+e[2]*e[2]);return[e[0]/t,e[1]/t,e[2]/t]}function _(e){return[l(e[1],e[0])*u,(t=c(-1,f(1,e[2])),(t>1?a:t<-1?-a:Math.asin(t))*u)];var t}function b(e){var t=e[0]*i,n=e[1]*i,o=s(n);return[o*s(t),o*p(t),p(n)]}function j(e){return y((e=e.map(e=>b(e)))[0],g(e[2],e[1]))}function M(e){const a=function(e){if(e.length<2)return{};const o=n.geoRotation(e[0]),a=n.geoStereographic().translate([0,0]).scale(1).rotate(o.invert([180,0])),u=[0];let i=1;for(let t=1,n=(e=e.map(a)).length;t<n;t++){let n=e[t][0]*e[t][0]+e[t][1]*e[t][1];isNaN(n)&&u.push(t),n>i&&(i=n)}const l=1e6*h(i);u.forEach((t,n)=>e[n]=[l/2,0]);for(let t=0;t<4;t++)e.push([l*s(t/2*r),l*p(t/2*r)]);const c=t.Delaunay.from(e);return c.projection=a,c}(e),u=function(e,t){if(!e.triangles)return[];const n=e.triangles.slice().map(e=>e>=t?0:e),o=[];for(let e=0,t=n.length/3;e<t;e++){const t=n[3*e],r=n[3*e+1],a=n[3*e+2];t!==r&&r!==a&&a!==t&&o.push([t,a,r])}return o}(a,e.length),i=function(e,t){const n={};return 2===t.length?[[0,1]]:(e.forEach(e=>{if(!(j(e.map(e=>t[e]))<0))for(let t,r=0;r<3;r++)t=(r+1)%3,n[o.extent([e[r],e[t]]).join("-")]=!0}),Object.keys(n).map(e=>e.split("-").map(Number)))}(u,e),l=function(e,t){const n=[];return e.forEach((e,t)=>{for(let t=0;t<3;t++){const o=e[t],r=e[(t+1)%3];n[o]=n[o]||[],n[o].push(r)}}),0===e.length&&(2===t?(n[0]=[1],n[1]=[0]):1===t&&(n[0]=[])),n}(u,e.length),c=function(e,t){return function(o,r,a){void 0===a&&(a=0);let u,i,l=a;do{u=a,a=null,i=n.geoDistance([o,r],t[u]),e[u].forEach(e=>{let u=n.geoDistance([o,r],t[e]);if(u<i)return i=u,a=e,void(l=e)})}while(null!==a);return l}}(l,e),f=function(e,t){return e.map(e=>{const n=e.map(e=>t[e]).map(b);return _(v(m(m(g(n[1],n[0]),g(n[2],n[1])),g(n[0],n[2]))))})}(u,e),{polygons:d,centers:y}=function(e,t,n){const o=[],r=e.slice();if(0===t.length){if(n.length<2)return{polygons:o,centers:r};if(2===n.length){const e=b(n[0]),t=b(n[1]),u=v(m(e,t)),i=v(g(e,t)),l=g(u,i),s=[u,g(u,l),g(g(u,l),l),g(g(g(u,l),l),l)].map(_).map(a);return o.push(s),o.push(s.slice().reverse()),{polygons:o,centers:r}}}function a(e){let n=-1;return r.slice(t.length,1/0).forEach((o,r)=>{o[0]===e[0]&&o[1]===e[1]&&(n=r+t.length)}),n<0&&(n=r.length,r.push(e)),n}return t.forEach((e,t)=>{for(let n=0;n<3;n++){const r=e[n],a=e[(n+1)%3],u=e[(n+2)%3];o[r]=o[r]||[],o[r].push([a,u,t,[r,a,u]])}}),{polygons:o.map(e=>{const t=[e[0][2]];let o=e[0][1];for(let n=1;n<e.length;n++)for(let n=0;n<e.length;n++)if(e[n][0]==o){o=e[n][1],t.push(e[n][2]);break}if(t.length>2)return t;if(2==t.length){const o=x(n[e[0][3][0]],n[e[0][3][1]],r[t[0]]),u=x(n[e[0][3][2]],n[e[0][3][0]],r[t[0]]),i=a(o),l=a(u);return[t[0],l,t[1],i]}console&&console.warn({here:"unreachable",poly:e})}),centers:r}}(f,u,e),M=function(e){const t=[];return e.forEach(e=>{if(!e)return;let n=e[e.length-1];for(let o of e)o>n&&t.push([n,o]),n=o}),t}(d),E=function(e,t){const n={},o=[];e.map(e=>{if(!(j(e.map(e=>t[e>t.length?0:e]))<0))for(let t=0;t<3;t++){let o=[e[t],e[(t+1)%3]],r=`${o[1]}-${o[0]}`;n[r]?delete n[r]:n[o.join("-")]=!0}});const r={};let a;if(Object.keys(n).forEach(e=>{e=e.split("-").map(Number),r[e[0]]=e[1],a=e[0]}),void 0===a)return o;let u=a;do{o.push(u),u=r[u]}while(u!==a);return o}(u,e),q=function(e,t){return function(n){const r={},a={};return e.forEach((e,t)=>{const o=e.join("-");r[o]=n[t],a[o]=!0}),t.forEach(e=>{let t=0,n=-1;for(var u=0;u<3;u++){let a=o.extent([e[u],e[(u+1)%3]]).join("-");r[a]>t&&(t=r[a],n=a)}a[n]=!1}),e.map(e=>a[e.join("-")])}}(i,u);return{delaunay:a,edges:i,triangles:u,centers:y,neighbors:l,polygons:d,mesh:M,hull:E,urquhart:q,find:c}}function x(e,t,n){e=b(e),t=b(t),n=b(n);const o=d(y(g(t,e),n));return _(v(m(e,t)).map(e=>o*e))}e.geoDelaunay=M,e.geoVoronoi=function(e){const t=function(e){return t.delaunay=null,t._data=e,"object"==typeof t._data&&"FeatureCollection"===t._data.type&&(t._data=t._data.features),"object"==typeof t._data&&(t.points=t._data.map(e=>[t._vx(e),t._vy(e)]),t.delaunay=M(t.points)),t};return t._vx=function(e){return"object"==typeof e&&"type"in e?n.geoCentroid(e)[0]:0 in e?e[0]:void 0},t._vy=function(e){return"object"==typeof e&&"type"in e?n.geoCentroid(e)[1]:1 in e?e[1]:void 0},t.x=function(e){return e?(t._vx=e,t):t._vx},t.y=function(e){return e?(t._vy=e,t):t._vy},t.polygons=function(e){return void 0!==e&&t(e),!!t.delaunay&&(0===t._data.length?null:1===t._data.length?{type:"Sphere"}:{type:"FeatureCollection",features:t.delaunay.polygons.map((e,n)=>({type:"Feature",geometry:e?{type:"Polygon",coordinates:[[...e,e[0]].map(e=>t.delaunay.centers[e])]}:null,properties:{site:t._data[n],sitecoordinates:t.points[n],neighbours:t.delaunay.neighbors[n]}}))})},t.triangles=function(e){return void 0!==e&&t(e),!!t.delaunay&&{type:"FeatureCollection",features:t.delaunay.triangles.map((e,n)=>((e=e.map(e=>t.points[e])).center=t.delaunay.centers[n],e)).filter(e=>j(e)>0).map(e=>({type:"Feature",properties:{circumcenter:e.center},geometry:{type:"Polygon",coordinates:[[...e,e[0]]]}}))}},t.links=function(e){if(void 0!==e&&t(e),!t.delaunay)return!1;const o=t.delaunay.edges.map(e=>n.geoDistance(t.points[e[0]],t.points[e[1]])),r=t.delaunay.urquhart(o);return{type:"FeatureCollection",features:t.delaunay.edges.map((e,n)=>({type:"Feature",properties:{source:t._data[e[0]],target:t._data[e[1]],length:o[n],urquhart:!!r[n]},geometry:{type:"LineString",coordinates:[t.points[e[0]],t.points[e[1]]]}}))}},t.mesh=function(e){return void 0!==e&&t(e),!!t.delaunay&&{type:"MultiLineString",coordinates:t.delaunay.edges.map(e=>[t.points[e[0]],t.points[e[1]]])}},t.cellMesh=function(e){if(void 0!==e&&t(e),!t.delaunay)return!1;const{centers:n,polygons:o}=t.delaunay,r=[];for(const e of o)if(e)for(let t=e.length,o=e[t-1],a=e[0],u=0;u<t;o=a,a=e[++u])a>o&&r.push([n[o],n[a]]);return{type:"MultiLineString",coordinates:r}},t._found=void 0,t.find=function(e,o,r){if(t._found=t.delaunay.find(e,o,t._found),!r||n.geoDistance([e,o],t.points[t._found])<r)return t._found},t.hull=function(e){void 0!==e&&t(e);const n=t.delaunay.hull,o=t.points;return 0===n.length?null:{type:"Polygon",coordinates:[[...n.map(e=>o[e]),o[n[0]]]]}},e?t(e):t},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
(function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-delaunay"),require("d3-geo"),require("d3-array")):"function"==typeof define&&define.amd?define(["exports","d3-delaunay","d3-geo","d3-array"],t):t(e.d3=e.d3||{},e.d3,e.d3,e.d3)})(this,function(e,t,n,o){"use strict";var r=Math.PI,a=r/2,u=180/r,i=r/180,l=Math.atan2,s=Math.cos,c=Math.max,f=Math.min,p=Math.sin,d=Math.sign||function(e){return e>0?1:e<0?-1:0},h=Math.sqrt;function y(e,t){return e[0]*t[0]+e[1]*t[1]+e[2]*t[2]}function g(e,t){return[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]]}function m(e,t){return[e[0]+t[0],e[1]+t[1],e[2]+t[2]]}function v(e){var t=h(e[0]*e[0]+e[1]*e[1]+e[2]*e[2]);return[e[0]/t,e[1]/t,e[2]/t]}function _(e){return[l(e[1],e[0])*u,(t=c(-1,f(1,e[2])),(t>1?a:t<-1?-a:Math.asin(t))*u)];var t}function b(e){var t=e[0]*i,n=e[1]*i,o=s(n);return[o*s(t),o*p(t),p(n)]}function j(e){return y((e=e.map(e=>b(e)))[0],g(e[2],e[1]))}function M(e){const a=function(e){if(e.length<2)return{};const o=n.geoRotation(e[0]),a=n.geoStereographic().translate([0,0]).scale(1).rotate(o.invert([180,0])),u=[0];let i=1;for(let t=1,n=(e=e.map(a)).length;t<n;t++){let n=e[t][0]*e[t][0]+e[t][1]*e[t][1];isNaN(n)&&u.push(t),n>i&&(i=n)}const l=1e6*h(i);u.forEach((t,n)=>e[n]=[l/2,0]);for(let t=0;t<4;t++)e.push([l*s(t/2*r),l*p(t/2*r)]);const c=t.Delaunay.from(e);return c.projection=a,c}(e),u=function(e,t){if(!e.triangles)return[];const n=e.triangles.slice().map(e=>e>=t?0:e),o=[];for(let e=0,t=n.length/3;e<t;e++){const t=n[3*e],r=n[3*e+1],a=n[3*e+2];t!==r&&r!==a&&a!==t&&o.push([t,a,r])}return o}(a,e.length),i=function(e,t){const n={};return 2===t.length?[[0,1]]:(e.forEach(e=>{if(!(j(e.map(e=>t[e]))<0))for(let t,r=0;r<3;r++)t=(r+1)%3,n[o.extent([e[r],e[t]]).join("-")]=!0}),Object.keys(n).map(e=>e.split("-").map(Number)))}(u,e),l=function(e,t){const n=[];return e.forEach((e,t)=>{for(let t=0;t<3;t++){const o=e[t],r=e[(t+1)%3];n[o]=n[o]||[],n[o].push(r)}}),0===e.length&&(2===t?(n[0]=[1],n[1]=[0]):1===t&&(n[0]=[])),n}(u,e.length),c=function(e,t){function n(e,t){let n=e[0]-t[0],o=e[1]-t[1],r=e[2]-t[2];return n*n+o*o+r*r}return function(o,r,a){void 0===a&&(a=0);let u,i,l=a;const s=b([o,r]);do{u=a,a=null,i=n(s,b(t[u])),e[u].forEach(e=>{let o=n(s,b(t[e]));if(o<i)return i=o,a=e,void(l=e)})}while(null!==a);return l}}(l,e),f=function(e,t){return e.map(e=>{const n=e.map(e=>t[e]).map(b);return _(v(m(m(g(n[1],n[0]),g(n[2],n[1])),g(n[0],n[2]))))})}(u,e),{polygons:d,centers:y}=function(e,t,n){const o=[],r=e.slice();if(0===t.length){if(n.length<2)return{polygons:o,centers:r};if(2===n.length){const e=b(n[0]),t=b(n[1]),u=v(m(e,t)),i=v(g(e,t)),l=g(u,i),s=[u,g(u,l),g(g(u,l),l),g(g(g(u,l),l),l)].map(_).map(a);return o.push(s),o.push(s.slice().reverse()),{polygons:o,centers:r}}}function a(e){let n=-1;return r.slice(t.length,1/0).forEach((o,r)=>{o[0]===e[0]&&o[1]===e[1]&&(n=r+t.length)}),n<0&&(n=r.length,r.push(e)),n}return t.forEach((e,t)=>{for(let n=0;n<3;n++){const r=e[n],a=e[(n+1)%3],u=e[(n+2)%3];o[r]=o[r]||[],o[r].push([a,u,t,[r,a,u]])}}),{polygons:o.map(e=>{const t=[e[0][2]];let o=e[0][1];for(let n=1;n<e.length;n++)for(let n=0;n<e.length;n++)if(e[n][0]==o){o=e[n][1],t.push(e[n][2]);break}if(t.length>2)return t;if(2==t.length){const o=x(n[e[0][3][0]],n[e[0][3][1]],r[t[0]]),u=x(n[e[0][3][2]],n[e[0][3][0]],r[t[0]]),i=a(o),l=a(u);return[t[0],l,t[1],i]}console&&console.warn({here:"unreachable",poly:e})}),centers:r}}(f,u,e),M=function(e){const t=[];return e.forEach(e=>{if(!e)return;let n=e[e.length-1];for(let o of e)o>n&&t.push([n,o]),n=o}),t}(d),E=function(e,t){const n={},o=[];e.map(e=>{if(!(j(e.map(e=>t[e>t.length?0:e]))<0))for(let t=0;t<3;t++){let o=[e[t],e[(t+1)%3]],r=`${o[1]}-${o[0]}`;n[r]?delete n[r]:n[o.join("-")]=!0}});const r={};let a;if(Object.keys(n).forEach(e=>{e=e.split("-").map(Number),r[e[0]]=e[1],a=e[0]}),void 0===a)return o;let u=a;do{o.push(u),u=r[u]}while(u!==a);return o}(u,e),q=function(e,t){return function(n){const r={},a={};return e.forEach((e,t)=>{const o=e.join("-");r[o]=n[t],a[o]=!0}),t.forEach(e=>{let t=0,n=-1;for(var u=0;u<3;u++){let a=o.extent([e[u],e[(u+1)%3]]).join("-");r[a]>t&&(t=r[a],n=a)}a[n]=!1}),e.map(e=>a[e.join("-")])}}(i,u);return{delaunay:a,edges:i,triangles:u,centers:y,neighbors:l,polygons:d,mesh:M,hull:E,urquhart:q,find:c}}function x(e,t,n){e=b(e),t=b(t),n=b(n);const o=d(y(g(t,e),n));return _(v(m(e,t)).map(e=>o*e))}e.geoDelaunay=M,e.geoVoronoi=function(e){const t=function(e){return t.delaunay=null,t._data=e,"object"==typeof t._data&&"FeatureCollection"===t._data.type&&(t._data=t._data.features),"object"==typeof t._data&&(t.points=t._data.map(e=>[t._vx(e),t._vy(e)]),t.delaunay=M(t.points)),t};return t._vx=function(e){return"object"==typeof e&&"type"in e?n.geoCentroid(e)[0]:0 in e?e[0]:void 0},t._vy=function(e){return"object"==typeof e&&"type"in e?n.geoCentroid(e)[1]:1 in e?e[1]:void 0},t.x=function(e){return e?(t._vx=e,t):t._vx},t.y=function(e){return e?(t._vy=e,t):t._vy},t.polygons=function(e){return void 0!==e&&t(e),!!t.delaunay&&(0===t._data.length?null:1===t._data.length?{type:"Sphere"}:{type:"FeatureCollection",features:t.delaunay.polygons.map((e,n)=>({type:"Feature",geometry:e?{type:"Polygon",coordinates:[[...e,e[0]].map(e=>t.delaunay.centers[e])]}:null,properties:{site:t._data[n],sitecoordinates:t.points[n],neighbours:t.delaunay.neighbors[n]}}))})},t.triangles=function(e){return void 0!==e&&t(e),!!t.delaunay&&{type:"FeatureCollection",features:t.delaunay.triangles.map((e,n)=>((e=e.map(e=>t.points[e])).center=t.delaunay.centers[n],e)).filter(e=>j(e)>0).map(e=>({type:"Feature",properties:{circumcenter:e.center},geometry:{type:"Polygon",coordinates:[[...e,e[0]]]}}))}},t.links=function(e){if(void 0!==e&&t(e),!t.delaunay)return!1;const o=t.delaunay.edges.map(e=>n.geoDistance(t.points[e[0]],t.points[e[1]])),r=t.delaunay.urquhart(o);return{type:"FeatureCollection",features:t.delaunay.edges.map((e,n)=>({type:"Feature",properties:{source:t._data[e[0]],target:t._data[e[1]],length:o[n],urquhart:!!r[n]},geometry:{type:"LineString",coordinates:[t.points[e[0]],t.points[e[1]]]}}))}},t.mesh=function(e){return void 0!==e&&t(e),!!t.delaunay&&{type:"MultiLineString",coordinates:t.delaunay.edges.map(e=>[t.points[e[0]],t.points[e[1]]])}},t.cellMesh=function(e){if(void 0!==e&&t(e),!t.delaunay)return!1;const{centers:n,polygons:o}=t.delaunay,r=[];for(const e of o)if(e)for(let t=e.length,o=e[t-1],a=e[0],u=0;u<t;o=a,a=e[++u])a>o&&r.push([n[o],n[a]]);return{type:"MultiLineString",coordinates:r}},t._found=void 0,t.find=function(e,o,r){if(t._found=t.delaunay.find(e,o,t._found),!r||n.geoDistance([e,o],t.points[t._found])<r)return t._found},t.hull=function(e){void 0!==e&&t(e);const n=t.delaunay.hull,o=t.points;return 0===n.length?null:{type:"Polygon",coordinates:[[...n.map(e=>o[e]),o[n[0]]]]}},e?t(e):t},Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-geo-voronoi", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Spherical Voronoi Diagram and Delaunay Triangulation", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,8 +7,10 @@ # d3-geo-voronoi | ||
The module offers two APIs. The legacy API is based on GeoJSON and follows as closely as possible the API of the [d3-voronoi](https://github.com/d3/d3-voronoi/) module. It is available with *d3.geoVoronoi()*. | ||
The module offers two APIs. | ||
A lighter API is available with *d3.geoDelaunay()*. It offers the same contents, but with a different presentation, where every vertex, edge, polygon… is referenced by an id rather than by its coordinates. This allows a more compact representation in memory and eases topology computations, but is a bit less convenient for drawing the results on a map. | ||
The GeoJSON API is the most convenient for drawing the results on a map. It follows as closely as possible the API of the [d3-voronoi](https://github.com/d3/d3-voronoi/) module. It is available with *d3.geoVoronoi()*. | ||
A lighter API is available with *d3.geoDelaunay()*. It offers the same contents, but with a different presentation, where every vertex, edge, polygon… is referenced by an id rather than by its coordinates. This allows a more compact representation in memory and eases topology computations. | ||
## Installing | ||
@@ -15,0 +17,0 @@ |
@@ -9,3 +9,3 @@ // | ||
import { Delaunay } from "d3-delaunay"; | ||
import { geoDistance, geoRotation, geoStereographic } from "d3-geo"; | ||
import { geoRotation, geoStereographic } from "d3-geo"; | ||
import { extent } from "d3-array"; | ||
@@ -82,2 +82,9 @@ import { | ||
function geo_find(neighbors, points) { | ||
function distance2(a,b) { | ||
let x = a[0] - b[0], | ||
y = a[1] - b[1], | ||
z = a[2] - b[2]; | ||
return x * x + y * y + z * z; | ||
} | ||
return function find(x, y, next) { | ||
@@ -88,8 +95,9 @@ if (next === undefined) next = 0; | ||
found = next; | ||
const xyz = cartesian([x, y]); | ||
do { | ||
cell = next; | ||
next = null; | ||
dist = geoDistance([x, y], points[cell]); | ||
dist = distance2(xyz, cartesian(points[cell])); | ||
neighbors[cell].forEach(i => { | ||
let ndist = geoDistance([x, y], points[i]); | ||
let ndist = distance2(xyz, cartesian(points[i])); | ||
if (ndist < dist) { | ||
@@ -96,0 +104,0 @@ dist = ndist; |
101592
2204
163